Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.app.timedetector; |
| 18 | |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 20 | import android.annotation.RequiresPermission; |
Neil Fuller | 2be7ac4 | 2020-01-07 11:09:29 +0000 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 22 | import android.annotation.SystemService; |
| 23 | import android.content.Context; |
| 24 | import android.os.RemoteException; |
| 25 | import android.os.ServiceManager; |
| 26 | import android.os.ServiceManager.ServiceNotFoundException; |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 27 | import android.os.SystemClock; |
Neil Fuller | d159015 | 2020-01-08 16:31:44 +0000 | [diff] [blame] | 28 | import android.os.TimestampedValue; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | |
| 31 | /** |
| 32 | * The interface through which system components can send signals to the TimeDetectorService. |
Neil Fuller | 2be7ac4 | 2020-01-07 11:09:29 +0000 | [diff] [blame] | 33 | * |
| 34 | * <p>This class is marked non-final for mockito. |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 35 | * @hide |
| 36 | */ |
Neil Fuller | 2be7ac4 | 2020-01-07 11:09:29 +0000 | [diff] [blame] | 37 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 38 | @SystemService(Context.TIME_DETECTOR_SERVICE) |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 39 | public class TimeDetector { |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 40 | private static final String TAG = "timedetector.TimeDetector"; |
| 41 | private static final boolean DEBUG = false; |
| 42 | |
| 43 | private final ITimeDetectorService mITimeDetectorService; |
| 44 | |
Neil Fuller | 2be7ac4 | 2020-01-07 11:09:29 +0000 | [diff] [blame] | 45 | /** @hide */ |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 46 | public TimeDetector() throws ServiceNotFoundException { |
| 47 | mITimeDetectorService = ITimeDetectorService.Stub.asInterface( |
| 48 | ServiceManager.getServiceOrThrow(Context.TIME_DETECTOR_SERVICE)); |
| 49 | } |
| 50 | |
| 51 | /** |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 52 | * Suggests the current phone-signal derived time to the detector. The detector may ignore the |
| 53 | * signal if better signals are available such as those that come from more reliable sources or |
| 54 | * were determined more recently. |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 55 | */ |
Neil Fuller | 09e8b7d | 2019-11-21 13:11:04 +0000 | [diff] [blame] | 56 | @RequiresPermission(android.Manifest.permission.SUGGEST_PHONE_TIME_AND_ZONE) |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 57 | public void suggestPhoneTime(@NonNull PhoneTimeSuggestion timeSuggestion) { |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 58 | if (DEBUG) { |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 59 | Log.d(TAG, "suggestPhoneTime called: " + timeSuggestion); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 60 | } |
| 61 | try { |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 62 | mITimeDetectorService.suggestPhoneTime(timeSuggestion); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 63 | } catch (RemoteException e) { |
| 64 | throw e.rethrowFromSystemServer(); |
| 65 | } |
| 66 | } |
| 67 | |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 68 | /** |
| 69 | * Suggests the user's manually entered current time to the detector. |
Neil Fuller | 2be7ac4 | 2020-01-07 11:09:29 +0000 | [diff] [blame] | 70 | * |
| 71 | * @hide |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 72 | */ |
Neil Fuller | 09e8b7d | 2019-11-21 13:11:04 +0000 | [diff] [blame] | 73 | @RequiresPermission(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE) |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 74 | public void suggestManualTime(@NonNull ManualTimeSuggestion timeSuggestion) { |
| 75 | if (DEBUG) { |
| 76 | Log.d(TAG, "suggestManualTime called: " + timeSuggestion); |
| 77 | } |
| 78 | try { |
| 79 | mITimeDetectorService.suggestManualTime(timeSuggestion); |
| 80 | } catch (RemoteException e) { |
| 81 | throw e.rethrowFromSystemServer(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * A shared utility method to create a {@link ManualTimeSuggestion}. |
Neil Fuller | 2be7ac4 | 2020-01-07 11:09:29 +0000 | [diff] [blame] | 87 | * |
| 88 | * @hide |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 89 | */ |
| 90 | public static ManualTimeSuggestion createManualTimeSuggestion(long when, String why) { |
| 91 | TimestampedValue<Long> utcTime = |
| 92 | new TimestampedValue<>(SystemClock.elapsedRealtime(), when); |
| 93 | ManualTimeSuggestion manualTimeSuggestion = new ManualTimeSuggestion(utcTime); |
| 94 | manualTimeSuggestion.addDebugInfo(why); |
| 95 | return manualTimeSuggestion; |
| 96 | } |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Suggests the time according to a network time source like NTP. |
Neil Fuller | 2be7ac4 | 2020-01-07 11:09:29 +0000 | [diff] [blame] | 100 | * |
| 101 | * @hide |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 102 | */ |
| 103 | @RequiresPermission(android.Manifest.permission.SET_TIME) |
| 104 | public void suggestNetworkTime(NetworkTimeSuggestion timeSuggestion) { |
| 105 | if (DEBUG) { |
| 106 | Log.d(TAG, "suggestNetworkTime called: " + timeSuggestion); |
| 107 | } |
| 108 | try { |
| 109 | mITimeDetectorService.suggestNetworkTime(timeSuggestion); |
| 110 | } catch (RemoteException e) { |
| 111 | throw e.rethrowFromSystemServer(); |
| 112 | } |
| 113 | } |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 114 | } |