The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Adam Powell | e7c74cc | 2016-01-28 16:42:27 +0000 | [diff] [blame] | 17 | package com.android.internal.app; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 19 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 20 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorListenerAdapter; |
| 23 | import android.animation.AnimatorSet; |
| 24 | import android.animation.ObjectAnimator; |
| 25 | import android.animation.ValueAnimator; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 26 | import android.annotation.IntDef; |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 27 | import android.annotation.Nullable; |
Artur Satayev | fc46be7 | 2019-11-04 17:50:59 +0000 | [diff] [blame] | 28 | import android.annotation.UnsupportedAppUsage; |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 29 | import android.app.Activity; |
Ng Zhi An | d3ec5fc | 2018-05-10 09:13:00 -0700 | [diff] [blame] | 30 | import android.app.ActivityManager; |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 31 | import android.app.prediction.AppPredictionContext; |
| 32 | import android.app.prediction.AppPredictionManager; |
| 33 | import android.app.prediction.AppPredictor; |
| 34 | import android.app.prediction.AppTarget; |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 35 | import android.app.prediction.AppTargetEvent; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 36 | import android.content.ClipData; |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 37 | import android.content.ClipboardManager; |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 38 | import android.content.ComponentName; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 39 | import android.content.ContentResolver; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 40 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.content.Intent; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 42 | import android.content.IntentFilter; |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 43 | import android.content.IntentSender; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 44 | import android.content.IntentSender.SendIntentException; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 45 | import android.content.ServiceConnection; |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 46 | import android.content.pm.ActivityInfo; |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 47 | import android.content.pm.ApplicationInfo; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 48 | import android.content.pm.LabeledIntent; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 49 | import android.content.pm.LauncherApps; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 50 | import android.content.pm.PackageManager; |
| 51 | import android.content.pm.PackageManager.NameNotFoundException; |
| 52 | import android.content.pm.ResolveInfo; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 53 | import android.content.pm.ShortcutInfo; |
| 54 | import android.content.pm.ShortcutManager; |
Matt Pietal | 18bbd82 | 2019-02-12 15:21:36 -0500 | [diff] [blame] | 55 | import android.content.res.Configuration; |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 56 | import android.database.Cursor; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 57 | import android.database.DataSetObserver; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 58 | import android.graphics.Bitmap; |
| 59 | import android.graphics.Canvas; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 60 | import android.graphics.Color; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 61 | import android.graphics.Paint; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 62 | import android.graphics.Path; |
Mike Digman | ac1d88c | 2019-04-18 15:15:55 -0700 | [diff] [blame] | 63 | import android.graphics.drawable.AnimatedVectorDrawable; |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 64 | import android.graphics.drawable.BitmapDrawable; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 65 | import android.graphics.drawable.Drawable; |
Adam Powell | 13036be | 2015-05-12 14:43:56 -0700 | [diff] [blame] | 66 | import android.graphics.drawable.Icon; |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 67 | import android.metrics.LogMaker; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 68 | import android.net.Uri; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 69 | import android.os.AsyncTask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import android.os.Bundle; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 71 | import android.os.Handler; |
| 72 | import android.os.IBinder; |
| 73 | import android.os.Message; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 74 | import android.os.Parcelable; |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 75 | import android.os.Process; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 76 | import android.os.RemoteException; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 77 | import android.os.ResultReceiver; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 78 | import android.os.UserHandle; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 79 | import android.os.UserManager; |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 80 | import android.provider.DeviceConfig; |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 81 | import android.provider.DocumentsContract; |
Matt Pietal | f38e9d2 | 2019-02-15 10:01:03 -0500 | [diff] [blame] | 82 | import android.provider.Downloads; |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 83 | import android.provider.OpenableColumns; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 84 | import android.service.chooser.ChooserTarget; |
| 85 | import android.service.chooser.ChooserTargetService; |
| 86 | import android.service.chooser.IChooserTargetResult; |
| 87 | import android.service.chooser.IChooserTargetService; |
Matt Pietal | 9d50143 | 2019-04-12 10:05:29 -0400 | [diff] [blame] | 88 | import android.text.SpannableStringBuilder; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 89 | import android.text.TextUtils; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 90 | import android.util.AttributeSet; |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 91 | import android.util.HashedStringCache; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 92 | import android.util.Log; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 93 | import android.util.Size; |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 94 | import android.util.Slog; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 95 | import android.view.LayoutInflater; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 96 | import android.view.View; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 97 | import android.view.View.MeasureSpec; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 98 | import android.view.View.OnClickListener; |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 99 | import android.view.View.OnLongClickListener; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 100 | import android.view.ViewGroup; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 101 | import android.view.ViewGroup.LayoutParams; |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 102 | import android.view.animation.AccelerateInterpolator; |
| 103 | import android.view.animation.DecelerateInterpolator; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 104 | import android.widget.AbsListView; |
| 105 | import android.widget.BaseAdapter; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 106 | import android.widget.ImageView; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 107 | import android.widget.ListView; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 108 | import android.widget.TextView; |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 109 | import android.widget.Toast; |
Jason Monk | 027dcfa | 2017-06-27 18:37:35 -0400 | [diff] [blame] | 110 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 111 | import com.android.internal.R; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 112 | import com.android.internal.annotations.VisibleForTesting; |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 113 | import com.android.internal.config.sysui.SystemUiDeviceConfigFlags; |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 114 | import com.android.internal.content.PackageMonitor; |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 115 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 116 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 117 | import com.android.internal.util.ImageUtils; |
Mike Digman | 849a9d1 | 2019-04-29 11:20:48 -0700 | [diff] [blame] | 118 | import com.android.internal.widget.ResolverDrawerLayout; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 119 | |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 120 | import com.google.android.collect.Lists; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 121 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 122 | import java.io.IOException; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 123 | import java.lang.annotation.Retention; |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 124 | import java.text.Collator; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 125 | import java.util.ArrayList; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 126 | import java.util.Arrays; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 127 | import java.util.Collections; |
| 128 | import java.util.Comparator; |
George Hodulik | aa5238c | 2019-04-18 14:17:51 -0700 | [diff] [blame] | 129 | import java.util.HashMap; |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 130 | import java.util.HashSet; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 131 | import java.util.List; |
George Hodulik | aa5238c | 2019-04-18 14:17:51 -0700 | [diff] [blame] | 132 | import java.util.Map; |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 133 | import java.util.Set; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 135 | /** |
| 136 | * The Chooser Activity handles intent resolution specifically for sharing intents - |
| 137 | * for example, those generated by @see android.content.Intent#createChooser(Intent, CharSequence). |
| 138 | * |
| 139 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | public class ChooserActivity extends ResolverActivity { |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 141 | private static final String TAG = "ChooserActivity"; |
| 142 | |
Artur Satayev | fc46be7 | 2019-11-04 17:50:59 +0000 | [diff] [blame] | 143 | @UnsupportedAppUsage |
| 144 | public ChooserActivity() { |
| 145 | } |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 146 | |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 147 | /** |
| 148 | * Boolean extra to change the following behavior: Normally, ChooserActivity finishes itself |
| 149 | * in onStop when launched in a new task. If this extra is set to true, we do not finish |
| 150 | * ourselves when onStop gets called. |
| 151 | */ |
| 152 | public static final String EXTRA_PRIVATE_RETAIN_IN_ON_STOP |
| 153 | = "com.android.internal.app.ChooserActivity.EXTRA_PRIVATE_RETAIN_IN_ON_STOP"; |
| 154 | |
Mike Digman | 849a9d1 | 2019-04-29 11:20:48 -0700 | [diff] [blame] | 155 | private static final String PREF_NUM_SHEET_EXPANSIONS = "pref_num_sheet_expansions"; |
| 156 | |
Mehdi Alizadeh | 3c335a2 | 2019-01-17 16:03:19 -0800 | [diff] [blame] | 157 | private static final boolean DEBUG = false; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 158 | |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 159 | /** |
| 160 | * If {@link #USE_SHORTCUT_MANAGER_FOR_DIRECT_TARGETS} and this is set to true, |
| 161 | * {@link AppPredictionManager} will be queried for direct share targets. |
| 162 | */ |
| 163 | // TODO(b/123089490): Replace with system flag |
George Hodulik | 1428beb | 2019-05-01 17:04:23 -0700 | [diff] [blame] | 164 | private static final boolean USE_PREDICTION_MANAGER_FOR_DIRECT_TARGETS = true; |
| 165 | private static final boolean USE_PREDICTION_MANAGER_FOR_SHARE_ACTIVITIES = true; |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 166 | // TODO(b/123088566) Share these in a better way. |
| 167 | private static final String APP_PREDICTION_SHARE_UI_SURFACE = "share"; |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 168 | public static final String LAUNCH_LOCATON_DIRECT_SHARE = "direct_share"; |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 169 | private static final int APP_PREDICTION_SHARE_TARGET_QUERY_PACKAGE_LIMIT = 20; |
| 170 | public static final String APP_PREDICTION_INTENT_FILTER_KEY = "intent_filter"; |
| 171 | private AppPredictor mAppPredictor; |
| 172 | private AppPredictor.Callback mAppPredictorCallback; |
George Hodulik | aa5238c | 2019-04-18 14:17:51 -0700 | [diff] [blame] | 173 | private Map<ChooserTarget, AppTarget> mDirectShareAppTargetCache; |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 174 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 175 | /** |
| 176 | * If set to true, use ShortcutManager to retrieve the matching direct share targets, instead of |
| 177 | * binding to every ChooserTargetService implementation. |
| 178 | */ |
| 179 | // TODO(b/121287573): Replace with a system flag (setprop?) |
Mehdi Alizadeh | 8e248ad | 2019-01-17 11:41:49 -0800 | [diff] [blame] | 180 | private static final boolean USE_SHORTCUT_MANAGER_FOR_DIRECT_TARGETS = true; |
| 181 | private static final boolean USE_CHOOSER_TARGET_SERVICE_FOR_DIRECT_TARGETS = true; |
| 182 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 183 | /** |
| 184 | * The transition time between placeholders for direct share to a message |
| 185 | * indicating that non are available. |
| 186 | */ |
| 187 | private static final int NO_DIRECT_SHARE_ANIM_IN_MILLIS = 200; |
| 188 | |
Matt Pietal | 394ebd0 | 2019-05-03 07:36:21 -0400 | [diff] [blame] | 189 | private static final float DIRECT_SHARE_EXPANSION_RATE = 0.78f; |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 190 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 191 | // TODO(b/121287224): Re-evaluate this limit |
| 192 | private static final int SHARE_TARGET_QUERY_PACKAGE_LIMIT = 20; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 193 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 194 | private static final int QUERY_TARGET_SERVICE_LIMIT = 5; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 195 | |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 196 | private static final int DEFAULT_SALT_EXPIRATION_DAYS = 7; |
| 197 | private int mMaxHashSaltDays = DeviceConfig.getInt(DeviceConfig.NAMESPACE_SYSTEMUI, |
| 198 | SystemUiDeviceConfigFlags.HASH_SALT_MAX_DAYS, |
| 199 | DEFAULT_SALT_EXPIRATION_DAYS); |
| 200 | |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 201 | private Bundle mReplacementExtras; |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 202 | private IntentSender mChosenComponentSender; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 203 | private IntentSender mRefinementIntentSender; |
| 204 | private RefinementResultReceiver mRefinementResultReceiver; |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 205 | private ChooserTarget[] mCallerChooserTargets; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 206 | private ComponentName[] mFilteredComponentNames; |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 207 | |
Adam Powell | 13036be | 2015-05-12 14:43:56 -0700 | [diff] [blame] | 208 | private Intent mReferrerFillInIntent; |
| 209 | |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 210 | private long mChooserShownTime; |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 211 | protected boolean mIsSuccessfullySelected; |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 212 | |
Mehdi Alizadeh | 97fb3ed | 2019-04-25 14:52:02 -0700 | [diff] [blame] | 213 | private long mQueriedTargetServicesTimeMs; |
| 214 | private long mQueriedSharingShortcutsTimeMs; |
| 215 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 216 | private ChooserListAdapter mChooserListAdapter; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 217 | private ChooserRowAdapter mChooserRowAdapter; |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 218 | private int mChooserRowServiceSpacing; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 219 | |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 220 | private int mCurrAvailableWidth = 0; |
| 221 | |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 222 | /** {@link ChooserActivity#getBaseScore} */ |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 223 | private static final float CALLER_TARGET_SCORE_BOOST = 900.f; |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 224 | /** {@link ChooserActivity#getBaseScore} */ |
Matt Pietal | 39d181d | 2019-05-08 14:48:30 -0400 | [diff] [blame] | 225 | private static final float SHORTCUT_TARGET_SCORE_BOOST = 90.f; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 226 | private static final String TARGET_DETAILS_FRAGMENT_TAG = "targetDetailsFragment"; |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 227 | // TODO: Update to handle landscape instead of using static value |
| 228 | private static final int MAX_RANKED_TARGETS = 4; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 229 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 230 | private final List<ChooserTargetServiceConnection> mServiceConnections = new ArrayList<>(); |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 231 | private final Set<ComponentName> mServicesRequested = new HashSet<>(); |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 232 | |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 233 | private static final int MAX_LOG_RANK_POSITION = 12; |
| 234 | |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 235 | @VisibleForTesting |
| 236 | public static final int LIST_VIEW_UPDATE_INTERVAL_IN_MILLIS = 250; |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 237 | |
Matt Pietal | 4e2e363 | 2019-04-05 08:32:47 -0400 | [diff] [blame] | 238 | private static final int MAX_EXTRA_INITIAL_INTENTS = 2; |
Matt Pietal | 9a6b23d | 2019-04-19 14:47:14 -0400 | [diff] [blame] | 239 | private static final int MAX_EXTRA_CHOOSER_TARGETS = 2; |
Matt Pietal | 4e2e363 | 2019-04-05 08:32:47 -0400 | [diff] [blame] | 240 | |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 241 | private boolean mListViewDataChanged = false; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 242 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 243 | @Retention(SOURCE) |
| 244 | @IntDef({CONTENT_PREVIEW_FILE, CONTENT_PREVIEW_IMAGE, CONTENT_PREVIEW_TEXT}) |
| 245 | private @interface ContentPreviewType { |
| 246 | } |
| 247 | |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 248 | // Starting at 1 since 0 is considered "undefined" for some of the database transformations |
| 249 | // of tron logs. |
| 250 | private static final int CONTENT_PREVIEW_IMAGE = 1; |
| 251 | private static final int CONTENT_PREVIEW_FILE = 2; |
| 252 | private static final int CONTENT_PREVIEW_TEXT = 3; |
| 253 | protected MetricsLogger mMetricsLogger; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 254 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 255 | // Sorted list of DisplayResolveInfos for the alphabetical app section. |
| 256 | private List<ResolverActivity.DisplayResolveInfo> mSortedList = new ArrayList<>(); |
| 257 | |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 258 | private ContentPreviewCoordinator mPreviewCoord; |
| 259 | |
| 260 | private class ContentPreviewCoordinator { |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 261 | private static final int IMAGE_FADE_IN_MILLIS = 150; |
| 262 | private static final int IMAGE_LOAD_TIMEOUT = 1; |
| 263 | private static final int IMAGE_LOAD_INTO_VIEW = 2; |
| 264 | |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 265 | private final int mImageLoadTimeoutMillis = |
| 266 | getResources().getInteger(R.integer.config_shortAnimTime); |
| 267 | |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 268 | private final View mParentView; |
| 269 | private boolean mHideParentOnFail; |
| 270 | private boolean mAtLeastOneLoaded = false; |
| 271 | |
| 272 | class LoadUriTask { |
| 273 | public final Uri mUri; |
| 274 | public final int mImageResourceId; |
| 275 | public final int mExtraCount; |
| 276 | public final Bitmap mBmp; |
| 277 | |
| 278 | LoadUriTask(int imageResourceId, Uri uri, int extraCount, Bitmap bmp) { |
| 279 | this.mImageResourceId = imageResourceId; |
| 280 | this.mUri = uri; |
| 281 | this.mExtraCount = extraCount; |
| 282 | this.mBmp = bmp; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // If at least one image loads within the timeout period, allow other |
| 287 | // loads to continue. Otherwise terminate and optionally hide |
| 288 | // the parent area |
| 289 | private final Handler mHandler = new Handler() { |
| 290 | @Override |
| 291 | public void handleMessage(Message msg) { |
| 292 | switch (msg.what) { |
| 293 | case IMAGE_LOAD_TIMEOUT: |
| 294 | maybeHideContentPreview(); |
| 295 | break; |
| 296 | |
| 297 | case IMAGE_LOAD_INTO_VIEW: |
| 298 | if (isFinishing()) break; |
| 299 | |
| 300 | LoadUriTask task = (LoadUriTask) msg.obj; |
| 301 | RoundedRectImageView imageView = mParentView.findViewById( |
| 302 | task.mImageResourceId); |
| 303 | if (task.mBmp == null) { |
| 304 | imageView.setVisibility(View.GONE); |
| 305 | maybeHideContentPreview(); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | mAtLeastOneLoaded = true; |
| 310 | imageView.setVisibility(View.VISIBLE); |
| 311 | imageView.setAlpha(0.0f); |
| 312 | imageView.setImageBitmap(task.mBmp); |
| 313 | |
| 314 | ValueAnimator fadeAnim = ObjectAnimator.ofFloat(imageView, "alpha", 0.0f, |
| 315 | 1.0f); |
| 316 | fadeAnim.setInterpolator(new DecelerateInterpolator(1.0f)); |
| 317 | fadeAnim.setDuration(IMAGE_FADE_IN_MILLIS); |
| 318 | fadeAnim.start(); |
| 319 | |
| 320 | if (task.mExtraCount > 0) { |
| 321 | imageView.setExtraImageCount(task.mExtraCount); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | }; |
| 326 | |
| 327 | ContentPreviewCoordinator(View parentView, boolean hideParentOnFail) { |
| 328 | super(); |
| 329 | |
| 330 | this.mParentView = parentView; |
| 331 | this.mHideParentOnFail = hideParentOnFail; |
| 332 | } |
| 333 | |
| 334 | private void loadUriIntoView(final int imageResourceId, final Uri uri, |
| 335 | final int extraImages) { |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 336 | mHandler.sendEmptyMessageDelayed(IMAGE_LOAD_TIMEOUT, mImageLoadTimeoutMillis); |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 337 | |
| 338 | AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> { |
| 339 | final Bitmap bmp = loadThumbnail(uri, new Size(200, 200)); |
| 340 | final Message msg = Message.obtain(); |
| 341 | msg.what = IMAGE_LOAD_INTO_VIEW; |
| 342 | msg.obj = new LoadUriTask(imageResourceId, uri, extraImages, bmp); |
| 343 | mHandler.sendMessage(msg); |
| 344 | }); |
| 345 | } |
| 346 | |
| 347 | private void cancelLoads() { |
| 348 | mHandler.removeMessages(IMAGE_LOAD_INTO_VIEW); |
| 349 | mHandler.removeMessages(IMAGE_LOAD_TIMEOUT); |
| 350 | } |
| 351 | |
| 352 | private void maybeHideContentPreview() { |
| 353 | if (!mAtLeastOneLoaded && mHideParentOnFail) { |
| 354 | Log.i(TAG, "Hiding image preview area. Timed out waiting for preview to load" |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 355 | + " within " + mImageLoadTimeoutMillis + "ms."); |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 356 | collapseParentView(); |
| 357 | if (mChooserRowAdapter != null) { |
| 358 | mChooserRowAdapter.hideContentPreview(); |
| 359 | } |
| 360 | mHideParentOnFail = false; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | private void collapseParentView() { |
| 365 | // This will effectively hide the content preview row by forcing the height |
| 366 | // to zero. It is faster than forcing a relayout of the listview |
| 367 | final View v = mParentView; |
| 368 | int widthSpec = MeasureSpec.makeMeasureSpec(v.getWidth(), MeasureSpec.EXACTLY); |
| 369 | int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY); |
| 370 | v.measure(widthSpec, heightSpec); |
| 371 | v.getLayoutParams().height = 0; |
| 372 | v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getTop()); |
| 373 | v.invalidate(); |
| 374 | } |
| 375 | } |
| 376 | |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 377 | private final ChooserHandler mChooserHandler = new ChooserHandler(); |
| 378 | |
| 379 | private class ChooserHandler extends Handler { |
| 380 | private static final int CHOOSER_TARGET_SERVICE_RESULT = 1; |
| 381 | private static final int CHOOSER_TARGET_SERVICE_WATCHDOG_MIN_TIMEOUT = 2; |
| 382 | private static final int CHOOSER_TARGET_SERVICE_WATCHDOG_MAX_TIMEOUT = 3; |
| 383 | private static final int SHORTCUT_MANAGER_SHARE_TARGET_RESULT = 4; |
| 384 | private static final int SHORTCUT_MANAGER_SHARE_TARGET_RESULT_COMPLETED = 5; |
| 385 | private static final int LIST_VIEW_UPDATE_MESSAGE = 6; |
| 386 | |
| 387 | private static final int WATCHDOG_TIMEOUT_MAX_MILLIS = 10000; |
| 388 | private static final int WATCHDOG_TIMEOUT_MIN_MILLIS = 3000; |
| 389 | |
| 390 | private boolean mMinTimeoutPassed = false; |
| 391 | |
| 392 | private void removeAllMessages() { |
| 393 | removeMessages(LIST_VIEW_UPDATE_MESSAGE); |
| 394 | removeMessages(CHOOSER_TARGET_SERVICE_WATCHDOG_MIN_TIMEOUT); |
| 395 | removeMessages(CHOOSER_TARGET_SERVICE_WATCHDOG_MAX_TIMEOUT); |
| 396 | removeMessages(CHOOSER_TARGET_SERVICE_RESULT); |
| 397 | removeMessages(SHORTCUT_MANAGER_SHARE_TARGET_RESULT); |
| 398 | removeMessages(SHORTCUT_MANAGER_SHARE_TARGET_RESULT_COMPLETED); |
| 399 | } |
| 400 | |
| 401 | private void restartServiceRequestTimer() { |
| 402 | mMinTimeoutPassed = false; |
| 403 | removeMessages(CHOOSER_TARGET_SERVICE_WATCHDOG_MIN_TIMEOUT); |
| 404 | removeMessages(CHOOSER_TARGET_SERVICE_WATCHDOG_MAX_TIMEOUT); |
| 405 | |
| 406 | if (DEBUG) { |
| 407 | Log.d(TAG, "queryTargets setting watchdog timer for " |
| 408 | + WATCHDOG_TIMEOUT_MIN_MILLIS + "-" |
| 409 | + WATCHDOG_TIMEOUT_MAX_MILLIS + "ms"); |
| 410 | } |
| 411 | |
| 412 | sendEmptyMessageDelayed(CHOOSER_TARGET_SERVICE_WATCHDOG_MIN_TIMEOUT, |
| 413 | WATCHDOG_TIMEOUT_MIN_MILLIS); |
| 414 | sendEmptyMessageDelayed(CHOOSER_TARGET_SERVICE_WATCHDOG_MAX_TIMEOUT, |
| 415 | WATCHDOG_TIMEOUT_MAX_MILLIS); |
| 416 | } |
| 417 | |
| 418 | private void maybeStopServiceRequestTimer() { |
| 419 | // Set a minimum timeout threshold, to ensure both apis, sharing shortcuts |
| 420 | // and older-style direct share services, have had time to load, otherwise |
| 421 | // just checking mServiceConnections could force us to end prematurely |
| 422 | if (mMinTimeoutPassed && mServiceConnections.isEmpty()) { |
| 423 | logDirectShareTargetReceived( |
| 424 | MetricsEvent.ACTION_DIRECT_SHARE_TARGETS_LOADED_CHOOSER_SERVICE); |
| 425 | sendVoiceChoicesIfNeeded(); |
| 426 | mChooserListAdapter.completeServiceTargetLoading(); |
| 427 | } |
| 428 | } |
| 429 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 430 | @Override |
| 431 | public void handleMessage(Message msg) { |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 432 | if (mChooserListAdapter == null || isDestroyed()) { |
| 433 | return; |
| 434 | } |
| 435 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 436 | switch (msg.what) { |
| 437 | case CHOOSER_TARGET_SERVICE_RESULT: |
| 438 | if (DEBUG) Log.d(TAG, "CHOOSER_TARGET_SERVICE_RESULT"); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 439 | final ServiceResultInfo sri = (ServiceResultInfo) msg.obj; |
| 440 | if (!mServiceConnections.contains(sri.connection)) { |
| 441 | Log.w(TAG, "ChooserTargetServiceConnection " + sri.connection |
| 442 | + " returned after being removed from active connections." |
| 443 | + " Have you considered returning results faster?"); |
| 444 | break; |
| 445 | } |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 446 | if (sri.resultTargets != null) { |
| 447 | mChooserListAdapter.addServiceResults(sri.originalTarget, |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 448 | sri.resultTargets, false); |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 449 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 450 | unbindService(sri.connection); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 451 | sri.connection.destroy(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 452 | mServiceConnections.remove(sri.connection); |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 453 | maybeStopServiceRequestTimer(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 454 | break; |
| 455 | |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 456 | case CHOOSER_TARGET_SERVICE_WATCHDOG_MIN_TIMEOUT: |
| 457 | mMinTimeoutPassed = true; |
| 458 | maybeStopServiceRequestTimer(); |
| 459 | break; |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 460 | |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 461 | case CHOOSER_TARGET_SERVICE_WATCHDOG_MAX_TIMEOUT: |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 462 | unbindRemainingServices(); |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 463 | maybeStopServiceRequestTimer(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 464 | break; |
| 465 | |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 466 | case LIST_VIEW_UPDATE_MESSAGE: |
| 467 | if (DEBUG) { |
| 468 | Log.d(TAG, "LIST_VIEW_UPDATE_MESSAGE; "); |
| 469 | } |
| 470 | |
| 471 | mChooserListAdapter.refreshListView(); |
| 472 | break; |
| 473 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 474 | case SHORTCUT_MANAGER_SHARE_TARGET_RESULT: |
| 475 | if (DEBUG) Log.d(TAG, "SHORTCUT_MANAGER_SHARE_TARGET_RESULT"); |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 476 | final ServiceResultInfo resultInfo = (ServiceResultInfo) msg.obj; |
| 477 | if (resultInfo.resultTargets != null) { |
| 478 | mChooserListAdapter.addServiceResults(resultInfo.originalTarget, |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 479 | resultInfo.resultTargets, true); |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 480 | } |
Mehdi Alizadeh | 8e248ad | 2019-01-17 11:41:49 -0800 | [diff] [blame] | 481 | break; |
| 482 | |
| 483 | case SHORTCUT_MANAGER_SHARE_TARGET_RESULT_COMPLETED: |
Mehdi Alizadeh | 97fb3ed | 2019-04-25 14:52:02 -0700 | [diff] [blame] | 484 | logDirectShareTargetReceived( |
| 485 | MetricsEvent.ACTION_DIRECT_SHARE_TARGETS_LOADED_SHORTCUT_MANAGER); |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 486 | sendVoiceChoicesIfNeeded(); |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 487 | break; |
| 488 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 489 | default: |
| 490 | super.handleMessage(msg); |
| 491 | } |
| 492 | } |
| 493 | }; |
| 494 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | @Override |
| 496 | protected void onCreate(Bundle savedInstanceState) { |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 497 | final long intentReceivedTime = System.currentTimeMillis(); |
| 498 | mIsSuccessfullySelected = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | Intent intent = getIntent(); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 500 | Parcelable targetParcelable = intent.getParcelableExtra(Intent.EXTRA_INTENT); |
| 501 | if (!(targetParcelable instanceof Intent)) { |
Christopher Tate | 9d6376a | 2014-02-12 13:14:10 -0800 | [diff] [blame] | 502 | Log.w("ChooserActivity", "Target is not an intent: " + targetParcelable); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 503 | finish(); |
Christopher Tate | 9d6376a | 2014-02-12 13:14:10 -0800 | [diff] [blame] | 504 | super.onCreate(null); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 505 | return; |
| 506 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 507 | Intent target = (Intent) targetParcelable; |
Craig Mautner | 411d2aed | 2014-05-08 09:07:43 -0700 | [diff] [blame] | 508 | if (target != null) { |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 509 | modifyTargetIntent(target); |
Craig Mautner | 411d2aed | 2014-05-08 09:07:43 -0700 | [diff] [blame] | 510 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 511 | Parcelable[] targetsParcelable |
| 512 | = intent.getParcelableArrayExtra(Intent.EXTRA_ALTERNATE_INTENTS); |
| 513 | if (targetsParcelable != null) { |
| 514 | final boolean offset = target == null; |
| 515 | Intent[] additionalTargets = |
| 516 | new Intent[offset ? targetsParcelable.length - 1 : targetsParcelable.length]; |
| 517 | for (int i = 0; i < targetsParcelable.length; i++) { |
| 518 | if (!(targetsParcelable[i] instanceof Intent)) { |
| 519 | Log.w(TAG, "EXTRA_ALTERNATE_INTENTS array entry #" + i + " is not an Intent: " |
| 520 | + targetsParcelable[i]); |
| 521 | finish(); |
| 522 | super.onCreate(null); |
| 523 | return; |
| 524 | } |
| 525 | final Intent additionalTarget = (Intent) targetsParcelable[i]; |
| 526 | if (i == 0 && target == null) { |
| 527 | target = additionalTarget; |
| 528 | modifyTargetIntent(target); |
| 529 | } else { |
| 530 | additionalTargets[offset ? i - 1 : i] = additionalTarget; |
| 531 | modifyTargetIntent(additionalTarget); |
| 532 | } |
| 533 | } |
| 534 | setAdditionalTargets(additionalTargets); |
| 535 | } |
| 536 | |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 537 | mReplacementExtras = intent.getBundleExtra(Intent.EXTRA_REPLACEMENT_EXTRAS); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 538 | |
| 539 | // Do not allow the title to be changed when sharing content |
| 540 | CharSequence title = null; |
| 541 | if (target != null) { |
Matt Pietal | 95574b0 | 2019-03-13 08:12:25 -0400 | [diff] [blame] | 542 | if (!isSendAction(target)) { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 543 | title = intent.getCharSequenceExtra(Intent.EXTRA_TITLE); |
| 544 | } else { |
| 545 | Log.w(TAG, "Ignoring intent's EXTRA_TITLE, deprecated in P. You may wish to set a" |
| 546 | + " preview title by using EXTRA_TITLE property of the wrapped" |
| 547 | + " EXTRA_INTENT."); |
| 548 | } |
| 549 | } |
| 550 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 551 | int defaultTitleRes = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | if (title == null) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 553 | defaultTitleRes = com.android.internal.R.string.chooseActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | } |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 555 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 556 | Parcelable[] pa = intent.getParcelableArrayExtra(Intent.EXTRA_INITIAL_INTENTS); |
| 557 | Intent[] initialIntents = null; |
| 558 | if (pa != null) { |
Matt Pietal | 4e2e363 | 2019-04-05 08:32:47 -0400 | [diff] [blame] | 559 | int count = Math.min(pa.length, MAX_EXTRA_INITIAL_INTENTS); |
| 560 | initialIntents = new Intent[count]; |
| 561 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 562 | if (!(pa[i] instanceof Intent)) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 563 | Log.w(TAG, "Initial intent #" + i + " not an Intent: " + pa[i]); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 564 | finish(); |
Christopher Tate | 9d6376a | 2014-02-12 13:14:10 -0800 | [diff] [blame] | 565 | super.onCreate(null); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 566 | return; |
| 567 | } |
Craig Mautner | 411d2aed | 2014-05-08 09:07:43 -0700 | [diff] [blame] | 568 | final Intent in = (Intent) pa[i]; |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 569 | modifyTargetIntent(in); |
Craig Mautner | 411d2aed | 2014-05-08 09:07:43 -0700 | [diff] [blame] | 570 | initialIntents[i] = in; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 573 | |
Adam Powell | 13036be | 2015-05-12 14:43:56 -0700 | [diff] [blame] | 574 | mReferrerFillInIntent = new Intent().putExtra(Intent.EXTRA_REFERRER, getReferrer()); |
| 575 | |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 576 | mChosenComponentSender = intent.getParcelableExtra( |
| 577 | Intent.EXTRA_CHOSEN_COMPONENT_INTENT_SENDER); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 578 | mRefinementIntentSender = intent.getParcelableExtra( |
| 579 | Intent.EXTRA_CHOOSER_REFINEMENT_INTENT_SENDER); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 580 | setSafeForwardingMode(true); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 581 | |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 582 | pa = intent.getParcelableArrayExtra(Intent.EXTRA_EXCLUDE_COMPONENTS); |
| 583 | if (pa != null) { |
| 584 | ComponentName[] names = new ComponentName[pa.length]; |
| 585 | for (int i = 0; i < pa.length; i++) { |
| 586 | if (!(pa[i] instanceof ComponentName)) { |
| 587 | Log.w(TAG, "Filtered component #" + i + " not a ComponentName: " + pa[i]); |
| 588 | names = null; |
| 589 | break; |
| 590 | } |
| 591 | names[i] = (ComponentName) pa[i]; |
| 592 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 593 | mFilteredComponentNames = names; |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | pa = intent.getParcelableArrayExtra(Intent.EXTRA_CHOOSER_TARGETS); |
| 597 | if (pa != null) { |
Matt Pietal | 9a6b23d | 2019-04-19 14:47:14 -0400 | [diff] [blame] | 598 | int count = Math.min(pa.length, MAX_EXTRA_CHOOSER_TARGETS); |
| 599 | ChooserTarget[] targets = new ChooserTarget[count]; |
| 600 | for (int i = 0; i < count; i++) { |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 601 | if (!(pa[i] instanceof ChooserTarget)) { |
| 602 | Log.w(TAG, "Chooser target #" + i + " not a ChooserTarget: " + pa[i]); |
| 603 | targets = null; |
| 604 | break; |
| 605 | } |
| 606 | targets[i] = (ChooserTarget) pa[i]; |
| 607 | } |
| 608 | mCallerChooserTargets = targets; |
| 609 | } |
| 610 | |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 611 | setRetainInOnStop(intent.getBooleanExtra(EXTRA_PRIVATE_RETAIN_IN_ON_STOP, false)); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 612 | super.onCreate(savedInstanceState, target, title, defaultTitleRes, initialIntents, |
| 613 | null, false); |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 614 | |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 615 | mChooserShownTime = System.currentTimeMillis(); |
| 616 | final long systemCost = mChooserShownTime - intentReceivedTime; |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 617 | |
| 618 | getMetricsLogger().write(new LogMaker(MetricsEvent.ACTION_ACTIVITY_CHOOSER_SHOWN) |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 619 | .setSubtype(isWorkProfile() ? MetricsEvent.MANAGED_PROFILE : |
| 620 | MetricsEvent.PARENT_PROFILE) |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 621 | .addTaggedData(MetricsEvent.FIELD_SHARESHEET_MIMETYPE, target.getType()) |
| 622 | .addTaggedData(MetricsEvent.FIELD_TIME_TO_APP_TARGETS, systemCost)); |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 623 | |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 624 | AppPredictor appPredictor = getAppPredictorForDirectShareIfEnabled(); |
| 625 | if (appPredictor != null) { |
George Hodulik | aa5238c | 2019-04-18 14:17:51 -0700 | [diff] [blame] | 626 | mDirectShareAppTargetCache = new HashMap<>(); |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 627 | mAppPredictorCallback = resultList -> { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 628 | if (isFinishing() || isDestroyed()) { |
| 629 | return; |
| 630 | } |
George Hodulik | 0dd5fbe | 2019-03-06 12:00:26 -0800 | [diff] [blame] | 631 | // May be null if there are no apps to perform share/open action. |
| 632 | if (mChooserListAdapter == null) { |
| 633 | return; |
| 634 | } |
George Hodulik | 3f399f2 | 2019-04-26 16:17:54 -0700 | [diff] [blame] | 635 | if (resultList.isEmpty()) { |
| 636 | // APS may be disabled, so try querying targets ourselves. |
| 637 | queryDirectShareTargets(mChooserListAdapter, true); |
| 638 | return; |
| 639 | } |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 640 | final List<DisplayResolveInfo> driList = |
| 641 | getDisplayResolveInfos(mChooserListAdapter); |
| 642 | final List<ShortcutManager.ShareShortcutInfo> shareShortcutInfos = |
| 643 | new ArrayList<>(); |
| 644 | for (AppTarget appTarget : resultList) { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 645 | if (appTarget.getShortcutInfo() == null) { |
| 646 | continue; |
| 647 | } |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 648 | shareShortcutInfos.add(new ShortcutManager.ShareShortcutInfo( |
| 649 | appTarget.getShortcutInfo(), |
| 650 | new ComponentName( |
| 651 | appTarget.getPackageName(), appTarget.getClassName()))); |
| 652 | } |
George Hodulik | aa5238c | 2019-04-18 14:17:51 -0700 | [diff] [blame] | 653 | sendShareShortcutInfoList(shareShortcutInfos, driList, resultList); |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 654 | }; |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 655 | appPredictor |
| 656 | .registerPredictionUpdates(this.getMainExecutor(), mAppPredictorCallback); |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 657 | } |
| 658 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 659 | mChooserRowServiceSpacing = getResources() |
| 660 | .getDimensionPixelSize(R.dimen.chooser_service_spacing); |
| 661 | |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 662 | if (mResolverDrawerLayout != null) { |
| 663 | mResolverDrawerLayout.addOnLayoutChangeListener(this::handleLayoutChange); |
| 664 | |
| 665 | // expand/shrink direct share 4 -> 8 viewgroup |
| 666 | if (isSendAction(target)) { |
| 667 | mResolverDrawerLayout.setOnScrollChangeListener(this::handleScroll); |
| 668 | } |
Matt Pietal | b1d629d | 2019-04-23 11:35:53 -0400 | [diff] [blame] | 669 | |
| 670 | final View chooserHeader = mResolverDrawerLayout.findViewById(R.id.chooser_header); |
| 671 | final float defaultElevation = chooserHeader.getElevation(); |
| 672 | final float chooserHeaderScrollElevation = |
| 673 | getResources().getDimensionPixelSize(R.dimen.chooser_header_scroll_elevation); |
| 674 | |
| 675 | mAdapterView.setOnScrollListener(new AbsListView.OnScrollListener() { |
| 676 | public void onScrollStateChanged(AbsListView view, int scrollState) { |
| 677 | } |
| 678 | |
| 679 | public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, |
| 680 | int totalItemCount) { |
| 681 | if (view.getChildCount() > 0) { |
| 682 | if (firstVisibleItem > 0 || view.getChildAt(0).getTop() < 0) { |
| 683 | chooserHeader.setElevation(chooserHeaderScrollElevation); |
| 684 | return; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | chooserHeader.setElevation(defaultElevation); |
| 689 | } |
| 690 | }); |
Mike Digman | 849a9d1 | 2019-04-29 11:20:48 -0700 | [diff] [blame] | 691 | |
| 692 | mResolverDrawerLayout.setOnCollapsedChangedListener( |
| 693 | new ResolverDrawerLayout.OnCollapsedChangedListener() { |
| 694 | |
| 695 | // Only consider one expansion per activity creation |
| 696 | private boolean mWrittenOnce = false; |
| 697 | |
| 698 | @Override |
| 699 | public void onCollapsedChanged(boolean isCollapsed) { |
| 700 | if (!isCollapsed && !mWrittenOnce) { |
| 701 | incrementNumSheetExpansions(); |
| 702 | mWrittenOnce = true; |
| 703 | } |
| 704 | } |
| 705 | }); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 708 | if (DEBUG) { |
| 709 | Log.d(TAG, "System Time Cost is " + systemCost); |
| 710 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | } |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 712 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 713 | /** |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 714 | * Check if the profile currently used is a work profile. |
| 715 | * @return true if it is work profile, false if it is parent profile (or no work profile is |
| 716 | * set up) |
| 717 | */ |
| 718 | protected boolean isWorkProfile() { |
| 719 | return ((UserManager) getSystemService(Context.USER_SERVICE)) |
| 720 | .getUserInfo(UserHandle.myUserId()).isManagedProfile(); |
| 721 | } |
| 722 | |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 723 | @Override |
| 724 | protected PackageMonitor createPackageMonitor() { |
| 725 | return new PackageMonitor() { |
| 726 | @Override |
| 727 | public void onSomePackagesChanged() { |
| 728 | mAdapter.handlePackagesChanged(); |
| 729 | bindProfileView(); |
| 730 | } |
| 731 | }; |
| 732 | } |
| 733 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 734 | private void onCopyButtonClicked(View v) { |
| 735 | Intent targetIntent = getTargetIntent(); |
| 736 | if (targetIntent == null) { |
| 737 | finish(); |
| 738 | } else { |
| 739 | final String action = targetIntent.getAction(); |
| 740 | |
| 741 | ClipData clipData = null; |
| 742 | if (Intent.ACTION_SEND.equals(action)) { |
| 743 | String extraText = targetIntent.getStringExtra(Intent.EXTRA_TEXT); |
| 744 | Uri extraStream = targetIntent.getParcelableExtra(Intent.EXTRA_STREAM); |
| 745 | |
| 746 | if (extraText != null) { |
| 747 | clipData = ClipData.newPlainText(null, extraText); |
| 748 | } else if (extraStream != null) { |
| 749 | clipData = ClipData.newUri(getContentResolver(), null, extraStream); |
| 750 | } else { |
| 751 | Log.w(TAG, "No data available to copy to clipboard"); |
| 752 | return; |
| 753 | } |
| 754 | } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) { |
| 755 | final ArrayList<Uri> streams = targetIntent.getParcelableArrayListExtra( |
| 756 | Intent.EXTRA_STREAM); |
| 757 | clipData = ClipData.newUri(getContentResolver(), null, streams.get(0)); |
| 758 | for (int i = 1; i < streams.size(); i++) { |
| 759 | clipData.addItem(getContentResolver(), new ClipData.Item(streams.get(i))); |
| 760 | } |
| 761 | } else { |
| 762 | // expected to only be visible with ACTION_SEND or ACTION_SEND_MULTIPLE |
| 763 | // so warn about unexpected action |
| 764 | Log.w(TAG, "Action (" + action + ") not supported for copying to clipboard"); |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | ClipboardManager clipboardManager = (ClipboardManager) getSystemService( |
| 769 | Context.CLIPBOARD_SERVICE); |
| 770 | clipboardManager.setPrimaryClip(clipData); |
| 771 | Toast.makeText(getApplicationContext(), R.string.copied, Toast.LENGTH_SHORT).show(); |
| 772 | |
| 773 | finish(); |
| 774 | } |
| 775 | } |
| 776 | |
Matt Pietal | 18bbd82 | 2019-02-12 15:21:36 -0500 | [diff] [blame] | 777 | @Override |
| 778 | public void onConfigurationChanged(Configuration newConfig) { |
| 779 | super.onConfigurationChanged(newConfig); |
| 780 | |
Matt Pietal | 3e4b56f | 2019-05-31 12:06:17 -0400 | [diff] [blame] | 781 | adjustPreviewWidth(newConfig.orientation, null); |
| 782 | } |
| 783 | |
| 784 | private boolean shouldDisplayLandscape(int orientation) { |
| 785 | // Sharesheet fixes the # of items per row and therefore can not correctly lay out |
| 786 | // when in the restricted size of multi-window mode. In the future, would be nice |
| 787 | // to use minimum dp size requirements instead |
| 788 | return orientation == Configuration.ORIENTATION_LANDSCAPE && !isInMultiWindowMode(); |
| 789 | } |
| 790 | |
| 791 | private void adjustPreviewWidth(int orientation, View parent) { |
Matt Pietal | 18bbd82 | 2019-02-12 15:21:36 -0500 | [diff] [blame] | 792 | int width = -1; |
Matt Pietal | 3e4b56f | 2019-05-31 12:06:17 -0400 | [diff] [blame] | 793 | if (shouldDisplayLandscape(orientation)) { |
Matt Pietal | 18bbd82 | 2019-02-12 15:21:36 -0500 | [diff] [blame] | 794 | width = getResources().getDimensionPixelSize(R.dimen.chooser_preview_width); |
| 795 | } |
| 796 | |
Matt Pietal | 3e4b56f | 2019-05-31 12:06:17 -0400 | [diff] [blame] | 797 | parent = parent == null ? getWindow().getDecorView() : parent; |
| 798 | |
| 799 | updateLayoutWidth(R.id.content_preview_text_layout, width, parent); |
| 800 | updateLayoutWidth(R.id.content_preview_title_layout, width, parent); |
| 801 | updateLayoutWidth(R.id.content_preview_file_layout, width, parent); |
Matt Pietal | 18bbd82 | 2019-02-12 15:21:36 -0500 | [diff] [blame] | 802 | } |
| 803 | |
Matt Pietal | 3e4b56f | 2019-05-31 12:06:17 -0400 | [diff] [blame] | 804 | private void updateLayoutWidth(int layoutResourceId, int width, View parent) { |
| 805 | View view = parent.findViewById(layoutResourceId); |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 806 | if (view != null && view.getLayoutParams() != null) { |
| 807 | LayoutParams params = view.getLayoutParams(); |
| 808 | params.width = width; |
| 809 | view.setLayoutParams(params); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 813 | private ViewGroup displayContentPreview(@ContentPreviewType int previewType, |
| 814 | Intent targetIntent, LayoutInflater layoutInflater, ViewGroup convertView, |
| 815 | ViewGroup parent) { |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 816 | if (convertView != null) return convertView; |
| 817 | |
Matt Pietal | 3e4b56f | 2019-05-31 12:06:17 -0400 | [diff] [blame] | 818 | ViewGroup layout = null; |
| 819 | |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 820 | switch (previewType) { |
| 821 | case CONTENT_PREVIEW_TEXT: |
Matt Pietal | 3e4b56f | 2019-05-31 12:06:17 -0400 | [diff] [blame] | 822 | layout = displayTextContentPreview(targetIntent, layoutInflater, parent); |
| 823 | break; |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 824 | case CONTENT_PREVIEW_IMAGE: |
Matt Pietal | 3e4b56f | 2019-05-31 12:06:17 -0400 | [diff] [blame] | 825 | layout = displayImageContentPreview(targetIntent, layoutInflater, parent); |
| 826 | break; |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 827 | case CONTENT_PREVIEW_FILE: |
Matt Pietal | 3e4b56f | 2019-05-31 12:06:17 -0400 | [diff] [blame] | 828 | layout = displayFileContentPreview(targetIntent, layoutInflater, parent); |
| 829 | break; |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 830 | default: |
| 831 | Log.e(TAG, "Unexpected content preview type: " + previewType); |
| 832 | } |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 833 | |
Matt Pietal | 3e4b56f | 2019-05-31 12:06:17 -0400 | [diff] [blame] | 834 | if (layout != null) { |
| 835 | adjustPreviewWidth(getResources().getConfiguration().orientation, layout); |
| 836 | } |
| 837 | |
| 838 | return layout; |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | private ViewGroup displayTextContentPreview(Intent targetIntent, LayoutInflater layoutInflater, |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 842 | ViewGroup parent) { |
| 843 | ViewGroup contentPreviewLayout = (ViewGroup) layoutInflater.inflate( |
| 844 | R.layout.chooser_grid_preview_text, parent, false); |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 845 | |
| 846 | contentPreviewLayout.findViewById(R.id.copy_button).setOnClickListener( |
| 847 | this::onCopyButtonClicked); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 848 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 849 | CharSequence sharingText = targetIntent.getCharSequenceExtra(Intent.EXTRA_TEXT); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 850 | if (sharingText == null) { |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 851 | contentPreviewLayout.findViewById(R.id.content_preview_text_layout).setVisibility( |
| 852 | View.GONE); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 853 | } else { |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 854 | TextView textView = contentPreviewLayout.findViewById(R.id.content_preview_text); |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 855 | textView.setText(sharingText); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | String previewTitle = targetIntent.getStringExtra(Intent.EXTRA_TITLE); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 859 | if (TextUtils.isEmpty(previewTitle)) { |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 860 | contentPreviewLayout.findViewById(R.id.content_preview_title_layout).setVisibility( |
| 861 | View.GONE); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 862 | } else { |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 863 | TextView previewTitleView = contentPreviewLayout.findViewById( |
| 864 | R.id.content_preview_title); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 865 | previewTitleView.setText(previewTitle); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 866 | |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 867 | ClipData previewData = targetIntent.getClipData(); |
| 868 | Uri previewThumbnail = null; |
| 869 | if (previewData != null) { |
| 870 | if (previewData.getItemCount() > 0) { |
| 871 | ClipData.Item previewDataItem = previewData.getItemAt(0); |
| 872 | previewThumbnail = previewDataItem.getUri(); |
| 873 | } |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 874 | } |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 875 | |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 876 | ImageView previewThumbnailView = contentPreviewLayout.findViewById( |
| 877 | R.id.content_preview_thumbnail); |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 878 | if (previewThumbnail == null) { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 879 | previewThumbnailView.setVisibility(View.GONE); |
| 880 | } else { |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 881 | mPreviewCoord = new ContentPreviewCoordinator(contentPreviewLayout, false); |
| 882 | mPreviewCoord.loadUriIntoView(R.id.content_preview_thumbnail, previewThumbnail, 0); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 883 | } |
| 884 | } |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 885 | |
| 886 | return contentPreviewLayout; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 887 | } |
| 888 | |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 889 | private ViewGroup displayImageContentPreview(Intent targetIntent, LayoutInflater layoutInflater, |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 890 | ViewGroup parent) { |
| 891 | ViewGroup contentPreviewLayout = (ViewGroup) layoutInflater.inflate( |
| 892 | R.layout.chooser_grid_preview_image, parent, false); |
| 893 | mPreviewCoord = new ContentPreviewCoordinator(contentPreviewLayout, true); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 894 | |
| 895 | String action = targetIntent.getAction(); |
| 896 | if (Intent.ACTION_SEND.equals(action)) { |
| 897 | Uri uri = targetIntent.getParcelableExtra(Intent.EXTRA_STREAM); |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 898 | mPreviewCoord.loadUriIntoView(R.id.content_preview_image_1_large, uri, 0); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 899 | } else { |
| 900 | ContentResolver resolver = getContentResolver(); |
| 901 | |
| 902 | List<Uri> uris = targetIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); |
| 903 | List<Uri> imageUris = new ArrayList<>(); |
| 904 | for (Uri uri : uris) { |
| 905 | if (isImageType(resolver.getType(uri))) { |
| 906 | imageUris.add(uri); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | if (imageUris.size() == 0) { |
| 911 | Log.i(TAG, "Attempted to display image preview area with zero" |
| 912 | + " available images detected in EXTRA_STREAM list"); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 913 | contentPreviewLayout.setVisibility(View.GONE); |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 914 | return contentPreviewLayout; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 915 | } |
| 916 | |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 917 | mPreviewCoord.loadUriIntoView(R.id.content_preview_image_1_large, imageUris.get(0), 0); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 918 | |
| 919 | if (imageUris.size() == 2) { |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 920 | mPreviewCoord.loadUriIntoView(R.id.content_preview_image_2_large, |
| 921 | imageUris.get(1), 0); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 922 | } else if (imageUris.size() > 2) { |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 923 | mPreviewCoord.loadUriIntoView(R.id.content_preview_image_2_small, |
| 924 | imageUris.get(1), 0); |
| 925 | mPreviewCoord.loadUriIntoView(R.id.content_preview_image_3_small, |
| 926 | imageUris.get(2), imageUris.size() - 3); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 927 | } |
| 928 | } |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 929 | |
| 930 | return contentPreviewLayout; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 931 | } |
| 932 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 933 | private static class FileInfo { |
| 934 | public final String name; |
| 935 | public final boolean hasThumbnail; |
| 936 | |
| 937 | FileInfo(String name, boolean hasThumbnail) { |
| 938 | this.name = name; |
| 939 | this.hasThumbnail = hasThumbnail; |
| 940 | } |
| 941 | } |
| 942 | |
Matt Pietal | f38e9d2 | 2019-02-15 10:01:03 -0500 | [diff] [blame] | 943 | /** |
| 944 | * Wrapping the ContentResolver call to expose for easier mocking, |
| 945 | * and to avoid mocking Android core classes. |
| 946 | */ |
| 947 | @VisibleForTesting |
| 948 | public Cursor queryResolver(ContentResolver resolver, Uri uri) { |
| 949 | return resolver.query(uri, null, null, null, null); |
| 950 | } |
| 951 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 952 | private FileInfo extractFileInfo(Uri uri, ContentResolver resolver) { |
| 953 | String fileName = null; |
| 954 | boolean hasThumbnail = false; |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 955 | |
Matt Pietal | f38e9d2 | 2019-02-15 10:01:03 -0500 | [diff] [blame] | 956 | try (Cursor cursor = queryResolver(resolver, uri)) { |
| 957 | if (cursor != null && cursor.getCount() > 0) { |
| 958 | int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); |
| 959 | int titleIndex = cursor.getColumnIndex(Downloads.Impl.COLUMN_TITLE); |
| 960 | int flagsIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_FLAGS); |
| 961 | |
| 962 | cursor.moveToFirst(); |
| 963 | if (nameIndex != -1) { |
| 964 | fileName = cursor.getString(nameIndex); |
| 965 | } else if (titleIndex != -1) { |
| 966 | fileName = cursor.getString(titleIndex); |
| 967 | } |
| 968 | |
| 969 | if (flagsIndex != -1) { |
| 970 | hasThumbnail = (cursor.getInt(flagsIndex) |
| 971 | & DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL) != 0; |
| 972 | } |
| 973 | } |
Matt Pietal | 73a873f | 2019-03-15 08:46:20 -0400 | [diff] [blame] | 974 | } catch (SecurityException | NullPointerException e) { |
Matt Pietal | 62532e5 | 2019-05-07 09:51:37 -0400 | [diff] [blame] | 975 | logContentPreviewWarning(uri); |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 976 | } |
| 977 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 978 | if (TextUtils.isEmpty(fileName)) { |
| 979 | fileName = uri.getPath(); |
| 980 | int index = fileName.lastIndexOf('/'); |
| 981 | if (index != -1) { |
| 982 | fileName = fileName.substring(index + 1); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | return new FileInfo(fileName, hasThumbnail); |
| 987 | } |
| 988 | |
Matt Pietal | 62532e5 | 2019-05-07 09:51:37 -0400 | [diff] [blame] | 989 | private void logContentPreviewWarning(Uri uri) { |
| 990 | // The ContentResolver already logs the exception. Log something more informative. |
| 991 | Log.w(TAG, "Could not load (" + uri.toString() + ") thumbnail/name for preview. If " |
| 992 | + "desired, consider using Intent#createChooser to launch the ChooserActivity, " |
| 993 | + "and set your Intent's clipData and flags in accordance with that method's " |
| 994 | + "documentation"); |
| 995 | } |
| 996 | |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 997 | private ViewGroup displayFileContentPreview(Intent targetIntent, LayoutInflater layoutInflater, |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 998 | ViewGroup parent) { |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 999 | |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 1000 | ViewGroup contentPreviewLayout = (ViewGroup) layoutInflater.inflate( |
| 1001 | R.layout.chooser_grid_preview_file, parent, false); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 1002 | |
| 1003 | // TODO(b/120417119): Disable file copy until after moving to sysui, |
| 1004 | // due to permissions issues |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 1005 | contentPreviewLayout.findViewById(R.id.file_copy_button).setVisibility(View.GONE); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 1006 | |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 1007 | String action = targetIntent.getAction(); |
| 1008 | if (Intent.ACTION_SEND.equals(action)) { |
| 1009 | Uri uri = targetIntent.getParcelableExtra(Intent.EXTRA_STREAM); |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 1010 | loadFileUriIntoView(uri, contentPreviewLayout); |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 1011 | } else { |
| 1012 | List<Uri> uris = targetIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); |
| 1013 | int uriCount = uris.size(); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 1014 | |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 1015 | if (uriCount == 0) { |
| 1016 | contentPreviewLayout.setVisibility(View.GONE); |
| 1017 | Log.i(TAG, |
| 1018 | "Appears to be no uris available in EXTRA_STREAM, removing " |
| 1019 | + "preview area"); |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 1020 | return contentPreviewLayout; |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 1021 | } else if (uriCount == 1) { |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 1022 | loadFileUriIntoView(uris.get(0), contentPreviewLayout); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 1023 | } else { |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 1024 | FileInfo fileInfo = extractFileInfo(uris.get(0), getContentResolver()); |
| 1025 | int remUriCount = uriCount - 1; |
Matt Pietal | acabc57 | 2019-02-14 11:02:05 -0500 | [diff] [blame] | 1026 | String fileName = getResources().getQuantityString(R.plurals.file_count, |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 1027 | remUriCount, fileInfo.name, remUriCount); |
Matt Pietal | acabc57 | 2019-02-14 11:02:05 -0500 | [diff] [blame] | 1028 | |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 1029 | TextView fileNameView = contentPreviewLayout.findViewById( |
| 1030 | R.id.content_preview_filename); |
Matt Pietal | acabc57 | 2019-02-14 11:02:05 -0500 | [diff] [blame] | 1031 | fileNameView.setText(fileName); |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 1032 | |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 1033 | View thumbnailView = contentPreviewLayout.findViewById( |
| 1034 | R.id.content_preview_file_thumbnail); |
| 1035 | thumbnailView.setVisibility(View.GONE); |
| 1036 | |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 1037 | ImageView fileIconView = contentPreviewLayout.findViewById( |
| 1038 | R.id.content_preview_file_icon); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 1039 | fileIconView.setVisibility(View.VISIBLE); |
Matt Pietal | acabc57 | 2019-02-14 11:02:05 -0500 | [diff] [blame] | 1040 | fileIconView.setImageResource(R.drawable.ic_file_copy); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 1041 | } |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 1042 | } |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 1043 | |
| 1044 | return contentPreviewLayout; |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 1045 | } |
| 1046 | |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 1047 | private void loadFileUriIntoView(final Uri uri, final View parent) { |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 1048 | FileInfo fileInfo = extractFileInfo(uri, getContentResolver()); |
| 1049 | |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 1050 | TextView fileNameView = parent.findViewById(R.id.content_preview_filename); |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 1051 | fileNameView.setText(fileInfo.name); |
| 1052 | |
| 1053 | if (fileInfo.hasThumbnail) { |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 1054 | mPreviewCoord = new ContentPreviewCoordinator(parent, false); |
| 1055 | mPreviewCoord.loadUriIntoView(R.id.content_preview_file_thumbnail, uri, 0); |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 1056 | } else { |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 1057 | View thumbnailView = parent.findViewById(R.id.content_preview_file_thumbnail); |
| 1058 | thumbnailView.setVisibility(View.GONE); |
| 1059 | |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 1060 | ImageView fileIconView = parent.findViewById(R.id.content_preview_file_icon); |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 1061 | fileIconView.setVisibility(View.VISIBLE); |
Matt Pietal | 832cdbf | 2019-04-05 13:20:31 -0400 | [diff] [blame] | 1062 | fileIconView.setImageResource(R.drawable.chooser_file_generic); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 1063 | } |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 1064 | } |
| 1065 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 1066 | @VisibleForTesting |
| 1067 | protected boolean isImageType(String mimeType) { |
| 1068 | return mimeType != null && mimeType.startsWith("image/"); |
| 1069 | } |
| 1070 | |
| 1071 | @ContentPreviewType |
| 1072 | private int findPreferredContentPreview(Uri uri, ContentResolver resolver) { |
| 1073 | if (uri == null) { |
| 1074 | return CONTENT_PREVIEW_TEXT; |
| 1075 | } |
| 1076 | |
| 1077 | String mimeType = resolver.getType(uri); |
| 1078 | return isImageType(mimeType) ? CONTENT_PREVIEW_IMAGE : CONTENT_PREVIEW_FILE; |
| 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * In {@link android.content.Intent#getType}, the app may specify a very general |
| 1083 | * mime-type that broadly covers all data being shared, such as {@literal *}/* |
| 1084 | * when sending an image and text. We therefore should inspect each item for the |
| 1085 | * the preferred type, in order of IMAGE, FILE, TEXT. |
| 1086 | */ |
| 1087 | @ContentPreviewType |
| 1088 | private int findPreferredContentPreview(Intent targetIntent, ContentResolver resolver) { |
| 1089 | String action = targetIntent.getAction(); |
| 1090 | if (Intent.ACTION_SEND.equals(action)) { |
| 1091 | Uri uri = targetIntent.getParcelableExtra(Intent.EXTRA_STREAM); |
| 1092 | return findPreferredContentPreview(uri, resolver); |
| 1093 | } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) { |
| 1094 | List<Uri> uris = targetIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); |
| 1095 | if (uris == null || uris.isEmpty()) { |
| 1096 | return CONTENT_PREVIEW_TEXT; |
| 1097 | } |
| 1098 | |
| 1099 | for (Uri uri : uris) { |
Matt Pietal | 832cdbf | 2019-04-05 13:20:31 -0400 | [diff] [blame] | 1100 | // Defaulting to file preview when there are mixed image/file types is |
| 1101 | // preferable, as it shows the user the correct number of items being shared |
| 1102 | if (findPreferredContentPreview(uri, resolver) == CONTENT_PREVIEW_FILE) { |
| 1103 | return CONTENT_PREVIEW_FILE; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Matt Pietal | 832cdbf | 2019-04-05 13:20:31 -0400 | [diff] [blame] | 1107 | return CONTENT_PREVIEW_IMAGE; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | return CONTENT_PREVIEW_TEXT; |
| 1111 | } |
| 1112 | |
Mike Digman | 849a9d1 | 2019-04-29 11:20:48 -0700 | [diff] [blame] | 1113 | private int getNumSheetExpansions() { |
| 1114 | return getPreferences(Context.MODE_PRIVATE).getInt(PREF_NUM_SHEET_EXPANSIONS, 0); |
| 1115 | } |
| 1116 | |
| 1117 | private void incrementNumSheetExpansions() { |
| 1118 | getPreferences(Context.MODE_PRIVATE).edit().putInt(PREF_NUM_SHEET_EXPANSIONS, |
| 1119 | getNumSheetExpansions() + 1).apply(); |
| 1120 | } |
| 1121 | |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 1122 | @Override |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1123 | protected void onDestroy() { |
| 1124 | super.onDestroy(); |
| 1125 | if (mRefinementResultReceiver != null) { |
| 1126 | mRefinementResultReceiver.destroy(); |
| 1127 | mRefinementResultReceiver = null; |
| 1128 | } |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1129 | unbindRemainingServices(); |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 1130 | mChooserHandler.removeAllMessages(); |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 1131 | |
| 1132 | if (mPreviewCoord != null) mPreviewCoord.cancelLoads(); |
| 1133 | |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 1134 | if (mAppPredictor != null) { |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1135 | mAppPredictor.unregisterPredictionUpdates(mAppPredictorCallback); |
| 1136 | mAppPredictor.destroy(); |
| 1137 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | @Override |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 1141 | public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) { |
| 1142 | Intent result = defIntent; |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 1143 | if (mReplacementExtras != null) { |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 1144 | final Bundle replExtras = mReplacementExtras.getBundle(aInfo.packageName); |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 1145 | if (replExtras != null) { |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 1146 | result = new Intent(defIntent); |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 1147 | result.putExtras(replExtras); |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 1148 | } |
| 1149 | } |
Nicolas Prevot | 741abfc | 2015-08-11 12:03:51 +0100 | [diff] [blame] | 1150 | if (aInfo.name.equals(IntentForwarderActivity.FORWARD_INTENT_TO_PARENT) |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 1151 | || aInfo.name.equals(IntentForwarderActivity.FORWARD_INTENT_TO_MANAGED_PROFILE)) { |
| 1152 | result = Intent.createChooser(result, |
| 1153 | getIntent().getCharSequenceExtra(Intent.EXTRA_TITLE)); |
Hakan Seyalioglu | 7317e8a | 2016-12-12 16:15:38 -0800 | [diff] [blame] | 1154 | |
| 1155 | // Don't auto-launch single intents if the intent is being forwarded. This is done |
| 1156 | // because automatically launching a resolving application as a response to the user |
| 1157 | // action of switching accounts is pretty unexpected. |
| 1158 | result.putExtra(Intent.EXTRA_AUTO_LAUNCH_SINGLE_CHOICE, false); |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 1159 | } |
| 1160 | return result; |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 1163 | @Override |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1164 | public void onActivityStarted(TargetInfo cti) { |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 1165 | if (mChosenComponentSender != null) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1166 | final ComponentName target = cti.getResolvedComponentName(); |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 1167 | if (target != null) { |
| 1168 | final Intent fillIn = new Intent().putExtra(Intent.EXTRA_CHOSEN_COMPONENT, target); |
| 1169 | try { |
| 1170 | mChosenComponentSender.sendIntent(this, Activity.RESULT_OK, fillIn, null, null); |
| 1171 | } catch (IntentSender.SendIntentException e) { |
| 1172 | Slog.e(TAG, "Unable to launch supplied IntentSender to report " |
| 1173 | + "the chosen component: " + e); |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1179 | @Override |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1180 | public void onPrepareAdapterView(AbsListView adapterView, ResolveListAdapter adapter) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1181 | final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null; |
| 1182 | mChooserListAdapter = (ChooserListAdapter) adapter; |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 1183 | if (mCallerChooserTargets != null && mCallerChooserTargets.length > 0) { |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 1184 | mChooserListAdapter.addServiceResults(null, Lists.newArrayList(mCallerChooserTargets), |
| 1185 | false); |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 1186 | } |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 1187 | mChooserRowAdapter = new ChooserRowAdapter(mChooserListAdapter); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1188 | if (listView != null) { |
| 1189 | listView.setItemsCanFocus(true); |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | @Override |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1194 | public int getLayoutResource() { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1195 | return R.layout.chooser_grid; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | @Override |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1199 | public boolean shouldGetActivityMetadata() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1200 | return true; |
| 1201 | } |
| 1202 | |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1203 | @Override |
Ben Lin | 145b0ca | 2016-10-14 14:23:40 -0700 | [diff] [blame] | 1204 | public boolean shouldAutoLaunchSingleChoice(TargetInfo target) { |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1205 | // Note that this is only safe because the Intent handled by the ChooserActivity is |
| 1206 | // guaranteed to contain no extras unknown to the local ClassLoader. That is why this |
| 1207 | // method can not be replaced in the ResolverActivity whole hog. |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1208 | if (!super.shouldAutoLaunchSingleChoice(target)) { |
| 1209 | return false; |
| 1210 | } |
| 1211 | |
| 1212 | return getIntent().getBooleanExtra(Intent.EXTRA_AUTO_LAUNCH_SINGLE_CHOICE, true); |
Ben Lin | 145b0ca | 2016-10-14 14:23:40 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | @Override |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1216 | public void showTargetDetails(ResolveInfo ri) { |
sanryhuang | 296ca9e | 2018-03-31 11:17:13 +0800 | [diff] [blame] | 1217 | if (ri == null) { |
| 1218 | return; |
| 1219 | } |
| 1220 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1221 | ComponentName name = ri.activityInfo.getComponentName(); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1222 | ResolverTargetActionsDialogFragment f = |
| 1223 | new ResolverTargetActionsDialogFragment(ri.loadLabel(getPackageManager()), |
Matt Pietal | df634cc | 2019-03-13 09:55:28 -0400 | [diff] [blame] | 1224 | name); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1225 | f.show(getFragmentManager(), TARGET_DETAILS_FRAGMENT_TAG); |
| 1226 | } |
| 1227 | |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 1228 | private void modifyTargetIntent(Intent in) { |
Matt Pietal | 95574b0 | 2019-03-13 08:12:25 -0400 | [diff] [blame] | 1229 | if (isSendAction(in)) { |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 1230 | in.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | |
Dianne Hackborn | 13420f2 | 2014-07-18 15:43:56 -0700 | [diff] [blame] | 1231 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 1232 | } |
| 1233 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1234 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1235 | @Override |
| 1236 | protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) { |
| 1237 | if (mRefinementIntentSender != null) { |
| 1238 | final Intent fillIn = new Intent(); |
| 1239 | final List<Intent> sourceIntents = target.getAllSourceIntents(); |
| 1240 | if (!sourceIntents.isEmpty()) { |
| 1241 | fillIn.putExtra(Intent.EXTRA_INTENT, sourceIntents.get(0)); |
| 1242 | if (sourceIntents.size() > 1) { |
| 1243 | final Intent[] alts = new Intent[sourceIntents.size() - 1]; |
| 1244 | for (int i = 1, N = sourceIntents.size(); i < N; i++) { |
| 1245 | alts[i - 1] = sourceIntents.get(i); |
| 1246 | } |
| 1247 | fillIn.putExtra(Intent.EXTRA_ALTERNATE_INTENTS, alts); |
| 1248 | } |
| 1249 | if (mRefinementResultReceiver != null) { |
| 1250 | mRefinementResultReceiver.destroy(); |
| 1251 | } |
| 1252 | mRefinementResultReceiver = new RefinementResultReceiver(this, target, null); |
| 1253 | fillIn.putExtra(Intent.EXTRA_RESULT_RECEIVER, |
| 1254 | mRefinementResultReceiver); |
| 1255 | try { |
| 1256 | mRefinementIntentSender.sendIntent(this, 0, fillIn, null, null); |
| 1257 | return false; |
| 1258 | } catch (SendIntentException e) { |
| 1259 | Log.e(TAG, "Refinement IntentSender failed to send", e); |
| 1260 | } |
| 1261 | } |
| 1262 | } |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 1263 | updateModelAndChooserCounts(target); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1264 | return super.onTargetSelected(target, alwaysCheck); |
| 1265 | } |
| 1266 | |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1267 | @Override |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1268 | public void startSelected(int which, boolean always, boolean filtered) { |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1269 | TargetInfo targetInfo = mChooserListAdapter.targetInfoForPosition(which, filtered); |
| 1270 | if (targetInfo != null && targetInfo instanceof NotSelectableTargetInfo) { |
| 1271 | return; |
| 1272 | } |
| 1273 | |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 1274 | final long selectionCost = System.currentTimeMillis() - mChooserShownTime; |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1275 | super.startSelected(which, always, filtered); |
| 1276 | |
| 1277 | if (mChooserListAdapter != null) { |
| 1278 | // Log the index of which type of target the user picked. |
| 1279 | // Lower values mean the ranking was better. |
| 1280 | int cat = 0; |
| 1281 | int value = which; |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1282 | int directTargetAlsoRanked = -1; |
Susi Kharraz-Post | 4bcca52 | 2019-04-23 15:07:10 -0400 | [diff] [blame] | 1283 | int numCallerProvided = 0; |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1284 | HashedStringCache.HashResult directTargetHashed = null; |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1285 | switch (mChooserListAdapter.getPositionTargetType(which)) { |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1286 | case ChooserListAdapter.TARGET_SERVICE: |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 1287 | cat = MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_SERVICE_TARGET; |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1288 | // Log the package name + target name to answer the question if most users |
| 1289 | // share to mostly the same person or to a bunch of different people. |
| 1290 | ChooserTarget target = |
| 1291 | mChooserListAdapter.mServiceTargets.get(value).getChooserTarget(); |
| 1292 | directTargetHashed = HashedStringCache.getInstance().hashString( |
| 1293 | this, |
| 1294 | TAG, |
| 1295 | target.getComponentName().getPackageName() |
| 1296 | + target.getTitle().toString(), |
| 1297 | mMaxHashSaltDays); |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1298 | directTargetAlsoRanked = getRankedPosition((SelectableTargetInfo) targetInfo); |
Matt Pietal | 9a6b23d | 2019-04-19 14:47:14 -0400 | [diff] [blame] | 1299 | |
| 1300 | if (mCallerChooserTargets != null) { |
Susi Kharraz-Post | 4bcca52 | 2019-04-23 15:07:10 -0400 | [diff] [blame] | 1301 | numCallerProvided = mCallerChooserTargets.length; |
Matt Pietal | 9a6b23d | 2019-04-19 14:47:14 -0400 | [diff] [blame] | 1302 | } |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1303 | break; |
Susi Kharraz-Post | 4bcca52 | 2019-04-23 15:07:10 -0400 | [diff] [blame] | 1304 | case ChooserListAdapter.TARGET_CALLER: |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1305 | case ChooserListAdapter.TARGET_STANDARD: |
Susi Kharraz-Post | 4bcca52 | 2019-04-23 15:07:10 -0400 | [diff] [blame] | 1306 | cat = MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_APP_TARGET; |
| 1307 | value -= mChooserListAdapter.getSelectableServiceTargetCount(); |
| 1308 | numCallerProvided = mChooserListAdapter.getCallerTargetCount(); |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1309 | break; |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 1310 | case ChooserListAdapter.TARGET_STANDARD_AZ: |
| 1311 | // A-Z targets are unranked standard targets; we use -1 to mark that they |
| 1312 | // are from the alphabetical pool. |
| 1313 | value = -1; |
| 1314 | cat = MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_STANDARD_TARGET; |
| 1315 | break; |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | if (cat != 0) { |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1319 | LogMaker targetLogMaker = new LogMaker(cat).setSubtype(value); |
| 1320 | if (directTargetHashed != null) { |
| 1321 | targetLogMaker.addTaggedData( |
| 1322 | MetricsEvent.FIELD_HASHED_TARGET_NAME, directTargetHashed.hashedString); |
| 1323 | targetLogMaker.addTaggedData( |
| 1324 | MetricsEvent.FIELD_HASHED_TARGET_SALT_GEN, |
| 1325 | directTargetHashed.saltGeneration); |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1326 | targetLogMaker.addTaggedData(MetricsEvent.FIELD_RANKED_POSITION, |
| 1327 | directTargetAlsoRanked); |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1328 | } |
Susi Kharraz-Post | 4bcca52 | 2019-04-23 15:07:10 -0400 | [diff] [blame] | 1329 | targetLogMaker.addTaggedData(MetricsEvent.FIELD_IS_CATEGORY_USED, |
| 1330 | numCallerProvided); |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1331 | getMetricsLogger().write(targetLogMaker); |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1332 | } |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 1333 | |
| 1334 | if (mIsSuccessfullySelected) { |
| 1335 | if (DEBUG) { |
| 1336 | Log.d(TAG, "User Selection Time Cost is " + selectionCost); |
| 1337 | Log.d(TAG, "position of selected app/service/caller is " + |
| 1338 | Integer.toString(value)); |
| 1339 | } |
| 1340 | MetricsLogger.histogram(null, "user_selection_cost_for_smart_sharing", |
| 1341 | (int) selectionCost); |
| 1342 | MetricsLogger.histogram(null, "app_position_for_smart_sharing", value); |
| 1343 | } |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1347 | private int getRankedPosition(SelectableTargetInfo targetInfo) { |
| 1348 | String targetPackageName = |
| 1349 | targetInfo.getChooserTarget().getComponentName().getPackageName(); |
| 1350 | int maxRankedResults = Math.min(mChooserListAdapter.mDisplayList.size(), |
| 1351 | MAX_LOG_RANK_POSITION); |
| 1352 | |
| 1353 | for (int i = 0; i < maxRankedResults; i++) { |
| 1354 | if (mChooserListAdapter.mDisplayList.get(i) |
| 1355 | .getResolveInfo().activityInfo.packageName.equals(targetPackageName)) { |
| 1356 | return i; |
| 1357 | } |
| 1358 | } |
| 1359 | return -1; |
| 1360 | } |
| 1361 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1362 | void queryTargetServices(ChooserListAdapter adapter) { |
Mehdi Alizadeh | 97fb3ed | 2019-04-25 14:52:02 -0700 | [diff] [blame] | 1363 | mQueriedTargetServicesTimeMs = System.currentTimeMillis(); |
| 1364 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1365 | final PackageManager pm = getPackageManager(); |
Mehdi Alizadeh | 85fd3d5 | 2019-01-23 12:49:53 -0800 | [diff] [blame] | 1366 | ShortcutManager sm = (ShortcutManager) getSystemService(ShortcutManager.class); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1367 | int targetsToQuery = 0; |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 1368 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1369 | for (int i = 0, N = adapter.getDisplayResolveInfoCount(); i < N; i++) { |
| 1370 | final DisplayResolveInfo dri = adapter.getDisplayResolveInfo(i); |
Adam Powell | 3a09c52 | 2015-10-21 13:21:28 -0700 | [diff] [blame] | 1371 | if (adapter.getScore(dri) == 0) { |
| 1372 | // A score of 0 means the app hasn't been used in some time; |
| 1373 | // don't query it as it's not likely to be relevant. |
| 1374 | continue; |
| 1375 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1376 | final ActivityInfo ai = dri.getResolveInfo().activityInfo; |
Mehdi Alizadeh | 85fd3d5 | 2019-01-23 12:49:53 -0800 | [diff] [blame] | 1377 | if (USE_SHORTCUT_MANAGER_FOR_DIRECT_TARGETS |
| 1378 | && sm.hasShareTargets(ai.packageName)) { |
| 1379 | // Share targets will be queried from ShortcutManager |
| 1380 | continue; |
| 1381 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1382 | final Bundle md = ai.metaData; |
| 1383 | final String serviceName = md != null ? convertServiceName(ai.packageName, |
| 1384 | md.getString(ChooserTargetService.META_DATA_NAME)) : null; |
| 1385 | if (serviceName != null) { |
| 1386 | final ComponentName serviceComponent = new ComponentName( |
| 1387 | ai.packageName, serviceName); |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 1388 | |
| 1389 | if (mServicesRequested.contains(serviceComponent)) { |
| 1390 | continue; |
| 1391 | } |
| 1392 | mServicesRequested.add(serviceComponent); |
| 1393 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1394 | final Intent serviceIntent = new Intent(ChooserTargetService.SERVICE_INTERFACE) |
| 1395 | .setComponent(serviceComponent); |
| 1396 | |
| 1397 | if (DEBUG) { |
| 1398 | Log.d(TAG, "queryTargets found target with service " + serviceComponent); |
| 1399 | } |
| 1400 | |
| 1401 | try { |
| 1402 | final String perm = pm.getServiceInfo(serviceComponent, 0).permission; |
| 1403 | if (!ChooserTargetService.BIND_PERMISSION.equals(perm)) { |
| 1404 | Log.w(TAG, "ChooserTargetService " + serviceComponent + " does not require" |
| 1405 | + " permission " + ChooserTargetService.BIND_PERMISSION |
| 1406 | + " - this service will not be queried for ChooserTargets." |
| 1407 | + " add android:permission=\"" |
| 1408 | + ChooserTargetService.BIND_PERMISSION + "\"" |
| 1409 | + " to the <service> tag for " + serviceComponent |
| 1410 | + " in the manifest."); |
| 1411 | continue; |
| 1412 | } |
| 1413 | } catch (NameNotFoundException e) { |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 1414 | Log.e(TAG, "Could not look up service " + serviceComponent |
| 1415 | + "; component name not found"); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1416 | continue; |
| 1417 | } |
| 1418 | |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1419 | final ChooserTargetServiceConnection conn = |
| 1420 | new ChooserTargetServiceConnection(this, dri); |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 1421 | |
| 1422 | // Explicitly specify Process.myUserHandle instead of calling bindService |
| 1423 | // to avoid the warning from calling from the system process without an explicit |
| 1424 | // user handle |
| 1425 | if (bindServiceAsUser(serviceIntent, conn, BIND_AUTO_CREATE | BIND_NOT_FOREGROUND, |
| 1426 | Process.myUserHandle())) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1427 | if (DEBUG) { |
| 1428 | Log.d(TAG, "Binding service connection for target " + dri |
| 1429 | + " intent " + serviceIntent); |
| 1430 | } |
| 1431 | mServiceConnections.add(conn); |
| 1432 | targetsToQuery++; |
| 1433 | } |
| 1434 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1435 | if (targetsToQuery >= QUERY_TARGET_SERVICE_LIMIT) { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1436 | if (DEBUG) { |
| 1437 | Log.d(TAG, "queryTargets hit query target limit " |
| 1438 | + QUERY_TARGET_SERVICE_LIMIT); |
| 1439 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1440 | break; |
| 1441 | } |
| 1442 | } |
| 1443 | |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 1444 | mChooserHandler.restartServiceRequestTimer(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1445 | } |
| 1446 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1447 | private IntentFilter getTargetIntentFilter() { |
| 1448 | try { |
| 1449 | final Intent intent = getTargetIntent(); |
| 1450 | String dataString = intent.getDataString(); |
| 1451 | if (TextUtils.isEmpty(dataString)) { |
| 1452 | dataString = intent.getType(); |
| 1453 | } |
| 1454 | return new IntentFilter(intent.getAction(), dataString); |
| 1455 | } catch (Exception e) { |
| 1456 | Log.e(TAG, "failed to get target intent filter " + e); |
| 1457 | return null; |
| 1458 | } |
| 1459 | } |
| 1460 | |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1461 | private List<DisplayResolveInfo> getDisplayResolveInfos(ChooserListAdapter adapter) { |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1462 | // Need to keep the original DisplayResolveInfos to be able to reconstruct ServiceResultInfo |
| 1463 | // and use the old code path. This Ugliness should go away when Sharesheet is refactored. |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1464 | List<DisplayResolveInfo> driList = new ArrayList<>(); |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1465 | int targetsToQuery = 0; |
| 1466 | for (int i = 0, n = adapter.getDisplayResolveInfoCount(); i < n; i++) { |
| 1467 | final DisplayResolveInfo dri = adapter.getDisplayResolveInfo(i); |
| 1468 | if (adapter.getScore(dri) == 0) { |
| 1469 | // A score of 0 means the app hasn't been used in some time; |
| 1470 | // don't query it as it's not likely to be relevant. |
| 1471 | continue; |
| 1472 | } |
| 1473 | driList.add(dri); |
| 1474 | targetsToQuery++; |
| 1475 | // TODO(b/121287224): Do we need this here? (similar to queryTargetServices) |
| 1476 | if (targetsToQuery >= SHARE_TARGET_QUERY_PACKAGE_LIMIT) { |
| 1477 | if (DEBUG) { |
| 1478 | Log.d(TAG, "queryTargets hit query target limit " |
| 1479 | + SHARE_TARGET_QUERY_PACKAGE_LIMIT); |
| 1480 | } |
| 1481 | break; |
| 1482 | } |
| 1483 | } |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1484 | return driList; |
| 1485 | } |
| 1486 | |
George Hodulik | 3f399f2 | 2019-04-26 16:17:54 -0700 | [diff] [blame] | 1487 | private void queryDirectShareTargets( |
| 1488 | ChooserListAdapter adapter, boolean skipAppPredictionService) { |
Mehdi Alizadeh | 97fb3ed | 2019-04-25 14:52:02 -0700 | [diff] [blame] | 1489 | mQueriedSharingShortcutsTimeMs = System.currentTimeMillis(); |
George Hodulik | 3f399f2 | 2019-04-26 16:17:54 -0700 | [diff] [blame] | 1490 | if (!skipAppPredictionService) { |
| 1491 | AppPredictor appPredictor = getAppPredictorForDirectShareIfEnabled(); |
| 1492 | if (appPredictor != null) { |
| 1493 | appPredictor.requestPredictionUpdate(); |
| 1494 | return; |
| 1495 | } |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1496 | } |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 1497 | // Default to just querying ShortcutManager if AppPredictor not present. |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1498 | final IntentFilter filter = getTargetIntentFilter(); |
| 1499 | if (filter == null) { |
| 1500 | return; |
| 1501 | } |
| 1502 | final List<DisplayResolveInfo> driList = getDisplayResolveInfos(adapter); |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1503 | |
| 1504 | AsyncTask.execute(() -> { |
| 1505 | ShortcutManager sm = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE); |
| 1506 | List<ShortcutManager.ShareShortcutInfo> resultList = sm.getShareTargets(filter); |
George Hodulik | aa5238c | 2019-04-18 14:17:51 -0700 | [diff] [blame] | 1507 | sendShareShortcutInfoList(resultList, driList, null); |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1508 | }); |
| 1509 | } |
| 1510 | |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1511 | private void sendShareShortcutInfoList( |
| 1512 | List<ShortcutManager.ShareShortcutInfo> resultList, |
George Hodulik | aa5238c | 2019-04-18 14:17:51 -0700 | [diff] [blame] | 1513 | List<DisplayResolveInfo> driList, |
| 1514 | @Nullable List<AppTarget> appTargets) { |
Mehdi Alizadeh | 3e3216f | 2019-05-27 17:56:51 -0700 | [diff] [blame] | 1515 | if (appTargets != null && appTargets.size() != resultList.size()) { |
| 1516 | throw new RuntimeException("resultList and appTargets must have the same size." |
| 1517 | + " resultList.size()=" + resultList.size() |
| 1518 | + " appTargets.size()=" + appTargets.size()); |
| 1519 | } |
| 1520 | |
| 1521 | for (int i = resultList.size() - 1; i >= 0; i--) { |
| 1522 | final String packageName = resultList.get(i).getTargetComponent().getPackageName(); |
| 1523 | if (!isPackageEnabled(packageName)) { |
| 1524 | resultList.remove(i); |
| 1525 | if (appTargets != null) { |
| 1526 | appTargets.remove(i); |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1531 | // Match ShareShortcutInfos with DisplayResolveInfos to be able to use the old code path |
| 1532 | // for direct share targets. After ShareSheet is refactored we should use the |
| 1533 | // ShareShortcutInfos directly. |
| 1534 | boolean resultMessageSent = false; |
| 1535 | for (int i = 0; i < driList.size(); i++) { |
| 1536 | List<ChooserTarget> chooserTargets = new ArrayList<>(); |
| 1537 | for (int j = 0; j < resultList.size(); j++) { |
| 1538 | if (driList.get(i).getResolvedComponentName().equals( |
| 1539 | resultList.get(j).getTargetComponent())) { |
George Hodulik | aa5238c | 2019-04-18 14:17:51 -0700 | [diff] [blame] | 1540 | ShortcutManager.ShareShortcutInfo shareShortcutInfo = resultList.get(j); |
Matt Pietal | bdbeea2 | 2019-07-01 13:06:21 -0400 | [diff] [blame] | 1541 | // Incoming results are ordered but without a score. Create a score |
| 1542 | // based on the index in order to be sorted appropriately when joined |
| 1543 | // with legacy direct share api results. |
| 1544 | float score = Math.max(1.0f - (0.05f * j), 0.0f); |
| 1545 | ChooserTarget chooserTarget = convertToChooserTarget(shareShortcutInfo, score); |
George Hodulik | aa5238c | 2019-04-18 14:17:51 -0700 | [diff] [blame] | 1546 | chooserTargets.add(chooserTarget); |
| 1547 | if (mDirectShareAppTargetCache != null && appTargets != null) { |
George Hodulik | aa5238c | 2019-04-18 14:17:51 -0700 | [diff] [blame] | 1548 | mDirectShareAppTargetCache.put(chooserTarget, appTargets.get(j)); |
| 1549 | } |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1550 | } |
| 1551 | } |
| 1552 | if (chooserTargets.isEmpty()) { |
| 1553 | continue; |
| 1554 | } |
| 1555 | final Message msg = Message.obtain(); |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 1556 | msg.what = ChooserHandler.SHORTCUT_MANAGER_SHARE_TARGET_RESULT; |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1557 | msg.obj = new ServiceResultInfo(driList.get(i), chooserTargets, null); |
| 1558 | mChooserHandler.sendMessage(msg); |
| 1559 | resultMessageSent = true; |
| 1560 | } |
| 1561 | |
| 1562 | if (resultMessageSent) { |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 1563 | sendShortcutManagerShareTargetResultCompleted(); |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1564 | } |
| 1565 | } |
| 1566 | |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 1567 | private void sendShortcutManagerShareTargetResultCompleted() { |
| 1568 | final Message msg = Message.obtain(); |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 1569 | msg.what = ChooserHandler.SHORTCUT_MANAGER_SHARE_TARGET_RESULT_COMPLETED; |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 1570 | mChooserHandler.sendMessage(msg); |
| 1571 | } |
| 1572 | |
Mehdi Alizadeh | 3e3216f | 2019-05-27 17:56:51 -0700 | [diff] [blame] | 1573 | private boolean isPackageEnabled(String packageName) { |
| 1574 | if (TextUtils.isEmpty(packageName)) { |
| 1575 | return false; |
| 1576 | } |
| 1577 | ApplicationInfo appInfo; |
| 1578 | try { |
| 1579 | appInfo = getPackageManager().getApplicationInfo(packageName, 0); |
| 1580 | } catch (NameNotFoundException e) { |
| 1581 | return false; |
| 1582 | } |
| 1583 | |
| 1584 | if (appInfo != null && appInfo.enabled |
| 1585 | && (appInfo.flags & ApplicationInfo.FLAG_SUSPENDED) == 0) { |
| 1586 | return true; |
| 1587 | } |
| 1588 | return false; |
| 1589 | } |
| 1590 | |
Matt Pietal | bdbeea2 | 2019-07-01 13:06:21 -0400 | [diff] [blame] | 1591 | private ChooserTarget convertToChooserTarget(ShortcutManager.ShareShortcutInfo shareShortcut, |
| 1592 | float score) { |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1593 | ShortcutInfo shortcutInfo = shareShortcut.getShortcutInfo(); |
| 1594 | Bundle extras = new Bundle(); |
| 1595 | extras.putString(Intent.EXTRA_SHORTCUT_ID, shortcutInfo.getId()); |
| 1596 | return new ChooserTarget( |
| 1597 | // The name of this target. |
| 1598 | shortcutInfo.getShortLabel(), |
| 1599 | // Don't load the icon until it is selected to be shown |
| 1600 | null, |
| 1601 | // The ranking score for this target (0.0-1.0); the system will omit items with low |
| 1602 | // scores when there are too many Direct Share items. |
Matt Pietal | bdbeea2 | 2019-07-01 13:06:21 -0400 | [diff] [blame] | 1603 | score, |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1604 | // The name of the component to be launched if this target is chosen. |
| 1605 | shareShortcut.getTargetComponent().clone(), |
| 1606 | // The extra values here will be merged into the Intent when this target is chosen. |
| 1607 | extras); |
| 1608 | } |
| 1609 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1610 | private String convertServiceName(String packageName, String serviceName) { |
| 1611 | if (TextUtils.isEmpty(serviceName)) { |
| 1612 | return null; |
| 1613 | } |
| 1614 | |
| 1615 | final String fullName; |
| 1616 | if (serviceName.startsWith(".")) { |
| 1617 | // Relative to the app package. Prepend the app package name. |
| 1618 | fullName = packageName + serviceName; |
| 1619 | } else if (serviceName.indexOf('.') >= 0) { |
| 1620 | // Fully qualified package name. |
| 1621 | fullName = serviceName; |
| 1622 | } else { |
| 1623 | fullName = null; |
| 1624 | } |
| 1625 | return fullName; |
| 1626 | } |
| 1627 | |
| 1628 | void unbindRemainingServices() { |
| 1629 | if (DEBUG) { |
| 1630 | Log.d(TAG, "unbindRemainingServices, " + mServiceConnections.size() + " left"); |
| 1631 | } |
| 1632 | for (int i = 0, N = mServiceConnections.size(); i < N; i++) { |
| 1633 | final ChooserTargetServiceConnection conn = mServiceConnections.get(i); |
| 1634 | if (DEBUG) Log.d(TAG, "unbinding " + conn); |
| 1635 | unbindService(conn); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1636 | conn.destroy(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1637 | } |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 1638 | mServicesRequested.clear(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1639 | mServiceConnections.clear(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1642 | public void onSetupVoiceInteraction() { |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 1643 | // Do nothing. We'll send the voice stuff ourselves. |
| 1644 | } |
| 1645 | |
Mehdi Alizadeh | 97fb3ed | 2019-04-25 14:52:02 -0700 | [diff] [blame] | 1646 | private void logDirectShareTargetReceived(int logCategory) { |
| 1647 | final long queryTime = |
| 1648 | logCategory == MetricsEvent.ACTION_DIRECT_SHARE_TARGETS_LOADED_SHORTCUT_MANAGER |
| 1649 | ? mQueriedSharingShortcutsTimeMs : mQueriedTargetServicesTimeMs; |
| 1650 | final int apiLatency = (int) (System.currentTimeMillis() - queryTime); |
| 1651 | getMetricsLogger().write(new LogMaker(logCategory).setSubtype(apiLatency)); |
| 1652 | } |
| 1653 | |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 1654 | void updateModelAndChooserCounts(TargetInfo info) { |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1655 | if (info != null) { |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 1656 | sendClickToAppPredictor(info); |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1657 | final ResolveInfo ri = info.getResolveInfo(); |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 1658 | Intent targetIntent = getTargetIntent(); |
| 1659 | if (ri != null && ri.activityInfo != null && targetIntent != null) { |
Kang Li | 0cef910d | 2017-01-05 09:14:36 -0800 | [diff] [blame] | 1660 | if (mAdapter != null) { |
| 1661 | mAdapter.updateModel(info.getResolvedComponentName()); |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 1662 | mAdapter.updateChooserCounts(ri.activityInfo.packageName, getUserId(), |
| 1663 | targetIntent.getAction()); |
Kang Li | 0cef910d | 2017-01-05 09:14:36 -0800 | [diff] [blame] | 1664 | } |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1665 | if (DEBUG) { |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 1666 | Log.d(TAG, "ResolveInfo Package is " + ri.activityInfo.packageName); |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 1667 | Log.d(TAG, "Action to be updated is " + targetIntent.getAction()); |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1668 | } |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 1669 | } else if (DEBUG) { |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1670 | Log.d(TAG, "Can not log Chooser Counts of null ResovleInfo"); |
| 1671 | } |
| 1672 | } |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 1673 | mIsSuccessfullySelected = true; |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 1676 | private void sendClickToAppPredictor(TargetInfo targetInfo) { |
George Hodulik | aa5238c | 2019-04-18 14:17:51 -0700 | [diff] [blame] | 1677 | AppPredictor directShareAppPredictor = getAppPredictorForDirectShareIfEnabled(); |
| 1678 | if (directShareAppPredictor == null) { |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 1679 | return; |
| 1680 | } |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 1681 | if (!(targetInfo instanceof ChooserTargetInfo)) { |
| 1682 | return; |
| 1683 | } |
| 1684 | ChooserTarget chooserTarget = ((ChooserTargetInfo) targetInfo).getChooserTarget(); |
George Hodulik | aa5238c | 2019-04-18 14:17:51 -0700 | [diff] [blame] | 1685 | AppTarget appTarget = null; |
| 1686 | if (mDirectShareAppTargetCache != null) { |
| 1687 | appTarget = mDirectShareAppTargetCache.get(chooserTarget); |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 1688 | } |
George Hodulik | aa5238c | 2019-04-18 14:17:51 -0700 | [diff] [blame] | 1689 | // This is a direct share click that was provided by the APS |
| 1690 | if (appTarget != null) { |
| 1691 | directShareAppPredictor.notifyAppTargetEvent( |
| 1692 | new AppTargetEvent.Builder(appTarget, AppTargetEvent.ACTION_LAUNCH) |
| 1693 | .setLaunchLocation(LAUNCH_LOCATON_DIRECT_SHARE) |
| 1694 | .build()); |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 1695 | } |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 1696 | } |
| 1697 | |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 1698 | @Nullable |
| 1699 | private AppPredictor getAppPredictor() { |
| 1700 | if (mAppPredictor == null |
| 1701 | && getPackageManager().getAppPredictionServicePackageName() != null) { |
| 1702 | final IntentFilter filter = getTargetIntentFilter(); |
| 1703 | Bundle extras = new Bundle(); |
| 1704 | extras.putParcelable(APP_PREDICTION_INTENT_FILTER_KEY, filter); |
| 1705 | AppPredictionContext appPredictionContext = new AppPredictionContext.Builder(this) |
| 1706 | .setUiSurface(APP_PREDICTION_SHARE_UI_SURFACE) |
| 1707 | .setPredictedTargetCount(APP_PREDICTION_SHARE_TARGET_QUERY_PACKAGE_LIMIT) |
| 1708 | .setExtras(extras) |
| 1709 | .build(); |
| 1710 | AppPredictionManager appPredictionManager |
| 1711 | = getSystemService(AppPredictionManager.class); |
| 1712 | mAppPredictor = appPredictionManager.createAppPredictionSession(appPredictionContext); |
| 1713 | } |
| 1714 | return mAppPredictor; |
| 1715 | } |
| 1716 | |
| 1717 | /** |
| 1718 | * This will return an app predictor if it is enabled for direct share sorting |
| 1719 | * and if one exists. Otherwise, it returns null. |
| 1720 | */ |
| 1721 | @Nullable |
| 1722 | private AppPredictor getAppPredictorForDirectShareIfEnabled() { |
Matt Pietal | 030bd84 | 2019-05-29 07:14:14 -0400 | [diff] [blame] | 1723 | return USE_PREDICTION_MANAGER_FOR_DIRECT_TARGETS && !ActivityManager.isLowRamDeviceStatic() |
| 1724 | ? getAppPredictor() : null; |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
George Hodulik | c681ce4 | 2019-04-12 17:10:31 -0700 | [diff] [blame] | 1727 | /** |
| 1728 | * This will return an app predictor if it is enabled for share activity sorting |
| 1729 | * and if one exists. Otherwise, it returns null. |
| 1730 | */ |
| 1731 | @Nullable |
| 1732 | private AppPredictor getAppPredictorForShareActivitesIfEnabled() { |
| 1733 | return USE_PREDICTION_MANAGER_FOR_SHARE_ACTIVITIES ? getAppPredictor() : null; |
| 1734 | } |
| 1735 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1736 | void onRefinementResult(TargetInfo selectedTarget, Intent matchingIntent) { |
| 1737 | if (mRefinementResultReceiver != null) { |
| 1738 | mRefinementResultReceiver.destroy(); |
| 1739 | mRefinementResultReceiver = null; |
| 1740 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1741 | if (selectedTarget == null) { |
| 1742 | Log.e(TAG, "Refinement result intent did not match any known targets; canceling"); |
| 1743 | } else if (!checkTargetSourceIntent(selectedTarget, matchingIntent)) { |
| 1744 | Log.e(TAG, "onRefinementResult: Selected target " + selectedTarget |
| 1745 | + " cannot match refined source intent " + matchingIntent); |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1746 | } else { |
| 1747 | TargetInfo clonedTarget = selectedTarget.cloneFilledIn(matchingIntent, 0); |
| 1748 | if (super.onTargetSelected(clonedTarget, false)) { |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 1749 | updateModelAndChooserCounts(clonedTarget); |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1750 | finish(); |
| 1751 | return; |
| 1752 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1753 | } |
| 1754 | onRefinementCanceled(); |
| 1755 | } |
| 1756 | |
| 1757 | void onRefinementCanceled() { |
| 1758 | if (mRefinementResultReceiver != null) { |
| 1759 | mRefinementResultReceiver.destroy(); |
| 1760 | mRefinementResultReceiver = null; |
| 1761 | } |
| 1762 | finish(); |
| 1763 | } |
| 1764 | |
| 1765 | boolean checkTargetSourceIntent(TargetInfo target, Intent matchingIntent) { |
| 1766 | final List<Intent> targetIntents = target.getAllSourceIntents(); |
| 1767 | for (int i = 0, N = targetIntents.size(); i < N; i++) { |
| 1768 | final Intent targetIntent = targetIntents.get(i); |
| 1769 | if (targetIntent.filterEquals(matchingIntent)) { |
| 1770 | return true; |
| 1771 | } |
| 1772 | } |
| 1773 | return false; |
| 1774 | } |
| 1775 | |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 1776 | void filterServiceTargets(String packageName, List<ChooserTarget> targets) { |
| 1777 | if (targets == null) { |
| 1778 | return; |
| 1779 | } |
| 1780 | |
| 1781 | final PackageManager pm = getPackageManager(); |
| 1782 | for (int i = targets.size() - 1; i >= 0; i--) { |
| 1783 | final ChooserTarget target = targets.get(i); |
| 1784 | final ComponentName targetName = target.getComponentName(); |
| 1785 | if (packageName != null && packageName.equals(targetName.getPackageName())) { |
| 1786 | // Anything from the original target's package is fine. |
| 1787 | continue; |
| 1788 | } |
| 1789 | |
| 1790 | boolean remove; |
| 1791 | try { |
| 1792 | final ActivityInfo ai = pm.getActivityInfo(targetName, 0); |
| 1793 | remove = !ai.exported || ai.permission != null; |
| 1794 | } catch (NameNotFoundException e) { |
| 1795 | Log.e(TAG, "Target " + target + " returned by " + packageName |
| 1796 | + " component not found"); |
| 1797 | remove = true; |
| 1798 | } |
| 1799 | |
| 1800 | if (remove) { |
| 1801 | targets.remove(i); |
| 1802 | } |
| 1803 | } |
| 1804 | } |
| 1805 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 1806 | private void updateAlphabeticalList() { |
Alison Cichowlas | 1331461 | 2019-04-11 15:20:39 -0400 | [diff] [blame] | 1807 | mSortedList.clear(); |
| 1808 | mSortedList.addAll(getDisplayList()); |
| 1809 | Collections.sort(mSortedList, new AzInfoComparator(ChooserActivity.this)); |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | /** |
| 1813 | * Sort intents alphabetically based on display label. |
| 1814 | */ |
| 1815 | class AzInfoComparator implements Comparator<ResolverActivity.DisplayResolveInfo> { |
| 1816 | Collator mCollator; |
| 1817 | AzInfoComparator(Context context) { |
| 1818 | mCollator = Collator.getInstance(context.getResources().getConfiguration().locale); |
| 1819 | } |
| 1820 | |
| 1821 | @Override |
| 1822 | public int compare(ResolverActivity.DisplayResolveInfo lhsp, |
| 1823 | ResolverActivity.DisplayResolveInfo rhsp) { |
| 1824 | return mCollator.compare(lhsp.getDisplayLabel(), rhsp.getDisplayLabel()); |
| 1825 | } |
| 1826 | } |
| 1827 | |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 1828 | protected MetricsLogger getMetricsLogger() { |
| 1829 | if (mMetricsLogger == null) { |
| 1830 | mMetricsLogger = new MetricsLogger(); |
| 1831 | } |
| 1832 | return mMetricsLogger; |
| 1833 | } |
| 1834 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1835 | public class ChooserListController extends ResolverListController { |
| 1836 | public ChooserListController(Context context, |
| 1837 | PackageManager pm, |
| 1838 | Intent targetIntent, |
| 1839 | String referrerPackageName, |
George Hodulik | c681ce4 | 2019-04-12 17:10:31 -0700 | [diff] [blame] | 1840 | int launchedFromUid, |
| 1841 | AbstractResolverComparator resolverComparator) { |
| 1842 | super(context, pm, targetIntent, referrerPackageName, launchedFromUid, |
| 1843 | resolverComparator); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | @Override |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1847 | boolean isComponentFiltered(ComponentName name) { |
| 1848 | if (mFilteredComponentNames == null) { |
| 1849 | return false; |
| 1850 | } |
| 1851 | for (ComponentName filteredComponentName : mFilteredComponentNames) { |
| 1852 | if (name.equals(filteredComponentName)) { |
| 1853 | return true; |
| 1854 | } |
| 1855 | } |
| 1856 | return false; |
| 1857 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1858 | } |
| 1859 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1860 | @Override |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1861 | public ResolveListAdapter createAdapter(Context context, List<Intent> payloadIntents, |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1862 | Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid, |
| 1863 | boolean filterLastUsed) { |
| 1864 | final ChooserListAdapter adapter = new ChooserListAdapter(context, payloadIntents, |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1865 | initialIntents, rList, launchedFromUid, filterLastUsed, createListController()); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1866 | return adapter; |
| 1867 | } |
| 1868 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1869 | @VisibleForTesting |
| 1870 | protected ResolverListController createListController() { |
George Hodulik | c681ce4 | 2019-04-12 17:10:31 -0700 | [diff] [blame] | 1871 | AppPredictor appPredictor = getAppPredictorForShareActivitesIfEnabled(); |
| 1872 | AbstractResolverComparator resolverComparator; |
| 1873 | if (appPredictor != null) { |
| 1874 | resolverComparator = new AppPredictionServiceResolverComparator(this, getTargetIntent(), |
George Hodulik | 3f399f2 | 2019-04-26 16:17:54 -0700 | [diff] [blame] | 1875 | getReferrerPackageName(), appPredictor, getUser()); |
George Hodulik | c681ce4 | 2019-04-12 17:10:31 -0700 | [diff] [blame] | 1876 | } else { |
| 1877 | resolverComparator = |
| 1878 | new ResolverRankerServiceResolverComparator(this, getTargetIntent(), |
| 1879 | getReferrerPackageName(), null); |
| 1880 | } |
| 1881 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1882 | return new ChooserListController( |
| 1883 | this, |
| 1884 | mPm, |
| 1885 | getTargetIntent(), |
| 1886 | getReferrerPackageName(), |
George Hodulik | c681ce4 | 2019-04-12 17:10:31 -0700 | [diff] [blame] | 1887 | mLaunchedFromUid, |
| 1888 | resolverComparator); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1889 | } |
| 1890 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1891 | @VisibleForTesting |
| 1892 | protected Bitmap loadThumbnail(Uri uri, Size size) { |
| 1893 | if (uri == null || size == null) { |
| 1894 | return null; |
| 1895 | } |
| 1896 | |
| 1897 | try { |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 1898 | return ImageUtils.loadThumbnail(getContentResolver(), uri, size); |
| 1899 | } catch (IOException | NullPointerException | SecurityException ex) { |
Matt Pietal | 62532e5 | 2019-05-07 09:51:37 -0400 | [diff] [blame] | 1900 | logContentPreviewWarning(uri); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1901 | } |
| 1902 | return null; |
| 1903 | } |
| 1904 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1905 | interface ChooserTargetInfo extends TargetInfo { |
| 1906 | float getModifiedScore(); |
| 1907 | |
| 1908 | ChooserTarget getChooserTarget(); |
Matt Pietal | 9d50143 | 2019-04-12 10:05:29 -0400 | [diff] [blame] | 1909 | |
| 1910 | /** |
| 1911 | * Do not label as 'equals', since this doesn't quite work |
| 1912 | * as intended with java 8. |
| 1913 | */ |
| 1914 | default boolean isSimilar(ChooserTargetInfo other) { |
| 1915 | if (other == null) return false; |
| 1916 | |
| 1917 | ChooserTarget ct1 = getChooserTarget(); |
| 1918 | ChooserTarget ct2 = other.getChooserTarget(); |
| 1919 | |
| 1920 | // If either is null, there is not enough info to make an informed decision |
| 1921 | // about equality, so just exit |
| 1922 | if (ct1 == null || ct2 == null) return false; |
| 1923 | |
| 1924 | if (ct1.getComponentName().equals(ct2.getComponentName()) |
| 1925 | && TextUtils.equals(getDisplayLabel(), other.getDisplayLabel()) |
| 1926 | && TextUtils.equals(getExtendedInfo(), other.getExtendedInfo())) { |
| 1927 | return true; |
| 1928 | } |
| 1929 | |
| 1930 | return false; |
| 1931 | } |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | /** |
| 1935 | * Distinguish between targets that selectable by the user, vs those that are |
| 1936 | * placeholders for the system while information is loading in an async manner. |
| 1937 | */ |
| 1938 | abstract class NotSelectableTargetInfo implements ChooserTargetInfo { |
| 1939 | |
| 1940 | public Intent getResolvedIntent() { |
| 1941 | return null; |
| 1942 | } |
| 1943 | |
| 1944 | public ComponentName getResolvedComponentName() { |
| 1945 | return null; |
| 1946 | } |
| 1947 | |
| 1948 | public boolean start(Activity activity, Bundle options) { |
| 1949 | return false; |
| 1950 | } |
| 1951 | |
| 1952 | public boolean startAsCaller(ResolverActivity activity, Bundle options, int userId) { |
| 1953 | return false; |
| 1954 | } |
| 1955 | |
| 1956 | public boolean startAsUser(Activity activity, Bundle options, UserHandle user) { |
| 1957 | return false; |
| 1958 | } |
| 1959 | |
| 1960 | public ResolveInfo getResolveInfo() { |
| 1961 | return null; |
| 1962 | } |
| 1963 | |
| 1964 | public CharSequence getDisplayLabel() { |
| 1965 | return null; |
| 1966 | } |
| 1967 | |
| 1968 | public CharSequence getExtendedInfo() { |
| 1969 | return null; |
| 1970 | } |
| 1971 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1972 | public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) { |
| 1973 | return null; |
| 1974 | } |
| 1975 | |
| 1976 | public List<Intent> getAllSourceIntents() { |
| 1977 | return null; |
| 1978 | } |
| 1979 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1980 | public float getModifiedScore() { |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 1981 | return -0.1f; |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | public ChooserTarget getChooserTarget() { |
| 1985 | return null; |
| 1986 | } |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 1987 | |
| 1988 | public boolean isSuspended() { |
| 1989 | return false; |
| 1990 | } |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | final class PlaceHolderTargetInfo extends NotSelectableTargetInfo { |
| 1994 | public Drawable getDisplayIcon() { |
Mike Digman | ac1d88c | 2019-04-18 15:15:55 -0700 | [diff] [blame] | 1995 | AnimatedVectorDrawable avd = (AnimatedVectorDrawable) |
| 1996 | getDrawable(R.drawable.chooser_direct_share_icon_placeholder); |
| 1997 | avd.start(); // Start animation after generation |
| 1998 | return avd; |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | |
| 2003 | final class EmptyTargetInfo extends NotSelectableTargetInfo { |
| 2004 | public Drawable getDisplayIcon() { |
| 2005 | return null; |
| 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | final class SelectableTargetInfo implements ChooserTargetInfo { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2010 | private final DisplayResolveInfo mSourceInfo; |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 2011 | private final ResolveInfo mBackupResolveInfo; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2012 | private final ChooserTarget mChooserTarget; |
Matt Pietal | 9d50143 | 2019-04-12 10:05:29 -0400 | [diff] [blame] | 2013 | private final String mDisplayLabel; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2014 | private Drawable mBadgeIcon = null; |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 2015 | private CharSequence mBadgeContentDescription; |
Adam Powell | 13036be | 2015-05-12 14:43:56 -0700 | [diff] [blame] | 2016 | private Drawable mDisplayIcon; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2017 | private final Intent mFillInIntent; |
| 2018 | private final int mFillInFlags; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2019 | private final float mModifiedScore; |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 2020 | private boolean mIsSuspended = false; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2021 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2022 | SelectableTargetInfo(DisplayResolveInfo sourceInfo, ChooserTarget chooserTarget, |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2023 | float modifiedScore) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2024 | mSourceInfo = sourceInfo; |
| 2025 | mChooserTarget = chooserTarget; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2026 | mModifiedScore = modifiedScore; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2027 | if (sourceInfo != null) { |
| 2028 | final ResolveInfo ri = sourceInfo.getResolveInfo(); |
| 2029 | if (ri != null) { |
| 2030 | final ActivityInfo ai = ri.activityInfo; |
| 2031 | if (ai != null && ai.applicationInfo != null) { |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 2032 | final PackageManager pm = getPackageManager(); |
| 2033 | mBadgeIcon = pm.getApplicationIcon(ai.applicationInfo); |
| 2034 | mBadgeContentDescription = pm.getApplicationLabel(ai.applicationInfo); |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 2035 | mIsSuspended = |
| 2036 | (ai.applicationInfo.flags & ApplicationInfo.FLAG_SUSPENDED) != 0; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2037 | } |
| 2038 | } |
| 2039 | } |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 2040 | // TODO(b/121287224): do this in the background thread, and only for selected targets |
| 2041 | mDisplayIcon = getChooserTargetIconDrawable(chooserTarget); |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 2042 | |
| 2043 | if (sourceInfo != null) { |
| 2044 | mBackupResolveInfo = null; |
| 2045 | } else { |
| 2046 | mBackupResolveInfo = getPackageManager().resolveActivity(getResolvedIntent(), 0); |
| 2047 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2048 | |
| 2049 | mFillInIntent = null; |
| 2050 | mFillInFlags = 0; |
Matt Pietal | 9d50143 | 2019-04-12 10:05:29 -0400 | [diff] [blame] | 2051 | |
| 2052 | mDisplayLabel = sanitizeDisplayLabel(chooserTarget.getTitle()); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2053 | } |
| 2054 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2055 | private SelectableTargetInfo(SelectableTargetInfo other, Intent fillInIntent, int flags) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2056 | mSourceInfo = other.mSourceInfo; |
| 2057 | mBackupResolveInfo = other.mBackupResolveInfo; |
| 2058 | mChooserTarget = other.mChooserTarget; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2059 | mBadgeIcon = other.mBadgeIcon; |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 2060 | mBadgeContentDescription = other.mBadgeContentDescription; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2061 | mDisplayIcon = other.mDisplayIcon; |
| 2062 | mFillInIntent = fillInIntent; |
| 2063 | mFillInFlags = flags; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2064 | mModifiedScore = other.mModifiedScore; |
Matt Pietal | 9d50143 | 2019-04-12 10:05:29 -0400 | [diff] [blame] | 2065 | |
| 2066 | mDisplayLabel = sanitizeDisplayLabel(mChooserTarget.getTitle()); |
| 2067 | } |
| 2068 | |
| 2069 | private String sanitizeDisplayLabel(CharSequence label) { |
| 2070 | SpannableStringBuilder sb = new SpannableStringBuilder(label); |
| 2071 | sb.clearSpans(); |
| 2072 | return sb.toString(); |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2073 | } |
| 2074 | |
Matt Pietal | a4b3007 | 2019-04-04 13:44:36 -0400 | [diff] [blame] | 2075 | public boolean isSuspended() { |
| 2076 | return mIsSuspended; |
| 2077 | } |
| 2078 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 2079 | /** |
| 2080 | * Since ShortcutInfos are returned by ShortcutManager, we can cache the shortcuts and skip |
| 2081 | * the call to LauncherApps#getShortcuts(ShortcutQuery). |
| 2082 | */ |
| 2083 | // TODO(121287224): Refactor code to apply the suggestion above |
| 2084 | private Drawable getChooserTargetIconDrawable(ChooserTarget target) { |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 2085 | Drawable directShareIcon = null; |
| 2086 | |
| 2087 | // First get the target drawable and associated activity info |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 2088 | final Icon icon = target.getIcon(); |
| 2089 | if (icon != null) { |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 2090 | directShareIcon = icon.loadDrawable(ChooserActivity.this); |
| 2091 | } else if (USE_SHORTCUT_MANAGER_FOR_DIRECT_TARGETS) { |
| 2092 | Bundle extras = target.getIntentExtras(); |
| 2093 | if (extras != null && extras.containsKey(Intent.EXTRA_SHORTCUT_ID)) { |
| 2094 | CharSequence shortcutId = extras.getCharSequence(Intent.EXTRA_SHORTCUT_ID); |
| 2095 | LauncherApps launcherApps = (LauncherApps) getSystemService( |
| 2096 | Context.LAUNCHER_APPS_SERVICE); |
| 2097 | final LauncherApps.ShortcutQuery q = new LauncherApps.ShortcutQuery(); |
| 2098 | q.setPackage(target.getComponentName().getPackageName()); |
| 2099 | q.setShortcutIds(Arrays.asList(shortcutId.toString())); |
| 2100 | q.setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC); |
| 2101 | final List<ShortcutInfo> shortcuts = launcherApps.getShortcuts(q, getUser()); |
| 2102 | if (shortcuts != null && shortcuts.size() > 0) { |
| 2103 | directShareIcon = launcherApps.getShortcutIconDrawable(shortcuts.get(0), 0); |
| 2104 | } |
| 2105 | } |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 2106 | } |
| 2107 | |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 2108 | if (directShareIcon == null) return null; |
| 2109 | |
| 2110 | ActivityInfo info = null; |
| 2111 | try { |
| 2112 | info = mPm.getActivityInfo(target.getComponentName(), 0); |
| 2113 | } catch (NameNotFoundException error) { |
| 2114 | Log.e(TAG, "Could not find activity associated with ChooserTarget"); |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 2115 | } |
| 2116 | |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 2117 | if (info == null) return null; |
| 2118 | |
| 2119 | // Now fetch app icon and raster with no badging even in work profile |
Mike Digman | b2e5e71 | 2019-04-19 15:49:10 -0700 | [diff] [blame] | 2120 | Bitmap appIcon = makePresentationGetter(info).getIconBitmap( |
| 2121 | UserHandle.getUserHandleForUid(UserHandle.myUserId())); |
Mike Digman | 9c4ae50 | 2019-03-19 17:02:25 -0700 | [diff] [blame] | 2122 | |
| 2123 | // Raster target drawable with appIcon as a badge |
| 2124 | SimpleIconFactory sif = SimpleIconFactory.obtain(ChooserActivity.this); |
| 2125 | Bitmap directShareBadgedIcon = sif.createAppBadgedIconBitmap(directShareIcon, appIcon); |
| 2126 | sif.recycle(); |
| 2127 | |
| 2128 | return new BitmapDrawable(getResources(), directShareBadgedIcon); |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 2129 | } |
| 2130 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2131 | public float getModifiedScore() { |
| 2132 | return mModifiedScore; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | @Override |
| 2136 | public Intent getResolvedIntent() { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2137 | if (mSourceInfo != null) { |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 2138 | return mSourceInfo.getResolvedIntent(); |
| 2139 | } |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 2140 | |
| 2141 | final Intent targetIntent = new Intent(getTargetIntent()); |
| 2142 | targetIntent.setComponent(mChooserTarget.getComponentName()); |
| 2143 | targetIntent.putExtras(mChooserTarget.getIntentExtras()); |
| 2144 | return targetIntent; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | @Override |
| 2148 | public ComponentName getResolvedComponentName() { |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 2149 | if (mSourceInfo != null) { |
| 2150 | return mSourceInfo.getResolvedComponentName(); |
| 2151 | } else if (mBackupResolveInfo != null) { |
| 2152 | return new ComponentName(mBackupResolveInfo.activityInfo.packageName, |
| 2153 | mBackupResolveInfo.activityInfo.name); |
| 2154 | } |
| 2155 | return null; |
| 2156 | } |
| 2157 | |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 2158 | private Intent getBaseIntentToSend() { |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 2159 | Intent result = getResolvedIntent(); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2160 | if (result == null) { |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 2161 | Log.e(TAG, "ChooserTargetInfo: no base intent available to send"); |
Adam Powell | 13036be | 2015-05-12 14:43:56 -0700 | [diff] [blame] | 2162 | } else { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2163 | result = new Intent(result); |
Adam Powell | 13036be | 2015-05-12 14:43:56 -0700 | [diff] [blame] | 2164 | if (mFillInIntent != null) { |
| 2165 | result.fillIn(mFillInIntent, mFillInFlags); |
| 2166 | } |
| 2167 | result.fillIn(mReferrerFillInIntent, 0); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2168 | } |
| 2169 | return result; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | @Override |
| 2173 | public boolean start(Activity activity, Bundle options) { |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 2174 | throw new RuntimeException("ChooserTargets should be started as caller."); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | @Override |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 2178 | public boolean startAsCaller(ResolverActivity activity, Bundle options, int userId) { |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 2179 | final Intent intent = getBaseIntentToSend(); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2180 | if (intent == null) { |
| 2181 | return false; |
| 2182 | } |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 2183 | intent.setComponent(mChooserTarget.getComponentName()); |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 2184 | intent.putExtras(mChooserTarget.getIntentExtras()); |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 2185 | |
| 2186 | // Important: we will ignore the target security checks in ActivityManager |
| 2187 | // if and only if the ChooserTarget's target package is the same package |
| 2188 | // where we got the ChooserTargetService that provided it. This lets a |
| 2189 | // ChooserTargetService provide a non-exported or permission-guarded target |
| 2190 | // to the chooser for the user to pick. |
| 2191 | // |
| 2192 | // If mSourceInfo is null, we got this ChooserTarget from the caller or elsewhere |
| 2193 | // so we'll obey the caller's normal security checks. |
| 2194 | final boolean ignoreTargetSecurity = mSourceInfo != null |
| 2195 | && mSourceInfo.getResolvedComponentName().getPackageName() |
| 2196 | .equals(mChooserTarget.getComponentName().getPackageName()); |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 2197 | return activity.startAsCallerImpl(intent, options, ignoreTargetSecurity, userId); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | @Override |
| 2201 | public boolean startAsUser(Activity activity, Bundle options, UserHandle user) { |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 2202 | throw new RuntimeException("ChooserTargets should be started as caller."); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | @Override |
| 2206 | public ResolveInfo getResolveInfo() { |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 2207 | return mSourceInfo != null ? mSourceInfo.getResolveInfo() : mBackupResolveInfo; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | @Override |
| 2211 | public CharSequence getDisplayLabel() { |
Matt Pietal | 9d50143 | 2019-04-12 10:05:29 -0400 | [diff] [blame] | 2212 | return mDisplayLabel; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | @Override |
| 2216 | public CharSequence getExtendedInfo() { |
Adam Powell | 00f4aad | 2015-09-17 13:38:16 -0700 | [diff] [blame] | 2217 | // ChooserTargets have badge icons, so we won't show the extended info to disambiguate. |
| 2218 | return null; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | @Override |
| 2222 | public Drawable getDisplayIcon() { |
| 2223 | return mDisplayIcon; |
| 2224 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2225 | |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 2226 | public ChooserTarget getChooserTarget() { |
| 2227 | return mChooserTarget; |
| 2228 | } |
| 2229 | |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 2230 | @Override |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2231 | public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2232 | return new SelectableTargetInfo(this, fillInIntent, flags); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | @Override |
| 2236 | public List<Intent> getAllSourceIntents() { |
| 2237 | final List<Intent> results = new ArrayList<>(); |
| 2238 | if (mSourceInfo != null) { |
| 2239 | // We only queried the service for the first one in our sourceinfo. |
| 2240 | results.add(mSourceInfo.getAllSourceIntents().get(0)); |
| 2241 | } |
| 2242 | return results; |
| 2243 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2244 | } |
| 2245 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2246 | private void handleScroll(View view, int x, int y, int oldx, int oldy) { |
| 2247 | if (mChooserRowAdapter != null) { |
| 2248 | mChooserRowAdapter.handleScroll(view, y, oldy); |
| 2249 | } |
| 2250 | } |
| 2251 | |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 2252 | /* |
| 2253 | * Need to dynamically adjust how many icons can fit per row before we add them, |
| 2254 | * which also means setting the correct offset to initially show the content |
| 2255 | * preview area + 2 rows of targets |
| 2256 | */ |
| 2257 | private void handleLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, |
| 2258 | int oldTop, int oldRight, int oldBottom) { |
| 2259 | if (mChooserRowAdapter == null || mAdapterView == null) { |
| 2260 | return; |
| 2261 | } |
| 2262 | |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 2263 | final int availableWidth = right - left - v.getPaddingLeft() - v.getPaddingRight(); |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 2264 | if (mChooserRowAdapter.consumeLayoutRequest() |
| 2265 | || mChooserRowAdapter.calculateChooserTargetWidth(availableWidth) |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 2266 | || mAdapterView.getAdapter() == null |
| 2267 | || availableWidth != mCurrAvailableWidth) { |
| 2268 | mCurrAvailableWidth = availableWidth; |
Matt Pietal | 3e4b56f | 2019-05-31 12:06:17 -0400 | [diff] [blame] | 2269 | mAdapterView.setAdapter(mChooserRowAdapter); |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 2270 | |
| 2271 | getMainThreadHandler().post(() -> { |
| 2272 | if (mResolverDrawerLayout == null || mChooserRowAdapter == null) { |
| 2273 | return; |
| 2274 | } |
| 2275 | |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 2276 | final int bottomInset = mSystemWindowInsets != null |
| 2277 | ? mSystemWindowInsets.bottom : 0; |
| 2278 | int offset = bottomInset; |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 2279 | int rowsToShow = mChooserRowAdapter.getContentPreviewRowCount() |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 2280 | + mChooserRowAdapter.getProfileRowCount() |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 2281 | + mChooserRowAdapter.getServiceTargetRowCount() |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2282 | + mChooserRowAdapter.getCallerAndRankedTargetRowCount(); |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 2283 | |
| 2284 | // then this is most likely not a SEND_* action, so check |
| 2285 | // the app target count |
| 2286 | if (rowsToShow == 0) { |
| 2287 | rowsToShow = mChooserRowAdapter.getCount(); |
| 2288 | } |
| 2289 | |
| 2290 | // still zero? then use a default height and leave, which |
| 2291 | // can happen when there are no targets to show |
| 2292 | if (rowsToShow == 0) { |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 2293 | offset += getResources().getDimensionPixelSize( |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 2294 | R.dimen.chooser_max_collapsed_height); |
| 2295 | mResolverDrawerLayout.setCollapsibleHeightReserved(offset); |
| 2296 | return; |
| 2297 | } |
| 2298 | |
Matt Pietal | 394ebd0 | 2019-05-03 07:36:21 -0400 | [diff] [blame] | 2299 | int directShareHeight = 0; |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 2300 | rowsToShow = Math.min(4, rowsToShow); |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 2301 | for (int i = 0; i < Math.min(rowsToShow, mAdapterView.getChildCount()); i++) { |
Matt Pietal | 394ebd0 | 2019-05-03 07:36:21 -0400 | [diff] [blame] | 2302 | View child = mAdapterView.getChildAt(i); |
| 2303 | int height = child.getHeight(); |
| 2304 | offset += height; |
| 2305 | |
| 2306 | if (child.getTag() != null |
| 2307 | && (child.getTag() instanceof DirectShareViewHolder)) { |
| 2308 | directShareHeight = height; |
| 2309 | } |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 2310 | } |
| 2311 | |
Matt Pietal | 3e4b56f | 2019-05-31 12:06:17 -0400 | [diff] [blame] | 2312 | boolean isExpandable = getResources().getConfiguration().orientation |
| 2313 | == Configuration.ORIENTATION_PORTRAIT && !isInMultiWindowMode(); |
| 2314 | if (directShareHeight != 0 && isSendAction(getTargetIntent()) && isExpandable) { |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 2315 | // make sure to leave room for direct share 4->8 expansion |
Matt Pietal | 394ebd0 | 2019-05-03 07:36:21 -0400 | [diff] [blame] | 2316 | int requiredExpansionHeight = |
| 2317 | (int) (directShareHeight / DIRECT_SHARE_EXPANSION_RATE); |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 2318 | int topInset = mSystemWindowInsets != null ? mSystemWindowInsets.top : 0; |
Matt Pietal | 394ebd0 | 2019-05-03 07:36:21 -0400 | [diff] [blame] | 2319 | int minHeight = bottom - top - mResolverDrawerLayout.getAlwaysShowHeight() |
Matt Pietal | 800136a | 2019-05-08 07:46:39 -0400 | [diff] [blame] | 2320 | - requiredExpansionHeight - topInset - bottomInset; |
Matt Pietal | 394ebd0 | 2019-05-03 07:36:21 -0400 | [diff] [blame] | 2321 | |
| 2322 | offset = Math.min(offset, minHeight); |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 2323 | } |
| 2324 | |
Matt Pietal | 399e8c7 | 2019-04-04 15:49:48 -0400 | [diff] [blame] | 2325 | mResolverDrawerLayout.setCollapsibleHeightReserved(Math.min(offset, bottom - top)); |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 2326 | }); |
| 2327 | } |
| 2328 | } |
| 2329 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2330 | public class ChooserListAdapter extends ResolveListAdapter { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2331 | public static final int TARGET_BAD = -1; |
| 2332 | public static final int TARGET_CALLER = 0; |
| 2333 | public static final int TARGET_SERVICE = 1; |
| 2334 | public static final int TARGET_STANDARD = 2; |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2335 | public static final int TARGET_STANDARD_AZ = 3; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2336 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2337 | private static final int MAX_SUGGESTED_APP_TARGETS = 4; |
Matt Pietal | 791b1c3 | 2019-04-30 13:36:49 -0400 | [diff] [blame] | 2338 | private static final int MAX_CHOOSER_TARGETS_PER_APP = 2; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2339 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2340 | private static final int MAX_SERVICE_TARGETS = 8; |
| 2341 | |
Matt Pietal | 3ed20a7 | 2019-06-24 12:14:52 -0400 | [diff] [blame] | 2342 | private final int mMaxShortcutTargetsPerApp = |
| 2343 | getResources().getInteger(R.integer.config_maxShortcutTargetsPerApp); |
| 2344 | |
Matt Pietal | e54dcc2e | 2019-05-02 12:59:38 -0400 | [diff] [blame] | 2345 | private int mNumShortcutResults = 0; |
| 2346 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2347 | // Reserve spots for incoming direct share targets by adding placeholders |
| 2348 | private ChooserTargetInfo mPlaceHolderTargetInfo = new PlaceHolderTargetInfo(); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2349 | private final List<ChooserTargetInfo> mServiceTargets = new ArrayList<>(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2350 | private final List<TargetInfo> mCallerTargets = new ArrayList<>(); |
Dan Sandler | f5e1769 | 2018-06-04 22:13:40 -0400 | [diff] [blame] | 2351 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2352 | private final BaseChooserTargetComparator mBaseTargetComparator |
| 2353 | = new BaseChooserTargetComparator(); |
| 2354 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2355 | public ChooserListAdapter(Context context, List<Intent> payloadIntents, |
| 2356 | Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid, |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 2357 | boolean filterLastUsed, ResolverListController resolverListController) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2358 | // Don't send the initial intents through the shared ResolverActivity path, |
| 2359 | // we want to separate them into a different section. |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 2360 | super(context, payloadIntents, null, rList, launchedFromUid, filterLastUsed, |
| 2361 | resolverListController); |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 2362 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2363 | createPlaceHolders(); |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2364 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2365 | if (initialIntents != null) { |
| 2366 | final PackageManager pm = getPackageManager(); |
| 2367 | for (int i = 0; i < initialIntents.length; i++) { |
| 2368 | final Intent ii = initialIntents[i]; |
| 2369 | if (ii == null) { |
| 2370 | continue; |
| 2371 | } |
Adam Powell | 86100d1 | 2016-05-12 16:13:17 -0700 | [diff] [blame] | 2372 | |
| 2373 | // We reimplement Intent#resolveActivityInfo here because if we have an |
| 2374 | // implicit intent, we want the ResolveInfo returned by PackageManager |
| 2375 | // instead of one we reconstruct ourselves. The ResolveInfo returned might |
| 2376 | // have extra metadata and resolvePackageName set and we want to respect that. |
| 2377 | ResolveInfo ri = null; |
| 2378 | ActivityInfo ai = null; |
| 2379 | final ComponentName cn = ii.getComponent(); |
| 2380 | if (cn != null) { |
| 2381 | try { |
| 2382 | ai = pm.getActivityInfo(ii.getComponent(), 0); |
| 2383 | ri = new ResolveInfo(); |
| 2384 | ri.activityInfo = ai; |
| 2385 | } catch (PackageManager.NameNotFoundException ignored) { |
| 2386 | // ai will == null below |
| 2387 | } |
| 2388 | } |
| 2389 | if (ai == null) { |
| 2390 | ri = pm.resolveActivity(ii, PackageManager.MATCH_DEFAULT_ONLY); |
| 2391 | ai = ri != null ? ri.activityInfo : null; |
| 2392 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2393 | if (ai == null) { |
| 2394 | Log.w(TAG, "No activity found for " + ii); |
| 2395 | continue; |
| 2396 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2397 | UserManager userManager = |
| 2398 | (UserManager) getSystemService(Context.USER_SERVICE); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2399 | if (ii instanceof LabeledIntent) { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 2400 | LabeledIntent li = (LabeledIntent) ii; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2401 | ri.resolvePackageName = li.getSourcePackage(); |
| 2402 | ri.labelRes = li.getLabelResource(); |
| 2403 | ri.nonLocalizedLabel = li.getNonLocalizedLabel(); |
| 2404 | ri.icon = li.getIconResource(); |
Sudheer Shanka | 9ded760 | 2015-05-19 21:17:25 +0100 | [diff] [blame] | 2405 | ri.iconResourceId = ri.icon; |
| 2406 | } |
| 2407 | if (userManager.isManagedProfile()) { |
| 2408 | ri.noResourceId = true; |
| 2409 | ri.icon = 0; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2410 | } |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 2411 | ResolveInfoPresentationGetter getter = makePresentationGetter(ri); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2412 | mCallerTargets.add(new DisplayResolveInfo(ii, ri, |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 2413 | getter.getLabel(), getter.getSubLabel(), ii)); |
Adam Powell | d974c7b | 2015-04-28 15:41:46 -0700 | [diff] [blame] | 2414 | } |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 2415 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 2418 | @Override |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 2419 | public void handlePackagesChanged() { |
| 2420 | if (DEBUG) { |
| 2421 | Log.d(TAG, "clearing queryTargets on package change"); |
| 2422 | } |
| 2423 | createPlaceHolders(); |
| 2424 | mServicesRequested.clear(); |
| 2425 | notifyDataSetChanged(); |
| 2426 | |
| 2427 | super.handlePackagesChanged(); |
| 2428 | } |
| 2429 | |
| 2430 | @Override |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 2431 | public void notifyDataSetChanged() { |
| 2432 | if (!mListViewDataChanged) { |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 2433 | mChooserHandler.sendEmptyMessageDelayed(ChooserHandler.LIST_VIEW_UPDATE_MESSAGE, |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 2434 | LIST_VIEW_UPDATE_INTERVAL_IN_MILLIS); |
| 2435 | mListViewDataChanged = true; |
| 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | private void refreshListView() { |
| 2440 | if (mListViewDataChanged) { |
| 2441 | super.notifyDataSetChanged(); |
| 2442 | } |
| 2443 | mListViewDataChanged = false; |
| 2444 | } |
| 2445 | |
| 2446 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2447 | private void createPlaceHolders() { |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 2448 | mNumShortcutResults = 0; |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2449 | mServiceTargets.clear(); |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2450 | for (int i = 0; i < MAX_SERVICE_TARGETS; i++) { |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2451 | mServiceTargets.add(mPlaceHolderTargetInfo); |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2452 | } |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2453 | } |
| 2454 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2455 | @Override |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2456 | public View onCreateView(ViewGroup parent) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2457 | return mInflater.inflate( |
| 2458 | com.android.internal.R.layout.resolve_grid_item, parent, false); |
| 2459 | } |
| 2460 | |
| 2461 | @Override |
Mike Digman | bd7e0df | 2019-04-23 14:52:45 -0700 | [diff] [blame] | 2462 | protected void onBindView(View view, TargetInfo info) { |
| 2463 | super.onBindView(view, info); |
| 2464 | |
Mike Digman | 4b83c21 | 2019-05-03 10:17:35 -0700 | [diff] [blame] | 2465 | // If target is loading, show a special placeholder shape in the label, make unclickable |
Mike Digman | bd7e0df | 2019-04-23 14:52:45 -0700 | [diff] [blame] | 2466 | final ViewHolder holder = (ViewHolder) view.getTag(); |
| 2467 | if (info instanceof PlaceHolderTargetInfo) { |
| 2468 | final int maxWidth = getResources().getDimensionPixelSize( |
| 2469 | R.dimen.chooser_direct_share_label_placeholder_max_width); |
| 2470 | holder.text.setMaxWidth(maxWidth); |
| 2471 | holder.text.setBackground(getResources().getDrawable( |
| 2472 | R.drawable.chooser_direct_share_label_placeholder, getTheme())); |
Mike Digman | 4b83c21 | 2019-05-03 10:17:35 -0700 | [diff] [blame] | 2473 | // Prevent rippling by removing background containing ripple |
| 2474 | holder.itemView.setBackground(null); |
Mike Digman | bd7e0df | 2019-04-23 14:52:45 -0700 | [diff] [blame] | 2475 | } else { |
| 2476 | holder.text.setMaxWidth(Integer.MAX_VALUE); |
| 2477 | holder.text.setBackground(null); |
Mike Digman | 4b83c21 | 2019-05-03 10:17:35 -0700 | [diff] [blame] | 2478 | holder.itemView.setBackground(holder.defaultItemViewBackground); |
Mike Digman | bd7e0df | 2019-04-23 14:52:45 -0700 | [diff] [blame] | 2479 | } |
| 2480 | } |
| 2481 | |
| 2482 | @Override |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2483 | public void onListRebuilt() { |
Matt Pietal | 030bd84 | 2019-05-29 07:14:14 -0400 | [diff] [blame] | 2484 | updateAlphabeticalList(); |
| 2485 | |
Ng Zhi An | d3ec5fc | 2018-05-10 09:13:00 -0700 | [diff] [blame] | 2486 | // don't support direct share on low ram devices |
| 2487 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 2488 | return; |
| 2489 | } |
| 2490 | |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 2491 | if (USE_SHORTCUT_MANAGER_FOR_DIRECT_TARGETS |
| 2492 | || USE_PREDICTION_MANAGER_FOR_DIRECT_TARGETS) { |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 2493 | if (DEBUG) { |
| 2494 | Log.d(TAG, "querying direct share targets from ShortcutManager"); |
| 2495 | } |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 2496 | |
George Hodulik | 3f399f2 | 2019-04-26 16:17:54 -0700 | [diff] [blame] | 2497 | queryDirectShareTargets(this, false); |
Mehdi Alizadeh | 8e248ad | 2019-01-17 11:41:49 -0800 | [diff] [blame] | 2498 | } |
| 2499 | if (USE_CHOOSER_TARGET_SERVICE_FOR_DIRECT_TARGETS) { |
| 2500 | if (DEBUG) { |
| 2501 | Log.d(TAG, "List built querying services"); |
| 2502 | } |
Matt Pietal | af044ae | 2019-03-29 06:53:53 -0400 | [diff] [blame] | 2503 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 2504 | queryTargetServices(this); |
| 2505 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | @Override |
Adam Powell | c6d5e3a | 2015-04-23 12:22:20 -0700 | [diff] [blame] | 2509 | public boolean shouldGetResolvedFilter() { |
| 2510 | return true; |
| 2511 | } |
| 2512 | |
| 2513 | @Override |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2514 | public int getCount() { |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2515 | return getRankedTargetCount() + getAlphaTargetCount() |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2516 | + getSelectableServiceTargetCount() + getCallerTargetCount(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2517 | } |
| 2518 | |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 2519 | @Override |
| 2520 | public int getUnfilteredCount() { |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2521 | int appTargets = super.getUnfilteredCount(); |
Alison Cichowlas | 1331461 | 2019-04-11 15:20:39 -0400 | [diff] [blame] | 2522 | if (appTargets > getMaxRankedTargets()) { |
| 2523 | appTargets = appTargets + getMaxRankedTargets(); |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2524 | } |
| 2525 | return appTargets + getSelectableServiceTargetCount() + getCallerTargetCount(); |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 2526 | } |
| 2527 | |
Alison Cichowlas | 1331461 | 2019-04-11 15:20:39 -0400 | [diff] [blame] | 2528 | |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 2529 | public int getCallerTargetCount() { |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2530 | return Math.min(mCallerTargets.size(), MAX_SUGGESTED_APP_TARGETS); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2531 | } |
| 2532 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2533 | /** |
| 2534 | * Filter out placeholders and non-selectable service targets |
| 2535 | */ |
| 2536 | public int getSelectableServiceTargetCount() { |
| 2537 | int count = 0; |
| 2538 | for (ChooserTargetInfo info : mServiceTargets) { |
| 2539 | if (info instanceof SelectableTargetInfo) { |
| 2540 | count++; |
| 2541 | } |
Adam Powell | 565943f | 2016-02-11 10:29:05 -0800 | [diff] [blame] | 2542 | } |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2543 | return count; |
| 2544 | } |
| 2545 | |
| 2546 | public int getServiceTargetCount() { |
Matt Pietal | 6e88b51 | 2019-06-10 10:20:15 -0400 | [diff] [blame] | 2547 | if (isSendAction(getTargetIntent()) && !ActivityManager.isLowRamDeviceStatic()) { |
Matt Pietal | 95574b0 | 2019-03-13 08:12:25 -0400 | [diff] [blame] | 2548 | return Math.min(mServiceTargets.size(), MAX_SERVICE_TARGETS); |
| 2549 | } |
| 2550 | |
| 2551 | return 0; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2552 | } |
| 2553 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2554 | int getAlphaTargetCount() { |
| 2555 | int standardCount = super.getCount(); |
Alison Cichowlas | 1331461 | 2019-04-11 15:20:39 -0400 | [diff] [blame] | 2556 | return standardCount > getMaxRankedTargets() ? standardCount : 0; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2557 | } |
| 2558 | |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2559 | int getRankedTargetCount() { |
Alison Cichowlas | 1331461 | 2019-04-11 15:20:39 -0400 | [diff] [blame] | 2560 | int spacesAvailable = getMaxRankedTargets() - getCallerTargetCount(); |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2561 | return Math.min(spacesAvailable, super.getCount()); |
| 2562 | } |
| 2563 | |
Alison Cichowlas | 1331461 | 2019-04-11 15:20:39 -0400 | [diff] [blame] | 2564 | private int getMaxRankedTargets() { |
| 2565 | return mChooserRowAdapter == null ? 4 : mChooserRowAdapter.getMaxTargetsPerRow(); |
| 2566 | } |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2567 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2568 | public int getPositionTargetType(int position) { |
| 2569 | int offset = 0; |
| 2570 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2571 | final int serviceTargetCount = getServiceTargetCount(); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2572 | if (position < serviceTargetCount) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2573 | return TARGET_SERVICE; |
| 2574 | } |
| 2575 | offset += serviceTargetCount; |
| 2576 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2577 | final int callerTargetCount = getCallerTargetCount(); |
| 2578 | if (position - offset < callerTargetCount) { |
| 2579 | return TARGET_CALLER; |
| 2580 | } |
| 2581 | offset += callerTargetCount; |
| 2582 | |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2583 | final int rankedTargetCount = getRankedTargetCount(); |
| 2584 | if (position - offset < rankedTargetCount) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2585 | return TARGET_STANDARD; |
| 2586 | } |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2587 | offset += rankedTargetCount; |
| 2588 | |
| 2589 | final int standardTargetCount = getAlphaTargetCount(); |
| 2590 | if (position - offset < standardTargetCount) { |
| 2591 | return TARGET_STANDARD_AZ; |
| 2592 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2593 | |
| 2594 | return TARGET_BAD; |
| 2595 | } |
| 2596 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2597 | @Override |
| 2598 | public TargetInfo getItem(int position) { |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 2599 | return targetInfoForPosition(position, true); |
| 2600 | } |
| 2601 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2602 | |
| 2603 | /** |
| 2604 | * Find target info for a given position. |
| 2605 | * Since ChooserActivity displays several sections of content, determine which |
| 2606 | * section provides this item. |
| 2607 | */ |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 2608 | @Override |
| 2609 | public TargetInfo targetInfoForPosition(int position, boolean filtered) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2610 | int offset = 0; |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 2611 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2612 | // Direct share targets |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2613 | final int serviceTargetCount = filtered ? getServiceTargetCount() : |
| 2614 | getSelectableServiceTargetCount(); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2615 | if (position < serviceTargetCount) { |
| 2616 | return mServiceTargets.get(position); |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 2617 | } |
| 2618 | offset += serviceTargetCount; |
| 2619 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2620 | // Targets provided by calling app |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2621 | final int callerTargetCount = getCallerTargetCount(); |
| 2622 | if (position - offset < callerTargetCount) { |
| 2623 | return mCallerTargets.get(position - offset); |
| 2624 | } |
| 2625 | offset += callerTargetCount; |
| 2626 | |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2627 | // Ranked standard app targets |
| 2628 | final int rankedTargetCount = getRankedTargetCount(); |
| 2629 | if (position - offset < rankedTargetCount) { |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2630 | return filtered ? super.getItem(position - offset) |
| 2631 | : getDisplayResolveInfo(position - offset); |
| 2632 | } |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2633 | offset += rankedTargetCount; |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2634 | |
| 2635 | // Alphabetical complete app target list. |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2636 | if (position - offset < getAlphaTargetCount() && !mSortedList.isEmpty()) { |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2637 | return mSortedList.get(position - offset); |
| 2638 | } |
| 2639 | |
| 2640 | return null; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2641 | } |
| 2642 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2643 | |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2644 | /** |
| 2645 | * Evaluate targets for inclusion in the direct share area. May not be included |
| 2646 | * if score is too low. |
| 2647 | */ |
| 2648 | public void addServiceResults(DisplayResolveInfo origTarget, List<ChooserTarget> targets, |
| 2649 | boolean isShortcutResult) { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 2650 | if (DEBUG) { |
| 2651 | Log.d(TAG, "addServiceResults " + origTarget + ", " + targets.size() |
| 2652 | + " targets"); |
| 2653 | } |
Dan Sandler | f5e1769 | 2018-06-04 22:13:40 -0400 | [diff] [blame] | 2654 | |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2655 | if (targets.size() == 0) { |
| 2656 | return; |
| 2657 | } |
| 2658 | |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2659 | final float baseScore = getBaseScore(origTarget, isShortcutResult); |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2660 | Collections.sort(targets, mBaseTargetComparator); |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2661 | |
Matt Pietal | 3ed20a7 | 2019-06-24 12:14:52 -0400 | [diff] [blame] | 2662 | final int maxTargets = isShortcutResult ? mMaxShortcutTargetsPerApp |
Matt Pietal | 791b1c3 | 2019-04-30 13:36:49 -0400 | [diff] [blame] | 2663 | : MAX_CHOOSER_TARGETS_PER_APP; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2664 | float lastScore = 0; |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2665 | boolean shouldNotify = false; |
Matt Pietal | 791b1c3 | 2019-04-30 13:36:49 -0400 | [diff] [blame] | 2666 | for (int i = 0, count = Math.min(targets.size(), maxTargets); i < count; i++) { |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2667 | final ChooserTarget target = targets.get(i); |
| 2668 | float targetScore = target.getScore(); |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2669 | targetScore *= baseScore; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2670 | if (i > 0 && targetScore >= lastScore) { |
| 2671 | // Apply a decay so that the top app can't crowd out everything else. |
| 2672 | // This incents ChooserTargetServices to define what's truly better. |
| 2673 | targetScore = lastScore * 0.95f; |
| 2674 | } |
Matt Pietal | e54dcc2e | 2019-05-02 12:59:38 -0400 | [diff] [blame] | 2675 | boolean isInserted = insertServiceTarget( |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2676 | new SelectableTargetInfo(origTarget, target, targetScore)); |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2677 | |
Matt Pietal | e54dcc2e | 2019-05-02 12:59:38 -0400 | [diff] [blame] | 2678 | if (isInserted && isShortcutResult) { |
| 2679 | mNumShortcutResults++; |
| 2680 | } |
| 2681 | |
| 2682 | shouldNotify |= isInserted; |
| 2683 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2684 | if (DEBUG) { |
| 2685 | Log.d(TAG, " => " + target.toString() + " score=" + targetScore |
| 2686 | + " base=" + target.getScore() |
| 2687 | + " lastScore=" + lastScore |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2688 | + " baseScore=" + baseScore); |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2689 | } |
| 2690 | |
| 2691 | lastScore = targetScore; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2692 | } |
| 2693 | |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2694 | if (shouldNotify) { |
| 2695 | notifyDataSetChanged(); |
| 2696 | } |
| 2697 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2698 | |
Matt Pietal | e54dcc2e | 2019-05-02 12:59:38 -0400 | [diff] [blame] | 2699 | private int getNumShortcutResults() { |
| 2700 | return mNumShortcutResults; |
| 2701 | } |
| 2702 | |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2703 | /** |
Matt Pietal | 39d181d | 2019-05-08 14:48:30 -0400 | [diff] [blame] | 2704 | * Use the scoring system along with artificial boosts to create up to 4 distinct buckets: |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2705 | * <ol> |
| 2706 | * <li>App-supplied targets |
Matt Pietal | 39d181d | 2019-05-08 14:48:30 -0400 | [diff] [blame] | 2707 | * <li>Shortcuts ranked via App Prediction Manager |
| 2708 | * <li>Shortcuts ranked via legacy heuristics |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2709 | * <li>Legacy direct share targets |
| 2710 | * </ol> |
| 2711 | */ |
| 2712 | private float getBaseScore(DisplayResolveInfo target, boolean isShortcutResult) { |
| 2713 | if (target == null) { |
| 2714 | return CALLER_TARGET_SCORE_BOOST; |
| 2715 | } |
| 2716 | |
Matt Pietal | 39d181d | 2019-05-08 14:48:30 -0400 | [diff] [blame] | 2717 | if (isShortcutResult && getAppPredictorForDirectShareIfEnabled() != null) { |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2718 | return SHORTCUT_TARGET_SCORE_BOOST; |
| 2719 | } |
| 2720 | |
| 2721 | float score = super.getScore(target); |
| 2722 | if (isShortcutResult) { |
| 2723 | return score * SHORTCUT_TARGET_SCORE_BOOST; |
| 2724 | } |
| 2725 | |
| 2726 | return score; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2727 | } |
| 2728 | |
Adam Powell | 565943f | 2016-02-11 10:29:05 -0800 | [diff] [blame] | 2729 | /** |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2730 | * Calling this marks service target loading complete, and will attempt to no longer |
| 2731 | * update the direct share area. |
| 2732 | */ |
| 2733 | public void completeServiceTargetLoading() { |
| 2734 | mServiceTargets.removeIf(o -> o instanceof PlaceHolderTargetInfo); |
| 2735 | |
| 2736 | if (mServiceTargets.isEmpty()) { |
| 2737 | mServiceTargets.add(new EmptyTargetInfo()); |
| 2738 | } |
| 2739 | notifyDataSetChanged(); |
| 2740 | } |
| 2741 | |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2742 | private boolean insertServiceTarget(ChooserTargetInfo chooserTargetInfo) { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2743 | // Avoid inserting any potentially late results |
| 2744 | if (mServiceTargets.size() == 1 |
| 2745 | && mServiceTargets.get(0) instanceof EmptyTargetInfo) { |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2746 | return false; |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2747 | } |
| 2748 | |
Matt Pietal | 9d50143 | 2019-04-12 10:05:29 -0400 | [diff] [blame] | 2749 | // Check for duplicates and abort if found |
| 2750 | for (ChooserTargetInfo otherTargetInfo : mServiceTargets) { |
| 2751 | if (chooserTargetInfo.isSimilar(otherTargetInfo)) { |
| 2752 | return false; |
| 2753 | } |
| 2754 | } |
| 2755 | |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2756 | int currentSize = mServiceTargets.size(); |
Matt Pietal | 9d50143 | 2019-04-12 10:05:29 -0400 | [diff] [blame] | 2757 | final float newScore = chooserTargetInfo.getModifiedScore(); |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2758 | for (int i = 0; i < Math.min(currentSize, MAX_SERVICE_TARGETS); i++) { |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2759 | final ChooserTargetInfo serviceTarget = mServiceTargets.get(i); |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2760 | if (serviceTarget == null) { |
| 2761 | mServiceTargets.set(i, chooserTargetInfo); |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2762 | return true; |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2763 | } else if (newScore > serviceTarget.getModifiedScore()) { |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2764 | mServiceTargets.add(i, chooserTargetInfo); |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2765 | return true; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2766 | } |
| 2767 | } |
Matt Pietal | fbfa049 | 2019-04-01 11:29:56 -0400 | [diff] [blame] | 2768 | |
| 2769 | if (currentSize < MAX_SERVICE_TARGETS) { |
| 2770 | mServiceTargets.add(chooserTargetInfo); |
| 2771 | return true; |
| 2772 | } |
| 2773 | |
| 2774 | return false; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2775 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2778 | static class BaseChooserTargetComparator implements Comparator<ChooserTarget> { |
| 2779 | @Override |
| 2780 | public int compare(ChooserTarget lhs, ChooserTarget rhs) { |
| 2781 | // Descending order |
Adam Powell | 77a533f | 2015-10-16 10:47:32 -0700 | [diff] [blame] | 2782 | return (int) Math.signum(rhs.getScore() - lhs.getScore()); |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2783 | } |
| 2784 | } |
| 2785 | |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2786 | |
Matt Pietal | 95574b0 | 2019-03-13 08:12:25 -0400 | [diff] [blame] | 2787 | private boolean isSendAction(Intent targetIntent) { |
| 2788 | if (targetIntent == null) { |
| 2789 | return false; |
| 2790 | } |
| 2791 | |
| 2792 | String action = targetIntent.getAction(); |
| 2793 | if (action == null) { |
| 2794 | return false; |
| 2795 | } |
| 2796 | |
| 2797 | if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)) { |
| 2798 | return true; |
| 2799 | } |
| 2800 | |
| 2801 | return false; |
| 2802 | } |
| 2803 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2804 | class ChooserRowAdapter extends BaseAdapter { |
| 2805 | private ChooserListAdapter mChooserListAdapter; |
| 2806 | private final LayoutInflater mLayoutInflater; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2807 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2808 | private DirectShareViewHolder mDirectShareViewHolder; |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 2809 | private int mChooserTargetWidth = 0; |
Mike Digman | 849a9d1 | 2019-04-29 11:20:48 -0700 | [diff] [blame] | 2810 | private boolean mShowAzLabelIfPoss; |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2811 | |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 2812 | private boolean mHideContentPreview = false; |
| 2813 | private boolean mLayoutRequested = false; |
| 2814 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2815 | private static final int VIEW_TYPE_DIRECT_SHARE = 0; |
| 2816 | private static final int VIEW_TYPE_NORMAL = 1; |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 2817 | private static final int VIEW_TYPE_CONTENT_PREVIEW = 2; |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 2818 | private static final int VIEW_TYPE_PROFILE = 3; |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 2819 | private static final int VIEW_TYPE_AZ_LABEL = 4; |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2820 | |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 2821 | private static final int MAX_TARGETS_PER_ROW_PORTRAIT = 4; |
| 2822 | private static final int MAX_TARGETS_PER_ROW_LANDSCAPE = 8; |
| 2823 | |
Mike Digman | 849a9d1 | 2019-04-29 11:20:48 -0700 | [diff] [blame] | 2824 | private static final int NUM_EXPANSIONS_TO_HIDE_AZ_LABEL = 20; |
| 2825 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2826 | public ChooserRowAdapter(ChooserListAdapter wrappedAdapter) { |
| 2827 | mChooserListAdapter = wrappedAdapter; |
| 2828 | mLayoutInflater = LayoutInflater.from(ChooserActivity.this); |
| 2829 | |
Mike Digman | 849a9d1 | 2019-04-29 11:20:48 -0700 | [diff] [blame] | 2830 | mShowAzLabelIfPoss = getNumSheetExpansions() < NUM_EXPANSIONS_TO_HIDE_AZ_LABEL; |
| 2831 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2832 | wrappedAdapter.registerDataSetObserver(new DataSetObserver() { |
| 2833 | @Override |
| 2834 | public void onChanged() { |
| 2835 | super.onChanged(); |
| 2836 | notifyDataSetChanged(); |
| 2837 | } |
| 2838 | |
| 2839 | @Override |
| 2840 | public void onInvalidated() { |
| 2841 | super.onInvalidated(); |
| 2842 | notifyDataSetInvalidated(); |
| 2843 | } |
| 2844 | }); |
| 2845 | } |
| 2846 | |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 2847 | /** |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 2848 | * Calculate the chooser target width to maximize space per item |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 2849 | * |
| 2850 | * @param width The new row width to use for recalculation |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 2851 | * @return true if the view width has changed |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 2852 | */ |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 2853 | public boolean calculateChooserTargetWidth(int width) { |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 2854 | if (width == 0) { |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 2855 | return false; |
| 2856 | } |
| 2857 | |
Matt Pietal | 9d50143 | 2019-04-12 10:05:29 -0400 | [diff] [blame] | 2858 | int newWidth = width / getMaxTargetsPerRow(); |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 2859 | if (newWidth != mChooserTargetWidth) { |
| 2860 | mChooserTargetWidth = newWidth; |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 2861 | return true; |
| 2862 | } |
| 2863 | |
| 2864 | return false; |
| 2865 | } |
| 2866 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2867 | private int getMaxTargetsPerRow() { |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 2868 | int maxTargets = MAX_TARGETS_PER_ROW_PORTRAIT; |
Matt Pietal | 3e4b56f | 2019-05-31 12:06:17 -0400 | [diff] [blame] | 2869 | if (shouldDisplayLandscape(getResources().getConfiguration().orientation)) { |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 2870 | maxTargets = MAX_TARGETS_PER_ROW_LANDSCAPE; |
| 2871 | } |
| 2872 | |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 2873 | return maxTargets; |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2874 | } |
| 2875 | |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 2876 | public void hideContentPreview() { |
| 2877 | mHideContentPreview = true; |
| 2878 | mLayoutRequested = true; |
| 2879 | notifyDataSetChanged(); |
| 2880 | } |
| 2881 | |
| 2882 | public boolean consumeLayoutRequest() { |
| 2883 | boolean oldValue = mLayoutRequested; |
| 2884 | mLayoutRequested = false; |
| 2885 | return oldValue; |
| 2886 | } |
| 2887 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2888 | @Override |
Matt Pietal | 8a8cfc4 | 2019-04-30 07:59:14 -0400 | [diff] [blame] | 2889 | public boolean areAllItemsEnabled() { |
| 2890 | return false; |
| 2891 | } |
| 2892 | |
| 2893 | @Override |
| 2894 | public boolean isEnabled(int position) { |
| 2895 | int viewType = getItemViewType(position); |
Matt Pietal | 9462c0b | 2019-05-16 09:26:33 -0400 | [diff] [blame] | 2896 | if (viewType == VIEW_TYPE_CONTENT_PREVIEW || viewType == VIEW_TYPE_AZ_LABEL) { |
Matt Pietal | 8a8cfc4 | 2019-04-30 07:59:14 -0400 | [diff] [blame] | 2897 | return false; |
| 2898 | } |
| 2899 | return true; |
| 2900 | } |
| 2901 | |
| 2902 | @Override |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2903 | public int getCount() { |
| 2904 | return (int) ( |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 2905 | getContentPreviewRowCount() |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 2906 | + getProfileRowCount() |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 2907 | + getServiceTargetRowCount() |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2908 | + getCallerAndRankedTargetRowCount() |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 2909 | + getAzLabelRowCount() |
Alison Cichowlas | 1c8816c | 2019-04-03 17:43:22 -0400 | [diff] [blame] | 2910 | + Math.ceil( |
| 2911 | (float) mChooserListAdapter.getAlphaTargetCount() |
| 2912 | / getMaxTargetsPerRow()) |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2913 | ); |
| 2914 | } |
| 2915 | |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 2916 | public int getContentPreviewRowCount() { |
| 2917 | if (!isSendAction(getTargetIntent())) { |
| 2918 | return 0; |
| 2919 | } |
| 2920 | |
Matt Pietal | e7cacab | 2019-05-23 07:21:36 -0400 | [diff] [blame] | 2921 | if (mHideContentPreview || mChooserListAdapter == null |
| 2922 | || mChooserListAdapter.getCount() == 0) { |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 2923 | return 0; |
| 2924 | } |
| 2925 | |
| 2926 | return 1; |
| 2927 | } |
| 2928 | |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 2929 | public int getProfileRowCount() { |
| 2930 | return mChooserListAdapter.getOtherProfile() == null ? 0 : 1; |
| 2931 | } |
| 2932 | |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2933 | public int getCallerAndRankedTargetRowCount() { |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2934 | return (int) Math.ceil( |
Alison Cichowlas | d0a075b | 2019-04-10 20:18:59 -0400 | [diff] [blame] | 2935 | ((float) mChooserListAdapter.getCallerTargetCount() |
| 2936 | + mChooserListAdapter.getRankedTargetCount()) / getMaxTargetsPerRow()); |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2937 | } |
| 2938 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2939 | // There can be at most one row in the listview, that is internally |
| 2940 | // a ViewGroup with 2 rows |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2941 | public int getServiceTargetRowCount() { |
Matt Pietal | 030bd84 | 2019-05-29 07:14:14 -0400 | [diff] [blame] | 2942 | if (isSendAction(getTargetIntent()) && !ActivityManager.isLowRamDeviceStatic()) { |
Matt Pietal | 95574b0 | 2019-03-13 08:12:25 -0400 | [diff] [blame] | 2943 | return 1; |
| 2944 | } |
| 2945 | return 0; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2946 | } |
| 2947 | |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 2948 | public int getAzLabelRowCount() { |
| 2949 | // Only show a label if the a-z list is showing |
Mike Digman | 849a9d1 | 2019-04-29 11:20:48 -0700 | [diff] [blame] | 2950 | return (mShowAzLabelIfPoss && mChooserListAdapter.getAlphaTargetCount() > 0) ? 1 : 0; |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 2951 | } |
| 2952 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2953 | @Override |
| 2954 | public Object getItem(int position) { |
| 2955 | // We have nothing useful to return here. |
| 2956 | return position; |
| 2957 | } |
| 2958 | |
| 2959 | @Override |
| 2960 | public long getItemId(int position) { |
| 2961 | return position; |
| 2962 | } |
| 2963 | |
| 2964 | @Override |
| 2965 | public View getView(int position, View convertView, ViewGroup parent) { |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2966 | final RowViewHolder holder; |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2967 | int viewType = getItemViewType(position); |
| 2968 | |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 2969 | if (viewType == VIEW_TYPE_CONTENT_PREVIEW) { |
| 2970 | return createContentPreviewView(convertView, parent); |
| 2971 | } |
| 2972 | |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 2973 | if (viewType == VIEW_TYPE_PROFILE) { |
| 2974 | return createProfileView(convertView, parent); |
| 2975 | } |
| 2976 | |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 2977 | if (viewType == VIEW_TYPE_AZ_LABEL) { |
| 2978 | return createAzLabelView(parent); |
| 2979 | } |
| 2980 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2981 | if (convertView == null) { |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2982 | holder = createViewHolder(viewType, parent); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2983 | } else { |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2984 | holder = (RowViewHolder) convertView.getTag(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2985 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2986 | |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 2987 | bindViewHolder(position, holder); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2988 | |
| 2989 | return holder.getViewGroup(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2990 | } |
| 2991 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 2992 | @Override |
| 2993 | public int getItemViewType(int position) { |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 2994 | int count; |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 2995 | |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 2996 | int countSum = (count = getContentPreviewRowCount()); |
| 2997 | if (count > 0 && position < countSum) return VIEW_TYPE_CONTENT_PREVIEW; |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 2998 | |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 2999 | countSum += (count = getProfileRowCount()); |
| 3000 | if (count > 0 && position < countSum) return VIEW_TYPE_PROFILE; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 3001 | |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 3002 | countSum += (count = getServiceTargetRowCount()); |
| 3003 | if (count > 0 && position < countSum) return VIEW_TYPE_DIRECT_SHARE; |
| 3004 | |
| 3005 | countSum += (count = getCallerAndRankedTargetRowCount()); |
| 3006 | if (count > 0 && position < countSum) return VIEW_TYPE_NORMAL; |
| 3007 | |
| 3008 | countSum += (count = getAzLabelRowCount()); |
| 3009 | if (count > 0 && position < countSum) return VIEW_TYPE_AZ_LABEL; |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3010 | |
| 3011 | return VIEW_TYPE_NORMAL; |
| 3012 | } |
| 3013 | |
| 3014 | @Override |
| 3015 | public int getViewTypeCount() { |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 3016 | return 5; |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | private ViewGroup createContentPreviewView(View convertView, ViewGroup parent) { |
| 3020 | Intent targetIntent = getTargetIntent(); |
| 3021 | int previewType = findPreferredContentPreview(targetIntent, getContentResolver()); |
| 3022 | |
| 3023 | if (convertView == null) { |
| 3024 | getMetricsLogger().write(new LogMaker(MetricsEvent.ACTION_SHARE_WITH_PREVIEW) |
| 3025 | .setSubtype(previewType)); |
| 3026 | } |
| 3027 | |
| 3028 | return displayContentPreview(previewType, targetIntent, mLayoutInflater, |
| 3029 | (ViewGroup) convertView, parent); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3030 | } |
| 3031 | |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 3032 | private View createProfileView(View convertView, ViewGroup parent) { |
| 3033 | View profileRow = convertView != null ? convertView : mLayoutInflater.inflate( |
| 3034 | R.layout.chooser_profile_row, parent, false); |
| 3035 | profileRow.setBackground( |
| 3036 | getResources().getDrawable(R.drawable.chooser_row_layer_list, null)); |
| 3037 | mProfileView = profileRow.findViewById(R.id.profile_button); |
| 3038 | mProfileView.setOnClickListener(ChooserActivity.this::onProfileClick); |
| 3039 | bindProfileView(); |
| 3040 | return profileRow; |
| 3041 | } |
| 3042 | |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 3043 | private View createAzLabelView(ViewGroup parent) { |
| 3044 | return mLayoutInflater.inflate(R.layout.chooser_az_label_row, parent, false); |
| 3045 | } |
| 3046 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3047 | private RowViewHolder loadViewsIntoRow(RowViewHolder holder) { |
| 3048 | final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 3049 | final int exactSpec = MeasureSpec.makeMeasureSpec(mChooserTargetWidth, |
| 3050 | MeasureSpec.EXACTLY); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3051 | int columnCount = holder.getColumnCount(); |
| 3052 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3053 | final boolean isDirectShare = holder instanceof DirectShareViewHolder; |
| 3054 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3055 | for (int i = 0; i < columnCount; i++) { |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3056 | final View v = mChooserListAdapter.createView(holder.getRowByIndex(i)); |
Adam Powell | 4eb9871 | 2015-10-14 13:10:18 -0700 | [diff] [blame] | 3057 | final int column = i; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 3058 | v.setOnClickListener(new OnClickListener() { |
| 3059 | @Override |
| 3060 | public void onClick(View v) { |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3061 | startSelected(holder.getItemIndex(column), false, true); |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 3062 | } |
| 3063 | }); |
| 3064 | v.setOnLongClickListener(new OnLongClickListener() { |
| 3065 | @Override |
| 3066 | public boolean onLongClick(View v) { |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 3067 | showTargetDetails( |
Adam Powell | 4eb9871 | 2015-10-14 13:10:18 -0700 | [diff] [blame] | 3068 | mChooserListAdapter.resolveInfoForPosition( |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3069 | holder.getItemIndex(column), true)); |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 3070 | return true; |
| 3071 | } |
| 3072 | }); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3073 | ViewGroup row = holder.addView(i, v); |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 3074 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3075 | // Force Direct Share to be 2 lines and auto-wrap to second line via hoz scroll = |
| 3076 | // false. TextView#setHorizontallyScrolling must be reset after #setLines. Must be |
| 3077 | // done before measuring. |
| 3078 | if (isDirectShare) { |
| 3079 | final ViewHolder vh = (ViewHolder) v.getTag(); |
| 3080 | vh.text.setLines(2); |
| 3081 | vh.text.setHorizontallyScrolling(false); |
| 3082 | vh.text2.setVisibility(View.GONE); |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 3083 | } |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3084 | |
| 3085 | // Force height to be a given so we don't have visual disruption during scaling. |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 3086 | v.measure(exactSpec, spec); |
| 3087 | setViewBounds(v, v.getMeasuredWidth(), v.getMeasuredHeight()); |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 3088 | } |
| 3089 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3090 | final ViewGroup viewGroup = holder.getViewGroup(); |
| 3091 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3092 | // Pre-measure and fix height so we can scale later. |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 3093 | holder.measure(); |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 3094 | setViewBounds(viewGroup, LayoutParams.MATCH_PARENT, holder.getMeasuredRowHeight()); |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3095 | |
| 3096 | if (isDirectShare) { |
| 3097 | DirectShareViewHolder dsvh = (DirectShareViewHolder) holder; |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 3098 | setViewBounds(dsvh.getRow(0), LayoutParams.MATCH_PARENT, dsvh.getMinRowHeight()); |
| 3099 | setViewBounds(dsvh.getRow(1), LayoutParams.MATCH_PARENT, dsvh.getMinRowHeight()); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 3100 | } |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3101 | |
| 3102 | viewGroup.setTag(holder); |
| 3103 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 3104 | return holder; |
| 3105 | } |
| 3106 | |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 3107 | private void setViewBounds(View view, int widthPx, int heightPx) { |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3108 | LayoutParams lp = view.getLayoutParams(); |
| 3109 | if (lp == null) { |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 3110 | lp = new LayoutParams(widthPx, heightPx); |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3111 | view.setLayoutParams(lp); |
| 3112 | } else { |
| 3113 | lp.height = heightPx; |
Matt Pietal | ab986b5 | 2019-04-10 10:14:32 -0400 | [diff] [blame] | 3114 | lp.width = widthPx; |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3115 | } |
| 3116 | } |
| 3117 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3118 | RowViewHolder createViewHolder(int viewType, ViewGroup parent) { |
| 3119 | if (viewType == VIEW_TYPE_DIRECT_SHARE) { |
| 3120 | ViewGroup parentGroup = (ViewGroup) mLayoutInflater.inflate( |
| 3121 | R.layout.chooser_row_direct_share, parent, false); |
| 3122 | ViewGroup row1 = (ViewGroup) mLayoutInflater.inflate(R.layout.chooser_row, |
| 3123 | parentGroup, false); |
| 3124 | ViewGroup row2 = (ViewGroup) mLayoutInflater.inflate(R.layout.chooser_row, |
| 3125 | parentGroup, false); |
| 3126 | parentGroup.addView(row1); |
| 3127 | parentGroup.addView(row2); |
| 3128 | |
| 3129 | mDirectShareViewHolder = new DirectShareViewHolder(parentGroup, |
| 3130 | Lists.newArrayList(row1, row2), getMaxTargetsPerRow()); |
| 3131 | loadViewsIntoRow(mDirectShareViewHolder); |
| 3132 | |
| 3133 | return mDirectShareViewHolder; |
| 3134 | } else { |
| 3135 | ViewGroup row = (ViewGroup) mLayoutInflater.inflate(R.layout.chooser_row, parent, |
| 3136 | false); |
| 3137 | RowViewHolder holder = new SingleRowViewHolder(row, getMaxTargetsPerRow()); |
| 3138 | loadViewsIntoRow(holder); |
| 3139 | |
| 3140 | return holder; |
| 3141 | } |
| 3142 | } |
| 3143 | |
Matt Pietal | dadc0d1 | 2019-04-16 12:53:28 -0400 | [diff] [blame] | 3144 | /** |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 3145 | * Need to merge CALLER + ranked STANDARD into a single row and prevent a separator from |
| 3146 | * showing on top of the AZ list if the AZ label is visible. All other types are placed into |
| 3147 | * their own row as determined by their target type, and dividers are added in the list to |
| 3148 | * separate each type. |
Matt Pietal | dadc0d1 | 2019-04-16 12:53:28 -0400 | [diff] [blame] | 3149 | */ |
| 3150 | int getRowType(int rowPosition) { |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 3151 | // Merge caller and ranked standard into a single row |
Matt Pietal | dadc0d1 | 2019-04-16 12:53:28 -0400 | [diff] [blame] | 3152 | int positionType = mChooserListAdapter.getPositionTargetType(rowPosition); |
| 3153 | if (positionType == ChooserListAdapter.TARGET_CALLER) { |
| 3154 | return ChooserListAdapter.TARGET_STANDARD; |
| 3155 | } |
| 3156 | |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 3157 | // If an the A-Z label is shown, prevent a separator from appearing by making the A-Z |
| 3158 | // row type the same as the suggestion row type |
| 3159 | if (getAzLabelRowCount() > 0 && positionType == ChooserListAdapter.TARGET_STANDARD_AZ) { |
| 3160 | return ChooserListAdapter.TARGET_STANDARD; |
| 3161 | } |
| 3162 | |
Matt Pietal | dadc0d1 | 2019-04-16 12:53:28 -0400 | [diff] [blame] | 3163 | return positionType; |
| 3164 | } |
| 3165 | |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 3166 | void bindViewHolder(int rowPosition, RowViewHolder holder) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 3167 | final int start = getFirstRowPosition(rowPosition); |
Matt Pietal | dadc0d1 | 2019-04-16 12:53:28 -0400 | [diff] [blame] | 3168 | final int startType = getRowType(start); |
| 3169 | final int lastStartType = getRowType(getFirstRowPosition(rowPosition - 1)); |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 3170 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3171 | final ViewGroup row = holder.getViewGroup(); |
| 3172 | |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 3173 | if (startType != lastStartType |
| 3174 | || rowPosition == getContentPreviewRowCount() + getProfileRowCount()) { |
Matt Pietal | 84a55bd | 2019-05-01 12:32:47 -0400 | [diff] [blame] | 3175 | row.setForeground( |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 3176 | getResources().getDrawable(R.drawable.chooser_row_layer_list, null)); |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 3177 | } else { |
Matt Pietal | 84a55bd | 2019-05-01 12:32:47 -0400 | [diff] [blame] | 3178 | row.setForeground(null); |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 3179 | } |
| 3180 | |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 3181 | int columnCount = holder.getColumnCount(); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3182 | int end = start + columnCount - 1; |
Matt Pietal | dadc0d1 | 2019-04-16 12:53:28 -0400 | [diff] [blame] | 3183 | while (getRowType(end) != startType && end >= start) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 3184 | end--; |
| 3185 | } |
| 3186 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 3187 | if (end == start && mChooserListAdapter.getItem(start) instanceof EmptyTargetInfo) { |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3188 | final TextView textView = row.findViewById(R.id.chooser_row_text_option); |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 3189 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 3190 | if (textView.getVisibility() != View.VISIBLE) { |
| 3191 | textView.setAlpha(0.0f); |
| 3192 | textView.setVisibility(View.VISIBLE); |
| 3193 | textView.setText(R.string.chooser_no_direct_share_targets); |
| 3194 | |
| 3195 | ValueAnimator fadeAnim = ObjectAnimator.ofFloat(textView, "alpha", 0.0f, 1.0f); |
| 3196 | fadeAnim.setInterpolator(new DecelerateInterpolator(1.0f)); |
| 3197 | |
| 3198 | float translationInPx = getResources().getDimensionPixelSize( |
| 3199 | R.dimen.chooser_row_text_option_translate); |
| 3200 | textView.setTranslationY(translationInPx); |
| 3201 | ValueAnimator translateAnim = ObjectAnimator.ofFloat(textView, "translationY", |
| 3202 | 0.0f); |
| 3203 | translateAnim.setInterpolator(new DecelerateInterpolator(1.0f)); |
| 3204 | |
| 3205 | AnimatorSet animSet = new AnimatorSet(); |
| 3206 | animSet.setDuration(NO_DIRECT_SHARE_ANIM_IN_MILLIS); |
| 3207 | animSet.setStartDelay(NO_DIRECT_SHARE_ANIM_IN_MILLIS); |
| 3208 | animSet.playTogether(fadeAnim, translateAnim); |
| 3209 | animSet.start(); |
| 3210 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 3211 | } |
| 3212 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3213 | for (int i = 0; i < columnCount; i++) { |
| 3214 | final View v = holder.getView(i); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 3215 | if (start + i <= end) { |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 3216 | holder.setViewVisibility(i, View.VISIBLE); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3217 | holder.setItemIndex(i, start + i); |
| 3218 | mChooserListAdapter.bindView(holder.getItemIndex(i), v); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 3219 | } else { |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 3220 | holder.setViewVisibility(i, View.INVISIBLE); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 3221 | } |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | int getFirstRowPosition(int row) { |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 3226 | row -= getContentPreviewRowCount() + getProfileRowCount(); |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 3227 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3228 | final int serviceCount = mChooserListAdapter.getServiceTargetCount(); |
| 3229 | final int serviceRows = (int) Math.ceil((float) serviceCount |
| 3230 | / ChooserListAdapter.MAX_SERVICE_TARGETS); |
| 3231 | if (row < serviceRows) { |
| 3232 | return row * getMaxTargetsPerRow(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 3233 | } |
| 3234 | |
Matt Pietal | dadc0d1 | 2019-04-16 12:53:28 -0400 | [diff] [blame] | 3235 | final int callerAndRankedCount = mChooserListAdapter.getCallerTargetCount() |
| 3236 | + mChooserListAdapter.getRankedTargetCount(); |
| 3237 | final int callerAndRankedRows = getCallerAndRankedTargetRowCount(); |
| 3238 | if (row < callerAndRankedRows + serviceRows) { |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3239 | return serviceCount + (row - serviceRows) * getMaxTargetsPerRow(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 3240 | } |
| 3241 | |
Mike Digman | ae730b1 | 2019-04-25 11:10:31 -0700 | [diff] [blame] | 3242 | row -= getAzLabelRowCount(); |
| 3243 | |
Matt Pietal | dadc0d1 | 2019-04-16 12:53:28 -0400 | [diff] [blame] | 3244 | return callerAndRankedCount + serviceCount |
| 3245 | + (row - callerAndRankedRows - serviceRows) * getMaxTargetsPerRow(); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | public void handleScroll(View v, int y, int oldy) { |
Matt Pietal | e54dcc2e | 2019-05-02 12:59:38 -0400 | [diff] [blame] | 3249 | // Only expand direct share area if there is a minimum number of shortcuts, |
| 3250 | // which will help reduce the amount of visible shuffling due to older-style |
| 3251 | // direct share targets. |
| 3252 | int orientation = getResources().getConfiguration().orientation; |
| 3253 | boolean canExpandDirectShare = |
| 3254 | mChooserListAdapter.getNumShortcutResults() > getMaxTargetsPerRow() |
Matt Pietal | 3e4b56f | 2019-05-31 12:06:17 -0400 | [diff] [blame] | 3255 | && orientation == Configuration.ORIENTATION_PORTRAIT |
| 3256 | && !isInMultiWindowMode(); |
Matt Pietal | e54dcc2e | 2019-05-02 12:59:38 -0400 | [diff] [blame] | 3257 | |
| 3258 | if (mDirectShareViewHolder != null && canExpandDirectShare) { |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3259 | mDirectShareViewHolder.handleScroll(mAdapterView, y, oldy, getMaxTargetsPerRow()); |
| 3260 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 3261 | } |
| 3262 | } |
| 3263 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3264 | abstract class RowViewHolder { |
| 3265 | protected int mMeasuredRowHeight; |
| 3266 | private int[] mItemIndices; |
| 3267 | protected final View[] mCells; |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3268 | private final int mColumnCount; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 3269 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3270 | RowViewHolder(int cellCount) { |
| 3271 | this.mCells = new View[cellCount]; |
| 3272 | this.mItemIndices = new int[cellCount]; |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3273 | this.mColumnCount = cellCount; |
| 3274 | } |
| 3275 | |
| 3276 | abstract ViewGroup addView(int index, View v); |
| 3277 | |
| 3278 | abstract ViewGroup getViewGroup(); |
| 3279 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3280 | abstract ViewGroup getRowByIndex(int index); |
| 3281 | |
| 3282 | abstract ViewGroup getRow(int rowNumber); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3283 | |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 3284 | abstract void setViewVisibility(int i, int visibility); |
| 3285 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3286 | public int getColumnCount() { |
| 3287 | return mColumnCount; |
| 3288 | } |
| 3289 | |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 3290 | public void measure() { |
| 3291 | final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3292 | getViewGroup().measure(spec, spec); |
| 3293 | mMeasuredRowHeight = getViewGroup().getMeasuredHeight(); |
| 3294 | } |
| 3295 | |
| 3296 | public int getMeasuredRowHeight() { |
| 3297 | return mMeasuredRowHeight; |
| 3298 | } |
| 3299 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3300 | public void setItemIndex(int itemIndex, int listIndex) { |
| 3301 | mItemIndices[itemIndex] = listIndex; |
| 3302 | } |
| 3303 | |
| 3304 | public int getItemIndex(int itemIndex) { |
| 3305 | return mItemIndices[itemIndex]; |
| 3306 | } |
| 3307 | |
| 3308 | public View getView(int index) { |
| 3309 | return mCells[index]; |
| 3310 | } |
| 3311 | } |
| 3312 | |
| 3313 | class SingleRowViewHolder extends RowViewHolder { |
| 3314 | private final ViewGroup mRow; |
| 3315 | |
| 3316 | SingleRowViewHolder(ViewGroup row, int cellCount) { |
| 3317 | super(cellCount); |
| 3318 | |
| 3319 | this.mRow = row; |
| 3320 | } |
| 3321 | |
| 3322 | public ViewGroup getViewGroup() { |
| 3323 | return mRow; |
| 3324 | } |
| 3325 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3326 | public ViewGroup getRowByIndex(int index) { |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3327 | return mRow; |
| 3328 | } |
| 3329 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3330 | public ViewGroup getRow(int rowNumber) { |
| 3331 | if (rowNumber == 0) return mRow; |
| 3332 | return null; |
| 3333 | } |
| 3334 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3335 | public ViewGroup addView(int index, View v) { |
| 3336 | mRow.addView(v); |
| 3337 | mCells[index] = v; |
| 3338 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3339 | return mRow; |
| 3340 | } |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 3341 | |
| 3342 | public void setViewVisibility(int i, int visibility) { |
| 3343 | getView(i).setVisibility(visibility); |
| 3344 | } |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | class DirectShareViewHolder extends RowViewHolder { |
| 3348 | private final ViewGroup mParent; |
| 3349 | private final List<ViewGroup> mRows; |
| 3350 | private int mCellCountPerRow; |
| 3351 | |
| 3352 | private boolean mHideDirectShareExpansion = false; |
| 3353 | private int mDirectShareMinHeight = 0; |
| 3354 | private int mDirectShareCurrHeight = 0; |
| 3355 | private int mDirectShareMaxHeight = 0; |
| 3356 | |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 3357 | private final boolean[] mCellVisibility; |
| 3358 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3359 | DirectShareViewHolder(ViewGroup parent, List<ViewGroup> rows, int cellCountPerRow) { |
| 3360 | super(rows.size() * cellCountPerRow); |
| 3361 | |
| 3362 | this.mParent = parent; |
| 3363 | this.mRows = rows; |
| 3364 | this.mCellCountPerRow = cellCountPerRow; |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 3365 | this.mCellVisibility = new boolean[rows.size() * cellCountPerRow]; |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3366 | } |
| 3367 | |
| 3368 | public ViewGroup addView(int index, View v) { |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3369 | ViewGroup row = getRowByIndex(index); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3370 | row.addView(v); |
| 3371 | mCells[index] = v; |
| 3372 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3373 | return row; |
| 3374 | } |
| 3375 | |
| 3376 | public ViewGroup getViewGroup() { |
| 3377 | return mParent; |
| 3378 | } |
| 3379 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3380 | public ViewGroup getRowByIndex(int index) { |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3381 | return mRows.get(index / mCellCountPerRow); |
| 3382 | } |
| 3383 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 3384 | public ViewGroup getRow(int rowNumber) { |
| 3385 | return mRows.get(rowNumber); |
| 3386 | } |
| 3387 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3388 | public void measure() { |
| 3389 | final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 3390 | getRow(0).measure(spec, spec); |
| 3391 | getRow(1).measure(spec, spec); |
| 3392 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3393 | mDirectShareMinHeight = getRow(0).getMeasuredHeight(); |
| 3394 | mDirectShareCurrHeight = mDirectShareCurrHeight > 0 |
| 3395 | ? mDirectShareCurrHeight : mDirectShareMinHeight; |
| 3396 | mDirectShareMaxHeight = 2 * mDirectShareMinHeight; |
| 3397 | } |
| 3398 | |
| 3399 | public int getMeasuredRowHeight() { |
| 3400 | return mDirectShareCurrHeight; |
| 3401 | } |
| 3402 | |
Matt Pietal | a9c8e50 | 2019-04-10 14:27:35 -0400 | [diff] [blame] | 3403 | public int getMinRowHeight() { |
| 3404 | return mDirectShareMinHeight; |
| 3405 | } |
| 3406 | |
Matt Pietal | faedea8 | 2019-03-21 10:36:54 -0400 | [diff] [blame] | 3407 | public void setViewVisibility(int i, int visibility) { |
| 3408 | final View v = getView(i); |
| 3409 | if (visibility == View.VISIBLE) { |
| 3410 | mCellVisibility[i] = true; |
| 3411 | v.setVisibility(visibility); |
| 3412 | v.setAlpha(1.0f); |
| 3413 | } else if (visibility == View.INVISIBLE && mCellVisibility[i]) { |
| 3414 | mCellVisibility[i] = false; |
| 3415 | |
| 3416 | ValueAnimator fadeAnim = ObjectAnimator.ofFloat(v, "alpha", 1.0f, 0f); |
| 3417 | fadeAnim.setDuration(NO_DIRECT_SHARE_ANIM_IN_MILLIS); |
| 3418 | fadeAnim.setInterpolator(new AccelerateInterpolator(1.0f)); |
| 3419 | fadeAnim.addListener(new AnimatorListenerAdapter() { |
| 3420 | public void onAnimationEnd(Animator animation) { |
| 3421 | v.setVisibility(View.INVISIBLE); |
| 3422 | } |
| 3423 | }); |
| 3424 | fadeAnim.start(); |
| 3425 | } |
| 3426 | } |
| 3427 | |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3428 | public void handleScroll(AbsListView view, int y, int oldy, int maxTargetsPerRow) { |
Matt Pietal | a9c8e50 | 2019-04-10 14:27:35 -0400 | [diff] [blame] | 3429 | // only exit early if fully collapsed, otherwise onListRebuilt() with shifting |
| 3430 | // targets can lock us into an expanded mode |
| 3431 | boolean notExpanded = mDirectShareCurrHeight == mDirectShareMinHeight; |
| 3432 | if (notExpanded) { |
| 3433 | if (mHideDirectShareExpansion) { |
| 3434 | return; |
| 3435 | } |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3436 | |
Matt Pietal | a9c8e50 | 2019-04-10 14:27:35 -0400 | [diff] [blame] | 3437 | // only expand if we have more than maxTargetsPerRow, and delay that decision |
| 3438 | // until they start to scroll |
| 3439 | if (mChooserListAdapter.getSelectableServiceTargetCount() <= maxTargetsPerRow) { |
| 3440 | mHideDirectShareExpansion = true; |
| 3441 | return; |
| 3442 | } |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3443 | } |
| 3444 | |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 3445 | int yDiff = (int) ((oldy - y) * DIRECT_SHARE_EXPANSION_RATE); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3446 | |
| 3447 | int prevHeight = mDirectShareCurrHeight; |
Matt Pietal | c6d3ac2 | 2019-04-25 14:38:30 -0400 | [diff] [blame] | 3448 | int newHeight = Math.min(prevHeight + yDiff, mDirectShareMaxHeight); |
| 3449 | newHeight = Math.max(newHeight, mDirectShareMinHeight); |
| 3450 | yDiff = newHeight - prevHeight; |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3451 | |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 3452 | if (view == null || view.getChildCount() == 0 || yDiff == 0) { |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3453 | return; |
| 3454 | } |
| 3455 | |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 3456 | // locate the item to expand, and offset the rows below that one |
| 3457 | boolean foundExpansion = false; |
| 3458 | for (int i = 0; i < view.getChildCount(); i++) { |
| 3459 | View child = view.getChildAt(i); |
Matt Pietal | 1ef8800 | 2019-03-13 10:43:18 -0400 | [diff] [blame] | 3460 | |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 3461 | if (foundExpansion) { |
| 3462 | child.offsetTopAndBottom(yDiff); |
| 3463 | } else { |
| 3464 | if (child.getTag() != null && child.getTag() instanceof DirectShareViewHolder) { |
| 3465 | int widthSpec = MeasureSpec.makeMeasureSpec(child.getWidth(), |
| 3466 | MeasureSpec.EXACTLY); |
Matt Pietal | c6d3ac2 | 2019-04-25 14:38:30 -0400 | [diff] [blame] | 3467 | int heightSpec = MeasureSpec.makeMeasureSpec(newHeight, |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 3468 | MeasureSpec.EXACTLY); |
| 3469 | child.measure(widthSpec, heightSpec); |
| 3470 | child.getLayoutParams().height = child.getMeasuredHeight(); |
| 3471 | child.layout(child.getLeft(), child.getTop(), child.getRight(), |
| 3472 | child.getTop() + child.getMeasuredHeight()); |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3473 | |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 3474 | foundExpansion = true; |
| 3475 | } |
| 3476 | } |
Matt Pietal | 5b64856 | 2019-03-12 07:40:26 -0400 | [diff] [blame] | 3477 | } |
Matt Pietal | c6d3ac2 | 2019-04-25 14:38:30 -0400 | [diff] [blame] | 3478 | |
| 3479 | if (foundExpansion) { |
| 3480 | mDirectShareCurrHeight = newHeight; |
| 3481 | } |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 3482 | } |
| 3483 | } |
| 3484 | |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 3485 | static class ChooserTargetServiceConnection implements ServiceConnection { |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 3486 | private DisplayResolveInfo mOriginalTarget; |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 3487 | private ComponentName mConnectedComponent; |
| 3488 | private ChooserActivity mChooserActivity; |
| 3489 | private final Object mLock = new Object(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 3490 | |
| 3491 | private final IChooserTargetResult mChooserTargetResult = new IChooserTargetResult.Stub() { |
| 3492 | @Override |
| 3493 | public void sendResult(List<ChooserTarget> targets) throws RemoteException { |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 3494 | synchronized (mLock) { |
| 3495 | if (mChooserActivity == null) { |
| 3496 | Log.e(TAG, "destroyed ChooserTargetServiceConnection received result from " |
| 3497 | + mConnectedComponent + "; ignoring..."); |
| 3498 | return; |
| 3499 | } |
| 3500 | mChooserActivity.filterServiceTargets( |
| 3501 | mOriginalTarget.getResolveInfo().activityInfo.packageName, targets); |
| 3502 | final Message msg = Message.obtain(); |
Matt Pietal | ab73a88 | 2019-06-05 07:04:55 -0400 | [diff] [blame] | 3503 | msg.what = ChooserHandler.CHOOSER_TARGET_SERVICE_RESULT; |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 3504 | msg.obj = new ServiceResultInfo(mOriginalTarget, targets, |
| 3505 | ChooserTargetServiceConnection.this); |
| 3506 | mChooserActivity.mChooserHandler.sendMessage(msg); |
| 3507 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 3508 | } |
| 3509 | }; |
| 3510 | |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 3511 | public ChooserTargetServiceConnection(ChooserActivity chooserActivity, |
| 3512 | DisplayResolveInfo dri) { |
| 3513 | mChooserActivity = chooserActivity; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 3514 | mOriginalTarget = dri; |
| 3515 | } |
| 3516 | |
| 3517 | @Override |
| 3518 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 3519 | if (DEBUG) Log.d(TAG, "onServiceConnected: " + name); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 3520 | synchronized (mLock) { |
| 3521 | if (mChooserActivity == null) { |
| 3522 | Log.e(TAG, "destroyed ChooserTargetServiceConnection got onServiceConnected"); |
| 3523 | return; |
| 3524 | } |
| 3525 | |
| 3526 | final IChooserTargetService icts = IChooserTargetService.Stub.asInterface(service); |
| 3527 | try { |
| 3528 | icts.getChooserTargets(mOriginalTarget.getResolvedComponentName(), |
| 3529 | mOriginalTarget.getResolveInfo().filter, mChooserTargetResult); |
| 3530 | } catch (RemoteException e) { |
| 3531 | Log.e(TAG, "Querying ChooserTargetService " + name + " failed.", e); |
| 3532 | mChooserActivity.unbindService(this); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 3533 | mChooserActivity.mServiceConnections.remove(this); |
Dan Sandler | fcd7fae | 2017-09-25 17:40:04 -0400 | [diff] [blame] | 3534 | destroy(); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 3535 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | @Override |
| 3540 | public void onServiceDisconnected(ComponentName name) { |
| 3541 | if (DEBUG) Log.d(TAG, "onServiceDisconnected: " + name); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 3542 | synchronized (mLock) { |
| 3543 | if (mChooserActivity == null) { |
| 3544 | Log.e(TAG, |
| 3545 | "destroyed ChooserTargetServiceConnection got onServiceDisconnected"); |
| 3546 | return; |
| 3547 | } |
| 3548 | |
| 3549 | mChooserActivity.unbindService(this); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 3550 | mChooserActivity.mServiceConnections.remove(this); |
| 3551 | if (mChooserActivity.mServiceConnections.isEmpty()) { |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 3552 | mChooserActivity.sendVoiceChoicesIfNeeded(); |
| 3553 | } |
| 3554 | mConnectedComponent = null; |
Dan Sandler | fcd7fae | 2017-09-25 17:40:04 -0400 | [diff] [blame] | 3555 | destroy(); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 3556 | } |
| 3557 | } |
| 3558 | |
| 3559 | public void destroy() { |
| 3560 | synchronized (mLock) { |
| 3561 | mChooserActivity = null; |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 3562 | mOriginalTarget = null; |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 3563 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | @Override |
| 3567 | public String toString() { |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 3568 | return "ChooserTargetServiceConnection{service=" |
| 3569 | + mConnectedComponent + ", activity=" |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 3570 | + (mOriginalTarget != null |
| 3571 | ? mOriginalTarget.getResolveInfo().activityInfo.toString() |
| 3572 | : "<connection destroyed>") + "}"; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 3573 | } |
| 3574 | } |
| 3575 | |
| 3576 | static class ServiceResultInfo { |
| 3577 | public final DisplayResolveInfo originalTarget; |
| 3578 | public final List<ChooserTarget> resultTargets; |
| 3579 | public final ChooserTargetServiceConnection connection; |
| 3580 | |
| 3581 | public ServiceResultInfo(DisplayResolveInfo ot, List<ChooserTarget> rt, |
| 3582 | ChooserTargetServiceConnection c) { |
| 3583 | originalTarget = ot; |
| 3584 | resultTargets = rt; |
| 3585 | connection = c; |
| 3586 | } |
| 3587 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 3588 | |
| 3589 | static class RefinementResultReceiver extends ResultReceiver { |
| 3590 | private ChooserActivity mChooserActivity; |
| 3591 | private TargetInfo mSelectedTarget; |
| 3592 | |
| 3593 | public RefinementResultReceiver(ChooserActivity host, TargetInfo target, |
| 3594 | Handler handler) { |
| 3595 | super(handler); |
| 3596 | mChooserActivity = host; |
| 3597 | mSelectedTarget = target; |
| 3598 | } |
| 3599 | |
| 3600 | @Override |
| 3601 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 3602 | if (mChooserActivity == null) { |
| 3603 | Log.e(TAG, "Destroyed RefinementResultReceiver received a result"); |
| 3604 | return; |
| 3605 | } |
| 3606 | if (resultData == null) { |
| 3607 | Log.e(TAG, "RefinementResultReceiver received null resultData"); |
| 3608 | return; |
| 3609 | } |
| 3610 | |
| 3611 | switch (resultCode) { |
| 3612 | case RESULT_CANCELED: |
| 3613 | mChooserActivity.onRefinementCanceled(); |
| 3614 | break; |
| 3615 | case RESULT_OK: |
| 3616 | Parcelable intentParcelable = resultData.getParcelable(Intent.EXTRA_INTENT); |
| 3617 | if (intentParcelable instanceof Intent) { |
| 3618 | mChooserActivity.onRefinementResult(mSelectedTarget, |
| 3619 | (Intent) intentParcelable); |
| 3620 | } else { |
| 3621 | Log.e(TAG, "RefinementResultReceiver received RESULT_OK but no Intent" |
| 3622 | + " in resultData with key Intent.EXTRA_INTENT"); |
| 3623 | } |
| 3624 | break; |
| 3625 | default: |
| 3626 | Log.w(TAG, "Unknown result code " + resultCode |
| 3627 | + " sent to RefinementResultReceiver"); |
| 3628 | break; |
| 3629 | } |
| 3630 | } |
| 3631 | |
| 3632 | public void destroy() { |
| 3633 | mChooserActivity = null; |
| 3634 | mSelectedTarget = null; |
| 3635 | } |
| 3636 | } |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 3637 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 3638 | /** |
| 3639 | * Used internally to round image corners while obeying view padding. |
| 3640 | */ |
| 3641 | public static class RoundedRectImageView extends ImageView { |
| 3642 | private int mRadius = 0; |
| 3643 | private Path mPath = new Path(); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 3644 | private Paint mOverlayPaint = new Paint(0); |
Matt Pietal | 9aaf00c | 2019-04-09 10:09:12 -0400 | [diff] [blame] | 3645 | private Paint mRoundRectPaint = new Paint(0); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 3646 | private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 3647 | private String mExtraImageCount = null; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 3648 | |
| 3649 | public RoundedRectImageView(Context context) { |
| 3650 | super(context); |
| 3651 | } |
| 3652 | |
| 3653 | public RoundedRectImageView(Context context, AttributeSet attrs) { |
| 3654 | this(context, attrs, 0); |
| 3655 | } |
| 3656 | |
| 3657 | public RoundedRectImageView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 3658 | this(context, attrs, defStyleAttr, 0); |
| 3659 | } |
| 3660 | |
| 3661 | public RoundedRectImageView(Context context, AttributeSet attrs, int defStyleAttr, |
| 3662 | int defStyleRes) { |
| 3663 | super(context, attrs, defStyleAttr, defStyleRes); |
| 3664 | mRadius = context.getResources().getDimensionPixelSize(R.dimen.chooser_corner_radius); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 3665 | |
| 3666 | mOverlayPaint.setColor(0x99000000); |
| 3667 | mOverlayPaint.setStyle(Paint.Style.FILL); |
| 3668 | |
Matt Pietal | 9aaf00c | 2019-04-09 10:09:12 -0400 | [diff] [blame] | 3669 | mRoundRectPaint.setColor(context.getResources().getColor(R.color.chooser_row_divider)); |
| 3670 | mRoundRectPaint.setStyle(Paint.Style.STROKE); |
| 3671 | mRoundRectPaint.setStrokeWidth(context.getResources() |
| 3672 | .getDimensionPixelSize(R.dimen.chooser_preview_image_border)); |
| 3673 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 3674 | mTextPaint.setColor(Color.WHITE); |
| 3675 | mTextPaint.setTextSize(context.getResources() |
| 3676 | .getDimensionPixelSize(R.dimen.chooser_preview_image_font_size)); |
| 3677 | mTextPaint.setTextAlign(Paint.Align.CENTER); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | private void updatePath(int width, int height) { |
| 3681 | mPath.reset(); |
| 3682 | |
Matt Pietal | 9aaf00c | 2019-04-09 10:09:12 -0400 | [diff] [blame] | 3683 | int imageWidth = width - getPaddingRight() - getPaddingLeft(); |
| 3684 | int imageHeight = height - getPaddingBottom() - getPaddingTop(); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 3685 | mPath.addRoundRect(getPaddingLeft(), getPaddingTop(), imageWidth, imageHeight, mRadius, |
| 3686 | mRadius, Path.Direction.CW); |
| 3687 | } |
| 3688 | |
| 3689 | /** |
| 3690 | * Sets the corner radius on all corners |
| 3691 | * |
| 3692 | * param radius 0 for no radius, > 0 for a visible corner radius |
| 3693 | */ |
| 3694 | public void setRadius(int radius) { |
| 3695 | mRadius = radius; |
| 3696 | updatePath(getWidth(), getHeight()); |
| 3697 | } |
| 3698 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 3699 | /** |
| 3700 | * Display an overlay with extra image count on 3rd image |
| 3701 | */ |
| 3702 | public void setExtraImageCount(int count) { |
| 3703 | if (count > 0) { |
| 3704 | this.mExtraImageCount = "+" + count; |
| 3705 | } else { |
| 3706 | this.mExtraImageCount = null; |
| 3707 | } |
| 3708 | } |
| 3709 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 3710 | @Override |
| 3711 | protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) { |
| 3712 | super.onSizeChanged(width, height, oldWidth, oldHeight); |
| 3713 | updatePath(width, height); |
| 3714 | } |
| 3715 | |
| 3716 | @Override |
| 3717 | protected void onDraw(Canvas canvas) { |
| 3718 | if (mRadius != 0) { |
| 3719 | canvas.clipPath(mPath); |
| 3720 | } |
| 3721 | |
| 3722 | super.onDraw(canvas); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 3723 | |
Matt Pietal | 9aaf00c | 2019-04-09 10:09:12 -0400 | [diff] [blame] | 3724 | int x = getPaddingLeft(); |
| 3725 | int y = getPaddingRight(); |
| 3726 | int width = getWidth() - getPaddingRight() - getPaddingLeft(); |
| 3727 | int height = getHeight() - getPaddingBottom() - getPaddingTop(); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 3728 | if (mExtraImageCount != null) { |
Matt Pietal | 9aaf00c | 2019-04-09 10:09:12 -0400 | [diff] [blame] | 3729 | canvas.drawRect(x, y, width, height, mOverlayPaint); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 3730 | |
| 3731 | int xPos = canvas.getWidth() / 2; |
| 3732 | int yPos = (int) ((canvas.getHeight() / 2.0f) |
| 3733 | - ((mTextPaint.descent() + mTextPaint.ascent()) / 2.0f)); |
| 3734 | |
| 3735 | canvas.drawText(mExtraImageCount, xPos, yPos, mTextPaint); |
| 3736 | } |
Matt Pietal | 9aaf00c | 2019-04-09 10:09:12 -0400 | [diff] [blame] | 3737 | |
| 3738 | canvas.drawRoundRect(x, y, width, height, mRadius, mRadius, mRoundRectPaint); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 3739 | } |
| 3740 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3741 | } |