1 /* <lambda>null2 * 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.app.role.RoleManager.ROLE_NOTES 21 import android.content.Context 22 import android.content.pm.PackageManager 23 import android.content.pm.ShortcutInfo 24 import android.graphics.drawable.Icon 25 import android.os.PersistableBundle 26 import android.os.UserHandle 27 import com.android.systemui.res.R 28 import com.android.systemui.notetask.shortcut.LaunchNoteTaskActivity 29 30 /** Extension functions for [RoleManager] used **internally** by note task. */ 31 @InternalNoteTaskApi 32 internal object NoteTaskRoleManagerExt { 33 34 /** 35 * Gets package name of the default (first) app holding the [role]. If none, returns either an 36 * empty string or null. 37 */ 38 fun RoleManager.getDefaultRoleHolderAsUser(role: String, user: UserHandle): String? = 39 getRoleHoldersAsUser(role, user).firstOrNull() 40 41 /** Creates a [ShortcutInfo] for [ROLE_NOTES]. */ 42 fun RoleManager.createNoteShortcutInfoAsUser( 43 context: Context, 44 user: UserHandle, 45 ): ShortcutInfo { 46 val packageName = getDefaultRoleHolderAsUser(ROLE_NOTES, user) 47 48 val extras = PersistableBundle() 49 if (packageName != null) { 50 // Set custom app badge using the icon from ROLES_NOTES default app. 51 extras.putString(NoteTaskController.EXTRA_SHORTCUT_BADGE_OVERRIDE_PACKAGE, packageName) 52 } 53 54 val shortLabel = context.getString(R.string.note_task_button_label) 55 56 val applicationLabel = context.packageManager.getApplicationLabel(packageName) 57 val longLabel = 58 if (applicationLabel == null) { 59 shortLabel 60 } else { 61 context.getString( 62 R.string.note_task_shortcut_long_label, 63 applicationLabel, 64 ) 65 } 66 67 val icon = Icon.createWithResource(context, R.drawable.ic_note_task_shortcut_widget) 68 69 return ShortcutInfo.Builder(context, NoteTaskController.SHORTCUT_ID) 70 .setIntent(LaunchNoteTaskActivity.createIntent(context)) 71 .setActivity(LaunchNoteTaskActivity.createComponent(context)) 72 .setShortLabel(shortLabel) 73 .setLongLabel(longLabel) 74 .setLongLived(true) 75 .setIcon(icon) 76 .setExtras(extras) 77 .build() 78 } 79 80 private fun PackageManager.getApplicationLabel(packageName: String?): String? { 81 if (packageName == null) { 82 return null 83 } 84 85 return runCatching { getApplicationInfo(packageName, /* flags= */ 0)!! } 86 .getOrNull() 87 ?.let { info -> getApplicationLabel(info).toString() } 88 } 89 } 90