OSDN Git Service

Merge "Fix search indexing for encryption_and_credential page" into oc-dr1-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / inputmethod / PhysicalKeyboardPreferenceController.java
1 /*
2  * Copyright (C) 2017 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.support.v7.preference.Preference;
22
23 import com.android.settings.R;
24 import com.android.settings.core.PreferenceController;
25 import com.android.settings.inputmethod.PhysicalKeyboardFragment.HardKeyboardDeviceInfo;
26 import com.android.settingslib.core.lifecycle.Lifecycle;
27 import com.android.settingslib.core.lifecycle.LifecycleObserver;
28 import com.android.settingslib.core.lifecycle.events.OnPause;
29 import com.android.settingslib.core.lifecycle.events.OnResume;
30
31 import java.util.List;
32
33 public class PhysicalKeyboardPreferenceController extends PreferenceController implements
34         LifecycleObserver, OnResume, OnPause, InputManager.InputDeviceListener {
35
36     private final InputManager mIm;
37
38     private Preference mPreference;
39
40     public PhysicalKeyboardPreferenceController(Context context, Lifecycle lifecycle) {
41         super(context);
42         mIm = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
43
44         if (lifecycle != null) {
45             lifecycle.addObserver(this);
46         }
47     }
48
49     @Override
50     public boolean isAvailable() {
51         return true;
52     }
53
54     @Override
55     public void updateState(Preference preference) {
56         mPreference = preference;
57         updateSummary();
58     }
59
60     @Override
61     public String getPreferenceKey() {
62         return "physical_keyboard_pref";
63     }
64
65     @Override
66     public void onPause() {
67         mIm.registerInputDeviceListener(this, null);
68     }
69
70     @Override
71     public void onResume() {
72         mIm.unregisterInputDeviceListener(this);
73     }
74
75     @Override
76     public void onInputDeviceAdded(int deviceId) {
77         updateSummary();
78     }
79
80     @Override
81     public void onInputDeviceRemoved(int deviceId) {
82         updateSummary();
83     }
84
85     @Override
86     public void onInputDeviceChanged(int deviceId) {
87         updateSummary();
88     }
89
90     private void updateSummary() {
91         if (mPreference == null) {
92             return;
93         }
94         final List<HardKeyboardDeviceInfo> keyboards =
95                 PhysicalKeyboardFragment.getHardKeyboards();
96         if (keyboards.isEmpty()) {
97             mPreference.setSummary(R.string.disconnected);
98             return;
99         }
100         String summary = null;
101         for (HardKeyboardDeviceInfo info : keyboards) {
102             if (summary == null) {
103                 summary = info.mDeviceName;
104             } else {
105                 summary = mContext.getString(R.string.join_many_items_middle, summary,
106                         info.mDeviceName);
107             }
108         }
109         mPreference.setSummary(summary);
110     }
111 }