安卓源码--]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编译整个工程。

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

Latin输入法怎么默认勾选几种语言 [DESCRIPTION] Latin输入法怎么默认勾选几种语言,Latin IME 不使用系统默认的语言 [SOLUTION] 【GB】 ...
  • sandform
  • sandform
  • 2016年06月20日 17:21
  • 248

Android 修改输入法的输入语言

Android原生系统中,默认的输入法是“Android键盘(AOSP)”,此输入法包括55种输入语言,具体有哪一些,去看看原生系统里的就知道了,目测没有中文(不知道为啥)。 还有一种“Google语...
  • u010216716
  • u010216716
  • 2015年01月19日 22:05
  • 1128

Android源码修改默认输入法

前一阵子,应泰国客户需求,需要在Android TV系统定制一个多语言输入法,至少支持中、英、泰三种语言。拿到这个任务,对于至今还是小白的我来说,当然先去google一下有没有大神专门做过符合要求的输...
  • johnWcheung
  • johnWcheung
  • 2016年02月22日 22:58
  • 4918

android 5.1.1 下修改出厂默认输入法

1、 需要提前将用到的默认输入法(.apk)内置到系统中 2、将系统中所有的输入法设置为可用,修改 frameworks/base/core/Java/com/Android/intern...
  • u011467537
  • u011467537
  • 2016年10月17日 19:51
  • 1643

android N0版本默认输入法语言

7.0系统自带的输入法中语言默认为系统语言,客户要求将输入法语言改为其他 在ProjectConfig.mk中: DEFAULT_LATIN_IME_LANGUAGES = en-US fr ru...
  • qq_32143319
  • qq_32143319
  • 2017年05月16日 15:07
  • 323

Android源码配置默认输入法

声明 Android源码修改默认输入法 关于配置默认输入法的包名和类名声明**郑重声明:博文为原创内容,可以转载或引用,但必须在明显位置标明原文作者和出处,未经同意不得擅自修改本文内容!博客地址:ht...
  • luzhenrong45
  • luzhenrong45
  • 2017年10月10日 17:50
  • 1250

android4.4修改出厂默认输入法

1、 需要提前将用到的默认输入法(.apk)内置到系统中 2、将系统中所有的输入法设置为可用,修改 frameworks/base/core/java/com/android/internal/inp...
  • yuxiangyunei
  • yuxiangyunei
  • 2015年03月22日 23:46
  • 7173

7.0 Android系统修改默认输入法后,切换系统语言时输入法自动切到AOSP的解决办法

Android 7.0设置默认输入法参考下面的文章 Android修改默认输入法 切换系统语言之后,系统会默认将输入法切换到Andorid Keyboard(AOSP) /frameworks/...
  • sinat_24946363
  • sinat_24946363
  • 2018年01月31日 19:33
  • 151

Android 如何修改默认输入法

前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net       ...
  • luckkof
  • luckkof
  • 2013年10月31日 15:10
  • 4709

LatinIME输入法分析

输入法的设置在res/xml/method.xml的标签中,主要设置两个属性:android:settingsActivity,输入法的设置程序入口。android:isDefault,这个输入法是不...
  • a345017062
  • a345017062
  • 2011年01月12日 08:49
  • 14571
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:安卓源码--]Latin输入法怎么默认勾选几种语言
举报原因:
原因补充:

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