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.preference.Preference;
24 
25 import com.android.settings.core.BasePreferenceController;
26 import com.android.settings.core.PreferenceControllerMixin;
27 import com.android.settingslib.core.lifecycle.LifecycleObserver;
28 import com.android.settingslib.core.lifecycle.events.OnStart;
29 import com.android.settingslib.core.lifecycle.events.OnStop;
30 
31 public class TrackpadSettingsController extends BasePreferenceController
32         implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop,
33         InputManager.InputDeviceListener {
34 
35     private final InputManager mIm;
36 
37     private Preference mPreference;
38 
TrackpadSettingsController(Context context, String key)39     public TrackpadSettingsController(Context context, String key) {
40         super(context, key);
41         mIm = context.getSystemService(InputManager.class);
42     }
43 
44     @Override
onInputDeviceAdded(int deviceId)45     public void onInputDeviceAdded(int deviceId) {
46         updateEntry();
47     }
48 
49     @Override
onInputDeviceRemoved(int deviceId)50     public void onInputDeviceRemoved(int deviceId) {
51         updateEntry();
52     }
53 
54     @Override
onInputDeviceChanged(int deviceId)55     public void onInputDeviceChanged(int deviceId) {
56         updateEntry();
57     }
58 
59     @Override
onStart()60     public void onStart() {
61         mIm.registerInputDeviceListener(this, null);
62     }
63 
64     @Override
onStop()65     public void onStop() {
66         mIm.unregisterInputDeviceListener(this);
67     }
68 
69     @Override
updateState(Preference preference)70     public void updateState(Preference preference) {
71         mPreference = preference;
72         updateEntry();
73     }
74 
updateEntry()75     private void updateEntry() {
76         if (mPreference == null) {
77             return;
78         }
79         mPreference.setVisible(isAvailable());
80     }
81 
82     @Override
getAvailabilityStatus()83     public int getAvailabilityStatus() {
84         boolean isFeatureOn = FeatureFlagUtils
85                 .isEnabled(mContext, FeatureFlagUtils.SETTINGS_NEW_KEYBOARD_TRACKPAD);
86         boolean isTouchpad = NewKeyboardSettingsUtils.isTouchpad();
87         return (isFeatureOn && isTouchpad) ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
88     }
89 }
90