Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.util; |
| 18 | |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 20 | import android.content.ContentResolver; |
| 21 | import android.content.Context; |
| 22 | import android.content.res.Resources; |
Lorenzo Colitti | df59053 | 2015-01-22 22:35:33 +0900 | [diff] [blame] | 23 | import android.net.ConnectivityManager; |
Kurt Marcinkiewicz | dfdd82e | 2018-03-05 14:45:04 -0800 | [diff] [blame] | 24 | import android.net.Network; |
Lorenzo Colitti | df59053 | 2015-01-22 22:35:33 +0900 | [diff] [blame] | 25 | import android.net.NetworkInfo; |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 26 | import android.net.SntpClient; |
| 27 | import android.os.SystemClock; |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 28 | import android.provider.Settings; |
Young-Ho Cha | 0eda7df | 2015-01-07 11:24:35 +0900 | [diff] [blame] | 29 | import android.text.TextUtils; |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 32 | * {@link TrustedTime} that connects with a remote NTP server as its trusted |
| 33 | * time source. |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 34 | * |
| 35 | * @hide |
| 36 | */ |
| 37 | public class NtpTrustedTime implements TrustedTime { |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 38 | private static final String TAG = "NtpTrustedTime"; |
| 39 | private static final boolean LOGD = false; |
| 40 | |
| 41 | private static NtpTrustedTime sSingleton; |
Lorenzo Colitti | df59053 | 2015-01-22 22:35:33 +0900 | [diff] [blame] | 42 | private static Context sContext; |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 43 | |
| 44 | private final String mServer; |
| 45 | private final long mTimeout; |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 46 | |
Lorenzo Colitti | df59053 | 2015-01-22 22:35:33 +0900 | [diff] [blame] | 47 | private ConnectivityManager mCM; |
| 48 | |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 49 | private boolean mHasCache; |
| 50 | private long mCachedNtpTime; |
| 51 | private long mCachedNtpElapsedRealtime; |
| 52 | private long mCachedNtpCertainty; |
| 53 | |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 54 | private NtpTrustedTime(String server, long timeout) { |
| 55 | if (LOGD) Log.d(TAG, "creating NtpTrustedTime using " + server); |
| 56 | mServer = server; |
| 57 | mTimeout = timeout; |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 60 | @UnsupportedAppUsage |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 61 | 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 Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 71 | final String secureServer = Settings.Global.getString( |
Jeff Sharkey | 023c05a | 2012-09-14 13:09:57 -0700 | [diff] [blame] | 72 | resolver, Settings.Global.NTP_SERVER); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 73 | final long timeout = Settings.Global.getLong( |
Jeff Sharkey | 023c05a | 2012-09-14 13:09:57 -0700 | [diff] [blame] | 74 | resolver, Settings.Global.NTP_TIMEOUT, defaultTimeout); |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 75 | |
| 76 | final String server = secureServer != null ? secureServer : defaultServer; |
| 77 | sSingleton = new NtpTrustedTime(server, timeout); |
Lorenzo Colitti | df59053 | 2015-01-22 22:35:33 +0900 | [diff] [blame] | 78 | sContext = context; |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | return sSingleton; |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Jeff Sharkey | 023c05a | 2012-09-14 13:09:57 -0700 | [diff] [blame] | 84 | @Override |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 85 | @UnsupportedAppUsage |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 86 | public boolean forceRefresh() { |
Kurt Marcinkiewicz | dfdd82e | 2018-03-05 14:45:04 -0800 | [diff] [blame] | 87 | // 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 Cha | 0eda7df | 2015-01-07 11:24:35 +0900 | [diff] [blame] | 99 | if (TextUtils.isEmpty(mServer)) { |
Jeff Sharkey | f055829 | 2011-05-04 11:47:39 -0700 | [diff] [blame] | 100 | // missing server, so no trusted time available |
| 101 | return false; |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Lorenzo Colitti | df59053 | 2015-01-22 22:35:33 +0900 | [diff] [blame] | 104 | // We can't do this at initialization time: ConnectivityService might not be running yet. |
| 105 | synchronized (this) { |
| 106 | if (mCM == null) { |
Kurt Marcinkiewicz | dfdd82e | 2018-03-05 14:45:04 -0800 | [diff] [blame] | 107 | mCM = sContext.getSystemService(ConnectivityManager.class); |
Lorenzo Colitti | df59053 | 2015-01-22 22:35:33 +0900 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Kurt Marcinkiewicz | dfdd82e | 2018-03-05 14:45:04 -0800 | [diff] [blame] | 111 | final NetworkInfo ni = mCM == null ? null : mCM.getNetworkInfo(network); |
Lorenzo Colitti | df59053 | 2015-01-22 22:35:33 +0900 | [diff] [blame] | 112 | if (ni == null || !ni.isConnected()) { |
| 113 | if (LOGD) Log.d(TAG, "forceRefresh: no connectivity"); |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 118 | if (LOGD) Log.d(TAG, "forceRefresh() from cache miss"); |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 119 | final SntpClient client = new SntpClient(); |
Kurt Marcinkiewicz | dfdd82e | 2018-03-05 14:45:04 -0800 | [diff] [blame] | 120 | if (client.requestTime(mServer, (int) mTimeout, network)) { |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 121 | 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 Sharkey | 023c05a | 2012-09-14 13:09:57 -0700 | [diff] [blame] | 131 | @Override |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 132 | @UnsupportedAppUsage |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 133 | public boolean hasCache() { |
| 134 | return mHasCache; |
| 135 | } |
| 136 | |
Jeff Sharkey | 023c05a | 2012-09-14 13:09:57 -0700 | [diff] [blame] | 137 | @Override |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 138 | public long getCacheAge() { |
| 139 | if (mHasCache) { |
| 140 | return SystemClock.elapsedRealtime() - mCachedNtpElapsedRealtime; |
| 141 | } else { |
| 142 | return Long.MAX_VALUE; |
| 143 | } |
| 144 | } |
| 145 | |
Jeff Sharkey | 023c05a | 2012-09-14 13:09:57 -0700 | [diff] [blame] | 146 | @Override |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 147 | public long getCacheCertainty() { |
| 148 | if (mHasCache) { |
| 149 | return mCachedNtpCertainty; |
| 150 | } else { |
| 151 | return Long.MAX_VALUE; |
| 152 | } |
| 153 | } |
| 154 | |
Jeff Sharkey | 023c05a | 2012-09-14 13:09:57 -0700 | [diff] [blame] | 155 | @Override |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 156 | @UnsupportedAppUsage |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 157 | public long currentTimeMillis() { |
| 158 | if (!mHasCache) { |
| 159 | throw new IllegalStateException("Missing authoritative time source"); |
| 160 | } |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 161 | if (LOGD) Log.d(TAG, "currentTimeMillis() cache hit"); |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 162 | |
| 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 Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 167 | |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 168 | @UnsupportedAppUsage |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 169 | public long getCachedNtpTime() { |
| 170 | if (LOGD) Log.d(TAG, "getCachedNtpTime() cache hit"); |
| 171 | return mCachedNtpTime; |
| 172 | } |
| 173 | |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 174 | @UnsupportedAppUsage |
Jeff Sharkey | 104344e | 2011-07-10 14:20:41 -0700 | [diff] [blame] | 175 | public long getCachedNtpTimeReference() { |
| 176 | return mCachedNtpElapsedRealtime; |
| 177 | } |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 178 | } |