How to modify the default input method under Android 4.3 source code

Source: Internet
Author: User

Should the project (Android 4.3) need to modify the default input method, the original thought is that this should be very simple to scatter. With Google's ~, found online mainly listed below this method:

Add a property to Frameworks\base\core\res\res\values\config.xml:
<!--Leo Add-->
<string name= "Config_default_input_method" >com.android.inputmethod.pinyin/. Pinyinime</string>
In Frameworks\base\services\java\com\android\server\inputmethodmanagerservice.java
The method buildinputmethodlistlocked () adds:
String defaultime = Settings.Secure.getString (mcontext
. Getcontentresolver (), Settings.Secure.DEFAULT_INPUT_METHOD);
if (defaultime = = null)
{
Final Resources res = mcontext.getresources ();
Try
{
Frameworks\base\core\res\res\values\config.xml
String myime = res.getstring (Com.android.internal.r.string.config_default_input_method);
if (myime! = null && myime.length () > 0)
{
Settings.Secure.putString (Mcontext.getcontentresolver (),
Settings.Secure.DEFAULT_INPUT_METHOD,
Myime);
}
}
catch (Exception e)
{
}
}

But in their own source code changes, brush machine debugging found that Wood has a role, and this direct modification of the source is not bad?

Later found that this is 4.2 of the source code, is not 4.3 of the same. Well, Wood has a way to start analysis from the source! (Senior guide, Google did not, just look at the source)

Log tracking, the first time the system is initialized to call the following function, and the subsequent boot of the function will not be called, and the following function is to reset the available Input method to select (In the Frameworks/base/core/java/com/android/ In Internal/inputmethod/inputmethodutils.java)

private void resetdefaultimelocked (context context) {
Do not reset the default (current) IME if it is a 3rd-party ime
if (Mcurmethodid! = null
&&! Inputmethodutils.issystemime (Mmethodmap.get (Mcurmethodid))) {
Return
}
  
Inputmethodinfo Defim = null;
for (Inputmethodinfo imi:mmethodlist) {
if (Defim = = null) {
if (Inputmethodutils.isvalidsystemdefaultime (
Msystemready, IMI, context)) {
Defim = IMI;
SLOG.I (TAG, "Selected default:" + Imi.getid ());
}
}
}
if (Defim = = null && mmethodlist.size () > 0) {
Defim = Inputmethodutils.getmostapplicabledefaultime (
Msettings.getenabledinputmethodlistlocked ());
SLOG.I (TAG, "No default found, using" + Defim.getid ());
}

Note The yellow bold part, when the system does not have the default input method, choose the most applicable input method (lying grass, also with this), and call the Inputmethodutils.getmostapplicabledefaultime function, OK continue to see the following function:

public static Inputmethodinfo Getmostapplicabledefaultime (
List<inputmethodinfo> enabledimes) {
if (enabledimes! = null && enabledimes.size () > 0) {
We ' d prefer to fall the back on a system IME, since this is safer.
int i = Enabledimes.size ();
int firstfoundsystemime =-1;
while (i > 0) {
i--;
Final Inputmethodinfo IMI = Enabledimes.get (i);
if (Inputmethodutils.issystemimethathasenglishkeyboardsubtype (IMI)
&&!imi.isauxiliaryime ()) {
Return IMI;
}
if (Firstfoundsystemime < 0 && Inputmethodutils.issystemime (IMI)
&&!imi.isauxiliaryime ()) {
Firstfoundsystemime = i;
}
}
Return Enabledimes.get (Math.max (firstfoundsystemime, 0));


The same note the Yellow Place code (the red part indicates whether it is a legal system input method, but also contains the English sub-keyboard and other characteristics, the normal input method has no characteristics, but the AOSP keyboard has this feature, the system will return directly to the input method). All right, here you go! Since you are so stipulated, I will be forced to change, so to modify the default input, can be modified here, when the system detects the AOSP keyboard, do not directly return, but continue to detect the system's available keyboard (when the system first starts, generally only the system keyboard is available, so you should set all the input methods to be available , modify the following function:

public void Enableallimesifthereisnoenabledime () {
if (! Textutils.isempty (Getenabledinputmethodsstr ())) {
StringBuilder sb = new StringBuilder ();
Final int N = Mmethodlist.size ();
for (int i = 0; i < N; i++) {
Inputmethodinfo IMI = Mmethodlist.get (i);
SLOG.I (TAG, "Adding:" + Imi.getid ());
if (i > 0) sb.append (': ');
Sb.append (Imi.getid ());
}
Putenabledinputmethodsstr (Sb.tostring ());
}
}

Set all the input methods to be available. Then the last function can find the input method you want, then in the previous function plus a judging condition, determine whether the loop input method for you want the default input method, yes, then set:
Firstfoundsystemime = i;

The judging conditions are as follows:

if (Inputmethodutils.issystemimethathasenglishkeyboardsubtype (IMI)
&&!imi.isauxiliaryime ()) {
continue;
}

for (int m = 0; m < enabledimes.size (); m + +) {
if (Enabledimes.get (m). GetId () = = "The Input method package name you want to set")
Firstfoundsystemime = m;
}
  
This can be modified to other input methods.
  

Compile the system, burn write, start can see the default input method from the traditional Android comes with, become you want input method.


PS: Novice, just write this analysis process article

How to modify the default input method under Android 4.3 source code

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.