1 /* 2 * Copyright (C) 2020 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.systemui.privacy.logging 18 19 import android.permission.PermissionGroupUsage 20 import com.android.systemui.log.dagger.PrivacyLog 21 import com.android.systemui.plugins.log.LogBuffer 22 import com.android.systemui.plugins.log.LogLevel 23 import com.android.systemui.plugins.log.LogMessage 24 import com.android.systemui.privacy.PrivacyDialog 25 import com.android.systemui.privacy.PrivacyItem 26 import java.text.SimpleDateFormat 27 import java.util.Locale 28 import javax.inject.Inject 29 30 private const val TAG = "PrivacyLog" 31 private val DATE_FORMAT = SimpleDateFormat("MM-dd HH:mm:ss.SSS", Locale.US) 32 class PrivacyLogger @Inject constructor( 33 @PrivacyLog private val buffer: LogBuffer 34 ) { 35 logUpdatedItemFromAppOpsnull36 fun logUpdatedItemFromAppOps(code: Int, uid: Int, packageName: String, active: Boolean) { 37 log(LogLevel.INFO, { 38 int1 = code 39 int2 = uid 40 str1 = packageName 41 bool1 = active 42 }, { 43 "App Op: $int1 for $str1($int2), active=$bool1" 44 }) 45 } 46 logUpdatedItemFromMediaProjectionnull47 fun logUpdatedItemFromMediaProjection(uid: Int, packageName: String, active: Boolean) { 48 log(LogLevel.INFO, { 49 int1 = uid 50 str1 = packageName 51 bool1 = active 52 }, { 53 "MediaProjection: $str1($int1), active=$bool1" 54 }) 55 } 56 logRetrievedPrivacyItemsListnull57 fun logRetrievedPrivacyItemsList(list: List<PrivacyItem>) { 58 log(LogLevel.INFO, { 59 str1 = listToString(list) 60 }, { 61 "Retrieved list to process: $str1" 62 }) 63 } 64 logPrivacyItemsToHoldnull65 fun logPrivacyItemsToHold(list: List<PrivacyItem>) { 66 log(LogLevel.DEBUG, { 67 str1 = listToString(list) 68 }, { 69 "Holding items: $str1" 70 }) 71 } 72 logPrivacyItemsUpdateSchedulednull73 fun logPrivacyItemsUpdateScheduled(delay: Long) { 74 log(LogLevel.INFO, { 75 val scheduledFor = System.currentTimeMillis() + delay 76 val formattedTimestamp = DATE_FORMAT.format(scheduledFor) 77 str1 = formattedTimestamp 78 }, { 79 "Updating items scheduled for $str1" 80 }) 81 } 82 logCurrentProfilesChangednull83 fun logCurrentProfilesChanged(profiles: List<Int>) { 84 log(LogLevel.INFO, { 85 str1 = profiles.toString() 86 }, { 87 "Profiles changed: $str1" 88 }) 89 } 90 logChipVisiblenull91 fun logChipVisible(visible: Boolean) { 92 log(LogLevel.INFO, { 93 bool1 = visible 94 }, { 95 "Chip visible: $bool1" 96 }) 97 } 98 logStatusBarIconsVisiblenull99 fun logStatusBarIconsVisible( 100 showCamera: Boolean, 101 showMicrophone: Boolean, 102 showLocation: Boolean 103 ) { 104 log(LogLevel.INFO, { 105 bool1 = showCamera 106 bool2 = showMicrophone 107 bool3 = showLocation 108 }, { 109 "Status bar icons visible: camera=$bool1, microphone=$bool2, location=$bool3" 110 }) 111 } 112 logUnfilteredPermGroupUsagenull113 fun logUnfilteredPermGroupUsage(contents: List<PermissionGroupUsage>) { 114 log(LogLevel.DEBUG, { 115 str1 = contents.toString() 116 }, { 117 "Perm group usage: $str1" 118 }) 119 } 120 logShowDialogContentsnull121 fun logShowDialogContents(contents: List<PrivacyDialog.PrivacyElement>) { 122 log(LogLevel.INFO, { 123 str1 = contents.toString() 124 }, { 125 "Privacy dialog shown. Contents: $str1" 126 }) 127 } 128 logEmptyDialognull129 fun logEmptyDialog() { 130 log(LogLevel.WARNING, {}, { 131 "Trying to show an empty dialog" 132 }) 133 } 134 logPrivacyDialogDismissednull135 fun logPrivacyDialogDismissed() { 136 log(LogLevel.INFO, {}, { 137 "Privacy dialog dismissed" 138 }) 139 } 140 logStartSettingsActivityFromDialognull141 fun logStartSettingsActivityFromDialog(packageName: String, userId: Int) { 142 log(LogLevel.INFO, { 143 str1 = packageName 144 int1 = userId 145 }, { 146 "Start settings activity from dialog for packageName=$str1, userId=$int1 " 147 }) 148 } 149 listToStringnull150 private fun listToString(list: List<PrivacyItem>): String { 151 return list.joinToString(separator = ", ", transform = PrivacyItem::log) 152 } 153 lognull154 private inline fun log( 155 logLevel: LogLevel, 156 initializer: LogMessage.() -> Unit, 157 noinline printer: LogMessage.() -> String 158 ) { 159 buffer.log(TAG, logLevel, initializer, printer) 160 } 161 }