Android 设置系统时区的源码追踪

简介: Android 设置系统时区的源码追踪

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);即可完成时区设置。


目录
相关文章
|
7天前
|
存储 监控 调度
Android系统服务:WMS、AMS相关知识
参考文献 Android窗口管理服务WindowManagerService计算Activity窗口大小的过程分析 Android窗口管理服务WindowManagerService显示Activity组件的启动窗口(Starting Window)的过程分析 Android窗口管理服务WindowManagerService对输入法窗口(Input Method Window)的管理分析 Android窗口管理服务WindowManagerService显示窗口动画的原理分析
|
11天前
|
Java Linux Android开发
Android面试题之说说系统的启动流程(总结)
这篇文章概述了Android系统的启动流程,从Boot Rom到Zygote进程和SystemServer的启动。init进程作为用户级别的第一个进程,负责创建文件目录、初始化服务并启动Zygote。Zygote通过预加载资源和创建Socket服务,使用fork函数生成SystemServer进程。fork过程中,子进程继承父进程大部分信息但具有独立的进程ID。Zygote预加载资源以减少后续进程的启动时间,而SystemServer启动众多服务并最终开启Launcher应用。文中还讨论了为何从Zygote而非init或SystemServer fork新进程的原因。
20 2
|
15天前
|
传感器 Android开发 UED
Android统一设置页面竖屏
【6月更文挑战第4天】
|
17天前
|
Android开发 UED
|
18天前
|
JavaScript Java 测试技术
基于ssm+vue.js+uniapp小程序的安卓的微博客系统附带文章和源代码部署视频讲解等
基于ssm+vue.js+uniapp小程序的安卓的微博客系统附带文章和源代码部署视频讲解等
26 2
|
2天前
|
XML Java Android开发
Android RecyclerView用代码动态设置item的selector
Android RecyclerView用代码动态设置item的selector
8 0
|
2天前
|
开发工具 Android开发 git
Android自定义View——可以设置最大宽高的FrameLayout
Android自定义View——可以设置最大宽高的FrameLayout
13 0
|
2天前
|
JSON Android开发 数据格式
Android动态添加view设置view大小(宽高)
Android动态添加view设置view大小(宽高)
6 0
|
10天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的安卓的微博客系统的详细设计和实现
基于SpringBoot+Vue+uniapp的安卓的微博客系统的详细设计和实现
9 0
|
17天前
|
前端开发 Java API
Android系统中读写和显示图片
Android系统中读写和显示图片
12 0