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 | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 23 | import android.app.timedetector.NetworkTimeSuggestion; |
Neil Fuller | c0d59e3 | 2020-02-04 14:30:06 +0000 | [diff] [blame] | 24 | import android.app.timedetector.TelephonyTimeSuggestion; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 25 | import android.content.ContentResolver; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 26 | import android.content.Context; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 27 | import android.database.ContentObserver; |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 28 | import android.os.Handler; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 29 | import android.provider.Settings; |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 30 | |
| 31 | import com.android.internal.annotations.VisibleForTesting; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 32 | import com.android.internal.util.DumpUtils; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 33 | import com.android.server.FgThread; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 34 | import com.android.server.SystemService; |
| 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 | |
Neil Fuller | cce4dcd | 2020-02-04 15:51:58 +0000 | [diff] [blame] | 40 | /** |
| 41 | * The implementation of ITimeDetectorService.aidl. |
| 42 | */ |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 43 | public final class TimeDetectorService extends ITimeDetectorService.Stub { |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 44 | private static final String TAG = "TimeDetectorService"; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 45 | |
| 46 | public static class Lifecycle extends SystemService { |
| 47 | |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 48 | public Lifecycle(@NonNull Context context) { |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 49 | super(context); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void onStart() { |
| 54 | TimeDetectorService service = TimeDetectorService.create(getContext()); |
| 55 | |
| 56 | // Publish the binder service so it can be accessed from other (appropriately |
| 57 | // permissioned) processes. |
| 58 | publishBinderService(Context.TIME_DETECTOR_SERVICE, service); |
| 59 | } |
| 60 | } |
| 61 | |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 62 | @NonNull private final Handler mHandler; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 63 | @NonNull private final Context mContext; |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 64 | @NonNull private final TimeDetectorStrategy mTimeDetectorStrategy; |
| 65 | |
| 66 | private static TimeDetectorService create(@NonNull Context context) { |
Neil Fuller | 19b4e5a | 2019-12-09 16:55:28 +0000 | [diff] [blame] | 67 | TimeDetectorStrategy timeDetectorStrategy = new TimeDetectorStrategyImpl(); |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 68 | TimeDetectorStrategyCallbackImpl callback = new TimeDetectorStrategyCallbackImpl(context); |
Neil Fuller | 19b4e5a | 2019-12-09 16:55:28 +0000 | [diff] [blame] | 69 | timeDetectorStrategy.initialize(callback); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 70 | |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 71 | Handler handler = FgThread.getHandler(); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 72 | TimeDetectorService timeDetectorService = |
Neil Fuller | 19b4e5a | 2019-12-09 16:55:28 +0000 | [diff] [blame] | 73 | new TimeDetectorService(context, handler, timeDetectorStrategy); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 74 | |
| 75 | // Wire up event listening. |
| 76 | ContentResolver contentResolver = context.getContentResolver(); |
| 77 | contentResolver.registerContentObserver( |
| 78 | Settings.Global.getUriFor(Settings.Global.AUTO_TIME), true, |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 79 | new ContentObserver(handler) { |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 80 | public void onChange(boolean selfChange) { |
Neil Fuller | cce4dcd | 2020-02-04 15:51:58 +0000 | [diff] [blame] | 81 | timeDetectorService.handleAutoTimeDetectionChanged(); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 82 | } |
| 83 | }); |
| 84 | |
| 85 | return timeDetectorService; |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 88 | @VisibleForTesting |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 89 | public TimeDetectorService(@NonNull Context context, @NonNull Handler handler, |
Neil Fuller | 19b4e5a | 2019-12-09 16:55:28 +0000 | [diff] [blame] | 90 | @NonNull TimeDetectorStrategy timeDetectorStrategy) { |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 91 | mContext = Objects.requireNonNull(context); |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 92 | mHandler = Objects.requireNonNull(handler); |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 93 | mTimeDetectorStrategy = Objects.requireNonNull(timeDetectorStrategy); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
Neil Fuller | c0d59e3 | 2020-02-04 14:30:06 +0000 | [diff] [blame] | 97 | public void suggestTelephonyTime(@NonNull TelephonyTimeSuggestion timeSignal) { |
| 98 | enforceSuggestTelephonyTimePermission(); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 99 | Objects.requireNonNull(timeSignal); |
Neil Fuller | 185a29a | 2018-06-13 16:56:38 +0100 | [diff] [blame] | 100 | |
Neil Fuller | c0d59e3 | 2020-02-04 14:30:06 +0000 | [diff] [blame] | 101 | mHandler.post(() -> mTimeDetectorStrategy.suggestTelephonyTime(timeSignal)); |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 104 | @Override |
| 105 | public void suggestManualTime(@NonNull ManualTimeSuggestion timeSignal) { |
| 106 | enforceSuggestManualTimePermission(); |
| 107 | Objects.requireNonNull(timeSignal); |
| 108 | |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 109 | mHandler.post(() -> mTimeDetectorStrategy.suggestManualTime(timeSignal)); |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 112 | @Override |
| 113 | public void suggestNetworkTime(@NonNull NetworkTimeSuggestion timeSignal) { |
| 114 | enforceSuggestNetworkTimePermission(); |
| 115 | Objects.requireNonNull(timeSignal); |
| 116 | |
| 117 | mHandler.post(() -> mTimeDetectorStrategy.suggestNetworkTime(timeSignal)); |
| 118 | } |
| 119 | |
Neil Fuller | cce4dcd | 2020-02-04 15:51:58 +0000 | [diff] [blame] | 120 | /** Internal method for handling the auto time setting being changed. */ |
Neil Fuller | 4980bbc | 2018-06-12 21:06:20 +0100 | [diff] [blame] | 121 | @VisibleForTesting |
Neil Fuller | cce4dcd | 2020-02-04 15:51:58 +0000 | [diff] [blame] | 122 | public void handleAutoTimeDetectionChanged() { |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 123 | mHandler.post(mTimeDetectorStrategy::handleAutoTimeDetectionChanged); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Override |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 127 | protected void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, |
| 128 | @Nullable String[] args) { |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 129 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
| 130 | |
Neil Fuller | 40cf295 | 2019-11-28 09:47:30 +0000 | [diff] [blame] | 131 | mTimeDetectorStrategy.dump(pw, args); |
Neil Fuller | 4773b9d | 2018-06-08 18:44:49 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Neil Fuller | c0d59e3 | 2020-02-04 14:30:06 +0000 | [diff] [blame] | 134 | private void enforceSuggestTelephonyTimePermission() { |
Neil Fuller | 09e8b7d | 2019-11-21 13:11:04 +0000 | [diff] [blame] | 135 | mContext.enforceCallingPermission( |
Neil Fuller | c0d59e3 | 2020-02-04 14:30:06 +0000 | [diff] [blame] | 136 | android.Manifest.permission.SUGGEST_TELEPHONY_TIME_AND_ZONE, |
| 137 | "suggest telephony time and time zone"); |
Neil Fuller | b557907 | 2018-05-30 14:35:24 +0100 | [diff] [blame] | 138 | } |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 139 | |
| 140 | private void enforceSuggestManualTimePermission() { |
Neil Fuller | 09e8b7d | 2019-11-21 13:11:04 +0000 | [diff] [blame] | 141 | mContext.enforceCallingOrSelfPermission( |
| 142 | android.Manifest.permission.SUGGEST_MANUAL_TIME_AND_ZONE, |
| 143 | "suggest manual time and time zone"); |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 144 | } |
Neil Fuller | 4ab8a19 | 2019-12-16 16:54:06 +0000 | [diff] [blame] | 145 | |
| 146 | private void enforceSuggestNetworkTimePermission() { |
| 147 | mContext.enforceCallingOrSelfPermission( |
| 148 | android.Manifest.permission.SET_TIME, |
| 149 | "set time"); |
| 150 | } |
Neil Fuller | 3aedd49 | 2019-11-23 11:33:57 +0000 | [diff] [blame] | 151 | } |