1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.intentresolver.domain
18 
19 import android.content.Intent
20 import android.content.Intent.EXTRA_ALTERNATE_INTENTS
21 import android.content.Intent.EXTRA_CHOOSER_CUSTOM_ACTIONS
22 import android.content.Intent.EXTRA_CHOOSER_MODIFY_SHARE_ACTION
23 import android.content.Intent.EXTRA_CHOOSER_REFINEMENT_INTENT_SENDER
24 import android.content.Intent.EXTRA_CHOOSER_RESULT_INTENT_SENDER
25 import android.content.Intent.EXTRA_CHOOSER_TARGETS
26 import android.content.Intent.EXTRA_CHOSEN_COMPONENT_INTENT_SENDER
27 import android.content.Intent.EXTRA_EXCLUDE_COMPONENTS
28 import android.content.Intent.EXTRA_INTENT
29 import android.content.Intent.EXTRA_METADATA_TEXT
30 import android.os.Bundle
31 import com.android.intentresolver.Flags.shareouselUpdateExcludeComponentsExtra
32 import com.android.intentresolver.contentpreview.payloadtoggle.domain.model.ShareouselUpdate
33 import com.android.intentresolver.contentpreview.payloadtoggle.domain.model.getOrDefault
34 import com.android.intentresolver.data.model.ChooserRequest
35 
36 /** Creates a new ChooserRequest with the target intent and updates from a Shareousel callback */
updateWithnull37 fun ChooserRequest.updateWith(targetIntent: Intent, update: ShareouselUpdate): ChooserRequest =
38     copy(
39         targetIntent = targetIntent,
40         callerChooserTargets = update.callerTargets.getOrDefault(callerChooserTargets),
41         modifyShareAction = update.modifyShareAction.getOrDefault(modifyShareAction),
42         additionalTargets = update.alternateIntents.getOrDefault(additionalTargets),
43         chosenComponentSender = update.resultIntentSender.getOrDefault(chosenComponentSender),
44         refinementIntentSender = update.refinementIntentSender.getOrDefault(refinementIntentSender),
45         metadataText = update.metadataText.getOrDefault(metadataText),
46         chooserActions = update.customActions.getOrDefault(chooserActions),
47         filteredComponentNames =
48             if (shareouselUpdateExcludeComponentsExtra()) {
49                 update.excludeComponents.getOrDefault(filteredComponentNames)
50             } else {
51                 filteredComponentNames
52             },
53     )
54 
55 /** Save ChooserRequest values that can be updated by the Shareousel into a Bundle */
ChooserRequestnull56 fun ChooserRequest.saveUpdates(bundle: Bundle): Bundle {
57     bundle.putParcelable(EXTRA_INTENT, targetIntent)
58     bundle.putParcelableArray(EXTRA_CHOOSER_TARGETS, callerChooserTargets.toTypedArray())
59     bundle.putParcelable(EXTRA_CHOOSER_MODIFY_SHARE_ACTION, modifyShareAction)
60     bundle.putParcelableArray(EXTRA_ALTERNATE_INTENTS, additionalTargets.toTypedArray())
61     bundle.putParcelable(EXTRA_CHOOSER_RESULT_INTENT_SENDER, chosenComponentSender)
62     bundle.putParcelable(EXTRA_CHOSEN_COMPONENT_INTENT_SENDER, chosenComponentSender)
63     bundle.putParcelable(EXTRA_CHOOSER_REFINEMENT_INTENT_SENDER, refinementIntentSender)
64     bundle.putCharSequence(EXTRA_METADATA_TEXT, metadataText)
65     bundle.putParcelableArray(EXTRA_CHOOSER_CUSTOM_ACTIONS, chooserActions.toTypedArray())
66     if (shareouselUpdateExcludeComponentsExtra()) {
67         bundle.putParcelableArray(EXTRA_EXCLUDE_COMPONENTS, filteredComponentNames.toTypedArray())
68     }
69     return bundle
70 }
71