• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 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 package com.android.settings.inputmethod;
17 
18 import static android.hardware.input.InputGestureData.TOUCHPAD_GESTURE_TYPE_THREE_FINGER_TAP;
19 import static android.hardware.input.InputGestureData.createTouchpadTrigger;
20 
21 import android.content.Context;
22 import android.hardware.input.InputGestureData;
23 import android.hardware.input.InputManager;
24 import android.hardware.input.KeyGestureEvent;
25 import android.os.UserHandle;
26 import android.provider.Settings;
27 import android.util.AttributeSet;
28 import android.view.PointerIcon;
29 import android.widget.LinearLayout;
30 import android.widget.RadioButton;
31 
32 import androidx.annotation.NonNull;
33 import androidx.annotation.Nullable;
34 import androidx.preference.Preference;
35 import androidx.preference.PreferenceViewHolder;
36 
37 import com.android.settings.R;
38 
39 public class TouchpadThreeFingerTapSelector extends Preference {
40     private static final InputGestureData.Trigger THREE_FINGER_TAP_TOUCHPAD_TRIGGER =
41             createTouchpadTrigger(TOUCHPAD_GESTURE_TYPE_THREE_FINGER_TAP);
42     private final InputManager mInputManager;
43 
TouchpadThreeFingerTapSelector(@onNull Context context, @Nullable AttributeSet attrs)44     public TouchpadThreeFingerTapSelector(@NonNull Context context, @Nullable AttributeSet attrs) {
45         super(context, attrs);
46         setLayoutResource(R.layout.touchpad_three_finger_tap_layout);
47         mInputManager = context.getSystemService(InputManager.class);
48         setSelectable(false);
49     }
50 
51     @Override
onBindViewHolder(@onNull PreferenceViewHolder holder)52     public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
53         super.onBindViewHolder(holder);
54 
55         LinearLayout buttonHolder = (LinearLayout) holder.findViewById(R.id.button_holder);
56         // Intercept hover events so setting row does not highlight when hovering buttons.
57         buttonHolder.setOnHoverListener((v, e) -> true);
58 
59         int currentCustomization = Settings.System.getIntForUser(getContext().getContentResolver(),
60                 Settings.System.TOUCHPAD_THREE_FINGER_TAP_CUSTOMIZATION,
61                 KeyGestureEvent.KEY_GESTURE_TYPE_UNSPECIFIED, UserHandle.USER_CURRENT);
62         initRadioButton(holder, R.id.launch_gemini,
63                 KeyGestureEvent.KEY_GESTURE_TYPE_LAUNCH_ASSISTANT, currentCustomization);
64         initRadioButton(holder, R.id.go_home, KeyGestureEvent.KEY_GESTURE_TYPE_HOME,
65                 currentCustomization);
66         initRadioButton(holder, R.id.go_back, KeyGestureEvent.KEY_GESTURE_TYPE_BACK,
67                 currentCustomization);
68         initRadioButton(holder, R.id.recent_apps, KeyGestureEvent.KEY_GESTURE_TYPE_RECENT_APPS,
69                 currentCustomization);
70         initRadioButton(holder, R.id.middle_click,
71                 KeyGestureEvent.KEY_GESTURE_TYPE_UNSPECIFIED, currentCustomization);
72     }
73 
initRadioButton(@onNull PreferenceViewHolder holder, int id, int customGestureType, int currentCustomization)74     private void initRadioButton(@NonNull PreferenceViewHolder holder, int id,
75             int customGestureType, int currentCustomization) {
76         RadioButton radioButton = (RadioButton) holder.findViewById(id);
77         if (radioButton == null) {
78             return;
79         }
80         boolean isUnspecified = customGestureType == KeyGestureEvent.KEY_GESTURE_TYPE_UNSPECIFIED;
81         InputGestureData gesture = isUnspecified ? null : new InputGestureData.Builder()
82                 .setTrigger(THREE_FINGER_TAP_TOUCHPAD_TRIGGER)
83                 .setKeyGestureType(customGestureType)
84                 .build();
85         radioButton.setOnCheckedChangeListener((v, isChecked) -> {
86             if (isChecked) {
87                 mInputManager.removeAllCustomInputGestures(InputGestureData.Filter.TOUCHPAD);
88                 if (!isUnspecified) {
89                     mInputManager.addCustomInputGesture(gesture);
90                 }
91                 Settings.System.putIntForUser(getContext().getContentResolver(),
92                         Settings.System.TOUCHPAD_THREE_FINGER_TAP_CUSTOMIZATION, customGestureType,
93                         UserHandle.USER_CURRENT);
94             }
95         });
96         radioButton.setChecked(currentCustomization == customGestureType);
97         radioButton.setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_ARROW));
98     }
99 }
100