From ec1f72d17dbbfd8012e0c4164f62b82ff2e7a9a9 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Tue, 18 Oct 2011 18:45:44 +0900 Subject: [PATCH] Update SoftKeyboard sample to use InputMethodSubtype Bug: 5475117 Change-Id: I94c70800e272e28c080edd5102adceabf06d7aff --- samples/SoftKeyboard/AndroidManifest.xml | 7 + .../res/drawable-hdpi/icon_en_gb.png | Bin 0 -> 2020 bytes .../res/drawable-hdpi/icon_en_us.png | Bin 0 -> 1340 bytes .../res/drawable-mdpi/icon_en_gb.png | Bin 0 -> 1226 bytes .../res/drawable-mdpi/icon_en_us.png | Bin 0 -> 700 bytes samples/SoftKeyboard/res/values/strings.xml | 10 + .../SoftKeyboard/res/xml/ime_preferences.xml | 19 ++ samples/SoftKeyboard/res/xml/method.xml | 15 +- .../InputMethodSettingsFragment.java | 101 +++++++++ .../InputMethodSettingsImpl.java | 198 ++++++++++++++++++ .../InputMethodSettingsInterface.java | 69 ++++++ .../android/softkeyboard/CandidateView.java | 24 +-- .../android/softkeyboard/ImePreferences.java | 55 +++++ .../android/softkeyboard/LatinKeyboard.java | 44 ++-- .../softkeyboard/LatinKeyboardView.java | 33 +-- .../android/softkeyboard/SoftKeyboard.java | 73 ++++--- 16 files changed, 572 insertions(+), 76 deletions(-) create mode 100644 samples/SoftKeyboard/res/drawable-hdpi/icon_en_gb.png create mode 100644 samples/SoftKeyboard/res/drawable-hdpi/icon_en_us.png create mode 100644 samples/SoftKeyboard/res/drawable-mdpi/icon_en_gb.png create mode 100644 samples/SoftKeyboard/res/drawable-mdpi/icon_en_us.png create mode 100644 samples/SoftKeyboard/res/xml/ime_preferences.xml create mode 100644 samples/SoftKeyboard/src/com/android/inputmethodcommon/InputMethodSettingsFragment.java create mode 100644 samples/SoftKeyboard/src/com/android/inputmethodcommon/InputMethodSettingsImpl.java create mode 100644 samples/SoftKeyboard/src/com/android/inputmethodcommon/InputMethodSettingsInterface.java create mode 100644 samples/SoftKeyboard/src/com/example/android/softkeyboard/ImePreferences.java diff --git a/samples/SoftKeyboard/AndroidManifest.xml b/samples/SoftKeyboard/AndroidManifest.xml index 61b513109..3d30779d3 100755 --- a/samples/SoftKeyboard/AndroidManifest.xml +++ b/samples/SoftKeyboard/AndroidManifest.xml @@ -8,5 +8,12 @@ + + + + + + + diff --git a/samples/SoftKeyboard/res/drawable-hdpi/icon_en_gb.png b/samples/SoftKeyboard/res/drawable-hdpi/icon_en_gb.png new file mode 100644 index 0000000000000000000000000000000000000000..53088d9944d361bdad0c6c7358e6537361d432a4 GIT binary patch literal 2020 zcmV;8QLGBG zNP~z#K{_26TG~!KolaNU4#U26rrWfg&hP#2>p*8-=_b^Lo7}W{@6CPZ>)hUR{{JYJ z>5JgBo`vslxhgPc>5pjIaUPUbv!HaCi@U_{^_l#PU#~?tWxWCv7Qqns1pZWd;;3?; zocx{Fc#qHU*@nJ{F*fZKg1M`9qC&)f^`H*=fFU@dbjM+(3y${~hV$R=h7zT8z_E^x z;?#-_D9N>Hzzru);ni7Rp;_1Y^#;G)z#h=%-4@`rkgYJp=fHmW7)+Di6l?bopq*go zH3E6-KSSC1OOi)PpE4V|d;8*$(gynWLy#T54kg)E4R~B)E~W>6gBD_O3EsZ;z-|{X z-_00WbYF-Wp%F5+f~XjzO(tldU}*=#eIsH0=yQ}^yyC^77!vYek4r+%tZ?X>so*i~ z9!6H!8Wd;dXuuPatau~jJNSv0)dqg6fjyvpfuWeW?tTs#0#F9`YHBCSzwPhA$QJl=vwqw!P^W(=7M)ol$Nal zPfD?2*21l5)gu&&U-jU(3Rr@QceL)g2tmuX!p?T}6V1q*{|;jO9+cp^)&r0+ zZxxEnnHumE%NYbOj(|Ud^#T98z#dTl-eH)xatBN%0lN1P%;R4Xpj}kZ0poDyv+v=` zcX-8BE)Lz@dlNBc(k8(ctTK}I`r@4aK~UNTWAx;Yv2(u(`9=$@VediT`XLou`~lK~ zmZLB^O#_~qehwkwKcTG%&I8_1U=L{9K1;A*^=_C`P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-z9XXHOM-C(xk`rwSD`3)= zK=Y1XaVEKQ43CX6znqYL$--n~H6%NerODQ|kZ8%?+a!w{6`PaQ|CRZrI#4C37S~3; z9#w>DVh##N^U_yvHvem>Hb<1|aJ?V%uU7g*ouXcO?S)I}81;<0Mt!5s#U$=gx~KGS zqFOnkt<+iSEp=D@y;6s%$L5(4uqP~sZQo(3*A+^)6+kLE6q6M o>6(3mZrZ+b1O5g80RR6302{6KS59Lr+W-In07*qoM6N<$f@gYK_W%F@ literal 0 HcmV?d00001 diff --git a/samples/SoftKeyboard/res/drawable-mdpi/icon_en_us.png b/samples/SoftKeyboard/res/drawable-mdpi/icon_en_us.png new file mode 100644 index 0000000000000000000000000000000000000000..da8d43f690ba84f4bf406553e2ad02c29298309a GIT binary patch literal 700 zcmV;t0z>_YP)f=_9Q!1VI#pRs?Ak zbm2m9;YRQ;(6pMgAQ-#WZ5PtdnF*1uy_3bq3}-IfncsZpoY5Kj(-<@fprh%0mZ=jP z=t~`1%l%t3m?acWc=6)M89Y0x=PF%)ZxU*L6~nF~Vv#w-qjPW%6;M#`u?-Z-=yIx< zh~Gi<@+>^=NeId+ZWtqY=Lq4gGt5=G4vk!P5kY?$fs4}+a*ME8Wt58dVY5gOb4&1# zO(7J#iCks@y=N16^7svKaKK%RP+nfah^K@Yp%ahI!!tYyl~AylMMlr#F186pX5k$v zAuHA}?M&iBFopNQ3|BEimO&9&a`Fx#O2J`M*mc=$G6X`eLnxcB!eA6|drZYAaS9)$ zGFLG|va}2zIeEuJUP4QeYwWrhJwj}PkfRRmi~_zHja_qj_q(3ggt_$8C7 zA`~byN*O|7$e|*ytdKF!s8Dj_gitzFfyt_%r1#_XDJwQl+qjAmq9z{oU1uJlKJpO? zlCT7)U4bOF>BXb-h!8>-$ajfE1q*ToJ8PS0tiR+c9j!G^d!C(~5(ov_6x^-?6sg8a z03}ArjfZBCP7*@K93E7k;`deqzqXoO#R$=~wwML7p&5k9PiYyTu^#;uh=i1xY~WJ( z7Q0d_Nr05FjQmM2Hjee+`SEjHrQ6=!!d(4q`8-Mqho98_PpOSn%_gb iUwm5i7XSeN{{sL?^Zx++cD<1R0000Go Next Send + + + %s + English (GB) + + + Sample Soft Keyboard Settings + Input languages + Select input languages + General diff --git a/samples/SoftKeyboard/res/xml/ime_preferences.xml b/samples/SoftKeyboard/res/xml/ime_preferences.xml new file mode 100644 index 000000000..1e973a62e --- /dev/null +++ b/samples/SoftKeyboard/res/xml/ime_preferences.xml @@ -0,0 +1,19 @@ + + + + + diff --git a/samples/SoftKeyboard/res/xml/method.xml b/samples/SoftKeyboard/res/xml/method.xml index d246624c4..2f2d6f0a3 100644 --- a/samples/SoftKeyboard/res/xml/method.xml +++ b/samples/SoftKeyboard/res/xml/method.xml @@ -20,4 +20,17 @@ - + + + + diff --git a/samples/SoftKeyboard/src/com/android/inputmethodcommon/InputMethodSettingsFragment.java b/samples/SoftKeyboard/src/com/android/inputmethodcommon/InputMethodSettingsFragment.java new file mode 100644 index 000000000..b3b7c8c09 --- /dev/null +++ b/samples/SoftKeyboard/src/com/android/inputmethodcommon/InputMethodSettingsFragment.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * This is a part of the inputmethod-common static Java library. + * The original source code can be found at frameworks/opt/inputmethodcommon of Android Open Source + * Project. + */ + +package com.android.inputmethodcommon; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.preference.PreferenceFragment; + +/** + * This is a helper class for an IME's settings preference fragment. It's recommended for every + * IME to have its own settings preference fragment which inherits this class. + */ +public abstract class InputMethodSettingsFragment extends PreferenceFragment + implements InputMethodSettingsInterface { + private final InputMethodSettingsImpl mSettings = new InputMethodSettingsImpl(); + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + final Context context = getActivity(); + setPreferenceScreen(getPreferenceManager().createPreferenceScreen(context)); + mSettings.init(context, getPreferenceScreen()); + } + + /** + * {@inheritDoc} + */ + @Override + public void setInputMethodSettingsCategoryTitle(int resId) { + mSettings.setInputMethodSettingsCategoryTitle(resId); + } + + /** + * {@inheritDoc} + */ + @Override + public void setInputMethodSettingsCategoryTitle(CharSequence title) { + mSettings.setInputMethodSettingsCategoryTitle(title); + } + + /** + * {@inheritDoc} + */ + @Override + public void setSubtypeEnablerTitle(int resId) { + mSettings.setSubtypeEnablerTitle(resId); + } + + /** + * {@inheritDoc} + */ + @Override + public void setSubtypeEnablerTitle(CharSequence title) { + mSettings.setSubtypeEnablerTitle(title); + } + + /** + * {@inheritDoc} + */ + @Override + public void setSubtypeEnablerIcon(int resId) { + mSettings.setSubtypeEnablerIcon(resId); + } + + /** + * {@inheritDoc} + */ + @Override + public void setSubtypeEnablerIcon(Drawable drawable) { + mSettings.setSubtypeEnablerIcon(drawable); + } + + /** + * {@inheritDoc} + */ + @Override + public void onResume() { + super.onResume(); + mSettings.updateSubtypeEnabler(); + } +} diff --git a/samples/SoftKeyboard/src/com/android/inputmethodcommon/InputMethodSettingsImpl.java b/samples/SoftKeyboard/src/com/android/inputmethodcommon/InputMethodSettingsImpl.java new file mode 100644 index 000000000..722148d82 --- /dev/null +++ b/samples/SoftKeyboard/src/com/android/inputmethodcommon/InputMethodSettingsImpl.java @@ -0,0 +1,198 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * This is a part of the inputmethod-common static Java library. + * The original source code can be found at frameworks/opt/inputmethodcommon of Android Open Source + * Project. + */ + +package com.android.inputmethodcommon; + +import android.content.Context; +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceClickListener; +import android.preference.PreferenceScreen; +import android.provider.Settings; +import android.text.TextUtils; +import android.view.inputmethod.InputMethodInfo; +import android.view.inputmethod.InputMethodManager; +import android.view.inputmethod.InputMethodSubtype; + +import java.util.List; + +/* package private */ class InputMethodSettingsImpl implements InputMethodSettingsInterface { + private Preference mSubtypeEnablerPreference; + private int mInputMethodSettingsCategoryTitleRes; + private CharSequence mInputMethodSettingsCategoryTitle; + private int mSubtypeEnablerTitleRes; + private CharSequence mSubtypeEnablerTitle; + private int mSubtypeEnablerIconRes; + private Drawable mSubtypeEnablerIcon; + private InputMethodManager mImm; + private InputMethodInfo mImi; + private Context mContext; + + /** + * Initialize internal states of this object. + * @param context the context for this application. + * @param prefScreen a PreferenceScreen of PreferenceActivity or PreferenceFragment. + * @return true if this application is an IME and has two or more subtypes, false otherwise. + */ + public boolean init(final Context context, final PreferenceScreen prefScreen) { + mContext = context; + mImm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); + mImi = getMyImi(context, mImm); + if (mImi == null || mImi.getSubtypeCount() <= 1) { + return false; + } + mSubtypeEnablerPreference = new Preference(context); + mSubtypeEnablerPreference + .setOnPreferenceClickListener(new OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + final CharSequence title = getSubtypeEnablerTitle(context); + final Intent intent = + new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS); + intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, mImi.getId()); + if (!TextUtils.isEmpty(title)) { + intent.putExtra(Intent.EXTRA_TITLE, title); + } + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED + | Intent.FLAG_ACTIVITY_CLEAR_TOP); + context.startActivity(intent); + return true; + } + }); + prefScreen.addPreference(mSubtypeEnablerPreference); + updateSubtypeEnabler(); + return true; + } + + private static InputMethodInfo getMyImi(Context context, InputMethodManager imm) { + final List imis = imm.getInputMethodList(); + for (int i = 0; i < imis.size(); ++i) { + final InputMethodInfo imi = imis.get(i); + if (imis.get(i).getPackageName().equals(context.getPackageName())) { + return imi; + } + } + return null; + } + + private static String getEnabledSubtypesLabel( + Context context, InputMethodManager imm, InputMethodInfo imi) { + if (context == null || imm == null || imi == null) return null; + final List subtypes = imm.getEnabledInputMethodSubtypeList(imi, true); + final StringBuilder sb = new StringBuilder(); + final int N = subtypes.size(); + for (int i = 0; i < N; ++i) { + final InputMethodSubtype subtype = subtypes.get(i); + if (sb.length() > 0) { + sb.append(", "); + } + sb.append(subtype.getDisplayName(context, imi.getPackageName(), + imi.getServiceInfo().applicationInfo)); + } + return sb.toString(); + } + /** + * {@inheritDoc} + */ + @Override + public void setInputMethodSettingsCategoryTitle(int resId) { + mInputMethodSettingsCategoryTitleRes = resId; + updateSubtypeEnabler(); + } + + /** + * {@inheritDoc} + */ + @Override + public void setInputMethodSettingsCategoryTitle(CharSequence title) { + mInputMethodSettingsCategoryTitleRes = 0; + mInputMethodSettingsCategoryTitle = title; + updateSubtypeEnabler(); + } + + /** + * {@inheritDoc} + */ + @Override + public void setSubtypeEnablerTitle(int resId) { + mSubtypeEnablerTitleRes = resId; + updateSubtypeEnabler(); + } + + /** + * {@inheritDoc} + */ + @Override + public void setSubtypeEnablerTitle(CharSequence title) { + mSubtypeEnablerTitleRes = 0; + mSubtypeEnablerTitle = title; + updateSubtypeEnabler(); + } + + /** + * {@inheritDoc} + */ + @Override + public void setSubtypeEnablerIcon(int resId) { + mSubtypeEnablerIconRes = resId; + updateSubtypeEnabler(); + } + + /** + * {@inheritDoc} + */ + @Override + public void setSubtypeEnablerIcon(Drawable drawable) { + mSubtypeEnablerIconRes = 0; + mSubtypeEnablerIcon = drawable; + updateSubtypeEnabler(); + } + + private CharSequence getSubtypeEnablerTitle(Context context) { + if (mSubtypeEnablerTitleRes != 0) { + return context.getString(mSubtypeEnablerTitleRes); + } else { + return mSubtypeEnablerTitle; + } + } + + public void updateSubtypeEnabler() { + if (mSubtypeEnablerPreference != null) { + if (mSubtypeEnablerTitleRes != 0) { + mSubtypeEnablerPreference.setTitle(mSubtypeEnablerTitleRes); + } else if (!TextUtils.isEmpty(mSubtypeEnablerTitle)) { + mSubtypeEnablerPreference.setTitle(mSubtypeEnablerTitle); + } + final String summary = getEnabledSubtypesLabel(mContext, mImm, mImi); + if (!TextUtils.isEmpty(summary)) { + mSubtypeEnablerPreference.setSummary(summary); + } + if (mSubtypeEnablerIconRes != 0) { + mSubtypeEnablerPreference.setIcon(mSubtypeEnablerIconRes); + } else if (mSubtypeEnablerIcon != null) { + mSubtypeEnablerPreference.setIcon(mSubtypeEnablerIcon); + } + } + } +} diff --git a/samples/SoftKeyboard/src/com/android/inputmethodcommon/InputMethodSettingsInterface.java b/samples/SoftKeyboard/src/com/android/inputmethodcommon/InputMethodSettingsInterface.java new file mode 100644 index 000000000..f41e224d7 --- /dev/null +++ b/samples/SoftKeyboard/src/com/android/inputmethodcommon/InputMethodSettingsInterface.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * This is a part of the inputmethod-common static Java library. + * The original source code can be found at frameworks/opt/inputmethodcommon of Android Open Source + * Project. + */ + +package com.android.inputmethodcommon; + +import android.graphics.drawable.Drawable; + +/** + * InputMethodSettingsInterface is the interface for adding IME related preferences to + * PreferenceActivity or PreferenceFragment. + */ +public interface InputMethodSettingsInterface { + /** + * Sets the title for the input method settings category with a resource ID. + * @param resId The resource ID of the title. + */ + public void setInputMethodSettingsCategoryTitle(int resId); + + /** + * Sets the title for the input method settings category with a CharSequence. + * @param title The title for this preference. + */ + public void setInputMethodSettingsCategoryTitle(CharSequence title); + + /** + * Sets the title for the input method enabler preference for launching subtype enabler with a + * resource ID. + * @param resId The resource ID of the title. + */ + public void setSubtypeEnablerTitle(int resId); + + /** + * Sets the title for the input method enabler preference for launching subtype enabler with a + * CharSequence. + * @param title The title for this preference. + */ + public void setSubtypeEnablerTitle(CharSequence title); + + /** + * Sets the icon for the preference for launching subtype enabler with a resource ID. + * @param resId The resource id of an optional icon for the preference. + */ + public void setSubtypeEnablerIcon(int resId); + + /** + * Sets the icon for the Preference for launching subtype enabler with a Drawable. + * @param drawable The drawable of an optional icon for the preference. + */ + public void setSubtypeEnablerIcon(Drawable drawable); +} diff --git a/samples/SoftKeyboard/src/com/example/android/softkeyboard/CandidateView.java b/samples/SoftKeyboard/src/com/example/android/softkeyboard/CandidateView.java index 7cadead8a..33ee467f3 100755 --- a/samples/SoftKeyboard/src/com/example/android/softkeyboard/CandidateView.java +++ b/samples/SoftKeyboard/src/com/example/android/softkeyboard/CandidateView.java @@ -1,17 +1,17 @@ /* - * Copyright (C) 2008-2009 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * Copyright (C) 2008-2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.example.android.softkeyboard; diff --git a/samples/SoftKeyboard/src/com/example/android/softkeyboard/ImePreferences.java b/samples/SoftKeyboard/src/com/example/android/softkeyboard/ImePreferences.java new file mode 100644 index 000000000..db6c1d984 --- /dev/null +++ b/samples/SoftKeyboard/src/com/example/android/softkeyboard/ImePreferences.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.softkeyboard; + +import android.content.Intent; +import android.os.Bundle; +import android.preference.PreferenceActivity; +import com.android.inputmethodcommon.InputMethodSettingsFragment; + +/** + * Displays the IME preferences inside the input method setting. + */ +public class ImePreferences extends PreferenceActivity { + @Override + public Intent getIntent() { + final Intent modIntent = new Intent(super.getIntent()); + modIntent.putExtra(EXTRA_SHOW_FRAGMENT, Settings.class.getName()); + modIntent.putExtra(EXTRA_NO_HEADERS, true); + return modIntent; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // We overwrite the title of the activity, as the default one is "Voice Search". + setTitle(R.string.settings_name); + } + + public static class Settings extends InputMethodSettingsFragment { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setInputMethodSettingsCategoryTitle(R.string.language_selection_title); + setSubtypeEnablerTitle(R.string.select_language); + + // Load the preferences from an XML resource + addPreferencesFromResource(R.xml.ime_preferences); + } + } +} diff --git a/samples/SoftKeyboard/src/com/example/android/softkeyboard/LatinKeyboard.java b/samples/SoftKeyboard/src/com/example/android/softkeyboard/LatinKeyboard.java index 179844234..67787dca4 100644 --- a/samples/SoftKeyboard/src/com/example/android/softkeyboard/LatinKeyboard.java +++ b/samples/SoftKeyboard/src/com/example/android/softkeyboard/LatinKeyboard.java @@ -1,17 +1,17 @@ /* - * Copyright (C) 2008-2009 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * Copyright (C) 2008-2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.example.android.softkeyboard; @@ -19,14 +19,14 @@ package com.example.android.softkeyboard; import android.content.Context; import android.content.res.Resources; import android.content.res.XmlResourceParser; +import android.graphics.drawable.Drawable; import android.inputmethodservice.Keyboard; -import android.inputmethodservice.Keyboard.Key; -import android.inputmethodservice.Keyboard.Row; import android.view.inputmethod.EditorInfo; public class LatinKeyboard extends Keyboard { private Key mEnterKey; + private Key mSpaceKey; public LatinKeyboard(Context context, int xmlLayoutResId) { super(context, xmlLayoutResId); @@ -43,6 +43,8 @@ public class LatinKeyboard extends Keyboard { Key key = new LatinKey(res, parent, x, y, parser); if (key.codes[0] == 10) { mEnterKey = key; + } else if (key.codes[0] == ' ') { + mSpaceKey = key; } return key; } @@ -68,8 +70,7 @@ public class LatinKeyboard extends Keyboard { mEnterKey.label = res.getText(R.string.label_next_key); break; case EditorInfo.IME_ACTION_SEARCH: - mEnterKey.icon = res.getDrawable( - R.drawable.sym_keyboard_search); + mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search); mEnterKey.label = null; break; case EditorInfo.IME_ACTION_SEND: @@ -78,13 +79,18 @@ public class LatinKeyboard extends Keyboard { mEnterKey.label = res.getText(R.string.label_send_key); break; default: - mEnterKey.icon = res.getDrawable( - R.drawable.sym_keyboard_return); + mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_return); mEnterKey.label = null; break; } } - + + void setSpaceIcon(final Drawable icon) { + if (mSpaceKey != null) { + mSpaceKey.icon = icon; + } + } + static class LatinKey extends Keyboard.Key { public LatinKey(Resources res, Keyboard.Row parent, int x, int y, XmlResourceParser parser) { diff --git a/samples/SoftKeyboard/src/com/example/android/softkeyboard/LatinKeyboardView.java b/samples/SoftKeyboard/src/com/example/android/softkeyboard/LatinKeyboardView.java index 7464607d8..489c283fd 100644 --- a/samples/SoftKeyboard/src/com/example/android/softkeyboard/LatinKeyboardView.java +++ b/samples/SoftKeyboard/src/com/example/android/softkeyboard/LatinKeyboardView.java @@ -1,26 +1,27 @@ /* - * Copyright (C) 2008-2009 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * Copyright (C) 2008-2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.example.android.softkeyboard; import android.content.Context; import android.inputmethodservice.Keyboard; -import android.inputmethodservice.KeyboardView; import android.inputmethodservice.Keyboard.Key; +import android.inputmethodservice.KeyboardView; import android.util.AttributeSet; +import android.view.inputmethod.InputMethodSubtype; public class LatinKeyboardView extends KeyboardView { @@ -43,4 +44,10 @@ public class LatinKeyboardView extends KeyboardView { return super.onLongPress(key); } } + + void setSubtypeOnSpaceKey(final InputMethodSubtype subtype) { + final LatinKeyboard keyboard = (LatinKeyboard)getKeyboard(); + keyboard.setSpaceIcon(getResources().getDrawable(subtype.getIconResId())); + invalidateAllKeys(); + } } diff --git a/samples/SoftKeyboard/src/com/example/android/softkeyboard/SoftKeyboard.java b/samples/SoftKeyboard/src/com/example/android/softkeyboard/SoftKeyboard.java index 50b353601..7c4a17e1f 100644 --- a/samples/SoftKeyboard/src/com/example/android/softkeyboard/SoftKeyboard.java +++ b/samples/SoftKeyboard/src/com/example/android/softkeyboard/SoftKeyboard.java @@ -1,17 +1,17 @@ /* - * Copyright (C) 2008-2009 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * Copyright (C) 2008-2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.example.android.softkeyboard; @@ -19,8 +19,8 @@ package com.example.android.softkeyboard; import android.inputmethodservice.InputMethodService; import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; +import android.text.InputType; import android.text.method.MetaKeyKeyListener; -import android.util.Log; import android.view.KeyCharacterMap; import android.view.KeyEvent; import android.view.View; @@ -28,6 +28,7 @@ import android.view.inputmethod.CompletionInfo; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodManager; +import android.view.inputmethod.InputMethodSubtype; import java.util.ArrayList; import java.util.List; @@ -52,8 +53,10 @@ public class SoftKeyboard extends InputMethodService * that are primarily intended to be used for on-screen text entry. */ static final boolean PROCESS_HARD_KEYS = true; - - private KeyboardView mInputView; + + private InputMethodManager mInputMethodManager; + + private LatinKeyboardView mInputView; private CandidateView mCandidateView; private CompletionInfo[] mCompletions; @@ -79,6 +82,7 @@ public class SoftKeyboard extends InputMethodService */ @Override public void onCreate() { super.onCreate(); + mInputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); mWordSeparators = getResources().getString(R.string.word_separators); } @@ -107,7 +111,7 @@ public class SoftKeyboard extends InputMethodService * a configuration change. */ @Override public View onCreateInputView() { - mInputView = (KeyboardView) getLayoutInflater().inflate( + mInputView = (LatinKeyboardView) getLayoutInflater().inflate( R.layout.input, null); mInputView.setOnKeyboardActionListener(this); mInputView.setKeyboard(mQwertyKeyboard); @@ -149,21 +153,21 @@ public class SoftKeyboard extends InputMethodService // We are now going to initialize our state based on the type of // text being edited. - switch (attribute.inputType&EditorInfo.TYPE_MASK_CLASS) { - case EditorInfo.TYPE_CLASS_NUMBER: - case EditorInfo.TYPE_CLASS_DATETIME: + switch (attribute.inputType & InputType.TYPE_MASK_CLASS) { + case InputType.TYPE_CLASS_NUMBER: + case InputType.TYPE_CLASS_DATETIME: // Numbers and dates default to the symbols keyboard, with // no extra features. mCurKeyboard = mSymbolsKeyboard; break; - case EditorInfo.TYPE_CLASS_PHONE: + case InputType.TYPE_CLASS_PHONE: // Phones will also default to the symbols keyboard, though // often you will want to have a dedicated phone keyboard. mCurKeyboard = mSymbolsKeyboard; break; - case EditorInfo.TYPE_CLASS_TEXT: + case InputType.TYPE_CLASS_TEXT: // This is general text editing. We will default to the // normal alphabetic keyboard, and assume that we should // be doing predictive text (showing candidates as the @@ -173,23 +177,23 @@ public class SoftKeyboard extends InputMethodService // We now look for a few special variations of text that will // modify our behavior. - int variation = attribute.inputType & EditorInfo.TYPE_MASK_VARIATION; - if (variation == EditorInfo.TYPE_TEXT_VARIATION_PASSWORD || - variation == EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) { + int variation = attribute.inputType & InputType.TYPE_MASK_VARIATION; + if (variation == InputType.TYPE_TEXT_VARIATION_PASSWORD || + variation == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) { // Do not display predictions / what the user is typing // when they are entering a password. mPredictionOn = false; } - if (variation == EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS - || variation == EditorInfo.TYPE_TEXT_VARIATION_URI - || variation == EditorInfo.TYPE_TEXT_VARIATION_FILTER) { + if (variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS + || variation == InputType.TYPE_TEXT_VARIATION_URI + || variation == InputType.TYPE_TEXT_VARIATION_FILTER) { // Our predictions are not useful for e-mail addresses // or URIs. mPredictionOn = false; } - if ((attribute.inputType&EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE) != 0) { + if ((attribute.inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE) != 0) { // If this is an auto-complete text view, then our predictions // will not be shown and instead we will allow the editor // to supply their own. We only show the editor's @@ -245,8 +249,15 @@ public class SoftKeyboard extends InputMethodService // Apply the selected keyboard to the input view. mInputView.setKeyboard(mCurKeyboard); mInputView.closing(); + final InputMethodSubtype subtype = mInputMethodManager.getCurrentInputMethodSubtype(); + mInputView.setSubtypeOnSpaceKey(subtype); } - + + @Override + public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype) { + mInputView.setSubtypeOnSpaceKey(subtype); + } + /** * Deal with the editor reporting movement of its cursor. */ @@ -284,7 +295,7 @@ public class SoftKeyboard extends InputMethodService } List stringList = new ArrayList(); - for (int i=0; i<(completions != null ? completions.length : 0); i++) { + for (int i = 0; i < completions.length; i++) { CompletionInfo ci = completions[i]; if (ci != null) stringList.add(ci.getText().toString()); } @@ -435,7 +446,7 @@ public class SoftKeyboard extends InputMethodService && mInputView != null && mQwertyKeyboard == mInputView.getKeyboard()) { int caps = 0; EditorInfo ei = getCurrentInputEditorInfo(); - if (ei != null && ei.inputType != EditorInfo.TYPE_NULL) { + if (ei != null && ei.inputType != InputType.TYPE_NULL) { caps = getCurrentInputConnection().getCursorCapsMode(attr.inputType); } mInputView.setShifted(mCapsLock || caps != 0);