Cherish:[SQUASH]Allow scheduling always on display and Add support for mixed time & sun modes [2/2]
Signed-off-by: Hưng Phan <phandinhhungvp2001@gmail.com>
This commit is contained in:
198
src/com/cherish/settings/fragments/AODSchedule.java
Normal file
198
src/com/cherish/settings/fragments/AODSchedule.java
Normal file
@@ -0,0 +1,198 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Yet Another AOSP Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.cherish.settings.fragments;
|
||||
|
||||
import static com.cherish.settings.fragments.LockScreenSettings.MODE_DISABLED;
|
||||
import static com.cherish.settings.fragments.LockScreenSettings.MODE_NIGHT;
|
||||
import static com.cherish.settings.fragments.LockScreenSettings.MODE_TIME;
|
||||
import static com.cherish.settings.fragments.LockScreenSettings.MODE_MIXED_SUNSET;
|
||||
import static com.cherish.settings.fragments.LockScreenSettings.MODE_MIXED_SUNRISE;
|
||||
|
||||
import android.app.TimePickerDialog;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.provider.Settings;
|
||||
import android.text.format.DateFormat;
|
||||
import android.widget.TimePicker;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settings.SettingsPreferenceFragment;
|
||||
import com.android.settingslib.search.SearchIndexable;
|
||||
|
||||
import com.cherish.settings.preferences.SecureSettingListPreference;
|
||||
|
||||
@SearchIndexable
|
||||
public class AODSchedule extends SettingsPreferenceFragment implements
|
||||
Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener {
|
||||
|
||||
private static final String MODE_KEY = "doze_always_on_auto_mode";
|
||||
private static final String SINCE_PREF_KEY = "doze_always_on_auto_since";
|
||||
private static final String TILL_PREF_KEY = "doze_always_on_auto_till";
|
||||
|
||||
private SecureSettingListPreference mModePref;
|
||||
private Preference mSincePref;
|
||||
private Preference mTillPref;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
addPreferencesFromResource(R.xml.cherish_settings_aod_schedule);
|
||||
PreferenceScreen screen = getPreferenceScreen();
|
||||
ContentResolver resolver = getActivity().getContentResolver();
|
||||
|
||||
mSincePref = findPreference(SINCE_PREF_KEY);
|
||||
mSincePref.setOnPreferenceClickListener(this);
|
||||
mTillPref = findPreference(TILL_PREF_KEY);
|
||||
mTillPref.setOnPreferenceClickListener(this);
|
||||
|
||||
int mode = Settings.Secure.getIntForUser(resolver,
|
||||
MODE_KEY, MODE_DISABLED, UserHandle.USER_CURRENT);
|
||||
mModePref = (SecureSettingListPreference) findPreference(MODE_KEY);
|
||||
mModePref.setValue(String.valueOf(mode));
|
||||
mModePref.setSummary(mModePref.getEntry());
|
||||
mModePref.setOnPreferenceChangeListener(this);
|
||||
|
||||
updateTimeEnablement(mode);
|
||||
updateTimeSummary(mode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object objValue) {
|
||||
int value = Integer.valueOf((String) objValue);
|
||||
int index = mModePref.findIndexOfValue((String) objValue);
|
||||
mModePref.setSummary(mModePref.getEntries()[index]);
|
||||
Settings.Secure.putIntForUser(getActivity().getContentResolver(),
|
||||
MODE_KEY, value, UserHandle.USER_CURRENT);
|
||||
updateTimeEnablement(value);
|
||||
updateTimeSummary(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
String[] times = getCustomTimeSetting();
|
||||
boolean isSince = preference == mSincePref;
|
||||
int hour, minute; hour = minute = 0;
|
||||
TimePickerDialog.OnTimeSetListener listener = new TimePickerDialog.OnTimeSetListener() {
|
||||
@Override
|
||||
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
|
||||
updateTimeSetting(isSince, hourOfDay, minute);
|
||||
}
|
||||
};
|
||||
if (isSince) {
|
||||
String[] sinceValues = times[0].split(":", 0);
|
||||
hour = Integer.parseInt(sinceValues[0]);
|
||||
minute = Integer.parseInt(sinceValues[1]);
|
||||
} else {
|
||||
String[] tillValues = times[1].split(":", 0);
|
||||
hour = Integer.parseInt(tillValues[0]);
|
||||
minute = Integer.parseInt(tillValues[1]);
|
||||
}
|
||||
TimePickerDialog dialog = new TimePickerDialog(getContext(), listener,
|
||||
hour, minute, DateFormat.is24HourFormat(getContext()));
|
||||
dialog.show();
|
||||
return true;
|
||||
}
|
||||
|
||||
private String[] getCustomTimeSetting() {
|
||||
String value = Settings.Secure.getStringForUser(getActivity().getContentResolver(),
|
||||
Settings.Secure.DOZE_ALWAYS_ON_AUTO_TIME, UserHandle.USER_CURRENT);
|
||||
if (value == null || value.equals("")) value = "20:00,07:00";
|
||||
return value.split(",", 0);
|
||||
}
|
||||
|
||||
private void updateTimeEnablement(int mode) {
|
||||
mSincePref.setEnabled(mode == MODE_TIME || mode == MODE_MIXED_SUNRISE);
|
||||
mTillPref.setEnabled(mode == MODE_TIME || mode == MODE_MIXED_SUNSET);
|
||||
}
|
||||
|
||||
private void updateTimeSummary(int mode) {
|
||||
updateTimeSummary(getCustomTimeSetting(), mode);
|
||||
}
|
||||
|
||||
private void updateTimeSummary(String[] times, int mode) {
|
||||
if (mode == MODE_DISABLED) {
|
||||
mSincePref.setSummary("-");
|
||||
mTillPref.setSummary("-");
|
||||
return;
|
||||
}
|
||||
if (mode == MODE_NIGHT) {
|
||||
mSincePref.setSummary(R.string.always_on_display_schedule_sunset);
|
||||
mTillPref.setSummary(R.string.always_on_display_schedule_sunrise);
|
||||
return;
|
||||
}
|
||||
if (mode == MODE_MIXED_SUNSET) {
|
||||
mSincePref.setSummary(R.string.always_on_display_schedule_sunset);
|
||||
} else if (mode == MODE_MIXED_SUNRISE) {
|
||||
mTillPref.setSummary(R.string.always_on_display_schedule_sunrise);
|
||||
}
|
||||
if (DateFormat.is24HourFormat(getContext())) {
|
||||
if (mode != MODE_MIXED_SUNSET) mSincePref.setSummary(times[0]);
|
||||
if (mode != MODE_MIXED_SUNRISE) mTillPref.setSummary(times[1]);
|
||||
return;
|
||||
}
|
||||
String[] sinceValues = times[0].split(":", 0);
|
||||
String[] tillValues = times[1].split(":", 0);
|
||||
int sinceHour = Integer.parseInt(sinceValues[0]);
|
||||
int tillHour = Integer.parseInt(tillValues[0]);
|
||||
String sinceSummary = "";
|
||||
String tillSummary = "";
|
||||
if (sinceHour > 12) {
|
||||
sinceHour -= 12;
|
||||
sinceSummary += String.valueOf(sinceHour) + ":" + sinceValues[1] + " PM";
|
||||
} else {
|
||||
sinceSummary = times[0].substring(1) + " AM";
|
||||
}
|
||||
if (tillHour > 12) {
|
||||
tillHour -= 12;
|
||||
tillSummary += String.valueOf(tillHour) + ":" + tillValues[1] + " PM";
|
||||
} else {
|
||||
tillSummary = times[0].substring(1) + " AM";
|
||||
}
|
||||
if (mode != MODE_MIXED_SUNSET) mSincePref.setSummary(sinceSummary);
|
||||
if (mode != MODE_MIXED_SUNRISE) mTillPref.setSummary(tillSummary);
|
||||
}
|
||||
|
||||
private void updateTimeSetting(boolean since, int hour, int minute) {
|
||||
String[] times = getCustomTimeSetting();
|
||||
String nHour = "";
|
||||
String nMinute = "";
|
||||
if (hour < 10) nHour += "0";
|
||||
if (minute < 10) nMinute += "0";
|
||||
nHour += String.valueOf(hour);
|
||||
nMinute += String.valueOf(minute);
|
||||
times[since ? 0 : 1] = nHour + ":" + nMinute;
|
||||
Settings.Secure.putStringForUser(getActivity().getContentResolver(),
|
||||
Settings.Secure.DOZE_ALWAYS_ON_AUTO_TIME,
|
||||
times[0] + "," + times[1], UserHandle.USER_CURRENT);
|
||||
updateTimeSummary(times, Integer.parseInt(mModePref.getValue()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsProto.MetricsEvent.CHERISH_SETTINGS;
|
||||
}
|
||||
|
||||
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
new BaseSearchIndexProvider(R.xml.cherish_settings_aod_schedule);
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import android.content.res.Resources;
|
||||
import android.hardware.fingerprint.FingerprintManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import androidx.preference.SwitchPreference;
|
||||
import androidx.preference.ListPreference;
|
||||
import androidx.preference.Preference;
|
||||
@@ -57,6 +58,13 @@ public class LockScreenSettings extends SettingsPreferenceFragment implements
|
||||
private static final String LOCKOWNER_FONT_SIZE = "lockowner_font_size";
|
||||
private static final String KEY_FOD_RECOGNIZING_ANIMATION = "fod_recognizing_animation";
|
||||
private static final String KEY_FOD_RECOGNIZING_ANIMATION_LIST = "fod_recognizing_animation_list";
|
||||
private static final String AOD_SCHEDULE_KEY = "always_on_display_schedule";
|
||||
|
||||
static final int MODE_DISABLED = 0;
|
||||
static final int MODE_NIGHT = 1;
|
||||
static final int MODE_TIME = 2;
|
||||
static final int MODE_MIXED_SUNSET = 3;
|
||||
static final int MODE_MIXED_SUNRISE = 4;
|
||||
|
||||
private ListPreference mLockClockFonts;
|
||||
private ListPreference mLockDateFonts;
|
||||
@@ -66,6 +74,7 @@ public class LockScreenSettings extends SettingsPreferenceFragment implements
|
||||
private CustomSeekBarPreference mDateFontSize;
|
||||
private CustomSeekBarPreference mOwnerInfoFontSize;
|
||||
private CustomSeekBarPreference mCustomTextClockFontSize;
|
||||
private Preference mAODPref;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
@@ -135,6 +144,39 @@ public class LockScreenSettings extends SettingsPreferenceFragment implements
|
||||
prefScreen.removePreference(mFODSwitchPref);
|
||||
prefScreen.removePreference(mFODListViewPref);
|
||||
}
|
||||
|
||||
mAODPref = findPreference(AOD_SCHEDULE_KEY);
|
||||
updateAlwaysOnSummary();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
updateAlwaysOnSummary();
|
||||
}
|
||||
|
||||
private void updateAlwaysOnSummary() {
|
||||
if (mAODPref == null) return;
|
||||
int mode = Settings.Secure.getIntForUser(getActivity().getContentResolver(),
|
||||
Settings.Secure.DOZE_ALWAYS_ON_AUTO_MODE, MODE_DISABLED, UserHandle.USER_CURRENT);
|
||||
switch (mode) {
|
||||
default:
|
||||
case MODE_DISABLED:
|
||||
mAODPref.setSummary(R.string.disabled);
|
||||
break;
|
||||
case MODE_NIGHT:
|
||||
mAODPref.setSummary(R.string.night_display_auto_mode_twilight);
|
||||
break;
|
||||
case MODE_TIME:
|
||||
mAODPref.setSummary(R.string.night_display_auto_mode_custom);
|
||||
break;
|
||||
case MODE_MIXED_SUNSET:
|
||||
mAODPref.setSummary(R.string.always_on_display_schedule_mixed_sunset);
|
||||
break;
|
||||
case MODE_MIXED_SUNRISE:
|
||||
mAODPref.setSummary(R.string.always_on_display_schedule_mixed_sunrise);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
|
||||
Reference in New Issue
Block a user