SetupWizard : Ensure we don't override user locale picks

Take scroll events into consideration when deciding whether to override
user locale with sim locale.

Also move locale fetching to be async so we don't cause any unecessary
blocking.

CYNGNOS-1521

Change-Id: I0c4c1b89dd227f661c1a1cdcce445349fe4a206f
This commit is contained in:
Danesh M 2016-01-25 10:41:33 -08:00
parent d59304c730
commit 00ad2213ff
2 changed files with 50 additions and 26 deletions

View File

@ -94,4 +94,5 @@
<string name="fingerprint_setup_backup_lock_method">Setup a secondary unlock method</string> <string name="fingerprint_setup_backup_lock_method">Setup a secondary unlock method</string>
<string name="fingerprint_setup_add_fingerprint">Add your fingerprint</string> <string name="fingerprint_setup_add_fingerprint">Add your fingerprint</string>
<string name="fingerprint_setup_screen_lock_setup">Setup screen lock</string> <string name="fingerprint_setup_screen_lock_setup">Setup screen lock</string>
<string name="sim_locale_changed">%1$s SIM detected</string>
</resources> </resources>

View File

@ -27,6 +27,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
@ -34,6 +35,7 @@ import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.NumberPicker; import android.widget.NumberPicker;
import android.widget.Toast;
import com.cyanogenmod.setupwizard.R; import com.cyanogenmod.setupwizard.R;
import com.cyanogenmod.setupwizard.SetupWizardApp; import com.cyanogenmod.setupwizard.SetupWizardApp;
@ -166,7 +168,7 @@ public class WelcomePage extends SetupPage {
public void simChanged() { public void simChanged() {
if (mWelcomeFragment != null) { if (mWelcomeFragment != null) {
mWelcomeFragment.simChanged(); mWelcomeFragment.fetchAndUpdateSimLocale();
} }
} }
@ -176,9 +178,9 @@ public class WelcomePage extends SetupPage {
private Locale mInitialLocale; private Locale mInitialLocale;
private Locale mCurrentLocale; private Locale mCurrentLocale;
private int[] mAdapterIndices; private int[] mAdapterIndices;
private boolean mUserPickedLocale; private boolean mIgnoreSimLocale;
private LocalePicker mLanguagePicker; private LocalePicker mLanguagePicker;
private FetchUpdateSimLocaleTask mFetchUpdateSimLocaleTask;
private final Handler mHandler = new Handler(); private final Handler mHandler = new Handler();
private final Runnable mUpdateLocale = new Runnable() { 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() { private void loadLanguages() {
mLocaleAdapter = com.android.internal.app.LocalePicker.constructAdapter(getActivity(), R.layout.locale_picker_item, R.id.locale); mLocaleAdapter = com.android.internal.app.LocalePicker.constructAdapter(getActivity(), R.layout.locale_picker_item, R.id.locale);
mCurrentLocale = mInitialLocale = Locale.getDefault(); mCurrentLocale = mInitialLocale = Locale.getDefault();
Locale simLocale = getSimLocale(); fetchAndUpdateSimLocale();
if (simLocale != null) {
mCurrentLocale = simLocale;
}
mAdapterIndices = new int[mLocaleAdapter.getCount()]; mAdapterIndices = new int[mLocaleAdapter.getCount()];
int currentLocaleIndex = 0; int currentLocaleIndex = 0;
String [] labels = new String[mLocaleAdapter.getCount()]; String [] labels = new String[mLocaleAdapter.getCount()];
@ -242,10 +228,18 @@ public class WelcomePage extends SetupPage {
setLocaleFromPicker(); 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() { private void setLocaleFromPicker() {
mUserPickedLocale = true; mIgnoreSimLocale = true;
int i = mAdapterIndices[mLanguagePicker.getValue()]; int i = mAdapterIndices[mLanguagePicker.getValue()];
final com.android.internal.app.LocalePicker.LocaleInfo localLocaleInfo = mLocaleAdapter.getItem(i); final com.android.internal.app.LocalePicker.LocaleInfo localLocaleInfo = mLocaleAdapter.getItem(i);
onLocaleChanged(localLocaleInfo.getLocale()); onLocaleChanged(localLocaleInfo.getLocale());
@ -277,13 +271,42 @@ public class WelcomePage extends SetupPage {
} }
} }
public void simChanged() { public void fetchAndUpdateSimLocale() {
if (mUserPickedLocale || isDetached()) { if (mIgnoreSimLocale || isDetached()) {
return; return;
} }
Locale simLocale = getSimLocale(); if (mFetchUpdateSimLocaleTask != null) {
if (simLocale != null && !simLocale.equals(mCurrentLocale)) { mFetchUpdateSimLocaleTask.cancel(true);
onLocaleChanged(simLocale); }
mFetchUpdateSimLocaleTask = new FetchUpdateSimLocaleTask();
mFetchUpdateSimLocaleTask.execute();
}
private class FetchUpdateSimLocaleTask extends AsyncTask<Void, Void, Locale> {
@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);
}
}
} }
} }
} }