1 /*
<lambda>null2  * 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.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.outlined.Launch
24 import com.android.settings.R
25 import com.android.settingslib.spa.widget.button.ActionButton
26 import com.android.settingslib.spaprivileged.model.app.userHandle
27 
28 class AppLaunchButton(packageInfoPresenter: PackageInfoPresenter) {
29     private val context = packageInfoPresenter.context
30     private val userPackageManager = packageInfoPresenter.userPackageManager
31 
32     fun getActionButton(app: ApplicationInfo): ActionButton? =
33         userPackageManager.getLaunchIntentForPackage(app.packageName)?.let { intent ->
34             launchButton(intent, app)
35         }
36 
37     private fun launchButton(intent: Intent, app: ApplicationInfo) = ActionButton(
38         text = context.getString(R.string.launch_instant_app),
39         imageVector = Icons.Outlined.Launch,
40     ) {
41         try {
42             context.startActivityAsUser(intent, app.userHandle)
43         } catch (_: ActivityNotFoundException) {
44             // Only happens after package changes like uninstall, and before page auto refresh or
45             // close, so ignore this exception is safe.
46         }
47     }
48 }
49