1 /*
<lambda>null2  * Copyright (C) 2021 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 @file:Suppress("DEPRECATION")
17 
18 package com.android.permissioncontroller.permission.ui.auto
19 
20 import android.annotation.SuppressLint
21 import android.content.Intent
22 import android.os.Bundle
23 import android.os.UserHandle
24 import androidx.lifecycle.ViewModelProvider
25 import androidx.preference.Preference
26 import com.android.car.ui.preference.CarUiPreference
27 import com.android.permissioncontroller.Constants
28 import com.android.permissioncontroller.DumpableLog
29 import com.android.permissioncontroller.R
30 import com.android.permissioncontroller.auto.AutoSettingsFrameFragment
31 import com.android.permissioncontroller.permission.data.v33.PermissionDecision
32 import com.android.permissioncontroller.permission.ui.model.v33.ReviewPermissionDecisionsViewModel
33 import com.android.permissioncontroller.permission.ui.model.v33.ReviewPermissionDecisionsViewModelFactory
34 
35 /** Shows all recent permission decisions. */
36 @SuppressLint("NewApi")
37 class AutoReviewPermissionDecisionsViewAllFragment : AutoSettingsFrameFragment() {
38 
39     companion object {
40         private const val LOG_TAG = "AutoReviewPermissionDecisionsViewAllFragment"
41 
42         /** Creates a new instance of [AutoReviewPermissionDecisionsViewAllFragment]. */
43         fun newInstance(
44             sessionId: Long,
45             userHandle: UserHandle
46         ): AutoReviewPermissionDecisionsViewAllFragment {
47             return AutoReviewPermissionDecisionsViewAllFragment().apply {
48                 arguments =
49                     Bundle().apply {
50                         putLong(Constants.EXTRA_SESSION_ID, sessionId)
51                         putParcelable(Intent.EXTRA_USER, userHandle)
52                     }
53             }
54         }
55     }
56 
57     private lateinit var user: UserHandle
58     private lateinit var viewModel: ReviewPermissionDecisionsViewModel
59 
60     override fun onCreate(savedInstanceState: Bundle?) {
61         super.onCreate(savedInstanceState)
62         if (arguments == null) {
63             DumpableLog.e(LOG_TAG, "Missing arguments")
64             activity?.finish()
65             return
66         }
67         if (!requireArguments().containsKey(Intent.EXTRA_USER)) {
68             DumpableLog.e(LOG_TAG, "Missing argument ${Intent.EXTRA_USER}")
69             activity?.finish()
70             return
71         }
72         user = requireArguments().getParcelable<UserHandle>(Intent.EXTRA_USER)!!
73         val factory =
74             ReviewPermissionDecisionsViewModelFactory(requireActivity().getApplication()!!, user)
75         viewModel = ViewModelProvider(this, factory)[ReviewPermissionDecisionsViewModel::class.java]
76         viewModel.recentPermissionDecisionsLiveData.observe(this) { recentDecisions ->
77             onRecentDecisionsChanged(recentDecisions)
78         }
79         headerLabel = getString(R.string.review_permission_decisions)
80     }
81 
82     override fun onCreatePreferences(bundle: Bundle?, s: String?) {
83         preferenceScreen = preferenceManager.createPreferenceScreen(context!!)
84     }
85 
86     private fun onRecentDecisionsChanged(recentDecisions: List<PermissionDecision>) {
87         preferenceScreen.removeAll()
88         for (recentDecision in recentDecisions) {
89             val decisionPreference =
90                 CarUiPreference(context).apply {
91                     icon = viewModel.getAppIcon(recentDecision.packageName)
92                     title = viewModel.createPreferenceTitle(recentDecision)
93                     summary = viewModel.createSummaryText(recentDecision)
94                     onPreferenceClickListener =
95                         Preference.OnPreferenceClickListener {
96                             viewModel.createManageAppPermissionIntent(recentDecision).also {
97                                 startActivity(it)
98                             }
99                             false
100                         }
101                 }
102             preferenceScreen.addPreference(decisionPreference)
103         }
104     }
105 }
106