Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +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 com.android.server.timedetector; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 21 | import android.app.timedetector.ManualTimeSuggestion; |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 22 | import android.app.timedetector.PhoneTimeSuggestion; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 23 | import android.content.Intent; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 24 | import android.util.TimestampedValue; |
| 25 | |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 26 | import java.io.PrintWriter; |
| 27 | |
| 28 | /** |
| 29 | * The interface for classes that implement the time detection algorithm used by the |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 30 | * 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 Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 35 | * |
| 36 | * @hide |
| 37 | */ |
| 38 | public interface TimeDetectorStrategy { |
| 39 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 40 | /** |
| 41 | * The interface used by the strategy to interact with the surrounding service. |
| 42 | */ |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 43 | interface Callback { |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 44 | |
| 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 Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 53 | boolean isAutoTimeDetectionEnabled(); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 54 | |
| 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 Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 74 | /** Initialize the strategy. */ |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 75 | void initialize(@NonNull Callback callback); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 76 | |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 77 | /** Process the suggested time from telephony sources. */ |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 78 | void suggestPhoneTime(@NonNull PhoneTimeSuggestion timeSuggestion); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 79 | |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 80 | /** Process the suggested manually entered time. */ |
| 81 | void suggestManualTime(@NonNull ManualTimeSuggestion timeSuggestion); |
| 82 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 83 | /** Handle the auto-time setting being toggled on or off. */ |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 84 | void handleAutoTimeDetectionChanged(); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 85 | |
| 86 | /** Dump debug information. */ |
| 87 | void dump(@NonNull PrintWriter pw, @Nullable String[] args); |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 88 | |
| 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 | } |