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