1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.inputmethod;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.util.FeatureFlagUtils;
22 
23 import androidx.fragment.app.Fragment;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.core.BasePreferenceController;
27 import com.android.settings.overlay.FeatureFactory;
28 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
29 import com.android.settingslib.widget.ButtonPreference;
30 
31 public class TouchGesturesButtonPreferenceController extends BasePreferenceController {
32 
33     private static final int ORDER_TOP = 0;
34     private static final int ORDER_BOTTOM = 100;
35     private static final String PREFERENCE_KEY = "trackpad_touch_gesture";
36     private static final String GESTURE_DIALOG_TAG = "GESTURE_DIALOG_TAG";
37 
38     private Fragment mParent;
39     private MetricsFeatureProvider mMetricsFeatureProvider;
40 
TouchGesturesButtonPreferenceController(Context context, String key)41     public TouchGesturesButtonPreferenceController(Context context, String key) {
42         super(context, key);
43         mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
44     }
45 
setFragment(Fragment parent)46     public void setFragment(Fragment parent) {
47         mParent = parent;
48     }
49 
50     @Override
displayPreference(PreferenceScreen screen)51     public void displayPreference(PreferenceScreen screen) {
52         super.displayPreference(screen);
53         ButtonPreference buttonPreference =
54                 (ButtonPreference) screen.findPreference(getPreferenceKey());
55         boolean touchGestureDeveloperMode = FeatureFlagUtils
56                 .isEnabled(mContext, FeatureFlagUtils.SETTINGS_NEW_KEYBOARD_TRACKPAD_GESTURE);
57         if (getPreferenceKey().equals(PREFERENCE_KEY)) {
58             if (touchGestureDeveloperMode) {
59                 buttonPreference.setOrder(ORDER_TOP);
60             } else {
61                 buttonPreference.setOrder(ORDER_BOTTOM);
62             }
63         }
64         buttonPreference.setOnClickListener(v -> {
65             showTouchpadGestureEducation();
66         });
67     }
68 
69     @Override
getAvailabilityStatus()70     public int getAvailabilityStatus() {
71         return AVAILABLE;
72     }
73 
showTouchpadGestureEducation()74     private void showTouchpadGestureEducation() {
75         mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_LEARN_TOUCHPAD_GESTURE_CLICK);
76         TrackpadGestureDialogFragment fragment = new TrackpadGestureDialogFragment();
77         fragment.setTargetFragment(mParent, 0);
78         fragment.show(mParent.getActivity().getSupportFragmentManager(), GESTURE_DIALOG_TAG);
79     }
80 }
81