blob: 7c29f017c02babcf3200329406ea2e710f047683 [file] [log] [blame]
Neil Fullerb5579072018-05-30 14:35:24 +01001/*
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
17package android.app.timedetector;
18
Neil Fuller4773b9d2018-06-08 18:44:49 +010019import android.annotation.NonNull;
Neil Fuller3aedd492019-11-23 11:33:57 +000020import android.annotation.RequiresPermission;
Neil Fuller2be7ac42020-01-07 11:09:29 +000021import android.annotation.SystemApi;
Neil Fullerb5579072018-05-30 14:35:24 +010022import android.annotation.SystemService;
23import android.content.Context;
24import android.os.RemoteException;
25import android.os.ServiceManager;
26import android.os.ServiceManager.ServiceNotFoundException;
Neil Fuller3aedd492019-11-23 11:33:57 +000027import android.os.SystemClock;
Neil Fullerd1590152020-01-08 16:31:44 +000028import android.os.TimestampedValue;
Neil Fullerb5579072018-05-30 14:35:24 +010029import android.util.Log;
30
31/**
32 * The interface through which system components can send signals to the TimeDetectorService.
Neil Fuller2be7ac42020-01-07 11:09:29 +000033 *
34 * <p>This class is marked non-final for mockito.
Neil Fullerb5579072018-05-30 14:35:24 +010035 * @hide
36 */
Neil Fuller2be7ac42020-01-07 11:09:29 +000037@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
Neil Fullerb5579072018-05-30 14:35:24 +010038@SystemService(Context.TIME_DETECTOR_SERVICE)
Neil Fuller3aedd492019-11-23 11:33:57 +000039public class TimeDetector {
Neil Fullerb5579072018-05-30 14:35:24 +010040 private static final String TAG = "timedetector.TimeDetector";
41 private static final boolean DEBUG = false;
42
43 private final ITimeDetectorService mITimeDetectorService;
44
Neil Fuller2be7ac42020-01-07 11:09:29 +000045 /** @hide */
Neil Fullerb5579072018-05-30 14:35:24 +010046 public TimeDetector() throws ServiceNotFoundException {
47 mITimeDetectorService = ITimeDetectorService.Stub.asInterface(
48 ServiceManager.getServiceOrThrow(Context.TIME_DETECTOR_SERVICE));
49 }
50
51 /**
Neil Fuller3aedd492019-11-23 11:33:57 +000052 * 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 Fullerb5579072018-05-30 14:35:24 +010055 */
Neil Fuller09e8b7d2019-11-21 13:11:04 +000056 @RequiresPermission(android.Manifest.permission.SUGGEST_PHONE_TIME_AND_ZONE)
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010057 public void suggestPhoneTime(@NonNull PhoneTimeSuggestion timeSuggestion) {
Neil Fullerb5579072018-05-30 14:35:24 +010058 if (DEBUG) {
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010059 Log.d(TAG, "suggestPhoneTime called: " + timeSuggestion);
Neil Fullerb5579072018-05-30 14:35:24 +010060 }
61 try {
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010062 mITimeDetectorService.suggestPhoneTime(timeSuggestion);
Neil Fullerb5579072018-05-30 14:35:24 +010063 } catch (RemoteException e) {
64 throw e.rethrowFromSystemServer();
65 }
66 }
67
Neil Fuller3aedd492019-11-23 11:33:57 +000068 /**
69 * Suggests the user's manually entered current time to the detector.
Neil Fuller2be7ac42020-01-07 11:09:29 +000070 *
71 * @hide
Neil Fuller3aedd492019-11-23 11:33:57 +000072 */
Neil Fuller09e8b7d2019-11-21 13:11:04 +000073 @RequiresPermission(android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE)
Neil Fuller3aedd492019-11-23 11:33:57 +000074 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 Fuller2be7ac42020-01-07 11:09:29 +000087 *
88 * @hide
Neil Fuller3aedd492019-11-23 11:33:57 +000089 */
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 Fuller4ab8a192019-12-16 16:54:06 +000097
98 /**
99 * Suggests the time according to a network time source like NTP.
Neil Fuller2be7ac42020-01-07 11:09:29 +0000100 *
101 * @hide
Neil Fuller4ab8a192019-12-16 16:54:06 +0000102 */
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 Fullerb5579072018-05-30 14:35:24 +0100114}