Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit af3c4f1e authored by Ajinkya Chalke's avatar Ajinkya Chalke Committed by Automerger Merge Worker
Browse files

Merge "Update AppClips to handle multi-user." into udc-qpr-dev am: 33399e83

parents a5bea7c4 33399e83
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -23,4 +23,6 @@ package com.android.internal.statusbar;
 */
interface IAppClipsService {
    boolean canLaunchCaptureContentActivityForNote(in int taskId);

    int canLaunchCaptureContentActivityForNoteInternal(in int taskId);
}
+11 −14
Original line number Diff line number Diff line
@@ -451,12 +451,14 @@
            android:noHistory="true" />

        <service android:name=".screenshot.appclips.AppClipsScreenshotHelperService"
            android:permission="com.android.systemui.permission.SELF"
            android:exported="false" />
            android:exported="false"
            android:singleUser="true"
            android:permission="com.android.systemui.permission.SELF" />

        <service android:name=".screenshot.appclips.AppClipsService"
            android:permission="android.permission.LAUNCH_CAPTURE_CONTENT_ACTIVITY_FOR_NOTE"
            android:exported="true" />
            android:exported="true"
            android:singleUser="true"
            android:permission="android.permission.LAUNCH_CAPTURE_CONTENT_ACTIVITY_FOR_NOTE" />

        <service android:name=".screenrecord.RecordingService"
                 android:foregroundServiceType="systemExempted"/>
@@ -990,6 +992,11 @@

        <service android:name=".notetask.NoteTaskControllerUpdateService" />

        <service android:name=".notetask.NoteTaskBubblesController$NoteTaskBubblesService"
            android:exported="false"
            android:singleUser="true"
            android:permission="com.android.systemui.permission.SELF" />

        <activity
            android:name=".notetask.shortcut.LaunchNoteTaskActivity"
            android:exported="true"
@@ -1003,16 +1010,6 @@
            </intent-filter>
        </activity>

        <!-- LaunchNoteTaskManagedProfileProxyActivity MUST NOT be exported because it allows caller
             to specify an Android user when launching the default notes app. -->
        <activity
            android:name=".notetask.shortcut.LaunchNoteTaskManagedProfileProxyActivity"
            android:exported="false"
            android:enabled="true"
            android:excludeFromRecents="true"
            android:resizeableActivity="false"
            android:theme="@android:style/Theme.NoDisplay" />

        <activity
            android:name=".notetask.LaunchNotesRoleSettingsTrampolineActivity"
            android:exported="true"
+29 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.notetask;

import android.content.Intent;
import android.graphics.drawable.Icon;
import android.os.UserHandle;

/** A service to help with controlling the state of notes app bubble through the system user. */
interface INoteTaskBubblesService {

    boolean areBubblesAvailable();

    void showOrHideAppBubble(in Intent intent, in UserHandle userHandle, in Icon icon);
}
+138 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.notetask

import android.app.Service
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Icon
import android.os.IBinder
import android.os.UserHandle
import com.android.internal.infra.ServiceConnector
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.log.DebugLogger.debugLog
import com.android.wm.shell.bubbles.Bubbles
import java.util.Optional
import javax.inject.Inject
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext

/**
 * A utility class to help interact with [Bubbles] as system user. The SysUI instance running as
 * system user is the only instance that has the instance of [Bubbles] that manages the notes app
 * bubble for all users.
 *
 * <p>Note: This class is made overridable so that a fake can be created for as mocking suspending
 * functions is not supported by the Android tree's version of mockito.
 */
@SysUISingleton
open class NoteTaskBubblesController
@Inject
constructor(
    @Application private val context: Context,
    @Background private val bgDispatcher: CoroutineDispatcher
) {

    private val serviceConnector: ServiceConnector<INoteTaskBubblesService> =
        ServiceConnector.Impl(
            context,
            Intent(context, NoteTaskBubblesService::class.java),
            Context.BIND_AUTO_CREATE or Context.BIND_WAIVE_PRIORITY or Context.BIND_NOT_VISIBLE,
            UserHandle.USER_SYSTEM,
            INoteTaskBubblesService.Stub::asInterface
        )

    /** Returns whether notes app bubble is supported. */
    open suspend fun areBubblesAvailable(): Boolean =
        withContext(bgDispatcher) {
            suspendCoroutine { continuation ->
                serviceConnector
                    .postForResult { it.areBubblesAvailable() }
                    .whenComplete { available, error ->
                        if (error != null) {
                            debugLog(error = error) { "Failed to query Bubbles as system user." }
                        }
                        continuation.resume(available ?: false)
                    }
            }
        }

    /** Calls the [Bubbles.showOrHideAppBubble] API as [UserHandle.USER_SYSTEM]. */
    open suspend fun showOrHideAppBubble(
        intent: Intent,
        userHandle: UserHandle,
        icon: Icon
    ) {
        withContext(bgDispatcher) {
            serviceConnector
                .post { it.showOrHideAppBubble(intent, userHandle, icon) }
                .whenComplete { _, error ->
                    if (error != null) {
                        debugLog(error = error) {
                            "Failed to show notes app bubble for intent $intent, " +
                                "user $userHandle, and icon $icon."
                        }
                    } else {
                        debugLog {
                            "Call to show notes app bubble for intent $intent, " +
                                "user $userHandle, and icon $icon successful."
                        }
                    }
                }
        }
    }

    /**
     * A helper service to call [Bubbles] APIs that should always be called from the system user
     * instance of SysUI.
     *
     * <p>Note: This service always runs in the SysUI process running on the system user
     * irrespective of which user started the service. This is required so that the correct instance
     * of {@link Bubbles} is injected. This is set via attribute {@code android:singleUser=”true”}
     * in AndroidManifest.
     */
    class NoteTaskBubblesService
    @Inject
    constructor(private val mOptionalBubbles: Optional<Bubbles>) : Service() {

        override fun onBind(intent: Intent): IBinder {
            return object : INoteTaskBubblesService.Stub() {
                override fun areBubblesAvailable() = mOptionalBubbles.isPresent

                override fun showOrHideAppBubble(
                    intent: Intent,
                    userHandle: UserHandle,
                    icon: Icon
                ) {
                    mOptionalBubbles.ifPresentOrElse(
                        { bubbles -> bubbles.showOrHideAppBubble(intent, userHandle, icon) },
                        {
                            debugLog {
                                "Failed to show or hide bubble for intent $intent," +
                                    "user $user, and icon $icon as bubble is empty."
                            }
                        }
                    )
                }
            }
        }
    }
}
+19 −18
Original line number Diff line number Diff line
@@ -38,22 +38,22 @@ import android.widget.Toast
import androidx.annotation.VisibleForTesting
import com.android.systemui.R
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.devicepolicy.areKeyguardShortcutsDisabled
import com.android.systemui.log.DebugLogger.debugLog
import com.android.systemui.notetask.NoteTaskRoleManagerExt.createNoteShortcutInfoAsUser
import com.android.systemui.notetask.NoteTaskRoleManagerExt.getDefaultRoleHolderAsUser
import com.android.systemui.notetask.shortcut.CreateNoteTaskShortcutActivity
import com.android.systemui.notetask.shortcut.LaunchNoteTaskManagedProfileProxyActivity
import com.android.systemui.settings.UserTracker
import com.android.systemui.shared.system.ActivityManagerKt.isInForeground
import com.android.systemui.util.kotlin.getOrNull
import com.android.systemui.util.settings.SecureSettings
import com.android.wm.shell.bubbles.Bubble
import com.android.wm.shell.bubbles.Bubbles
import com.android.wm.shell.bubbles.Bubbles.BubbleExpandListener
import java.util.Optional
import java.util.concurrent.atomic.AtomicReference
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/**
 * Entry point for creating and managing note.
@@ -71,7 +71,7 @@ constructor(
    private val shortcutManager: ShortcutManager,
    private val resolver: NoteTaskInfoResolver,
    private val eventLogger: NoteTaskEventLogger,
    private val optionalBubbles: Optional<Bubbles>,
    private val noteTaskBubblesController: NoteTaskBubblesController,
    private val userManager: UserManager,
    private val keyguardManager: KeyguardManager,
    private val activityManager: ActivityManager,
@@ -79,6 +79,7 @@ constructor(
    private val devicePolicyManager: DevicePolicyManager,
    private val userTracker: UserTracker,
    private val secureSettings: SecureSettings,
    @Application private val applicationScope: CoroutineScope
) {

    @VisibleForTesting val infoReference = AtomicReference<NoteTaskInfo?>()
@@ -103,18 +104,6 @@ constructor(
        }
    }

    /** Starts [LaunchNoteTaskProxyActivity] on the given [user]. */
    fun startNoteTaskProxyActivityForUser(user: UserHandle) {
        context.startActivityAsUser(
            Intent().apply {
                component =
                    ComponentName(context, LaunchNoteTaskManagedProfileProxyActivity::class.java)
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            },
            user
        )
    }

    /** Starts the notes role setting. */
    fun startNotesRoleSetting(activityContext: Context, entryPoint: NoteTaskEntryPoint?) {
        val user =
@@ -178,7 +167,19 @@ constructor(
    ) {
        if (!isEnabled) return

        val bubbles = optionalBubbles.getOrNull() ?: return
        applicationScope.launch { awaitShowNoteTaskAsUser(entryPoint, user) }
    }

    private suspend fun awaitShowNoteTaskAsUser(
        entryPoint: NoteTaskEntryPoint,
        user: UserHandle,
    ) {
        if (!isEnabled) return

        if (!noteTaskBubblesController.areBubblesAvailable()) {
            debugLog { "Bubbles not available in the system user SysUI instance" }
            return
        }

        // TODO(b/249954038): We should handle direct boot (isUserUnlocked). For now, we do nothing.
        if (!userManager.isUserUnlocked) return
@@ -213,7 +214,7 @@ constructor(
                    val intent = createNoteTaskIntent(info)
                    val icon =
                        Icon.createWithResource(context, R.drawable.ic_note_task_shortcut_widget)
                    bubbles.showOrHideAppBubble(intent, user, icon)
                    noteTaskBubblesController.showOrHideAppBubble(intent, user, icon)
                    // App bubble logging happens on `onBubbleExpandChanged`.
                    debugLog { "onShowNoteTask - opened as app bubble: $info" }
                }
Loading