Latin输入法怎么默认勾选几种语言
时间:10-02
整理:3721RD
点击:
[DESCRIPTION]
Latin输入法怎么默认勾选几种语言,Latin IME 不使用系统默认的语言
[SOLUTION]
【GB】
提供简单的sample code,如默认将俄语、英文、法语输入法勾选:
1.新增文件LatinImeReceiver.java
package com.android.inputmethod.latin;
import android.content.BroADCastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethoDMAnager;
//import android.view.inputmethod.InputMethodSubtype;
import android.text.TextUtils;
public class LatinImeReceiver extends BroadcastReceiver {
private static final String TAG = LatinImeReceiver.class.getSIMpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d("LatinImeReceiver", "step1");
SharedPreferences sp = context.getSharedPreferences("com.android.inputmethod.latin_preferences",
Context.MODE_PRIVATE);
boolean hasSet = sp.getBoolean("has_set", false);
if (!hasSet) {
Log.d("LatinImeReceiver", "step2");
Editor editor = sp.edit();
Log.d("LatinImeReceiver", "step3");
editor.putString(LatinIME.PREF_SELECTED_LANGUAGES, "en_US,ru,fr"); //默认将英语、俄语勾选,具体该怎么写
可以参考inputlanguageselection.java中的WHITELIST_LANGUAGES
editor.putBoolean("has_set", true);
Log.d("LatinImeReceiver", "step4");
//editor.coMMIt();
SharedPreferencesCompat.apply(editor);
Log.d("LatinImeReceiver", "step5");
}
}
将其放置到路径packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin文件夹下面
2.注册intent,在packages/inputmethods/LatinIME/java/androidManifest.xml中增加
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
权限
并在最后面加入LatinImeReceiver:
<receiver android:name="LatinImeReceiver" android:enabled="true">
<intent-fiLTEr>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
3 重新编译整个工程
【ICS】
1.在mediatek/config/{projectname}/ProjectConfig.mk中修改如下的config,只需要把默认的语言加在后边即可
DEFAULT_LATIN_IME_LANGUAGES = en_US hi
DEFAULT_LATIN_IME_LANGUAGES后面的格式需要与packages\inputmethods\LatinIME\java\res\xml\method.xml中的
android:imeSubtypeLocale="XXX"字符相匹配,后面添加的语言要与“XXX”相同,例如如果要加印度语言
DEFAULT_LATIN_IME_LANGUAGES = en_US hi 中间用空格隔开。这个后面的格式与mtk_PRODUCT_LOCALES不一样,
MTK_PRODUCT_LOCALES后面是系统的语言以及要编译到系统中的资源的配置。
2.在LatinIMEReceiver.java中 setDefaultSubtypes函数中增加红色部分
private void setDefaultSubtypes(Context context) {
final String serviceName = "com.android.inputmethod.latin/.LatinIME";
final String currentPackageName = "com.android.inputmethod.latin";
final String enable = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ENABLED_INPUT_METHODS);
final InputMethodManager imm = (InputMethodManager) context.getSystEMService(
Context.INPUT_METHOD_SERVICE);
final StringBuilder builder = new StringBuilder();
// Get sub type hash code
for (InputMethodInfo info : imm.getInputMethodList()) {
if (currentPackageName.equals(info.getPackageName())) {
for (int i = 0; i < info.getSubtypeCount(); i++) {
final InputMethodSubtype subtype = info.getSubtypeAt(i);
final String locale = subtype.getLocale().toString();
if (isDefaultLocale(locale)) {
Log.i(TAG, "default enabled subtype locale = " + locale);
builder.append(';');
builder.append(subtype.hashCode());
}
}
break;
}
}
// Insert the sub type
if (builder.length() > 0 && !TextUtils.isEmpty(enable)) {
final String subtype = builder.toString();
builder.setLength(0);
final int index = enable.indexOf(serviceName) + serviceName.length();
if (enable.length() > index) {
builder.append(enable.substring(0, index));
builder.append(subtype);
builder.append(enable.substring(index));
} else if (enable.length() == index) {
builder.append(enable);
builder.append(subtype);
} else {
return;
}
} else {
Log.w(TAG, "Build Latin IME subtype failed: " + " builder length = " + builder.length()
+ "; enable isEmpty :" + TextUtils.isEmpty(enable));
return;
}
// Commit the result
android.provider.Settings.Secure.putString(context.getContentResolver(),
android.provider.Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
String lastInputMethodId = Settings.Secure.getString(context
.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
Log.w(TAG, "DEFAULT_INPUT_METHOD = " + lastInputMethodId);
if(lastInputMethodId.equals(serviceName)) {
Log.w(TAG, "DEFAULT_INPUT_METHOD = com.android.inputmethod.latin/.LatinIME" );
for (InputMethodInfo info : imm.getInputMethodList()) {
if (currentPackageName.equals(info.getPackageName())) {
for (int i = 0; i < info.getSubtypeCount(); i++) {
final InputMethodSubtype subtype = info.getSubtypeAt(i);
final String[] locales = IMEFeatureOption.DEFAULT_LATIN_IME_LANGUAGES;
Log.w(TAG, "i = " + i + ", locales[0] = " + locales[0]);
if((subtype.getLocale()).equals(locales[0])) {
Log.w(TAG, "putString " + subtype.hashCode());
android.provider.Settings.Secure.putInt(context.getContentResolver(),
android.provider.Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtype.hashCode());
}
}
break;
}
}
}
}
【JB、KK】
JB中DEFAULT_LATIN_IME_LANGUAGES config没有用到,没有LatinIMEReceiver.java这个文件。所以要默认勾选多种语
言需要参照ICS的做法增加以下部分:
比如默认选中泰语和英语:
1.增加LatinImeReceiver.java文件放在packages\inputmethods\LatinIME\java\src\com\android\inputmethod\latin
目录下
package com.android.inputmethod.latin;
import com.mediatek.common.featureoption.IMEFeatureOption;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.provider.Settings;
import android.util.Log;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import android.text.TextUtils;
public class LatinImeReceiver extends BroadcastReceiver {
private static final String TAG = LatinImeReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
// Set the default input language at the system boot completed.
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
SharedPreferences sp = context.getSharedPreferences("default_input_language_config",
Context.MODE_PRIVATE);
boolean hasSet = sp.getBoolean("has_set", false);
if (!hasSet) {
setDefaultSubtypes(context);
sp.edit().putBoolean("has_set", true).commit();
}
}
}
/**
* M: Set the default IME subtype.
*/
private void setDefaultSubtypes(Context context) {
final String serviceName = "com.android.inputmethod.latin/.LatinIME";
final String currentPackageName = "com.android.inputmethod.latin";
final String enable = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ENABLED_INPUT_METHODS);
final InputMethodManager imm = (InputMethodManager) context.getSystemService(
Context.INPUT_METHOD_SERVICE);
final StringBuilder builder = new StringBuilder();
// Get sub type hash code
for (InputMethodInfo info : imm.getInputMethodList()) {
if (currentPackageName.equals(info.getPackageName())) {
for (int i = 0; i < info.getSubtypeCount(); i++) {
final InputMethodSubtype subtype = info.getSubtypeAt(i);
final String locale = subtype.getLocale().toString();
if (isDefaultLocale(locale)) {
Log.i(TAG, "default enabled subtype locale = " + locale);
builder.append(';');
builder.append(subtype.hashCode());
}
}
break;
}
}
// Insert the sub type
if (builder.length() > 0 && !TextUtils.isEmpty(enable)) {
final String subtype = builder.toString();
builder.setLength(0);
final int index = enable.indexOf(serviceName) + serviceName.length();
if (enable.length() > index) {
builder.append(enable.substring(0, index));
builder.append(subtype);
builder.append(enable.substring(index));
} else if (enable.length() == index) {
builder.append(enable);
builder.append(subtype);
} else {
return;
}
} else {
Log.w(TAG, "Build Latin IME subtype failed: " + " builder length = " + builder.length()
+ "; enable isEmpty :" + TextUtils.isEmpty(enable));
return;
}
// Commit the result
android.provider.Settings.Secure.putString(context.getContentResolver(),
android.provider.Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
String lastInputMethodId = Settings.Secure.getString(context
.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
Log.w(TAG, "DEFAULT_INPUT_METHOD = " + lastInputMethodId);
if(lastInputMethodId.equals(serviceName)) {
Log.w(TAG, "DEFAULT_INPUT_METHOD = com.android.inputmethod.latin/.LatinIME" );
for (InputMethodInfo info : imm.getInputMethodList()) {
if (currentPackageName.equals(info.getPackageName())) {
for (int i = 0; i < info.getSubtypeCount(); i++) {
final InputMethodSubtype subtype = info.getSubtypeAt(i);
final String[] locales = IMEFeatureOption.DEFAULT_LATIN_IME_LANGUAGES;
Log.w(TAG, "i = " + i + ", locales[0] = " + locales[0]);
if((subtype.getLocale()).equals(locales[0])) {
Log.w(TAG, "putString " + subtype.hashCode());
android.provider.Settings.Secure.putInt(context.getContentResolver(),
android.provider.Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtype.hashCode());
}
}
break;
}
}
}
}
/**
* M: Check if the current locale is default or not.
*/
private boolean isDefaultLocale (String locale) {
final String[] locales = IMEFeatureOption.DEFAULT_LATIN_IME_LANGUAGES;
for (String s : locales) {
if (s.equals(locale)) {
return true;
}
}
return false;
}
}
2.修改packages/inputmethods/latinime/java/androidmanifest.xml文件,在此文件中增加LatinImeReceiver的申明
,
<receiver android:name="LatinImeReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
并增加下面3个权限
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
改动如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
coreApp="true"
package="com.android.inputmethod.latin">
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_USER_DICTIONARY" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<application android:label="@string/aosp_android_keyboard_ime_name"
android:icon="@drawable/ic_ime_settings"
android:backupAgent="BackupAgent"
android:killAfterRestore="false">
<service android:name="LatinIME"
android:label="@string/aosp_android_keyboard_ime_name"
android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter>
<action android:name="android.view.InputMethod" />
</intent-filter>
<meta-data android:name="android.view.im" android:resource="@xml/method" />
</service>
<service android:name=".spellcheck.AndroiDSPellCheckerService"
android:label="@string/spell_checker_service_name"
android:permission="android.permission.BIND_TEXT_SERVICE">
<intent-filter>
<action android:name="android.service.textservice.SpellCheckerService" />
</intent-filter>
<meta-data android:name="android.view.textservice.scs" android:resource="@xml/spellchecker" />
</service>
<activity android:name="SettingsActivity" android:label="@string/english_ime_settings"
android:uiOptions="splitActionBarWhenNarrow">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
<activity android:name="com.android.inputmethod.latin.spellcheck.SpellCheckerSettingsActivity"
android:label="@string/android_spell_checker_settings">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
<activity android:name="DebugSettingsActivity" android:label="@string/english_ime_debug_settings">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
<receiver android:name="SuggestionSpanPickedNotificationReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.text.style.SUGGESTION_PICKED" />
</intent-filter>
</receiver>
<receiver android:name="LatinImeReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
3. 还需要在packages/inputmethods/latinime/java/Android.mk文件当中加入LOCAL_JAVA_LIBRARIES := mediatekcommon
才能编译通过;
4. 在packages\apps\Settings\src\com\android\settings\inputmethod\InputMethodAndSubtypeUtil.java文档中的
updateSubtypesPreferenceChecked函数中把break改为continue。
5.在mediatek/config/{projectname}/ProjectConfig.mk中修改如下的config,只需要把默认的语言写在在后边即可
DEFAULT_LATIN_IME_LANGUAGES = hi en_US 其中第一个语言即为默认的输入语言
DEFAULT_LATIN_IME_LANGUAGES后面的格式需要与packages\inputmethods\LatinIME\java\res\xml\method.xml中的
android:imeSubtypeLocale="XXX"字符相匹配,后面添加的语言要与“XXX”相同,例如如果要加印度语言
DEFAULT_LATIN_IME_LANGUAGES = en_US hi 中间用空格隔开。这个后面的格式与MTK_PRODUCT_LOCALES不一样,
MTK_PRODUCT_LOCALES后面是系统的语言以及要编译到系统中的资源的配置。
6.KK的版本还需要增加以下修改:
修改Latin\java的Android.mk,增加LOCAL_PRIVILEGED_MODULE := true
再重新new整个工程,烧机后就可以看到效果。谢谢
【L版本】
(1)新增加一个LatinImeRe.java文件
放在packages\inputmethods\LatinIME\java\src\com\android\inputmethod\latin目录下
文件内容如下:
package com.android.inputmethod.latin;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.provider.Settings;
import android.util.Log;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import android.text.TextUtils;
public class LatinImeRe extends BroadcastReceiver {
private static final String TAG = "LatinImeRe";
@Override
public void onReceive(Context context, Intent intent) {
// Set the default input language at the system boot completed.
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Log.w(TAG, "onReceive");
SharedPreferences sp = context.getSharedPreferences("default_input_language_config",
Context.MODE_PRIVATE);
boolean hasSet = sp.getBoolean("has_set", false);
setDefaultSubtypes(context);
sp.edit().putBoolean("has_set", true).commit();
}
}
/**
* M: Set the default IME subtype.
*/
private void setDefaultSubtypes(Context context) {
final String serviceName = "com.android.inputmethod.latin/.LatinIME";
final String currentPackageName = "com.android.inputmethod.latin";
final String enable = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ENABLED_INPUT_METHODS);
Log.w(TAG, "setDefaultSubtypes() enable :" + enable);
final InputMethodManager imm = (InputMethodManager) context.getSystemService(
Context.INPUT_METHOD_SERVICE);
final StringBuilder builder = new StringBuilder();
// Get sub type hash code
for (InputMethodInfo info : imm.getInputMethodList()) {
if (currentPackageName.equals(info.getPackageName())) {
for (int i = 0; i < info.getSubtypeCount(); i++) {
final InputMethodSubtype subtype = info.getSubtypeAt(i);
final String locale = subtype.getLocale().toString();
//winny
Log.w(TAG, "subtype.getLocale().toString :" + locale);
if (isDefaultLocale(locale)) {
Log.i(TAG, "default enabled subtype locale = " + locale);
builder.append(';');
builder.append(subtype.hashCode());
}
}
break;
}
}
Log.w(TAG, "after for loop :" + builder.toString());
// Insert the sub type
if (builder.length() > 0) {
final String subtype = builder.toString();
builder.setLength(0);
final int index = enable.indexOf(serviceName) + serviceName.length();
if (enable.length() > index) {
builder.append(enable.substring(0, index));
builder.append(subtype);
builder.append(enable.substring(index));
} else if (enable.length() == index) {
builder.append(enable);
builder.append(subtype);
} else {
return;
}
}
// Commit the result
android.provider.Settings.Secure.putString(context.getContentResolver(),
android.provider.Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
}
/**
* M: Check if the current locale is default or not.
*/
private boolean isDefaultLocale (String locale) {
String[] locales= new String[]{"th","fr"};
//将默认的语言在此添加,注意写法与method.xml中的subtype语言保持一致,这里th和fr是泰语和
法语的示例。
for (String s : locales) {
if (s.equals(locale)) {
return true;
}
}
return false;
}
}
(2)修改packages/inputmethods/latinime/java/androidmanifest.xml文件,在此文件中增加
LatinImeRe的申明
<receiver android:name="LatinImeRe" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
并增加下面2个权限
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
(3)在packages/inputmethods/latinime/java/android.xml中修改LOCAL_CERTIFICATE :=
platform,增加LOCAL_PRIVILEGED_MODULE := true ,之后make new编译整个工程。
Latin输入法怎么默认勾选几种语言,Latin IME 不使用系统默认的语言
[SOLUTION]
【GB】
提供简单的sample code,如默认将俄语、英文、法语输入法勾选:
1.新增文件LatinImeReceiver.java
package com.android.inputmethod.latin;
import android.content.BroADCastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethoDMAnager;
//import android.view.inputmethod.InputMethodSubtype;
import android.text.TextUtils;
public class LatinImeReceiver extends BroadcastReceiver {
private static final String TAG = LatinImeReceiver.class.getSIMpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d("LatinImeReceiver", "step1");
SharedPreferences sp = context.getSharedPreferences("com.android.inputmethod.latin_preferences",
Context.MODE_PRIVATE);
boolean hasSet = sp.getBoolean("has_set", false);
if (!hasSet) {
Log.d("LatinImeReceiver", "step2");
Editor editor = sp.edit();
Log.d("LatinImeReceiver", "step3");
editor.putString(LatinIME.PREF_SELECTED_LANGUAGES, "en_US,ru,fr"); //默认将英语、俄语勾选,具体该怎么写
可以参考inputlanguageselection.java中的WHITELIST_LANGUAGES
editor.putBoolean("has_set", true);
Log.d("LatinImeReceiver", "step4");
//editor.coMMIt();
SharedPreferencesCompat.apply(editor);
Log.d("LatinImeReceiver", "step5");
}
}
将其放置到路径packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin文件夹下面
2.注册intent,在packages/inputmethods/LatinIME/java/androidManifest.xml中增加
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
权限
并在最后面加入LatinImeReceiver:
<receiver android:name="LatinImeReceiver" android:enabled="true">
<intent-fiLTEr>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
3 重新编译整个工程
【ICS】
1.在mediatek/config/{projectname}/ProjectConfig.mk中修改如下的config,只需要把默认的语言加在后边即可
DEFAULT_LATIN_IME_LANGUAGES = en_US hi
DEFAULT_LATIN_IME_LANGUAGES后面的格式需要与packages\inputmethods\LatinIME\java\res\xml\method.xml中的
android:imeSubtypeLocale="XXX"字符相匹配,后面添加的语言要与“XXX”相同,例如如果要加印度语言
DEFAULT_LATIN_IME_LANGUAGES = en_US hi 中间用空格隔开。这个后面的格式与mtk_PRODUCT_LOCALES不一样,
MTK_PRODUCT_LOCALES后面是系统的语言以及要编译到系统中的资源的配置。
2.在LatinIMEReceiver.java中 setDefaultSubtypes函数中增加红色部分
private void setDefaultSubtypes(Context context) {
final String serviceName = "com.android.inputmethod.latin/.LatinIME";
final String currentPackageName = "com.android.inputmethod.latin";
final String enable = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ENABLED_INPUT_METHODS);
final InputMethodManager imm = (InputMethodManager) context.getSystEMService(
Context.INPUT_METHOD_SERVICE);
final StringBuilder builder = new StringBuilder();
// Get sub type hash code
for (InputMethodInfo info : imm.getInputMethodList()) {
if (currentPackageName.equals(info.getPackageName())) {
for (int i = 0; i < info.getSubtypeCount(); i++) {
final InputMethodSubtype subtype = info.getSubtypeAt(i);
final String locale = subtype.getLocale().toString();
if (isDefaultLocale(locale)) {
Log.i(TAG, "default enabled subtype locale = " + locale);
builder.append(';');
builder.append(subtype.hashCode());
}
}
break;
}
}
// Insert the sub type
if (builder.length() > 0 && !TextUtils.isEmpty(enable)) {
final String subtype = builder.toString();
builder.setLength(0);
final int index = enable.indexOf(serviceName) + serviceName.length();
if (enable.length() > index) {
builder.append(enable.substring(0, index));
builder.append(subtype);
builder.append(enable.substring(index));
} else if (enable.length() == index) {
builder.append(enable);
builder.append(subtype);
} else {
return;
}
} else {
Log.w(TAG, "Build Latin IME subtype failed: " + " builder length = " + builder.length()
+ "; enable isEmpty :" + TextUtils.isEmpty(enable));
return;
}
// Commit the result
android.provider.Settings.Secure.putString(context.getContentResolver(),
android.provider.Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
String lastInputMethodId = Settings.Secure.getString(context
.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
Log.w(TAG, "DEFAULT_INPUT_METHOD = " + lastInputMethodId);
if(lastInputMethodId.equals(serviceName)) {
Log.w(TAG, "DEFAULT_INPUT_METHOD = com.android.inputmethod.latin/.LatinIME" );
for (InputMethodInfo info : imm.getInputMethodList()) {
if (currentPackageName.equals(info.getPackageName())) {
for (int i = 0; i < info.getSubtypeCount(); i++) {
final InputMethodSubtype subtype = info.getSubtypeAt(i);
final String[] locales = IMEFeatureOption.DEFAULT_LATIN_IME_LANGUAGES;
Log.w(TAG, "i = " + i + ", locales[0] = " + locales[0]);
if((subtype.getLocale()).equals(locales[0])) {
Log.w(TAG, "putString " + subtype.hashCode());
android.provider.Settings.Secure.putInt(context.getContentResolver(),
android.provider.Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtype.hashCode());
}
}
break;
}
}
}
}
【JB、KK】
JB中DEFAULT_LATIN_IME_LANGUAGES config没有用到,没有LatinIMEReceiver.java这个文件。所以要默认勾选多种语
言需要参照ICS的做法增加以下部分:
比如默认选中泰语和英语:
1.增加LatinImeReceiver.java文件放在packages\inputmethods\LatinIME\java\src\com\android\inputmethod\latin
目录下
package com.android.inputmethod.latin;
import com.mediatek.common.featureoption.IMEFeatureOption;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.provider.Settings;
import android.util.Log;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import android.text.TextUtils;
public class LatinImeReceiver extends BroadcastReceiver {
private static final String TAG = LatinImeReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
// Set the default input language at the system boot completed.
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
SharedPreferences sp = context.getSharedPreferences("default_input_language_config",
Context.MODE_PRIVATE);
boolean hasSet = sp.getBoolean("has_set", false);
if (!hasSet) {
setDefaultSubtypes(context);
sp.edit().putBoolean("has_set", true).commit();
}
}
}
/**
* M: Set the default IME subtype.
*/
private void setDefaultSubtypes(Context context) {
final String serviceName = "com.android.inputmethod.latin/.LatinIME";
final String currentPackageName = "com.android.inputmethod.latin";
final String enable = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ENABLED_INPUT_METHODS);
final InputMethodManager imm = (InputMethodManager) context.getSystemService(
Context.INPUT_METHOD_SERVICE);
final StringBuilder builder = new StringBuilder();
// Get sub type hash code
for (InputMethodInfo info : imm.getInputMethodList()) {
if (currentPackageName.equals(info.getPackageName())) {
for (int i = 0; i < info.getSubtypeCount(); i++) {
final InputMethodSubtype subtype = info.getSubtypeAt(i);
final String locale = subtype.getLocale().toString();
if (isDefaultLocale(locale)) {
Log.i(TAG, "default enabled subtype locale = " + locale);
builder.append(';');
builder.append(subtype.hashCode());
}
}
break;
}
}
// Insert the sub type
if (builder.length() > 0 && !TextUtils.isEmpty(enable)) {
final String subtype = builder.toString();
builder.setLength(0);
final int index = enable.indexOf(serviceName) + serviceName.length();
if (enable.length() > index) {
builder.append(enable.substring(0, index));
builder.append(subtype);
builder.append(enable.substring(index));
} else if (enable.length() == index) {
builder.append(enable);
builder.append(subtype);
} else {
return;
}
} else {
Log.w(TAG, "Build Latin IME subtype failed: " + " builder length = " + builder.length()
+ "; enable isEmpty :" + TextUtils.isEmpty(enable));
return;
}
// Commit the result
android.provider.Settings.Secure.putString(context.getContentResolver(),
android.provider.Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
String lastInputMethodId = Settings.Secure.getString(context
.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
Log.w(TAG, "DEFAULT_INPUT_METHOD = " + lastInputMethodId);
if(lastInputMethodId.equals(serviceName)) {
Log.w(TAG, "DEFAULT_INPUT_METHOD = com.android.inputmethod.latin/.LatinIME" );
for (InputMethodInfo info : imm.getInputMethodList()) {
if (currentPackageName.equals(info.getPackageName())) {
for (int i = 0; i < info.getSubtypeCount(); i++) {
final InputMethodSubtype subtype = info.getSubtypeAt(i);
final String[] locales = IMEFeatureOption.DEFAULT_LATIN_IME_LANGUAGES;
Log.w(TAG, "i = " + i + ", locales[0] = " + locales[0]);
if((subtype.getLocale()).equals(locales[0])) {
Log.w(TAG, "putString " + subtype.hashCode());
android.provider.Settings.Secure.putInt(context.getContentResolver(),
android.provider.Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subtype.hashCode());
}
}
break;
}
}
}
}
/**
* M: Check if the current locale is default or not.
*/
private boolean isDefaultLocale (String locale) {
final String[] locales = IMEFeatureOption.DEFAULT_LATIN_IME_LANGUAGES;
for (String s : locales) {
if (s.equals(locale)) {
return true;
}
}
return false;
}
}
2.修改packages/inputmethods/latinime/java/androidmanifest.xml文件,在此文件中增加LatinImeReceiver的申明
,
<receiver android:name="LatinImeReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
并增加下面3个权限
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
改动如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
coreApp="true"
package="com.android.inputmethod.latin">
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_USER_DICTIONARY" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<application android:label="@string/aosp_android_keyboard_ime_name"
android:icon="@drawable/ic_ime_settings"
android:backupAgent="BackupAgent"
android:killAfterRestore="false">
<service android:name="LatinIME"
android:label="@string/aosp_android_keyboard_ime_name"
android:permission="android.permission.BIND_INPUT_METHOD">
<intent-filter>
<action android:name="android.view.InputMethod" />
</intent-filter>
<meta-data android:name="android.view.im" android:resource="@xml/method" />
</service>
<service android:name=".spellcheck.AndroiDSPellCheckerService"
android:label="@string/spell_checker_service_name"
android:permission="android.permission.BIND_TEXT_SERVICE">
<intent-filter>
<action android:name="android.service.textservice.SpellCheckerService" />
</intent-filter>
<meta-data android:name="android.view.textservice.scs" android:resource="@xml/spellchecker" />
</service>
<activity android:name="SettingsActivity" android:label="@string/english_ime_settings"
android:uiOptions="splitActionBarWhenNarrow">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
<activity android:name="com.android.inputmethod.latin.spellcheck.SpellCheckerSettingsActivity"
android:label="@string/android_spell_checker_settings">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
<activity android:name="DebugSettingsActivity" android:label="@string/english_ime_debug_settings">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
<receiver android:name="SuggestionSpanPickedNotificationReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.text.style.SUGGESTION_PICKED" />
</intent-filter>
</receiver>
<receiver android:name="LatinImeReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
3. 还需要在packages/inputmethods/latinime/java/Android.mk文件当中加入LOCAL_JAVA_LIBRARIES := mediatekcommon
才能编译通过;
4. 在packages\apps\Settings\src\com\android\settings\inputmethod\InputMethodAndSubtypeUtil.java文档中的
updateSubtypesPreferenceChecked函数中把break改为continue。
5.在mediatek/config/{projectname}/ProjectConfig.mk中修改如下的config,只需要把默认的语言写在在后边即可
DEFAULT_LATIN_IME_LANGUAGES = hi en_US 其中第一个语言即为默认的输入语言
DEFAULT_LATIN_IME_LANGUAGES后面的格式需要与packages\inputmethods\LatinIME\java\res\xml\method.xml中的
android:imeSubtypeLocale="XXX"字符相匹配,后面添加的语言要与“XXX”相同,例如如果要加印度语言
DEFAULT_LATIN_IME_LANGUAGES = en_US hi 中间用空格隔开。这个后面的格式与MTK_PRODUCT_LOCALES不一样,
MTK_PRODUCT_LOCALES后面是系统的语言以及要编译到系统中的资源的配置。
6.KK的版本还需要增加以下修改:
修改Latin\java的Android.mk,增加LOCAL_PRIVILEGED_MODULE := true
再重新new整个工程,烧机后就可以看到效果。谢谢
【L版本】
(1)新增加一个LatinImeRe.java文件
放在packages\inputmethods\LatinIME\java\src\com\android\inputmethod\latin目录下
文件内容如下:
package com.android.inputmethod.latin;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.provider.Settings;
import android.util.Log;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import android.text.TextUtils;
public class LatinImeRe extends BroadcastReceiver {
private static final String TAG = "LatinImeRe";
@Override
public void onReceive(Context context, Intent intent) {
// Set the default input language at the system boot completed.
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Log.w(TAG, "onReceive");
SharedPreferences sp = context.getSharedPreferences("default_input_language_config",
Context.MODE_PRIVATE);
boolean hasSet = sp.getBoolean("has_set", false);
setDefaultSubtypes(context);
sp.edit().putBoolean("has_set", true).commit();
}
}
/**
* M: Set the default IME subtype.
*/
private void setDefaultSubtypes(Context context) {
final String serviceName = "com.android.inputmethod.latin/.LatinIME";
final String currentPackageName = "com.android.inputmethod.latin";
final String enable = Settings.Secure.getString(context.getContentResolver(),
Settings.Secure.ENABLED_INPUT_METHODS);
Log.w(TAG, "setDefaultSubtypes() enable :" + enable);
final InputMethodManager imm = (InputMethodManager) context.getSystemService(
Context.INPUT_METHOD_SERVICE);
final StringBuilder builder = new StringBuilder();
// Get sub type hash code
for (InputMethodInfo info : imm.getInputMethodList()) {
if (currentPackageName.equals(info.getPackageName())) {
for (int i = 0; i < info.getSubtypeCount(); i++) {
final InputMethodSubtype subtype = info.getSubtypeAt(i);
final String locale = subtype.getLocale().toString();
//winny
Log.w(TAG, "subtype.getLocale().toString :" + locale);
if (isDefaultLocale(locale)) {
Log.i(TAG, "default enabled subtype locale = " + locale);
builder.append(';');
builder.append(subtype.hashCode());
}
}
break;
}
}
Log.w(TAG, "after for loop :" + builder.toString());
// Insert the sub type
if (builder.length() > 0) {
final String subtype = builder.toString();
builder.setLength(0);
final int index = enable.indexOf(serviceName) + serviceName.length();
if (enable.length() > index) {
builder.append(enable.substring(0, index));
builder.append(subtype);
builder.append(enable.substring(index));
} else if (enable.length() == index) {
builder.append(enable);
builder.append(subtype);
} else {
return;
}
}
// Commit the result
android.provider.Settings.Secure.putString(context.getContentResolver(),
android.provider.Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
}
/**
* M: Check if the current locale is default or not.
*/
private boolean isDefaultLocale (String locale) {
String[] locales= new String[]{"th","fr"};
//将默认的语言在此添加,注意写法与method.xml中的subtype语言保持一致,这里th和fr是泰语和
法语的示例。
for (String s : locales) {
if (s.equals(locale)) {
return true;
}
}
return false;
}
}
(2)修改packages/inputmethods/latinime/java/androidmanifest.xml文件,在此文件中增加
LatinImeRe的申明
<receiver android:name="LatinImeRe" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
并增加下面2个权限
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
(3)在packages/inputmethods/latinime/java/android.xml中修改LOCAL_CERTIFICATE :=
platform,增加LOCAL_PRIVILEGED_MODULE := true ,之后make new编译整个工程。