Skip to content

Commit 21f8ddb

Browse files
committed
Add InputMethodSettingsFragment to a static library
Bug: 4979539 Change-Id: I4a019f3578c3f0da0086bf3d27ddcf55c2d1b57f
1 parent 55f2b58 commit 21f8ddb

2 files changed

Lines changed: 221 additions & 0 deletions

File tree

Android.mk

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright (C) 2011 The Android Open Source Project
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
LOCAL_PATH := $(call my-dir)
16+
17+
include $(CLEAR_VARS)
18+
LOCAL_MODULE := inputmethod-common
19+
# TODO: Change a sdk version to 14 once the framework gets ready
20+
LOCAL_SDK_VERSION := 13
21+
LOCAL_SRC_FILES := \
22+
$(call all-java-files-under, java)
23+
include $(BUILD_STATIC_JAVA_LIBRARY)
24+
25+
# Build the test package
26+
include $(call all-makefiles-under, $(LOCAL_PATH))
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* Copyright (C) 2011 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+
package com.android.inputmethodcommon;
17+
18+
import android.content.Context;
19+
import android.graphics.drawable.Drawable;
20+
import android.os.Bundle;
21+
import android.preference.Preference;
22+
import android.preference.Preference.OnPreferenceClickListener;
23+
import android.preference.PreferenceCategory;
24+
import android.preference.PreferenceFragment;
25+
import android.preference.PreferenceScreen;
26+
import android.provider.Settings;
27+
import android.text.TextUtils;
28+
import android.view.inputmethod.InputMethodInfo;
29+
import android.view.inputmethod.InputMethodManager;
30+
import android.view.ViewGroup;
31+
32+
import java.util.List;
33+
34+
/**
35+
* This is a helper class for an IME's settings preference fragment. It's recommended for every
36+
* IME to have its own settings preference fragment which inherits this class.
37+
*/
38+
public abstract class InputMethodSettingsFragment extends PreferenceFragment {
39+
private PreferenceCategory mInputMethodSettingsCategory;
40+
private Preference mSubtypeEnablerPreference;
41+
private int mInputMethodSettingsCategoryTitleRes;
42+
private CharSequence mInputMethodSettingsCategoryTitle;
43+
private int mSubtypeEnablerTitleRes;
44+
private CharSequence mSubtypeEnablerTitle;
45+
private int mSubtypeEnablerSummaryRes;
46+
private CharSequence mSubtypeEnablerSummary;
47+
private int mSubtypeEnablerIconRes;
48+
private Drawable mSubtypeEnablerIcon;
49+
50+
@Override
51+
public void onCreate(Bundle savedInstanceState) {
52+
super.onCreate(savedInstanceState);
53+
final Context context = getActivity();
54+
final InputMethodManager imm =
55+
(InputMethodManager) context.getSystemService(
56+
Context.INPUT_METHOD_SERVICE);
57+
58+
final InputMethodInfo imi = getMyImi(imm);
59+
if (imi != null && imi.getSubtypeCount() > 1) {
60+
mInputMethodSettingsCategory = new PreferenceCategory(context);
61+
mSubtypeEnablerPreference =
62+
getPreferenceManager().createPreferenceScreen(context);
63+
mSubtypeEnablerPreference
64+
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
65+
@Override
66+
public boolean onPreferenceClick(Preference preference) {
67+
final Bundle bundle = new Bundle();
68+
bundle.putString(Settings.EXTRA_INPUT_METHOD_ID, imi.getId());
69+
return true;
70+
}
71+
});
72+
mInputMethodSettingsCategory.addPreference(mSubtypeEnablerPreference);
73+
getPreferenceScreen().addPreference(mInputMethodSettingsCategory);
74+
}
75+
updateSubtypeEnabler();
76+
}
77+
78+
private InputMethodInfo getMyImi(InputMethodManager imm) {
79+
final List<InputMethodInfo> imis = imm.getInputMethodList();
80+
for (int i = 0; i < imis.size(); ++i) {
81+
final InputMethodInfo imi = imis.get(i);
82+
if (imis.get(i).getPackageName().equals(getActivity().getPackageName())) {
83+
return imi;
84+
}
85+
}
86+
return null;
87+
}
88+
89+
/**
90+
* Sets the title for the input method settings category with a resource ID.
91+
* @param resId The resource ID of the title.
92+
*/
93+
public void setInputMethodSettingsCategoryTitle(int resId) {
94+
mInputMethodSettingsCategoryTitleRes = resId;
95+
updateSubtypeEnabler();
96+
}
97+
98+
/**
99+
* Sets the title for the input method settings category with a CharSequence.
100+
* @param title The title for this preference.
101+
*/
102+
public void setInputMethodSettingsCategoryTitle(CharSequence title) {
103+
mInputMethodSettingsCategoryTitleRes = 0;
104+
mInputMethodSettingsCategoryTitle = title;
105+
updateSubtypeEnabler();
106+
}
107+
108+
/**
109+
* Sets the title for the input method enabler preference for launching subtype enabler with a
110+
* resource ID.
111+
* @param resId The resource ID of the title.
112+
*/
113+
public void setSubtypeEnablerTitle(int resId) {
114+
mSubtypeEnablerTitleRes = resId;
115+
updateSubtypeEnabler();
116+
}
117+
118+
/**
119+
* Sets the title for the input method enabler preference for launching subtype enabler with a
120+
* CharSequence.
121+
* @param title The title for this preference.
122+
*/
123+
public void setSubtypeEnablerTitle(CharSequence title) {
124+
mSubtypeEnablerTitleRes = 0;
125+
mSubtypeEnablerTitle = title;
126+
updateSubtypeEnabler();
127+
}
128+
129+
/**
130+
* Sets the summary for the inputmethod enabler preference for launching subtype enabler with a
131+
* resource ID.
132+
* @param resId The resource id of the summary for the preference.
133+
*/
134+
public void setSubtypeEnablerSummary(int resId) {
135+
mSubtypeEnablerSummaryRes = resId;
136+
updateSubtypeEnabler();
137+
}
138+
139+
/**
140+
* Sets the summary for the inputmethod enabler preference for launching subtype enabler with a
141+
* CharSequence.
142+
* @param summary The summary of the preference.
143+
*/
144+
public void setSubtypeEnablerSummary(CharSequence summary) {
145+
mSubtypeEnablerSummaryRes = 0;
146+
mSubtypeEnablerSummary = summary;
147+
updateSubtypeEnabler();
148+
}
149+
150+
/**
151+
* Sets the icon for the preference for launching subtype enabler with a resource ID.
152+
* @param resId The resource id of an optional icon for the preference.
153+
*/
154+
public void setSubtypeEnablerIcon(int resId) {
155+
mSubtypeEnablerIconRes = resId;
156+
updateSubtypeEnabler();
157+
}
158+
159+
/**
160+
* Sets the icon for the Preference for launching subtype enabler with a Drawable.
161+
* @param drawable The drawable of an optional icon for the preference.
162+
*/
163+
public void setSubtypeEnablerIcon(Drawable drawable) {
164+
mSubtypeEnablerIconRes = 0;
165+
mSubtypeEnablerIcon = drawable;
166+
updateSubtypeEnabler();
167+
}
168+
169+
private void updateSubtypeEnabler() {
170+
if (mSubtypeEnablerPreference != null) {
171+
if (mSubtypeEnablerTitleRes != 0) {
172+
mSubtypeEnablerPreference.setTitle(mSubtypeEnablerTitleRes);
173+
} else if (!TextUtils.isEmpty(mSubtypeEnablerTitle)) {
174+
mSubtypeEnablerPreference.setTitle(mSubtypeEnablerTitle);
175+
}
176+
if (mSubtypeEnablerSummaryRes != 0) {
177+
mSubtypeEnablerPreference.setSummary(mSubtypeEnablerSummaryRes);
178+
} else if (!TextUtils.isEmpty(mSubtypeEnablerSummary)) {
179+
mSubtypeEnablerPreference.setSummary(mSubtypeEnablerSummary);
180+
}
181+
if (mSubtypeEnablerIconRes != 0) {
182+
mSubtypeEnablerPreference.setIcon(mSubtypeEnablerIconRes);
183+
} else if (mSubtypeEnablerIcon != null) {
184+
mSubtypeEnablerPreference.setIcon(mSubtypeEnablerIcon);
185+
}
186+
}
187+
if (mInputMethodSettingsCategory != null) {
188+
if (mInputMethodSettingsCategoryTitleRes != 0) {
189+
mInputMethodSettingsCategory.setTitle(mInputMethodSettingsCategoryTitleRes);
190+
} else if (!TextUtils.isEmpty(mInputMethodSettingsCategoryTitle)) {
191+
mInputMethodSettingsCategory.setTitle(mInputMethodSettingsCategoryTitle);
192+
}
193+
}
194+
}
195+
}

0 commit comments

Comments
 (0)