主要是参考了息屏时间设置的Preference。
增加DateFormatController.java和DateFormatListPreference.java这2个文件。
//vendor\mediatek\proprietary\packages\apps\MtkSettings\src\com\android\settings\datetime\DateFormatController.java
public class DateFormatListPreference extends RestrictedListPreference{
private final CharSequence[] mInitialEntries;
private final CharSequence[] mInitialValues;
public DateFormatListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mInitialEntries = getEntries();
mInitialValues = getEntryValues();
}
}
intent发送前要调用addFlags(0x01000000),否则会报错Background execution not allowed:******************
//vendor\mediatek\proprietary\packages\apps\MtkSettings\src\com\android\settings\datetime\DateFormatController.java
public class DateFormatController extends AbstractPreferenceController implements
PreferenceControllerMixin, Preference.OnPreferenceChangeListener{
private static final String TAG = "DateFormatController";
private final String mDateFormatKey;
public DateFormatController(Context context, String key) {
super(context);
mDateFormatKey = key;
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return mDateFormatKey;
}
@Override
public void updateState(Preference preference) {
final DateFormatListPreference dateFormatListPreference = (DateFormatListPreference) preference;
final String value = Settings.System.getString(mContext.getContentResolver(), DATE_FORMAT);
dateFormatListPreference.setValue(value);
preference.setSummary(getDate(value));
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String value = (String) newValue;
Settings.System.putString(mContext.getContentResolver(), DATE_FORMAT, value);
preference.setSummary(getDate(value));
sendDateFormatUpdateBroadcast();
return true;
}
private String getDate(String dateFormat){
Date date = new Date(System.currentTimeMillis());
String dateString = new SimpleDateFormat(dateFormat).format(date);
return dateString;
}
private void sendDateFormatUpdateBroadcast() {
Intent dateFormatUpdate = new Intent("android.intent.action.DATE_FORMAT_UPDATE");
dateFormatUpdate.addFlags(0x01000000);
mContext.sendBroadcast(dateFormatUpdate);
}
}
DateTimeSettings中增加控制部分。private static final String KEY_DATE_FORMAT = "date_format";和controllers.add(new DateFormatController(context, KEY_DATE_FORMAT));
具体如下。
public class DateTimeSettings extends DashboardFragment implements
TimePreferenceController.TimePreferenceHost, DatePreferenceController.DatePreferenceHost,
AutoTimeExtPreferenceController.GPSPreferenceHost, DialogInterface.OnCancelListener {
private static final String KEY_DATE_FORMAT = "date_format";
................
@Override
protected List getPreferenceControllers(Context context) {
................
controllers.add(new DateFormatController(context, KEY_DATE_FORMAT));
................
}
}
date_time_prefs中增加DateFormatListPreference。
//vendor\mediatek\proprietary\packages\apps\MtkSettings\res\xml\date_time_prefs.xml
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
android:title="@string/date_and_time"
settings:keywords="@string/keywords_date_and_time">
............
android:key="date_format"
android:title="@string/title_date_format"
android:summary="@string/summary_placeholder"
android:entries="@array/date_format_entries"
android:entryValues="@array/date_format_values" />
android:key="24 hour"
android:title="@string/date_time_24hour" />
定义相关string和array 。
//vendor\mediatek\proprietary\packages\apps\MtkSettings\res\values\arrays.xml
year-month-day
month-day-year
day-month-year
yyyy-MM-dd
MM-dd-yyyy
dd-MM-yyyy
//vendor\mediatek\proprietary\packages\apps\MtkSettings\res\values\strings.xml
date format
//vendor\mediatek\proprietary\packages\apps\MtkSettings\res\values-zh-rCN\arrays.xml
年-月-日
月-日-年
日-月-年
//vendor\mediatek\proprietary\packages\apps\MtkSettings\res\values-zh-rCN\strings.xml
日期格式
SettingsProvider中增加Settings.System.DATE_FORMAT默认设置。
//vendor\mediatek\proprietary\packages\apps\SettingsProvider\src\com\android\providers\settings\DatabaseHelper.java
private void loadSystemSettings(SQLiteDatabase db) {
SQLiteStatement stmt = null;
try {
stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
+ " VALUES(?,?);");
loadStringSetting(stmt,Settings.System.DATE_FORMAT,R.string.def_date_format);
....................
}
//vendor\mediatek\proprietary\packages\apps\SettingsProvider\res\values\defaults.xml
yyyy-MM-dd
以下为桌面时钟布局修改部分。
AndroidManifest.xml中增加protected-broadcast定义,否则发送广播后会报错Sending non-protected broadcast,无法接受到广播。
增加
//frameworks/base/core/res/AndroidManifest.xml
DigitalAppWidgetProvider中有3处修改。
增加 private static final String ACTION_DATE_FORMAT_UPDATE = "android.intent.action.DATE_FORMAT_UPDATE";
增加当收到广播时更新UI。
case ACTION_DATE_FORMAT_UPDATE:
Log.d(TAG,"ACTION_DATE_FORMAT_UPDATE");
for (int widgetId : widgetIds) {
relayoutWidget(context, wm, widgetId, wm.getAppWidgetOptions(widgetId));
}
break;
3.修改getDateFormat(Context context),修改中文和英文语言模式下的日期格式 。
增加
if (locale.getCountry().equals("CN")){
return Settings.System.getString(context.getContentResolver(), DATE_FORMAT) + " EEE";
}else if (locale.getCountry().equals("US")){
return "EEE, " + Settings.System.getString(context.getContentResolver(), DATE_FORMAT);
}
修改后如下。
//vendor/mediatek/proprietary/packages/apps/DeskClock/src/com/android/alarmclock/DigitalAppWidgetProvider.java
public class DigitalAppWidgetProvider extends AppWidgetProvider {
private static final String ACTION_DATE_FORMAT_UPDATE = "android.intent.action.DATE_FORMAT_UPDATE";
@Override
public void onReceive(@NonNull Context context, @NonNull Intent intent) {
...........
final String action = intent.getAction();
switch (action) {
case ACTION_NEXT_ALARM_CLOCK_CHANGED:
case ACTION_DATE_CHANGED:
case ACTION_LOCALE_CHANGED:
case ACTION_SCREEN_ON:
case ACTION_TIME_CHANGED:
case ACTION_TIMEZONE_CHANGED:
case ACTION_ALARM_CHANGED:
case ACTION_ON_DAY_CHANGE:
case ACTION_WORLD_CITIES_CHANGED:
for (int widgetId : widgetIds) {
relayoutWidget(context, wm, widgetId, wm.getAppWidgetOptions(widgetId));
}
case ACTION_DATE_FORMAT_UPDATE:
Log.d(TAG,"ACTION_DATE_FORMAT_UPDATE");
for (int widgetId : widgetIds) {
relayoutWidget(context, wm, widgetId, wm.getAppWidgetOptions(widgetId));
}
break;
}
...........
}
/**
* @return the locale-specific date pattern
*/
private static String getDateFormat(Context context) {
final Locale locale = Locale.getDefault();
if (locale.getCountry().equals("CN")){
return Settings.System.getString(context.getContentResolver(), DATE_FORMAT) + " EEE";
}else if (locale.getCountry().equals("US")){
return "EEE, " + Settings.System.getString(context.getContentResolver(), DATE_FORMAT);
}
final String skeleton = context.getString(R.string.abbrev_wday_month_day_no_year);
return DateFormat.getBestDateTimePattern(locale, skeleton);
}
}
receiver增加定义。
//vendor/mediatek/proprietary/packages/apps/DeskClock/AndroidManifest.xml
android:name="com.android.alarmclock.DigitalAppWidgetProvider"
android:label="@string/digital_gadget">
android:name="android.appwidget.provider"
android:resource="@xml/digital_appwidget" />