Skip to content

Commit

Permalink
Issue #10 theme switcher in settings and a black mode for AMOLED screens
Browse files Browse the repository at this point in the history
  • Loading branch information
yeriomin committed Jan 25, 2017
1 parent 65018bb commit 34b5f82
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract public class AppListActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
ThemeManager.setTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.applist_activity_layout);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ protected void onResume() {

@Override
protected void onCreate(Bundle savedInstanceState) {
ThemeManager.setTheme(this);
super.onCreate(savedInstanceState);

onNewIntent(getIntent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,13 @@ public MultiSelectListPreference(Context context, AttributeSet attrs) {
attrs, R.styleable.MultiSelectListPreference);

final int n = a.getIndexCount();
System.out.println("a has " + n + " entries");

for (int i = 0; i < n; i++) {
final int index = a.getIndex(i);
System.out.println("a[" + i + "]=" + index);
if (index == R.styleable.MultiSelectListPreference_android_entries) {
System.out.println("mEntries has " + mEntries.length + " entries");
mEntries = a.getTextArray(index);
for (int j = 0; j < mEntries.length; j++) {
System.out.println("mEntries[" + j + "]=" + mEntries[j]);
}
} else if (index == R.styleable.MultiSelectListPreference_android_entryValues) {
System.out.println("mEntryValues has " + mEntryValues.length + " entries");
mEntryValues = a.getTextArray(index);
for (int j = 0; j < mEntryValues.length; j++) {
System.out.println("mEntryValues[" + j + "]=" + mEntryValues[j]);
}
}
}
a.recycle();
Expand Down Expand Up @@ -196,16 +186,9 @@ public void onClick(DialogInterface dialog, int which,
}

private boolean[] getSelectedItems(final Set<String> values) {
for (String v: values) {
System.out.println("Value " + v);
}
final CharSequence[] entries = mEntryValues;
final int entryCount = entries.length;
boolean[] result = new boolean[entryCount];
System.out.println("entryCount " + entryCount);
for (CharSequence v: mEntryValues) {
System.out.println("entries " + v);
}

for (int i = 0; i < entryCount; i++) {
result[i] = values.contains(entries[i].toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ public class PreferenceActivity extends android.preference.PreferenceActivity {
public static final String PREFERENCE_HIDE_NONFREE_APPS = "PREFERENCE_HIDE_NONFREE_APPS";
public static final String PREFERENCE_UPDATE_LIST_WHITE_OR_BLACK = "PREFERENCE_UPDATE_LIST_WHITE_OR_BLACK";
public static final String PREFERENCE_UPDATE_LIST = "PREFERENCE_UPDATE_LIST";
public static final String PREFERENCE_UI_THEME = "PREFERENCE_UI_THEME";

public static final String LIST_WHITE = "white";
public static final String LIST_BLACK = "black";

public static final String THEME_NONE = "none";
public static final String THEME_LIGHT = "light";
public static final String THEME_DARK = "dark";
public static final String THEME_BLACK = "black";

@Override
public void onCreate(Bundle savedInstanceState) {
ThemeManager.setTheme(this);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);

Expand All @@ -42,11 +49,15 @@ public void onCreate(Bundle savedInstanceState) {
}

final MultiSelectListPreference m = (MultiSelectListPreference) findPreference(PREFERENCE_UPDATE_LIST);
ListPreference blackOrWhite = (ListPreference) findPreference(PREFERENCE_UPDATE_LIST_WHITE_OR_BLACK);
final ListPreference blackOrWhite = (ListPreference) findPreference(PREFERENCE_UPDATE_LIST_WHITE_OR_BLACK);
m.setTitle(blackOrWhite.getValue() == LIST_BLACK
? getString(R.string.pref_update_list_black)
: getString(R.string.pref_update_list_white)
);
blackOrWhite.setSummary(blackOrWhite.getValue() == LIST_BLACK
? getString(R.string.pref_update_list_white_or_black_black)
: getString(R.string.pref_update_list_white_or_black_white)
);
m.setEntries(entries.toArray(new CharSequence[entries.size()]));
m.setEntryValues(entryValues.toArray(new CharSequence[entryValues.size()]));

Expand All @@ -57,13 +68,45 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
switch (value) {
case LIST_BLACK:
m.setTitle(getString(R.string.pref_update_list_black));
blackOrWhite.setSummary(getString(R.string.pref_update_list_white_or_black_black));
break;
case LIST_WHITE:
m.setTitle(getString(R.string.pref_update_list_white));
blackOrWhite.setSummary(getString(R.string.pref_update_list_white_or_black_white));
break;
}
return true;
}
});

final ListPreference theme = (ListPreference) findPreference(PREFERENCE_UI_THEME);
theme.setSummary(getString(getThemeSummaryStringId(theme.getValue())));
theme.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, final Object newValue) {
theme.setSummary(getString(getThemeSummaryStringId((String) newValue)));
return true;
}
});
}

private int getThemeSummaryStringId(String theme) {
int summaryId;
switch (theme) {
case THEME_LIGHT:
summaryId = R.string.pref_ui_theme_light;
break;
case THEME_DARK:
summaryId = R.string.pref_ui_theme_dark;
break;
case THEME_BLACK:
summaryId = R.string.pref_ui_theme_black;
break;
case THEME_NONE:
default:
summaryId = R.string.pref_ui_theme_none;
break;
}
return summaryId;
}
}
50 changes: 50 additions & 0 deletions app/src/main/java/com/github/yeriomin/yalpstore/ThemeManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.yeriomin.yalpstore;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;

public class ThemeManager {

static public void setTheme(Activity activity) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
String theme = prefs.getString(PreferenceActivity.PREFERENCE_UI_THEME, PreferenceActivity.THEME_NONE);
switch (theme) {
case PreferenceActivity.THEME_NONE:
break;
case PreferenceActivity.THEME_LIGHT:
activity.setTheme(getThemeLight());
break;
case PreferenceActivity.THEME_DARK:
activity.setTheme(getThemeDark());
break;
case PreferenceActivity.THEME_BLACK:
activity.setTheme(getThemeDark());
activity.getWindow().setBackgroundDrawableResource(android.R.color.black);
break;
}
}

static private int getThemeLight() {
int sdk = Build.VERSION.SDK_INT;
if (sdk >= Build.VERSION_CODES.LOLLIPOP) {
return android.R.style.Theme_Material_Light;
} else if (sdk >= Build.VERSION_CODES.HONEYCOMB) {
return android.R.style.Theme_Holo_Light;
} else {
return android.R.style.Theme_Light;
}
}

static private int getThemeDark() {
int sdk = Build.VERSION.SDK_INT;
if (sdk >= Build.VERSION_CODES.LOLLIPOP) {
return android.R.style.Theme_Material;
} else if (sdk >= Build.VERSION_CODES.HONEYCOMB) {
return android.R.style.Theme_Holo;
} else {
return android.R.style.Theme;
}
}
}
12 changes: 12 additions & 0 deletions app/src/main/res/values/array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,16 @@
<item>@string/pref_update_list_white_or_black_black</item>
<item>@string/pref_update_list_white_or_black_white</item>
</string-array>
<string-array name="uiThemeValues">
<item>none</item>
<item>light</item>
<item>dark</item>
<item>black</item>
</string-array>
<string-array name="uiThemeLabels">
<item>@string/pref_ui_theme_none</item>
<item>@string/pref_ui_theme_light</item>
<item>@string/pref_ui_theme_dark</item>
<item>@string/pref_ui_theme_black</item>
</string-array>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
<string name="pref_update_list_white_or_black">Black or white list of apps for update</string>
<string name="pref_update_list_black">Change apps black list</string>
<string name="pref_update_list_white">Change apps white list</string>
<string name="pref_ui_theme">Theme</string>
<string name="pref_ui_theme_none">Default</string>
<string name="pref_ui_theme_light">Light</string>
<string name="pref_ui_theme_dark">Dark</string>
<string name="pref_ui_theme_black">Black</string>
<string name="download_manager_ERROR_UNKNOWN">Unknown error.</string>
<string name="download_manager_ERROR_FILE_ERROR">Unknown file storage error.</string>
<string name="download_manager_ERROR_UNHANDLED_HTTP_CODE">HTTP code was received that download manager can not handle. This is a server error.</string>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@
android:persistent="true"
android:key="PREFERENCE_UPDATE_LIST"
android:title="@string/pref_update_list_black" />
<ListPreference
android:key="PREFERENCE_UI_THEME"
android:defaultValue="none"
android:title="@string/pref_ui_theme"
android:entries="@array/uiThemeLabels"
android:entryValues="@array/uiThemeValues"/>
</PreferenceScreen>

0 comments on commit 34b5f82

Please sign in to comment.