Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [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 | |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 19 | import android.app.AlarmManager; |
| 20 | import android.app.PendingIntent; |
| 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
| 26 | import android.database.ContentObserver; |
Amith Yamasani | 8d394fa | 2011-03-01 12:41:04 -0800 | [diff] [blame] | 27 | import android.net.ConnectivityManager; |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 28 | import android.os.Handler; |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 29 | import android.os.HandlerThread; |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 30 | import android.os.Looper; |
| 31 | import android.os.Message; |
| 32 | import android.os.SystemClock; |
Thierry Strudel | 8c84217 | 2015-10-29 14:40:29 -0700 | [diff] [blame^] | 33 | import android.os.PowerManager; |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 34 | import android.provider.Settings; |
| 35 | import android.util.Log; |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 36 | import android.util.NtpTrustedTime; |
| 37 | import android.util.TrustedTime; |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 38 | |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 39 | import com.android.internal.telephony.TelephonyIntents; |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Monitors the network time and updates the system time if it is out of sync |
| 43 | * and there hasn't been any NITZ update from the carrier recently. |
| 44 | * If looking up the network time fails for some reason, it tries a few times with a short |
| 45 | * interval and then resets to checking on longer intervals. |
| 46 | * <p> |
| 47 | * If the user enables AUTO_TIME, it will check immediately for the network time, if NITZ wasn't |
| 48 | * available. |
| 49 | * </p> |
| 50 | */ |
| 51 | public class NetworkTimeUpdateService { |
| 52 | |
| 53 | private static final String TAG = "NetworkTimeUpdateService"; |
| 54 | private static final boolean DBG = false; |
| 55 | |
| 56 | private static final int EVENT_AUTO_TIME_CHANGED = 1; |
| 57 | private static final int EVENT_POLL_NETWORK_TIME = 2; |
Lorenzo Colitti | df59053 | 2015-01-22 22:35:33 +0900 | [diff] [blame] | 58 | private static final int EVENT_NETWORK_CHANGED = 3; |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 59 | |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 60 | private static final String ACTION_POLL = |
| 61 | "com.android.server.NetworkTimeUpdateService.action.POLL"; |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 62 | private static int POLL_REQUEST = 0; |
| 63 | |
| 64 | private static final long NOT_SET = -1; |
| 65 | private long mNitzTimeSetTime = NOT_SET; |
| 66 | // TODO: Have a way to look up the timezone we are in |
| 67 | private long mNitzZoneSetTime = NOT_SET; |
| 68 | |
| 69 | private Context mContext; |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 70 | private TrustedTime mTime; |
| 71 | |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 72 | // NTP lookup is done on this thread and handler |
| 73 | private Handler mHandler; |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 74 | private AlarmManager mAlarmManager; |
| 75 | private PendingIntent mPendingPollIntent; |
| 76 | private SettingsObserver mSettingsObserver; |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 77 | // The last time that we successfully fetched the NTP time. |
| 78 | private long mLastNtpFetchTime = NOT_SET; |
Thierry Strudel | 8c84217 | 2015-10-29 14:40:29 -0700 | [diff] [blame^] | 79 | private final PowerManager.WakeLock mWakeLock; |
Jaewan Kim | c3560b8 | 2012-11-20 23:58:07 +0900 | [diff] [blame] | 80 | |
| 81 | // Normal polling frequency |
| 82 | private final long mPollingIntervalMs; |
| 83 | // Try-again polling interval, in case the network request failed |
| 84 | private final long mPollingIntervalShorterMs; |
| 85 | // Number of times to try again |
| 86 | private final int mTryAgainTimesMax; |
| 87 | // If the time difference is greater than this threshold, then update the time. |
| 88 | private final int mTimeErrorThresholdMs; |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 89 | // Keeps track of how many quick attempts were made to fetch NTP time. |
| 90 | // During bootup, the network may not have been up yet, or it's taking time for the |
| 91 | // connection to happen. |
| 92 | private int mTryAgainCounter; |
| 93 | |
| 94 | public NetworkTimeUpdateService(Context context) { |
| 95 | mContext = context; |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 96 | mTime = NtpTrustedTime.getInstance(context); |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 97 | mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
| 98 | Intent pollIntent = new Intent(ACTION_POLL, null); |
| 99 | mPendingPollIntent = PendingIntent.getBroadcast(mContext, POLL_REQUEST, pollIntent, 0); |
Jaewan Kim | c3560b8 | 2012-11-20 23:58:07 +0900 | [diff] [blame] | 100 | |
| 101 | mPollingIntervalMs = mContext.getResources().getInteger( |
| 102 | com.android.internal.R.integer.config_ntpPollingInterval); |
| 103 | mPollingIntervalShorterMs = mContext.getResources().getInteger( |
| 104 | com.android.internal.R.integer.config_ntpPollingIntervalShorter); |
| 105 | mTryAgainTimesMax = mContext.getResources().getInteger( |
| 106 | com.android.internal.R.integer.config_ntpRetry); |
| 107 | mTimeErrorThresholdMs = mContext.getResources().getInteger( |
| 108 | com.android.internal.R.integer.config_ntpThreshold); |
Thierry Strudel | 8c84217 | 2015-10-29 14:40:29 -0700 | [diff] [blame^] | 109 | |
| 110 | mWakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE)).newWakeLock( |
| 111 | PowerManager.PARTIAL_WAKE_LOCK, TAG); |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** Initialize the receivers and initiate the first NTP request */ |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 115 | public void systemRunning() { |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 116 | registerForTelephonyIntents(); |
| 117 | registerForAlarms(); |
Amith Yamasani | 8d394fa | 2011-03-01 12:41:04 -0800 | [diff] [blame] | 118 | registerForConnectivityIntents(); |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 119 | |
Amith Yamasani | 450a16b | 2013-09-18 16:28:50 -0700 | [diff] [blame] | 120 | HandlerThread thread = new HandlerThread(TAG); |
| 121 | thread.start(); |
| 122 | mHandler = new MyHandler(thread.getLooper()); |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 123 | // Check the network time on the new thread |
| 124 | mHandler.obtainMessage(EVENT_POLL_NETWORK_TIME).sendToTarget(); |
| 125 | |
| 126 | mSettingsObserver = new SettingsObserver(mHandler, EVENT_AUTO_TIME_CHANGED); |
| 127 | mSettingsObserver.observe(mContext); |
| 128 | } |
| 129 | |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 130 | private void registerForTelephonyIntents() { |
| 131 | IntentFilter intentFilter = new IntentFilter(); |
| 132 | intentFilter.addAction(TelephonyIntents.ACTION_NETWORK_SET_TIME); |
| 133 | intentFilter.addAction(TelephonyIntents.ACTION_NETWORK_SET_TIMEZONE); |
| 134 | mContext.registerReceiver(mNitzReceiver, intentFilter); |
| 135 | } |
| 136 | |
| 137 | private void registerForAlarms() { |
| 138 | mContext.registerReceiver( |
| 139 | new BroadcastReceiver() { |
| 140 | @Override |
| 141 | public void onReceive(Context context, Intent intent) { |
| 142 | mHandler.obtainMessage(EVENT_POLL_NETWORK_TIME).sendToTarget(); |
| 143 | } |
| 144 | }, new IntentFilter(ACTION_POLL)); |
| 145 | } |
| 146 | |
Amith Yamasani | 8d394fa | 2011-03-01 12:41:04 -0800 | [diff] [blame] | 147 | private void registerForConnectivityIntents() { |
| 148 | IntentFilter intentFilter = new IntentFilter(); |
| 149 | intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
| 150 | mContext.registerReceiver(mConnectivityReceiver, intentFilter); |
| 151 | } |
| 152 | |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 153 | private void onPollNetworkTime(int event) { |
| 154 | // If Automatic time is not set, don't bother. |
| 155 | if (!isAutomaticTimeRequested()) return; |
Thierry Strudel | 8c84217 | 2015-10-29 14:40:29 -0700 | [diff] [blame^] | 156 | mWakeLock.acquire(); |
| 157 | try { |
| 158 | onPollNetworkTimeUnderWakeLock(event); |
| 159 | } finally { |
| 160 | mWakeLock.release(); |
| 161 | } |
| 162 | } |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 163 | |
Thierry Strudel | 8c84217 | 2015-10-29 14:40:29 -0700 | [diff] [blame^] | 164 | private void onPollNetworkTimeUnderWakeLock(int event) { |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 165 | final long refTime = SystemClock.elapsedRealtime(); |
Jaewan Kim | c3560b8 | 2012-11-20 23:58:07 +0900 | [diff] [blame] | 166 | // If NITZ time was received less than mPollingIntervalMs time ago, |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 167 | // no need to sync to NTP. |
Jaewan Kim | c3560b8 | 2012-11-20 23:58:07 +0900 | [diff] [blame] | 168 | if (mNitzTimeSetTime != NOT_SET && refTime - mNitzTimeSetTime < mPollingIntervalMs) { |
| 169 | resetAlarm(mPollingIntervalMs); |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | final long currentTime = System.currentTimeMillis(); |
| 173 | if (DBG) Log.d(TAG, "System time = " + currentTime); |
| 174 | // Get the NTP time |
Jaewan Kim | c3560b8 | 2012-11-20 23:58:07 +0900 | [diff] [blame] | 175 | if (mLastNtpFetchTime == NOT_SET || refTime >= mLastNtpFetchTime + mPollingIntervalMs |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 176 | || event == EVENT_AUTO_TIME_CHANGED) { |
| 177 | if (DBG) Log.d(TAG, "Before Ntp fetch"); |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 178 | |
| 179 | // force refresh NTP cache when outdated |
Jaewan Kim | c3560b8 | 2012-11-20 23:58:07 +0900 | [diff] [blame] | 180 | if (mTime.getCacheAge() >= mPollingIntervalMs) { |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 181 | mTime.forceRefresh(); |
| 182 | } |
| 183 | |
| 184 | // only update when NTP time is fresh |
Jaewan Kim | c3560b8 | 2012-11-20 23:58:07 +0900 | [diff] [blame] | 185 | if (mTime.getCacheAge() < mPollingIntervalMs) { |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 186 | final long ntp = mTime.currentTimeMillis(); |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 187 | mTryAgainCounter = 0; |
Amith Yamasani | 708d5d4 | 2012-05-02 11:50:31 -0700 | [diff] [blame] | 188 | // If the clock is more than N seconds off or this is the first time it's been |
| 189 | // fetched since boot, set the current time. |
Jaewan Kim | c3560b8 | 2012-11-20 23:58:07 +0900 | [diff] [blame] | 190 | if (Math.abs(ntp - currentTime) > mTimeErrorThresholdMs |
Amith Yamasani | 708d5d4 | 2012-05-02 11:50:31 -0700 | [diff] [blame] | 191 | || mLastNtpFetchTime == NOT_SET) { |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 192 | // Set the system time |
Amith Yamasani | 708d5d4 | 2012-05-02 11:50:31 -0700 | [diff] [blame] | 193 | if (DBG && mLastNtpFetchTime == NOT_SET |
Jaewan Kim | c3560b8 | 2012-11-20 23:58:07 +0900 | [diff] [blame] | 194 | && Math.abs(ntp - currentTime) <= mTimeErrorThresholdMs) { |
Amith Yamasani | 708d5d4 | 2012-05-02 11:50:31 -0700 | [diff] [blame] | 195 | Log.d(TAG, "For initial setup, rtc = " + currentTime); |
| 196 | } |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 197 | if (DBG) Log.d(TAG, "Ntp time to be set = " + ntp); |
| 198 | // Make sure we don't overflow, since it's going to be converted to an int |
| 199 | if (ntp / 1000 < Integer.MAX_VALUE) { |
| 200 | SystemClock.setCurrentTimeMillis(ntp); |
| 201 | } |
| 202 | } else { |
| 203 | if (DBG) Log.d(TAG, "Ntp time is close enough = " + ntp); |
| 204 | } |
Amith Yamasani | 708d5d4 | 2012-05-02 11:50:31 -0700 | [diff] [blame] | 205 | mLastNtpFetchTime = SystemClock.elapsedRealtime(); |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 206 | } else { |
| 207 | // Try again shortly |
| 208 | mTryAgainCounter++; |
Jaewan Kim | c3560b8 | 2012-11-20 23:58:07 +0900 | [diff] [blame] | 209 | if (mTryAgainTimesMax < 0 || mTryAgainCounter <= mTryAgainTimesMax) { |
| 210 | resetAlarm(mPollingIntervalShorterMs); |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 211 | } else { |
| 212 | // Try much later |
| 213 | mTryAgainCounter = 0; |
Jaewan Kim | c3560b8 | 2012-11-20 23:58:07 +0900 | [diff] [blame] | 214 | resetAlarm(mPollingIntervalMs); |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 215 | } |
| 216 | return; |
| 217 | } |
| 218 | } |
Jaewan Kim | c3560b8 | 2012-11-20 23:58:07 +0900 | [diff] [blame] | 219 | resetAlarm(mPollingIntervalMs); |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Cancel old alarm and starts a new one for the specified interval. |
| 224 | * |
| 225 | * @param interval when to trigger the alarm, starting from now. |
| 226 | */ |
| 227 | private void resetAlarm(long interval) { |
| 228 | mAlarmManager.cancel(mPendingPollIntent); |
| 229 | long now = SystemClock.elapsedRealtime(); |
| 230 | long next = now + interval; |
| 231 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, next, mPendingPollIntent); |
| 232 | } |
| 233 | |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 234 | /** |
| 235 | * Checks if the user prefers to automatically set the time. |
| 236 | */ |
| 237 | private boolean isAutomaticTimeRequested() { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 238 | return Settings.Global.getInt( |
| 239 | mContext.getContentResolver(), Settings.Global.AUTO_TIME, 0) != 0; |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /** Receiver for Nitz time events */ |
| 243 | private BroadcastReceiver mNitzReceiver = new BroadcastReceiver() { |
| 244 | |
| 245 | @Override |
| 246 | public void onReceive(Context context, Intent intent) { |
| 247 | String action = intent.getAction(); |
| 248 | if (TelephonyIntents.ACTION_NETWORK_SET_TIME.equals(action)) { |
| 249 | mNitzTimeSetTime = SystemClock.elapsedRealtime(); |
| 250 | } else if (TelephonyIntents.ACTION_NETWORK_SET_TIMEZONE.equals(action)) { |
| 251 | mNitzZoneSetTime = SystemClock.elapsedRealtime(); |
| 252 | } |
| 253 | } |
| 254 | }; |
| 255 | |
Amith Yamasani | 8d394fa | 2011-03-01 12:41:04 -0800 | [diff] [blame] | 256 | /** Receiver for ConnectivityManager events */ |
| 257 | private BroadcastReceiver mConnectivityReceiver = new BroadcastReceiver() { |
| 258 | |
| 259 | @Override |
| 260 | public void onReceive(Context context, Intent intent) { |
| 261 | String action = intent.getAction(); |
| 262 | if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) { |
Lorenzo Colitti | df59053 | 2015-01-22 22:35:33 +0900 | [diff] [blame] | 263 | // Don't bother checking if we have connectivity, NtpTrustedTime does that for us. |
| 264 | mHandler.obtainMessage(EVENT_NETWORK_CHANGED).sendToTarget(); |
Amith Yamasani | 8d394fa | 2011-03-01 12:41:04 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | }; |
| 268 | |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 269 | /** Handler to do the network accesses on */ |
| 270 | private class MyHandler extends Handler { |
| 271 | |
| 272 | public MyHandler(Looper l) { |
| 273 | super(l); |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public void handleMessage(Message msg) { |
| 278 | switch (msg.what) { |
| 279 | case EVENT_AUTO_TIME_CHANGED: |
| 280 | case EVENT_POLL_NETWORK_TIME: |
Lorenzo Colitti | df59053 | 2015-01-22 22:35:33 +0900 | [diff] [blame] | 281 | case EVENT_NETWORK_CHANGED: |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 282 | onPollNetworkTime(msg.what); |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /** Observer to watch for changes to the AUTO_TIME setting */ |
| 289 | private static class SettingsObserver extends ContentObserver { |
| 290 | |
| 291 | private int mMsg; |
| 292 | private Handler mHandler; |
| 293 | |
| 294 | SettingsObserver(Handler handler, int msg) { |
| 295 | super(handler); |
| 296 | mHandler = handler; |
| 297 | mMsg = msg; |
| 298 | } |
| 299 | |
| 300 | void observe(Context context) { |
| 301 | ContentResolver resolver = context.getContentResolver(); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 302 | resolver.registerContentObserver(Settings.Global.getUriFor(Settings.Global.AUTO_TIME), |
Amith Yamasani | 6734b9f | 2010-09-13 16:24:08 -0700 | [diff] [blame] | 303 | false, this); |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public void onChange(boolean selfChange) { |
| 308 | mHandler.obtainMessage(mMsg).sendToTarget(); |
| 309 | } |
| 310 | } |
| 311 | } |