1 /*
2  * Copyright (C) 2022 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.permissioncontroller.safetycenter.ui
18 
19 import android.app.PendingIntent
20 import android.os.Build.VERSION_CODES.TIRAMISU
21 import androidx.annotation.RequiresApi
22 import androidx.fragment.app.FragmentActivity
23 import com.android.safetycenter.internaldata.SafetyCenterIds
24 
25 /** An object which sends pendingIntents, in a proper task, if needed. */
26 @RequiresApi(TIRAMISU)
27 object PendingIntentSender {
28 
29     @JvmStatic
30     @Throws(PendingIntent.CanceledException::class)
sendnull31     fun send(pi: PendingIntent?, launchTaskId: Int? = null) {
32         if (pi == null) {
33             return
34         }
35         com.android.safetycenter.pendingintents.PendingIntentSender.send(pi, launchTaskId)
36     }
37 
38     /**
39      * Gets the current task ID for sending pending intents in a fragment
40      *
41      * @param entryId identifies an entry on the Safety Center page
42      * @param sameTaskSourceIds list of safety source IDs to show in the same task as Safety Center
43      * @param activity represents the parent activity of the fragment
44      */
45     @JvmStatic
getTaskIdForEntrynull46     fun getTaskIdForEntry(
47         entryId: String,
48         sameTaskSourceIds: List<String>,
49         activity: FragmentActivity
50     ): Int? {
51         val sourceId: String = SafetyCenterIds.entryIdFromString(entryId).getSafetySourceId()
52         return if (sameTaskSourceIds.contains(sourceId)) activity.getTaskId() else null
53     }
54 }
55