blob: ed2d3c60fcd04d97825c7db684c121443d0434ec [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
Jeff Sharkey104344e2011-07-10 14:20:41 -070019import android.content.ContentResolver;
20import android.content.Context;
21import android.content.res.Resources;
Lorenzo Colittidf590532015-01-22 22:35:33 +090022import android.net.ConnectivityManager;
23import android.net.NetworkInfo;
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070024import android.net.SntpClient;
25import android.os.SystemClock;
Jeff Sharkey104344e2011-07-10 14:20:41 -070026import android.provider.Settings;
Young-Ho Cha0eda7df2015-01-07 11:24:35 +090027import android.text.TextUtils;
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070028
29/**
Jeff Sharkey104344e2011-07-10 14:20:41 -070030 * {@link TrustedTime} that connects with a remote NTP server as its trusted
31 * time source.
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070032 *
33 * @hide
34 */
35public class NtpTrustedTime implements TrustedTime {
Jeff Sharkey104344e2011-07-10 14:20:41 -070036 private static final String TAG = "NtpTrustedTime";
37 private static final boolean LOGD = false;
38
39 private static NtpTrustedTime sSingleton;
Lorenzo Colittidf590532015-01-22 22:35:33 +090040 private static Context sContext;
Jeff Sharkey104344e2011-07-10 14:20:41 -070041
42 private final String mServer;
43 private final long mTimeout;
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070044
Lorenzo Colittidf590532015-01-22 22:35:33 +090045 private ConnectivityManager mCM;
46
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070047 private boolean mHasCache;
48 private long mCachedNtpTime;
49 private long mCachedNtpElapsedRealtime;
50 private long mCachedNtpCertainty;
51
Jeff Sharkey104344e2011-07-10 14:20:41 -070052 private NtpTrustedTime(String server, long timeout) {
53 if (LOGD) Log.d(TAG, "creating NtpTrustedTime using " + server);
54 mServer = server;
55 mTimeout = timeout;
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070056 }
57
Jeff Sharkey104344e2011-07-10 14:20:41 -070058 public static synchronized NtpTrustedTime getInstance(Context context) {
59 if (sSingleton == null) {
60 final Resources res = context.getResources();
61 final ContentResolver resolver = context.getContentResolver();
62
63 final String defaultServer = res.getString(
64 com.android.internal.R.string.config_ntpServer);
65 final long defaultTimeout = res.getInteger(
66 com.android.internal.R.integer.config_ntpTimeout);
67
Jeff Sharkeye6e61972012-09-14 13:47:51 -070068 final String secureServer = Settings.Global.getString(
Jeff Sharkey023c05a2012-09-14 13:09:57 -070069 resolver, Settings.Global.NTP_SERVER);
Jeff Sharkeye6e61972012-09-14 13:47:51 -070070 final long timeout = Settings.Global.getLong(
Jeff Sharkey023c05a2012-09-14 13:09:57 -070071 resolver, Settings.Global.NTP_TIMEOUT, defaultTimeout);
Jeff Sharkey104344e2011-07-10 14:20:41 -070072
73 final String server = secureServer != null ? secureServer : defaultServer;
74 sSingleton = new NtpTrustedTime(server, timeout);
Lorenzo Colittidf590532015-01-22 22:35:33 +090075 sContext = context;
Jeff Sharkey104344e2011-07-10 14:20:41 -070076 }
77
78 return sSingleton;
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070079 }
80
Jeff Sharkey023c05a2012-09-14 13:09:57 -070081 @Override
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070082 public boolean forceRefresh() {
Young-Ho Cha0eda7df2015-01-07 11:24:35 +090083 if (TextUtils.isEmpty(mServer)) {
Jeff Sharkeyf0558292011-05-04 11:47:39 -070084 // missing server, so no trusted time available
85 return false;
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -070086 }
87
Lorenzo Colittidf590532015-01-22 22:35:33 +090088 // We can't do this at initialization time: ConnectivityService might not be running yet.
89 synchronized (this) {
90 if (mCM == null) {
91 mCM = (ConnectivityManager) sContext.getSystemService(Context.CONNECTIVITY_SERVICE);
92 }
93 }
94
95 final NetworkInfo ni = mCM == null ? null : mCM.getActiveNetworkInfo();
96 if (ni == null || !ni.isConnected()) {
97 if (LOGD) Log.d(TAG, "forceRefresh: no connectivity");
98 return false;
99 }
100
101
Jeff Sharkey104344e2011-07-10 14:20:41 -0700102 if (LOGD) Log.d(TAG, "forceRefresh() from cache miss");
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700103 final SntpClient client = new SntpClient();
Jeff Sharkey104344e2011-07-10 14:20:41 -0700104 if (client.requestTime(mServer, (int) mTimeout)) {
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700105 mHasCache = true;
106 mCachedNtpTime = client.getNtpTime();
107 mCachedNtpElapsedRealtime = client.getNtpTimeReference();
108 mCachedNtpCertainty = client.getRoundTripTime() / 2;
109 return true;
110 } else {
111 return false;
112 }
113 }
114
Jeff Sharkey023c05a2012-09-14 13:09:57 -0700115 @Override
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700116 public boolean hasCache() {
117 return mHasCache;
118 }
119
Jeff Sharkey023c05a2012-09-14 13:09:57 -0700120 @Override
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700121 public long getCacheAge() {
122 if (mHasCache) {
123 return SystemClock.elapsedRealtime() - mCachedNtpElapsedRealtime;
124 } else {
125 return Long.MAX_VALUE;
126 }
127 }
128
Jeff Sharkey023c05a2012-09-14 13:09:57 -0700129 @Override
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700130 public long getCacheCertainty() {
131 if (mHasCache) {
132 return mCachedNtpCertainty;
133 } else {
134 return Long.MAX_VALUE;
135 }
136 }
137
Jeff Sharkey023c05a2012-09-14 13:09:57 -0700138 @Override
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700139 public long currentTimeMillis() {
140 if (!mHasCache) {
141 throw new IllegalStateException("Missing authoritative time source");
142 }
Jeff Sharkey104344e2011-07-10 14:20:41 -0700143 if (LOGD) Log.d(TAG, "currentTimeMillis() cache hit");
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700144
145 // current time is age after the last ntp cache; callers who
146 // want fresh values will hit makeAuthoritative() first.
147 return mCachedNtpTime + getCacheAge();
148 }
Jeff Sharkey104344e2011-07-10 14:20:41 -0700149
150 public long getCachedNtpTime() {
151 if (LOGD) Log.d(TAG, "getCachedNtpTime() cache hit");
152 return mCachedNtpTime;
153 }
154
155 public long getCachedNtpTimeReference() {
156 return mCachedNtpElapsedRealtime;
157 }
Jeff Sharkeyb7342ac2011-04-25 23:44:11 -0700158}