Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface AppPreferences {
* events.
*/
interface Listener {
default void onDarkThemeEnabledChanged(boolean enabled) {
default void onDarkThemeModeChanged(DarkMode mode) {
/* default empty implementation */
};
}
Expand Down Expand Up @@ -274,18 +274,18 @@ default void onDarkThemeEnabledChanged(boolean enabled) {
int getUploaderBehaviour();

/**
* Enable dark theme.
* Changes dark theme mode
*
* This is reactive property. Listeners will be invoked if registered.
*
* @param enabled true to turn dark theme on, false to turn it off
* @param mode dark mode setting: on, off, system
*/
void setDarkThemeEnabled(boolean enabled);
void setDarkThemeMode(DarkMode mode);

/**
* @return true if application uses dark UI theme, false otherwise
* @return dark mode setting: on, off, system
*/
boolean isDarkThemeEnabled();
DarkMode getDarkThemeMode();

/**
* Saves the uploader behavior which the user has set last.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

package com.nextcloud.client.preferences;

import android.accounts.Account;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
Expand Down Expand Up @@ -57,6 +56,7 @@ public final class AppPreferencesImpl implements AppPreferences {
*/
public static final String AUTO_PREF__LAST_SEEN_VERSION_CODE = "lastSeenVersionCode";
public static final String STORAGE_PATH = "storage_path";
public static final String PREF__DARK_THEME = "dark_theme_mode";
public static final float DEFAULT_GRID_COLUMN = 4.0f;

private static final String AUTO_PREF__LAST_UPLOAD_PATH = "last_upload_path";
Expand All @@ -79,7 +79,6 @@ public final class AppPreferencesImpl implements AppPreferences {
private static final String PREF__AUTO_UPLOAD_INIT = "autoUploadInit";
private static final String PREF__FOLDER_SORT_ORDER = "folder_sort_order";
private static final String PREF__FOLDER_LAYOUT = "folder_layout";
static final String PREF__DARK_THEME_ENABLED = "dark_theme_enabled";

private static final String PREF__LOCK_TIMESTAMP = "lock_timestamp";
private static final String PREF__SHOW_MEDIA_SCAN_NOTIFICATIONS = "show_media_scan_notifications";
Expand Down Expand Up @@ -121,10 +120,10 @@ void remove(@Nullable final Listener listener) {

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(PREF__DARK_THEME_ENABLED.equals(key)) {
boolean enabled = preferences.isDarkThemeEnabled();
if (PREF__DARK_THEME.equals(key)) {
DarkMode mode = preferences.getDarkThemeMode();
for(Listener l : listeners) {
l.onDarkThemeEnabledChanged(enabled);
l.onDarkThemeModeChanged(mode);
}
}
}
Expand Down Expand Up @@ -408,13 +407,18 @@ public int getUploaderBehaviour() {
}

@Override
public void setDarkThemeEnabled(boolean enabled) {
preferences.edit().putBoolean(PREF__DARK_THEME_ENABLED, enabled).apply();
public void setDarkThemeMode(DarkMode mode) {
preferences.edit().putString(PREF__DARK_THEME, mode.name()).apply();
}

@Override
public boolean isDarkThemeEnabled() {
return preferences.getBoolean(PREF__DARK_THEME_ENABLED, false);
public DarkMode getDarkThemeMode() {
try {
return DarkMode.valueOf(preferences.getString(PREF__DARK_THEME, DarkMode.LIGHT.name()));
} catch (ClassCastException e) {
preferences.edit().putString(PREF__DARK_THEME, DarkMode.LIGHT.name()).apply();
return DarkMode.DARK;
}
}

@Override
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/nextcloud/client/preferences/DarkMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2019 Tobias Kaminsky
* Copyright (C) 2019 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.nextcloud.client.preferences;

public enum DarkMode {
DARK, LIGHT, SYSTEM
}
19 changes: 13 additions & 6 deletions src/main/java/com/owncloud/android/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import com.nextcloud.client.onboarding.OnboardingService;
import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.client.preferences.AppPreferencesImpl;
import com.nextcloud.client.preferences.DarkMode;
import com.owncloud.android.authentication.PassCodeManager;
import com.owncloud.android.datamodel.ArbitraryDataProvider;
import com.owncloud.android.datamodel.MediaFolder;
Expand Down Expand Up @@ -247,7 +248,7 @@ protected void attachBaseContext(Context base) {
@SuppressFBWarnings("ST")
@Override
public void onCreate() {
setAppTheme(preferences.isDarkThemeEnabled());
setAppTheme(preferences.getDarkThemeMode());
super.onCreate();

insertConscrypt();
Expand Down Expand Up @@ -821,11 +822,17 @@ public AndroidInjector<Object> androidInjector() {
}


public static void setAppTheme(Boolean darkTheme) {
if (darkTheme) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
public static void setAppTheme(DarkMode mode) {
switch (mode) {
case LIGHT:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
case DARK:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
case SYSTEM:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
break;
}
}
}
13 changes: 7 additions & 6 deletions src/main/java/com/owncloud/android/ui/activity/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.nextcloud.client.di.Injectable;
import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.java.util.Optional;
import com.nextcloud.client.preferences.DarkMode;
import com.owncloud.android.MainApp;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
Expand Down Expand Up @@ -59,8 +60,8 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab

private AppPreferences.Listener onPreferencesChanged = new AppPreferences.Listener() {
@Override
public void onDarkThemeEnabledChanged(boolean enabled) {
BaseActivity.this.onThemeSettingsChanged();
public void onDarkThemeModeChanged(DarkMode mode) {
onThemeSettingsModeChanged();
}
};

Expand Down Expand Up @@ -91,7 +92,7 @@ protected void onResume() {
super.onResume();
paused = false;

if(themeChangePending) {
if (themeChangePending) {
recreate();
}
}
Expand Down Expand Up @@ -129,8 +130,8 @@ protected void onRestart() {
Log_OC.v(TAG, "onRestart() end");
}

private void onThemeSettingsChanged() {
if(paused) {
private void onThemeSettingsModeChanged() {
if (paused) {
themeChangePending = true;
} else {
recreate();
Expand Down Expand Up @@ -226,7 +227,7 @@ public Optional<User> getUser() {
return Optional.empty();
}
}

public FileDataStorageManager getStorageManager() {
return storageManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@
import com.bumptech.glide.request.target.SimpleTarget;
import com.google.android.material.navigation.NavigationView;
import com.nextcloud.client.account.User;
import com.nextcloud.client.account.UserAccountManager;
import com.nextcloud.client.di.Injectable;
import com.nextcloud.client.network.ClientFactory;
import com.nextcloud.client.onboarding.FirstRunActivity;
import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.client.preferences.DarkMode;
import com.owncloud.android.MainApp;
import com.owncloud.android.R;
import com.owncloud.android.authentication.PassCodeManager;
Expand All @@ -71,7 +71,6 @@
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.lib.common.ExternalLink;
import com.owncloud.android.lib.common.ExternalLinkType;
import com.owncloud.android.lib.common.OwnCloudAccount;
import com.owncloud.android.lib.common.Quota;
import com.owncloud.android.lib.common.UserInfo;
import com.owncloud.android.lib.common.accounts.ExternalLinksOperation;
Expand Down Expand Up @@ -1275,9 +1274,12 @@ public void onBackPressed() {
@Override
protected void onResume() {
super.onResume();
getDelegate().setLocalNightMode(preferences.isDarkThemeEnabled() ?
AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
getDelegate().applyDayNight();
if (AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) {

getDelegate().setLocalNightMode(DarkMode.DARK == preferences.getDarkThemeMode() ?
AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
getDelegate().applyDayNight();
}
setDrawerMenuItemChecked(mCheckedMenuItem);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import com.nextcloud.client.network.ClientFactory;
import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.client.preferences.AppPreferencesImpl;
import com.nextcloud.client.preferences.DarkMode;
import com.owncloud.android.BuildConfig;
import com.owncloud.android.MainApp;
import com.owncloud.android.R;
Expand All @@ -79,6 +80,7 @@
import com.owncloud.android.utils.ThemeUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

Expand Down Expand Up @@ -106,6 +108,7 @@ public class SettingsActivity extends ThemedPreferenceActivity
public static final String LOCK_PASSCODE = "passcode";
public static final String LOCK_DEVICE_CREDENTIALS = "device_credentials";


public final static String PREFERENCE_USE_FINGERPRINT = "use_fingerprint";
public static final String PREFERENCE_SHOW_MEDIA_SCAN_NOTIFICATIONS = "show_media_scan_notifications";

Expand Down Expand Up @@ -692,13 +695,27 @@ private void setupGeneralCategory(int accentColor) {

loadStoragePath();

SwitchPreference themePref = (SwitchPreference) findPreference("dark_theme_enabled");
boolean darkThemeEnabled = preferences.isDarkThemeEnabled();
int summaryResId = darkThemeEnabled ? R.string.prefs_value_theme_dark : R.string.prefs_value_theme_light;
themePref.setSummary(summaryResId);
ListPreference themePref = (ListPreference) findPreference("darkTheme");

List<String> themeEntries = new ArrayList<>(3);
themeEntries.add(getString(R.string.prefs_value_theme_light));
themeEntries.add(getString(R.string.prefs_value_theme_dark));
themeEntries.add(getString(R.string.prefs_value_theme_system));

List<String> themeValues = new ArrayList<>(3);
themeValues.add(DarkMode.LIGHT.name());
themeValues.add(DarkMode.DARK.name());
themeValues.add(DarkMode.SYSTEM.name());

themePref.setEntries(themeEntries.toArray(new String[0]));
themePref.setEntryValues(themeValues.toArray(new String[0]));
themePref.setSummary(themePref.getEntry().length() == 0 ? DarkMode.LIGHT.name() : themePref.getEntry());

themePref.setOnPreferenceChangeListener((preference, newValue) -> {
boolean enabled = (Boolean)newValue;
MainApp.setAppTheme(enabled);
DarkMode mode = DarkMode.valueOf((String) newValue);
preferences.setDarkThemeMode(mode);
MainApp.setAppTheme(mode);

return true;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.preference.PreferenceActivity;

import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.client.preferences.DarkMode;

import javax.inject.Inject;

Expand All @@ -41,8 +42,10 @@ public class ThemedPreferenceActivity extends PreferenceActivity {

private AppPreferences.Listener onThemeChangedListener = new AppPreferences.Listener() {
@Override
public void onDarkThemeEnabledChanged(boolean enabled) {
if(paused) {
public void onDarkThemeModeChanged(DarkMode mode) {
preferences.setDarkThemeMode(mode);

if (paused) {
themeChangePending = true;
return;
}
Expand Down Expand Up @@ -73,7 +76,7 @@ protected void onResume() {
super.onResume();
paused = false;

if(themeChangePending) {
if (themeChangePending) {
recreate();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<string name="prefs_imprint">Imprint</string>
<string name="prefs_value_theme_light">Light</string>
<string name="prefs_value_theme_dark">Dark</string>
<string name="prefs_value_theme_system">Follow system</string>
<string name="prefs_theme_title">Theme</string>


Expand Down
3 changes: 0 additions & 3 deletions src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,4 @@
<item name="android:scaleType">fitCenter</item>
<item name="android:layout_gravity">center_vertical</item>
</style>
<style name="SwitchPreference" parent="Widget.AppCompat.CompoundButton.Switch">
<item name="android:colorForeground">@color/fg_default</item>
</style>
</resources>
9 changes: 3 additions & 6 deletions src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@
<ListPreference
android:title="@string/prefs_storage_path"
android:key="storage_path"/>
<com.owncloud.android.ui.ThemeableSwitchPreference
android:id="@+id/dark_theme_preference"
android:defaultValue="@string/prefs_value_theme_light"
android:key="dark_theme_enabled"
android:summary="%s"
<ListPreference
android:title="@string/prefs_theme_title"
android:theme="@style/SwitchPreference"/>
android:key="darkTheme"
android:summary="%s" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/drawer_synced_folders"
Expand Down
Loading