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

Commit 089280f5 authored by Matt Casey's avatar Matt Casey Committed by Android (Google) Code Review
Browse files

Merge "Make actions editable" into 24D1-dev

parents bb338d7a 64426041
Loading
Loading
Loading
Loading
+61 −53
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ import com.android.systemui.screenshot.ScreenshotEvent.SCREENSHOT_EDIT_TAPPED
import com.android.systemui.screenshot.ScreenshotEvent.SCREENSHOT_PREVIEW_TAPPED
import com.android.systemui.screenshot.ScreenshotEvent.SCREENSHOT_SHARE_TAPPED
import com.android.systemui.screenshot.ScreenshotEvent.SCREENSHOT_SMART_ACTION_TAPPED
import com.android.systemui.screenshot.ui.viewmodel.ActionButtonViewModel
import com.android.systemui.screenshot.ui.viewmodel.ActionButtonAppearance
import com.android.systemui.screenshot.ui.viewmodel.ScreenshotViewModel
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
@@ -81,10 +81,11 @@ constructor(
            }
        }
        viewModel.addAction(
            ActionButtonViewModel(
            ActionButtonAppearance(
                AppCompatResources.getDrawable(context, R.drawable.ic_screenshot_edit),
                context.resources.getString(R.string.screenshot_edit_label),
                context.resources.getString(R.string.screenshot_edit_description),
            )
        ) {
            debugLog(LogConfig.DEBUG_ACTIONS) { "Edit tapped" }
            uiEventLogger.log(SCREENSHOT_EDIT_TAPPED, 0, request.packageNameString)
@@ -96,12 +97,13 @@ constructor(
                )
            }
        }
        )

        viewModel.addAction(
            ActionButtonViewModel(
            ActionButtonAppearance(
                AppCompatResources.getDrawable(context, R.drawable.ic_screenshot_share),
                context.resources.getString(R.string.screenshot_share_label),
                context.resources.getString(R.string.screenshot_share_description),
            )
        ) {
            debugLog(LogConfig.DEBUG_ACTIONS) { "Share tapped" }
            uiEventLogger.log(SCREENSHOT_SHARE_TAPPED, 0, request.packageNameString)
@@ -113,14 +115,15 @@ constructor(
                )
            }
        }
        )

        smartActionsProvider.requestQuickShare(request, requestId) { quickShare ->
            if (!quickShare.actionIntent.isImmutable) {
                viewModel.addAction(
                    ActionButtonViewModel(
                    ActionButtonAppearance(
                        quickShare.getIcon().loadDrawable(context),
                        quickShare.title,
                        quickShare.title
                    )
                ) {
                    debugLog(LogConfig.DEBUG_ACTIONS) { "Quickshare tapped" }
                    onDeferrableActionTapped { result ->
@@ -139,7 +142,6 @@ constructor(
                        actionExecutor.sendPendingIntent(pendingIntentWithUri)
                    }
                }
                )
            } else {
                Log.w(TAG, "Received immutable quick share pending intent; ignoring")
            }
@@ -148,14 +150,14 @@ constructor(

    override fun onScrollChipReady(onClick: Runnable) {
        viewModel.addAction(
            ActionButtonViewModel(
            ActionButtonAppearance(
                AppCompatResources.getDrawable(context, R.drawable.ic_screenshot_scroll),
                context.resources.getString(R.string.screenshot_scroll_label),
                context.resources.getString(R.string.screenshot_scroll_label),
            )
        ) {
            onClick.run()
        }
        )
    }

    override fun setCompletedScreenshot(result: ScreenshotSavedResult) {
@@ -166,13 +168,19 @@ constructor(
        this.result = result
        pendingAction?.invoke(result)
        smartActionsProvider.requestSmartActions(request, requestId, result) { smartActions ->
            viewModel.addActions(
                smartActions.map {
                    ActionButtonViewModel(it.getIcon().loadDrawable(context), it.title, it.title) {
                        actionExecutor.sendPendingIntent(it.actionIntent)
            smartActions.forEach {
                smartActions.forEach { action ->
                    viewModel.addAction(
                        ActionButtonAppearance(
                            action.getIcon().loadDrawable(context),
                            action.title,
                            action.title,
                        )
                    ) {
                        actionExecutor.sendPendingIntent(action.actionIntent)
                    }
                }
            }
            )
        }
    }

+4 −4
Original line number Diff line number Diff line
@@ -28,16 +28,16 @@ object ActionButtonViewBinder {
    fun bind(view: View, viewModel: ActionButtonViewModel) {
        val iconView = view.requireViewById<ImageView>(R.id.overlay_action_chip_icon)
        val textView = view.requireViewById<TextView>(R.id.overlay_action_chip_text)
        iconView.setImageDrawable(viewModel.icon)
        textView.text = viewModel.name
        setMargins(iconView, textView, viewModel.name?.isNotEmpty() ?: false)
        iconView.setImageDrawable(viewModel.appearance.icon)
        textView.text = viewModel.appearance.label
        setMargins(iconView, textView, viewModel.appearance.label?.isNotEmpty() ?: false)
        if (viewModel.onClicked != null) {
            view.setOnClickListener { viewModel.onClicked.invoke() }
        } else {
            view.setOnClickListener(null)
        }
        view.tag = viewModel.id
        view.contentDescription = viewModel.description
        view.contentDescription = viewModel.appearance.description
        view.visibility = View.VISIBLE
        view.alpha = 1f
    }
+26 −0
Original line number Diff line number Diff line
/*
 * 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.screenshot.ui.viewmodel

import android.graphics.drawable.Drawable

/** Data describing how an action should be shown to the user. */
data class ActionButtonAppearance(
    val icon: Drawable?,
    val label: CharSequence?,
    val description: CharSequence,
)
+7 −7
Original line number Diff line number Diff line
@@ -16,19 +16,19 @@

package com.android.systemui.screenshot.ui.viewmodel

import android.graphics.drawable.Drawable

data class ActionButtonViewModel(
    val icon: Drawable?,
    val name: CharSequence?,
    val description: CharSequence,
    val appearance: ActionButtonAppearance,
    val id: Int,
    val onClicked: (() -> Unit)?,
) {
    val id: Int = getId()

    companion object {
        private var nextId = 0

        private fun getId() = nextId.also { nextId += 1 }

        fun withNextId(
            appearance: ActionButtonAppearance,
            onClicked: (() -> Unit)?
        ): ActionButtonViewModel = ActionButtonViewModel(appearance, getId(), onClicked)
    }
}
+27 −4
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.systemui.screenshot.ui.viewmodel

import android.graphics.Bitmap
import android.util.Log
import android.view.accessibility.AccessibilityManager
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -39,16 +40,34 @@ class ScreenshotViewModel(private val accessibilityManager: AccessibilityManager
        _previewAction.value = onClick
    }

    fun addAction(action: ActionButtonViewModel) {
    fun addAction(actionAppearance: ActionButtonAppearance, onClicked: (() -> Unit)): Int {
        val actionList = _actions.value.toMutableList()
        val action = ActionButtonViewModel.withNextId(actionAppearance, onClicked)
        actionList.add(action)
        _actions.value = actionList
        return action.id
    }

    fun addActions(actions: List<ActionButtonViewModel>) {
    fun updateActionAppearance(actionId: Int, appearance: ActionButtonAppearance) {
        val actionList = _actions.value.toMutableList()
        actionList.addAll(actions)
        val index = actionList.indexOfFirst { it.id == actionId }
        if (index >= 0) {
            actionList[index] =
                ActionButtonViewModel(appearance, actionId, actionList[index].onClicked)
            _actions.value = actionList
        } else {
            Log.w(TAG, "Attempted to update unknown action id $actionId")
        }
    }

    fun removeAction(actionId: Int) {
        val actionList = _actions.value.toMutableList()
        if (actionList.removeIf { it.id == actionId }) {
            // Update if something was removed.
            _actions.value = actionList
        } else {
            Log.w(TAG, "Attempted to remove unknown action id $actionId")
        }
    }

    fun reset() {
@@ -56,4 +75,8 @@ class ScreenshotViewModel(private val accessibilityManager: AccessibilityManager
        _previewAction.value = null
        _actions.value = listOf()
    }

    companion object {
        const val TAG = "ScreenshotViewModel"
    }
}
Loading