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 | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 21 | import android.annotation.SystemService; |
| 22 | import android.content.Context; |
| 23 | import android.os.RemoteException; |
| 24 | import android.os.ServiceManager; |
| 25 | import android.os.ServiceManager.ServiceNotFoundException; |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 26 | import android.os.SystemClock; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 27 | import android.util.Log; |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 28 | import android.util.TimestampedValue; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * The interface through which system components can send signals to the TimeDetectorService. |
| 32 | * @hide |
| 33 | */ |
| 34 | @SystemService(Context.TIME_DETECTOR_SERVICE) |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 35 | public class TimeDetector { |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 36 | private static final String TAG = "timedetector.TimeDetector"; |
| 37 | private static final boolean DEBUG = false; |
| 38 | |
| 39 | private final ITimeDetectorService mITimeDetectorService; |
| 40 | |
| 41 | public TimeDetector() throws ServiceNotFoundException { |
| 42 | mITimeDetectorService = ITimeDetectorService.Stub.asInterface( |
| 43 | ServiceManager.getServiceOrThrow(Context.TIME_DETECTOR_SERVICE)); |
| 44 | } |
| 45 | |
| 46 | /** |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 47 | * Suggests the current phone-signal derived time to the detector. The detector may ignore the |
| 48 | * signal if better signals are available such as those that come from more reliable sources or |
| 49 | * were determined more recently. |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 50 | */ |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 51 | @RequiresPermission(android.Manifest.permission.SET_TIME) |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 52 | public void suggestPhoneTime(@NonNull PhoneTimeSuggestion timeSuggestion) { |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 53 | if (DEBUG) { |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 54 | Log.d(TAG, "suggestPhoneTime called: " + timeSuggestion); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 55 | } |
| 56 | try { |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 57 | mITimeDetectorService.suggestPhoneTime(timeSuggestion); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 58 | } catch (RemoteException e) { |
| 59 | throw e.rethrowFromSystemServer(); |
| 60 | } |
| 61 | } |
| 62 | |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 63 | /** |
| 64 | * Suggests the user's manually entered current time to the detector. |
| 65 | */ |
| 66 | @RequiresPermission(android.Manifest.permission.SET_TIME) |
| 67 | public void suggestManualTime(@NonNull ManualTimeSuggestion timeSuggestion) { |
| 68 | if (DEBUG) { |
| 69 | Log.d(TAG, "suggestManualTime called: " + timeSuggestion); |
| 70 | } |
| 71 | try { |
| 72 | mITimeDetectorService.suggestManualTime(timeSuggestion); |
| 73 | } catch (RemoteException e) { |
| 74 | throw e.rethrowFromSystemServer(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * A shared utility method to create a {@link ManualTimeSuggestion}. |
| 80 | */ |
| 81 | public static ManualTimeSuggestion createManualTimeSuggestion(long when, String why) { |
| 82 | TimestampedValue<Long> utcTime = |
| 83 | new TimestampedValue<>(SystemClock.elapsedRealtime(), when); |
| 84 | ManualTimeSuggestion manualTimeSuggestion = new ManualTimeSuggestion(utcTime); |
| 85 | manualTimeSuggestion.addDebugInfo(why); |
| 86 | return manualTimeSuggestion; |
| 87 | } |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 88 | } |