blob: 77b9e62810866de321f70c327a77e36cb74d91ec [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.app.AlarmManager;
Neil Fuller4980bbc2018-06-12 21:06:20 +010021import android.content.ContentResolver;
Neil Fuller4773b9d2018-06-08 18:44:49 +010022import android.content.Context;
Neil Fuller4980bbc2018-06-12 21:06:20 +010023import android.content.Intent;
Neil Fuller4773b9d2018-06-08 18:44:49 +010024import android.os.PowerManager;
25import android.os.SystemClock;
Neil Fuller4980bbc2018-06-12 21:06:20 +010026import android.os.SystemProperties;
27import android.os.UserHandle;
28import android.provider.Settings;
Neil Fuller4773b9d2018-06-08 18:44:49 +010029import android.util.Slog;
Neil Fuller4980bbc2018-06-12 21:06:20 +010030
31import java.util.Objects;
Neil Fuller4773b9d2018-06-08 18:44:49 +010032
33/**
34 * The real implementation of {@link TimeDetectorStrategy.Callback} used on device.
35 */
Neil Fuller4980bbc2018-06-12 21:06:20 +010036public final class TimeDetectorStrategyCallbackImpl implements TimeDetectorStrategy.Callback {
Neil Fuller4773b9d2018-06-08 18:44:49 +010037
38 private final static String TAG = "timedetector.TimeDetectorStrategyCallbackImpl";
39
Neil Fuller4980bbc2018-06-12 21:06:20 +010040 private static final int SYSTEM_CLOCK_UPDATE_THRESHOLD_MILLIS_DEFAULT = 2 * 1000;
Neil Fuller4773b9d2018-06-08 18:44:49 +010041
Neil Fuller4980bbc2018-06-12 21:06:20 +010042 /**
43 * If a newly calculated system clock time and the current system clock time differs by this or
44 * more the system clock will actually be updated. Used to prevent the system clock being set
45 * for only minor differences.
46 */
47 private final int mSystemClockUpdateThresholdMillis;
48
49 @NonNull private final Context mContext;
50 @NonNull private final ContentResolver mContentResolver;
51 @NonNull private final PowerManager.WakeLock mWakeLock;
52 @NonNull private final AlarmManager mAlarmManager;
53
54 public TimeDetectorStrategyCallbackImpl(@NonNull Context context) {
55 mContext = Objects.requireNonNull(context);
56 mContentResolver = Objects.requireNonNull(context.getContentResolver());
57
Neil Fuller4773b9d2018-06-08 18:44:49 +010058 PowerManager powerManager = context.getSystemService(PowerManager.class);
Neil Fuller4980bbc2018-06-12 21:06:20 +010059 mWakeLock = Objects.requireNonNull(
60 powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG));
Neil Fuller4773b9d2018-06-08 18:44:49 +010061
Neil Fuller4980bbc2018-06-12 21:06:20 +010062 mAlarmManager = Objects.requireNonNull(context.getSystemService(AlarmManager.class));
Neil Fuller4773b9d2018-06-08 18:44:49 +010063
Neil Fuller4980bbc2018-06-12 21:06:20 +010064 mSystemClockUpdateThresholdMillis =
65 SystemProperties.getInt("ro.sys.time_detector_update_diff",
66 SYSTEM_CLOCK_UPDATE_THRESHOLD_MILLIS_DEFAULT);
Neil Fuller4773b9d2018-06-08 18:44:49 +010067 }
68
69 @Override
Neil Fuller4980bbc2018-06-12 21:06:20 +010070 public int systemClockUpdateThresholdMillis() {
71 return mSystemClockUpdateThresholdMillis;
72 }
73
74 @Override
75 public boolean isTimeDetectionEnabled() {
Neil Fuller4773b9d2018-06-08 18:44:49 +010076 try {
Neil Fuller4980bbc2018-06-12 21:06:20 +010077 return Settings.Global.getInt(mContentResolver, Settings.Global.AUTO_TIME) != 0;
78 } catch (Settings.SettingNotFoundException snfe) {
79 return true;
80 }
81 }
82
83 @Override
84 public void acquireWakeLock() {
85 if (mWakeLock.isHeld()) {
86 Slog.wtf(TAG, "WakeLock " + mWakeLock + " already held");
87 }
88 mWakeLock.acquire();
89 }
90
91 @Override
92 public long elapsedRealtimeMillis() {
93 checkWakeLockHeld();
94 return SystemClock.elapsedRealtime();
95 }
96
97 @Override
98 public long systemClockMillis() {
99 checkWakeLockHeld();
100 return System.currentTimeMillis();
101 }
102
103 @Override
104 public void setSystemClock(long newTimeMillis) {
105 checkWakeLockHeld();
106 mAlarmManager.setTime(newTimeMillis);
107 }
108
109 @Override
110 public void releaseWakeLock() {
111 checkWakeLockHeld();
112 mWakeLock.release();
113 }
114
115 @Override
116 public void sendStickyBroadcast(@NonNull Intent intent) {
117 mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
118 }
119
120 private void checkWakeLockHeld() {
121 if (!mWakeLock.isHeld()) {
122 Slog.wtf(TAG, "WakeLock " + mWakeLock + " not held");
Neil Fuller4773b9d2018-06-08 18:44:49 +0100123 }
124 }
125}