blob: 32cee2de3d75d4b549b101a559fb4d4faa0d3a86 [file] [log] [blame]
Neil Fuller4773b9d2018-06-08 18:44:49 +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 com.android.server.timedetector;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
Neil Fuller3aedd492019-11-23 11:33:57 +000021import android.app.timedetector.ManualTimeSuggestion;
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010022import android.app.timedetector.PhoneTimeSuggestion;
Neil Fuller4980bbc2018-06-12 21:06:20 +010023import android.content.Intent;
Neil Fuller4773b9d2018-06-08 18:44:49 +010024import android.util.TimestampedValue;
25
Neil Fuller4773b9d2018-06-08 18:44:49 +010026import java.io.PrintWriter;
27
28/**
29 * The interface for classes that implement the time detection algorithm used by the
Neil Fuller40cf2952019-11-28 09:47:30 +000030 * TimeDetectorService.
31 *
32 * <p>Most calls will be handled by a single thread but that is not true for all calls. For example
33 * {@link #dump(PrintWriter, String[])}) may be called on a different thread so implementations must
34 * handle thread safety.
Neil Fuller4773b9d2018-06-08 18:44:49 +010035 *
36 * @hide
37 */
38public interface TimeDetectorStrategy {
39
Neil Fuller4980bbc2018-06-12 21:06:20 +010040 /**
41 * The interface used by the strategy to interact with the surrounding service.
42 */
Neil Fuller4773b9d2018-06-08 18:44:49 +010043 interface Callback {
Neil Fuller4980bbc2018-06-12 21:06:20 +010044
45 /**
46 * The absolute threshold below which the system clock need not be updated. i.e. if setting
47 * the system clock would adjust it by less than this (either backwards or forwards) then it
48 * need not be set.
49 */
50 int systemClockUpdateThresholdMillis();
51
52 /** Returns true if automatic time detection is enabled. */
Neil Fuller3aedd492019-11-23 11:33:57 +000053 boolean isAutoTimeDetectionEnabled();
Neil Fuller4980bbc2018-06-12 21:06:20 +010054
55 /** Acquire a suitable wake lock. Must be followed by {@link #releaseWakeLock()} */
56 void acquireWakeLock();
57
58 /** Returns the elapsedRealtimeMillis clock value. The WakeLock must be held. */
59 long elapsedRealtimeMillis();
60
61 /** Returns the system clock value. The WakeLock must be held. */
62 long systemClockMillis();
63
64 /** Sets the device system clock. The WakeLock must be held. */
65 void setSystemClock(long newTimeMillis);
66
67 /** Release the wake lock acquired by a call to {@link #acquireWakeLock()}. */
68 void releaseWakeLock();
69
70 /** Send the supplied intent as a stick broadcast. */
71 void sendStickyBroadcast(@NonNull Intent intent);
Neil Fuller4773b9d2018-06-08 18:44:49 +010072 }
73
Neil Fuller4980bbc2018-06-12 21:06:20 +010074 /** Initialize the strategy. */
Neil Fuller4773b9d2018-06-08 18:44:49 +010075 void initialize(@NonNull Callback callback);
Neil Fuller4980bbc2018-06-12 21:06:20 +010076
Neil Fuller3aedd492019-11-23 11:33:57 +000077 /** Process the suggested time from telephony sources. */
Neil Fulleraf3eeaf2019-10-15 14:37:37 +010078 void suggestPhoneTime(@NonNull PhoneTimeSuggestion timeSuggestion);
Neil Fuller4980bbc2018-06-12 21:06:20 +010079
Neil Fuller3aedd492019-11-23 11:33:57 +000080 /** Process the suggested manually entered time. */
81 void suggestManualTime(@NonNull ManualTimeSuggestion timeSuggestion);
82
Neil Fuller4980bbc2018-06-12 21:06:20 +010083 /** Handle the auto-time setting being toggled on or off. */
Neil Fuller40cf2952019-11-28 09:47:30 +000084 void handleAutoTimeDetectionChanged();
Neil Fuller4980bbc2018-06-12 21:06:20 +010085
86 /** Dump debug information. */
87 void dump(@NonNull PrintWriter pw, @Nullable String[] args);
Neil Fuller4773b9d2018-06-08 18:44:49 +010088
89 // Utility methods below are to be moved to a better home when one becomes more obvious.
90
91 /**
92 * Adjusts the supplied time value by applying the difference between the reference time
93 * supplied and the reference time associated with the time.
94 */
95 static long getTimeAt(@NonNull TimestampedValue<Long> timeValue, long referenceClockMillisNow) {
96 return (referenceClockMillisNow - timeValue.getReferenceTimeMillis())
97 + timeValue.getValue();
98 }
99}