Skip to content

Commit

Permalink
#162: Fixed Merge Error from themePreference. Recreate Settings after…
Browse files Browse the repository at this point in the history
… theme change
  • Loading branch information
k3b committed Jan 29, 2020
1 parent 659a079 commit dc860d7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 88 deletions.
11 changes: 9 additions & 2 deletions app/src/main/java/de/k3b/android/GuiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
* gui utils
*/
public class GuiUtil {

public static final String PREF_KEY_USER_THEME = "user_theme";

public static String getAppVersionName(final Context context) {
try {

Expand All @@ -41,7 +44,7 @@ public static String getAppVersionName(final Context context) {
return null;
}
public static void setTheme(Activity act) {
final String theme = PreferenceManager.getDefaultSharedPreferences(act).getString("user_theme", "Light");
final String theme = getTheme(act);
switch (theme) {
case "Light":
act.setTheme(R.style.AppTheme_Light);
Expand All @@ -50,7 +53,11 @@ public static void setTheme(Activity act) {
act.setTheme(R.style.AppTheme_Dark);
break;
default:
throw new RuntimeException("Invalid theme selected");
throw new RuntimeException("Invalid theme '" + theme+ "' selected");
}
}

public static String getTheme(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getString(PREF_KEY_USER_THEME, "Light");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class SettingsActivity extends PreferenceActivity {
private SharedPreferences prefsInstance = null;
private ListPreference defaultLocalePreference; // #21: Support to change locale at runtime
private ListPreference mediaUpdateStrategyPreference;
private ListPreference themePreference; // #21: Support to change locale at runtime

private int INSTALL_REQUEST_CODE = 1927;

Expand Down Expand Up @@ -95,6 +96,15 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
}
});

themePreference = (ListPreference) findPreference(GuiUtil.PREF_KEY_USER_THEME);
themePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
setUserTheme((String) newValue);
LocalizedActivity.recreate(SettingsActivity.this);
return true; // change is allowed
}
});
mediaUpdateStrategyPreference =
(ListPreference) findPreference("mediaUpdateStrategy");
mediaUpdateStrategyPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
Expand Down Expand Up @@ -133,12 +143,6 @@ public boolean onPreferenceClick(Preference preference) {
updateSummary();
}

@Override
public void onPause() {
prefs2Global(this);
super.onPause();
}

public static void global2Prefs(Context context) {
fixDefaults(context, null, null);

Expand Down Expand Up @@ -302,79 +306,6 @@ public void onPause() {
super.onPause();
}

@Override
protected void onCreate(final Bundle savedInstanceState) {
LocalizedActivity.fixLocale(this); // #21: Support to change locale at runtime
super.onCreate(savedInstanceState);

if (Global.debugEnabled) {
// todo create junit integration tests with arabic locale from this.
StringFormatResourceTests.test(this);
}

final Intent intent = getIntent();
if (Global.debugEnabled && (intent != null)) {
Log.d(Global.LOG_CONTEXT, "SettingsActivity onCreate " + intent.toUri(Intent.URI_INTENT_SCHEME));
}

this.addPreferencesFromResource(R.xml.preferences);
if (Global.allow_emulate_ao10) {
this.addPreferencesFromResource(R.xml.preferences_ao10_test);
}
prefsInstance = PreferenceManager
.getDefaultSharedPreferences(this);
global2Prefs(this.getApplication());

// #21: Support to change locale at runtime
defaultLocalePreference =
(ListPreference) findPreference(Global.PREF_KEY_USER_LOCALE);
defaultLocalePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
setLanguage((String) newValue);
LocalizedActivity.recreate(SettingsActivity.this);
return true; // change is allowed
}
});

mediaUpdateStrategyPreference =
(ListPreference) findPreference("mediaUpdateStrategy");
mediaUpdateStrategyPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
LibGlobal.mediaUpdateStrategy = (String) newValue;
setPref(LibGlobal.mediaUpdateStrategy, mediaUpdateStrategyPreference, R.array.pref_media_update_strategy_names);
return true;
}
});
setPref(LibGlobal.mediaUpdateStrategy, mediaUpdateStrategyPreference, R.array.pref_media_update_strategy_names);

findPreference("debugClearLog").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onDebugClearLogCat();
return false; // donot close
}
});
findPreference("debugSaveLog").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onDebugSaveLogCat();
return false; // donot close
}
});
findPreference("translate").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onTranslate();
return false; // donot close
}
});

// #21: Support to change locale at runtime
updateSummary();
}

private static void fixDefaults(Context context, File previousCacheRoot, File previousMapsForgeDir) {
boolean mustSave = false;

Expand Down Expand Up @@ -483,12 +414,19 @@ public static void showActivity(Activity parent) {
private void updateSummary() {
final String languageKey = prefsInstance.getString(Global.PREF_KEY_USER_LOCALE, "");
setLanguage(languageKey);

setUserTheme(GuiUtil.getTheme(this));
AboutDialogPreference about =
(AboutDialogPreference) findPreference("about");
about.setTitle(AboutDialogPreference.getAboutTitle(this));
}

// #21: Support to change locale at runtime
private void setUserTheme(String newValue) {
setPref(GuiUtil.PREF_KEY_USER_THEME, this.themePreference, R.array.pref_themes_names);
}


// #21: Support to change locale at runtime
private void setLanguage(String languageKey) {
setPref(languageKey, defaultLocalePreference, R.array.pref_locale_names);
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
<item>00000</item>
</string-array>

<string-array name="pref_available_themes" translatable="false">
<item>Light</item>
<string-array name="pref_themes_values" translatable="false">
<item>Lgiht</item>
<item>Dark</item>
</string-array>

Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,15 @@ You can undo hiding by calling the mediascanner from gallery-menu."</string>

<!-- when should backup start ? -->
<string name="lbl_when">When: </string>
<string name="settings_theme">Theme</string>
<string name="theme_light">Holo light</string>
<string name="theme_dark">Holo dark</string>
<!-- #108 Zip-file export/backup will be implemented later
< ! - - what type of data should be saven ? - - >
<string name="lbl_what">What: </string>
-->

<string name="settings_theme">Theme</string>
<string name="theme_light">Holo light</string>
<string name="theme_dark">Holo dark</string>

<!-- #155: android10 -->
<string name="load_db_menu_title">(Re)Load Media Database</string>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ this program. If not, see <http://www.gnu.org/licenses/>
<ListPreference
android:defaultValue="Light"
android:entries="@array/pref_themes_names"
android:entryValues="@array/pref_available_themes"
android:entryValues="@array/pref_themes_values"
android:key="user_theme"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
Expand Down

0 comments on commit dc860d7

Please sign in to comment.