背景:时间格式有12//24小时制,系统设置时间格式之后,状态栏和锁屏的时间显示也会相应的发生变化,但是现在发现一个问题:当我切换到多用户设置时间格式的时候发现,状态栏的时间格式变化正常,但是锁屏时间格式,只会随着主用户的时间格式进行变化。基于这个现象,我就去朝看锁屏时间格式设置的相关流程代码。
使用的时间格式,有个对应的字段值进行存储,存储在Settings.System.TIME_12_24字段里,如果锁屏需要刷新时间格式,大概会监听这个字段,所以我根据这个字段查看锁屏相关代码对它的监听。
1.frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java
mTimeFormatChangeObserver = new ContentObserver(mHandler) {
@Override
public void onChange(boolean selfChange) {
mHandler.sendMessage(mHandler.obtainMessage(
MSG_TIME_FORMAT_UPDATE,
Settings.System.getString(
mContext.getContentResolver(),
Settings.System.TIME_12_24)));
}
};
mContext.getContentResolver().registerContentObserver(
Settings.System.getUriFor(Settings.System.TIME_12_24),
false, mTimeFormatChangeObserver, UserHandle.USER_ALL);
根据MSG_TIME_FORMAT_UPDATE,查看到:
/**
* Handle (@line #MSG_TIME_FORMAT_UPDATE}
*
* @param timeFormat "12" for 12-hour format, "24" for 24-hour format
*/
private void handleTimeFormatUpdate(String timeFormat) {
Assert.isMainThread();
if (DEBUG || true) Log.d(TAG, "handleTimeFormatUpdate timeFormat=" + timeFormat);
for (int i = 0; i < mCallbacks.size(); i++) {
KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
if (cb != null) {
if (DEBUG || true) Log.d(TAG, "cb != null mCallbacks.size() = "+mCallbacks.size()+";");
cb.onTimeFormatChanged(timeFormat);
}
}
}
cb.onTimeFormatChanged(timeFormat);
2.frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardStatusViewController.java
private KeyguardUpdateMonitorCallback mInfoCallback = new KeyguardUpdateMonitorCallback() {
@Override
public void onLockScreenModeChanged(int mode) {
mKeyguardSliceViewController.updateLockScreenMode(mode);
}
@Override
public void onTimeChanged() {
refreshTime();
}
@Override
public void onTimeFormatChanged(String timeFormat) {
Log.d("time_12","onTimeFormatChanged");
mKeyguardClockSwitchController.refreshFormat();
}
@Override
public void onTimeZoneChanged(TimeZone timeZone) {
mKeyguardClockSwitchController.updateTimeZone(timeZone);
}
@Override
public void onKeyguardVisibilityChanged(boolean showing) {
if (showing) {
if (DEBUG) Slog.v(TAG, "refresh statusview showing:" + showing);
refreshTime();
}
}
@Override
public void onUserSwitchComplete(int userId) {
Log.d("time_12","onUserSwitchComplete");
mKeyguardClockSwitchController.refreshFormat();
}
};
在frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitorCallback.java定义了方法
/**
* Called when time format changes.
*/
public void onTimeFormatChanged(String timeFormat) { }
根据mKeyguardClockSwitchController.refreshFormat();
3.frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardClockSwitchController.java
void refreshFormat() {
Log.d("time_12","refreshFormat 1111");
if (mClockViewController != null) {
Log.d("time_12","refreshFormat 2222");
mClockViewController.refreshFormat();
mLargeClockViewController.refreshFormat();
}
}
mClockViewController.refreshFormat();
4.frameworks/base/packages/SystemUI/src/com/android/keyguard/AnimatableClockController.java
/**
* Trigger a time format update
*/
public void refreshFormat() {
Log.d("TIME_12","refreshFormat");
mView.refreshFormat();
}
mView.refreshFormat();
5.frameworks/base/packages/SystemUI/src/com/android/keyguard/AnimatableClockView.java
void refreshFormat() {
Patterns.update(mContext);
final boolean use24HourFormat = DateFormat.is24HourFormat(getContext());
Log.d("TIME_12","use24HourFormat = "+use24HourFormat+";");
//context.getUserId()
Log.d("TIME_12","getUserId = "+getContext().getUserId()+";");
if (mIsSingleLine && use24HourFormat) {
mFormat = Patterns.sClockView24;
} else if (!mIsSingleLine && use24HourFormat) {
mFormat = DOUBLE_LINE_FORMAT_24_HOUR;
} else if (mIsSingleLine && !use24HourFormat) {
mFormat = Patterns.sClockView12;
} else {
mFormat = DOUBLE_LINE_FORMAT_12_HOUR;
}
mDescFormat = use24HourFormat ? Patterns.sClockView24 : Patterns.sClockView12;
Log.d("TIME_12","mDescFormat = "+mDescFormat+";");
refreshTime();
}
至此大概能看到打开关闭24小时制开关时,对锁屏界面的一个刷新过程,主用户打开关闭此开关会正常刷新。但是我发现其他用户对此开关的开关,则只会在第一步正常的监听和获取新的小时制12或者24。但是无法响应刷新,也就是在cb.onTimeFormatChanged(timeFormat);处发现对应的方法内的log没打出来。另外在查看frameworks/base/core/java/android/text/format/DateFormat.java里的方法,
/**
* Returns true if times should be formatted as 24 hour times, false if times should be
* formatted as 12 hour (AM/PM) times. Based on the user's chosen locale and other preferences.
* @param context the context to use for the content resolver
* @return true if 24 hour time format is selected, false otherwise.
*/
public static boolean is24HourFormat(Context context) {
return is24HourFormat(context, context.getUserId());
}
我发现每次获取的都是主用户的时间格式,context.getUserId()为0。
此问题大概是多用户的设计就是这样的,根本原因我还没查到,只是查看到此流程以及发现这些问题和现象,对比Android12原声机的现象亦是如此。