1 /*
2 * Copyright (C) 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 * http://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.contentpreview.payloadtoggle.domain.intent
18
19 import android.app.PendingIntent
20 import android.service.chooser.ChooserAction
21 import android.util.Log
22 import com.android.intentresolver.contentpreview.payloadtoggle.data.model.CustomActionModel
23 import dagger.Module
24 import dagger.Provides
25 import dagger.hilt.InstallIn
26 import dagger.hilt.android.components.ViewModelComponent
27
28 @Module
29 @InstallIn(ViewModelComponent::class)
30 object InitialCustomActionsModule {
31 @Provides
initialCustomActionModelsnull32 fun initialCustomActionModels(
33 chooserActions: List<ChooserAction>,
34 @CustomAction pendingIntentSender: PendingIntentSender,
35 ): List<CustomActionModel> = chooserActions.map { it.toCustomActionModel(pendingIntentSender) }
36 }
37
38 /**
39 * Returns a [CustomActionModel] that sends this [ChooserAction]'s
40 * [PendingIntent][ChooserAction.getAction].
41 */
toCustomActionModelnull42 fun ChooserAction.toCustomActionModel(pendingIntentSender: PendingIntentSender) =
43 CustomActionModel(
44 label = label,
45 icon = icon,
46 performAction = {
47 try {
48 pendingIntentSender.send(action)
49 } catch (_: PendingIntent.CanceledException) {
50 Log.d(TAG, "Custom action, $label, has been cancelled")
51 }
52 }
53 )
54
55 private const val TAG = "CustomShareActions"
56