diff --git a/res/values/strings.xml b/res/values/strings.xml index 8791d7a..afbfaa9 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -94,4 +94,5 @@ Setup a secondary unlock method Add your fingerprint Setup screen lock + %1$s SIM detected diff --git a/src/com/cyanogenmod/setupwizard/setup/WelcomePage.java b/src/com/cyanogenmod/setupwizard/setup/WelcomePage.java index 3c4e510..248420e 100644 --- a/src/com/cyanogenmod/setupwizard/setup/WelcomePage.java +++ b/src/com/cyanogenmod/setupwizard/setup/WelcomePage.java @@ -27,6 +27,7 @@ import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.content.res.Resources; +import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.telephony.TelephonyManager; @@ -34,6 +35,7 @@ import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.NumberPicker; +import android.widget.Toast; import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.SetupWizardApp; @@ -166,7 +168,7 @@ public class WelcomePage extends SetupPage { public void simChanged() { if (mWelcomeFragment != null) { - mWelcomeFragment.simChanged(); + mWelcomeFragment.fetchAndUpdateSimLocale(); } } @@ -176,9 +178,9 @@ public class WelcomePage extends SetupPage { private Locale mInitialLocale; private Locale mCurrentLocale; private int[] mAdapterIndices; - private boolean mUserPickedLocale; + private boolean mIgnoreSimLocale; private LocalePicker mLanguagePicker; - + private FetchUpdateSimLocaleTask mFetchUpdateSimLocaleTask; private final Handler mHandler = new Handler(); private final Runnable mUpdateLocale = new Runnable() { @@ -201,26 +203,10 @@ public class WelcomePage extends SetupPage { } } - private Locale getSimLocale() { - Activity activity = getActivity(); - if (activity != null) { - TelephonyManager telephonyManager = (TelephonyManager) activity. - getSystemService(Context.TELEPHONY_SERVICE); - String locale = telephonyManager.getLocaleFromDefaultSim(); - if (locale != null) { - return Locale.forLanguageTag(locale); - } - } - return null; - } - private void loadLanguages() { mLocaleAdapter = com.android.internal.app.LocalePicker.constructAdapter(getActivity(), R.layout.locale_picker_item, R.id.locale); mCurrentLocale = mInitialLocale = Locale.getDefault(); - Locale simLocale = getSimLocale(); - if (simLocale != null) { - mCurrentLocale = simLocale; - } + fetchAndUpdateSimLocale(); mAdapterIndices = new int[mLocaleAdapter.getCount()]; int currentLocaleIndex = 0; String [] labels = new String[mLocaleAdapter.getCount()]; @@ -242,10 +228,18 @@ public class WelcomePage extends SetupPage { setLocaleFromPicker(); } }); + mLanguagePicker.setOnScrollListener(new LocalePicker.OnScrollListener() { + @Override + public void onScrollStateChange(LocalePicker view, int scrollState) { + if (scrollState == SCROLL_STATE_TOUCH_SCROLL) { + mIgnoreSimLocale = true; + } + } + }); } private void setLocaleFromPicker() { - mUserPickedLocale = true; + mIgnoreSimLocale = true; int i = mAdapterIndices[mLanguagePicker.getValue()]; final com.android.internal.app.LocalePicker.LocaleInfo localLocaleInfo = mLocaleAdapter.getItem(i); onLocaleChanged(localLocaleInfo.getLocale()); @@ -277,13 +271,42 @@ public class WelcomePage extends SetupPage { } } - public void simChanged() { - if (mUserPickedLocale || isDetached()) { + public void fetchAndUpdateSimLocale() { + if (mIgnoreSimLocale || isDetached()) { return; } - Locale simLocale = getSimLocale(); - if (simLocale != null && !simLocale.equals(mCurrentLocale)) { - onLocaleChanged(simLocale); + if (mFetchUpdateSimLocaleTask != null) { + mFetchUpdateSimLocaleTask.cancel(true); + } + mFetchUpdateSimLocaleTask = new FetchUpdateSimLocaleTask(); + mFetchUpdateSimLocaleTask.execute(); + } + + private class FetchUpdateSimLocaleTask extends AsyncTask { + @Override + protected Locale doInBackground(Void... params) { + Activity activity = getActivity(); + if (activity != null) { + TelephonyManager telephonyManager = (TelephonyManager) activity. + getSystemService(Context.TELEPHONY_SERVICE); + String locale = telephonyManager.getLocaleFromDefaultSim(); + if (locale != null) { + return Locale.forLanguageTag(locale); + } + } + return null; + } + + @Override + protected void onPostExecute(Locale simLocale) { + if (simLocale != null && !simLocale.equals(mCurrentLocale)) { + if (!mIgnoreSimLocale && !isDetached()) { + String label = getString(R.string.sim_locale_changed, + simLocale.getDisplayName()); + Toast.makeText(getActivity(), label, Toast.LENGTH_SHORT).show(); + onLocaleChanged(simLocale); + } + } } } }