Cherish:Status bar clock/date customizations [2/2]

Signed-off-by: Hưng Phan <phandinhhungvp2001@gmail.com>
This commit is contained in:
Lars Greiss
2018-11-22 10:43:14 -05:00
committed by Hưng Phan
parent 8392239373
commit eb7d3bf5c1
5 changed files with 453 additions and 0 deletions

View File

@@ -74,4 +74,71 @@
<item>2</item>
</string-array>
<!-- Status bar clock and date options -->
<string-array name="entries_status_bar_am_pm">
<item>@string/am_pm_clock_none</item>
<item>@string/am_pm_clock_small</item>
<item>@string/am_pm_clock_normal</item>
</string-array>
<string-array name="values_status_bar_am_pm" translatable="false">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
<string-array name="clock_date_display_entries">
<item>@string/clock_date_display_none</item>
<item>@string/clock_date_display_small</item>
<item>@string/clock_date_display_normal</item>
</string-array>
<string-array name="clock_date_display_values" translatable="false">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
<string-array name="clock_date_style_entries">
<item>@string/clock_date_style_normal</item>
<item>@string/clock_date_style_lowercase</item>
<item>@string/clock_date_style_uppercase</item>
</string-array>
<string-array name="clock_date_style_values" translatable="false">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
<string-array name="clock_date_format_entries_values">
<item>dd/MM/yy</item>
<item>MM/dd/yy</item>
<item>yyyy-MM-dd</item>
<item>yyyy-dd-MM</item>
<item>dd-MM-yyyy</item>
<item>MM-dd-yyyy</item>
<item>MMM dd</item>
<item>MMM dd, yyyy</item>
<item>MMMM dd, yyyy</item>
<item>EEE</item>
<item>EEE dd</item>
<item>EEE dd/MM</item>
<item>EEE MM/dd</item>
<item>EEE dd MMM</item>
<item>EEE MMM dd</item>
<item>EEE MMMM dd</item>
<item>EEEE dd/MM</item>
</string-array>
<string-array name="clock_date_position_entries" translatable="false">
<item>@string/clock_date_left</item>
<item>@string/clock_date_right</item>
</string-array>
<string-array name="clock_date_position_values" translatable="false">
<item>0</item>
<item>1</item>
</string-array>
</resources>

View File

@@ -231,4 +231,35 @@
<string name="quick_pulldown_right">Pulldown statusbar from right side</string>
<string name="quick_pulldown_left">Pulldown statusbar from left side</string>
<!-- Status bar clock and date options -->
<string name="status_bar_clock_title">Clock settings</string>
<string name="status_bar_clock">Clock &amp; Date</string>
<string name="clock_enabled">Enabled</string>
<string name="clock_disabled">Disabled</string>
<string name="status_bar_clock_style_reset">Reset color</string>
<string name="status_bar_show_clock_title">Show clock &amp; date</string>
<string name="status_bar_am_pm_title">AM/PM</string>
<string name="status_bar_am_pm_info">24-hour clock is enabled</string>
<string name="enable_clock_weekday">Day of the week</string>
<string name="enable_clock_weekday_summary">Abbreviated day of the week before the time</string>
<string name="dont_show">Don\'t show</string>
<string name="clock_date_display">Date</string>
<string name="clock_date_display_summary">Display custom date before the clock</string>
<string name="clock_date_style">Date style</string>
<string name="clock_date_format">Date format</string>
<string name="clock_date_string_edittext_title">Must be in DateFormat eg. MM/dd/yy</string>
<string name="clock_date_string_edittext_summary">Enter string</string>
<string name="am_pm_clock_normal">Normal font size</string>
<string name="am_pm_clock_small">Small font size</string>
<string name="am_pm_clock_none">Don\'t show</string>
<string name="clock_date_display_none">Don\'t show</string>
<string name="clock_date_display_small">Small font size</string>
<string name="clock_date_display_normal">Normal font size</string>
<string name="clock_date_style_normal">Normal</string>
<string name="clock_date_style_lowercase">Lowercase</string>
<string name="clock_date_style_uppercase">Uppercase</string>
<string name="clock_date_right">Right of time</string>
<string name="clock_date_left">Left of time</string>
<string name="clock_date_position">Date position</string>
</resources>

View File

@@ -30,6 +30,11 @@
android:key="statusbar_icons"
android:title="@string/sb_icons">
<Preference
android:key="status_bar_clock"
android:fragment="com.cherish.settings.fragments.ClockDateSettings"
android:title="@string/status_bar_clock_title" />
<com.cherish.settings.preferences.SystemSettingSwitchPreference
android:key="show_fourg_icon"
android:title="@string/show_fourg_icon_title"

View File

@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2018 AospExtended ROM 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.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings">
<SwitchPreference
android:key="status_bar_show_clock"
android:title="@string/status_bar_show_clock_title"
android:defaultValue="true" />
<ListPreference
android:key="status_bar_am_pm"
android:dialogTitle="@string/status_bar_am_pm_title"
android:title="@string/status_bar_am_pm_title"
android:entries="@array/entries_status_bar_am_pm"
android:entryValues="@array/values_status_bar_am_pm"
android:dependency="status_bar_show_clock" />
<ListPreference
android:key="clock_date_display"
android:title="@string/clock_date_display"
android:summary="@string/clock_date_display_summary"
android:entries="@array/clock_date_display_entries"
android:entryValues="@array/clock_date_display_values"
android:dependency="status_bar_show_clock" />
<ListPreference
android:key="clock_date_style"
android:title="@string/clock_date_style"
android:entries="@array/clock_date_style_entries"
android:entryValues="@array/clock_date_style_values"
android:dependency="status_bar_show_clock" />
<ListPreference
android:key="clock_date_format"
android:title="@string/clock_date_format"
android:entries="@array/clock_date_format_entries_values"
android:entryValues="@array/clock_date_format_entries_values"
android:dependency="status_bar_show_clock" />
<ListPreference
android:key="clock_date_position"
android:title="@string/clock_date_position"
android:entries="@array/clock_date_position_entries"
android:entryValues="@array/clock_date_position_values"
android:dependency="status_bar_show_clock" />
</PreferenceScreen>

View File

@@ -0,0 +1,290 @@
/*
* Copyright (C) 2020 The CherishOS 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 android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.SearchIndexableResource;
import androidx.preference.PreferenceCategory;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.SwitchPreference;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
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.settings.Utils;
import com.android.settingslib.search.Indexable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ClockDateSettings extends SettingsPreferenceFragment
implements Preference.OnPreferenceChangeListener, Indexable {
private static final String PREF_AM_PM_STYLE = "status_bar_am_pm";
private static final String PREF_CLOCK_DATE_DISPLAY = "clock_date_display";
private static final String PREF_CLOCK_DATE_STYLE = "clock_date_style";
private static final String PREF_CLOCK_DATE_FORMAT = "clock_date_format";
private static final String PREF_STATUS_BAR_CLOCK = "status_bar_show_clock";
private static final String PREF_CLOCK_DATE_POSITION = "clock_date_position";
public static final int CLOCK_DATE_STYLE_LOWERCASE = 1;
public static final int CLOCK_DATE_STYLE_UPPERCASE = 2;
private static final int CUSTOM_CLOCK_DATE_FORMAT_INDEX = 18;
private ListPreference mClockAmPmStyle;
private ListPreference mClockDateDisplay;
private ListPreference mClockDateStyle;
private ListPreference mClockDateFormat;
private ListPreference mClockDatePosition;
private SwitchPreference mStatusBarClock;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.clock_date_settings);
mClockAmPmStyle = (ListPreference) findPreference(PREF_AM_PM_STYLE);
mClockAmPmStyle.setOnPreferenceChangeListener(this);
mClockAmPmStyle.setValue(Integer.toString(Settings.System.getInt(getActivity()
.getContentResolver(), Settings.System.STATUSBAR_CLOCK_AM_PM_STYLE,
0)));
boolean is24hour = DateFormat.is24HourFormat(getActivity());
if (is24hour) {
mClockAmPmStyle.setSummary(R.string.status_bar_am_pm_info);
} else {
mClockAmPmStyle.setSummary(mClockAmPmStyle.getEntry());
}
mClockAmPmStyle.setEnabled(!is24hour);
mClockDateDisplay = (ListPreference) findPreference(PREF_CLOCK_DATE_DISPLAY);
mClockDateDisplay.setOnPreferenceChangeListener(this);
mClockDateDisplay.setValue(Integer.toString(Settings.System.getInt(getActivity()
.getContentResolver(), Settings.System.STATUSBAR_CLOCK_DATE_DISPLAY,
0)));
mClockDateDisplay.setSummary(mClockDateDisplay.getEntry());
mClockDateStyle = (ListPreference) findPreference(PREF_CLOCK_DATE_STYLE);
mClockDateStyle.setOnPreferenceChangeListener(this);
mClockDateStyle.setValue(Integer.toString(Settings.System.getInt(getActivity()
.getContentResolver(), Settings.System.STATUSBAR_CLOCK_DATE_STYLE,
0)));
mClockDateStyle.setSummary(mClockDateStyle.getEntry());
mClockDateFormat = (ListPreference) findPreference(PREF_CLOCK_DATE_FORMAT);
mClockDateFormat.setOnPreferenceChangeListener(this);
String value = Settings.System.getString(getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_DATE_FORMAT);
if (value == null || value.isEmpty()) {
value = "EEE";
}
int index = mClockDateFormat.findIndexOfValue((String) value);
if (index == -1) {
mClockDateFormat.setValueIndex(CUSTOM_CLOCK_DATE_FORMAT_INDEX);
} else {
mClockDateFormat.setValue(value);
}
parseClockDateFormats();
mStatusBarClock = (SwitchPreference) findPreference(PREF_STATUS_BAR_CLOCK);
mStatusBarClock.setChecked((Settings.System.getInt(
getActivity().getApplicationContext().getContentResolver(),
Settings.System.STATUSBAR_CLOCK, 1) == 1));
mStatusBarClock.setOnPreferenceChangeListener(this);
mClockDatePosition = (ListPreference) findPreference(PREF_CLOCK_DATE_POSITION);
mClockDatePosition.setOnPreferenceChangeListener(this);
mClockDatePosition.setValue(Integer.toString(Settings.System.getInt(getActivity()
.getContentResolver(), Settings.System.STATUSBAR_CLOCK_DATE_POSITION,
0)));
mClockDatePosition.setSummary(mClockDatePosition.getEntry());
boolean mClockDateToggle = Settings.System.getInt(getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_DATE_DISPLAY, 0) != 0;
if (!mClockDateToggle) {
mClockDateStyle.setEnabled(false);
mClockDateFormat.setEnabled(false);
mClockDatePosition.setEnabled(false);
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
AlertDialog dialog;
if (preference == mClockAmPmStyle) {
int val = Integer.parseInt((String) newValue);
int index = mClockAmPmStyle.findIndexOfValue((String) newValue);
Settings.System.putInt(getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_AM_PM_STYLE, val);
mClockAmPmStyle.setSummary(mClockAmPmStyle.getEntries()[index]);
return true;
} else if (preference == mClockDateDisplay) {
int val = Integer.parseInt((String) newValue);
int index = mClockDateDisplay.findIndexOfValue((String) newValue);
Settings.System.putInt(getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_DATE_DISPLAY, val);
mClockDateDisplay.setSummary(mClockDateDisplay.getEntries()[index]);
if (val == 0) {
mClockDateStyle.setEnabled(false);
mClockDateFormat.setEnabled(false);
mClockDatePosition.setEnabled(false);
} else {
mClockDateStyle.setEnabled(true);
mClockDateFormat.setEnabled(true);
mClockDatePosition.setEnabled(true);
}
return true;
} else if (preference == mClockDateStyle) {
int val = Integer.parseInt((String) newValue);
int index = mClockDateStyle.findIndexOfValue((String) newValue);
Settings.System.putInt(getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_DATE_STYLE, val);
mClockDateStyle.setSummary(mClockDateStyle.getEntries()[index]);
parseClockDateFormats();
return true;
} else if (preference == mStatusBarClock) {
Settings.System.putInt(getActivity().getApplicationContext().getContentResolver(),
Settings.System.STATUSBAR_CLOCK,
(Boolean) newValue ? 1 : 0);
return true;
} else if (preference == mClockDateFormat) {
int index = mClockDateFormat.findIndexOfValue((String) newValue);
if (index == CUSTOM_CLOCK_DATE_FORMAT_INDEX) {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle(R.string.clock_date_string_edittext_title);
alert.setMessage(R.string.clock_date_string_edittext_summary);
final EditText input = new EditText(getActivity());
String oldText = Settings.System.getString(
getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_DATE_FORMAT);
if (oldText != null) {
input.setText(oldText);
}
alert.setView(input);
alert.setPositiveButton(R.string.menu_save, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int whichButton) {
String value = input.getText().toString();
if (value.equals("")) {
return;
}
Settings.System.putString(getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_DATE_FORMAT, value);
return;
}
});
alert.setNegativeButton(R.string.menu_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int which) {
return;
}
});
dialog = alert.create();
dialog.show();
} else {
if ((String) newValue != null) {
Settings.System.putString(getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_DATE_FORMAT, (String) newValue);
}
}
return true;
} else if (preference == mClockDatePosition) {
int val = Integer.parseInt((String) newValue);
int index = mClockDatePosition.findIndexOfValue((String) newValue);
Settings.System.putInt(getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_DATE_POSITION, val);
mClockDatePosition.setSummary(mClockDatePosition.getEntries()[index]);
parseClockDateFormats();
return true;
}
return false;
}
private void parseClockDateFormats() {
String[] dateEntries = getResources().getStringArray(R.array.clock_date_format_entries_values);
CharSequence parsedDateEntries[];
parsedDateEntries = new String[dateEntries.length];
Date now = new Date();
int lastEntry = dateEntries.length - 1;
int dateFormat = Settings.System.getInt(getActivity()
.getContentResolver(), Settings.System.STATUSBAR_CLOCK_DATE_STYLE, 0);
for (int i = 0; i < dateEntries.length; i++) {
if (i == lastEntry) {
parsedDateEntries[i] = dateEntries[i];
} else {
String newDate;
CharSequence dateString = DateFormat.format(dateEntries[i], now);
if (dateFormat == CLOCK_DATE_STYLE_LOWERCASE) {
newDate = dateString.toString().toLowerCase();
} else if (dateFormat == CLOCK_DATE_STYLE_UPPERCASE) {
newDate = dateString.toString().toUpperCase();
} else {
newDate = dateString.toString();
}
parsedDateEntries[i] = newDate;
}
}
mClockDateFormat.setEntries(parsedDateEntries);
}
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.CHERISH_SETTINGS;
}
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
boolean enabled) {
final ArrayList<SearchIndexableResource> result = new ArrayList<>();
final SearchIndexableResource sir = new SearchIndexableResource(context);
sir.xmlResId = R.xml.clock_date_settings;
result.add(sir);
return result;
}
@Override
public List<String> getNonIndexableKeys(Context context) {
final List<String> keys = super.getNonIndexableKeys(context);
return keys;
}
};
}