-
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathPreferencesFragment.java
More file actions
153 lines (130 loc) · 6.11 KB
/
PreferencesFragment.java
File metadata and controls
153 lines (130 loc) · 6.11 KB
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
/*
* Nextcloud Notes - Android Client
*
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package it.niedermann.owncloud.notes.preferences;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.Branded;
import it.niedermann.owncloud.notes.branding.BrandedSwitchPreference;
import it.niedermann.owncloud.notes.branding.BrandingUtil;
import it.niedermann.owncloud.notes.persistence.SyncWorker;
import it.niedermann.owncloud.notes.shared.util.DeviceCredentialUtil;
public class PreferencesFragment extends PreferenceFragmentCompat implements Branded {
private static final String TAG = PreferencesFragment.class.getSimpleName();
private PreferencesViewModel viewModel;
private BrandedSwitchPreference fontPref;
private BrandedSwitchPreference lockPref;
private BrandedSwitchPreference wifiOnlyPref;
private BrandedSwitchPreference gridViewPref;
private BrandedSwitchPreference preventScreenCapturePref;
private BrandedSwitchPreference backgroundSyncPref;
private BrandedSwitchPreference keepScreenOnPref;
private BrandedSwitchPreference enableDirectEditorPref;
private BrandedSwitchPreference showEcosystemAppBarPref;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences);
viewModel = new ViewModelProvider(requireActivity()).get(PreferencesViewModel.class);
fontPref = findPreference(getString(R.string.pref_key_font));
showEcosystemAppBarPref = findPreference(getString(R.string.pref_key_show_ecosystem_apps));
gridViewPref = findPreference(getString(R.string.pref_key_gridview));
if (gridViewPref != null) {
gridViewPref.setOnPreferenceChangeListener((Preference preference, Object newValue) -> {
final Boolean gridView = (Boolean) newValue;
Log.v(TAG, "gridView: " + gridView);
viewModel.resultCode$.setValue(Activity.RESULT_OK);
NotesApplication.updateGridViewEnabled(gridView);
return true;
});
} else {
Log.e(TAG, "Could not find preference with key: \"" + getString(R.string.pref_key_gridview) + "\"");
}
keepScreenOnPref = findPreference(getString(R.string.pref_key_keep_screen_on));
if (keepScreenOnPref != null) {
keepScreenOnPref.setOnPreferenceChangeListener((Preference preference, Object newValue) -> {
Log.v(TAG, "keepScreenOnPref: " + keepScreenOnPref);
return true;
});
} else {
Log.e(TAG, "Could not find preference with key: \"" + getString(R.string.pref_key_gridview) + "\"");
}
preventScreenCapturePref = findPreference(getString(R.string.pref_key_prevent_screen_capture));
if (preventScreenCapturePref == null) {
Log.e(TAG, "Could not find \"" + getString(R.string.pref_key_prevent_screen_capture) + "\"-preference.");
}
lockPref = findPreference(getString(R.string.pref_key_lock));
if (lockPref != null) {
if (!DeviceCredentialUtil.areCredentialsAvailable(requireContext())) {
lockPref.setVisible(false);
} else {
lockPref.setOnPreferenceChangeListener((preference, newValue) -> {
NotesApplication.setLockedPreference((Boolean) newValue);
return true;
});
}
} else {
Log.e(TAG, "Could not find \"" + getString(R.string.pref_key_lock) + "\"-preference.");
}
final var themePref = findPreference(getString(R.string.pref_key_theme));
assert themePref != null;
themePref.setOnPreferenceChangeListener((preference, newValue) -> {
NotesApplication.setAppTheme(DarkModeSetting.valueOf((String) newValue));
viewModel.resultCode$.setValue(Activity.RESULT_OK);
ActivityCompat.recreate(requireActivity());
return true;
});
wifiOnlyPref = findPreference(getString(R.string.pref_key_wifi_only));
assert wifiOnlyPref != null;
wifiOnlyPref.setOnPreferenceChangeListener((preference, newValue) -> {
Log.i(TAG, "syncOnWifiOnly: " + newValue);
return true;
});
backgroundSyncPref = findPreference(getString(R.string.pref_key_background_sync));
assert backgroundSyncPref != null;
backgroundSyncPref.setOnPreferenceChangeListener((preference, newValue) -> {
Log.i(TAG, "backgroundSync: " + newValue);
SyncWorker.update(requireContext(), (Boolean) newValue);
return true;
});
enableDirectEditorPref = findPreference(getString(R.string.pref_key_enable_direct_edit));
}
@Override
public void onStart() {
super.onStart();
final var context = requireContext();
@ColorInt final int color = BrandingUtil.readBrandMainColor(context);
applyBrand(color);
}
/**
* Change color for backgroundSyncPref as well
* https://github.com/stefan-niedermann/nextcloud-deck/issues/531
*
* @param color color of main brand
*/
@Override
public void applyBrand(int color) {
fontPref.applyBrand(color);
lockPref.applyBrand(color);
wifiOnlyPref.applyBrand(color);
gridViewPref.applyBrand(color);
showEcosystemAppBarPref.applyBrand(color);
preventScreenCapturePref.applyBrand(color);
backgroundSyncPref.applyBrand(color);
keepScreenOnPref.applyBrand(color);
enableDirectEditorPref.applyBrand(color);
}
}