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 static com.android.systemui.shared.Flags.newTouchpadGesturesTutorial; 20 21 import android.app.settings.SettingsEnums; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.hardware.input.InputSettings; 25 import android.os.UserHandle; 26 27 import androidx.annotation.NonNull; 28 import androidx.annotation.Nullable; 29 import androidx.fragment.app.Fragment; 30 import androidx.preference.PreferenceScreen; 31 32 import com.android.settings.Utils; 33 import com.android.settings.core.BasePreferenceController; 34 import com.android.settings.overlay.FeatureFactory; 35 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 36 import com.android.settingslib.widget.ButtonPreference; 37 38 public class TouchpadGesturesTutorialButtonPreferenceController extends BasePreferenceController { 39 40 private static final int ORDER_BOTTOM = 100; 41 private static final String PREFERENCE_KEY = "touchpad_touch_gesture"; 42 private static final String GESTURE_DIALOG_TAG = "GESTURE_DIALOG_TAG"; 43 private static final String TUTORIAL_ACTION = "com.android.systemui.action.TOUCHPAD_TUTORIAL"; 44 45 private @Nullable Fragment mParent; 46 private final MetricsFeatureProvider mMetricsFeatureProvider; 47 TouchpadGesturesTutorialButtonPreferenceController(@onNull Context context, @NonNull String key)48 public TouchpadGesturesTutorialButtonPreferenceController(@NonNull Context context, 49 @NonNull String key) { 50 super(context, key); 51 mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider(); 52 } 53 setFragment(Fragment parent)54 public void setFragment(Fragment parent) { 55 mParent = parent; 56 } 57 58 @Override displayPreference(PreferenceScreen screen)59 public void displayPreference(PreferenceScreen screen) { 60 super.displayPreference(screen); 61 ButtonPreference buttonPreference = 62 (ButtonPreference) screen.findPreference(getPreferenceKey()); 63 if (getPreferenceKey().equals(PREFERENCE_KEY)) { 64 buttonPreference.setOrder(ORDER_BOTTOM); 65 } 66 buttonPreference.setOnClickListener(v -> { 67 showTouchpadGestureEducation(); 68 }); 69 } 70 71 @Override getAvailabilityStatus()72 public int getAvailabilityStatus() { 73 boolean isTouchpad = InputPeripheralsSettingsUtils.isTouchpad(); 74 if (isTouchpad) { 75 // If the user's disabled touchpad system gestures in the accessibility settings, the 76 // tutorial won't work or be relevant, so disable the button. 77 return InputSettings.useTouchpadSystemGestures(mContext) ? AVAILABLE 78 : DISABLED_DEPENDENT_SETTING; 79 } else { 80 return CONDITIONALLY_UNAVAILABLE; 81 } 82 } 83 showTouchpadGestureEducation()84 private void showTouchpadGestureEducation() { 85 mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_LEARN_TOUCHPAD_GESTURE_CLICK); 86 if (newTouchpadGesturesTutorial()) { 87 Intent intent = new Intent(TUTORIAL_ACTION) 88 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 89 .setPackage(Utils.SYSTEMUI_PACKAGE_NAME); 90 // touchpad tutorial must be started as system user as it needs to have access to state 91 // of user 0 sysui instance 92 mContext.startActivityAsUser(intent, UserHandle.SYSTEM); 93 } else { 94 TouchpadGestureDialogFragment fragment = new TouchpadGestureDialogFragment(); 95 fragment.setTargetFragment(mParent, 0); 96 fragment.show(mParent.getActivity().getSupportFragmentManager(), GESTURE_DIALOG_TAG); 97 } 98 } 99 } 100