From a089ad729e83f3a8f840ecb4a0e97a6fab7d7c9a Mon Sep 17 00:00:00 2001 From: Andrey Epin Date: Thu, 8 Feb 2024 14:02:48 -0800 Subject: Shareousel target intent modifier component A building block for the payload toggling functinality. Modifies target intent based on the set of selected items. Bug: 302691505 Test: IntentResolver-tests-unit Change-Id: If3b165254fef70cf58718e20d28e874554a9818f --- .../contentpreview/TargetIntentModifier.kt | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 java/src/com/android/intentresolver/contentpreview/TargetIntentModifier.kt (limited to 'java/src') diff --git a/java/src/com/android/intentresolver/contentpreview/TargetIntentModifier.kt b/java/src/com/android/intentresolver/contentpreview/TargetIntentModifier.kt new file mode 100644 index 00000000..99cfc0f8 --- /dev/null +++ b/java/src/com/android/intentresolver/contentpreview/TargetIntentModifier.kt @@ -0,0 +1,66 @@ +/* + * 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.content.ClipDescription.compareMimeTypes +import android.content.Intent +import android.content.Intent.ACTION_SEND +import android.content.Intent.ACTION_SEND_MULTIPLE +import android.content.Intent.EXTRA_STREAM +import android.net.Uri + +/** Modifies target intent based on current payload selection. */ +class TargetIntentModifier( + private val originalTargetIntent: Intent, + private val getUri: Item.() -> Uri, + private val getMimeType: Item.() -> String?, +) : (List) -> Intent { + fun onSelectionChanged(selection: List): Intent { + val uris = ArrayList(selection.size) + var targetMimeType: String? = null + for (item in selection) { + targetMimeType = updateMimeType(item.getMimeType(), targetMimeType) + uris.add(item.getUri()) + } + val action = if (uris.size == 1) ACTION_SEND else ACTION_SEND_MULTIPLE + return Intent(originalTargetIntent).apply { + this.action = action + this.type = targetMimeType + if (action == ACTION_SEND) { + putExtra(EXTRA_STREAM, uris[0]) + } else { + putParcelableArrayListExtra(EXTRA_STREAM, uris) + } + } + } + + private fun updateMimeType(itemMimeType: String?, unitedMimeType: String?): String { + itemMimeType ?: return "*/*" + unitedMimeType ?: return itemMimeType + if (compareMimeTypes(itemMimeType, unitedMimeType)) return unitedMimeType + val slashIdx = unitedMimeType.indexOf('/') + if (slashIdx >= 0 && unitedMimeType.regionMatches(0, itemMimeType, 0, slashIdx + 1)) { + return buildString { + append(unitedMimeType.substring(0, slashIdx + 1)) + append('*') + } + } + return "*/*" + } + + override fun invoke(selection: List): Intent = onSelectionChanged(selection) +} -- cgit v1.2.3-59-g8ed1b