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

Commit 00ad2213 authored by Danesh M's avatar Danesh M
Browse files

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
parent d59304c7
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -94,4 +94,5 @@
    <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_screen_lock_setup">Setup screen lock</string>
    <string name="sim_locale_changed">%1$s SIM detected</string>
</resources>
+49 −26
Original line number Diff line number Diff line
@@ -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,15 +271,44 @@ public class WelcomePage extends SetupPage {
            }
        }

        public void simChanged() {
            if (mUserPickedLocale || isDetached()) {
        public void fetchAndUpdateSimLocale() {
            if (mIgnoreSimLocale || isDetached()) {
                return;
            }
            Locale simLocale = getSimLocale();
            if (mFetchUpdateSimLocaleTask != null) {
                mFetchUpdateSimLocaleTask.cancel(true);
            }
            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);
                    }
                }
            }
        }
    }

}