summaryrefslogtreecommitdiff
path: root/java/src
diff options
context:
space:
mode:
author Andrey Epin <ayepin@google.com> 2024-02-08 21:27:02 -0800
committer Andrey Epin <ayepin@google.com> 2024-02-12 21:06:28 -0800
commit3f006c856c4ff60cca6ed9b61e503fae86c64ca5 (patch)
treed31ea1107ef252c2ac657355480686177a5e468f /java/src
parent3fa28761c639dfde7dbe1929de42cd015cf57af9 (diff)
Add observable custom actions to v2 action factory
The new code is not yet used. Bug: 302691505 Test: atest IntentResolver-tests-unit Change-Id: I32aa2c2c10cc1c19a534116ebeabd4777b8d47ca
Diffstat (limited to 'java/src')
-rw-r--r--java/src/com/android/intentresolver/contentpreview/MutableActionFactory.kt29
-rw-r--r--java/src/com/android/intentresolver/v2/ChooserActionFactory.java36
-rw-r--r--java/src/com/android/intentresolver/v2/ChooserMutableActionFactory.kt54
3 files changed, 97 insertions, 22 deletions
diff --git a/java/src/com/android/intentresolver/contentpreview/MutableActionFactory.kt b/java/src/com/android/intentresolver/contentpreview/MutableActionFactory.kt
new file mode 100644
index 00000000..1cc1a6a6
--- /dev/null
+++ b/java/src/com/android/intentresolver/contentpreview/MutableActionFactory.kt
@@ -0,0 +1,29 @@
+/*
+ * Copyright 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.intentresolver.contentpreview
+
+import android.service.chooser.ChooserAction
+import com.android.intentresolver.widget.ActionRow
+import kotlinx.coroutines.flow.Flow
+
+interface MutableActionFactory {
+ /** A flow of custom actions */
+ val customActionsFlow: Flow<List<ActionRow.Action>>
+
+ /** Update custom actions */
+ fun updateCustomActions(actions: List<ChooserAction>)
+}
diff --git a/java/src/com/android/intentresolver/v2/ChooserActionFactory.java b/java/src/com/android/intentresolver/v2/ChooserActionFactory.java
index 70a2b58e..f9de9f4b 100644
--- a/java/src/com/android/intentresolver/v2/ChooserActionFactory.java
+++ b/java/src/com/android/intentresolver/v2/ChooserActionFactory.java
@@ -212,13 +212,7 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio
for (int i = 0; i < mCustomActions.size(); i++) {
final int position = i;
ActionRow.Action actionRow = createCustomAction(
- mContext,
- mCustomActions.get(i),
- mFinishCallback,
- () -> {
- mLog.logCustomActionSelected(position);
- }
- );
+ mCustomActions.get(i), () -> logCustomAction(position));
if (actionRow != null) {
actions.add(actionRow);
}
@@ -232,13 +226,7 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio
@Override
@Nullable
public ActionRow.Action getModifyShareAction() {
- return createCustomAction(
- mContext,
- mModifyShareAction,
- mFinishCallback,
- () -> {
- mLog.logActionSelected(EventLog.SELECTION_TYPE_MODIFY_SHARE);
- });
+ return createCustomAction(mModifyShareAction, this::logModifyShareAction);
}
/**
@@ -374,15 +362,11 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio
}
@Nullable
- private ActionRow.Action createCustomAction(
- Context context,
- @Nullable ChooserAction action,
- Consumer<Integer> finishCallback,
- Runnable loggingRunnable) {
+ ActionRow.Action createCustomAction(@Nullable ChooserAction action, Runnable loggingRunnable) {
if (action == null) {
return null;
}
- Drawable icon = action.getIcon().loadDrawable(context);
+ Drawable icon = action.getIcon().loadDrawable(mContext);
if (icon == null && TextUtils.isEmpty(action.getLabel())) {
return null;
}
@@ -399,7 +383,7 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio
null,
null,
ActivityOptions.makeCustomAnimation(
- context,
+ mContext,
R.anim.slide_in_right,
R.anim.slide_out_left)
.toBundle());
@@ -412,8 +396,16 @@ public final class ChooserActionFactory implements ChooserContentPreviewUi.Actio
if (mShareResultSender != null) {
mShareResultSender.onActionSelected(ShareAction.APPLICATION_DEFINED);
}
- finishCallback.accept(Activity.RESULT_OK);
+ mFinishCallback.accept(Activity.RESULT_OK);
}
);
}
+
+ void logCustomAction(int position) {
+ mLog.logCustomActionSelected(position);
+ }
+
+ private void logModifyShareAction() {
+ mLog.logActionSelected(EventLog.SELECTION_TYPE_MODIFY_SHARE);
+ }
}
diff --git a/java/src/com/android/intentresolver/v2/ChooserMutableActionFactory.kt b/java/src/com/android/intentresolver/v2/ChooserMutableActionFactory.kt
new file mode 100644
index 00000000..2f8ccf77
--- /dev/null
+++ b/java/src/com/android/intentresolver/v2/ChooserMutableActionFactory.kt
@@ -0,0 +1,54 @@
+/*
+ * Copyright 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.intentresolver.v2
+
+import android.service.chooser.ChooserAction
+import com.android.intentresolver.contentpreview.ChooserContentPreviewUi
+import com.android.intentresolver.contentpreview.MutableActionFactory
+import com.android.intentresolver.widget.ActionRow
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.MutableStateFlow
+
+/** A wrapper around [ChooserActionFactory] that provides observable custom actions */
+class ChooserMutableActionFactory(
+ private val actionFactory: ChooserActionFactory,
+) : MutableActionFactory, ChooserContentPreviewUi.ActionFactory by actionFactory {
+ private val customActions =
+ MutableStateFlow<List<ActionRow.Action>>(actionFactory.createCustomActions())
+
+ override val customActionsFlow: Flow<List<ActionRow.Action>>
+ get() = customActions
+
+ override fun updateCustomActions(actions: List<ChooserAction>) {
+ customActions.tryEmit(mapChooserActions(actions))
+ }
+
+ override fun createCustomActions(): List<ActionRow.Action> = customActions.value
+
+ private fun mapChooserActions(chooserActions: List<ChooserAction>): List<ActionRow.Action> =
+ buildList(chooserActions.size) {
+ chooserActions.forEachIndexed { i, chooserAction ->
+ val actionRow =
+ actionFactory.createCustomAction(chooserAction) {
+ actionFactory.logCustomAction(i)
+ }
+ if (actionRow != null) {
+ add(actionRow)
+ }
+ }
+ }
+}