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.settings.spa.app.appinfo
18 
19 import android.content.ActivityNotFoundException
20 import android.content.Intent
21 import android.content.pm.ApplicationInfo
22 import androidx.compose.material.icons.Icons
23 import androidx.compose.material.icons.automirrored.outlined.Launch
24 import androidx.compose.material3.Icon
25 import androidx.compose.material3.IconButton
26 import androidx.compose.runtime.Composable
27 import androidx.compose.ui.res.stringResource
28 import com.android.settings.R
29 import com.android.settingslib.spaprivileged.model.app.userHandle
30 
31 @Composable
TopBarAppLaunchButtonnull32 fun TopBarAppLaunchButton(packageInfoPresenter: PackageInfoPresenter, app: ApplicationInfo) {
33     val intent = packageInfoPresenter.launchIntent(app = app) ?: return
34     IconButton({ launchButtonAction(intent, app, packageInfoPresenter) }) {
35         Icon(
36             imageVector = Icons.AutoMirrored.Outlined.Launch,
37             contentDescription = stringResource(R.string.launch_instant_app),
38         )
39     }
40 }
41 
PackageInfoPresenternull42 private fun PackageInfoPresenter.launchIntent(
43     app: ApplicationInfo
44 ): Intent? {
45     return userPackageManager.getLaunchIntentForPackage(app.packageName)
46 }
47 
launchButtonActionnull48 private fun launchButtonAction(
49     intent: Intent,
50     app: ApplicationInfo,
51     packageInfoPresenter: PackageInfoPresenter
52 ) {
53     try {
54         packageInfoPresenter.context.startActivityAsUser(intent, app.userHandle)
55     } catch (_: ActivityNotFoundException) {
56         // Only happens after package changes like uninstall, and before page auto refresh or
57         // close, so ignore this exception is safe.
58     }
59 }