Convert DreamOverlayRegistrant to Kotlin.
This changelist converts DreamOverlayRegistrant to Kotlin in preparation
for further changes.
Test: manual - ensured registration still worked.
Bug: 378111815
Flag: EXEMPT refactor
Change-Id: Id38f54724889af6c4ac227b1fc93e5c760d7f629
diff --git a/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayRegistrant.java b/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayRegistrant.java
deleted file mode 100644
index 80e68cf..0000000
--- a/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayRegistrant.java
+++ /dev/null
@@ -1,129 +0,0 @@
-/*
- * Copyright (C) 2021 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.systemui.dreams;
-
-import static com.android.systemui.dreams.dagger.DreamModule.DREAM_OVERLAY_SERVICE_COMPONENT;
-
-import android.content.BroadcastReceiver;
-import android.content.ComponentName;
-import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.content.pm.PackageManager;
-import android.content.pm.PackageManager.NameNotFoundException;
-import android.content.res.Resources;
-import android.os.PatternMatcher;
-import android.os.RemoteException;
-import android.os.ServiceManager;
-import android.service.dreams.DreamService;
-import android.service.dreams.IDreamManager;
-import android.util.Log;
-
-import com.android.systemui.dagger.qualifiers.Main;
-import com.android.systemui.dagger.qualifiers.SystemUser;
-import com.android.systemui.shared.condition.Monitor;
-import com.android.systemui.util.condition.ConditionalCoreStartable;
-
-import javax.inject.Inject;
-import javax.inject.Named;
-
-/**
- * {@link DreamOverlayRegistrant} is responsible for telling system server that SystemUI should be
- * the designated dream overlay component.
- */
-public class DreamOverlayRegistrant extends ConditionalCoreStartable {
- private static final String TAG = "DreamOverlayRegistrant";
- private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
- private final IDreamManager mDreamManager;
- private final ComponentName mOverlayServiceComponent;
- private final Context mContext;
- private final Resources mResources;
- private boolean mCurrentRegisteredState = false;
-
- private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- if (DEBUG) {
- Log.d(TAG, "package changed receiver - onReceive");
- }
-
- registerOverlayService();
- }
- };
-
- private void registerOverlayService() {
- // Check to see if the service has been disabled by the user. In this case, we should not
- // proceed modifying the enabled setting.
- final PackageManager packageManager = mContext.getPackageManager();
- final int enabledState =
- packageManager.getComponentEnabledSetting(mOverlayServiceComponent);
-
- // The overlay service is only registered when its component setting is enabled.
- boolean register = false;
-
- try {
- register = packageManager.getServiceInfo(mOverlayServiceComponent,
- PackageManager.GET_META_DATA).enabled;
- } catch (NameNotFoundException e) {
- Log.e(TAG, "could not find dream overlay service");
- }
-
- if (mCurrentRegisteredState == register) {
- return;
- }
-
- mCurrentRegisteredState = register;
-
- try {
- if (DEBUG) {
- Log.d(TAG, mCurrentRegisteredState
- ? "registering dream overlay service:" + mOverlayServiceComponent
- : "clearing dream overlay service");
- }
-
- mDreamManager.registerDreamOverlayService(
- mCurrentRegisteredState ? mOverlayServiceComponent : null);
- } catch (RemoteException e) {
- Log.e(TAG, "could not register dream overlay service:" + e);
- }
- }
-
- @Inject
- public DreamOverlayRegistrant(Context context, @Main Resources resources,
- @Named(DREAM_OVERLAY_SERVICE_COMPONENT) ComponentName dreamOverlayServiceComponent,
- @SystemUser Monitor monitor) {
- super(monitor);
- mContext = context;
- mResources = resources;
- mDreamManager = IDreamManager.Stub.asInterface(
- ServiceManager.getService(DreamService.DREAM_SERVICE));
- mOverlayServiceComponent = dreamOverlayServiceComponent;
- }
-
- @Override
- protected void onStart() {
- final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_CHANGED);
- filter.addDataScheme("package");
- filter.addDataSchemeSpecificPart(mOverlayServiceComponent.getPackageName(),
- PatternMatcher.PATTERN_LITERAL);
- // Note that we directly register the receiver here as data schemes are not supported by
- // BroadcastDispatcher.
- mContext.registerReceiver(mReceiver, filter);
-
- registerOverlayService();
- }
-}
diff --git a/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayRegistrant.kt b/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayRegistrant.kt
new file mode 100644
index 0000000..4400ee6
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayRegistrant.kt
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2024 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.systemui.dreams
+
+import android.content.BroadcastReceiver
+import android.content.ComponentName
+import android.content.Context
+import android.content.Intent
+import android.content.IntentFilter
+import android.content.pm.PackageManager
+import android.os.PatternMatcher
+import android.os.RemoteException
+import android.os.ServiceManager
+import android.service.dreams.DreamService
+import android.service.dreams.IDreamManager
+import android.util.Log
+import com.android.systemui.dagger.qualifiers.SystemUser
+import com.android.systemui.dreams.dagger.DreamModule
+import com.android.systemui.shared.condition.Monitor
+import com.android.systemui.util.condition.ConditionalCoreStartable
+import javax.inject.Inject
+import javax.inject.Named
+
+/**
+ * [DreamOverlayRegistrant] is responsible for telling system server that SystemUI should be the
+ * designated dream overlay component.
+ */
+class DreamOverlayRegistrant
+@Inject
+constructor(
+ private val context: Context,
+ @param:Named(DreamModule.DREAM_OVERLAY_SERVICE_COMPONENT)
+ private val overlayServiceComponent: ComponentName,
+ @SystemUser monitor: Monitor,
+) : ConditionalCoreStartable(monitor) {
+ private val dreamManager: IDreamManager =
+ IDreamManager.Stub.asInterface(ServiceManager.getService(DreamService.DREAM_SERVICE))
+ private var currentRegisteredState = false
+
+ private val receiver: BroadcastReceiver =
+ object : BroadcastReceiver() {
+ override fun onReceive(context: Context, intent: Intent) {
+ if (DEBUG) {
+ Log.d(TAG, "package changed receiver - onReceive")
+ }
+
+ registerOverlayService()
+ }
+ }
+
+ private fun registerOverlayService() {
+ // Check to see if the service has been disabled by the user. In this case, we should not
+ // proceed modifying the enabled setting.
+ val packageManager = context.packageManager
+
+ // The overlay service is only registered when its component setting is enabled.
+ var register = false
+
+ try {
+ register =
+ packageManager
+ .getServiceInfo(overlayServiceComponent, PackageManager.GET_META_DATA)
+ .enabled
+ } catch (e: PackageManager.NameNotFoundException) {
+ Log.e(TAG, "could not find dream overlay service")
+ }
+
+ if (currentRegisteredState == register) {
+ return
+ }
+
+ currentRegisteredState = register
+
+ try {
+ if (DEBUG) {
+ Log.d(
+ TAG,
+ if (currentRegisteredState)
+ "registering dream overlay service:$overlayServiceComponent"
+ else "clearing dream overlay service",
+ )
+ }
+
+ dreamManager.registerDreamOverlayService(
+ if (currentRegisteredState) overlayServiceComponent else null
+ )
+ } catch (e: RemoteException) {
+ Log.e(TAG, "could not register dream overlay service:$e")
+ }
+ }
+
+ override fun onStart() {
+ val filter = IntentFilter(Intent.ACTION_PACKAGE_CHANGED)
+ filter.addDataScheme("package")
+ filter.addDataSchemeSpecificPart(
+ overlayServiceComponent.packageName,
+ PatternMatcher.PATTERN_LITERAL,
+ )
+ // Note that we directly register the receiver here as data schemes are not supported by
+ // BroadcastDispatcher.
+ context.registerReceiver(receiver, filter)
+
+ registerOverlayService()
+ }
+
+ companion object {
+ private const val TAG = "DreamOverlayRegistrant"
+ private val DEBUG = Log.isLoggable(TAG, Log.DEBUG)
+ }
+}