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