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.permission.ui.auto.dashboard
18 
19 import android.content.Context
20 import android.os.Build
21 import android.text.format.DateFormat
22 import androidx.annotation.RequiresApi
23 import androidx.preference.Preference.OnPreferenceClickListener
24 import com.android.car.ui.preference.CarUiPreference
25 import com.android.permissioncontroller.R
26 import com.android.permissioncontroller.permission.ui.legacy.PermissionUsageDetailsViewModelLegacy
27 import com.android.permissioncontroller.permission.ui.model.v31.PermissionUsageDetailsViewModel
28 
29 /** Preference that displays a permission usage for an app. */
30 @RequiresApi(Build.VERSION_CODES.S)
31 class AutoPermissionHistoryPreference(
32     context: Context,
33     historyPreferenceData: PermissionUsageDetailsViewModelLegacy.HistoryPreferenceData
34 ) : CarUiPreference(context) {
35 
36     init {
37         title = historyPreferenceData.preferenceTitle
38         summary =
39             if (historyPreferenceData.summaryText != null) {
40                 context.getString(
41                     R.string.auto_permission_usage_timeline_summary,
42                     DateFormat.getTimeFormat(context).format(historyPreferenceData.accessEndTime),
43                     historyPreferenceData.summaryText
44                 )
45             } else {
46                 DateFormat.getTimeFormat(context).format(historyPreferenceData.accessEndTime)
47             }
48         if (historyPreferenceData.appIcon != null) {
49             icon = historyPreferenceData.appIcon
50         }
51 
<lambda>null52         onPreferenceClickListener = OnPreferenceClickListener {
53             // This Intent should ideally be part of the preference data, and can be consolidated
54             // when the Legacy and New viewmodels are merged.
55             context.startActivity(
56                 PermissionUsageDetailsViewModel.createHistoryPreferenceClickIntent(
57                     context = context,
58                     userHandle = historyPreferenceData.userHandle,
59                     packageName = historyPreferenceData.pkgName,
60                     permissionGroup = historyPreferenceData.permissionGroup,
61                     accessEndTime = historyPreferenceData.accessEndTime,
62                     accessStartTime = historyPreferenceData.accessStartTime,
63                     showingAttribution = historyPreferenceData.showingAttribution,
64                     attributionTags = historyPreferenceData.attributionTags
65                 )
66             )
67             true
68         }
69     }
70 }
71