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.content.Context; 20 import android.hardware.input.InputManager; 21 import android.util.FeatureFlagUtils; 22 23 import androidx.annotation.NonNull; 24 import androidx.annotation.Nullable; 25 import androidx.preference.Preference; 26 27 import com.android.settings.core.BasePreferenceController; 28 import com.android.settings.core.PreferenceControllerMixin; 29 import com.android.settings.keyboard.Flags; 30 import com.android.settingslib.core.lifecycle.LifecycleObserver; 31 import com.android.settingslib.core.lifecycle.events.OnStart; 32 import com.android.settingslib.core.lifecycle.events.OnStop; 33 34 public class TouchpadAndMouseSettingsController extends BasePreferenceController 35 implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop, 36 InputManager.InputDeviceListener { 37 38 private final InputManager mIm; 39 40 @Nullable 41 private Preference mPreference; 42 TouchpadAndMouseSettingsController(@onNull Context context, @NonNull String key)43 public TouchpadAndMouseSettingsController(@NonNull Context context, @NonNull String key) { 44 super(context, key); 45 mIm = context.getSystemService(InputManager.class); 46 } 47 48 @Override onInputDeviceAdded(int deviceId)49 public void onInputDeviceAdded(int deviceId) { 50 updateEntry(); 51 } 52 53 @Override onInputDeviceRemoved(int deviceId)54 public void onInputDeviceRemoved(int deviceId) { 55 updateEntry(); 56 } 57 58 @Override onInputDeviceChanged(int deviceId)59 public void onInputDeviceChanged(int deviceId) { 60 updateEntry(); 61 } 62 63 @Override onStart()64 public void onStart() { 65 mIm.registerInputDeviceListener(this, null); 66 } 67 68 @Override onStop()69 public void onStop() { 70 mIm.unregisterInputDeviceListener(this); 71 } 72 73 @Override updateState(Preference preference)74 public void updateState(Preference preference) { 75 mPreference = preference; 76 updateEntry(); 77 } 78 updateEntry()79 private void updateEntry() { 80 if (mPreference == null) { 81 return; 82 } 83 mPreference.setVisible(isAvailable()); 84 mPreference.setTitle(InputPeripheralsSettingsUtils.getTouchpadAndMouseTitleTitleResId()); 85 } 86 87 @Override getAvailabilityStatus()88 public int getAvailabilityStatus() { 89 boolean isNewPageFlagDisabled = !Flags.keyboardAndTouchpadA11yNewPageEnabled(); 90 boolean isFeatureOn = FeatureFlagUtils 91 .isEnabled(mContext, FeatureFlagUtils.SETTINGS_NEW_KEYBOARD_TRACKPAD); 92 boolean isTouchpad = InputPeripheralsSettingsUtils.isTouchpad(); 93 boolean isPointerCustomizationEnabled = 94 android.view.flags.Flags.enableVectorCursorA11ySettings(); 95 boolean isMouse = InputPeripheralsSettingsUtils.isMouse(); 96 return ((isFeatureOn && isTouchpad) || (isPointerCustomizationEnabled && isMouse)) 97 && isNewPageFlagDisabled ? AVAILABLE 98 : CONDITIONALLY_UNAVAILABLE; 99 } 100 } 101