blob: b350ccf2fdc7e385bcc71e73128edd7821d5f33c [file] [log] [blame]
cretin453593f032016-04-20 16:21:05 -07001/*
Michael Bestasec050052024-02-14 20:35:43 +02002 * SPDX-FileCopyrightText: 2016 The CyanogenMod Project
3 * SPDX-FileCopyrightText: 2017-2024 The LineageOS Project
4 * SPDX-License-Identifier: Apache-2.0
cretin453593f032016-04-20 16:21:05 -07005 */
6
Michael Bestasc83309e2018-02-03 17:42:13 +02007package org.lineageos.setupwizard;
cretin453593f032016-04-20 16:21:05 -07008
9import android.app.Activity;
10import android.content.BroadcastReceiver;
11import android.content.Context;
12import android.content.Intent;
13import android.content.IntentFilter;
cretin453593f032016-04-20 16:21:05 -070014import android.os.Bundle;
15import android.os.Handler;
Oliver Scottf58c0f42024-01-08 14:52:12 -050016import android.os.Looper;
cretin453593f032016-04-20 16:21:05 -070017import android.telephony.SubscriptionInfo;
18import android.telephony.SubscriptionManager;
19import android.telephony.TelephonyManager;
Tommy Webbac47a022024-04-15 18:34:09 +000020import android.util.Log;
Timi Rautamäkic893e612022-01-27 15:41:46 +000021import android.view.View;
cretin453593f032016-04-20 16:21:05 -070022import android.widget.ArrayAdapter;
cretin453593f032016-04-20 16:21:05 -070023import android.widget.NumberPicker;
cretin453593f032016-04-20 16:21:05 -070024import android.widget.Toast;
25
cretin453593f032016-04-20 16:21:05 -070026import com.android.internal.telephony.TelephonyIntents;
Renlord4027def2020-09-21 22:48:41 +100027import com.android.internal.telephony.util.LocaleUtils;
cretin453593f032016-04-20 16:21:05 -070028
Timi Rautamäkiab4f1232021-08-23 16:56:13 +000029import com.google.android.setupcompat.util.SystemBarHelper;
30
Michael Bestasc83309e2018-02-03 17:42:13 +020031import org.lineageos.setupwizard.widget.LocalePicker;
cretin453593f032016-04-20 16:21:05 -070032
33import java.util.List;
34import java.util.Locale;
Oliver Scottf58c0f42024-01-08 14:52:12 -050035import java.util.concurrent.ExecutorService;
36import java.util.concurrent.Executors;
cretin453593f032016-04-20 16:21:05 -070037
38public class LocaleActivity extends BaseSetupWizardActivity {
39
Tommy Webbac47a022024-04-15 18:34:09 +000040 private static final String TAG = LocaleActivity.class.getSimpleName();
41
cretin453593f032016-04-20 16:21:05 -070042 private ArrayAdapter<com.android.internal.app.LocalePicker.LocaleInfo> mLocaleAdapter;
43 private Locale mCurrentLocale;
44 private int[] mAdapterIndices;
45 private LocalePicker mLanguagePicker;
Oliver Scottf58c0f42024-01-08 14:52:12 -050046 private ExecutorService mFetchUpdateSimLocaleTask;
47 private final Handler mHandler = new Handler(Looper.getMainLooper());
cretin453593f032016-04-20 16:21:05 -070048 private boolean mPendingLocaleUpdate;
49 private boolean mPaused = true;
50
51 private final Runnable mUpdateLocale = new Runnable() {
52 public void run() {
53 if (mCurrentLocale != null) {
54 mLanguagePicker.setEnabled(false);
Oliver Scottd9d28592024-04-09 11:43:05 -040055 com.android.internal.app.LocalePicker.updateLocale(mCurrentLocale);
cretin453593f032016-04-20 16:21:05 -070056 }
57 }
58 };
59
60 private final BroadcastReceiver mSimChangedReceiver = new BroadcastReceiver() {
61 @Override
62 public void onReceive(Context context, Intent intent) {
63 if (intent.getAction().equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
64 fetchAndUpdateSimLocale();
65 }
66 }
67 };
68
cretin453593f032016-04-20 16:21:05 -070069 @Override
70 protected void onCreate(Bundle savedInstanceState) {
71 super.onCreate(savedInstanceState);
Timi Rautamäkiab4f1232021-08-23 16:56:13 +000072 SystemBarHelper.setBackButtonVisible(getWindow(), true);
cretin453593f032016-04-20 16:21:05 -070073 setNextText(R.string.next);
Oliver Scott51fde0a2024-01-06 22:15:26 -050074 mLanguagePicker = findViewById(R.id.locale_list);
Timi Rautamäkidaef80e2022-01-15 09:25:02 +000075 mLanguagePicker.setNextRight(getNextButton().getId());
76 mLanguagePicker.requestFocus();
Timi Rautamäkic893e612022-01-27 15:41:46 +000077 if (getResources().getBoolean(R.bool.config_isLargeNoTouch)) {
Oliver Scottfd0fdb82024-01-10 18:39:27 -050078 mLanguagePicker.setOnClickListener((View v) -> getNextButton().performClick());
Timi Rautamäkic893e612022-01-27 15:41:46 +000079 }
cretin453593f032016-04-20 16:21:05 -070080 loadLanguages();
81 }
82
83 @Override
84 public void onPause() {
85 super.onPause();
86 mPaused = true;
87 unregisterReceiver(mSimChangedReceiver);
88 }
89
90 @Override
91 public void onResume() {
92 super.onResume();
93 mPaused = false;
94 registerReceiver(mSimChangedReceiver,
95 new IntentFilter(TelephonyIntents.ACTION_SIM_STATE_CHANGED));
96 if (mLanguagePicker != null) {
97 mLanguagePicker.setEnabled(true);
98 }
99 if (mPendingLocaleUpdate) {
100 mPendingLocaleUpdate = false;
101 fetchAndUpdateSimLocale();
102 }
103 }
104
105 @Override
cretin45d4cea552016-04-25 11:00:04 -0700106 protected int getLayoutResId() {
107 return R.layout.setup_locale;
108 }
109
110 @Override
111 protected int getTitleResId() {
112 return R.string.setup_locale;
113 }
114
115 @Override
116 protected int getIconResId() {
117 return R.drawable.ic_locale;
cretin453593f032016-04-20 16:21:05 -0700118 }
119
120 private void loadLanguages() {
121 mLocaleAdapter = com.android.internal.app.LocalePicker.constructAdapter(this,
122 R.layout.locale_picker_item, R.id.locale);
123 mCurrentLocale = Locale.getDefault();
124 fetchAndUpdateSimLocale();
125 mAdapterIndices = new int[mLocaleAdapter.getCount()];
126 int currentLocaleIndex = 0;
Michael Bestasb7b34b92021-08-25 19:13:30 +0300127 String[] labels = new String[mLocaleAdapter.getCount()];
128 for (int i = 0; i < mAdapterIndices.length; i++) {
cretin453593f032016-04-20 16:21:05 -0700129 com.android.internal.app.LocalePicker.LocaleInfo localLocaleInfo =
130 mLocaleAdapter.getItem(i);
131 Locale localLocale = localLocaleInfo.getLocale();
132 if (localLocale.equals(mCurrentLocale)) {
133 currentLocaleIndex = i;
134 }
135 mAdapterIndices[i] = i;
136 labels[i] = localLocaleInfo.getLabel();
137 }
138 mLanguagePicker.setDisplayedValues(labels);
139 mLanguagePicker.setMaxValue(labels.length - 1);
140 mLanguagePicker.setValue(currentLocaleIndex);
141 mLanguagePicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
Michael W2236d292018-07-15 15:09:20 +0200142 mLanguagePicker.setOnValueChangedListener((pkr, oldVal, newVal) -> setLocaleFromPicker());
143
144 mLanguagePicker.setOnScrollListener((view, scrollState) -> {
145 if (scrollState == NumberPicker.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
Michael Bestasb7b34b92021-08-25 19:13:30 +0300146 ((SetupWizardApp) getApplication()).setIgnoreSimLocale(true);
cretin453593f032016-04-20 16:21:05 -0700147 }
148 });
149 }
150
151 private void setLocaleFromPicker() {
Michael Bestasb7b34b92021-08-25 19:13:30 +0300152 ((SetupWizardApp) getApplication()).setIgnoreSimLocale(true);
cretin453593f032016-04-20 16:21:05 -0700153 int i = mAdapterIndices[mLanguagePicker.getValue()];
Michael W2236d292018-07-15 15:09:20 +0200154 final com.android.internal.app.LocalePicker.LocaleInfo localLocaleInfo =
155 mLocaleAdapter.getItem(i);
cretin453593f032016-04-20 16:21:05 -0700156 onLocaleChanged(localLocaleInfo.getLocale());
157 }
158
159 private void onLocaleChanged(Locale paramLocale) {
160 mLanguagePicker.setEnabled(true);
cretin453593f032016-04-20 16:21:05 -0700161 mHandler.removeCallbacks(mUpdateLocale);
162 mCurrentLocale = paramLocale;
163 mHandler.postDelayed(mUpdateLocale, 1000);
164 }
165
166 private void fetchAndUpdateSimLocale() {
Michael Bestasb7b34b92021-08-25 19:13:30 +0300167 if (((SetupWizardApp) getApplication()).ignoreSimLocale() || isDestroyed()) {
cretin453593f032016-04-20 16:21:05 -0700168 return;
169 }
170 if (mPaused) {
171 mPendingLocaleUpdate = true;
172 return;
173 }
174 if (mFetchUpdateSimLocaleTask != null) {
Oliver Scottf58c0f42024-01-08 14:52:12 -0500175 mFetchUpdateSimLocaleTask.shutdown();
cretin453593f032016-04-20 16:21:05 -0700176 }
Oliver Scottf58c0f42024-01-08 14:52:12 -0500177 mFetchUpdateSimLocaleTask = Executors.newSingleThreadExecutor();
178 mFetchUpdateSimLocaleTask.execute(() -> {
Tommy Webbac47a022024-04-15 18:34:09 +0000179 Locale locale = null;
cretin453593f032016-04-20 16:21:05 -0700180 Activity activity = LocaleActivity.this;
181 if (!activity.isFinishing() || !activity.isDestroyed()) {
182 // If the sim is currently pin locked, return
183 TelephonyManager telephonyManager = (TelephonyManager)
184 activity.getSystemService(Context.TELEPHONY_SERVICE);
185 int state = telephonyManager.getSimState();
Michael Bestasb7b34b92021-08-25 19:13:30 +0300186 if (state == TelephonyManager.SIM_STATE_PIN_REQUIRED ||
cretin453593f032016-04-20 16:21:05 -0700187 state == TelephonyManager.SIM_STATE_PUK_REQUIRED) {
Oliver Scottf58c0f42024-01-08 14:52:12 -0500188 return;
cretin453593f032016-04-20 16:21:05 -0700189 }
190
191 final SubscriptionManager subscriptionManager =
Oliver Scottf58c0f42024-01-08 14:52:12 -0500192 activity.getSystemService(SubscriptionManager.class);
cretin453593f032016-04-20 16:21:05 -0700193 List<SubscriptionInfo> activeSubs =
194 subscriptionManager.getActiveSubscriptionInfoList();
195 if (activeSubs == null || activeSubs.isEmpty()) {
Oliver Scottf58c0f42024-01-08 14:52:12 -0500196 return;
cretin453593f032016-04-20 16:21:05 -0700197 }
198
199 // Fetch locale for active sim's MCC
Tommy Webbac47a022024-04-15 18:34:09 +0000200 final String mccString = activeSubs.get(0).getMccString();
201 try {
202 if (mccString != null && !mccString.isEmpty()) {
203 int mcc = Integer.parseInt(mccString);
204 locale = LocaleUtils.getLocaleFromMcc(activity, mcc, null);
205 } else {
206 Log.w(TAG, "Unexpected mccString: '" + mccString + "'");
207 }
208 } catch (NumberFormatException e) {
209 Log.w(TAG, "mccString not a number: '" + mccString + "'", e);
210 }
cretin453593f032016-04-20 16:21:05 -0700211
212 // If that fails, fall back to preferred languages reported
213 // by the sim
214 if (locale == null) {
Michael Bestas79e9f1c2024-03-05 23:17:41 +0200215 Locale simLocale = telephonyManager.getSimLocale();
216 if (simLocale != null) {
217 locale = simLocale;
cretin453593f032016-04-20 16:21:05 -0700218 }
219 }
Oliver Scottf58c0f42024-01-08 14:52:12 -0500220 Locale finalLocale = locale;
221 mHandler.post(() -> {
222 if (finalLocale != null && !finalLocale.equals(mCurrentLocale)) {
223 if (!((SetupWizardApp) getApplication()).ignoreSimLocale()
224 && !isDestroyed()) {
225 String label = getString(R.string.sim_locale_changed,
226 finalLocale.getDisplayName());
227 Toast.makeText(LocaleActivity.this, label, Toast.LENGTH_SHORT).show();
228 onLocaleChanged(finalLocale);
229 ((SetupWizardApp) getApplication()).setIgnoreSimLocale(true);
230 }
231 }
232 });
cretin453593f032016-04-20 16:21:05 -0700233 }
Oliver Scottf58c0f42024-01-08 14:52:12 -0500234 });
cretin453593f032016-04-20 16:21:05 -0700235 }
cretin453593f032016-04-20 16:21:05 -0700236}