blob: da566c934ef743a69eb5639779b939d8e84803f0 [file] [log] [blame]
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -07001/*
2 * Copyright (C) 2011 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
17package android.util;
18
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010019import android.annotation.UnsupportedAppUsage;
Jeff Sharkey104344e2011-07-10 14:20:41 -070020import android.content.ContentResolver;
21import android.content.Context;
22import android.content.res.Resources;
Lorenzo Colittidf590532015-01-22 22:35:33 +090023import android.net.ConnectivityManager;
Kurt Marcinkiewiczdfdd82e2018-03-05 14:45:04 -080024import android.net.Network;
Lorenzo Colittidf590532015-01-22 22:35:33 +090025import android.net.NetworkInfo;
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070026import android.net.SntpClient;
27import android.os.SystemClock;
Jeff Sharkey104344e2011-07-10 14:20:41 -070028import android.provider.Settings;
Young-Ho Cha0eda7df2015-01-07 11:24:35 +090029import android.text.TextUtils;
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070030
31/**
Jeff Sharkey104344e2011-07-10 14:20:41 -070032 * {@link TrustedTime} that connects with a remote NTP server as its trusted
33 * time source.
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070034 *
35 * @hide
36 */
37public class NtpTrustedTime implements TrustedTime {
Jeff Sharkey104344e2011-07-10 14:20:41 -070038 private static final String TAG = "NtpTrustedTime";
39 private static final boolean LOGD = false;
40
41 private static NtpTrustedTime sSingleton;
Lorenzo Colittidf590532015-01-22 22:35:33 +090042 private static Context sContext;
Jeff Sharkey104344e2011-07-10 14:20:41 -070043
44 private final String mServer;
45 private final long mTimeout;
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070046
Lorenzo Colittidf590532015-01-22 22:35:33 +090047 private ConnectivityManager mCM;
48
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070049 private boolean mHasCache;
50 private long mCachedNtpTime;
51 private long mCachedNtpElapsedRealtime;
52 private long mCachedNtpCertainty;
53
Jeff Sharkey104344e2011-07-10 14:20:41 -070054 private NtpTrustedTime(String server, long timeout) {
55 if (LOGD) Log.d(TAG, "creating NtpTrustedTime using " + server);
56 mServer = server;
57 mTimeout = timeout;
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070058 }
59
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010060 @UnsupportedAppUsage
Jeff Sharkey104344e2011-07-10 14:20:41 -070061 public static synchronized NtpTrustedTime getInstance(Context context) {
62 if (sSingleton == null) {
63 final Resources res = context.getResources();
64 final ContentResolver resolver = context.getContentResolver();
65
66 final String defaultServer = res.getString(
67 com.android.internal.R.string.config_ntpServer);
68 final long defaultTimeout = res.getInteger(
69 com.android.internal.R.integer.config_ntpTimeout);
70
Jeff Sharkeye6e61972012-09-14 13:47:51 -070071 final String secureServer = Settings.Global.getString(
Jeff Sharkey023c05a2012-09-14 13:09:57 -070072 resolver, Settings.Global.NTP_SERVER);
Jeff Sharkeye6e61972012-09-14 13:47:51 -070073 final long timeout = Settings.Global.getLong(
Jeff Sharkey023c05a2012-09-14 13:09:57 -070074 resolver, Settings.Global.NTP_TIMEOUT, defaultTimeout);
Jeff Sharkey104344e2011-07-10 14:20:41 -070075
76 final String server = secureServer != null ? secureServer : defaultServer;
77 sSingleton = new NtpTrustedTime(server, timeout);
Lorenzo Colittidf590532015-01-22 22:35:33 +090078 sContext = context;
Jeff Sharkey104344e2011-07-10 14:20:41 -070079 }
80
81 return sSingleton;
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070082 }
83
Jeff Sharkey023c05a2012-09-14 13:09:57 -070084 @Override
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010085 @UnsupportedAppUsage
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070086 public boolean forceRefresh() {
Kurt Marcinkiewiczdfdd82e2018-03-05 14:45:04 -080087 // We can't do this at initialization time: ConnectivityService might not be running yet.
88 synchronized (this) {
89 if (mCM == null) {
90 mCM = sContext.getSystemService(ConnectivityManager.class);
91 }
92 }
93
94 final Network network = mCM == null ? null : mCM.getActiveNetwork();
95 return forceRefresh(network);
96 }
97
98 public boolean forceRefresh(Network network) {
Young-Ho Cha0eda7df2015-01-07 11:24:35 +090099 if (TextUtils.isEmpty(mServer)) {
Jeff Sharkeyf0558292011-05-04 11:47:39 -0700100 // missing server, so no trusted time available
101 return false;
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700102 }
103
Lorenzo Colittidf590532015-01-22 22:35:33 +0900104 // We can't do this at initialization time: ConnectivityService might not be running yet.
105 synchronized (this) {
106 if (mCM == null) {
Kurt Marcinkiewiczdfdd82e2018-03-05 14:45:04 -0800107 mCM = sContext.getSystemService(ConnectivityManager.class);
Lorenzo Colittidf590532015-01-22 22:35:33 +0900108 }
109 }
110
Kurt Marcinkiewiczdfdd82e2018-03-05 14:45:04 -0800111 final NetworkInfo ni = mCM == null ? null : mCM.getNetworkInfo(network);
Lorenzo Colittidf590532015-01-22 22:35:33 +0900112 if (ni == null || !ni.isConnected()) {
113 if (LOGD) Log.d(TAG, "forceRefresh: no connectivity");
114 return false;
115 }
116
117
Jeff Sharkey104344e2011-07-10 14:20:41 -0700118 if (LOGD) Log.d(TAG, "forceRefresh() from cache miss");
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700119 final SntpClient client = new SntpClient();
Kurt Marcinkiewiczdfdd82e2018-03-05 14:45:04 -0800120 if (client.requestTime(mServer, (int) mTimeout, network)) {
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700121 mHasCache = true;
122 mCachedNtpTime = client.getNtpTime();
123 mCachedNtpElapsedRealtime = client.getNtpTimeReference();
124 mCachedNtpCertainty = client.getRoundTripTime() / 2;
125 return true;
126 } else {
127 return false;
128 }
129 }
130
Jeff Sharkey023c05a2012-09-14 13:09:57 -0700131 @Override
Mathew Inwood4eb56ab2018-08-14 17:24:32 +0100132 @UnsupportedAppUsage
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700133 public boolean hasCache() {
134 return mHasCache;
135 }
136
Jeff Sharkey023c05a2012-09-14 13:09:57 -0700137 @Override
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700138 public long getCacheAge() {
139 if (mHasCache) {
140 return SystemClock.elapsedRealtime() - mCachedNtpElapsedRealtime;
141 } else {
142 return Long.MAX_VALUE;
143 }
144 }
145
Jeff Sharkey023c05a2012-09-14 13:09:57 -0700146 @Override
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700147 public long getCacheCertainty() {
148 if (mHasCache) {
149 return mCachedNtpCertainty;
150 } else {
151 return Long.MAX_VALUE;
152 }
153 }
154
Jeff Sharkey023c05a2012-09-14 13:09:57 -0700155 @Override
Mathew Inwood4eb56ab2018-08-14 17:24:32 +0100156 @UnsupportedAppUsage
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700157 public long currentTimeMillis() {
158 if (!mHasCache) {
159 throw new IllegalStateException("Missing authoritative time source");
160 }
Jeff Sharkey104344e2011-07-10 14:20:41 -0700161 if (LOGD) Log.d(TAG, "currentTimeMillis() cache hit");
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700162
163 // current time is age after the last ntp cache; callers who
164 // want fresh values will hit makeAuthoritative() first.
165 return mCachedNtpTime + getCacheAge();
166 }
Jeff Sharkey104344e2011-07-10 14:20:41 -0700167
Mathew Inwood4eb56ab2018-08-14 17:24:32 +0100168 @UnsupportedAppUsage
Jeff Sharkey104344e2011-07-10 14:20:41 -0700169 public long getCachedNtpTime() {
170 if (LOGD) Log.d(TAG, "getCachedNtpTime() cache hit");
171 return mCachedNtpTime;
172 }
173
Mathew Inwood4eb56ab2018-08-14 17:24:32 +0100174 @UnsupportedAppUsage
Jeff Sharkey104344e2011-07-10 14:20:41 -0700175 public long getCachedNtpTimeReference() {
176 return mCachedNtpElapsedRealtime;
177 }
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700178}