1.TimeZoneFragment中查看具体的点击事件
packages\apps\TvSettings\Settings\src\com\android\tv\settings\system\TimeZoneFragment.java
onCreatePreferences 加载系统的所有时区,并封装成ZonePreference,加到List里
@Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { final Context themedContext = getPreferenceManager().getContext(); final PreferenceScreen screen = getPreferenceManager().createPreferenceScreen( themedContext); screen.setTitle(R.string.system_set_time_zone); setPreferenceScreen(screen); final List<Map<String, Object>> zoneList = ZoneGetter.getZonesList(getActivity()); final List<ZonePreference> zonePrefs = new ArrayList<>(zoneList.size()); for (final Map<String, Object> zone : zoneList) { zonePrefs.add(new ZonePreference(themedContext, zone)); } zonePrefs.sort(new ZonePrefComparator()); for (final Preference zonePref : zonePrefs) { screen.addPreference(zonePref); } }
点击事件
@Override public boolean onPreferenceTreeClick(Preference preference) { if (preference instanceof ZonePreference) { // Update the system timezone value final TimeZoneDetector timeZoneDetector = getActivity().getSystemService(TimeZoneDetector.class); ManualTimeZoneSuggestion suggestion = TimeZoneDetector.createManualTimeZoneSuggestion( preference.getKey(), "Settings: Set time zone"); timeZoneDetector.suggestManualTimeZone(suggestion); if (getParentFragment() instanceof TwoPanelSettingsFragment) { ((TwoPanelSettingsFragment) getParentFragment()).navigateBack(); } else if (!getFragmentManager().popBackStackImmediate()) { getActivity().finish(); } } return super.onPreferenceTreeClick(preference); }
调用了 TimeZoneDetector的suggestManualTimeZone方法来设置时区
frameworks\base\core\java\android\app\timezonedetector\TimeZoneDetector.java是抽象类,
@RequiresPermission(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE) void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion);
具体实现是frameworks\base\core\java\android\app\timezonedetector\TimeZoneDetectorImpl.java
private final ITimeZoneDetectorService mITimeZoneDetectorService; @Override public void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion) { if (DEBUG) { Log.d(TAG, "suggestManualTimeZone called: " + timeZoneSuggestion); } try { mITimeZoneDetectorService.suggestManualTimeZone(timeZoneSuggestion); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } }
frameworks\base\core\java\android\app\timezonedetector\ITimeZoneDetectorService.aidl
interface ITimeZoneDetectorService { void suggestManualTimeZone(in ManualTimeZoneSuggestion timeZoneSuggestion); void suggestTelephonyTimeZone(in TelephonyTimeZoneSuggestion timeZoneSuggestion); }
ITimeZoneDetectorService的具体实现是TimeZoneDetectorService
\frameworks\base\services\core\java\com\android\server\timezonedetector\TimeZoneDetectorService.java
private final TimeZoneDetectorStrategy mTimeZoneDetectorStrategy; @Override public void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion timeZoneSuggestion) { enforceSuggestManualTimeZonePermission(); Objects.requireNonNull(timeZoneSuggestion); mHandler.post(() -> mTimeZoneDetectorStrategy.suggestManualTimeZone(timeZoneSuggestion)); }
调用了mTimeZoneDetectorStrategy.suggestManualTimeZone(timeZoneSuggestion))方法,而mTimeZoneDetectorStrategy是TimeZoneDetectorStrategy 类,TimeZoneDetectorStrategy是抽象类,具体实现在 TimeZoneDetectorStrategyImpl里
frameworks\base\services\core\java\com\android\server\timezonedetector\TimeZoneDetectorStrategy.java
void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion suggestion);
frameworks\base\services\core\java\com\android\server\timezonedetector\TimeZoneDetectorStrategyImpl.java
@Override public synchronized void suggestManualTimeZone(@NonNull ManualTimeZoneSuggestion suggestion) { Objects.requireNonNull(suggestion); String timeZoneId = suggestion.getZoneId(); String cause = "Manual time suggestion received: suggestion=" + suggestion; setDeviceTimeZoneIfRequired(ORIGIN_MANUAL, timeZoneId, cause); }
调用 setDeviceTimeZoneIfRequired方法
@GuardedBy("this") private void setDeviceTimeZoneIfRequired( @Origin int origin, @NonNull String newZoneId, @NonNull String cause) { Objects.requireNonNull(newZoneId); Objects.requireNonNull(cause); boolean isOriginAutomatic = isOriginAutomatic(origin); if (isOriginAutomatic) { if (!mCallback.isAutoTimeZoneDetectionEnabled()) { if (DBG) { Slog.d(LOG_TAG, "Auto time zone detection is not enabled." + " origin=" + origin + ", newZoneId=" + newZoneId + ", cause=" + cause); } return; } } else { if (mCallback.isAutoTimeZoneDetectionEnabled()) { if (DBG) { Slog.d(LOG_TAG, "Auto time zone detection is enabled." + " origin=" + origin + ", newZoneId=" + newZoneId + ", cause=" + cause); } return; } } String currentZoneId = mCallback.getDeviceTimeZone(); // Avoid unnecessary changes / intents. if (newZoneId.equals(currentZoneId)) { // No need to set the device time zone - the setting is already what we would be // suggesting. if (DBG) { Slog.d(LOG_TAG, "No need to change the time zone;" + " device is already set to the suggested zone." + " origin=" + origin + ", newZoneId=" + newZoneId + ", cause=" + cause); } return; } mCallback.setDeviceTimeZone(newZoneId); String msg = "Set device time zone." + " origin=" + origin + ", currentZoneId=" + currentZoneId + ", newZoneId=" + newZoneId + ", cause=" + cause; if (DBG) { Slog.d(LOG_TAG, msg); } mTimeZoneChangesLog.log(msg); }
最终调用 的是 mCallback.setDeviceTimeZone(newZoneId);
private final Callback mCallback;
Callback是TimeZoneDetectorStrategyImpl.java的内部类,具体实现在TimeZoneDetectorCallbackImpl
frameworks\base\services\core\java\com\android\server\timezonedetector\TimeZoneDetectorCallbackImpl.java
@Override public void setDeviceTimeZone(String zoneId) { AlarmManager alarmManager = mContext.getSystemService(AlarmManager.class); alarmManager.setTimeZone(zoneId); }
所以应用层调用alarmManager.setTimeZone(zoneId);即可完成时区设置。