blob: 48d5cd2d65a58e232be12c170ea5473ac1af67ce [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 Fullerb5579072018-05-30 14:35:24 +010021import android.annotation.SystemService;
22import android.content.Context;
23import android.os.RemoteException;
24import android.os.ServiceManager;
25import android.os.ServiceManager.ServiceNotFoundException;
Neil Fuller3aedd492019-11-23 11:33:57 +000026import android.os.SystemClock;
Neil Fullerb5579072018-05-30 14:35:24 +010027import android.util.Log;
Neil Fuller3aedd492019-11-23 11:33:57 +000028import android.util.TimestampedValue;
Neil Fullerb5579072018-05-30 14:35:24 +010029
30/**
31 * The interface through which system components can send signals to the TimeDetectorService.
32 * @hide
33 */
34@SystemService(Context.TIME_DETECTOR_SERVICE)
Neil Fuller3aedd492019-11-23 11:33:57 +000035public class TimeDetector {
Neil Fullerb5579072018-05-30 14:35:24 +010036 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 Fuller3aedd492019-11-23 11:33:57 +000047 * 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 Fullerb5579072018-05-30 14:35:24 +010050 */
Neil Fuller3aedd492019-11-23 11:33:57 +000051 @RequiresPermission(android.Manifest.permission.SET_TIME)
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010052 public void suggestPhoneTime(@NonNull PhoneTimeSuggestion timeSuggestion) {
Neil Fullerb5579072018-05-30 14:35:24 +010053 if (DEBUG) {
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010054 Log.d(TAG, "suggestPhoneTime called: " + timeSuggestion);
Neil Fullerb5579072018-05-30 14:35:24 +010055 }
56 try {
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010057 mITimeDetectorService.suggestPhoneTime(timeSuggestion);
Neil Fullerb5579072018-05-30 14:35:24 +010058 } catch (RemoteException e) {
59 throw e.rethrowFromSystemServer();
60 }
61 }
62
Neil Fuller3aedd492019-11-23 11:33:57 +000063 /**
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 Fullerb5579072018-05-30 14:35:24 +010088}