From f05a98cbff4647de518daf349fe7f69a4993a6fb Mon Sep 17 00:00:00 2001 From: Robert Greenwalt <> Date: Tue, 31 Mar 2009 18:00:50 -0700 Subject: AI 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. BUG=1731384 Automated import of CL 143898 --- .../com/android/internal/telephony/PhoneBase.java | 94 ++++++++++++++++++++++ .../android/internal/telephony/gsm/SIMRecords.java | 50 +----------- 2 files changed, 98 insertions(+), 46 deletions(-) (limited to 'telephony/java/com') diff --git a/telephony/java/com/android/internal/telephony/PhoneBase.java b/telephony/java/com/android/internal/telephony/PhoneBase.java index 580814f..4fb5f61 100644 --- a/telephony/java/com/android/internal/telephony/PhoneBase.java +++ b/telephony/java/com/android/internal/telephony/PhoneBase.java @@ -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; /** * (Not for SDK use) @@ -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 + } + } + } } diff --git a/telephony/java/com/android/internal/telephony/gsm/SIMRecords.java b/telephony/java/com/android/internal/telephony/gsm/SIMRecords.java index a62f6cf..4467536 100644 --- a/telephony/java/com/android/internal/telephony/gsm/SIMRecords.java +++ b/telephony/java/com/android/internal/telephony/gsm/SIMRecords.java @@ -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(); - - 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 - } - } + String language = MccTable.defaultLanguageForMcc(mcc); + String country = MccTable.countryCodeForMcc(mcc); + + phone.setSystemLocale(language, country); } //***** Overridden from Handler -- cgit v1.1