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.app.AlarmManager; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 21 | import android.content.ContentResolver; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 22 | import android.content.Context; |
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.os.PowerManager; |
| 25 | import android.os.SystemClock; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 26 | import android.os.SystemProperties; |
| 27 | import android.os.UserHandle; |
| 28 | import android.provider.Settings; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 29 | import android.util.Slog; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 30 | |
| 31 | import java.util.Objects; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * The real implementation of {@link TimeDetectorStrategy.Callback} used on device. |
| 35 | */ |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 36 | public final class TimeDetectorStrategyCallbackImpl implements TimeDetectorStrategy.Callback { |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 37 | |
| 38 | private final static String TAG = "timedetector.TimeDetectorStrategyCallbackImpl"; |
| 39 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 40 | private static final int SYSTEM_CLOCK_UPDATE_THRESHOLD_MILLIS_DEFAULT = 2 * 1000; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 41 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 42 | /** |
| 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 Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 58 | PowerManager powerManager = context.getSystemService(PowerManager.class); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 59 | mWakeLock = Objects.requireNonNull( |
| 60 | powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG)); |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 61 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 62 | mAlarmManager = Objects.requireNonNull(context.getSystemService(AlarmManager.class)); |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 63 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 64 | mSystemClockUpdateThresholdMillis = |
| 65 | SystemProperties.getInt("ro.sys.time_detector_update_diff", |
| 66 | SYSTEM_CLOCK_UPDATE_THRESHOLD_MILLIS_DEFAULT); |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 70 | public int systemClockUpdateThresholdMillis() { |
| 71 | return mSystemClockUpdateThresholdMillis; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean isTimeDetectionEnabled() { |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 76 | try { |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 77 | 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 Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | } |