KeyboardLayoutPickerFragment2.java revision 5f0b59babfbfe483855b294098613f8d0fc2f9b4
1/*
2 * Copyright (C) 2012 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
17package com.android.settings.inputmethod;
18
19import android.app.Activity;
20import android.hardware.input.InputDeviceIdentifier;
21import android.hardware.input.InputManager;
22import android.hardware.input.InputManager.InputDeviceListener;
23import android.hardware.input.KeyboardLayout;
24import android.os.Bundle;
25import android.support.v7.preference.Preference;
26import android.support.v7.preference.PreferenceScreen;
27import android.view.InputDevice;
28
29import android.view.inputmethod.InputMethodInfo;
30import android.view.inputmethod.InputMethodSubtype;
31import com.android.internal.logging.MetricsLogger;
32import com.android.internal.util.Preconditions;
33import com.android.settings.R;
34import com.android.settings.SettingsPreferenceFragment;
35
36import java.util.Arrays;
37import java.util.HashMap;
38import java.util.Map;
39
40public final class KeyboardLayoutPickerFragment2 extends SettingsPreferenceFragment
41        implements InputDeviceListener {
42
43    private InputDeviceIdentifier mInputDeviceIdentifier;
44    private int mInputDeviceId = -1;
45    private InputManager mIm;
46    private InputMethodInfo mImi;
47    private InputMethodSubtype mSubtype;
48    private KeyboardLayout[] mKeyboardLayouts;
49    private Map<Preference, KeyboardLayout> mPreferenceMap = new HashMap<>();
50
51    // TODO: Make these constants public API for b/25752827
52
53    /**
54     * Intent extra: The input device descriptor of the keyboard whose keyboard
55     * layout is to be changed.
56     */
57    public static final String EXTRA_INPUT_DEVICE_IDENTIFIER = "input_device_identifier";
58
59    /**
60     * Intent extra: The associated {@link InputMethodInfo}.
61     */
62    public static final String EXTRA_INPUT_METHOD_INFO = "input_method_info";
63
64    /**
65     * Intent extra: The associated {@link InputMethodSubtype}.
66     */
67    public static final String EXTRA_INPUT_METHOD_SUBTYPE = "input_method_subtype";
68
69    @Override
70    protected int getMetricsCategory() {
71        return MetricsLogger.INPUTMETHOD_KEYBOARD;
72    }
73
74    @Override
75    public void onCreate(Bundle icicle) {
76        super.onCreate(icicle);
77        Activity activity = Preconditions.checkNotNull(getActivity());
78
79        mInputDeviceIdentifier = activity.getIntent().getParcelableExtra(
80                EXTRA_INPUT_DEVICE_IDENTIFIER);
81        mImi = activity.getIntent().getParcelableExtra(EXTRA_INPUT_METHOD_INFO);
82        mSubtype = activity.getIntent().getParcelableExtra(EXTRA_INPUT_METHOD_SUBTYPE);
83
84        if (mInputDeviceIdentifier == null || mImi == null || mSubtype == null) {
85            activity.finish();
86        }
87
88        mIm = activity.getSystemService(InputManager.class);
89        mKeyboardLayouts = mIm.getKeyboardLayoutsForInputDevice(mInputDeviceIdentifier);
90        Arrays.sort(mKeyboardLayouts);
91        setPreferenceScreen(createPreferenceHierarchy());
92    }
93
94    @Override
95    public void onResume() {
96        super.onResume();
97
98        mIm.registerInputDeviceListener(this, null);
99
100        InputDevice inputDevice =
101                mIm.getInputDeviceByDescriptor(mInputDeviceIdentifier.getDescriptor());
102        if (inputDevice == null) {
103            getActivity().finish();
104            return;
105        }
106        mInputDeviceId = inputDevice.getId();
107    }
108
109    @Override
110    public void onPause() {
111        mIm.unregisterInputDeviceListener(this);
112        mInputDeviceId = -1;
113
114        super.onPause();
115    }
116
117    @Override
118    public boolean onPreferenceTreeClick(Preference preference) {
119        KeyboardLayout layout = mPreferenceMap.get(preference);
120        if (layout != null) {
121            mIm.setKeyboardLayoutForInputDevice(mInputDeviceIdentifier, mImi, mSubtype,
122                    layout.getDescriptor());
123            getActivity().finish();
124            return true;
125        }
126        return super.onPreferenceTreeClick(preference);
127    }
128
129    @Override
130    public void onInputDeviceAdded(int deviceId) {}
131
132    @Override
133    public void onInputDeviceChanged(int deviceId) {}
134
135    @Override
136    public void onInputDeviceRemoved(int deviceId) {
137        if (mInputDeviceId >= 0 && deviceId == mInputDeviceId) {
138            getActivity().finish();
139        }
140    }
141
142    private PreferenceScreen createPreferenceHierarchy() {
143        PreferenceScreen root = getPreferenceManager().createPreferenceScreen(getActivity());
144
145        for (KeyboardLayout layout : mKeyboardLayouts) {
146            Preference pref = new Preference(getPrefContext());
147            pref.setTitle(layout.getLabel());
148            root.addPreference(pref);
149            mPreferenceMap.put(pref, layout);
150        }
151
152        root.setTitle(PhysicalKeyboardFragment.getDisplayName(getContext(), mImi, mSubtype));
153        return root;
154    }
155}
156