Neil Fuller | b557907 | 2018-05-30 14:35:24 +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 | |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.app.timedetector.ITimeDetectorService; |
| 22 | import android.app.timedetector.TimeSignal; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 23 | import android.content.ContentResolver; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 24 | import android.content.Context; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 25 | import android.database.ContentObserver; |
Neil Fuller | 185a29a | 2018-06-13 16:56:38 +0100 | [diff] [blame] | 26 | import android.os.Binder; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 27 | import android.provider.Settings; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 28 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 29 | import com.android.internal.annotations.GuardedBy; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 30 | import com.android.internal.annotations.VisibleForTesting; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 31 | import com.android.internal.util.DumpUtils; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 32 | import com.android.server.FgThread; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 33 | import com.android.server.SystemService; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 34 | import com.android.server.timedetector.TimeDetectorStrategy.Callback; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 35 | |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 36 | import java.io.FileDescriptor; |
| 37 | import java.io.PrintWriter; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 38 | import java.util.Objects; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 39 | |
| 40 | public final class TimeDetectorService extends ITimeDetectorService.Stub { |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 41 | private static final String TAG = "timedetector.TimeDetectorService"; |
| 42 | |
| 43 | public static class Lifecycle extends SystemService { |
| 44 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 45 | public Lifecycle(@NonNull Context context) { |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 46 | super(context); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void onStart() { |
| 51 | TimeDetectorService service = TimeDetectorService.create(getContext()); |
| 52 | |
| 53 | // Publish the binder service so it can be accessed from other (appropriately |
| 54 | // permissioned) processes. |
| 55 | publishBinderService(Context.TIME_DETECTOR_SERVICE, service); |
| 56 | } |
| 57 | } |
| 58 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 59 | @NonNull private final Context mContext; |
| 60 | @NonNull private final Callback mCallback; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 61 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 62 | // The lock used when call the strategy to ensure thread safety. |
| 63 | @NonNull private final Object mStrategyLock = new Object(); |
| 64 | |
| 65 | @GuardedBy("mStrategyLock") |
| 66 | @NonNull private final TimeDetectorStrategy mTimeDetectorStrategy; |
| 67 | |
| 68 | private static TimeDetectorService create(@NonNull Context context) { |
| 69 | final TimeDetectorStrategy timeDetector = new SimpleTimeDetectorStrategy(); |
| 70 | final TimeDetectorStrategyCallbackImpl callback = |
| 71 | new TimeDetectorStrategyCallbackImpl(context); |
| 72 | timeDetector.initialize(callback); |
| 73 | |
| 74 | TimeDetectorService timeDetectorService = |
| 75 | new TimeDetectorService(context, callback, timeDetector); |
| 76 | |
| 77 | // Wire up event listening. |
| 78 | ContentResolver contentResolver = context.getContentResolver(); |
| 79 | contentResolver.registerContentObserver( |
| 80 | Settings.Global.getUriFor(Settings.Global.AUTO_TIME), true, |
| 81 | new ContentObserver(FgThread.getHandler()) { |
| 82 | public void onChange(boolean selfChange) { |
| 83 | timeDetectorService.handleAutoTimeDetectionToggle(); |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | return timeDetectorService; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 90 | @VisibleForTesting |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 91 | public TimeDetectorService(@NonNull Context context, @NonNull Callback callback, |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 92 | @NonNull TimeDetectorStrategy timeDetectorStrategy) { |
| 93 | mContext = Objects.requireNonNull(context); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 94 | mCallback = Objects.requireNonNull(callback); |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 95 | mTimeDetectorStrategy = Objects.requireNonNull(timeDetectorStrategy); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 99 | public void suggestTime(@NonNull TimeSignal timeSignal) { |
| 100 | enforceSetTimePermission(); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 101 | Objects.requireNonNull(timeSignal); |
Neil Fuller | 185a29a | 2018-06-13 16:56:38 +0100 | [diff] [blame] | 102 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 103 | long idToken = Binder.clearCallingIdentity(); |
Neil Fuller | 185a29a | 2018-06-13 16:56:38 +0100 | [diff] [blame] | 104 | try { |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 105 | synchronized (mStrategyLock) { |
| 106 | mTimeDetectorStrategy.suggestTime(timeSignal); |
| 107 | } |
Neil Fuller | 185a29a | 2018-06-13 16:56:38 +0100 | [diff] [blame] | 108 | } finally { |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 109 | Binder.restoreCallingIdentity(idToken); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | @VisibleForTesting |
| 114 | public void handleAutoTimeDetectionToggle() { |
| 115 | synchronized (mStrategyLock) { |
| 116 | final boolean timeDetectionEnabled = mCallback.isTimeDetectionEnabled(); |
| 117 | mTimeDetectorStrategy.handleAutoTimeDetectionToggle(timeDetectionEnabled); |
Neil Fuller | 185a29a | 2018-06-13 16:56:38 +0100 | [diff] [blame] | 118 | } |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 122 | protected void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, |
| 123 | @Nullable String[] args) { |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 124 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
| 125 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 126 | synchronized (mStrategyLock) { |
| 127 | mTimeDetectorStrategy.dump(pw, args); |
| 128 | } |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | private void enforceSetTimePermission() { |
| 132 | mContext.enforceCallingPermission(android.Manifest.permission.SET_TIME, "set time"); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 133 | } |
| 134 | } |