安卓源码--]Latin输入法怎么默认勾选几种语言

转载 2015年11月18日 11:13:33
[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 := mediatek-common才能编译通过;

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编译整个工程。

Android系统之路(初识MTK) ------ 设置系统默认语言/客制化可选语言/设置默认时区

Android系统之路(初识MTK) ------ 设置系统默认语言/客制化可选语言/设置默认时区...
  • jspping
  • jspping
  • 2016年01月07日 14:47
  • 2991

WINCE 6.0 输入法添加

WINCE6.0里面有两种IME输入法,一个是MSPY 3.0 ,另一个是Pocket IME.这两个任选其一都可以。   组件添加: 一、POCKET IME添加: 1.在C...

Android默认输入法语言的修改以及SettingsProvider作用

Android源码中默认的有三种输入法:英文,中文,日文。对应的工程代码路径为: /packages/inputmethods/LatinIME/ /packages/inputmethods/O...
  • BENKG
  • BENKG
  • 2017年03月14日 12:39
  • 471

Latin输入法怎么默认勾选几种语言

Latin输入法怎么默认勾选几种语言 [DESCRIPTION] Latin输入法怎么默认勾选几种语言,Latin IME 不使用系统默认的语言 [SOLUTION] 【GB】 ...

安卓源码--修改设置语言选择列表

查找过程切换中文:setting下grep查找:语言和输入法 找到两个 自我感觉是第二个: 在manifest查到如下内容: 发现跳转到InputMethodAndLanguage...

Android中Notification的framework层讲解【安卓源码解析四】

Android中Notification的framework层讲解【安卓源码解析四】 分类: android源码解析 2011-12-18 17:23 251人阅读 评论(0) 收藏 ...

学习整理——从安卓源码上看Activity

安卓开发的四大组件是Activity, service, broadcast receiver, 和content provider。作为业余的开发者,可能不需要太深入理解这些组件的内部实现,以及运行...

位运算,安卓源码里的各种Flag

一直看不懂源码里的各种位运算,各种flag与非或非的,头大。 View里面有各种状态,都是用flag常量来标记的,然后用一个成员变量跟这些值通过位运算,来判断view当前的状态。 奈何在学校的时候没有...

Android中铃声总结【安卓源码解析一】

最近研究源码程序,改了改手机短信铃声的源码,最近总结了下铃声的代码,写个activity继承PreferenceActivity有:手机短信铃声,手机铃声,闹钟铃声,还有sdcard中的铃声,通过选择...

安卓源码总体结构(2)基础知识汇总

安卓源码总体结构(2)基础知识汇总
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:安卓源码--]Latin输入法怎么默认勾选几种语言
举报原因:
原因补充:

(最多只允许输入30个字)