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.content.Intent
20 import android.os.Build
21 import android.os.UserHandle
22 import android.safetycenter.SafetyCenterManager.EXTRA_SAFETY_SOURCE_ID
23 import android.safetycenter.SafetyCenterManager.EXTRA_SAFETY_SOURCE_ISSUE_ID
24 import android.safetycenter.SafetyCenterManager.EXTRA_SAFETY_SOURCE_USER_HANDLE
25 import androidx.annotation.RequiresApi
26 import com.android.permissioncontroller.safetycenter.SafetyCenterConstants.EXPAND_ISSUE_GROUP_QS_FRAGMENT_KEY
27 import com.android.safetycenter.internaldata.SafetyCenterIssueKey
28 
29 /** Class representing parsed intent extra values for use in [SafetyCenterDashboardFragment] */
30 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
31 data class ParsedSafetyCenterIntent(
32     val safetyCenterIssueKey: SafetyCenterIssueKey? = null,
33     val shouldExpandIssuesGroup: Boolean
34 ) {
35     companion object {
36         @JvmStatic
toSafetyCenterIntentnull37         fun Intent.toSafetyCenterIntent(): ParsedSafetyCenterIntent {
38             val safetySourceId: String? = getStringExtra(EXTRA_SAFETY_SOURCE_ID)
39             val safetySourceIssueId: String? = getStringExtra(EXTRA_SAFETY_SOURCE_ISSUE_ID)
40             val safetySourceUserHandle: UserHandle? =
41                 getParcelableExtra(EXTRA_SAFETY_SOURCE_USER_HANDLE, UserHandle::class.java)
42             val safetyCenterIssueKey: SafetyCenterIssueKey? =
43                 createSafetyCenterIssueKey(
44                     safetySourceId,
45                     safetySourceIssueId,
46                     safetySourceUserHandle
47                 )
48 
49             // Check if we've navigated from QS or if focusing on single issue and issues should be
50             // expanded
51             val shouldExpandIssuesGroup: Boolean =
52                 getBooleanExtra(EXPAND_ISSUE_GROUP_QS_FRAGMENT_KEY, false)
53 
54             return ParsedSafetyCenterIntent(safetyCenterIssueKey, shouldExpandIssuesGroup)
55         }
56 
57         /**
58          * Creates [SafetyCenterIssueKey] using the provided values
59          *
60          * @param safetySourceId source ID for a {@link SafetySourceIssue}
61          * @param safetySourceIssueId an issue ID for a {@link SafetySourceIssue}
62          * @param safetySourceUserHandle the specific a {@link android.os.UserHandle} associated
63          *   with issue
64          */
createSafetyCenterIssueKeynull65         private fun createSafetyCenterIssueKey(
66             safetySourceId: String?,
67             safetySourceIssueId: String?,
68             safetySourceUserHandle: UserHandle?
69         ): SafetyCenterIssueKey? {
70             if (safetySourceId == null || safetySourceIssueId == null) {
71                 return null
72             }
73             return SafetyCenterIssueKey.newBuilder()
74                 .setSafetySourceId(safetySourceId)
75                 .setSafetySourceIssueId(safetySourceIssueId)
76                 // Default to current user
77                 .setUserId(safetySourceUserHandle?.identifier ?: UserHandle.myUserId())
78                 .build()
79         }
80     }
81 }
82