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; |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 22 | import android.app.timedetector.ManualTimeSuggestion; |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 23 | import android.app.timedetector.PhoneTimeSuggestion; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 24 | import android.content.ContentResolver; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 25 | import android.content.Context; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 26 | import android.database.ContentObserver; |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 27 | import android.os.Handler; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 28 | import android.provider.Settings; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 29 | |
| 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 | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 41 | private static final String TAG = "TimeDetectorService"; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 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 | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 59 | @NonNull private final Handler mHandler; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 60 | @NonNull private final Context mContext; |
| 61 | @NonNull private final Callback mCallback; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 62 | @NonNull private final TimeDetectorStrategy mTimeDetectorStrategy; |
| 63 | |
| 64 | private static TimeDetectorService create(@NonNull Context context) { |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 65 | TimeDetectorStrategy timeDetector = new SimpleTimeDetectorStrategy(); |
| 66 | TimeDetectorStrategyCallbackImpl callback = new TimeDetectorStrategyCallbackImpl(context); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 67 | timeDetector.initialize(callback); |
| 68 | |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 69 | Handler handler = FgThread.getHandler(); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 70 | TimeDetectorService timeDetectorService = |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 71 | new TimeDetectorService(context, handler, callback, timeDetector); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 72 | |
| 73 | // Wire up event listening. |
| 74 | ContentResolver contentResolver = context.getContentResolver(); |
| 75 | contentResolver.registerContentObserver( |
| 76 | Settings.Global.getUriFor(Settings.Global.AUTO_TIME), true, |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 77 | new ContentObserver(handler) { |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 78 | public void onChange(boolean selfChange) { |
| 79 | timeDetectorService.handleAutoTimeDetectionToggle(); |
| 80 | } |
| 81 | }); |
| 82 | |
| 83 | return timeDetectorService; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 86 | @VisibleForTesting |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 87 | public TimeDetectorService(@NonNull Context context, @NonNull Handler handler, |
| 88 | @NonNull Callback callback, @NonNull TimeDetectorStrategy timeDetectorStrategy) { |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 89 | mContext = Objects.requireNonNull(context); |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 90 | mHandler = Objects.requireNonNull(handler); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 91 | mCallback = Objects.requireNonNull(callback); |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 92 | mTimeDetectorStrategy = Objects.requireNonNull(timeDetectorStrategy); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
Neil Fuller | af3eeaf | 2019-10-15 14:37:37 +0100 | [diff] [blame] | 96 | public void suggestPhoneTime(@NonNull PhoneTimeSuggestion timeSignal) { |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 97 | enforceSuggestPhoneTimePermission(); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 98 | Objects.requireNonNull(timeSignal); |
Neil Fuller | 185a29a | 2018-06-13 16:56:38 +0100 | [diff] [blame] | 99 | |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 100 | mHandler.post(() -> mTimeDetectorStrategy.suggestPhoneTime(timeSignal)); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 103 | @Override |
| 104 | public void suggestManualTime(@NonNull ManualTimeSuggestion timeSignal) { |
| 105 | enforceSuggestManualTimePermission(); |
| 106 | Objects.requireNonNull(timeSignal); |
| 107 | |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 108 | mHandler.post(() -> mTimeDetectorStrategy.suggestManualTime(timeSignal)); |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 111 | @VisibleForTesting |
| 112 | public void handleAutoTimeDetectionToggle() { |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 113 | mHandler.post(mTimeDetectorStrategy::handleAutoTimeDetectionChanged); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 117 | protected void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, |
| 118 | @Nullable String[] args) { |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 119 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
| 120 | |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 121 | mTimeDetectorStrategy.dump(pw, args); |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 124 | private void enforceSuggestPhoneTimePermission() { |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 125 | mContext.enforceCallingPermission(android.Manifest.permission.SET_TIME, "set time"); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 126 | } |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 127 | |
| 128 | private void enforceSuggestManualTimePermission() { |
Neil Fuller | 44fcf29 | 2019-12-02 16:37:28 +0000 | [diff] [blame] | 129 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.SET_TIME, "set time"); |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 130 | } |
| 131 | } |