From b557907f51a8a92954b60fe1d181e7f4e9f5c803 Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Wed, 30 May 2018 14:35:24 +0100 Subject: [PATCH] Minimum viable TimeDetectorService This is a do-nothing TimeDetectorService that can be populated in following commits. A temporary method has been added so the service has one method. Unit tests can be run with: atest FrameworksServicesTests:TimeDetectorServiceTest Test: build / boot Test: See above Merged-In: I9e4eac70b944441f34491315cd1ce7fa2b9ae150 Change-Id: I9e4eac70b944441f34491315cd1ce7fa2b9ae150 (cherry picked from commit feeee682a23f25760c4a1aa07c00945901911ba3) --- Android.bp | 1 + .../android/app/SystemServiceRegistry.java | 9 +++ .../timedetector/ITimeDetectorService.aidl | 34 +++++++++ .../app/timedetector/TimeDetector.java | 57 +++++++++++++++ core/java/android/content/Context.java | 12 +++- .../timedetector/TimeDetectorService.java | 72 +++++++++++++++++++ .../java/com/android/server/SystemServer.java | 10 +++ .../timedetector/TimeDetectorServiceTest.java | 46 ++++++++++++ 8 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 core/java/android/app/timedetector/ITimeDetectorService.aidl create mode 100644 core/java/android/app/timedetector/TimeDetector.java create mode 100644 services/core/java/com/android/server/timedetector/TimeDetectorService.java create mode 100644 services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java diff --git a/Android.bp b/Android.bp index 58cb9afa06bfb..cb32b36be7fc3 100644 --- a/Android.bp +++ b/Android.bp @@ -97,6 +97,7 @@ java_library { "core/java/android/app/backup/IRestoreObserver.aidl", "core/java/android/app/backup/IRestoreSession.aidl", "core/java/android/app/backup/ISelectBackupTransportCallback.aidl", + "core/java/android/app/timedetector/ITimeDetectorService.aidl", "core/java/android/app/timezone/ICallback.aidl", "core/java/android/app/timezone/IRulesManager.aidl", "core/java/android/app/usage/ICacheQuotaService.aidl", diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java index 69bd4532cdafb..aa1738dc1d975 100644 --- a/core/java/android/app/SystemServiceRegistry.java +++ b/core/java/android/app/SystemServiceRegistry.java @@ -22,6 +22,7 @@ import android.app.admin.DevicePolicyManager; import android.app.admin.IDevicePolicyManager; import android.app.job.IJobScheduler; import android.app.job.JobScheduler; +import android.app.timedetector.TimeDetector; import android.app.timezone.RulesManager; import android.app.trust.TrustManager; import android.app.usage.IStorageStatsManager; @@ -905,6 +906,14 @@ final class SystemServiceRegistry { public RulesManager createService(ContextImpl ctx) { return new RulesManager(ctx.getOuterContext()); }}); + + registerService(Context.TIME_DETECTOR_SERVICE, TimeDetector.class, + new CachedServiceFetcher() { + @Override + public TimeDetector createService(ContextImpl ctx) + throws ServiceNotFoundException { + return new TimeDetector(); + }}); } /** diff --git a/core/java/android/app/timedetector/ITimeDetectorService.aidl b/core/java/android/app/timedetector/ITimeDetectorService.aidl new file mode 100644 index 0000000000000..bdbe111c7ebe0 --- /dev/null +++ b/core/java/android/app/timedetector/ITimeDetectorService.aidl @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.app.timedetector; + +/** + * System private API to comunicate with time detector service. + * + *

Used by parts of the Android system with signals associated with the device's time to provide + * information to the Time Detector Service. + * + *

Use the {@link android.app.timedetector.TimeDetector} class rather than going through + * this Binder interface directly. See {@link android.app.timedetector.TimeDetectorService} for + * more complete documentation. + * + * + * {@hide} + */ +interface ITimeDetectorService { + void stubbedCall(); +} diff --git a/core/java/android/app/timedetector/TimeDetector.java b/core/java/android/app/timedetector/TimeDetector.java new file mode 100644 index 0000000000000..ac1e2233357c3 --- /dev/null +++ b/core/java/android/app/timedetector/TimeDetector.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.app.timedetector; + +import android.annotation.SystemService; +import android.content.Context; +import android.os.RemoteException; +import android.os.ServiceManager; +import android.os.ServiceManager.ServiceNotFoundException; +import android.util.Log; + +/** + * The interface through which system components can send signals to the TimeDetectorService. + * @hide + */ +@SystemService(Context.TIME_DETECTOR_SERVICE) +public final class TimeDetector { + private static final String TAG = "timedetector.TimeDetector"; + private static final boolean DEBUG = false; + + private final ITimeDetectorService mITimeDetectorService; + + public TimeDetector() throws ServiceNotFoundException { + mITimeDetectorService = ITimeDetectorService.Stub.asInterface( + ServiceManager.getServiceOrThrow(Context.TIME_DETECTOR_SERVICE)); + } + + /** + * Does nothing. + * TODO: Remove this when the service implementation is built out. + */ + public void stubbedCall() { + if (DEBUG) { + Log.d(TAG, "stubbedCall called"); + } + try { + mITimeDetectorService.stubbedCall(); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + +} diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index fcf7dbfeaf72e..da3e7cb2f815a 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -3019,7 +3019,8 @@ public abstract class Context { //@hide: CONTEXTHUB_SERVICE, SYSTEM_HEALTH_SERVICE, //@hide: INCIDENT_SERVICE, - COMPANION_DEVICE_SERVICE + COMPANION_DEVICE_SERVICE, + //@hide: TIME_DETECTOR_SERVICE, }) @Retention(RetentionPolicy.SOURCE) public @interface ServiceName {} @@ -4096,6 +4097,15 @@ public abstract class Context { @SystemApi public static final String SECURE_ELEMENT_SERVICE = "secure_element"; + /** + * Use with {@link #getSystemService(String)} to retrieve an + * {@link android.app.timedetector.ITimeDetectorService}. + * @hide + * + * @see #getSystemService(String) + */ + public static final String TIME_DETECTOR_SERVICE = "time_detector"; + /** * Determine whether the given permission is allowed for a particular * process and user ID running in the system. diff --git a/services/core/java/com/android/server/timedetector/TimeDetectorService.java b/services/core/java/com/android/server/timedetector/TimeDetectorService.java new file mode 100644 index 0000000000000..0b63e29e149a4 --- /dev/null +++ b/services/core/java/com/android/server/timedetector/TimeDetectorService.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.timedetector; + +import com.android.internal.util.DumpUtils; +import com.android.server.SystemService; + +import android.app.timedetector.ITimeDetectorService; +import android.content.Context; +import android.util.Slog; + +import java.io.FileDescriptor; +import java.io.PrintWriter; + +public final class TimeDetectorService extends ITimeDetectorService.Stub { + + private static final String TAG = "timedetector.TimeDetectorService"; + + public static class Lifecycle extends SystemService { + + public Lifecycle(Context context) { + super(context); + } + + @Override + public void onStart() { + TimeDetectorService service = TimeDetectorService.create(getContext()); + + // Publish the binder service so it can be accessed from other (appropriately + // permissioned) processes. + publishBinderService(Context.TIME_DETECTOR_SERVICE, service); + } + } + + private final Context mContext; + + private static TimeDetectorService create(Context context) { + return new TimeDetectorService(context); + } + + public TimeDetectorService(Context context) { + mContext = context; + } + + @Override + public void stubbedCall() { + // Empty call for initial tests. + Slog.d(TAG, "stubbedCall() called"); + // TODO(nfuller): Remove when there are real methods. + } + + @Override + protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { + if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; + + // TODO(nfuller): Implement when there is state. + } +} \ No newline at end of file diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java index 7caa4d7c4149d..58e77e8c078f6 100644 --- a/services/java/com/android/server/SystemServer.java +++ b/services/java/com/android/server/SystemServer.java @@ -204,6 +204,8 @@ public final class SystemServer { "com.android.server.autofill.AutofillManagerService"; private static final String TIME_ZONE_RULES_MANAGER_SERVICE_CLASS = "com.android.server.timezone.RulesManagerService$Lifecycle"; + private static final String TIME_DETECTOR_SERVICE_CLASS = + "com.android.server.timedetector.TimeDetectorService$Lifecycle"; private static final String PERSISTENT_DATA_BLOCK_PROP = "ro.frp.pst"; @@ -1201,6 +1203,14 @@ public final class SystemServer { traceEnd(); } + traceBeginAndSlog("StartTimeDetectorService"); + try { + mSystemServiceManager.startService(TIME_DETECTOR_SERVICE_CLASS); + } catch (Throwable e) { + reportWtf("starting StartTimeDetectorService service", e); + } + traceEnd(); + if (!disableNonCoreServices && !disableSearchManager) { traceBeginAndSlog("StartSearchManagerService"); try { diff --git a/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java new file mode 100644 index 0000000000000..dc17eeb299f32 --- /dev/null +++ b/services/tests/servicestests/src/com/android/server/timedetector/TimeDetectorServiceTest.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.timedetector; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Unit tests for the {@link TimeDetectorService}. + */ +@RunWith(AndroidJUnit4.class) +public class TimeDetectorServiceTest { + + private TimeDetectorService mTimeDetectorService; + + @Before + public void setUp() { + final Context context = InstrumentationRegistry.getContext(); + mTimeDetectorService = new TimeDetectorService(context); + } + + @Test + public void testStubbedCall() { + mTimeDetectorService.stubbedCall(); + } + +}