Cherish: Statusbar clock date customization (2/2)

This commit is contained in:
Lars Greiss
2019-11-23 01:25:17 +05:30
committed by Hưng Phan
parent 5da459db7d
commit 63157ec6d9
7 changed files with 467 additions and 6 deletions

View File

@@ -92,5 +92,68 @@
<item>7</item>
<item>8</item>
</string-array>
<!-- Statusbar Clock -->
<string-array name="status_bar_clock_style_entries" translatable="false">
<item>@string/status_bar_clock_style_left</item>
<item>@string/status_bar_clock_style_center</item>
<item>@string/status_bar_clock_style_right</item>
</string-array>
<string-array name="status_bar_clock_style_values" translatable="false">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
<string-array name="status_bar_am_pm_entries" translatable="false">
<item>@string/status_bar_am_pm_hidden</item>
<item>@string/status_bar_am_pm_small</item>
<item>@string/status_bar_am_pm_normal</item>
</string-array>
<string-array name="status_bar_am_pm_values" 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>
<item>EEEE MM/dd</item>
<item>@string/clock_date_format_custom</item>
</string-array>
</resources>

View File

@@ -112,5 +112,37 @@
<string name="status_bar_icons_title">Status bar icons</string>
<string name="status_bar_system_icons_title">System icons</string>
<string name="status_bar_system_icons_summary">Control which status bar icons are shown</string>
<!-- Statusbar Clock -->
<string name="statusbar_items_title">Statusbar items</string>
<string name="clock_category_title">Clock</string>
<string name="date_category_title">Date</string>
<string name="status_bar_clock_title">Clock &amp; Date</string>
<string name="status_bar_clock_summary">Customize clock and date in statusbar</string>
<string name="status_bar_clock_style_title">Clock position</string>
<string name="status_bar_clock_style_right">Right</string>
<string name="status_bar_clock_style_center">Center</string>
<string name="status_bar_clock_style_left">Left</string>
<string name="status_bar_am_pm_title">AM/PM style</string>
<string name="status_bar_am_pm_info">24-hour clock is enabled</string>
<string name="status_bar_am_pm_normal">Normal</string>
<string name="status_bar_am_pm_small">Small</string>
<string name="status_bar_am_pm_hidden">Hidden</string>
<string name="clock_seconds_display">Show seconds</string>
<string name="clock_seconds_display_summary">Display seconds in the clock</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_display_none">Hidden</string>
<string name="clock_date_display_small">Small</string>
<string name="clock_date_display_normal">Normal</string>
<string name="clock_date_style">Date style</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_format_custom">Custom java format</string>
<string name="clock_date_format">Date format</string>
<string name="clock_date_format_summary">%1$s</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>
</resources>

View File

@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2017-2018 The Dirty Unicorns 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"
android:title="@string/status_bar_clock_title" >
<PreferenceCategory
android:key="clock_category"
android:title="@string/clock_category_title">
<ListPreference
android:key="statusbar_clock_style"
android:title="@string/status_bar_clock_style_title"
android:entries="@array/status_bar_clock_style_entries"
android:entryValues="@array/status_bar_clock_style_values" />
<ListPreference
android:key="status_bar_am_pm"
android:title="@string/status_bar_am_pm_title"
android:entries="@array/status_bar_am_pm_entries"
android:entryValues="@array/status_bar_am_pm_values" />
<com.cherish.settings.preferences.SystemSettingSwitchPreference
android:key="status_bar_clock_seconds"
android:title="@string/clock_seconds_display"
android:summary="@string/clock_seconds_display_summary"
android:defaultValue="false" />
</PreferenceCategory>
<PreferenceCategory
android:key="date_category"
android:title="@string/date_category_title">
<ListPreference
android:key="clock_date_display"
android:title="@string/clock_date_display"
android:entries="@array/clock_date_display_entries"
android:entryValues="@array/clock_date_display_values" />
<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="clock_date_display" />
<ListPreference
android:key="clock_date_format"
android:title="@string/clock_date_format"
android:summary="@string/clock_date_format_summary"
android:entries="@array/clock_date_format_entries_values"
android:entryValues="@array/clock_date_format_entries_values"
android:dependency="clock_date_display" />
</PreferenceCategory>
</PreferenceScreen>

View File

@@ -22,7 +22,17 @@
android:title="@string/status_bar_icons_title">
<PreferenceScreen
android:key="status_bar_icons"
android:title="@string/status_bar_system_icons_title"
android:title="@string/status_bar_system_icons_title">
<com.cherish.settings.preferences.SystemSettingMasterSwitchPreference
android:key="status_bar_clock"
android:fragment="com.cherish.settings.fragments.ClockSettings"
android:title="@string/status_bar_clock_title"
android:summary="@string/status_bar_clock_summary"
android:defaultValue="true" />
<Preference
android:title="@string/statusbar_items_title"
android:icon="@drawable/ic_shortcuts">
android:summary="@string/status_bar_system_icons_summary">
<intent android:action="com.android.settings.action.STATUS_BAR_TUNER" />

View File

@@ -0,0 +1,274 @@
/*
* Copyright (C) 2017-2018 The Dirty Unicorns 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.content.Intent;
import android.os.Bundle;
import android.os.UserHandle;
import androidx.preference.ListPreference;
import androidx.preference.SwitchPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import androidx.preference.Preference.OnPreferenceChangeListener;
import android.provider.Settings;
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.SettingsPreferenceFragment;
import com.android.settings.Utils;
import com.cherish.settings.preferences.SystemSettingSwitchPreference;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
public class ClockSettings extends SettingsPreferenceFragment
implements Preference.OnPreferenceChangeListener {
private static final String STATUS_BAR_CLOCK_SECONDS = "status_bar_clock_seconds";
private static final String STATUS_BAR_CLOCK_STYLE = "statusbar_clock_style";
private static final String STATUS_BAR_AM_PM = "status_bar_am_pm";
private static final String STATUS_BAR_CLOCK_DATE_DISPLAY = "clock_date_display";
private static final String STATUS_BAR_CLOCK_DATE_STYLE = "clock_date_style";
private static final String STATUS_BAR_CLOCK_DATE_FORMAT = "clock_date_format";
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 SystemSettingSwitchPreference mStatusBarSecondsShow;
private ListPreference mStatusBarClock;
private ListPreference mStatusBarAmPm;
private ListPreference mClockDateDisplay;
private ListPreference mClockDateStyle;
private ListPreference mClockDateFormat;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.cherish_settings_clock);
PreferenceScreen prefSet = getPreferenceScreen();
ContentResolver resolver = getActivity().getContentResolver();
mStatusBarSecondsShow = (SystemSettingSwitchPreference) findPreference(STATUS_BAR_CLOCK_SECONDS);
mStatusBarClock = (ListPreference) findPreference(STATUS_BAR_CLOCK_STYLE);
mStatusBarAmPm = (ListPreference) findPreference(STATUS_BAR_AM_PM);
mClockDateDisplay = (ListPreference) findPreference(STATUS_BAR_CLOCK_DATE_DISPLAY);
mClockDateStyle = (ListPreference) findPreference(STATUS_BAR_CLOCK_DATE_STYLE);
mStatusBarSecondsShow.setChecked((Settings.System.getInt(resolver,
Settings.System.STATUS_BAR_CLOCK_SECONDS, 0) == 1));
mStatusBarSecondsShow.setOnPreferenceChangeListener(this);
int clockStyle = Settings.System.getInt(resolver,
Settings.System.STATUSBAR_CLOCK_STYLE, 0);
mStatusBarClock.setValue(String.valueOf(clockStyle));
mStatusBarClock.setSummary(mStatusBarClock.getEntry());
mStatusBarClock.setOnPreferenceChangeListener(this);
if (DateFormat.is24HourFormat(getActivity())) {
mStatusBarAmPm.setEnabled(false);
mStatusBarAmPm.setSummary(R.string.status_bar_am_pm_info);
} else {
int statusBarAmPm = Settings.System.getInt(resolver,
Settings.System.STATUSBAR_CLOCK_AM_PM_STYLE, 2);
mStatusBarAmPm.setValue(String.valueOf(statusBarAmPm));
mStatusBarAmPm.setSummary(mStatusBarAmPm.getEntry());
mStatusBarAmPm.setOnPreferenceChangeListener(this);
}
int clockDateDisplay = Settings.System.getInt(resolver,
Settings.System.STATUSBAR_CLOCK_DATE_DISPLAY, 0);
mClockDateDisplay.setValue(String.valueOf(clockDateDisplay));
mClockDateDisplay.setSummary(mClockDateDisplay.getEntry());
mClockDateDisplay.setOnPreferenceChangeListener(this);
int clockDateStyle = Settings.System.getInt(resolver,
Settings.System.STATUSBAR_CLOCK_DATE_STYLE, 0);
mClockDateStyle.setValue(String.valueOf(clockDateStyle));
mClockDateStyle.setSummary(mClockDateStyle.getEntry());
mClockDateStyle.setOnPreferenceChangeListener(this);
mClockDateFormat = (ListPreference) findPreference(STATUS_BAR_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();
setDateOptions();
}
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.CHERISH_SETTINGS;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
AlertDialog dialog;
ContentResolver resolver = getActivity().getContentResolver();
if (preference == mStatusBarSecondsShow) {
boolean value = (Boolean) newValue;
Settings.System.putInt(getActivity().getContentResolver(),
Settings.System.STATUS_BAR_CLOCK_SECONDS, value ? 1 : 0);
return true;
} else if (preference == mStatusBarClock) {
int clockStyle = Integer.parseInt((String) newValue);
int index = mStatusBarClock.findIndexOfValue((String) newValue);
Settings.System.putInt(getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_STYLE, clockStyle);
mStatusBarClock.setSummary(mStatusBarClock.getEntries()[index]);
return true;
} else if (preference == mStatusBarAmPm) {
int statusBarAmPm = Integer.valueOf((String) newValue);
int index = mStatusBarAmPm.findIndexOfValue((String) newValue);
Settings.System.putInt(getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_AM_PM_STYLE, statusBarAmPm);
mStatusBarAmPm.setSummary(mStatusBarAmPm.getEntries()[index]);
return true;
} else if (preference == mClockDateDisplay) {
int clockDateDisplay = Integer.valueOf((String) newValue);
int index = mClockDateDisplay.findIndexOfValue((String) newValue);
Settings.System.putInt(getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_DATE_DISPLAY, clockDateDisplay);
mClockDateDisplay.setSummary(mClockDateDisplay.getEntries()[index]);
setDateOptions();
return true;
} else if (preference == mClockDateStyle) {
int clockDateStyle = Integer.valueOf((String) newValue);
int index = mClockDateStyle.findIndexOfValue((String) newValue);
Settings.System.putInt(getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_DATE_STYLE, clockDateStyle);
mClockDateStyle.setSummary(mClockDateStyle.getEntries()[index]);
parseClockDateFormats();
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;
}
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);
}
private void setDateOptions() {
int enableDateOptions = Settings.System.getInt(getActivity().getContentResolver(),
Settings.System.STATUSBAR_CLOCK_DATE_DISPLAY, 0);
if (enableDateOptions == 0) {
mClockDateStyle.setEnabled(false);
mClockDateFormat.setEnabled(false);
} else {
mClockDateStyle.setEnabled(true);
mClockDateFormat.setEnabled(true);
}
}
}

View File

@@ -43,12 +43,10 @@ public class NavbarSettings extends SettingsPreferenceFragment implements OnPref
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
addPreferencesFromResource(R.xml.cherish_settings_navigation);
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
return false;
}

View File

@@ -25,6 +25,9 @@ import android.text.TextUtils;
import android.view.View;
import com.android.settings.SettingsPreferenceFragment;
import com.cherish.settings.preferences.CustomSeekBarPreference;
import com.cherish.settings.preferences.SystemSettingSwitchPreference;
import com.cherish.settings.preferences.SystemSettingMasterSwitchPreference;
import com.android.settings.Utils;
import android.util.Log;
@@ -37,19 +40,34 @@ import java.util.Collections;
public class StatusBarSettings extends SettingsPreferenceFragment implements
OnPreferenceChangeListener {
private static final String STATUS_BAR_CLOCK = "status_bar_clock";
private SystemSettingMasterSwitchPreference mStatusBarClockShow;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
addPreferencesFromResource(R.xml.cherish_settings_statusbar);
ContentResolver resolver = getActivity().getContentResolver();
PreferenceScreen prefSet = getPreferenceScreen();
mStatusBarClockShow = (SystemSettingMasterSwitchPreference) findPreference(STATUS_BAR_CLOCK);
mStatusBarClockShow.setChecked((Settings.System.getInt(resolver,
Settings.System.STATUS_BAR_CLOCK, 1) == 1));
mStatusBarClockShow.setOnPreferenceChangeListener(this);
}
@Override
public boolean onPreferenceChange(Preference preference, Object objValue) {
if (preference == mStatusBarClockShow) {
boolean value = (Boolean) objValue;
Settings.System.putInt(getActivity().getContentResolver(),
Settings.System.STATUS_BAR_CLOCK, value ? 1 : 0);
return true;
}
return false;
}
@@ -58,4 +76,4 @@ public class StatusBarSettings extends SettingsPreferenceFragment implements
return MetricsProto.MetricsEvent.CHERISH_SETTINGS;
}
}
}