Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | */ |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 16 | package android.view.textclassifier.intent; |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 17 | |
| 18 | import android.annotation.Nullable; |
| 19 | import android.app.PendingIntent; |
| 20 | import android.app.RemoteAction; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.ResolveInfo; |
| 26 | import android.graphics.drawable.Icon; |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 27 | import android.os.Bundle; |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 28 | import android.text.TextUtils; |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 29 | import android.view.textclassifier.ExtrasUtils; |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 30 | import android.view.textclassifier.Log; |
| 31 | import android.view.textclassifier.TextClassification; |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 32 | import android.view.textclassifier.TextClassifier; |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 33 | |
| 34 | import com.android.internal.annotations.VisibleForTesting; |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 35 | |
| 36 | import java.util.Objects; |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Helper class to store the information from which RemoteActions are built. |
| 40 | * |
| 41 | * @hide |
| 42 | */ |
| 43 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 44 | public final class LabeledIntent { |
| 45 | private static final String TAG = "LabeledIntent"; |
| 46 | public static final int DEFAULT_REQUEST_CODE = 0; |
| 47 | private static final TitleChooser DEFAULT_TITLE_CHOOSER = |
| 48 | (labeledIntent, resolveInfo) -> { |
| 49 | if (!TextUtils.isEmpty(labeledIntent.titleWithEntity)) { |
| 50 | return labeledIntent.titleWithEntity; |
| 51 | } |
| 52 | return labeledIntent.titleWithoutEntity; |
| 53 | }; |
| 54 | |
| 55 | @Nullable |
| 56 | public final String titleWithoutEntity; |
| 57 | @Nullable |
| 58 | public final String titleWithEntity; |
| 59 | public final String description; |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 60 | @Nullable |
| 61 | public final String descriptionWithAppName; |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 62 | // Do not update this intent. |
| 63 | public final Intent intent; |
| 64 | public final int requestCode; |
| 65 | |
| 66 | /** |
| 67 | * Initializes a LabeledIntent. |
| 68 | * |
| 69 | * <p>NOTE: {@code requestCode} is required to not be {@link #DEFAULT_REQUEST_CODE} |
| 70 | * if distinguishing info (e.g. the classified text) is represented in intent extras only. |
| 71 | * In such circumstances, the request code should represent the distinguishing info |
| 72 | * (e.g. by generating a hashcode) so that the generated PendingIntent is (somewhat) |
| 73 | * unique. To be correct, the PendingIntent should be definitely unique but we try a |
| 74 | * best effort approach that avoids spamming the system with PendingIntents. |
| 75 | */ |
| 76 | // TODO: Fix the issue mentioned above so the behaviour is correct. |
| 77 | public LabeledIntent( |
| 78 | @Nullable String titleWithoutEntity, |
| 79 | @Nullable String titleWithEntity, |
| 80 | String description, |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 81 | @Nullable String descriptionWithAppName, |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 82 | Intent intent, |
| 83 | int requestCode) { |
| 84 | if (TextUtils.isEmpty(titleWithEntity) && TextUtils.isEmpty(titleWithoutEntity)) { |
| 85 | throw new IllegalArgumentException( |
| 86 | "titleWithEntity and titleWithoutEntity should not be both null"); |
| 87 | } |
| 88 | this.titleWithoutEntity = titleWithoutEntity; |
| 89 | this.titleWithEntity = titleWithEntity; |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 90 | this.description = Objects.requireNonNull(description); |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 91 | this.descriptionWithAppName = descriptionWithAppName; |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 92 | this.intent = Objects.requireNonNull(intent); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 93 | this.requestCode = requestCode; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Return the resolved result. |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 98 | * |
| 99 | * @param context the context to resolve the result's intent and action |
| 100 | * @param titleChooser for choosing an action title |
| 101 | * @param textLanguagesBundle containing language detection information |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 102 | */ |
| 103 | @Nullable |
| 104 | public Result resolve( |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 105 | Context context, |
| 106 | @Nullable TitleChooser titleChooser, |
| 107 | @Nullable Bundle textLanguagesBundle) { |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 108 | final PackageManager pm = context.getPackageManager(); |
| 109 | final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0); |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 110 | |
| 111 | if (resolveInfo == null || resolveInfo.activityInfo == null) { |
| 112 | Log.w(TAG, "resolveInfo or activityInfo is null"); |
| 113 | return null; |
| 114 | } |
| 115 | final String packageName = resolveInfo.activityInfo.packageName; |
| 116 | final String className = resolveInfo.activityInfo.name; |
| 117 | if (packageName == null || className == null) { |
| 118 | Log.w(TAG, "packageName or className is null"); |
| 119 | return null; |
| 120 | } |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 121 | Intent resolvedIntent = new Intent(intent); |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 122 | resolvedIntent.putExtra( |
| 123 | TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER, |
| 124 | getFromTextClassifierExtra(textLanguagesBundle)); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 125 | boolean shouldShowIcon = false; |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 126 | Icon icon = null; |
| 127 | if (!"android".equals(packageName)) { |
Tony Mak | 82e6002 | 2019-06-05 11:53:28 +0100 | [diff] [blame] | 128 | // We only set the component name when the package name is not resolved to "android" |
| 129 | // to workaround a bug that explicit intent with component name == ResolverActivity |
| 130 | // can't be launched on keyguard. |
| 131 | resolvedIntent.setComponent(new ComponentName(packageName, className)); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 132 | if (resolveInfo.activityInfo.getIconResource() != 0) { |
| 133 | icon = Icon.createWithResource( |
| 134 | packageName, resolveInfo.activityInfo.getIconResource()); |
| 135 | shouldShowIcon = true; |
| 136 | } |
| 137 | } |
| 138 | if (icon == null) { |
| 139 | // RemoteAction requires that there be an icon. |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 140 | icon = Icon.createWithResource( |
| 141 | "android", com.android.internal.R.drawable.ic_more_items); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 142 | } |
| 143 | final PendingIntent pendingIntent = |
| 144 | TextClassification.createPendingIntent(context, resolvedIntent, requestCode); |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 145 | titleChooser = titleChooser == null ? DEFAULT_TITLE_CHOOSER : titleChooser; |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 146 | CharSequence title = titleChooser.chooseTitle(this, resolveInfo); |
| 147 | if (TextUtils.isEmpty(title)) { |
| 148 | Log.w(TAG, "Custom titleChooser return null, fallback to the default titleChooser"); |
| 149 | title = DEFAULT_TITLE_CHOOSER.chooseTitle(this, resolveInfo); |
| 150 | } |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 151 | final RemoteAction action = |
| 152 | new RemoteAction(icon, title, resolveDescription(resolveInfo, pm), pendingIntent); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 153 | action.setShouldShowIcon(shouldShowIcon); |
| 154 | return new Result(resolvedIntent, action); |
| 155 | } |
| 156 | |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 157 | private String resolveDescription(ResolveInfo resolveInfo, PackageManager packageManager) { |
| 158 | if (!TextUtils.isEmpty(descriptionWithAppName)) { |
| 159 | // Example string format of descriptionWithAppName: "Use %1$s to open map". |
| 160 | String applicationName = getApplicationName(resolveInfo, packageManager); |
| 161 | if (!TextUtils.isEmpty(applicationName)) { |
| 162 | return String.format(descriptionWithAppName, applicationName); |
| 163 | } |
| 164 | } |
| 165 | return description; |
| 166 | } |
| 167 | |
| 168 | @Nullable |
| 169 | private String getApplicationName( |
| 170 | ResolveInfo resolveInfo, PackageManager packageManager) { |
| 171 | if (resolveInfo.activityInfo == null) { |
| 172 | return null; |
| 173 | } |
| 174 | if ("android".equals(resolveInfo.activityInfo.packageName)) { |
| 175 | return null; |
| 176 | } |
| 177 | if (resolveInfo.activityInfo.applicationInfo == null) { |
| 178 | return null; |
| 179 | } |
| 180 | return (String) packageManager.getApplicationLabel( |
| 181 | resolveInfo.activityInfo.applicationInfo); |
| 182 | } |
| 183 | |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 184 | private Bundle getFromTextClassifierExtra(@Nullable Bundle textLanguagesBundle) { |
| 185 | if (textLanguagesBundle != null) { |
| 186 | final Bundle bundle = new Bundle(); |
| 187 | ExtrasUtils.putTextLanguagesExtra(bundle, textLanguagesBundle); |
| 188 | return bundle; |
| 189 | } else { |
| 190 | return Bundle.EMPTY; |
| 191 | } |
| 192 | } |
| 193 | |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 194 | /** |
| 195 | * Data class that holds the result. |
| 196 | */ |
| 197 | public static final class Result { |
| 198 | public final Intent resolvedIntent; |
| 199 | public final RemoteAction remoteAction; |
| 200 | |
| 201 | public Result(Intent resolvedIntent, RemoteAction remoteAction) { |
Daulet Zhanguzin | e155947 | 2019-12-18 14:17:56 +0000 | [diff] [blame] | 202 | this.resolvedIntent = Objects.requireNonNull(resolvedIntent); |
| 203 | this.remoteAction = Objects.requireNonNull(remoteAction); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * An object to choose a title from resolved info. If {@code null} is returned, |
| 209 | * {@link #titleWithEntity} will be used if it exists, {@link #titleWithoutEntity} otherwise. |
| 210 | */ |
| 211 | public interface TitleChooser { |
| 212 | /** |
| 213 | * Picks a title from a {@link LabeledIntent} by looking into resolved info. |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 214 | * {@code resolveInfo} is guaranteed to have a non-null {@code activityInfo}. |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 215 | */ |
| 216 | @Nullable |
| 217 | CharSequence chooseTitle(LabeledIntent labeledIntent, ResolveInfo resolveInfo); |
| 218 | } |
| 219 | } |