Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 88b861c8 authored by Robert Greenwalt's avatar Robert Greenwalt Committed by The Android Open Source Project
Browse files

AI 143900: am: CL 143898 Attempt to set the device Locale (if not already set) at phone

  init based on the phone's reported carrier ID.
  Uses a core system resource string-array to contain the mapping
  of carrier ID -> default locale.  This should be set per project in
  an overlay.
  Original author: rgreenwalt
  Merged from: //branches/cupcake/...

Automated import of CL 143900
parent 958b9adc
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -2322,4 +2322,7 @@
    <!-- This string appears (on two lines) when you type a number into contacts search, to let you create a contact whose phone number is the number you typed.  The first line will be in bigger type than the second. -->
    <string name="create_contact_using">Create contact\nusing <xliff:g id="number" example="555">%s</xliff:g></string>

    <!-- This string array should be overridden by the manufacture to present a list of carrier-id,locale pairs.  This is used at startup to set a default locale by checking the system property ro.carrier for the carrier-id and searching through this array -->
    <string-array translatable="false" name="carrier_locales">
    </string-array>    
</resources>
+94 −0
Original line number Diff line number Diff line
@@ -16,15 +16,22 @@

package com.android.internal.telephony;

import android.app.ActivityManagerNative;
import android.app.IActivityManager;
import android.content.Context;
import android.content.res.Configuration;
import android.os.AsyncResult;
import android.os.Handler;
import android.os.Looper;
import android.os.RegistrantList;
import android.os.SystemProperties;
import android.telephony.ServiceState;
import android.util.Log;
import com.android.internal.R;
import com.android.internal.telephony.test.SimulatedRadioControl;

import java.util.List;
import java.util.Locale;

/**
 * (<em>Not for SDK use</em>) 
@@ -109,6 +116,8 @@ public abstract class PhoneBase implements Phone {
        this.mContext = context;
        mLooper = Looper.myLooper();

        setLocaleByCarrier();

        setUnitTestMode(unitTestMode);
    }

@@ -307,4 +316,89 @@ public abstract class PhoneBase implements Phone {
        }
    }

    /**
     * Set the locale by matching the carrier string in
     * a string-array resource
     */
    private void setLocaleByCarrier() {
        String carrier = SystemProperties.get("ro.carrier");

        if (null == carrier || 0 == carrier.length()) {
            return;
        }

        CharSequence[] carrierLocales = mContext.
                getResources().getTextArray(R.array.carrier_locales);

        for (int i = 0; i < carrierLocales.length-1; i+=2) {
            String c = carrierLocales[i].toString();
            String l = carrierLocales[i+1].toString();
            if (carrier.equals(c)) {
                String language = l.substring(0, 2);
                String country = "";
                if (l.length() >=5) {
                    country = l.substring(3, 5);
                }
                setSystemLocale(language, country);
                return;
            }
        }
    }

    /**
     * Utility code to set the system locale if it's not set already
     * @param langauge Two character language code desired
     * @param country Two character country code desired
     *
     *  {@hide}
     */
    public void setSystemLocale(String language, String country) {
        String l = SystemProperties.get("persist.sys.language");
        String c = SystemProperties.get("persist.sys.country");

        if (null == language) {
            return; // no match possible
        }
        language.toLowerCase();
        if (null != country) {
            country = "";
        }
        country = country.toUpperCase();

        if((null == l || 0 == l.length()) && (null == c || 0 == c.length())) {
            try {
                // try to find a good match
                String[] locales = mContext.getAssets().getLocales();
                final int N = locales.length;
                String bestMatch = null;
                for(int i = 0; i < N; i++) {
                    if (locales[i]!=null && locales[i].length() >= 2 &&
                            locales[i].substring(0,2).equals(language)) {
                        if (locales[i].length() >= 5 &&
                                locales[i].substring(3,5).equals(country)) {
                            bestMatch = locales[i];
                            break;
                        } else if (null == bestMatch) {
                            bestMatch = locales[i];
                        }
                    }
                }
                if (null != bestMatch) {
                    IActivityManager am = ActivityManagerNative.getDefault();
                    Configuration config = am.getConfiguration();

                    if (bestMatch.length() >= 5) {
                        config.locale = new Locale(bestMatch.substring(0,2),
                                                   bestMatch.substring(3,5));
                    } else {
                        config.locale = new Locale(bestMatch.substring(0,2));
                    }
                    config.userSetLocale = true;
                    am.updateConfiguration(config);
                }
            } catch (Exception e) {
                // Intentionally left blank
            }
        }
    }
}
+4 −46
Original line number Diff line number Diff line
@@ -27,10 +27,6 @@ import android.os.SystemProperties;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import java.util.ArrayList;
import android.app.ActivityManagerNative;
import android.app.IActivityManager;
import java.util.Locale;
import android.content.res.Configuration;

import static com.android.internal.telephony.TelephonyProperties.*;
import com.android.internal.telephony.SimCard;
@@ -554,48 +550,10 @@ public final class SIMRecords extends Handler implements SimConstants
     * @param mcc Mobile Country Code of the SIM
     */
    private void setLocaleFromMccIfNeeded(int mcc) {
        String language = SystemProperties.get("persist.sys.language");
        String country = SystemProperties.get("persist.sys.country");
        Log.d(LOG_TAG,"setLocaleFromMcc");
        if((language == null || language.length() == 0) && (country == null || country.length() == 0)) {
            try {
                language = MccTable.defaultLanguageForMcc(mcc);
                country = MccTable.countryCodeForMcc(mcc).toUpperCase();
                // try to find a good match
                String[] locales = phone.getContext().getAssets().getLocales();
                final int N = locales.length;
                String bestMatch = null;
                for(int i = 0; i < N; i++) {
                    Log.d(LOG_TAG," trying "+locales[i]);
                    if(locales[i]!=null && locales[i].length() >= 2 &&
                       locales[i].substring(0,2).equals(language)) {
                        if(locales[i].length() >= 5 &&
                           locales[i].substring(3,5).equals(country)) {
                            bestMatch = locales[i];
                            break;
                        } else if(bestMatch == null) {
                            bestMatch = locales[i];
                        }
                    }
                }
                Log.d(LOG_TAG," got bestmatch = "+bestMatch);
                if(bestMatch != null) {
                    IActivityManager am = ActivityManagerNative.getDefault();
                    Configuration config = am.getConfiguration();
        String language = MccTable.defaultLanguageForMcc(mcc);
        String country = MccTable.countryCodeForMcc(mcc);

                    if(bestMatch.length() >= 5) {
                        config.locale = new Locale(bestMatch.substring(0,2),
                                                   bestMatch.substring(3,5));
                    } else {
                        config.locale = new Locale(bestMatch.substring(0,2));
                    }
                    config.userSetLocale = true;
                    am.updateConfiguration(config);
               }
           } catch (Exception e) {
                // Intentionally left blank
           }
        }
        phone.setSystemLocale(language, country);
    }

    //***** Overridden from Handler