• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2025 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.hardware.input.InputManager;
20 import android.os.Bundle;
21 
22 import androidx.annotation.NonNull;
23 
24 import com.android.internal.util.Preconditions;
25 import com.android.settings.dashboard.DashboardFragment;
26 
27 public abstract class InputDeviceDashboardFragment extends DashboardFragment
28         implements InputManager.InputDeviceListener {
29     private InputManager mInputManager;
30 
31     @Override
onCreate(@onNull Bundle icicle)32     public void onCreate(@NonNull Bundle icicle) {
33         super.onCreate(icicle);
34         mInputManager = Preconditions.checkNotNull(getActivity()
35                 .getSystemService(InputManager.class));
36     }
37 
38     @Override
onResume()39     public void onResume() {
40         super.onResume();
41         finishEarlyIfNeeded();
42         mInputManager.registerInputDeviceListener(this /* listener */, null /* handler */);
43     }
44 
45     @Override
onPause()46     public void onPause() {
47         super.onPause();
48         mInputManager.unregisterInputDeviceListener(this /* listener */);
49     }
50 
51     @Override
onInputDeviceAdded(int deviceId)52     public void onInputDeviceAdded(int deviceId) {
53         finishEarlyIfNeeded();
54     }
55 
56     @Override
onInputDeviceRemoved(int deviceId)57     public void onInputDeviceRemoved(int deviceId) {
58         finishEarlyIfNeeded();
59     }
60 
61     @Override
onInputDeviceChanged(int deviceId)62     public void onInputDeviceChanged(int deviceId) {
63         finishEarlyIfNeeded();
64     }
65 
finishEarlyIfNeeded()66     private void finishEarlyIfNeeded() {
67         if (getActivity() == null) {
68             return;
69         }
70         if (needToFinishEarly()) {
71             getActivity().finish();
72         }
73     }
74 
75     /**
76      * Returns whether the fragment should still be displayed given the input devices that are
77      * currently connected.
78      */
needToFinishEarly()79     protected abstract boolean needToFinishEarly();
80 
isTouchpadDetached()81     protected static boolean isTouchpadDetached() {
82         return !InputPeripheralsSettingsUtils.isTouchpad();
83     }
84 
isMouseDetached()85     protected static boolean isMouseDetached() {
86         return !InputPeripheralsSettingsUtils.isMouse();
87     }
88 
isHardKeyboardDetached()89     protected static boolean isHardKeyboardDetached() {
90         return !InputPeripheralsSettingsUtils.isHardKeyboard();
91     }
92 }
93