Android時間設置的3個小彩蛋分享
問題現象
最近處理瞭一個非常有意思的系統bug,修改系統時間,重啟後居然沒有生效
註意要關閉使用網絡提供的時間和使用網絡提供的時區這兩個開關。
重啟後顯示的時間日期為
顯示的時間既不是我設置的時間,也不是當前時間(當前時間為2023-03-20 15:49),那麼顯示的這個時間到底是什麼時間呢?
為瞭弄清楚這個問題,我研究瞭一下Android設置時間的邏輯,研究過程中還發現瞭一些彩蛋。
源碼分析
首先是設置時間的邏輯,源碼位於packages/apps/Settings/src/com/android/settings/datetime/DatePreferenceController.java
public class DatePreferenceController extends AbstractPreferenceController implements PreferenceControllerMixin, DatePickerDialog.OnDateSetListener { //省略部分代碼 private final DatePreferenceHost mHost; @Override public boolean handlePreferenceTreeClick(Preference preference) { //點擊日期後處理 if (!TextUtils.equals(preference.getKey(), KEY_DATE)) { return false; } //顯示日期選擇框 mHost.showDatePicker(); return true; } //省略部分代碼 }
mHost
是DatePreferenceHost
接口,接口實現在packages/apps/Settings/src/com/android/settings/DateTimeSettings.java
中,因此,showDatePicker()
的邏輯位於該實現類中
@SearchIndexable public class DateTimeSettings extends DashboardFragment implements TimePreferenceController.TimePreferenceHost, DatePreferenceController.DatePreferenceHost { //省略部分代碼 @Override public void showDatePicker() { //顯示日期選擇對話框 showDialog(DatePreferenceController.DIALOG_DATEPICKER); } //省略部分代碼 }
showDialog()
定義在父類packages/apps/Settings/src/com/android/settings/SettingsPreferenceFragment.java
中
public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceFragment implements DialogCreatable, HelpResourceProvider, Indexable { protected void showDialog(int dialogId) { if (mDialogFragment != null) { Log.e(TAG, "Old dialog fragment not null!"); } //創建SettingsDialogFragment並進行show mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId); mDialogFragment.show(getChildFragmentManager(), Integer.toString(dialogId)); } }
showDialog()
中就是創建瞭SettingsDialogFragment
然後顯示,SettingsDialogFragment
是SettingsPreferenceFragment
的一個內部類,看一下SettingsDialogFragment
的定義
public static class SettingsDialogFragment extends InstrumentedDialogFragment { private static final String KEY_DIALOG_ID = "key_dialog_id"; private static final String KEY_PARENT_FRAGMENT_ID = "key_parent_fragment_id"; private Fragment mParentFragment; private DialogInterface.OnCancelListener mOnCancelListener; private DialogInterface.OnDismissListener mOnDismissListener; public static SettingsDialogFragment newInstance(DialogCreatable fragment, int dialogId) { if (!(fragment instanceof Fragment)) { throw new IllegalArgumentException("fragment argument must be an instance of " + Fragment.class.getName()); } final SettingsDialogFragment settingsDialogFragment = new SettingsDialogFragment(); settingsDialogFragment.setParentFragment(fragment); settingsDialogFragment.setDialogId(dialogId); return settingsDialogFragment; } @Override public int getMetricsCategory() { if (mParentFragment == null) { return Instrumentable.METRICS_CATEGORY_UNKNOWN; } final int metricsCategory = ((DialogCreatable) mParentFragment).getDialogMetricsCategory(mDialogId); if (metricsCategory <= 0) { throw new IllegalStateException("Dialog must provide a metrics category"); } return metricsCategory; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (mParentFragment != null) { outState.putInt(KEY_DIALOG_ID, mDialogId); outState.putInt(KEY_PARENT_FRAGMENT_ID, mParentFragment.getId()); } } @Override public void onStart() { super.onStart(); if (mParentFragment != null && mParentFragment instanceof SettingsPreferenceFragment) { ((SettingsPreferenceFragment) mParentFragment).onDialogShowing(); } } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { if (savedInstanceState != null) { mDialogId = savedInstanceState.getInt(KEY_DIALOG_ID, 0); mParentFragment = getParentFragment(); int mParentFragmentId = savedInstanceState.getInt(KEY_PARENT_FRAGMENT_ID, -1); if (mParentFragment == null) { mParentFragment = getFragmentManager().findFragmentById(mParentFragmentId); } if (!(mParentFragment instanceof DialogCreatable)) { throw new IllegalArgumentException( (mParentFragment != null ? mParentFragment.getClass().getName() : mParentFragmentId) + " must implement " + DialogCreatable.class.getName()); } // This dialog fragment could be created from non-SettingsPreferenceFragment if (mParentFragment instanceof SettingsPreferenceFragment) { // restore mDialogFragment in mParentFragment ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = this; } } //通過DialogCreatable接口剝離瞭dialog的創建 return ((DialogCreatable) mParentFragment).onCreateDialog(mDialogId); } @Override public void onCancel(DialogInterface dialog) { super.onCancel(dialog); if (mOnCancelListener != null) { mOnCancelListener.onCancel(dialog); } } @Override public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); if (mOnDismissListener != null) { mOnDismissListener.onDismiss(dialog); } } public int getDialogId() { return mDialogId; } @Override public void onDetach() { super.onDetach(); // This dialog fragment could be created from non-SettingsPreferenceFragment if (mParentFragment instanceof SettingsPreferenceFragment) { // in case the dialog is not explicitly removed by removeDialog() if (((SettingsPreferenceFragment) mParentFragment).mDialogFragment == this) { ((SettingsPreferenceFragment) mParentFragment).mDialogFragment = null; } } } private void setParentFragment(DialogCreatable fragment) { mParentFragment = (Fragment) fragment; } private void setDialogId(int dialogId) { mDialogId = dialogId; } }
很標準的自定義DialogFragment
的模板代碼,核心代碼在onCreateDialog()
方法當中,但此方法通過DialogCreatable
接口剝離瞭dialog的創建,這裡也很好理解,因為不僅有設置日期的Dialog,還有設置時間的Dialog,如果寫死的話,那麼就需要定義兩個DialogFragment
,所以這裡它給抽象出來瞭,DialogCreatable
接口的實現仍然在DateTimeSettings
當中,它的父類SettingsPreferenceFragment
實現瞭DialogCreatable
@SearchIndexable public class DateTimeSettings extends DashboardFragment implements TimePreferenceController.TimePreferenceHost, DatePreferenceController.DatePreferenceHost { //省略部分代碼 @Override public Dialog onCreateDialog(int id) { //根據選項創建對應的dialog switch (id) { case DatePreferenceController.DIALOG_DATEPICKER: return use(DatePreferenceController.class) .buildDatePicker(getActivity()); case TimePreferenceController.DIALOG_TIMEPICKER: return use(TimePreferenceController.class) .buildTimePicker(getActivity()); default: throw new IllegalArgumentException(); } } //省略部分代碼 }
根據用戶選擇的操作(設置日期or設置時間),創建對應的dialog,最終的創建過程由DatePreferenceController
來完成
public class DatePreferenceController extends AbstractPreferenceController implements PreferenceControllerMixin, DatePickerDialog.OnDateSetListener { //省略部分代碼 public DatePickerDialog buildDatePicker(Activity activity) { final Calendar calendar = Calendar.getInstance(); //創建DatePickerDialog final DatePickerDialog d = new DatePickerDialog( activity, this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); // The system clock can't represent dates outside this range. calendar.clear(); calendar.set(2007, Calendar.JANUARY, 1); //設置最小時間為2007-01-01 d.getDatePicker().setMinDate(calendar.getTimeInMillis()); calendar.clear(); calendar.set(2037, Calendar.DECEMBER, 31); //設置最大時間為2037-12-31 d.getDatePicker().setMaxDate(calendar.getTimeInMillis()); return d; } //省略部分代碼 }
這裡可以看到,系統限制瞭可選的日期范圍為2007-01-01至2037-12-31,實際操作也確實是這樣子的(開發板和小米手機都是),此為彩蛋1。
看一下DatePickerDialog
的定義
public class DatePickerDialog extends AlertDialog implements OnClickListener, OnDateChangedListener { private static final String YEAR = "year"; private static final String MONTH = "month"; private static final String DAY = "day"; @UnsupportedAppUsage private final DatePicker mDatePicker; private OnDateSetListener mDateSetListener; //省略部分代碼 private DatePickerDialog(@NonNull Context context, @StyleRes int themeResId, @Nullable OnDateSetListener listener, @Nullable Calendar calendar, int year, int monthOfYear, int dayOfMonth) { super(context, resolveDialogTheme(context, themeResId)); final Context themeContext = getContext(); final LayoutInflater inflater = LayoutInflater.from(themeContext); //初始化Dialog的View final View view = inflater.inflate(R.layout.date_picker_dialog, null); setView(view); setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this); setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this); setButtonPanelLayoutHint(LAYOUT_HINT_SIDE); if (calendar != null) { year = calendar.get(Calendar.YEAR); monthOfYear = calendar.get(Calendar.MONTH); dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); } mDatePicker = (DatePicker) view.findViewById(R.id.datePicker); mDatePicker.init(year, monthOfYear, dayOfMonth, this); mDatePicker.setValidationCallback(mValidationCallback); mDateSetListener = listener; } //省略部分代碼 /** * Sets the listener to call when the user sets the date. * * @param listener the listener to call when the user sets the date */ public void setOnDateSetListener(@Nullable OnDateSetListener listener) { mDateSetListener = listener; } @Override public void onClick(@NonNull DialogInterface dialog, int which) { switch (which) { case BUTTON_POSITIVE: if (mDateSetListener != null) { // Clearing focus forces the dialog to commit any pending // changes, e.g. typed text in a NumberPicker. mDatePicker.clearFocus(); //設置完成回調 mDateSetListener.onDateSet(mDatePicker, mDatePicker.getYear(), mDatePicker.getMonth(), mDatePicker.getDayOfMonth()); } break; case BUTTON_NEGATIVE: cancel(); break; } } //省略部分代碼 /** * The listener used to indicate the user has finished selecting a date. */ public interface OnDateSetListener { /** * @param view the picker associated with the dialog * @param year the selected year * @param month the selected month (0-11 for compatibility with * {@link Calendar#MONTH}) * @param dayOfMonth the selected day of the month (1-31, depending on * month) */ void onDateSet(DatePicker view, int year, int month, int dayOfMonth); } }
可以看到也是標準的自定義Dialog,不過它是繼承的AlertDialog
,設置完成後通過OnDateSetListener
進行回調,而DatePreferenceController
實現瞭該接口
public class DatePreferenceController extends AbstractPreferenceController implements PreferenceControllerMixin, DatePickerDialog.OnDateSetListener { //省略部分代碼 @Override public void onDateSet(DatePicker view, int year, int month, int day) { //設置日期 setDate(year, month, day); //更新UI mHost.updateTimeAndDateDisplay(mContext); } //省略部分代碼 @VisibleForTesting void setDate(int year, int month, int day) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); c.set(Calendar.DAY_OF_MONTH, day); //設置日期與定義的最小日期取最大值,也就意味著設置的日期不能小於定義的最小日期 long when = Math.max(c.getTimeInMillis(), DatePreferenceHost.MIN_DATE); if (when / 1000 < Integer.MAX_VALUE) { //設置系統時間 ((AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE)).setTime(when); } } }
可以看到系統定義瞭一個最小日期DatePreferenceHost.MIN_DATE
,其值為2007-11-05 0:00
public interface UpdateTimeAndDateCallback { // Minimum time is Nov 5, 2007, 0:00. long MIN_DATE = 1194220800000L; void updateTimeAndDateDisplay(Context context); }
最終顯示日期會在目標日期和最小日期中取最大值,也就是說設定的日期不能小於最小日期,而上文說到,選擇的日期范圍為2007-01-01至2037-12-31,因此,如果你設置的日期在2007-01-01至2007-11-05之間,最終都會顯示2007-11-05,實際測試也是如此(開發板和小米手機都是),此為彩蛋2。
選擇完時間後,最後通過AlarmManagerService
來設置系統內核的時間,此處涉及到跨進程通信,使用的通信方式是AIDL,直接到AlarmManagerService
看看如何設置內核時間的
class AlarmManagerService extends SystemService { //省略部分代碼 /** * Public-facing binder interface */ private final IBinder mService = new IAlarmManager.Stub() { //省略部分代碼 @Override public boolean setTime(long millis) { //先授權 getContext().enforceCallingOrSelfPermission( "android.permission.SET_TIME", "setTime"); //然後設置系統內核時間 return setTimeImpl(millis); } //省略部分代碼 } //省略部分代碼 boolean setTimeImpl(long millis) { if (!mInjector.isAlarmDriverPresent()) { Slog.w(TAG, "Not setting time since no alarm driver is available."); return false; } synchronized (mLock) { final long currentTimeMillis = mInjector.getCurrentTimeMillis(); //設置系統內核時間 mInjector.setKernelTime(millis); final TimeZone timeZone = TimeZone.getDefault(); final int currentTzOffset = timeZone.getOffset(currentTimeMillis); final int newTzOffset = timeZone.getOffset(millis); if (currentTzOffset != newTzOffset) { Slog.i(TAG, "Timezone offset has changed, updating kernel timezone"); //設置系統內核時區 mInjector.setKernelTimezone(-(newTzOffset / 60000)); } // The native implementation of setKernelTime can return -1 even when the kernel // time was set correctly, so assume setting kernel time was successful and always // return true. return true; } } //省略部分代碼 @VisibleForTesting static class Injector { //省略部分代碼 void setKernelTime(long millis) { Log.d("jasonwan", "setKernelTime: "+millis); if (mNativeData != 0) { //在native層完成內核時間的設置 AlarmManagerService.setKernelTime(mNativeData, millis); } } //省略部分代碼 } //native層完成 private static native int setKernelTime(long nativeData, long millis); private static native int setKernelTimezone(long nativeData, int minuteswest); //省略部分代碼 }
可以看到最終是在native層完成內核時間的設置,這也理所當然,畢竟java是應用層,觸及不到kernel層。
回到最開始的問題,為啥開機之後卻不是我們設置的時間呢,這就要看看開機之後系統是怎麼設置時間的。同樣在AlarmManagerService
裡面,因為它是SystemService
的子類,所以會隨著開機啟動而啟動,而Service啟動後必定會執行它的生命周期方法,設置時間的邏輯就是在onStart()
生命周期方法裡面
class AlarmManagerService extends SystemService { //省略部分代碼 @Override public void onStart() { mInjector.init(); synchronized (mLock) { //省略部分代碼 // We have to set current TimeZone info to kernel // because kernel doesn't keep this after reboot //設置時區,從SystemProperty中讀取 setTimeZoneImpl(SystemProperties.get(TIMEZONE_PROPERTY)); // Ensure that we're booting with a halfway sensible current time. Use the // most recent of Build.TIME, the root file system's timestamp, and the // value of the ro.build.date.utc system property (which is in seconds). //設置時區 //先讀取系統編譯時間 long utc = 1000L * SystemProperties.getLong("ro.build.date.utc", -1L); //再讀取根目錄最近的修改的時間 long lastModified = Environment.getRootDirectory().lastModified(); //然後讀取系統構建時間,三個時間取最大值 final long systemBuildTime = Long.max( utc, Long.max(lastModified, Build.TIME)); //代碼1 Log.d("jasonwan", "onStart: utc="+utc+", lastModified="+lastModified+", BuildTime="+Build.TIME+", currentTimeMillis="+mInjector.getCurrentTimeMillis()); //設置的時間小於最大值,則將最大值設置為系統內核的時間,註意,因為我們剛剛已經設置瞭內核時間,所以重啟後通過System.currentTimeMillis()得到的時間戳為我們設置的時間,此判斷意味著,系統編譯時間、根目錄最近修改時間、系統構建時間、設置的時間,這四者當中取最大值作為重啟後的內核時間 if (mInjector.getCurrentTimeMillis() < systemBuildTime) { //這裡mInjector.getCurrentTimeMillis()其實就是System.currentTimeMillis() Slog.i(TAG, "Current time only " + mInjector.getCurrentTimeMillis() + ", advancing to build time " + systemBuildTime); mInjector.setKernelTime(systemBuildTime); } //省略部分代碼 } //省略部分代碼 @VisibleForTesting static class Injector { //省略部分代碼 void setKernelTimezone(int minutesWest) { AlarmManagerService.setKernelTimezone(mNativeData, minutesWest); } void setKernelTime(long millis) { //代碼2 Log.d("jasonwan", "setKernelTime: "+millis); if (mNativeData != 0) { AlarmManagerService.setKernelTime(mNativeData, millis); } } //省略部分代碼 long getElapsedRealtime() { return SystemClock.elapsedRealtime(); } long getCurrentTimeMillis() { return System.currentTimeMillis(); } //省略部分代碼 } }
實踐驗證
根據源碼分析得知,系統最終會在系統編譯時間、根目錄最近修改時間、系統構建時間、設置的時間,這四者當中取最大值作為重啟後的內核時間,這裡我在代碼1和代碼2處埋下瞭log,看看四個時間的值分別是多少,以及最終設置的內核時間是多少,我在設置中手動設置的日期為2022-10-01,重啟後的日志如下
四個值分別為:
- 系統編譯時間:1669271830000,格式化後為2022-11-24 14:37:10
- 根目錄最近修改時間:1678865533000,格式化後為2023-03-15 15:32:13
- 構建時間:1669271830000,同系統編譯時間
- 設置的時間:1664609754998,格式化後為2022-10-01 15:35:54
註意,我們隻需要註意日期,不需要關註時分秒,可以看到四個時間當中,最大的為根目錄最近修改時間,所以最終顯示的日期為2023-03-15,此為彩蛋3。
我在開發板和小米手機上測試的結果相同,說明MIUI保留瞭這一塊的邏輯,但是MIUI也有一個bug,就是明明我關閉瞭使用網絡提供的時間和使用網絡提供的時區,它還是給我自動更新瞭日期和時間,除非開啟飛行模式之後才不自動更新。
同時我們還註意到,系統編譯時間ro.build.date.utc
跟系統構建時間Build.TIME
是相同的,這很好理解,編譯跟構建是一個意思,而且Build.TIME
的取值其實也來自於ro.build.date.utc
/** * Information about the current build, extracted from system properties. */ public class Build { //省略部分代碼 /** The time at which the build was produced, given in milliseconds since the UNIX epoch. */ public static final long TIME = getLong("ro.build.date.utc") * 1000; //省略部分代碼 }
我也搞不懂Google為什麼要設計兩個概念,搞得我一開始還去研究這兩個概念的區別,結果沒區別,數據源是一樣的,尷尬。
結論
設置系統時間必須大於系統編譯時間和根目錄最近修改時間才會生效。
最後我在想,MIUI是不是可以在這一塊優化一下,直接設置裡面告訴用戶我能設置的時間區域豈不是更人性化,畢竟細節決定成敗。
到此這篇關於Android時間設置的3個小彩蛋的文章就介紹到這瞭,更多相關Android時間設置彩蛋內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Java 進行時間處理的步驟
- 新手小白學JAVA 日期類Date SimpleDateFormat Calendar(入門)
- Android使用DatePickerDialog顯示時間
- Java中Date日期時間類具體使用
- Python calendar模塊詳情