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.model.v31 18 19 import android.Manifest 20 import android.content.Context 21 import android.content.Intent 22 import android.os.Build 23 import androidx.annotation.RequiresApi 24 import androidx.preference.Preference 25 import androidx.preference.Preference.OnPreferenceClickListener 26 import com.android.permissioncontroller.PermissionControllerStatsLog 27 import com.android.permissioncontroller.PermissionControllerStatsLog.PERMISSION_USAGE_FRAGMENT_INTERACTION 28 import com.android.permissioncontroller.PermissionControllerStatsLog.PERMISSION_USAGE_FRAGMENT_INTERACTION__ACTION__CAMERA_ACCESS_TIMELINE_VIEWED 29 import com.android.permissioncontroller.PermissionControllerStatsLog.PERMISSION_USAGE_FRAGMENT_INTERACTION__ACTION__LOCATION_ACCESS_TIMELINE_VIEWED 30 import com.android.permissioncontroller.PermissionControllerStatsLog.PERMISSION_USAGE_FRAGMENT_INTERACTION__ACTION__MICROPHONE_ACCESS_TIMELINE_VIEWED 31 import com.android.permissioncontroller.R 32 import com.android.permissioncontroller.permission.ui.ManagePermissionsActivity 33 import com.android.permissioncontroller.permission.utils.KotlinUtils 34 import com.android.permissioncontroller.permission.utils.StringUtils 35 36 @RequiresApi(Build.VERSION_CODES.S) 37 object PermissionUsageControlPreferenceUtils { 38 39 private val SENSOR_DATA_PERMISSIONS: List<String> = listOf( 40 Manifest.permission_group.LOCATION, 41 Manifest.permission_group.CAMERA, 42 Manifest.permission_group.MICROPHONE 43 ) 44 45 @JvmStatic initPreferencenull46 fun initPreference( 47 preference: Preference, 48 context: Context, 49 groupName: String, 50 count: Int, 51 showSystem: Boolean, 52 sessionId: Long, 53 show7Days: Boolean 54 ): Preference { 55 val permGroupLabel = KotlinUtils.getPermGroupLabel(context, groupName) 56 return preference.apply { 57 title = permGroupLabel 58 icon = KotlinUtils.getPermGroupIcon(context, groupName) 59 summary = StringUtils.getIcuPluralsString(context, 60 R.string.permission_usage_preference_label, count) 61 if (count == 0) { 62 isEnabled = false 63 val permissionUsageSummaryNotUsed = if (show7Days) { 64 StringUtils.getIcuPluralsString(context, 65 R.string.permission_usage_preference_summary_not_used_in_past_n_days, 66 7) 67 } else { 68 StringUtils.getIcuPluralsString(context, 69 R.string.permission_usage_preference_summary_not_used_in_past_n_hours, 70 24) 71 } 72 setSummary(permissionUsageSummaryNotUsed) 73 } else if (SENSOR_DATA_PERMISSIONS.contains(groupName)) { 74 onPreferenceClickListener = OnPreferenceClickListener { 75 val intent = Intent(Intent.ACTION_REVIEW_PERMISSION_HISTORY) 76 intent.putExtra(Intent.EXTRA_PERMISSION_GROUP_NAME, groupName) 77 intent.putExtra(ManagePermissionsActivity.EXTRA_SHOW_SYSTEM, showSystem) 78 intent.putExtra(ManagePermissionsActivity.EXTRA_SHOW_7_DAYS, show7Days) 79 logSensorDataTimelineViewed(groupName, sessionId) 80 context.startActivity(intent) 81 true 82 } 83 } else { 84 onPreferenceClickListener = OnPreferenceClickListener { 85 val intent = Intent(Intent.ACTION_MANAGE_PERMISSION_APPS) 86 intent.putExtra(Intent.EXTRA_PERMISSION_GROUP_NAME, groupName) 87 context.startActivity(intent) 88 true 89 } 90 } 91 } 92 } 93 logSensorDataTimelineViewednull94 private fun logSensorDataTimelineViewed(groupName: String, sessionId: Long) { 95 val act = when (groupName) { 96 Manifest.permission_group.LOCATION -> { 97 PERMISSION_USAGE_FRAGMENT_INTERACTION__ACTION__LOCATION_ACCESS_TIMELINE_VIEWED 98 } 99 Manifest.permission_group.CAMERA -> { 100 PERMISSION_USAGE_FRAGMENT_INTERACTION__ACTION__CAMERA_ACCESS_TIMELINE_VIEWED 101 } 102 Manifest.permission_group.MICROPHONE -> { 103 PERMISSION_USAGE_FRAGMENT_INTERACTION__ACTION__MICROPHONE_ACCESS_TIMELINE_VIEWED 104 } 105 else -> 0 106 } 107 PermissionControllerStatsLog.write(PERMISSION_USAGE_FRAGMENT_INTERACTION, sessionId, act) 108 } 109 } 110