blob: e050865d96c74d5fccb08e2391fe0b73e11efcfd [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;
21import android.app.timedetector.TimeSignal;
Neil Fuller4980bbc2018-06-12 21:06:20 +010022import android.content.Intent;
Neil Fuller4773b9d2018-06-08 18:44:49 +010023import android.util.TimestampedValue;
24
Neil Fuller4773b9d2018-06-08 18:44:49 +010025import java.io.PrintWriter;
26
27/**
28 * The interface for classes that implement the time detection algorithm used by the
Neil Fuller4980bbc2018-06-12 21:06:20 +010029 * TimeDetectorService. The TimeDetectorService handles thread safety: all calls to implementations
30 * of this interface can be assumed to be single threaded (though the thread used may vary).
Neil Fuller4773b9d2018-06-08 18:44:49 +010031 *
32 * @hide
33 */
Neil Fuller4980bbc2018-06-12 21:06:20 +010034// @NotThreadSafe
Neil Fuller4773b9d2018-06-08 18:44:49 +010035public interface TimeDetectorStrategy {
36
Neil Fuller4980bbc2018-06-12 21:06:20 +010037 /**
38 * The interface used by the strategy to interact with the surrounding service.
39 */
Neil Fuller4773b9d2018-06-08 18:44:49 +010040 interface Callback {
Neil Fuller4980bbc2018-06-12 21:06:20 +010041
42 /**
43 * The absolute threshold below which the system clock need not be updated. i.e. if setting
44 * the system clock would adjust it by less than this (either backwards or forwards) then it
45 * need not be set.
46 */
47 int systemClockUpdateThresholdMillis();
48
49 /** Returns true if automatic time detection is enabled. */
50 boolean isTimeDetectionEnabled();
51
52 /** Acquire a suitable wake lock. Must be followed by {@link #releaseWakeLock()} */
53 void acquireWakeLock();
54
55 /** Returns the elapsedRealtimeMillis clock value. The WakeLock must be held. */
56 long elapsedRealtimeMillis();
57
58 /** Returns the system clock value. The WakeLock must be held. */
59 long systemClockMillis();
60
61 /** Sets the device system clock. The WakeLock must be held. */
62 void setSystemClock(long newTimeMillis);
63
64 /** Release the wake lock acquired by a call to {@link #acquireWakeLock()}. */
65 void releaseWakeLock();
66
67 /** Send the supplied intent as a stick broadcast. */
68 void sendStickyBroadcast(@NonNull Intent intent);
Neil Fuller4773b9d2018-06-08 18:44:49 +010069 }
70
Neil Fuller4980bbc2018-06-12 21:06:20 +010071 /** Initialize the strategy. */
Neil Fuller4773b9d2018-06-08 18:44:49 +010072 void initialize(@NonNull Callback callback);
Neil Fuller4980bbc2018-06-12 21:06:20 +010073
74 /** Process the suggested time. */
Neil Fuller4773b9d2018-06-08 18:44:49 +010075 void suggestTime(@NonNull TimeSignal timeSignal);
Neil Fuller4980bbc2018-06-12 21:06:20 +010076
77 /** Handle the auto-time setting being toggled on or off. */
78 void handleAutoTimeDetectionToggle(boolean enabled);
79
80 /** Dump debug information. */
81 void dump(@NonNull PrintWriter pw, @Nullable String[] args);
Neil Fuller4773b9d2018-06-08 18:44:49 +010082
83 // Utility methods below are to be moved to a better home when one becomes more obvious.
84
85 /**
86 * Adjusts the supplied time value by applying the difference between the reference time
87 * supplied and the reference time associated with the time.
88 */
89 static long getTimeAt(@NonNull TimestampedValue<Long> timeValue, long referenceClockMillisNow) {
90 return (referenceClockMillisNow - timeValue.getReferenceTimeMillis())
91 + timeValue.getValue();
92 }
93}