Skip to content

Commit

Permalink
plusonelabs#28 Add "Different colors for Android system Dark theme" o…
Browse files Browse the repository at this point in the history
…ption. It doesn't change anything yet.
  • Loading branch information
yvolk committed Sep 13, 2020
1 parent 4b77801 commit 2b93824
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ In particular, [if buttons don't work, look here](https://github.com/andstatus/t

## Changelog

<a id="v4.3"/>

### 2020-09-13 v4.3.0 Android system Dark theme support.
* [Different colors for Android system Dark theme](https://github.com/andstatus/todoagenda/issues/28)
option added for Android 10+.
When the option is turned on, the widget uses different sets of colors for Dark and Light themes.<br />
Both sets of colors are configured in the same "Colors" settings section.
Colors for "Dark theme" are configured, when Dark theme is turned on.
Colors for "Light theme" are configured, when Dark theme is turned off.

<a id="v4.2.4"/>

### 2020-09-13 v4.2.4 All day events improvements.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_DAY_HEADER_ALIGNMENT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_DAY_HEADER_DATE_FORMAT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_DAY_HEADER_DATE_FORMAT_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_DIFFERENT_COLORS_FOR_DARK;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ENTRY_DATE_FORMAT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_ENTRY_DATE_FORMAT_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_EVENTS_BACKGROUND_COLOR;
Expand Down Expand Up @@ -193,6 +194,10 @@ private static void setHideBasedOnKeywords(Context context, String value) {
setString(context, PREF_HIDE_BASED_ON_KEYWORDS, value);
}

public static boolean areDifferentColorsForDark(Context context) {
return getBoolean(context, PREF_DIFFERENT_COLORS_FOR_DARK, false);
}

public static int getWidgetHeaderBackgroundColor(Context context) {
return getInt(context, PREF_WIDGET_HEADER_BACKGROUND_COLOR,
PREF_WIDGET_HEADER_BACKGROUND_COLOR_DEFAULT);
Expand Down Expand Up @@ -428,7 +433,8 @@ public static WidgetHeaderLayout getWidgetHeaderLayout(Context context) {
}

public static boolean noPastEvents(Context context) {
return !getShowPastEventsWithDefaultColor(context) &&
return context != null &&
!getShowPastEventsWithDefaultColor(context) &&
getEventsEnded(context) == EndedSomeTimeAgo.NONE &&
noTaskSources(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,38 @@
public class ColorsPreferencesFragment extends PreferenceFragmentCompat
implements SharedPreferences.OnSharedPreferenceChangeListener {

private boolean hidePast;

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences_colors);
removeUnavailablePreferences();
}

@Override
public void onResume() {
super.onResume();
removeUnavailablePreferences();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
hidePast = ApplicationPreferences.noPastEvents(getActivity());
showShadings();
}

private void showShadings() {
if (hidePast) {
private void removeUnavailablePreferences() {
if (!InstanceSettings.canHaveDifferentColorsForDark()) {
PreferenceScreen screen = getPreferenceScreen();
Preference preference = findPreference(InstanceSettings.PREF_DIFFERENT_COLORS_FOR_DARK);
if (screen != null && preference != null) {
screen.removePreference(preference);
}
}
if (ApplicationPreferences.noPastEvents(getActivity())) {
PreferenceScreen screen = getPreferenceScreen();
Preference preference = findPreference(TimeSection.PAST.preferenceCategoryKey);
if (screen != null && preference != null) {
screen.removePreference(preference);
}
}
}

private void showShadings() {
for (TextShadingPref shadingPref : TextShadingPref.values()) {
showShading(shadingPref);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.ContextThemeWrapper;

Expand Down Expand Up @@ -92,6 +93,9 @@ public class InstanceSettings {

// ----------------------------------------------------------------------------------
// Colors
static final String PREF_DIFFERENT_COLORS_FOR_DARK = "differentColorsForDark";
private boolean differentColorsForDark = false;

final Map<TextShadingPref, TextShading> shadings = new ConcurrentHashMap<>();

static final String PREF_WIDGET_HEADER_BACKGROUND_COLOR = "widgetHeaderBackgroundColor";
Expand Down Expand Up @@ -221,6 +225,11 @@ private InstanceSettings setFromJson(JSONObject json) {
if (json.has(PREF_HIDE_BASED_ON_KEYWORDS)) {
hideBasedOnKeywords = json.getString(PREF_HIDE_BASED_ON_KEYWORDS);
}

// Colors
if (json.has(PREF_DIFFERENT_COLORS_FOR_DARK)) {
differentColorsForDark = json.getBoolean(PREF_DIFFERENT_COLORS_FOR_DARK);
}
if (json.has(PREF_WIDGET_HEADER_BACKGROUND_COLOR)) {
widgetHeaderBackgroundColor = json.getInt(PREF_WIDGET_HEADER_BACKGROUND_COLOR);
}
Expand All @@ -233,6 +242,7 @@ private InstanceSettings setFromJson(JSONObject json) {
if (json.has(PREF_EVENTS_BACKGROUND_COLOR)) {
eventsBackgroundColor = json.getInt(PREF_EVENTS_BACKGROUND_COLOR);
}

if (json.has(PREF_SHOW_DAYS_WITHOUT_EVENTS)) {
showDaysWithoutEvents = json.getBoolean(PREF_SHOW_DAYS_WITHOUT_EVENTS);
}
Expand Down Expand Up @@ -354,6 +364,9 @@ static InstanceSettings fromApplicationPreferences(Context context, int widgetId
settings.eventsEnded = ApplicationPreferences.getEventsEnded(context);
settings.fillAllDayEvents = ApplicationPreferences.getFillAllDayEvents(context);
settings.hideBasedOnKeywords = ApplicationPreferences.getHideBasedOnKeywords(context);

// Colors
settings.differentColorsForDark = ApplicationPreferences.areDifferentColorsForDark(context);
settings.widgetHeaderBackgroundColor = ApplicationPreferences.getWidgetHeaderBackgroundColor(context);
settings.pastEventsBackgroundColor = ApplicationPreferences.getPastEventsBackgroundColor(context);
settings.todaysEventsBackgroundColor = ApplicationPreferences.getTodaysEventsBackgroundColor(context);
Expand Down Expand Up @@ -446,10 +459,14 @@ public JSONObject toJson() {
json.put(PREF_EVENTS_ENDED, eventsEnded.save());
json.put(PREF_FILL_ALL_DAY, fillAllDayEvents);
json.put(PREF_HIDE_BASED_ON_KEYWORDS, hideBasedOnKeywords);

// Colors
json.put(PREF_DIFFERENT_COLORS_FOR_DARK, differentColorsForDark);
json.put(PREF_WIDGET_HEADER_BACKGROUND_COLOR, widgetHeaderBackgroundColor);
json.put(PREF_PAST_EVENTS_BACKGROUND_COLOR, pastEventsBackgroundColor);
json.put(PREF_TODAYS_EVENTS_BACKGROUND_COLOR, todaysEventsBackgroundColor);
json.put(PREF_EVENTS_BACKGROUND_COLOR, eventsBackgroundColor);

json.put(PREF_SHOW_DAYS_WITHOUT_EVENTS, showDaysWithoutEvents);
json.put(PREF_SHOW_DAY_HEADERS, showDayHeaders);
json.put(PREF_DAY_HEADER_DATE_FORMAT, dayHeaderDateFormat.save());
Expand Down Expand Up @@ -547,6 +564,14 @@ public String getHideBasedOnKeywords() {
return hideBasedOnKeywords;
}

/** See https://developer.android.com/guide/topics/ui/look-and-feel/darktheme */
public static boolean canHaveDifferentColorsForDark() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q;
}

public boolean areDifferentColorsForDark(Context context) {
return differentColorsForDark;
}

public int getWidgetHeaderBackgroundColor() {
return widgetHeaderBackgroundColor;
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@
<string name="in_N_days">in %d days</string>

<!-- Preference frame: Colors -->
<string name="different_colors_for_dark_title">Different colors for Android system Dark theme</string>
<string name="different_colors_for_dark_off_desc">Colors are the same</string>
<string name="different_colors_for_dark_on_desc">Switch Android system Dark theme on/off to set colors
for Dark/Light theme correspondingly.</string>
<string name="colors_prefs_desc">Colors and opacity of texts and backgrounds</string>
<string name="appearance_group_color_title">Colors</string>
<string name="appearance_background_color_title">Background color</string>
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/xml/preferences_colors.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<org.andstatus.todoagenda.prefs.MultilineCheckBoxPreference
android:key="differentColorsForDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:defaultValue="false"
android:title="@string/different_colors_for_dark_title"
android:summaryOn="@string/different_colors_for_dark_on_desc"
android:summaryOff="@string/different_colors_for_dark_off_desc" />

<PreferenceCategory app:title="@string/widget_header">
<ListPreference
android:key="headerTheme"
Expand Down

0 comments on commit 2b93824

Please sign in to comment.