23
23
import android .content .ComponentName ;
24
24
import android .content .SharedPreferences ;
25
25
import android .content .pm .PackageManager ;
26
+ import android .os .Build ;
26
27
import android .os .Bundle ;
27
28
import android .view .View ;
28
- import android .widget .AdapterView ;
29
29
import android .widget .ArrayAdapter ;
30
30
import android .widget .CheckBox ;
31
31
import android .widget .EditText ;
38
38
import androidx .appcompat .app .AppCompatDelegate ;
39
39
import androidx .core .os .LocaleListCompat ;
40
40
41
+ import java .util .ArrayList ;
42
+ import java .util .Arrays ;
43
+
41
44
import de .syss .MifareClassicTool .Common ;
42
45
import de .syss .MifareClassicTool .R ;
43
46
44
47
/**
45
48
* This view will let the user edit global preferences.
46
49
* @author Gerhard Klostermeier
47
50
*/
48
- public class Preferences extends BasicActivity implements AdapterView . OnItemSelectedListener {
51
+ public class Preferences extends BasicActivity {
49
52
50
53
/**
51
54
* Enumeration with all preferences. This enumeration implements
@@ -61,7 +64,8 @@ public enum Preference {
61
64
CustomSectorCount ("custom_sector_count" ),
62
65
UseRetryAuthentication ("use_retry_authentication" ),
63
66
RetryAuthenticationCount ("retry_authentication_count" ),
64
- CustomAppLanguage ("custom_app_language" );
67
+ CustomAppLanguage ("custom_app_language" ),
68
+ CustomAppTheme ("custom_app_theme" );
65
69
// Add more preferences here (comma separated).
66
70
67
71
private final String text ;
@@ -87,6 +91,7 @@ public String toString() {
87
91
private EditText mRetryAuthenticationCount ;
88
92
private RadioGroup mUIDFormatRadioGroup ;
89
93
private Spinner mLangauge ;
94
+ private Spinner mTheme ;
90
95
91
96
private PackageManager mPackageManager ;
92
97
private ComponentName mComponentName ;
@@ -122,6 +127,7 @@ public void onCreate(Bundle savedInstanceState) {
122
127
mRetryAuthenticationCount = findViewById (
123
128
R .id .editTextPreferencesRetryAuthenticationCount );
124
129
mLangauge = findViewById (R .id .spinnerPreferencesLanguage );
130
+ mTheme = findViewById (R .id .spinnerPreferencesTheme );
125
131
126
132
// Assign the last stored values.
127
133
SharedPreferences pref = Common .getPreferences ();
@@ -145,6 +151,7 @@ public void onCreate(Bundle savedInstanceState) {
145
151
Preference .RetryAuthenticationCount .toString (), 1 ));
146
152
detectAutostartIfCardDetectedState ();
147
153
getLanguageAndUpdateChooser ();
154
+ getThemeAndUpdateChooser ();
148
155
149
156
// UID Format Options (hide/show)
150
157
mUIDFormatRadioGroup = findViewById (
@@ -164,7 +171,28 @@ private void getLanguageAndUpdateChooser() {
164
171
SharedPreferences pref = Common .getPreferences ();
165
172
int langID = pref .getInt (Preference .CustomAppLanguage .toString (), 0 );
166
173
mLangauge .setSelection (langID );
167
- mLangauge .setOnItemSelectedListener (this );
174
+ }
175
+
176
+ /**
177
+ * Get the used theme from MCTs own settings and set it in the custom app theme chooser UI.
178
+ * Defaults to "dark" theme for Android < 10 if not set.
179
+ * Defualts to "follow system theme" for Android >= 10 if not set.
180
+ */
181
+ private void getThemeAndUpdateChooser () {
182
+ CharSequence themeArray [] = getResources ().getStringArray (R .array .action_themes );
183
+ ArrayList <CharSequence > themeArrayList = new ArrayList <>(Arrays .asList (themeArray ));
184
+ SharedPreferences pref = Common .getPreferences ();
185
+ int themeID ;
186
+ if (android .os .Build .VERSION .SDK_INT < Build .VERSION_CODES .Q ) {
187
+ themeArrayList .remove (2 );
188
+ themeID = pref .getInt (Preference .CustomAppTheme .toString (), 0 );
189
+ } else {
190
+ themeID = pref .getInt (Preference .CustomAppTheme .toString (), 2 );
191
+ }
192
+ ArrayAdapter <CharSequence > adapter = new ArrayAdapter <CharSequence >(this ,
193
+ android .R .layout .simple_spinner_dropdown_item , themeArrayList );
194
+ mTheme .setAdapter (adapter );
195
+ mTheme .setSelection (themeID );
168
196
}
169
197
170
198
/**
@@ -188,29 +216,6 @@ private void detectAutostartIfCardDetectedState() {
188
216
}
189
217
}
190
218
191
- /**
192
- * Change the app language to the selected language.
193
- * @param parent The AdapterView where the selection happened
194
- * @param view The view within the AdapterView that was clicked
195
- * @param pos The position of the view in the adapter
196
- * @param id The row id of the item that is selected
197
- */
198
- public void onItemSelected (AdapterView <?> parent , View view ,
199
- int pos , long id ) {
200
- String lang = (String )parent .getItemAtPosition (pos );
201
- String langCode = lang .substring (lang .indexOf ("(" )+1 , lang .indexOf (")" ));
202
- LocaleListCompat appLocale = LocaleListCompat .forLanguageTags (langCode );
203
- AppCompatDelegate .setApplicationLocales (appLocale );
204
- }
205
-
206
- /**
207
- * Dummy. This implementation is required by OnItemSelectedListener.
208
- * @param parent The AdapterView that now contains no selected item.
209
- */
210
- public void onNothingSelected (AdapterView <?> parent ) {
211
- // Do nothing (this implementation is required by OnItemSelectedListener).
212
- }
213
-
214
219
/**
215
220
* Show information on the "auto reconnect" preference.
216
221
* @param view The View object that triggered the method
@@ -397,8 +402,11 @@ public void onSave(View view) {
397
402
retryAuthenticationCount );
398
403
edit .putInt (Preference .CustomAppLanguage .toString (),
399
404
(int )mLangauge .getSelectedItemId ());
405
+ edit .putInt (Preference .CustomAppTheme .toString (),
406
+ (int )mTheme .getSelectedItemId ());
400
407
edit .apply ();
401
408
409
+ // Update card detection on autostart.
402
410
int newState ;
403
411
if (mPrefAutostartIfCardDetected .isChecked ()) {
404
412
newState = PackageManager .COMPONENT_ENABLED_STATE_ENABLED ;
@@ -410,6 +418,29 @@ public void onSave(View view) {
410
418
newState ,
411
419
PackageManager .DONT_KILL_APP );
412
420
421
+ // Update language.
422
+ String lang = (String ) mLangauge .getSelectedItem ();
423
+ String langCode = lang .substring (lang .indexOf ("(" ) + 1 , lang .indexOf (")" ));
424
+ LocaleListCompat appLocale = LocaleListCompat .forLanguageTags (langCode );
425
+ AppCompatDelegate .setApplicationLocales (appLocale );
426
+
427
+ // Update theme.
428
+ int theme = (int )mTheme .getSelectedItemId ();
429
+ switch (theme ) {
430
+ case 0 :
431
+ AppCompatDelegate .setDefaultNightMode (
432
+ AppCompatDelegate .MODE_NIGHT_YES );
433
+ break ;
434
+ case 1 :
435
+ AppCompatDelegate .setDefaultNightMode (
436
+ AppCompatDelegate .MODE_NIGHT_NO );
437
+ break ;
438
+ case 2 :
439
+ AppCompatDelegate .setDefaultNightMode (
440
+ AppCompatDelegate .MODE_NIGHT_FOLLOW_SYSTEM );
441
+ break ;
442
+ }
443
+
413
444
// Exit the preferences view.
414
445
finish ();
415
446
}
0 commit comments