Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 79e630b9 authored by Christoph Loy's avatar Christoph Loy
Browse files

Add basic implementation to support "System default" in dark mode settings

parent 8f7ab6d8
Loading
Loading
Loading
Loading
+64 −0
Original line number Diff line number Diff line
package it.niedermann.owncloud.notes.android;

import androidx.appcompat.app.AppCompatDelegate;

import java.util.NoSuchElementException;

/**
 * Possible values of the Dark Mode Setting.
 * <p>
 * The Dark Mode Setting can be stored in {@link android.content.SharedPreferences} as String by using {@link DarkModeSetting#name()} and received via {@link DarkModeSetting#valueOf(String)}.
 * <p>
 * Additionally, the equivalent {@link AppCompatDelegate}-Mode can be received via {@link #getModeId()}. To convert a {@link AppCompatDelegate}-Mode to a {@link DarkModeSetting}, use {@link #fromModeID(int)}
 *
 * @see AppCompatDelegate#MODE_NIGHT_YES
 * @see AppCompatDelegate#MODE_NIGHT_NO
 * @see AppCompatDelegate#MODE_NIGHT_FOLLOW_SYSTEM
 */
public enum DarkModeSetting {
    /**
     * Always use light mode.
     */
    LIGHT(AppCompatDelegate.MODE_NIGHT_NO),
    /**
     * Always use dark mode.
     */
    DARK(AppCompatDelegate.MODE_NIGHT_YES),
    /**
     * Follow the global system setting for dark mode.
     */
    SYSTEM_DEFAULT(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);

    private final int modeId;

    DarkModeSetting(int modeId) {
        this.modeId = modeId;
    }

    public int getModeId() {
        return modeId;
    }

    /**
     * Returns the instance of {@link DarkModeSetting} that corresponds to the ModeID of {@link AppCompatDelegate}
     * <p>
     * Possible ModeIDs are:
     * <ul>
     *     <li>{@link AppCompatDelegate#MODE_NIGHT_YES}</li>
     *     <li>{@link AppCompatDelegate#MODE_NIGHT_NO}</li>
     *     <li>{@link AppCompatDelegate#MODE_NIGHT_FOLLOW_SYSTEM}</li>
     * </ul>
     *
     * @param id One of the {@link AppCompatDelegate}-Night-Modes
     * @return An instance of {@link DarkModeSetting}
     */
    public static DarkModeSetting fromModeID(int id) {
        for (DarkModeSetting value : DarkModeSetting.values()) {
            if (value.modeId == id) {
                return value;
            }
        }

        throw new NoSuchElementException("No NightMode with ID " + id + " found");
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ public class SelectSingleNoteActivity extends NotesListViewActivity {

        sp.putLong(SingleNoteWidget.WIDGET_KEY + appWidgetId, noteID);
        sp.putLong(SingleNoteWidget.ACCOUNT_ID_KEY + appWidgetId, note.getAccountId());
        sp.putBoolean(SingleNoteWidget.DARK_THEME_KEY + appWidgetId, Notes.getAppTheme(getApplicationContext()));
        sp.putString(SingleNoteWidget.DARK_THEME_KEY + appWidgetId, Notes.getAppTheme(getApplicationContext()).name());
        sp.apply();

        Intent updateIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null,
+5 −3
Original line number Diff line number Diff line
@@ -13,8 +13,10 @@ import android.util.Log;
import android.widget.RemoteViews;

import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.android.DarkModeSetting;
import it.niedermann.owncloud.notes.android.activity.EditNoteActivity;
import it.niedermann.owncloud.notes.android.activity.NotesListViewActivity;
import it.niedermann.owncloud.notes.util.Notes;

public class NoteListWidget extends AppWidgetProvider {
    private static final String TAG = NoteListWidget.class.getSimpleName();
@@ -28,7 +30,7 @@ public class NoteListWidget extends AppWidgetProvider {

    static void updateAppWidget(Context context, AppWidgetManager awm, int[] appWidgetIds) {
        RemoteViews views;
        boolean darkTheme;
        String darkTheme;

        for (int appWidgetId : appWidgetIds) {
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
@@ -40,7 +42,7 @@ public class NoteListWidget extends AppWidgetProvider {
            }

            String category = sp.getString(NoteListWidget.WIDGET_CATEGORY_KEY + appWidgetId, null);
            darkTheme = sp.getBoolean(NoteListWidget.DARK_THEME_KEY + appWidgetId, false);
            darkTheme = sp.getString(NoteListWidget.DARK_THEME_KEY + appWidgetId, DarkModeSetting.SYSTEM_DEFAULT.name());

            Intent serviceIntent = new Intent(context, NoteListWidgetService.class);
            serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
@@ -70,7 +72,7 @@ public class NoteListWidget extends AppWidgetProvider {
                    (new Intent(context, EditNoteActivity.class)),
                    PendingIntent.FLAG_UPDATE_CURRENT);

            if (darkTheme) {
            if (Notes.isDarkThemeActive(context, DarkModeSetting.valueOf(darkTheme))) {
                views = new RemoteViews(context.getPackageName(), R.layout.widget_note_list_dark);
                views.setTextViewText(R.id.widget_note_list_title_tv_dark, getWidgetTitle(context, displayMode, category));
                views.setOnClickPendingIntent(R.id.widget_note_header_icon_dark, openAppI);
+1 −1
Original line number Diff line number Diff line
@@ -100,7 +100,7 @@ public class NoteListWidgetConfiguration extends AppCompatActivity {
                }

                sp.putLong(NoteListWidget.ACCOUNT_ID_KEY + appWidgetId, localAccount.getId());
                sp.putBoolean(NoteListWidget.DARK_THEME_KEY + appWidgetId, Notes.getAppTheme(getApplicationContext()));
                sp.putString(NoteListWidget.DARK_THEME_KEY + appWidgetId, Notes.getAppTheme(getApplicationContext()).name());
                sp.apply();

                Intent updateIntent = new Intent(   AppWidgetManager.ACTION_APPWIDGET_UPDATE, null,
+5 −1
Original line number Diff line number Diff line
@@ -13,9 +13,11 @@ import android.widget.RemoteViewsService;
import java.util.List;

import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.android.DarkModeSetting;
import it.niedermann.owncloud.notes.android.activity.EditNoteActivity;
import it.niedermann.owncloud.notes.model.DBNote;
import it.niedermann.owncloud.notes.persistence.NoteSQLiteOpenHelper;
import it.niedermann.owncloud.notes.util.Notes;

public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFactory {
    private final Context context;
@@ -32,7 +34,9 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact
                AppWidgetManager.INVALID_APPWIDGET_ID);
        final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this.context);
        displayMode = sp.getInt(NoteListWidget.WIDGET_MODE_KEY + appWidgetId, -1);
        darkTheme = sp.getBoolean(NoteListWidget.DARK_THEME_KEY + appWidgetId, false);
        String themeName = sp.getString(NoteListWidget.DARK_THEME_KEY + appWidgetId, DarkModeSetting.SYSTEM_DEFAULT.name());
        DarkModeSetting theme = DarkModeSetting.valueOf(themeName);
        darkTheme = Notes.isDarkThemeActive(context, theme);
        category = sp.getString(NoteListWidget.WIDGET_CATEGORY_KEY + appWidgetId, "");
        accountId = sp.getLong(NoteListWidget.ACCOUNT_ID_KEY + appWidgetId, -1);
    }
Loading