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