Android default IME language modification and Settingsprovider effect

Source: Internet
Author: User

Android source code in the default has three kinds of input methods: English, Chinese, Japanese. The corresponding project code path is:
<android_root>/packages/inputmethods/latinime/
<android_root>/packages/inputmethods/openwnn/
<android_root>/packages/inputmethods/pinyinime/

In general, the default is the choice of latinime input method, but the Android system is selected by default system language as input method, for example, we want to use Chinese input methods,
You need to switch the system language to Chinese, or uncheck the system language, active check Chinese, but how do we change the default language input method?
The main concern is to focus on the code of 3 modules:
(1) Setting source code
(2) Settingsprovider source code
(3) Latinime Input method source code

Method One :(modify the default input language to English and Thai, pro-Test OK)
(1) Modify Packages\inputmethods\latinime\java\androidmanifest.xml, increase

 <uses-permissionAndroid:name= "Android.permission.WRITE_SETTINGS" /> <uses-permissionAndroid:name= "Android.permission.WRITE_SECURE_SETTINGS" /> <uses-permissionAndroid:name= "Android.permission.RECEIVE_BOOT_COMPLETED" />  <receiverAndroid:name= "Latinimereceiver"android:enabled= "true">  <Intent-filter>   <ActionAndroid:name= "Android.intent.action.BOOT_COMPLETED" />   </Intent-filter> </receiver>


(2) in the Packages\inputmethods\latinime\java\src\com\android\inputmethod\latin directory to add Latinimereceiver.java file, the source code is as follows

 PackageCom.android.inputmethod.latin; ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;Importandroid.content.SharedPreferences;Importandroid.provider.Settings;ImportAndroid.util.Log;ImportAndroid.view.inputmethod.InputMethodInfo;ImportAndroid.view.inputmethod.InputMethodManager;ImportAndroid.view.inputmethod.InputMethodSubtype;Importandroid.text.TextUtils;ImportAndroid.content.SharedPreferences.Editor;ImportAndroid.preference.PreferenceManager; Public classLatinimereceiverextendsBroadcastreceiver {Private Static FinalString TAG = Latinimereceiver.class. Getsimplename (); Private Static FinalString[] Default_latin_ime_languages = {"en_US", "th"};//the language of the IME is turned on by default@Override Public voidOnReceive (Context context, Intent Intent) {//Set The default input language at the system boot completed.                if(Intent.ACTION_BOOT_COMPLETED.equals (Intent.getaction ())) {log.i (TAG,"Onreceive:action_boot_completed"); Sharedpreferences SP= Context.getsharedpreferences ("Default_input_language_config", context.mode_private); BooleanHasset = Sp.getboolean ("Has_set",false); if(!Hasset)                {setdefaultsubtypes (context); Sp.edit (). Putboolean ("Has_set",true). commit (); }        }    }         /*** M:set The default IME subtype. */          Private voidSetdefaultsubtypes (Context context) {log.i (TAG,"Setdefaultsubtypes"); FinalString serviceName = "com.android.inputmethod.latin/. Latinime "; FinalString currentpackagename = "Com.android.inputmethod.latin"; FinalString enable =Settings.Secure.getString (Context.getcontentresolver (), Setti Ngs.        Secure.enabled_input_methods); LOG.I (TAG,"Enable=" +enable);//com.android.inputmethod.latin/. Latinime                FinalInputmethodmanager IMM =(Inputmethodmanager) Context.getsystemservice (Context.input_method_service); FinalStringBuilder Builder =NewStringBuilder (); //Get Sub Type hash code         for(Inputmethodinfo info:imm.getInputMethodList ()) {if(Currentpackagename.equals (Info.getpackagename ())) {log.i (TAG,"Info.getsubtypecount () =" +info.getsubtypecount ());// -                 for(inti = 0; I < Info.getsubtypecount (); i++) {                      FinalInputmethodsubtype subtype =info.getsubtypeat (i); FinalString locale =Subtype.getlocale (). toString (); LOG.I (TAG,"Locale=" +locale); 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)) {            FinalString subtype =builder.tostring (); Builder.setlength (0); Final intindex = 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; }                          /*android/packages/inputmethods/latinime/java/res/xml/method.xml-921088104;529847764 represents en_us and th */log.i (TAG,"Commoit:" +builder.tostring ());//com.android.inputmethod.latin/. latinime;-921088104;529847764//Commit The resultandroid.provider.Settings.Secure.putString (Context.getcontentresolver (), Android Oid.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);//com.android.inputmethod.latin/. Latinime                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(inti = 0; I < Info.getsubtypecount (); i++) {                         FinalInputmethodsubtype subtype =info.getsubtypeat (i); FinalString[] locales =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.Se Ttings.                        Secure.selected_input_method_subtype, Subtype.hashcode ()); }                     }                  }            }        }    }             /*** M:check If the current locale was default or not. */      Private BooleanIsdefaultlocale (String locale) {FinalString[] locales =default_latin_ime_languages;  for(String s:locales) {if(s.equals (locale)) {return true; }        }                return false; }         }  


Method Two :
There is also a modification scenario: (in amlogic validation is OK)
(1) First frameworks\base\packages\settingsprovider\res\values\defaults.xml add the following statement
<string name= "Def_input_methods" >com.android.inputmethod.latin/. Latinime;529847764;-921088104</string>

Add English and Thai IME, android/packages/inputmethods/latinime/java/res/xml/method.xml defined
-921088104;529847764 represents en_us and th respectively

(2) and then in Frameworks\base\packages\settingsprovider\src\com\android\providers\settings\databasehelper.java
Add the following code
loadstringsetting (stmt, Settings.Secure.ENABLED_INPUT_METHODS, r.string.def_input_methods);


Frameworks/base/packages/settingsprovider's role
When we call Android.provider.Settings to modify some settings, settings calls the real Settingsprovider to access the database.
Android put Settingsprovider's code under Frameworks/base/packages.

Android Framework system default settings modified
Modify the settings source code can modify the system settings, settings data is stored in com.android.providers.settings/databases/settings.db, if you want to modify the system startup after loading the default value, One way is to modify the value of the settings.db directly, and the other is to modify the Settingsprovider default value
The Settings app can configure various settings for the Android system, and the default values for these settings are read from the database by Settingsprovider in frameworks.
So where did the data come from the first time it was powered on?
Frameworks/base/packages/settingsprovider/res/values/defaults.xml This file is used to store the Android system's default settings
If you want to define what is not in the defaults.xml, after adding it here, you need to modify the frameworks/base/packages/settingsprovider/src/com/android/providers/ Settings/databasehelper.java, add your own storage code.
For example:
600000 Setting the default value for the off-screen timeout time
102 Setting the default value for brightness
False to set whether to allow default values for non-market applications to be installed


Reference:
Language Settings for Android (i)
http://blog.csdn.net/seker_xinjian/article/details/6288957

Latin IME How to select several languages by default
Http://wenku.baidu.com/link?url=gAscqnKoMNOi_wzR3LEsk9kw-Hsp6k-hkWsW3_ Jvyz3smmxkenodd6xjrts9brndas4iy2ium8nqaxj05j4numbfdfe5-7nl9p9bviwqfcm

Android Framework system default settings modified
http://blog.csdn.net/tiantian715/article/details/7739294

Add a new setting to the Android Settings.db database
Http://www.2cto.com/kf/201303/198067.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.