Skip to content

Commit 0754569

Browse files
committed
Made theme choosable and fixed a lot of theme bugs.
1 parent 36175eb commit 0754569

16 files changed

+141
-66
lines changed

Mifare Classic Tool/app/src/main/java/de/syss/MifareClassicTool/Activities/AccessConditionDecoder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private void addSectorAC(byte[][] acMatrix, String sectorHeader,
111111
// Add sector header.
112112
TextView header = new TextView(this);
113113
header.setText(Common.colorString(sectorHeader,
114-
ContextCompat.getColor(this, R.color.accent)),
114+
Common.getThemeAccentColor(this)),
115115
BufferType.SPANNABLE);
116116
TableRow tr = new TableRow(this);
117117
tr.setLayoutParams(new LayoutParams(

Mifare Classic Tool/app/src/main/java/de/syss/MifareClassicTool/Activities/DiffTool.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ private void runDiff() {
188188
TextViewCompat.setTextAppearance(header,
189189
android.R.style.TextAppearance_Medium);
190190
header.setPadding(0, Common.dpToPx(20), 0, 0);
191-
header.setTextColor(Color.WHITE);
191+
header.setTextColor(Common.getThemeAccentColor(this));
192192
header.setText(getString(R.string.text_sector) + ": " + sector);
193193
mDiffContent.addView(header);
194194

Mifare Classic Tool/app/src/main/java/de/syss/MifareClassicTool/Activities/DumpEditor.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import android.widget.TextView.BufferType;
4444
import android.widget.Toast;
4545

46+
import androidx.appcompat.widget.AppCompatEditText;
4647
import androidx.core.content.ContextCompat;
4748
import androidx.core.text.HtmlCompat;
4849

@@ -159,8 +160,7 @@ public void onCreate(Bundle savedInstanceState) {
159160
TextView captionTitle = findViewById(
160161
R.id.textViewDumpEditorCaptionTitle);
161162
SpannableString updateText = Common.colorString(
162-
getString(R.string.text_update_colors),
163-
ContextCompat.getColor(this, R.color.accent));
163+
getString(R.string.text_update_colors), Common.getThemeAccentColor(this));
164164
updateText.setSpan(new UnderlineSpan(), 0, updateText.length(), 0);
165165
captionTitle.setText(TextUtils.concat(
166166
getString(R.string.text_caption_title),
@@ -493,7 +493,7 @@ private int checkDumpAndUpdateLines() {
493493
ArrayList<String> checkedLines = new ArrayList<>();
494494
for(int i = 0; i < mLayout.getChildCount(); i++) {
495495
View child = mLayout.getChildAt(i);
496-
if (child instanceof EditText) {
496+
if (child instanceof AppCompatEditText) {
497497
String[] lines = ((EditText)child).getText().toString()
498498
.split(System.getProperty("line.separator"));
499499
if (lines.length != 4 && lines.length != 16) {
@@ -553,7 +553,7 @@ private void initEditor(String[] lines) {
553553
boolean tmpDumpChanged = mDumpChanged;
554554
mLayout.removeAllViews();
555555
boolean isFirstBlock = false;
556-
EditText et = null;
556+
AppCompatEditText et = null;
557557
ArrayList<SpannableString> blocks =
558558
new ArrayList<>(4);
559559
for (int i = 0; i < lines.length; i++) {
@@ -563,16 +563,15 @@ private void initEditor(String[] lines) {
563563
String sectorNumber = lines[i].split(": ")[1];
564564
// Add sector header (TextView).
565565
TextView tv = new TextView(this);
566-
tv.setTextColor(
567-
ContextCompat.getColor(this, R.color.accent));
566+
tv.setTextColor(Common.getThemeAccentColor(this));
568567
tv.setText(getString(R.string.text_sector) +
569568
": " + sectorNumber);
570569
mLayout.addView(tv);
571570
// Add sector data (EditText) if not at the end and if the
572571
// next line is not an error line ("*").
573572
if (i+1 != lines.length && !lines[i+1].startsWith("*")) {
574573
// Add sector data (EditText).
575-
et = new EditText(this);
574+
et = new AppCompatEditText(this);
576575
et.setLayoutParams(new LayoutParams(
577576
LayoutParams.WRAP_CONTENT,
578577
LayoutParams.WRAP_CONTENT));

Mifare Classic Tool/app/src/main/java/de/syss/MifareClassicTool/Activities/FileChooser.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@
3131
import android.view.inputmethod.InputMethodManager;
3232
import android.widget.Button;
3333
import android.widget.EditText;
34-
import android.widget.RadioButton;
3534
import android.widget.RadioGroup;
3635
import android.widget.TextView;
3736
import android.widget.Toast;
3837

38+
import androidx.appcompat.widget.AppCompatRadioButton;
39+
3940
import java.io.File;
4041
import java.util.Arrays;
4142

@@ -241,7 +242,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
241242
* @see #EXTRA_CHOSEN_FILENAME
242243
*/
243244
public void onFileChosen(View view) {
244-
RadioButton selected = findViewById(
245+
AppCompatRadioButton selected = findViewById(
245246
mGroupOfFiles.getCheckedRadioButtonId());
246247
Intent intent = new Intent();
247248
File file = new File(mDir.getPath(), selected.getText().toString());
@@ -274,14 +275,14 @@ private boolean updateFileIndex(File path) {
274275
Arrays.sort(files);
275276
for (File f : files) {
276277
if (f.isFile()) { // Do not list directories.
277-
RadioButton r = new RadioButton(this);
278+
AppCompatRadioButton r = new AppCompatRadioButton(this);
278279
r.setText(f.getName());
279280
mGroupOfFiles.addView(r);
280281
}
281282
}
282283
if (mGroupOfFiles.getChildCount() > 0) {
283284
isEmpty = false;
284-
((RadioButton) mGroupOfFiles.getChildAt(0)).setChecked(true);
285+
((AppCompatRadioButton) mGroupOfFiles.getChildAt(0)).setChecked(true);
285286
}
286287
} else {
287288
// No files in directory.
@@ -390,7 +391,7 @@ private void onNewFile() {
390391
* @see #updateFileIndex(File)
391392
*/
392393
private void onDeleteFile() {
393-
RadioButton selected = findViewById(
394+
AppCompatRadioButton selected = findViewById(
394395
mGroupOfFiles.getCheckedRadioButtonId());
395396
File file = new File(mDir.getPath(), selected.getText().toString());
396397
file.delete();

Mifare Classic Tool/app/src/main/java/de/syss/MifareClassicTool/Activities/KeyMapCreator.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737
import android.view.ViewGroup.LayoutParams;
3838
import android.view.WindowManager;
3939
import android.widget.Button;
40-
import android.widget.EditText;
4140
import android.widget.LinearLayout;
4241
import android.widget.ProgressBar;
4342
import android.widget.TextView;
4443
import android.widget.Toast;
4544

4645
import androidx.appcompat.widget.AppCompatCheckBox;
46+
import androidx.appcompat.widget.AppCompatEditText;
4747

4848
import java.io.File;
4949
import java.util.ArrayList;
@@ -565,15 +565,15 @@ public void onChangeSectorRange(View view) {
565565

566566
InputFilter[] f = new InputFilter[1];
567567
f[0] = new InputFilter.LengthFilter(2);
568-
final EditText from = new EditText(this);
568+
final AppCompatEditText from = new AppCompatEditText(this);
569569
from.setEllipsize(TruncateAt.END);
570570
from.setMaxLines(1);
571571
from.setSingleLine();
572572
from.setInputType(InputType.TYPE_CLASS_NUMBER);
573573
from.setMinimumWidth(60);
574574
from.setFilters(f);
575575
from.setGravity(Gravity.CENTER_HORIZONTAL);
576-
final EditText to = new EditText(this);
576+
final AppCompatEditText to = new AppCompatEditText(this);
577577
to.setEllipsize(TruncateAt.END);
578578
to.setMaxLines(1);
579579
to.setSingleLine();

Mifare Classic Tool/app/src/main/java/de/syss/MifareClassicTool/Activities/MainMenu.java

+27
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import android.widget.Toast;
4949

5050
import androidx.appcompat.app.AppCompatActivity;
51+
import androidx.appcompat.app.AppCompatDelegate;
5152
import androidx.core.content.pm.PackageInfoCompat;
5253
import androidx.core.text.HtmlCompat;
5354

@@ -107,6 +108,7 @@ private enum StartUpNode {
107108
@SuppressWarnings("deprecation")
108109
@Override
109110
public void onCreate(Bundle savedInstanceState) {
111+
setTheme();
110112
super.onCreate(savedInstanceState);
111113
setContentView(R.layout.activity_main_menu);
112114

@@ -331,6 +333,31 @@ private void useAsEditorOnly(boolean useAsEditorOnly) {
331333
mWriteTag.setEnabled(!useAsEditorOnly);
332334
}
333335

336+
/**
337+
* Set the theme according to preferences or system settings.
338+
* Default to "dark" theme before Android 10.
339+
* Defualt to "follow system" theme for Android 10+.
340+
*/
341+
private void setTheme() {
342+
SharedPreferences pref = Common.getPreferences();
343+
int themeID;
344+
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
345+
// Default to dark before Android 10.
346+
themeID = pref.getInt(Preferences.Preference.CustomAppTheme.toString(), 0);
347+
} else {
348+
// Follow system theme for Android 10+.
349+
themeID = pref.getInt(Preferences.Preference.CustomAppTheme.toString(), 2);
350+
}
351+
switch (themeID) {
352+
case 0:
353+
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
354+
break;
355+
case 1:
356+
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
357+
break;
358+
}
359+
}
360+
334361
/**
335362
* Create the dialog which is displayed once the app was started for the
336363
* first time. After showing the dialog, {@link #runStartUpNode(StartUpNode)}

Mifare Classic Tool/app/src/main/java/de/syss/MifareClassicTool/Activities/Preferences.java

+58-27
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import android.content.ComponentName;
2424
import android.content.SharedPreferences;
2525
import android.content.pm.PackageManager;
26+
import android.os.Build;
2627
import android.os.Bundle;
2728
import android.view.View;
28-
import android.widget.AdapterView;
2929
import android.widget.ArrayAdapter;
3030
import android.widget.CheckBox;
3131
import android.widget.EditText;
@@ -38,14 +38,17 @@
3838
import androidx.appcompat.app.AppCompatDelegate;
3939
import androidx.core.os.LocaleListCompat;
4040

41+
import java.util.ArrayList;
42+
import java.util.Arrays;
43+
4144
import de.syss.MifareClassicTool.Common;
4245
import de.syss.MifareClassicTool.R;
4346

4447
/**
4548
* This view will let the user edit global preferences.
4649
* @author Gerhard Klostermeier
4750
*/
48-
public class Preferences extends BasicActivity implements AdapterView.OnItemSelectedListener {
51+
public class Preferences extends BasicActivity {
4952

5053
/**
5154
* Enumeration with all preferences. This enumeration implements
@@ -61,7 +64,8 @@ public enum Preference {
6164
CustomSectorCount("custom_sector_count"),
6265
UseRetryAuthentication("use_retry_authentication"),
6366
RetryAuthenticationCount("retry_authentication_count"),
64-
CustomAppLanguage("custom_app_language");
67+
CustomAppLanguage("custom_app_language"),
68+
CustomAppTheme("custom_app_theme");
6569
// Add more preferences here (comma separated).
6670

6771
private final String text;
@@ -87,6 +91,7 @@ public String toString() {
8791
private EditText mRetryAuthenticationCount;
8892
private RadioGroup mUIDFormatRadioGroup;
8993
private Spinner mLangauge;
94+
private Spinner mTheme;
9095

9196
private PackageManager mPackageManager;
9297
private ComponentName mComponentName;
@@ -122,6 +127,7 @@ public void onCreate(Bundle savedInstanceState) {
122127
mRetryAuthenticationCount = findViewById(
123128
R.id.editTextPreferencesRetryAuthenticationCount);
124129
mLangauge = findViewById(R.id.spinnerPreferencesLanguage);
130+
mTheme = findViewById(R.id.spinnerPreferencesTheme);
125131

126132
// Assign the last stored values.
127133
SharedPreferences pref = Common.getPreferences();
@@ -145,6 +151,7 @@ public void onCreate(Bundle savedInstanceState) {
145151
Preference.RetryAuthenticationCount.toString(), 1));
146152
detectAutostartIfCardDetectedState();
147153
getLanguageAndUpdateChooser();
154+
getThemeAndUpdateChooser();
148155

149156
// UID Format Options (hide/show)
150157
mUIDFormatRadioGroup = findViewById(
@@ -164,7 +171,28 @@ private void getLanguageAndUpdateChooser() {
164171
SharedPreferences pref = Common.getPreferences();
165172
int langID = pref.getInt(Preference.CustomAppLanguage.toString(), 0);
166173
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);
168196
}
169197

170198
/**
@@ -188,29 +216,6 @@ private void detectAutostartIfCardDetectedState() {
188216
}
189217
}
190218

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-
214219
/**
215220
* Show information on the "auto reconnect" preference.
216221
* @param view The View object that triggered the method
@@ -397,8 +402,11 @@ public void onSave(View view) {
397402
retryAuthenticationCount);
398403
edit.putInt(Preference.CustomAppLanguage.toString(),
399404
(int)mLangauge.getSelectedItemId());
405+
edit.putInt(Preference.CustomAppTheme.toString(),
406+
(int)mTheme.getSelectedItemId());
400407
edit.apply();
401408

409+
// Update card detection on autostart.
402410
int newState;
403411
if (mPrefAutostartIfCardDetected.isChecked()) {
404412
newState = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
@@ -410,6 +418,29 @@ public void onSave(View view) {
410418
newState,
411419
PackageManager.DONT_KILL_APP);
412420

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+
413444
// Exit the preferences view.
414445
finish();
415446
}

Mifare Classic Tool/app/src/main/java/de/syss/MifareClassicTool/Activities/TagInfoTool.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ private void updateTagInfo(Tag tag) {
135135
// Create views and add them to the layout.
136136
TextView headerGenericInfo = new TextView(this);
137137
headerGenericInfo.setText(Common.colorString(
138-
getString(R.string.text_generic_info),
139-
ContextCompat.getColor(this, R.color.accent)));
138+
getString(R.string.text_generic_info), Common.getThemeAccentColor(this)));
140139
TextViewCompat.setTextAppearance(headerGenericInfo,
141140
android.R.style.TextAppearance_Large);
142141
headerGenericInfo.setGravity(Gravity.CENTER_HORIZONTAL);
@@ -192,7 +191,7 @@ private void updateTagInfo(Tag tag) {
192191
tagType = getString(tagTypeResourceID);
193192
}
194193

195-
int hc = ContextCompat.getColor(this, R.color.accent);
194+
int hc = Common.getThemeAccentColor(this);
196195
genericInfo.setText(TextUtils.concat(
197196
Common.colorString(getString(R.string.text_uid) + ":", hc),
198197
"\n", uid, "\n",
@@ -228,8 +227,7 @@ private void updateTagInfo(Tag tag) {
228227
// Create views and add them to the layout.
229228
TextView headerMifareInfo = new TextView(this);
230229
headerMifareInfo.setText(Common.colorString(
231-
getString(R.string.text_mf_info),
232-
ContextCompat.getColor(this, R.color.accent)));
230+
getString(R.string.text_mf_info), Common.getThemeAccentColor(this)));
233231
TextViewCompat.setTextAppearance(headerMifareInfo,
234232
android.R.style.TextAppearance_Large);
235233
headerMifareInfo.setGravity(Gravity.CENTER_HORIZONTAL);

0 commit comments

Comments
 (0)