summaryrefslogtreecommitdiff
path: root/PermissionController/src/com/android/permissioncontroller/safetycenter/ui/ParsedSafetyCenterIntent.kt
blob: 377c8d5f52a696a3e77905ec98dee27078941e54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Copyright (C) 2022 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.permissioncontroller.safetycenter.ui

import android.content.Intent
import android.os.Build
import android.os.UserHandle
import android.safetycenter.SafetyCenterManager.EXTRA_SAFETY_SOURCE_ID
import android.safetycenter.SafetyCenterManager.EXTRA_SAFETY_SOURCE_ISSUE_ID
import android.safetycenter.SafetyCenterManager.EXTRA_SAFETY_SOURCE_USER_HANDLE
import androidx.annotation.RequiresApi
import com.android.permissioncontroller.safetycenter.SafetyCenterConstants.EXPAND_ISSUE_GROUP_QS_FRAGMENT_KEY
import com.android.safetycenter.internaldata.SafetyCenterIssueKey

/** Class representing parsed intent extra values for use in [SafetyCenterDashboardFragment] */
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
data class ParsedSafetyCenterIntent(
    val safetyCenterIssueKey: SafetyCenterIssueKey? = null,
    val shouldExpandIssuesGroup: Boolean
) {
    companion object {
        @JvmStatic
        fun Intent.toSafetyCenterIntent(): ParsedSafetyCenterIntent {
            val safetySourceId: String? = getStringExtra(EXTRA_SAFETY_SOURCE_ID)
            val safetySourceIssueId: String? = getStringExtra(EXTRA_SAFETY_SOURCE_ISSUE_ID)
            val safetySourceUserHandle: UserHandle? =
                getParcelableExtra(EXTRA_SAFETY_SOURCE_USER_HANDLE, UserHandle::class.java)
            val safetyCenterIssueKey: SafetyCenterIssueKey? =
                createSafetyCenterIssueKey(
                    safetySourceId,
                    safetySourceIssueId,
                    safetySourceUserHandle
                )

            // Check if we've navigated from QS or if focusing on single issue and issues should be
            // expanded
            val shouldExpandIssuesGroup: Boolean =
                getBooleanExtra(EXPAND_ISSUE_GROUP_QS_FRAGMENT_KEY, false)

            return ParsedSafetyCenterIntent(safetyCenterIssueKey, shouldExpandIssuesGroup)
        }

        /**
         * Creates [SafetyCenterIssueKey] using the provided values
         *
         * @param safetySourceId source ID for a {@link SafetySourceIssue}
         * @param safetySourceIssueId an issue ID for a {@link SafetySourceIssue}
         * @param safetySourceUserHandle the specific a {@link android.os.UserHandle} associated
         *   with issue
         */
        private fun createSafetyCenterIssueKey(
            safetySourceId: String?,
            safetySourceIssueId: String?,
            safetySourceUserHandle: UserHandle?
        ): SafetyCenterIssueKey? {
            if (safetySourceId == null || safetySourceIssueId == null) {
                return null
            }
            return SafetyCenterIssueKey.newBuilder()
                .setSafetySourceId(safetySourceId)
                .setSafetySourceIssueId(safetySourceIssueId)
                // Default to current user
                .setUserId(safetySourceUserHandle?.identifier ?: UserHandle.myUserId())
                .build()
        }
    }
}