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 | |
| 19 | import android.net.SntpClient; |
| 20 | import android.os.SystemClock; |
| 21 | |
| 22 | /** |
| 23 | * {@link TrustedTime} that connects with a remote NTP server as its remote |
| 24 | * trusted time source. |
| 25 | * |
| 26 | * @hide |
| 27 | */ |
| 28 | public class NtpTrustedTime implements TrustedTime { |
| 29 | private String mNtpServer; |
| 30 | private long mNtpTimeout; |
| 31 | |
| 32 | private boolean mHasCache; |
| 33 | private long mCachedNtpTime; |
| 34 | private long mCachedNtpElapsedRealtime; |
| 35 | private long mCachedNtpCertainty; |
| 36 | |
| 37 | public NtpTrustedTime() { |
| 38 | } |
| 39 | |
| 40 | public void setNtpServer(String server, long timeout) { |
| 41 | mNtpServer = server; |
| 42 | mNtpTimeout = timeout; |
| 43 | } |
| 44 | |
| 45 | /** {@inheritDoc} */ |
| 46 | public boolean forceRefresh() { |
| 47 | if (mNtpServer == null) { |
Jeff Sharkey | f055829 | 2011-05-04 11:47:39 -0700 | [diff] [blame^] | 48 | // missing server, so no trusted time available |
| 49 | return false; |
Jeff Sharkey | b7342ac | 2011-04-25 23:44:11 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | final SntpClient client = new SntpClient(); |
| 53 | if (client.requestTime(mNtpServer, (int) mNtpTimeout)) { |
| 54 | mHasCache = true; |
| 55 | mCachedNtpTime = client.getNtpTime(); |
| 56 | mCachedNtpElapsedRealtime = client.getNtpTimeReference(); |
| 57 | mCachedNtpCertainty = client.getRoundTripTime() / 2; |
| 58 | return true; |
| 59 | } else { |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** {@inheritDoc} */ |
| 65 | public boolean hasCache() { |
| 66 | return mHasCache; |
| 67 | } |
| 68 | |
| 69 | /** {@inheritDoc} */ |
| 70 | public long getCacheAge() { |
| 71 | if (mHasCache) { |
| 72 | return SystemClock.elapsedRealtime() - mCachedNtpElapsedRealtime; |
| 73 | } else { |
| 74 | return Long.MAX_VALUE; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** {@inheritDoc} */ |
| 79 | public long getCacheCertainty() { |
| 80 | if (mHasCache) { |
| 81 | return mCachedNtpCertainty; |
| 82 | } else { |
| 83 | return Long.MAX_VALUE; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** {@inheritDoc} */ |
| 88 | public long currentTimeMillis() { |
| 89 | if (!mHasCache) { |
| 90 | throw new IllegalStateException("Missing authoritative time source"); |
| 91 | } |
| 92 | |
| 93 | // current time is age after the last ntp cache; callers who |
| 94 | // want fresh values will hit makeAuthoritative() first. |
| 95 | return mCachedNtpTime + getCacheAge(); |
| 96 | } |
| 97 | } |