ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.android.server.timedetector;
 
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.timedetector.TimeSignal;
import android.content.Intent;
import android.util.TimestampedValue;
 
import java.io.PrintWriter;
 
/**
 * The interface for classes that implement the time detection algorithm used by the
 * TimeDetectorService. The TimeDetectorService handles thread safety: all calls to implementations
 * of this interface can be assumed to be single threaded (though the thread used may vary).
 *
 * @hide
 */
// @NotThreadSafe
public interface TimeDetectorStrategy {
 
    /**
     * The interface used by the strategy to interact with the surrounding service.
     */
    interface Callback {
 
        /**
         * The absolute threshold below which the system clock need not be updated. i.e. if setting
         * the system clock would adjust it by less than this (either backwards or forwards) then it
         * need not be set.
         */
        int systemClockUpdateThresholdMillis();
 
        /** Returns true if automatic time detection is enabled. */
        boolean isTimeDetectionEnabled();
 
        /** Acquire a suitable wake lock. Must be followed by {@link #releaseWakeLock()} */
        void acquireWakeLock();
 
        /** Returns the elapsedRealtimeMillis clock value. The WakeLock must be held. */
        long elapsedRealtimeMillis();
 
        /** Returns the system clock value. The WakeLock must be held. */
        long systemClockMillis();
 
        /** Sets the device system clock. The WakeLock must be held. */
        void setSystemClock(long newTimeMillis);
 
        /** Release the wake lock acquired by a call to {@link #acquireWakeLock()}. */
        void releaseWakeLock();
 
        /** Send the supplied intent as a stick broadcast. */
        void sendStickyBroadcast(@NonNull Intent intent);
    }
 
    /** Initialize the strategy. */
    void initialize(@NonNull Callback callback);
 
    /** Process the suggested time. */
    void suggestTime(@NonNull TimeSignal timeSignal);
 
    /** Handle the auto-time setting being toggled on or off. */
    void handleAutoTimeDetectionToggle(boolean enabled);
 
    /** Dump debug information. */
    void dump(@NonNull PrintWriter pw, @Nullable String[] args);
 
    // Utility methods below are to be moved to a better home when one becomes more obvious.
 
    /**
     * Adjusts the supplied time value by applying the difference between the reference time
     * supplied and the reference time associated with the time.
     */
    static long getTimeAt(@NonNull TimestampedValue<Long> timeValue, long referenceClockMillisNow) {
        return (referenceClockMillisNow - timeValue.getReferenceTimeMillis())
                + timeValue.getValue();
    }
}