1 /* 2 * Copyright (C) 2023 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.notetask 18 19 import android.app.role.RoleManager 20 import android.content.Context 21 import android.content.pm.PackageManager 22 import android.os.UserHandle 23 import android.util.Log 24 import javax.inject.Inject 25 26 internal class NoteTaskInfoResolver 27 @Inject 28 constructor( 29 private val context: Context, 30 private val roleManager: RoleManager, 31 private val packageManager: PackageManager, 32 ) { resolveInfonull33 fun resolveInfo(): NoteTaskInfo? { 34 // TODO(b/267634412): Select UserHandle depending on where the user initiated note-taking. 35 val user = context.user 36 val packageName = roleManager.getRoleHoldersAsUser(ROLE_NOTES, user).firstOrNull() 37 38 if (packageName.isNullOrEmpty()) return null 39 40 return NoteTaskInfo(packageName, packageManager.getUidOf(packageName, user)) 41 } 42 43 /** Package name and kernel user-ID of a note-taking app. */ 44 data class NoteTaskInfo(val packageName: String, val uid: Int) 45 46 companion object { 47 private val TAG = NoteTaskInfoResolver::class.simpleName.orEmpty() 48 49 private val EMPTY_APPLICATION_INFO_FLAGS = PackageManager.ApplicationInfoFlags.of(0)!! 50 51 /** 52 * Returns the kernel user-ID of [packageName] for a [user]. Returns zero if the app cannot 53 * be found. 54 */ PackageManagernull55 private fun PackageManager.getUidOf(packageName: String, user: UserHandle): Int { 56 val applicationInfo = 57 try { 58 getApplicationInfoAsUser(packageName, EMPTY_APPLICATION_INFO_FLAGS, user) 59 } catch (e: PackageManager.NameNotFoundException) { 60 Log.e(TAG, "Couldn't find notes app UID", e) 61 return 0 62 } 63 return applicationInfo.uid 64 } 65 66 // TODO(b/265912743): Use RoleManager.NOTES_ROLE instead. 67 const val ROLE_NOTES = "android.app.role.NOTES" 68 } 69 } 70