1 /*
2 * Copyright (C) 2024 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 @file:JvmName("AppToWebUtils")
18
19 package com.android.wm.shell.apptoweb
20
21 import android.app.assist.AssistContent
22 import android.content.Context
23 import android.content.Intent
24 import android.content.Intent.ACTION_VIEW
25 import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
26 import android.content.pm.PackageManager
27 import android.content.pm.verify.domain.DomainVerificationManager
28 import android.content.pm.verify.domain.DomainVerificationUserState
29 import android.net.Uri
30 import com.android.internal.protolog.ProtoLog
31 import com.android.wm.shell.protolog.ShellProtoLogGroup
32
33 private const val TAG = "AppToWebUtils"
34
35 private val GenericBrowserIntent = Intent()
36 .setAction(ACTION_VIEW)
37 .addCategory(Intent.CATEGORY_BROWSABLE)
38 .setData(Uri.parse("http:"))
39
40 /**
41 * Returns a boolean indicating whether a given package is a browser app.
42 */
isBrowserAppnull43 fun isBrowserApp(context: Context, packageName: String, userId: Int): Boolean {
44 GenericBrowserIntent.setPackage(packageName)
45 val list = context.packageManager.queryIntentActivitiesAsUser(
46 GenericBrowserIntent, PackageManager.MATCH_ALL, userId
47 )
48
49 list.forEach {
50 if (it.activityInfo != null && it.handleAllWebDataURI) {
51 return true
52 }
53 }
54 return false
55 }
56
57 /**
58 * Returns intent if there is a browser application available to handle the uri. Otherwise, returns
59 * null.
60 */
getBrowserIntentnull61 fun getBrowserIntent(uri: Uri, packageManager: PackageManager, userId: Int): Intent? {
62 val intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER)
63 .setData(uri)
64 .addFlags(FLAG_ACTIVITY_NEW_TASK)
65 // If there is a browser application available to handle the intent, return the intent.
66 // Otherwise, return null.
67 val resolveInfo = packageManager.resolveActivityAsUser(intent, /* flags= */ 0, userId)
68 ?: return null
69 intent.setComponent(resolveInfo.componentInfo.componentName)
70 return intent
71 }
72
73 /**
74 * Returns intent if there is a non-browser application available to handle the uri. Otherwise,
75 * returns null.
76 */
getAppIntentnull77 fun getAppIntent(uri: Uri, packageManager: PackageManager, userId: Int): Intent? {
78 val intent = Intent(ACTION_VIEW, uri).addFlags(FLAG_ACTIVITY_NEW_TASK)
79 val resolveInfo = packageManager.resolveActivityAsUser(intent, /* flags= */ 0, userId)
80 ?: return null
81 // If there is a non-browser application available to handle the intent, return the intent.
82 // Otherwise, return null.
83 if (resolveInfo.activityInfo != null && !resolveInfo.handleAllWebDataURI) {
84 intent.setComponent(resolveInfo.componentInfo.componentName)
85 return intent
86 }
87 return null
88 }
89
90 /**
91 * Returns the [DomainVerificationUserState] of the user associated with the given
92 * [DomainVerificationManager] and the given package.
93 */
getDomainVerificationUserStatenull94 fun getDomainVerificationUserState(
95 manager: DomainVerificationManager,
96 packageName: String
97 ): DomainVerificationUserState? {
98 try {
99 return manager.getDomainVerificationUserState(packageName)
100 } catch (e: PackageManager.NameNotFoundException) {
101 ProtoLog.w(
102 ShellProtoLogGroup.WM_SHELL_DESKTOP_MODE,
103 "%s: Failed to get domain verification user state: %s",
104 TAG,
105 e.message!!
106 )
107 return null
108 }
109 }
110
111 /**
112 * Returns the web uri from the given [AssistContent].
113 */
AssistContentnull114 fun AssistContent.getSessionWebUri(): Uri? {
115 return sessionTransferUri ?: webUri
116 }
117