Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.app.AlarmManager; |
| 20 | import android.app.PendingIntent; |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 21 | import android.app.timedetector.NetworkTimeSuggestion; |
| 22 | import android.app.timedetector.TimeDetector; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
| 24 | import android.content.ContentResolver; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 28 | import android.database.ContentObserver; |
| 29 | import android.net.ConnectivityManager; |
| 30 | import android.net.ConnectivityManager.NetworkCallback; |
| 31 | import android.net.Network; |
| 32 | import android.os.Binder; |
| 33 | import android.os.Handler; |
| 34 | import android.os.HandlerThread; |
| 35 | import android.os.Looper; |
| 36 | import android.os.Message; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 37 | import android.os.PowerManager; |
Neil Fuller | ca48eb7 | 2018-06-27 11:40:06 +0100 | [diff] [blame] | 38 | import android.os.SystemClock; |
Neil Fuller | d159015 | 2020-01-08 16:31:44 +0000 | [diff] [blame] | 39 | import android.os.TimestampedValue; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 40 | import android.provider.Settings; |
| 41 | import android.util.Log; |
| 42 | import android.util.NtpTrustedTime; |
| 43 | import android.util.TimeUtils; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 44 | |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 45 | import com.android.internal.util.DumpUtils; |
| 46 | |
| 47 | import java.io.FileDescriptor; |
| 48 | import java.io.PrintWriter; |
| 49 | |
| 50 | /** |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 51 | * Monitors the network time. If looking up the network time fails for some reason, it tries a few |
| 52 | * times with a short interval and then resets to checking on longer intervals. |
| 53 | * |
| 54 | * <p>When available, the time is always suggested to the {@link |
| 55 | * com.android.server.timedetector.TimeDetectorService} where it may be used to set the device |
| 56 | * system clock, depending on user settings and what other signals are available. |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 57 | */ |
Neil Fuller | e9efed9 | 2019-06-27 15:01:01 +0100 | [diff] [blame] | 58 | public class NetworkTimeUpdateServiceImpl extends Binder implements NetworkTimeUpdateService { |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 59 | |
| 60 | private static final String TAG = "NetworkTimeUpdateService"; |
| 61 | private static final boolean DBG = false; |
| 62 | |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 63 | private static final int EVENT_AUTO_TIME_ENABLED = 1; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 64 | private static final int EVENT_POLL_NETWORK_TIME = 2; |
| 65 | private static final int EVENT_NETWORK_CHANGED = 3; |
| 66 | |
| 67 | private static final String ACTION_POLL = |
| 68 | "com.android.server.NetworkTimeUpdateService.action.POLL"; |
| 69 | |
| 70 | private static final int POLL_REQUEST = 0; |
| 71 | |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 72 | private Network mDefaultNetwork = null; |
| 73 | |
Neil Fuller | ca48eb7 | 2018-06-27 11:40:06 +0100 | [diff] [blame] | 74 | private final Context mContext; |
| 75 | private final NtpTrustedTime mTime; |
| 76 | private final AlarmManager mAlarmManager; |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 77 | private final TimeDetector mTimeDetector; |
Neil Fuller | ca48eb7 | 2018-06-27 11:40:06 +0100 | [diff] [blame] | 78 | private final ConnectivityManager mCM; |
| 79 | private final PendingIntent mPendingPollIntent; |
| 80 | private final PowerManager.WakeLock mWakeLock; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 81 | |
| 82 | // NTP lookup is done on this thread and handler |
| 83 | private Handler mHandler; |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 84 | private AutoTimeSettingObserver mAutoTimeSettingObserver; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 85 | private NetworkTimeUpdateCallback mNetworkTimeUpdateCallback; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 86 | |
| 87 | // Normal polling frequency |
| 88 | private final long mPollingIntervalMs; |
| 89 | // Try-again polling interval, in case the network request failed |
| 90 | private final long mPollingIntervalShorterMs; |
| 91 | // Number of times to try again |
| 92 | private final int mTryAgainTimesMax; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 93 | // Keeps track of how many quick attempts were made to fetch NTP time. |
| 94 | // During bootup, the network may not have been up yet, or it's taking time for the |
| 95 | // connection to happen. |
| 96 | private int mTryAgainCounter; |
| 97 | |
Neil Fuller | e9efed9 | 2019-06-27 15:01:01 +0100 | [diff] [blame] | 98 | public NetworkTimeUpdateServiceImpl(Context context) { |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 99 | mContext = context; |
| 100 | mTime = NtpTrustedTime.getInstance(context); |
Neil Fuller | ca48eb7 | 2018-06-27 11:40:06 +0100 | [diff] [blame] | 101 | mAlarmManager = mContext.getSystemService(AlarmManager.class); |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 102 | mTimeDetector = mContext.getSystemService(TimeDetector.class); |
Neil Fuller | ca48eb7 | 2018-06-27 11:40:06 +0100 | [diff] [blame] | 103 | mCM = mContext.getSystemService(ConnectivityManager.class); |
| 104 | |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 105 | Intent pollIntent = new Intent(ACTION_POLL, null); |
| 106 | mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0); |
| 107 | |
| 108 | mPollingIntervalMs = mContext.getResources().getInteger( |
| 109 | com.android.internal.R.integer.config_ntpPollingInterval); |
| 110 | mPollingIntervalShorterMs = mContext.getResources().getInteger( |
| 111 | com.android.internal.R.integer.config_ntpPollingIntervalShorter); |
| 112 | mTryAgainTimesMax = mContext.getResources().getInteger( |
| 113 | com.android.internal.R.integer.config_ntpRetry); |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 114 | |
Neil Fuller | ca48eb7 | 2018-06-27 11:40:06 +0100 | [diff] [blame] | 115 | mWakeLock = context.getSystemService(PowerManager.class).newWakeLock( |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 116 | PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 117 | } |
| 118 | |
Neil Fuller | ca48eb7 | 2018-06-27 11:40:06 +0100 | [diff] [blame] | 119 | @Override |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 120 | public void systemRunning() { |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 121 | registerForAlarms(); |
| 122 | |
| 123 | HandlerThread thread = new HandlerThread(TAG); |
| 124 | thread.start(); |
| 125 | mHandler = new MyHandler(thread.getLooper()); |
| 126 | mNetworkTimeUpdateCallback = new NetworkTimeUpdateCallback(); |
| 127 | mCM.registerDefaultNetworkCallback(mNetworkTimeUpdateCallback, mHandler); |
| 128 | |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 129 | mAutoTimeSettingObserver = new AutoTimeSettingObserver(mContext, mHandler, |
| 130 | EVENT_AUTO_TIME_ENABLED); |
| 131 | mAutoTimeSettingObserver.observe(); |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | private void registerForAlarms() { |
| 135 | mContext.registerReceiver( |
| 136 | new BroadcastReceiver() { |
| 137 | @Override |
| 138 | public void onReceive(Context context, Intent intent) { |
| 139 | mHandler.obtainMessage(EVENT_POLL_NETWORK_TIME).sendToTarget(); |
| 140 | } |
| 141 | }, new IntentFilter(ACTION_POLL)); |
| 142 | } |
| 143 | |
| 144 | private void onPollNetworkTime(int event) { |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 145 | // If we don't have any default network, don't bother. |
Neil Fuller | ca48eb7 | 2018-06-27 11:40:06 +0100 | [diff] [blame] | 146 | if (mDefaultNetwork == null) return; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 147 | mWakeLock.acquire(); |
| 148 | try { |
| 149 | onPollNetworkTimeUnderWakeLock(event); |
| 150 | } finally { |
| 151 | mWakeLock.release(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private void onPollNetworkTimeUnderWakeLock(int event) { |
Neil Fuller | ca48eb7 | 2018-06-27 11:40:06 +0100 | [diff] [blame] | 156 | // Force an NTP fix when outdated |
Neil Fuller | 65f0f31 | 2019-12-20 15:59:38 +0000 | [diff] [blame] | 157 | NtpTrustedTime.TimeResult cachedNtpResult = mTime.getCachedTimeResult(); |
| 158 | if (cachedNtpResult == null || cachedNtpResult.getAgeMillis() >= mPollingIntervalMs) { |
Neil Fuller | ca48eb7 | 2018-06-27 11:40:06 +0100 | [diff] [blame] | 159 | if (DBG) Log.d(TAG, "Stale NTP fix; forcing refresh"); |
| 160 | mTime.forceRefresh(); |
Neil Fuller | 65f0f31 | 2019-12-20 15:59:38 +0000 | [diff] [blame] | 161 | cachedNtpResult = mTime.getCachedTimeResult(); |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 162 | } |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 163 | |
Neil Fuller | 65f0f31 | 2019-12-20 15:59:38 +0000 | [diff] [blame] | 164 | if (cachedNtpResult != null && cachedNtpResult.getAgeMillis() < mPollingIntervalMs) { |
Neil Fuller | ca48eb7 | 2018-06-27 11:40:06 +0100 | [diff] [blame] | 165 | // Obtained fresh fix; schedule next normal update |
| 166 | resetAlarm(mPollingIntervalMs); |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 167 | |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 168 | // Suggest the time to the time detector. It may choose use it to set the system clock. |
Neil Fuller | 65f0f31 | 2019-12-20 15:59:38 +0000 | [diff] [blame] | 169 | TimestampedValue<Long> timeSignal = new TimestampedValue<>( |
| 170 | cachedNtpResult.getElapsedRealtimeMillis(), cachedNtpResult.getTimeMillis()); |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 171 | NetworkTimeSuggestion timeSuggestion = new NetworkTimeSuggestion(timeSignal); |
| 172 | timeSuggestion.addDebugInfo("Origin: NetworkTimeUpdateServiceImpl. event=" + event); |
| 173 | mTimeDetector.suggestNetworkTime(timeSuggestion); |
Neil Fuller | ca48eb7 | 2018-06-27 11:40:06 +0100 | [diff] [blame] | 174 | } else { |
| 175 | // No fresh fix; schedule retry |
| 176 | mTryAgainCounter++; |
| 177 | if (mTryAgainTimesMax < 0 || mTryAgainCounter <= mTryAgainTimesMax) { |
| 178 | resetAlarm(mPollingIntervalShorterMs); |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 179 | } else { |
Neil Fuller | ca48eb7 | 2018-06-27 11:40:06 +0100 | [diff] [blame] | 180 | // Try much later |
| 181 | mTryAgainCounter = 0; |
| 182 | resetAlarm(mPollingIntervalMs); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 187 | /** |
| 188 | * Cancel old alarm and starts a new one for the specified interval. |
| 189 | * |
| 190 | * @param interval when to trigger the alarm, starting from now. |
| 191 | */ |
| 192 | private void resetAlarm(long interval) { |
| 193 | mAlarmManager.cancel(mPendingPollIntent); |
| 194 | long now = SystemClock.elapsedRealtime(); |
| 195 | long next = now + interval; |
| 196 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, next, mPendingPollIntent); |
| 197 | } |
| 198 | |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 199 | /** Handler to do the network accesses on */ |
| 200 | private class MyHandler extends Handler { |
| 201 | |
| 202 | public MyHandler(Looper l) { |
| 203 | super(l); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void handleMessage(Message msg) { |
| 208 | switch (msg.what) { |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 209 | case EVENT_AUTO_TIME_ENABLED: |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 210 | case EVENT_POLL_NETWORK_TIME: |
| 211 | case EVENT_NETWORK_CHANGED: |
| 212 | onPollNetworkTime(msg.what); |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | private class NetworkTimeUpdateCallback extends NetworkCallback { |
| 219 | @Override |
| 220 | public void onAvailable(Network network) { |
| 221 | Log.d(TAG, String.format("New default network %s; checking time.", network)); |
| 222 | mDefaultNetwork = network; |
| 223 | // Running on mHandler so invoke directly. |
| 224 | onPollNetworkTime(EVENT_NETWORK_CHANGED); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void onLost(Network network) { |
| 229 | if (network.equals(mDefaultNetwork)) mDefaultNetwork = null; |
| 230 | } |
| 231 | } |
| 232 | |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 233 | /** |
| 234 | * Observer to watch for changes to the AUTO_TIME setting. It only triggers when the setting |
| 235 | * is enabled. |
| 236 | */ |
| 237 | private static class AutoTimeSettingObserver extends ContentObserver { |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 238 | |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 239 | private final Context mContext; |
| 240 | private final int mMsg; |
| 241 | private final Handler mHandler; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 242 | |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 243 | AutoTimeSettingObserver(Context context, Handler handler, int msg) { |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 244 | super(handler); |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 245 | mContext = context; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 246 | mHandler = handler; |
| 247 | mMsg = msg; |
| 248 | } |
| 249 | |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 250 | void observe() { |
| 251 | ContentResolver resolver = mContext.getContentResolver(); |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 252 | resolver.registerContentObserver(Settings.Global.getUriFor(Settings.Global.AUTO_TIME), |
| 253 | false, this); |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public void onChange(boolean selfChange) { |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 258 | if (isAutomaticTimeEnabled()) { |
| 259 | mHandler.obtainMessage(mMsg).sendToTarget(); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Checks if the user prefers to automatically set the time. |
| 265 | */ |
| 266 | private boolean isAutomaticTimeEnabled() { |
| 267 | ContentResolver resolver = mContext.getContentResolver(); |
| 268 | return Settings.Global.getInt(resolver, Settings.Global.AUTO_TIME, 0) != 0; |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 274 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
| 275 | pw.print("PollingIntervalMs: "); |
| 276 | TimeUtils.formatDuration(mPollingIntervalMs, pw); |
| 277 | pw.print("\nPollingIntervalShorterMs: "); |
| 278 | TimeUtils.formatDuration(mPollingIntervalShorterMs, pw); |
| 279 | pw.println("\nTryAgainTimesMax: " + mTryAgainTimesMax); |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 280 | pw.println("\nTryAgainCounter: " + mTryAgainCounter); |
Neil Fuller | 65f0f31 | 2019-12-20 15:59:38 +0000 | [diff] [blame] | 281 | NtpTrustedTime.TimeResult ntpResult = mTime.getCachedTimeResult(); |
| 282 | pw.println("NTP cache result: " + ntpResult); |
| 283 | if (ntpResult != null) { |
| 284 | pw.println("NTP result age: " + ntpResult.getAgeMillis()); |
| 285 | } |
Neil Fuller | b3d6abb | 2018-06-21 15:16:36 +0100 | [diff] [blame] | 286 | pw.println(); |
| 287 | } |
| 288 | } |