Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ThemeChooser.java « helper « owncloudnewsreader « luhmer « de « java « main « src « News-Android-App - github.com/nextcloud/news-android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6602ed49190cefc0266e300d95c35afa98f0fa2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Android ownCloud News
*
* @author David Luhmer
* @copyright 2013 David Luhmer david-dev@live.de
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*
*/

package de.luhmer.owncloudnewsreader.helper;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Build;
import android.util.Log;

import androidx.appcompat.app.AppCompatDelegate;

import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.SettingsActivity;

public class ThemeChooser {

    private static final String TAG = ThemeChooser.class.getCanonicalName();

    public enum THEME { LIGHT, DARK, OLED }


    // Contains the selected theme defined in the settings (used for checking whether the app needs
    // to restart after changing the theme
    private static Integer mSelectedThemeFromPreferences;
    private static Boolean mOledMode;
    private static SharedPreferences mPrefs;

    // Contains the current selected theme
    private static THEME mSelectedTheme = THEME.LIGHT;

    private ThemeChooser() { }

    public static void chooseTheme(Activity act) {
        int defaultNightMode;
        switch(getSelectedThemeFromPreferences(false)) {
            case 0: // Auto (Light / Dark)
                Log.v(TAG, "Auto (Light / Dark)");
                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
                    defaultNightMode = AppCompatDelegate.MODE_NIGHT_AUTO_TIME;
                } else { // Android 10+ (Q) supports a system-wide dark mode
                    defaultNightMode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
                }
                mSelectedTheme = THEME.LIGHT;
                break;
            case 1: // Light Theme
                Log.v(TAG, "Light");
                defaultNightMode = AppCompatDelegate.MODE_NIGHT_NO;
                mSelectedTheme = THEME.LIGHT;
                break;
            case 2: // Dark Theme
                Log.v(TAG, "Dark");
                defaultNightMode = AppCompatDelegate.MODE_NIGHT_YES;
                mSelectedTheme = THEME.DARK;
                break;
            default:
                // This should never happen - just in case.. use the light theme..
                Log.v(TAG, "Default");
                defaultNightMode = AppCompatDelegate.MODE_NIGHT_AUTO_TIME;
                mSelectedTheme = THEME.LIGHT;
                break;
        }
        act.setTheme(R.style.AppTheme);
        AppCompatDelegate.setDefaultNightMode(defaultNightMode);
    }

    public static void afterOnCreate(Activity act) {
        //int uiNightMode = Configuration.UI_MODE_NIGHT_NO;

        if(isDarkTheme(act)) {
            mSelectedTheme = THEME.DARK; // this is required for auto mode at night

            if (isOledMode(false) && isDarkTheme(act)) {
                act.setTheme(R.style.AppThemeOLED);
                Log.v(TAG, "activate OLED mode");
                //uiNightMode = Configuration.UI_MODE_NIGHT_YES;
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

                mSelectedTheme = THEME.OLED;
            }
        }

        /*
        Configuration newConfig = new Configuration(act.getResources().getConfiguration());
        newConfig.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
        newConfig.uiMode |= uiNightMode;
        act.getResources().updateConfiguration(newConfig, null);
        */
    }

    /**
     * Returns true if automatic theme selection is enabled.
     * Otherwise it'll return false
     */
    public static boolean isAutoThemeSelectionEnabled() {
        int selectedTheme = getSelectedThemeFromPreferences(false);
        return selectedTheme == 0; // 0 => Auto (Light / Dark)
    }

    // Check if the currently loaded theme is different from the one set in the settings, or if OLED mode changed
    public static boolean themeRequiresRestartOfUI() {
        boolean themeChanged = !mSelectedThemeFromPreferences.equals(getSelectedThemeFromPreferences(true));
        boolean oledChanged = !mOledMode.equals(isOledMode(true));
        Log.v(TAG, "themeChanged: " + themeChanged + "; oledChanged: " + oledChanged);
        return themeChanged || oledChanged;
    }

    private static boolean isDarkTheme(Context context) {
        switch(AppCompatDelegate.getDefaultNightMode()) {
            case AppCompatDelegate.MODE_NIGHT_YES:
                Log.v(TAG, "MODE_NIGHT_YES (Dark Theme)");
                return true;
            case AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM:
            case AppCompatDelegate.MODE_NIGHT_AUTO:
                //Log.v(TAG, "MODE_NIGHT_AUTO");
                int nightModeFlags = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
                if(Configuration.UI_MODE_NIGHT_YES == nightModeFlags) {
                    Log.v(TAG, "MODE_NIGHT_AUTO (Dark Theme)");
                    return true;
                }
                Log.v(TAG, "MODE_NIGHT_AUTO (Light Theme)");
                return false;
            case AppCompatDelegate.MODE_NIGHT_NO:
                Log.v(TAG, "MODE_NIGHT_NO (Light Theme)");
                return false;
            default:
                Log.v(TAG, "Undefined Night-Mode");
                return false;
        }
    }

    public static boolean isOledMode(boolean forceReloadCache) {
        if(mOledMode == null || forceReloadCache) {
            mOledMode = mPrefs.getBoolean(SettingsActivity.CB_OLED_MODE, false);
        }
        return mOledMode;
    }

    public static THEME getSelectedTheme() {
        return mSelectedTheme;
    }

    private static int getSelectedThemeFromPreferences(boolean forceReloadCache) {
        if(mSelectedThemeFromPreferences == null || forceReloadCache) {
            mSelectedThemeFromPreferences = Integer.parseInt(mPrefs.getString(SettingsActivity.SP_APP_THEME, "0"));
        }
        return mSelectedThemeFromPreferences;
    }

    public static void init(SharedPreferences prefs) {
        mPrefs = prefs;
        getSelectedThemeFromPreferences(true); // Init cache
        isOledMode(true); // Init cache
    }
}