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
|
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.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;
import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets;
public class PreferencesFragment extends PreferenceFragmentCompat implements Branded {
private static final String TAG = PreferencesFragment.class.getSimpleName();
private BrandedSwitchPreference fontPref;
private BrandedSwitchPreference lockPref;
private BrandedSwitchPreference wifiOnlyPref;
private BrandedSwitchPreference brandingPref;
private BrandedSwitchPreference gridViewPref;
private BrandedSwitchPreference preventScreenCapturePref;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences);
fontPref = findPreference(getString(R.string.pref_key_font));
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);
requireActivity().setResult(Activity.RESULT_OK);
NotesApplication.updateGridViewEnabled(gridView);
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 ListPreference themePref = findPreference(getString(R.string.pref_key_theme));
assert themePref != null;
themePref.setOnPreferenceChangeListener((preference, newValue) -> {
NotesApplication.setAppTheme(DarkModeSetting.valueOf((String) newValue));
requireActivity().setResult(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;
});
final ListPreference syncPref = findPreference(getString(R.string.pref_key_background_sync));
assert syncPref != null;
syncPref.setOnPreferenceChangeListener((preference, newValue) -> {
Log.i(TAG, "syncPref: " + preference + " - newValue: " + newValue);
SyncWorker.update(requireContext(), newValue.toString());
return true;
});
}
@Override
public void onStart() {
super.onStart();
@Nullable Context context = getContext();
if (context != null) {
@ColorInt final int mainColor = BrandingUtil.readBrandMainColor(context);
@ColorInt final int textColor = BrandingUtil.readBrandTextColor(context);
applyBrand(mainColor, textColor);
}
}
@Override
public void applyBrand(int mainColor, int textColor) {
fontPref.applyBrand(mainColor, textColor);
lockPref.applyBrand(mainColor, textColor);
wifiOnlyPref.applyBrand(mainColor, textColor);
brandingPref.applyBrand(mainColor, textColor);
gridViewPref.applyBrand(mainColor, textColor);
preventScreenCapturePref.applyBrand(mainColor, textColor);
}
}
|