Skip to content

Commit

Permalink
#162: Fixed Merge Error; Refactored Theme code to UserTheme.java; THe…
Browse files Browse the repository at this point in the history
…me-change takes immediate effect
  • Loading branch information
k3b committed Jan 30, 2020
1 parent dc860d7 commit 20963c0
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 42 deletions.
23 changes: 0 additions & 23 deletions app/src/main/java/de/k3b/android/GuiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,13 @@

package de.k3b.android;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.preference.PreferenceManager;

import de.k3b.android.androFotoFinder.R;

/**
* 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 @@ -43,21 +36,5 @@ public static String getAppVersionName(final Context context) {
}
return null;
}
public static void setTheme(Activity act) {
final String theme = getTheme(act);
switch (theme) {
case "Light":
act.setTheme(R.style.AppTheme_Light);
break;
case "Dark":
act.setTheme(R.style.AppTheme_Dark);
break;
default:
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 @@ -39,11 +39,11 @@
import java.io.File;

import de.k3b.LibGlobal;
import de.k3b.android.GuiUtil;
import de.k3b.android.androFotoFinder.imagedetail.HugeImageLoader;
import de.k3b.android.util.PhotoPropertiesMediaFilesScanner;
import de.k3b.android.util.PhotoPropertiesMediaFilesScannerExifInterface;
import de.k3b.android.util.PhotoPropertiesMediaFilesScannerImageMetaReader;
import de.k3b.android.util.UserTheme;
import de.k3b.android.widget.AboutDialogPreference;
import de.k3b.android.widget.ActivityWithCallContext;
import de.k3b.android.widget.LocalizedActivity;
Expand All @@ -65,8 +65,7 @@ public class SettingsActivity extends PreferenceActivity {

@Override
protected void onCreate(final Bundle savedInstanceState) {
GuiUtil.setTheme(this); // this activity doesn't extent ActivityWithCallContext
LocalizedActivity.fixLocale(this); // #21: Support to change locale at runtime
LocalizedActivity.fixThemeAndLocale(this); // #21: Support to change locale at runtime
super.onCreate(savedInstanceState);

if (Global.debugEnabled) {
Expand Down Expand Up @@ -96,7 +95,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
}
});

themePreference = (ListPreference) findPreference(GuiUtil.PREF_KEY_USER_THEME);
themePreference = (ListPreference) findPreference(UserTheme.PREF_KEY_USER_THEME);
themePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Expand Down Expand Up @@ -415,17 +414,17 @@ private void updateSummary() {
final String languageKey = prefsInstance.getString(Global.PREF_KEY_USER_LOCALE, "");
setLanguage(languageKey);

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

private void setUserTheme(String newValue) {
setPref(GuiUtil.PREF_KEY_USER_THEME, this.themePreference, R.array.pref_themes_names);
setPref(UserTheme.PREF_KEY_USER_THEME, this.themePreference, R.array.pref_themes_names);
LocalizedActivity.setMustRecreate();
}


// #21: Support to change locale at runtime
private void setLanguage(String languageKey) {
setPref(languageKey, defaultLocalePreference, R.array.pref_locale_names);
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/de/k3b/android/util/UserTheme.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2019-2020 by vinceh121
*
* This file is part of AndroFotoFinder.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>
*/

package de.k3b.android.util;

import android.app.Activity;
import android.content.Context;
import android.preference.PreferenceManager;

import de.k3b.android.androFotoFinder.R;

/**
* #162: change Theme at runtime
*/
public class UserTheme {
public static final String PREF_KEY_USER_THEME = "user_theme";

public static void setTheme(Activity act) {
final String themeKey = getThemeKey(act);
int themeID = ("Dark".compareTo(themeKey) != 0)
? R.style.AppTheme_Light
: R.style.AppTheme_Dark;
act.setTheme(themeID);
}

public static String getThemeKey(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getString(PREF_KEY_USER_THEME, "Light");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2019 by k3b.
* Copyright (c) 2018-2020 by k3b.
*
* This file is part of AndroFotoFinder and of ToGoZip.
*
Expand All @@ -24,7 +24,6 @@
import android.os.Bundle;
import android.util.Log;

import de.k3b.android.GuiUtil;
import de.k3b.android.androFotoFinder.Global;

/**
Expand Down Expand Up @@ -76,7 +75,6 @@ public static void addContext(String debugContext, Intent targetIntent, Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
GuiUtil.setTheme(this); // Almost every activity extends this so use this to set theme
super.onCreate(savedInstanceState);

if (isCallContextEnabled) {
Expand Down
22 changes: 14 additions & 8 deletions app/src/main/java/de/k3b/android/widget/LocalizedActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2019 by k3b.
* Copyright (c) 2015-2020 by k3b.
*
* This file is part of AndroFotoFinder and of ToGoZip.
*
Expand Down Expand Up @@ -32,6 +32,7 @@
import java.util.Locale;

import de.k3b.android.androFotoFinder.Global;
import de.k3b.android.util.UserTheme;

/**
* An activity that can change the locale (language) of its content.
Expand All @@ -57,17 +58,16 @@ public static void setMustRecreate() {
LocalizedActivity.currentRecreationId++;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
fixLocale(this);
super.onCreate(savedInstanceState);
}

/**
* Set Activity-s locale to SharedPreferences-setting.
* Must be called before
*/
public static void fixLocale(Context context) {
public static void fixThemeAndLocale(Activity context) {
UserTheme.setTheme(context);
fixLocale(context);
}

private static void fixLocale(Context context) {
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
String language = prefs.getString(Global.PREF_KEY_USER_LOCALE, "");
Expand All @@ -94,6 +94,12 @@ public static void fixLocale(Context context) {
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
fixThemeAndLocale(this);
super.onCreate(savedInstanceState);
}

@Override
protected void onResume() {
super.onResume();
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,12 @@ Verstecken kann über das 'Media-Scanner' Gallery-Menü rückgängig gemacht wer
<string name="lbl_what">What: </string>
-->

<!-- #162: Theme support -->
<string name="settings_theme">Farbschema</string>
<string name="theme_light">Hell (Holo light)</string>
<string name="theme_dark">Dunkel (Holo dark)</string>

<!-- #155: android10 -->
<string name="load_db_menu_title">Photo.Datenbank (neu) laden</string>

</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@
<item>00000</item>
</string-array>

<!-- #162: Theme support: R.array.pref_themes_xxx must be in same order -->
<string-array name="pref_themes_values" translatable="false">
<item>Lgiht</item>
<item>Light</item>
<item>Dark</item>
</string-array>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ You can undo hiding by calling the mediascanner from gallery-menu."</string>
<string name="lbl_what">What: </string>
-->

<!-- #162: Theme support -->
<string name="settings_theme">Theme</string>
<string name="theme_light">Holo light</string>
<string name="theme_dark">Holo dark</string>
Expand Down

0 comments on commit 20963c0

Please sign in to comment.