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 | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 21 | import android.app.timedetector.PhoneTimeSuggestion; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 22 | import android.content.Intent; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 23 | import android.util.TimestampedValue; |
| 24 | |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 25 | import java.io.PrintWriter; |
| 26 | |
| 27 | /** |
| 28 | * The interface for classes that implement the time detection algorithm used by the |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 29 | * 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 Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 31 | * |
| 32 | * @hide |
| 33 | */ |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 34 | // @NotThreadSafe |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 35 | public interface TimeDetectorStrategy { |
| 36 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 37 | /** |
| 38 | * The interface used by the strategy to interact with the surrounding service. |
| 39 | */ |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 40 | interface Callback { |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 41 | |
| 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 Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 71 | /** Initialize the strategy. */ |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 72 | void initialize(@NonNull Callback callback); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 73 | |
| 74 | /** Process the suggested time. */ |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 75 | void suggestPhoneTime(@NonNull PhoneTimeSuggestion timeSuggestion); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 76 | |
| 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 Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 82 | |
| 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 | } |