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; |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 27 | import android.app.Activity; |
Ng Zhi An | d3ec5fc | 2018-05-10 09:13:00 -0700 | [diff] [blame] | 28 | import android.app.ActivityManager; |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 29 | import android.app.prediction.AppPredictionContext; |
| 30 | import android.app.prediction.AppPredictionManager; |
| 31 | import android.app.prediction.AppPredictor; |
| 32 | import android.app.prediction.AppTarget; |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 33 | import android.app.prediction.AppTargetEvent; |
| 34 | import android.app.prediction.AppTargetId; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 35 | import android.content.ClipData; |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 36 | import android.content.ClipboardManager; |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 37 | import android.content.ComponentName; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 38 | import android.content.ContentResolver; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 39 | import android.content.Context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.content.Intent; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 41 | import android.content.IntentFilter; |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 42 | import android.content.IntentSender; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 43 | import android.content.IntentSender.SendIntentException; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 44 | import android.content.ServiceConnection; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 45 | import android.content.SharedPreferences; |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 46 | import android.content.pm.ActivityInfo; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 47 | import android.content.pm.LabeledIntent; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 48 | import android.content.pm.LauncherApps; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 49 | import android.content.pm.PackageManager; |
| 50 | import android.content.pm.PackageManager.NameNotFoundException; |
| 51 | import android.content.pm.ResolveInfo; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 52 | import android.content.pm.ShortcutInfo; |
| 53 | import android.content.pm.ShortcutManager; |
Matt Pietal | 18bbd82 | 2019-02-12 15:21:36 -0500 | [diff] [blame] | 54 | import android.content.res.Configuration; |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 55 | import android.database.Cursor; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 56 | import android.database.DataSetObserver; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 57 | import android.graphics.Bitmap; |
| 58 | import android.graphics.Canvas; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 59 | import android.graphics.Color; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 60 | import android.graphics.Paint; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 61 | import android.graphics.Path; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 62 | import android.graphics.drawable.Drawable; |
Adam Powell | 13036be | 2015-05-12 14:43:56 -0700 | [diff] [blame] | 63 | import android.graphics.drawable.Icon; |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 64 | import android.metrics.LogMaker; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 65 | import android.net.Uri; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 66 | import android.os.AsyncTask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.os.Bundle; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 68 | import android.os.Environment; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 69 | import android.os.Handler; |
| 70 | import android.os.IBinder; |
| 71 | import android.os.Message; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 72 | import android.os.Parcelable; |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 73 | import android.os.Process; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 74 | import android.os.RemoteException; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 75 | import android.os.ResultReceiver; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 76 | import android.os.UserHandle; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 77 | import android.os.UserManager; |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 78 | import android.os.storage.StorageManager; |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 79 | import android.provider.DocumentsContract; |
Matt Pietal | f38e9d2 | 2019-02-15 10:01:03 -0500 | [diff] [blame] | 80 | import android.provider.Downloads; |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 81 | import android.provider.OpenableColumns; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 82 | import android.service.chooser.ChooserTarget; |
| 83 | import android.service.chooser.ChooserTargetService; |
| 84 | import android.service.chooser.IChooserTargetResult; |
| 85 | import android.service.chooser.IChooserTargetService; |
| 86 | import android.text.TextUtils; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 87 | import android.util.AttributeSet; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 88 | import android.util.Log; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 89 | import android.util.Size; |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 90 | import android.util.Slog; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 91 | import android.view.LayoutInflater; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 92 | import android.view.View; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 93 | import android.view.View.MeasureSpec; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 94 | import android.view.View.OnClickListener; |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 95 | import android.view.View.OnLongClickListener; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 96 | import android.view.ViewGroup; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 97 | import android.view.ViewGroup.LayoutParams; |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 98 | import android.view.animation.AccelerateInterpolator; |
| 99 | import android.view.animation.DecelerateInterpolator; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 100 | import android.widget.AbsListView; |
| 101 | import android.widget.BaseAdapter; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 102 | import android.widget.ImageView; |
Jason Monk | 027dcfa | 2017-06-27 18:37:35 -0400 | [diff] [blame] | 103 | import android.widget.LinearLayout; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 104 | import android.widget.ListView; |
Jason Monk | 027dcfa | 2017-06-27 18:37:35 -0400 | [diff] [blame] | 105 | import android.widget.Space; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 106 | import android.widget.TextView; |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 107 | import android.widget.Toast; |
Jason Monk | 027dcfa | 2017-06-27 18:37:35 -0400 | [diff] [blame] | 108 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 109 | import com.android.internal.R; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 110 | import com.android.internal.annotations.VisibleForTesting; |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 111 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 112 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 113 | import com.android.internal.util.ImageUtils; |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 114 | |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 115 | import com.google.android.collect.Lists; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 116 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 117 | import java.io.File; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 118 | import java.io.IOException; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 119 | import java.lang.annotation.Retention; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 120 | import java.util.ArrayList; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 121 | import java.util.Arrays; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 122 | import java.util.Collections; |
| 123 | import java.util.Comparator; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 124 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | |
| 126 | public class ChooserActivity extends ResolverActivity { |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 127 | private static final String TAG = "ChooserActivity"; |
| 128 | |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 129 | /** |
| 130 | * Boolean extra to change the following behavior: Normally, ChooserActivity finishes itself |
| 131 | * in onStop when launched in a new task. If this extra is set to true, we do not finish |
| 132 | * ourselves when onStop gets called. |
| 133 | */ |
| 134 | public static final String EXTRA_PRIVATE_RETAIN_IN_ON_STOP |
| 135 | = "com.android.internal.app.ChooserActivity.EXTRA_PRIVATE_RETAIN_IN_ON_STOP"; |
| 136 | |
Mehdi Alizadeh | 3c335a2 | 2019-01-17 16:03:19 -0800 | [diff] [blame] | 137 | private static final boolean DEBUG = false; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 138 | |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * If {@link #USE_SHORTCUT_MANAGER_FOR_DIRECT_TARGETS} and this is set to true, |
| 142 | * {@link AppPredictionManager} will be queried for direct share targets. |
| 143 | */ |
| 144 | // TODO(b/123089490): Replace with system flag |
George Hodulik | 2bd5881 | 2019-03-06 19:22:10 +0000 | [diff] [blame] | 145 | private static final boolean USE_PREDICTION_MANAGER_FOR_DIRECT_TARGETS = false; |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 146 | // TODO(b/123088566) Share these in a better way. |
| 147 | private static final String APP_PREDICTION_SHARE_UI_SURFACE = "share"; |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 148 | public static final String LAUNCH_LOCATON_DIRECT_SHARE = "direct_share"; |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 149 | private static final int APP_PREDICTION_SHARE_TARGET_QUERY_PACKAGE_LIMIT = 20; |
| 150 | public static final String APP_PREDICTION_INTENT_FILTER_KEY = "intent_filter"; |
| 151 | private AppPredictor mAppPredictor; |
| 152 | private AppPredictor.Callback mAppPredictorCallback; |
| 153 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 154 | /** |
| 155 | * If set to true, use ShortcutManager to retrieve the matching direct share targets, instead of |
| 156 | * binding to every ChooserTargetService implementation. |
| 157 | */ |
| 158 | // TODO(b/121287573): Replace with a system flag (setprop?) |
Mehdi Alizadeh | 8e248ad | 2019-01-17 11:41:49 -0800 | [diff] [blame] | 159 | private static final boolean USE_SHORTCUT_MANAGER_FOR_DIRECT_TARGETS = true; |
| 160 | private static final boolean USE_CHOOSER_TARGET_SERVICE_FOR_DIRECT_TARGETS = true; |
| 161 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 162 | /** |
| 163 | * The transition time between placeholders for direct share to a message |
| 164 | * indicating that non are available. |
| 165 | */ |
| 166 | private static final int NO_DIRECT_SHARE_ANIM_IN_MILLIS = 200; |
| 167 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 168 | // TODO(b/121287224): Re-evaluate this limit |
| 169 | private static final int SHARE_TARGET_QUERY_PACKAGE_LIMIT = 20; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 170 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 171 | private static final int QUERY_TARGET_SERVICE_LIMIT = 5; |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 172 | private static final int WATCHDOG_TIMEOUT_MILLIS = 3000; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 173 | |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 174 | private Bundle mReplacementExtras; |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 175 | private IntentSender mChosenComponentSender; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 176 | private IntentSender mRefinementIntentSender; |
| 177 | private RefinementResultReceiver mRefinementResultReceiver; |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 178 | private ChooserTarget[] mCallerChooserTargets; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 179 | private ComponentName[] mFilteredComponentNames; |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 180 | |
Adam Powell | 13036be | 2015-05-12 14:43:56 -0700 | [diff] [blame] | 181 | private Intent mReferrerFillInIntent; |
| 182 | |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 183 | private long mChooserShownTime; |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 184 | protected boolean mIsSuccessfullySelected; |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 185 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 186 | private ChooserListAdapter mChooserListAdapter; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 187 | private ChooserRowAdapter mChooserRowAdapter; |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 188 | private Drawable mChooserRowLayer; |
| 189 | private int mChooserRowServiceSpacing; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 190 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 191 | private SharedPreferences mPinnedSharedPrefs; |
| 192 | private static final float PINNED_TARGET_SCORE_BOOST = 1000.f; |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 193 | private static final float CALLER_TARGET_SCORE_BOOST = 900.f; |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 194 | private static final String PINNED_SHARED_PREFS_NAME = "chooser_pin_settings"; |
| 195 | private static final String TARGET_DETAILS_FRAGMENT_TAG = "targetDetailsFragment"; |
| 196 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 197 | private final List<ChooserTargetServiceConnection> mServiceConnections = new ArrayList<>(); |
| 198 | |
| 199 | private static final int CHOOSER_TARGET_SERVICE_RESULT = 1; |
| 200 | private static final int CHOOSER_TARGET_SERVICE_WATCHDOG_TIMEOUT = 2; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 201 | private static final int SHORTCUT_MANAGER_SHARE_TARGET_RESULT = 3; |
Mehdi Alizadeh | 8e248ad | 2019-01-17 11:41:49 -0800 | [diff] [blame] | 202 | private static final int SHORTCUT_MANAGER_SHARE_TARGET_RESULT_COMPLETED = 4; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 203 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 204 | @Retention(SOURCE) |
| 205 | @IntDef({CONTENT_PREVIEW_FILE, CONTENT_PREVIEW_IMAGE, CONTENT_PREVIEW_TEXT}) |
| 206 | private @interface ContentPreviewType { |
| 207 | } |
| 208 | |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 209 | // Starting at 1 since 0 is considered "undefined" for some of the database transformations |
| 210 | // of tron logs. |
| 211 | private static final int CONTENT_PREVIEW_IMAGE = 1; |
| 212 | private static final int CONTENT_PREVIEW_FILE = 2; |
| 213 | private static final int CONTENT_PREVIEW_TEXT = 3; |
| 214 | protected MetricsLogger mMetricsLogger; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 215 | |
Adam Powell | 13036be | 2015-05-12 14:43:56 -0700 | [diff] [blame] | 216 | private final Handler mChooserHandler = new Handler() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 217 | @Override |
| 218 | public void handleMessage(Message msg) { |
| 219 | switch (msg.what) { |
| 220 | case CHOOSER_TARGET_SERVICE_RESULT: |
| 221 | if (DEBUG) Log.d(TAG, "CHOOSER_TARGET_SERVICE_RESULT"); |
| 222 | if (isDestroyed()) break; |
| 223 | final ServiceResultInfo sri = (ServiceResultInfo) msg.obj; |
| 224 | if (!mServiceConnections.contains(sri.connection)) { |
| 225 | Log.w(TAG, "ChooserTargetServiceConnection " + sri.connection |
| 226 | + " returned after being removed from active connections." |
| 227 | + " Have you considered returning results faster?"); |
| 228 | break; |
| 229 | } |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 230 | if (sri.resultTargets != null) { |
| 231 | mChooserListAdapter.addServiceResults(sri.originalTarget, |
| 232 | sri.resultTargets); |
| 233 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 234 | unbindService(sri.connection); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 235 | sri.connection.destroy(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 236 | mServiceConnections.remove(sri.connection); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 237 | if (mServiceConnections.isEmpty()) { |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 238 | sendVoiceChoicesIfNeeded(); |
Adam Powell | 565943f | 2016-02-11 10:29:05 -0800 | [diff] [blame] | 239 | mChooserListAdapter.setShowServiceTargets(true); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 240 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 241 | break; |
| 242 | |
| 243 | case CHOOSER_TARGET_SERVICE_WATCHDOG_TIMEOUT: |
| 244 | if (DEBUG) { |
| 245 | Log.d(TAG, "CHOOSER_TARGET_SERVICE_WATCHDOG_TIMEOUT; unbinding services"); |
| 246 | } |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 247 | if (isDestroyed()) { |
| 248 | break; |
| 249 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 250 | unbindRemainingServices(); |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 251 | sendVoiceChoicesIfNeeded(); |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 252 | mChooserListAdapter.completeServiceTargetLoading(); |
Adam Powell | 565943f | 2016-02-11 10:29:05 -0800 | [diff] [blame] | 253 | mChooserListAdapter.setShowServiceTargets(true); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 254 | break; |
| 255 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 256 | case SHORTCUT_MANAGER_SHARE_TARGET_RESULT: |
| 257 | if (DEBUG) Log.d(TAG, "SHORTCUT_MANAGER_SHARE_TARGET_RESULT"); |
| 258 | if (isDestroyed()) break; |
| 259 | final ServiceResultInfo resultInfo = (ServiceResultInfo) msg.obj; |
| 260 | if (resultInfo.resultTargets != null) { |
| 261 | mChooserListAdapter.addServiceResults(resultInfo.originalTarget, |
| 262 | resultInfo.resultTargets); |
| 263 | } |
Mehdi Alizadeh | 8e248ad | 2019-01-17 11:41:49 -0800 | [diff] [blame] | 264 | break; |
| 265 | |
| 266 | case SHORTCUT_MANAGER_SHARE_TARGET_RESULT_COMPLETED: |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 267 | sendVoiceChoicesIfNeeded(); |
| 268 | mChooserListAdapter.setShowServiceTargets(true); |
| 269 | break; |
| 270 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 271 | default: |
| 272 | super.handleMessage(msg); |
| 273 | } |
| 274 | } |
| 275 | }; |
| 276 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | @Override |
| 278 | protected void onCreate(Bundle savedInstanceState) { |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 279 | final long intentReceivedTime = System.currentTimeMillis(); |
| 280 | mIsSuccessfullySelected = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | Intent intent = getIntent(); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 282 | Parcelable targetParcelable = intent.getParcelableExtra(Intent.EXTRA_INTENT); |
| 283 | if (!(targetParcelable instanceof Intent)) { |
Christopher Tate | 9d6376a | 2014-02-12 13:14:10 -0800 | [diff] [blame] | 284 | Log.w("ChooserActivity", "Target is not an intent: " + targetParcelable); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 285 | finish(); |
Christopher Tate | 9d6376a | 2014-02-12 13:14:10 -0800 | [diff] [blame] | 286 | super.onCreate(null); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 287 | return; |
| 288 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 289 | Intent target = (Intent) targetParcelable; |
Craig Mautner | 411d2aed | 2014-05-08 09:07:43 -0700 | [diff] [blame] | 290 | if (target != null) { |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 291 | modifyTargetIntent(target); |
Craig Mautner | 411d2aed | 2014-05-08 09:07:43 -0700 | [diff] [blame] | 292 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 293 | Parcelable[] targetsParcelable |
| 294 | = intent.getParcelableArrayExtra(Intent.EXTRA_ALTERNATE_INTENTS); |
| 295 | if (targetsParcelable != null) { |
| 296 | final boolean offset = target == null; |
| 297 | Intent[] additionalTargets = |
| 298 | new Intent[offset ? targetsParcelable.length - 1 : targetsParcelable.length]; |
| 299 | for (int i = 0; i < targetsParcelable.length; i++) { |
| 300 | if (!(targetsParcelable[i] instanceof Intent)) { |
| 301 | Log.w(TAG, "EXTRA_ALTERNATE_INTENTS array entry #" + i + " is not an Intent: " |
| 302 | + targetsParcelable[i]); |
| 303 | finish(); |
| 304 | super.onCreate(null); |
| 305 | return; |
| 306 | } |
| 307 | final Intent additionalTarget = (Intent) targetsParcelable[i]; |
| 308 | if (i == 0 && target == null) { |
| 309 | target = additionalTarget; |
| 310 | modifyTargetIntent(target); |
| 311 | } else { |
| 312 | additionalTargets[offset ? i - 1 : i] = additionalTarget; |
| 313 | modifyTargetIntent(additionalTarget); |
| 314 | } |
| 315 | } |
| 316 | setAdditionalTargets(additionalTargets); |
| 317 | } |
| 318 | |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 319 | mReplacementExtras = intent.getBundleExtra(Intent.EXTRA_REPLACEMENT_EXTRAS); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 320 | |
| 321 | // Do not allow the title to be changed when sharing content |
| 322 | CharSequence title = null; |
| 323 | if (target != null) { |
| 324 | String targetAction = target.getAction(); |
| 325 | if (!(Intent.ACTION_SEND.equals(targetAction) || Intent.ACTION_SEND_MULTIPLE.equals( |
| 326 | targetAction))) { |
| 327 | title = intent.getCharSequenceExtra(Intent.EXTRA_TITLE); |
| 328 | } else { |
| 329 | Log.w(TAG, "Ignoring intent's EXTRA_TITLE, deprecated in P. You may wish to set a" |
| 330 | + " preview title by using EXTRA_TITLE property of the wrapped" |
| 331 | + " EXTRA_INTENT."); |
| 332 | } |
| 333 | } |
| 334 | |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 335 | int defaultTitleRes = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | if (title == null) { |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 337 | defaultTitleRes = com.android.internal.R.string.chooseActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | } |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 339 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 340 | Parcelable[] pa = intent.getParcelableArrayExtra(Intent.EXTRA_INITIAL_INTENTS); |
| 341 | Intent[] initialIntents = null; |
| 342 | if (pa != null) { |
| 343 | initialIntents = new Intent[pa.length]; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 344 | for (int i = 0; i < pa.length; i++) { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 345 | if (!(pa[i] instanceof Intent)) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 346 | Log.w(TAG, "Initial intent #" + i + " not an Intent: " + pa[i]); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 347 | finish(); |
Christopher Tate | 9d6376a | 2014-02-12 13:14:10 -0800 | [diff] [blame] | 348 | super.onCreate(null); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 349 | return; |
| 350 | } |
Craig Mautner | 411d2aed | 2014-05-08 09:07:43 -0700 | [diff] [blame] | 351 | final Intent in = (Intent) pa[i]; |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 352 | modifyTargetIntent(in); |
Craig Mautner | 411d2aed | 2014-05-08 09:07:43 -0700 | [diff] [blame] | 353 | initialIntents[i] = in; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 356 | |
Adam Powell | 13036be | 2015-05-12 14:43:56 -0700 | [diff] [blame] | 357 | mReferrerFillInIntent = new Intent().putExtra(Intent.EXTRA_REFERRER, getReferrer()); |
| 358 | |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 359 | mChosenComponentSender = intent.getParcelableExtra( |
| 360 | Intent.EXTRA_CHOSEN_COMPONENT_INTENT_SENDER); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 361 | mRefinementIntentSender = intent.getParcelableExtra( |
| 362 | Intent.EXTRA_CHOOSER_REFINEMENT_INTENT_SENDER); |
Dianne Hackborn | 028ceeb | 2014-08-17 17:45:48 -0700 | [diff] [blame] | 363 | setSafeForwardingMode(true); |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 364 | |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 365 | pa = intent.getParcelableArrayExtra(Intent.EXTRA_EXCLUDE_COMPONENTS); |
| 366 | if (pa != null) { |
| 367 | ComponentName[] names = new ComponentName[pa.length]; |
| 368 | for (int i = 0; i < pa.length; i++) { |
| 369 | if (!(pa[i] instanceof ComponentName)) { |
| 370 | Log.w(TAG, "Filtered component #" + i + " not a ComponentName: " + pa[i]); |
| 371 | names = null; |
| 372 | break; |
| 373 | } |
| 374 | names[i] = (ComponentName) pa[i]; |
| 375 | } |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 376 | mFilteredComponentNames = names; |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | pa = intent.getParcelableArrayExtra(Intent.EXTRA_CHOOSER_TARGETS); |
| 380 | if (pa != null) { |
| 381 | ChooserTarget[] targets = new ChooserTarget[pa.length]; |
| 382 | for (int i = 0; i < pa.length; i++) { |
| 383 | if (!(pa[i] instanceof ChooserTarget)) { |
| 384 | Log.w(TAG, "Chooser target #" + i + " not a ChooserTarget: " + pa[i]); |
| 385 | targets = null; |
| 386 | break; |
| 387 | } |
| 388 | targets[i] = (ChooserTarget) pa[i]; |
| 389 | } |
| 390 | mCallerChooserTargets = targets; |
| 391 | } |
| 392 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 393 | mPinnedSharedPrefs = getPinnedSharedPrefs(this); |
Jorim Jaggi | f631ef7 | 2017-02-24 13:49:47 +0100 | [diff] [blame] | 394 | setRetainInOnStop(intent.getBooleanExtra(EXTRA_PRIVATE_RETAIN_IN_ON_STOP, false)); |
Adam Powell | 278902c | 2014-07-12 18:33:22 -0700 | [diff] [blame] | 395 | super.onCreate(savedInstanceState, target, title, defaultTitleRes, initialIntents, |
| 396 | null, false); |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 397 | |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 398 | mChooserShownTime = System.currentTimeMillis(); |
| 399 | final long systemCost = mChooserShownTime - intentReceivedTime; |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 400 | |
| 401 | getMetricsLogger().write(new LogMaker(MetricsEvent.ACTION_ACTIVITY_CHOOSER_SHOWN) |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 402 | .setSubtype(isWorkProfile() ? MetricsEvent.MANAGED_PROFILE : |
| 403 | MetricsEvent.PARENT_PROFILE) |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 404 | .addTaggedData(MetricsEvent.FIELD_SHARESHEET_MIMETYPE, target.getType()) |
| 405 | .addTaggedData(MetricsEvent.FIELD_TIME_TO_APP_TARGETS, systemCost)); |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 406 | |
| 407 | if (USE_PREDICTION_MANAGER_FOR_DIRECT_TARGETS) { |
| 408 | final IntentFilter filter = getTargetIntentFilter(); |
| 409 | Bundle extras = new Bundle(); |
| 410 | extras.putParcelable(APP_PREDICTION_INTENT_FILTER_KEY, filter); |
| 411 | AppPredictionManager appPredictionManager = |
| 412 | getSystemService(AppPredictionManager.class); |
| 413 | mAppPredictor = appPredictionManager.createAppPredictionSession( |
| 414 | new AppPredictionContext.Builder(this) |
| 415 | .setPredictedTargetCount(APP_PREDICTION_SHARE_TARGET_QUERY_PACKAGE_LIMIT) |
| 416 | .setUiSurface(APP_PREDICTION_SHARE_UI_SURFACE) |
| 417 | .setExtras(extras) |
| 418 | .build()); |
| 419 | mAppPredictorCallback = resultList -> { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 420 | if (isFinishing() || isDestroyed()) { |
| 421 | return; |
| 422 | } |
George Hodulik | 0dd5fbe | 2019-03-06 12:00:26 -0800 | [diff] [blame] | 423 | // May be null if there are no apps to perform share/open action. |
| 424 | if (mChooserListAdapter == null) { |
| 425 | return; |
| 426 | } |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 427 | final List<DisplayResolveInfo> driList = |
| 428 | getDisplayResolveInfos(mChooserListAdapter); |
| 429 | final List<ShortcutManager.ShareShortcutInfo> shareShortcutInfos = |
| 430 | new ArrayList<>(); |
| 431 | for (AppTarget appTarget : resultList) { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 432 | if (appTarget.getShortcutInfo() == null) { |
| 433 | continue; |
| 434 | } |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 435 | shareShortcutInfos.add(new ShortcutManager.ShareShortcutInfo( |
| 436 | appTarget.getShortcutInfo(), |
| 437 | new ComponentName( |
| 438 | appTarget.getPackageName(), appTarget.getClassName()))); |
| 439 | } |
| 440 | sendShareShortcutInfoList(shareShortcutInfos, driList); |
| 441 | }; |
| 442 | mAppPredictor.registerPredictionUpdates(this.getMainExecutor(), mAppPredictorCallback); |
| 443 | } |
| 444 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 445 | mChooserRowLayer = getResources().getDrawable(R.drawable.chooser_row_layer_list, null); |
| 446 | mChooserRowServiceSpacing = getResources() |
| 447 | .getDimensionPixelSize(R.dimen.chooser_service_spacing); |
| 448 | |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 449 | if (DEBUG) { |
| 450 | Log.d(TAG, "System Time Cost is " + systemCost); |
| 451 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | } |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 453 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 454 | /** |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 455 | * Check if the profile currently used is a work profile. |
| 456 | * @return true if it is work profile, false if it is parent profile (or no work profile is |
| 457 | * set up) |
| 458 | */ |
| 459 | protected boolean isWorkProfile() { |
| 460 | return ((UserManager) getSystemService(Context.USER_SERVICE)) |
| 461 | .getUserInfo(UserHandle.myUserId()).isManagedProfile(); |
| 462 | } |
| 463 | |
| 464 | /** |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 465 | * Override method to add content preview area, specific to the chooser activity. |
| 466 | */ |
| 467 | @Override |
| 468 | public void setHeader() { |
| 469 | super.setHeader(); |
| 470 | |
| 471 | Intent targetIntent = getTargetIntent(); |
| 472 | if (targetIntent == null) { |
| 473 | return; |
| 474 | } |
| 475 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 476 | String action = targetIntent.getAction(); |
| 477 | if (!(Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action))) { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 478 | return; |
| 479 | } |
| 480 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 481 | if (mChooserListAdapter == null || mChooserListAdapter.getCount() == 0) { |
| 482 | return; |
| 483 | } |
| 484 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 485 | int previewType = findPreferredContentPreview(targetIntent, getContentResolver()); |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 486 | |
| 487 | getMetricsLogger().write(new LogMaker(MetricsEvent.ACTION_SHARE_WITH_PREVIEW) |
| 488 | .setSubtype(previewType)); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 489 | displayContentPreview(previewType, targetIntent); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 490 | } |
| 491 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 492 | private void onCopyButtonClicked(View v) { |
| 493 | Intent targetIntent = getTargetIntent(); |
| 494 | if (targetIntent == null) { |
| 495 | finish(); |
| 496 | } else { |
| 497 | final String action = targetIntent.getAction(); |
| 498 | |
| 499 | ClipData clipData = null; |
| 500 | if (Intent.ACTION_SEND.equals(action)) { |
| 501 | String extraText = targetIntent.getStringExtra(Intent.EXTRA_TEXT); |
| 502 | Uri extraStream = targetIntent.getParcelableExtra(Intent.EXTRA_STREAM); |
| 503 | |
| 504 | if (extraText != null) { |
| 505 | clipData = ClipData.newPlainText(null, extraText); |
| 506 | } else if (extraStream != null) { |
| 507 | clipData = ClipData.newUri(getContentResolver(), null, extraStream); |
| 508 | } else { |
| 509 | Log.w(TAG, "No data available to copy to clipboard"); |
| 510 | return; |
| 511 | } |
| 512 | } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) { |
| 513 | final ArrayList<Uri> streams = targetIntent.getParcelableArrayListExtra( |
| 514 | Intent.EXTRA_STREAM); |
| 515 | clipData = ClipData.newUri(getContentResolver(), null, streams.get(0)); |
| 516 | for (int i = 1; i < streams.size(); i++) { |
| 517 | clipData.addItem(getContentResolver(), new ClipData.Item(streams.get(i))); |
| 518 | } |
| 519 | } else { |
| 520 | // expected to only be visible with ACTION_SEND or ACTION_SEND_MULTIPLE |
| 521 | // so warn about unexpected action |
| 522 | Log.w(TAG, "Action (" + action + ") not supported for copying to clipboard"); |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | ClipboardManager clipboardManager = (ClipboardManager) getSystemService( |
| 527 | Context.CLIPBOARD_SERVICE); |
| 528 | clipboardManager.setPrimaryClip(clipData); |
| 529 | Toast.makeText(getApplicationContext(), R.string.copied, Toast.LENGTH_SHORT).show(); |
| 530 | |
| 531 | finish(); |
| 532 | } |
| 533 | } |
| 534 | |
Matt Pietal | 18bbd82 | 2019-02-12 15:21:36 -0500 | [diff] [blame] | 535 | @Override |
| 536 | public void onConfigurationChanged(Configuration newConfig) { |
| 537 | super.onConfigurationChanged(newConfig); |
| 538 | |
| 539 | int width = -1; |
| 540 | if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { |
| 541 | width = getResources().getDimensionPixelSize(R.dimen.chooser_preview_width); |
| 542 | } |
| 543 | |
| 544 | updateLayoutWidth(R.id.content_preview_text_layout, width); |
| 545 | updateLayoutWidth(R.id.content_preview_title_layout, width); |
| 546 | updateLayoutWidth(R.id.content_preview_file_layout, width); |
| 547 | } |
| 548 | |
| 549 | private void updateLayoutWidth(int layoutResourceId, int width) { |
| 550 | View view = findViewById(layoutResourceId); |
| 551 | LayoutParams params = view.getLayoutParams(); |
| 552 | params.width = width; |
| 553 | view.setLayoutParams(params); |
| 554 | } |
| 555 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 556 | private void displayContentPreview(@ContentPreviewType int previewType, Intent targetIntent) { |
| 557 | switch (previewType) { |
| 558 | case CONTENT_PREVIEW_TEXT: |
| 559 | displayTextContentPreview(targetIntent); |
| 560 | break; |
| 561 | case CONTENT_PREVIEW_IMAGE: |
| 562 | displayImageContentPreview(targetIntent); |
| 563 | break; |
| 564 | case CONTENT_PREVIEW_FILE: |
| 565 | displayFileContentPreview(targetIntent); |
| 566 | break; |
| 567 | default: |
| 568 | Log.e(TAG, "Unexpected content preview type: " + previewType); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | private void displayTextContentPreview(Intent targetIntent) { |
| 573 | ViewGroup contentPreviewLayout = findViewById(R.id.content_preview_text_area); |
| 574 | contentPreviewLayout.setVisibility(View.VISIBLE); |
| 575 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 576 | findViewById(R.id.copy_button).setOnClickListener(this::onCopyButtonClicked); |
| 577 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 578 | CharSequence sharingText = targetIntent.getCharSequenceExtra(Intent.EXTRA_TEXT); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 579 | if (sharingText == null) { |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 580 | findViewById(R.id.content_preview_text_layout).setVisibility(View.GONE); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 581 | } else { |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 582 | TextView textView = findViewById(R.id.content_preview_text); |
| 583 | textView.setText(sharingText); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | String previewTitle = targetIntent.getStringExtra(Intent.EXTRA_TITLE); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 587 | if (TextUtils.isEmpty(previewTitle)) { |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 588 | findViewById(R.id.content_preview_title_layout).setVisibility(View.GONE); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 589 | } else { |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 590 | TextView previewTitleView = findViewById(R.id.content_preview_title); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 591 | previewTitleView.setText(previewTitle); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 592 | |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 593 | ClipData previewData = targetIntent.getClipData(); |
| 594 | Uri previewThumbnail = null; |
| 595 | if (previewData != null) { |
| 596 | if (previewData.getItemCount() > 0) { |
| 597 | ClipData.Item previewDataItem = previewData.getItemAt(0); |
| 598 | previewThumbnail = previewDataItem.getUri(); |
| 599 | } |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 600 | } |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 601 | |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 602 | ImageView previewThumbnailView = findViewById(R.id.content_preview_thumbnail); |
| 603 | if (previewThumbnail == null) { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 604 | previewThumbnailView.setVisibility(View.GONE); |
| 605 | } else { |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 606 | Bitmap bmp = loadThumbnail(previewThumbnail, new Size(100, 100)); |
| 607 | if (bmp == null) { |
| 608 | previewThumbnailView.setVisibility(View.GONE); |
| 609 | } else { |
| 610 | previewThumbnailView.setImageBitmap(bmp); |
| 611 | } |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 616 | private void displayImageContentPreview(Intent targetIntent) { |
| 617 | ViewGroup contentPreviewLayout = findViewById(R.id.content_preview_image_area); |
| 618 | contentPreviewLayout.setVisibility(View.VISIBLE); |
| 619 | |
| 620 | String action = targetIntent.getAction(); |
| 621 | if (Intent.ACTION_SEND.equals(action)) { |
| 622 | Uri uri = targetIntent.getParcelableExtra(Intent.EXTRA_STREAM); |
| 623 | loadUriIntoView(R.id.content_preview_image_1_large, uri); |
| 624 | } else { |
| 625 | ContentResolver resolver = getContentResolver(); |
| 626 | |
| 627 | List<Uri> uris = targetIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); |
| 628 | List<Uri> imageUris = new ArrayList<>(); |
| 629 | for (Uri uri : uris) { |
| 630 | if (isImageType(resolver.getType(uri))) { |
| 631 | imageUris.add(uri); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | if (imageUris.size() == 0) { |
| 636 | Log.i(TAG, "Attempted to display image preview area with zero" |
| 637 | + " available images detected in EXTRA_STREAM list"); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 638 | contentPreviewLayout.setVisibility(View.GONE); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | |
| 642 | loadUriIntoView(R.id.content_preview_image_1_large, imageUris.get(0)); |
| 643 | |
| 644 | if (imageUris.size() == 2) { |
| 645 | loadUriIntoView(R.id.content_preview_image_2_large, imageUris.get(1)); |
| 646 | } else if (imageUris.size() > 2) { |
| 647 | loadUriIntoView(R.id.content_preview_image_2_small, imageUris.get(1)); |
| 648 | RoundedRectImageView imageView = loadUriIntoView( |
| 649 | R.id.content_preview_image_3_small, imageUris.get(2)); |
| 650 | |
| 651 | if (imageUris.size() > 3) { |
| 652 | imageView.setExtraImageCount(imageUris.size() - 3); |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 658 | private static class FileInfo { |
| 659 | public final String name; |
| 660 | public final boolean hasThumbnail; |
| 661 | |
| 662 | FileInfo(String name, boolean hasThumbnail) { |
| 663 | this.name = name; |
| 664 | this.hasThumbnail = hasThumbnail; |
| 665 | } |
| 666 | } |
| 667 | |
Matt Pietal | f38e9d2 | 2019-02-15 10:01:03 -0500 | [diff] [blame] | 668 | /** |
| 669 | * Wrapping the ContentResolver call to expose for easier mocking, |
| 670 | * and to avoid mocking Android core classes. |
| 671 | */ |
| 672 | @VisibleForTesting |
| 673 | public Cursor queryResolver(ContentResolver resolver, Uri uri) { |
| 674 | return resolver.query(uri, null, null, null, null); |
| 675 | } |
| 676 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 677 | private FileInfo extractFileInfo(Uri uri, ContentResolver resolver) { |
| 678 | String fileName = null; |
| 679 | boolean hasThumbnail = false; |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 680 | |
Matt Pietal | f38e9d2 | 2019-02-15 10:01:03 -0500 | [diff] [blame] | 681 | try (Cursor cursor = queryResolver(resolver, uri)) { |
| 682 | if (cursor != null && cursor.getCount() > 0) { |
| 683 | int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); |
| 684 | int titleIndex = cursor.getColumnIndex(Downloads.Impl.COLUMN_TITLE); |
| 685 | int flagsIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_FLAGS); |
| 686 | |
| 687 | cursor.moveToFirst(); |
| 688 | if (nameIndex != -1) { |
| 689 | fileName = cursor.getString(nameIndex); |
| 690 | } else if (titleIndex != -1) { |
| 691 | fileName = cursor.getString(titleIndex); |
| 692 | } |
| 693 | |
| 694 | if (flagsIndex != -1) { |
| 695 | hasThumbnail = (cursor.getInt(flagsIndex) |
| 696 | & DocumentsContract.Document.FLAG_SUPPORTS_THUMBNAIL) != 0; |
| 697 | } |
| 698 | } |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 699 | } catch (SecurityException e) { |
| 700 | Log.w(TAG, "Error loading file preview", e); |
| 701 | } |
| 702 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 703 | if (TextUtils.isEmpty(fileName)) { |
| 704 | fileName = uri.getPath(); |
| 705 | int index = fileName.lastIndexOf('/'); |
| 706 | if (index != -1) { |
| 707 | fileName = fileName.substring(index + 1); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | return new FileInfo(fileName, hasThumbnail); |
| 712 | } |
| 713 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 714 | private void displayFileContentPreview(Intent targetIntent) { |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 715 | ViewGroup contentPreviewLayout = findViewById(R.id.content_preview_file_area); |
| 716 | contentPreviewLayout.setVisibility(View.VISIBLE); |
| 717 | |
| 718 | // TODO(b/120417119): Disable file copy until after moving to sysui, |
| 719 | // due to permissions issues |
| 720 | findViewById(R.id.file_copy_button).setVisibility(View.GONE); |
| 721 | |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 722 | String action = targetIntent.getAction(); |
| 723 | if (Intent.ACTION_SEND.equals(action)) { |
| 724 | Uri uri = targetIntent.getParcelableExtra(Intent.EXTRA_STREAM); |
| 725 | loadFileUriIntoView(uri); |
| 726 | } else { |
| 727 | List<Uri> uris = targetIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); |
| 728 | int uriCount = uris.size(); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 729 | |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 730 | if (uriCount == 0) { |
| 731 | contentPreviewLayout.setVisibility(View.GONE); |
| 732 | Log.i(TAG, |
| 733 | "Appears to be no uris available in EXTRA_STREAM, removing " |
| 734 | + "preview area"); |
| 735 | return; |
| 736 | } else if (uriCount == 1) { |
| 737 | loadFileUriIntoView(uris.get(0)); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 738 | } else { |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 739 | FileInfo fileInfo = extractFileInfo(uris.get(0), getContentResolver()); |
| 740 | int remUriCount = uriCount - 1; |
Matt Pietal | acabc57 | 2019-02-14 11:02:05 -0500 | [diff] [blame] | 741 | String fileName = getResources().getQuantityString(R.plurals.file_count, |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 742 | remUriCount, fileInfo.name, remUriCount); |
Matt Pietal | acabc57 | 2019-02-14 11:02:05 -0500 | [diff] [blame] | 743 | |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 744 | TextView fileNameView = findViewById(R.id.content_preview_filename); |
Matt Pietal | acabc57 | 2019-02-14 11:02:05 -0500 | [diff] [blame] | 745 | fileNameView.setText(fileName); |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 746 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 747 | ImageView fileIconView = findViewById(R.id.content_preview_file_icon); |
| 748 | fileIconView.setVisibility(View.VISIBLE); |
Matt Pietal | acabc57 | 2019-02-14 11:02:05 -0500 | [diff] [blame] | 749 | fileIconView.setImageResource(R.drawable.ic_file_copy); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 750 | } |
Matt Pietal | 3087bca | 2019-02-14 12:19:16 -0500 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
| 754 | private void loadFileUriIntoView(Uri uri) { |
| 755 | FileInfo fileInfo = extractFileInfo(uri, getContentResolver()); |
| 756 | |
| 757 | TextView fileNameView = findViewById(R.id.content_preview_filename); |
| 758 | fileNameView.setText(fileInfo.name); |
| 759 | |
| 760 | if (fileInfo.hasThumbnail) { |
| 761 | loadUriIntoView(R.id.content_preview_file_thumbnail, uri); |
| 762 | } else { |
| 763 | ImageView fileIconView = findViewById(R.id.content_preview_file_icon); |
| 764 | fileIconView.setVisibility(View.VISIBLE); |
| 765 | fileIconView.setImageResource(R.drawable.ic_doc_generic); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 766 | } |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | private RoundedRectImageView loadUriIntoView(int imageResourceId, Uri uri) { |
| 770 | RoundedRectImageView imageView = findViewById(imageResourceId); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 771 | Bitmap bmp = loadThumbnail(uri, new Size(200, 200)); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 772 | if (bmp != null) { |
| 773 | imageView.setVisibility(View.VISIBLE); |
| 774 | imageView.setImageBitmap(bmp); |
| 775 | } |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 776 | |
| 777 | return imageView; |
| 778 | } |
| 779 | |
| 780 | @VisibleForTesting |
| 781 | protected boolean isImageType(String mimeType) { |
| 782 | return mimeType != null && mimeType.startsWith("image/"); |
| 783 | } |
| 784 | |
| 785 | @ContentPreviewType |
| 786 | private int findPreferredContentPreview(Uri uri, ContentResolver resolver) { |
| 787 | if (uri == null) { |
| 788 | return CONTENT_PREVIEW_TEXT; |
| 789 | } |
| 790 | |
| 791 | String mimeType = resolver.getType(uri); |
| 792 | return isImageType(mimeType) ? CONTENT_PREVIEW_IMAGE : CONTENT_PREVIEW_FILE; |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * In {@link android.content.Intent#getType}, the app may specify a very general |
| 797 | * mime-type that broadly covers all data being shared, such as {@literal *}/* |
| 798 | * when sending an image and text. We therefore should inspect each item for the |
| 799 | * the preferred type, in order of IMAGE, FILE, TEXT. |
| 800 | */ |
| 801 | @ContentPreviewType |
| 802 | private int findPreferredContentPreview(Intent targetIntent, ContentResolver resolver) { |
| 803 | String action = targetIntent.getAction(); |
| 804 | if (Intent.ACTION_SEND.equals(action)) { |
| 805 | Uri uri = targetIntent.getParcelableExtra(Intent.EXTRA_STREAM); |
| 806 | return findPreferredContentPreview(uri, resolver); |
| 807 | } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) { |
| 808 | List<Uri> uris = targetIntent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); |
| 809 | if (uris == null || uris.isEmpty()) { |
| 810 | return CONTENT_PREVIEW_TEXT; |
| 811 | } |
| 812 | |
| 813 | for (Uri uri : uris) { |
| 814 | if (findPreferredContentPreview(uri, resolver) == CONTENT_PREVIEW_IMAGE) { |
| 815 | return CONTENT_PREVIEW_IMAGE; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | return CONTENT_PREVIEW_FILE; |
| 820 | } |
| 821 | |
| 822 | return CONTENT_PREVIEW_TEXT; |
| 823 | } |
| 824 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 825 | static SharedPreferences getPinnedSharedPrefs(Context context) { |
| 826 | // The code below is because in the android:ui process, no one can hear you scream. |
| 827 | // The package info in the context isn't initialized in the way it is for normal apps, |
| 828 | // so the standard, name-based context.getSharedPreferences doesn't work. Instead, we |
| 829 | // build the path manually below using the same policy that appears in ContextImpl. |
| 830 | // This fails silently under the hood if there's a problem, so if we find ourselves in |
| 831 | // the case where we don't have access to credential encrypted storage we just won't |
| 832 | // have our pinned target info. |
| 833 | final File prefsFile = new File(new File( |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 834 | Environment.getDataUserCePackageDirectory(StorageManager.UUID_PRIVATE_INTERNAL, |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 835 | context.getUserId(), context.getPackageName()), |
| 836 | "shared_prefs"), |
| 837 | PINNED_SHARED_PREFS_NAME + ".xml"); |
| 838 | return context.getSharedPreferences(prefsFile, MODE_PRIVATE); |
| 839 | } |
| 840 | |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 841 | @Override |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 842 | protected void onDestroy() { |
| 843 | super.onDestroy(); |
| 844 | if (mRefinementResultReceiver != null) { |
| 845 | mRefinementResultReceiver.destroy(); |
| 846 | mRefinementResultReceiver = null; |
| 847 | } |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 848 | unbindRemainingServices(); |
| 849 | mChooserHandler.removeMessages(CHOOSER_TARGET_SERVICE_RESULT); |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 850 | if (USE_PREDICTION_MANAGER_FOR_DIRECT_TARGETS) { |
| 851 | mAppPredictor.unregisterPredictionUpdates(mAppPredictorCallback); |
| 852 | mAppPredictor.destroy(); |
| 853 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | @Override |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 857 | public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) { |
| 858 | Intent result = defIntent; |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 859 | if (mReplacementExtras != null) { |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 860 | final Bundle replExtras = mReplacementExtras.getBundle(aInfo.packageName); |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 861 | if (replExtras != null) { |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 862 | result = new Intent(defIntent); |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 863 | result.putExtras(replExtras); |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 864 | } |
| 865 | } |
Nicolas Prevot | 741abfc | 2015-08-11 12:03:51 +0100 | [diff] [blame] | 866 | if (aInfo.name.equals(IntentForwarderActivity.FORWARD_INTENT_TO_PARENT) |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 867 | || aInfo.name.equals(IntentForwarderActivity.FORWARD_INTENT_TO_MANAGED_PROFILE)) { |
| 868 | result = Intent.createChooser(result, |
| 869 | getIntent().getCharSequenceExtra(Intent.EXTRA_TITLE)); |
Hakan Seyalioglu | 7317e8a | 2016-12-12 16:15:38 -0800 | [diff] [blame] | 870 | |
| 871 | // Don't auto-launch single intents if the intent is being forwarded. This is done |
| 872 | // because automatically launching a resolving application as a response to the user |
| 873 | // action of switching accounts is pretty unexpected. |
| 874 | result.putExtra(Intent.EXTRA_AUTO_LAUNCH_SINGLE_CHOICE, false); |
Nicolas Prevot | 0e2b73f | 2014-10-27 10:06:11 +0000 | [diff] [blame] | 875 | } |
| 876 | return result; |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 879 | @Override |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 880 | public void onActivityStarted(TargetInfo cti) { |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 881 | if (mChosenComponentSender != null) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 882 | final ComponentName target = cti.getResolvedComponentName(); |
Adam Powell | 0b3c112 | 2014-10-09 12:50:14 -0700 | [diff] [blame] | 883 | if (target != null) { |
| 884 | final Intent fillIn = new Intent().putExtra(Intent.EXTRA_CHOSEN_COMPONENT, target); |
| 885 | try { |
| 886 | mChosenComponentSender.sendIntent(this, Activity.RESULT_OK, fillIn, null, null); |
| 887 | } catch (IntentSender.SendIntentException e) { |
| 888 | Slog.e(TAG, "Unable to launch supplied IntentSender to report " |
| 889 | + "the chosen component: " + e); |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 895 | @Override |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 896 | public void onPrepareAdapterView(AbsListView adapterView, ResolveListAdapter adapter) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 897 | final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null; |
| 898 | mChooserListAdapter = (ChooserListAdapter) adapter; |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 899 | if (mCallerChooserTargets != null && mCallerChooserTargets.length > 0) { |
| 900 | mChooserListAdapter.addServiceResults(null, Lists.newArrayList(mCallerChooserTargets)); |
| 901 | } |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 902 | mChooserRowAdapter = new ChooserRowAdapter(mChooserListAdapter); |
| 903 | mChooserRowAdapter.registerDataSetObserver(new OffsetDataSetObserver(adapterView)); |
| 904 | adapterView.setAdapter(mChooserRowAdapter); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 905 | if (listView != null) { |
| 906 | listView.setItemsCanFocus(true); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | @Override |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 911 | public int getLayoutResource() { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 912 | return R.layout.chooser_grid; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | @Override |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 916 | public boolean shouldGetActivityMetadata() { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 917 | return true; |
| 918 | } |
| 919 | |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 920 | @Override |
Ben Lin | 145b0ca | 2016-10-14 14:23:40 -0700 | [diff] [blame] | 921 | public boolean shouldAutoLaunchSingleChoice(TargetInfo target) { |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 922 | // Note that this is only safe because the Intent handled by the ChooserActivity is |
| 923 | // guaranteed to contain no extras unknown to the local ClassLoader. That is why this |
| 924 | // method can not be replaced in the ResolverActivity whole hog. |
Ben Lin | 145b0ca | 2016-10-14 14:23:40 -0700 | [diff] [blame] | 925 | return getIntent().getBooleanExtra(Intent.EXTRA_AUTO_LAUNCH_SINGLE_CHOICE, |
| 926 | super.shouldAutoLaunchSingleChoice(target)); |
| 927 | } |
| 928 | |
| 929 | @Override |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 930 | public void showTargetDetails(ResolveInfo ri) { |
sanryhuang | 296ca9e | 2018-03-31 11:17:13 +0800 | [diff] [blame] | 931 | if (ri == null) { |
| 932 | return; |
| 933 | } |
| 934 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 935 | ComponentName name = ri.activityInfo.getComponentName(); |
| 936 | boolean pinned = mPinnedSharedPrefs.getBoolean(name.flattenToString(), false); |
| 937 | ResolverTargetActionsDialogFragment f = |
| 938 | new ResolverTargetActionsDialogFragment(ri.loadLabel(getPackageManager()), |
| 939 | name, pinned); |
| 940 | f.show(getFragmentManager(), TARGET_DETAILS_FRAGMENT_TAG); |
| 941 | } |
| 942 | |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 943 | private void modifyTargetIntent(Intent in) { |
| 944 | final String action = in.getAction(); |
| 945 | if (Intent.ACTION_SEND.equals(action) || |
| 946 | Intent.ACTION_SEND_MULTIPLE.equals(action)) { |
| 947 | in.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | |
Dianne Hackborn | 13420f2 | 2014-07-18 15:43:56 -0700 | [diff] [blame] | 948 | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); |
Adam Powell | e49d939 | 2014-07-17 18:45:19 -0700 | [diff] [blame] | 949 | } |
| 950 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 951 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 952 | @Override |
| 953 | protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 954 | if (target instanceof NotSelectableTargetInfo) { |
| 955 | return false; |
| 956 | } |
| 957 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 958 | if (mRefinementIntentSender != null) { |
| 959 | final Intent fillIn = new Intent(); |
| 960 | final List<Intent> sourceIntents = target.getAllSourceIntents(); |
| 961 | if (!sourceIntents.isEmpty()) { |
| 962 | fillIn.putExtra(Intent.EXTRA_INTENT, sourceIntents.get(0)); |
| 963 | if (sourceIntents.size() > 1) { |
| 964 | final Intent[] alts = new Intent[sourceIntents.size() - 1]; |
| 965 | for (int i = 1, N = sourceIntents.size(); i < N; i++) { |
| 966 | alts[i - 1] = sourceIntents.get(i); |
| 967 | } |
| 968 | fillIn.putExtra(Intent.EXTRA_ALTERNATE_INTENTS, alts); |
| 969 | } |
| 970 | if (mRefinementResultReceiver != null) { |
| 971 | mRefinementResultReceiver.destroy(); |
| 972 | } |
| 973 | mRefinementResultReceiver = new RefinementResultReceiver(this, target, null); |
| 974 | fillIn.putExtra(Intent.EXTRA_RESULT_RECEIVER, |
| 975 | mRefinementResultReceiver); |
| 976 | try { |
| 977 | mRefinementIntentSender.sendIntent(this, 0, fillIn, null, null); |
| 978 | return false; |
| 979 | } catch (SendIntentException e) { |
| 980 | Log.e(TAG, "Refinement IntentSender failed to send", e); |
| 981 | } |
| 982 | } |
| 983 | } |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 984 | updateModelAndChooserCounts(target); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 985 | return super.onTargetSelected(target, alwaysCheck); |
| 986 | } |
| 987 | |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 988 | @Override |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 989 | public void startSelected(int which, boolean always, boolean filtered) { |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 990 | final long selectionCost = System.currentTimeMillis() - mChooserShownTime; |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 991 | super.startSelected(which, always, filtered); |
| 992 | |
| 993 | if (mChooserListAdapter != null) { |
| 994 | // Log the index of which type of target the user picked. |
| 995 | // Lower values mean the ranking was better. |
| 996 | int cat = 0; |
| 997 | int value = which; |
| 998 | switch (mChooserListAdapter.getPositionTargetType(which)) { |
| 999 | case ChooserListAdapter.TARGET_CALLER: |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 1000 | cat = MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_APP_TARGET; |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1001 | break; |
| 1002 | case ChooserListAdapter.TARGET_SERVICE: |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 1003 | cat = MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_SERVICE_TARGET; |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1004 | value -= mChooserListAdapter.getCallerTargetCount(); |
| 1005 | break; |
| 1006 | case ChooserListAdapter.TARGET_STANDARD: |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 1007 | cat = MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_STANDARD_TARGET; |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1008 | value -= mChooserListAdapter.getCallerTargetCount() |
| 1009 | + mChooserListAdapter.getServiceTargetCount(); |
| 1010 | break; |
| 1011 | } |
| 1012 | |
| 1013 | if (cat != 0) { |
| 1014 | MetricsLogger.action(this, cat, value); |
| 1015 | } |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 1016 | |
| 1017 | if (mIsSuccessfullySelected) { |
| 1018 | if (DEBUG) { |
| 1019 | Log.d(TAG, "User Selection Time Cost is " + selectionCost); |
| 1020 | Log.d(TAG, "position of selected app/service/caller is " + |
| 1021 | Integer.toString(value)); |
| 1022 | } |
| 1023 | MetricsLogger.histogram(null, "user_selection_cost_for_smart_sharing", |
| 1024 | (int) selectionCost); |
| 1025 | MetricsLogger.histogram(null, "app_position_for_smart_sharing", value); |
| 1026 | } |
Adam Powell | 98b7f89 | 2015-06-19 12:38:45 -0700 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1030 | void queryTargetServices(ChooserListAdapter adapter) { |
| 1031 | final PackageManager pm = getPackageManager(); |
Mehdi Alizadeh | 85fd3d5 | 2019-01-23 12:49:53 -0800 | [diff] [blame] | 1032 | ShortcutManager sm = (ShortcutManager) getSystemService(ShortcutManager.class); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1033 | int targetsToQuery = 0; |
| 1034 | for (int i = 0, N = adapter.getDisplayResolveInfoCount(); i < N; i++) { |
| 1035 | final DisplayResolveInfo dri = adapter.getDisplayResolveInfo(i); |
Adam Powell | 3a09c52 | 2015-10-21 13:21:28 -0700 | [diff] [blame] | 1036 | if (adapter.getScore(dri) == 0) { |
| 1037 | // A score of 0 means the app hasn't been used in some time; |
| 1038 | // don't query it as it's not likely to be relevant. |
| 1039 | continue; |
| 1040 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1041 | final ActivityInfo ai = dri.getResolveInfo().activityInfo; |
Mehdi Alizadeh | 85fd3d5 | 2019-01-23 12:49:53 -0800 | [diff] [blame] | 1042 | if (USE_SHORTCUT_MANAGER_FOR_DIRECT_TARGETS |
| 1043 | && sm.hasShareTargets(ai.packageName)) { |
| 1044 | // Share targets will be queried from ShortcutManager |
| 1045 | continue; |
| 1046 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1047 | final Bundle md = ai.metaData; |
| 1048 | final String serviceName = md != null ? convertServiceName(ai.packageName, |
| 1049 | md.getString(ChooserTargetService.META_DATA_NAME)) : null; |
| 1050 | if (serviceName != null) { |
| 1051 | final ComponentName serviceComponent = new ComponentName( |
| 1052 | ai.packageName, serviceName); |
| 1053 | final Intent serviceIntent = new Intent(ChooserTargetService.SERVICE_INTERFACE) |
| 1054 | .setComponent(serviceComponent); |
| 1055 | |
| 1056 | if (DEBUG) { |
| 1057 | Log.d(TAG, "queryTargets found target with service " + serviceComponent); |
| 1058 | } |
| 1059 | |
| 1060 | try { |
| 1061 | final String perm = pm.getServiceInfo(serviceComponent, 0).permission; |
| 1062 | if (!ChooserTargetService.BIND_PERMISSION.equals(perm)) { |
| 1063 | Log.w(TAG, "ChooserTargetService " + serviceComponent + " does not require" |
| 1064 | + " permission " + ChooserTargetService.BIND_PERMISSION |
| 1065 | + " - this service will not be queried for ChooserTargets." |
| 1066 | + " add android:permission=\"" |
| 1067 | + ChooserTargetService.BIND_PERMISSION + "\"" |
| 1068 | + " to the <service> tag for " + serviceComponent |
| 1069 | + " in the manifest."); |
| 1070 | continue; |
| 1071 | } |
| 1072 | } catch (NameNotFoundException e) { |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 1073 | Log.e(TAG, "Could not look up service " + serviceComponent |
| 1074 | + "; component name not found"); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1075 | continue; |
| 1076 | } |
| 1077 | |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1078 | final ChooserTargetServiceConnection conn = |
| 1079 | new ChooserTargetServiceConnection(this, dri); |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 1080 | |
| 1081 | // Explicitly specify Process.myUserHandle instead of calling bindService |
| 1082 | // to avoid the warning from calling from the system process without an explicit |
| 1083 | // user handle |
| 1084 | if (bindServiceAsUser(serviceIntent, conn, BIND_AUTO_CREATE | BIND_NOT_FOREGROUND, |
| 1085 | Process.myUserHandle())) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1086 | if (DEBUG) { |
| 1087 | Log.d(TAG, "Binding service connection for target " + dri |
| 1088 | + " intent " + serviceIntent); |
| 1089 | } |
| 1090 | mServiceConnections.add(conn); |
| 1091 | targetsToQuery++; |
| 1092 | } |
| 1093 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1094 | if (targetsToQuery >= QUERY_TARGET_SERVICE_LIMIT) { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1095 | if (DEBUG) { |
| 1096 | Log.d(TAG, "queryTargets hit query target limit " |
| 1097 | + QUERY_TARGET_SERVICE_LIMIT); |
| 1098 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1099 | break; |
| 1100 | } |
| 1101 | } |
| 1102 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1103 | if (DEBUG) { |
| 1104 | Log.d(TAG, "queryTargets setting watchdog timer for " |
| 1105 | + WATCHDOG_TIMEOUT_MILLIS + "ms"); |
| 1106 | } |
| 1107 | mChooserHandler.sendEmptyMessageDelayed(CHOOSER_TARGET_SERVICE_WATCHDOG_TIMEOUT, |
| 1108 | WATCHDOG_TIMEOUT_MILLIS); |
| 1109 | |
| 1110 | if (mServiceConnections.isEmpty()) { |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 1111 | sendVoiceChoicesIfNeeded(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1115 | private IntentFilter getTargetIntentFilter() { |
| 1116 | try { |
| 1117 | final Intent intent = getTargetIntent(); |
| 1118 | String dataString = intent.getDataString(); |
| 1119 | if (TextUtils.isEmpty(dataString)) { |
| 1120 | dataString = intent.getType(); |
| 1121 | } |
| 1122 | return new IntentFilter(intent.getAction(), dataString); |
| 1123 | } catch (Exception e) { |
| 1124 | Log.e(TAG, "failed to get target intent filter " + e); |
| 1125 | return null; |
| 1126 | } |
| 1127 | } |
| 1128 | |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1129 | private List<DisplayResolveInfo> getDisplayResolveInfos(ChooserListAdapter adapter) { |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1130 | // Need to keep the original DisplayResolveInfos to be able to reconstruct ServiceResultInfo |
| 1131 | // 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] | 1132 | List<DisplayResolveInfo> driList = new ArrayList<>(); |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1133 | int targetsToQuery = 0; |
| 1134 | for (int i = 0, n = adapter.getDisplayResolveInfoCount(); i < n; i++) { |
| 1135 | final DisplayResolveInfo dri = adapter.getDisplayResolveInfo(i); |
| 1136 | if (adapter.getScore(dri) == 0) { |
| 1137 | // A score of 0 means the app hasn't been used in some time; |
| 1138 | // don't query it as it's not likely to be relevant. |
| 1139 | continue; |
| 1140 | } |
| 1141 | driList.add(dri); |
| 1142 | targetsToQuery++; |
| 1143 | // TODO(b/121287224): Do we need this here? (similar to queryTargetServices) |
| 1144 | if (targetsToQuery >= SHARE_TARGET_QUERY_PACKAGE_LIMIT) { |
| 1145 | if (DEBUG) { |
| 1146 | Log.d(TAG, "queryTargets hit query target limit " |
| 1147 | + SHARE_TARGET_QUERY_PACKAGE_LIMIT); |
| 1148 | } |
| 1149 | break; |
| 1150 | } |
| 1151 | } |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1152 | return driList; |
| 1153 | } |
| 1154 | |
| 1155 | private void queryDirectShareTargets(ChooserListAdapter adapter) { |
| 1156 | if (USE_PREDICTION_MANAGER_FOR_DIRECT_TARGETS) { |
| 1157 | mAppPredictor.requestPredictionUpdate(); |
| 1158 | return; |
| 1159 | } |
| 1160 | final IntentFilter filter = getTargetIntentFilter(); |
| 1161 | if (filter == null) { |
| 1162 | return; |
| 1163 | } |
| 1164 | final List<DisplayResolveInfo> driList = getDisplayResolveInfos(adapter); |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1165 | |
| 1166 | AsyncTask.execute(() -> { |
| 1167 | ShortcutManager sm = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE); |
| 1168 | List<ShortcutManager.ShareShortcutInfo> resultList = sm.getShareTargets(filter); |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1169 | sendShareShortcutInfoList(resultList, driList); |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1170 | }); |
| 1171 | } |
| 1172 | |
George Hodulik | 69d4a08 | 2019-01-18 11:27:03 -0800 | [diff] [blame] | 1173 | private void sendShareShortcutInfoList( |
| 1174 | List<ShortcutManager.ShareShortcutInfo> resultList, |
| 1175 | List<DisplayResolveInfo> driList) { |
| 1176 | // Match ShareShortcutInfos with DisplayResolveInfos to be able to use the old code path |
| 1177 | // for direct share targets. After ShareSheet is refactored we should use the |
| 1178 | // ShareShortcutInfos directly. |
| 1179 | boolean resultMessageSent = false; |
| 1180 | for (int i = 0; i < driList.size(); i++) { |
| 1181 | List<ChooserTarget> chooserTargets = new ArrayList<>(); |
| 1182 | for (int j = 0; j < resultList.size(); j++) { |
| 1183 | if (driList.get(i).getResolvedComponentName().equals( |
| 1184 | resultList.get(j).getTargetComponent())) { |
| 1185 | chooserTargets.add(convertToChooserTarget(resultList.get(j))); |
| 1186 | } |
| 1187 | } |
| 1188 | if (chooserTargets.isEmpty()) { |
| 1189 | continue; |
| 1190 | } |
| 1191 | final Message msg = Message.obtain(); |
| 1192 | msg.what = SHORTCUT_MANAGER_SHARE_TARGET_RESULT; |
| 1193 | msg.obj = new ServiceResultInfo(driList.get(i), chooserTargets, null); |
| 1194 | mChooserHandler.sendMessage(msg); |
| 1195 | resultMessageSent = true; |
| 1196 | } |
| 1197 | |
| 1198 | if (resultMessageSent) { |
| 1199 | final Message msg = Message.obtain(); |
| 1200 | msg.what = SHORTCUT_MANAGER_SHARE_TARGET_RESULT_COMPLETED; |
| 1201 | mChooserHandler.sendMessage(msg); |
| 1202 | } |
| 1203 | } |
| 1204 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1205 | private ChooserTarget convertToChooserTarget(ShortcutManager.ShareShortcutInfo shareShortcut) { |
| 1206 | ShortcutInfo shortcutInfo = shareShortcut.getShortcutInfo(); |
| 1207 | Bundle extras = new Bundle(); |
| 1208 | extras.putString(Intent.EXTRA_SHORTCUT_ID, shortcutInfo.getId()); |
| 1209 | return new ChooserTarget( |
| 1210 | // The name of this target. |
| 1211 | shortcutInfo.getShortLabel(), |
| 1212 | // Don't load the icon until it is selected to be shown |
| 1213 | null, |
| 1214 | // The ranking score for this target (0.0-1.0); the system will omit items with low |
| 1215 | // scores when there are too many Direct Share items. |
| 1216 | 0.5f, |
| 1217 | // The name of the component to be launched if this target is chosen. |
| 1218 | shareShortcut.getTargetComponent().clone(), |
| 1219 | // The extra values here will be merged into the Intent when this target is chosen. |
| 1220 | extras); |
| 1221 | } |
| 1222 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1223 | private String convertServiceName(String packageName, String serviceName) { |
| 1224 | if (TextUtils.isEmpty(serviceName)) { |
| 1225 | return null; |
| 1226 | } |
| 1227 | |
| 1228 | final String fullName; |
| 1229 | if (serviceName.startsWith(".")) { |
| 1230 | // Relative to the app package. Prepend the app package name. |
| 1231 | fullName = packageName + serviceName; |
| 1232 | } else if (serviceName.indexOf('.') >= 0) { |
| 1233 | // Fully qualified package name. |
| 1234 | fullName = serviceName; |
| 1235 | } else { |
| 1236 | fullName = null; |
| 1237 | } |
| 1238 | return fullName; |
| 1239 | } |
| 1240 | |
| 1241 | void unbindRemainingServices() { |
| 1242 | if (DEBUG) { |
| 1243 | Log.d(TAG, "unbindRemainingServices, " + mServiceConnections.size() + " left"); |
| 1244 | } |
| 1245 | for (int i = 0, N = mServiceConnections.size(); i < N; i++) { |
| 1246 | final ChooserTargetServiceConnection conn = mServiceConnections.get(i); |
| 1247 | if (DEBUG) Log.d(TAG, "unbinding " + conn); |
| 1248 | unbindService(conn); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 1249 | conn.destroy(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1250 | } |
| 1251 | mServiceConnections.clear(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1254 | public void onSetupVoiceInteraction() { |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 1255 | // Do nothing. We'll send the voice stuff ourselves. |
| 1256 | } |
| 1257 | |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 1258 | void updateModelAndChooserCounts(TargetInfo info) { |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1259 | if (info != null) { |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 1260 | if (USE_PREDICTION_MANAGER_FOR_DIRECT_TARGETS) { |
| 1261 | sendClickToAppPredictor(info); |
| 1262 | } |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1263 | final ResolveInfo ri = info.getResolveInfo(); |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 1264 | Intent targetIntent = getTargetIntent(); |
| 1265 | if (ri != null && ri.activityInfo != null && targetIntent != null) { |
Kang Li | 0cef910d | 2017-01-05 09:14:36 -0800 | [diff] [blame] | 1266 | if (mAdapter != null) { |
| 1267 | mAdapter.updateModel(info.getResolvedComponentName()); |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 1268 | mAdapter.updateChooserCounts(ri.activityInfo.packageName, getUserId(), |
| 1269 | targetIntent.getAction()); |
Kang Li | 0cef910d | 2017-01-05 09:14:36 -0800 | [diff] [blame] | 1270 | } |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1271 | if (DEBUG) { |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 1272 | Log.d(TAG, "ResolveInfo Package is " + ri.activityInfo.packageName); |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 1273 | Log.d(TAG, "Action to be updated is " + targetIntent.getAction()); |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1274 | } |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 1275 | } else if (DEBUG) { |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1276 | Log.d(TAG, "Can not log Chooser Counts of null ResovleInfo"); |
| 1277 | } |
| 1278 | } |
Kang Li | 9082f5b | 2016-12-02 10:56:21 -0800 | [diff] [blame] | 1279 | mIsSuccessfullySelected = true; |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 1282 | private void sendClickToAppPredictor(TargetInfo targetInfo) { |
| 1283 | if (!(targetInfo instanceof ChooserTargetInfo)) { |
| 1284 | return; |
| 1285 | } |
| 1286 | ChooserTarget chooserTarget = ((ChooserTargetInfo) targetInfo).getChooserTarget(); |
| 1287 | ComponentName componentName = chooserTarget.getComponentName(); |
| 1288 | Bundle extras = chooserTarget.getIntentExtras(); |
| 1289 | if (extras == null) { |
| 1290 | return; |
| 1291 | } |
| 1292 | String shortcutId = extras.getString(Intent.EXTRA_SHORTCUT_ID); |
| 1293 | if (shortcutId == null) { |
| 1294 | return; |
| 1295 | } |
| 1296 | mAppPredictor.notifyAppTargetEvent( |
| 1297 | new AppTargetEvent.Builder( |
| 1298 | new AppTarget( |
| 1299 | new AppTargetId(shortcutId), |
| 1300 | componentName.getPackageName(), |
| 1301 | componentName.getClassName(), |
| 1302 | getUser()), |
| 1303 | AppTargetEvent.ACTION_LAUNCH |
| 1304 | ).setLaunchLocation(LAUNCH_LOCATON_DIRECT_SHARE) |
| 1305 | .build()); |
| 1306 | } |
| 1307 | |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1308 | void onRefinementResult(TargetInfo selectedTarget, Intent matchingIntent) { |
| 1309 | if (mRefinementResultReceiver != null) { |
| 1310 | mRefinementResultReceiver.destroy(); |
| 1311 | mRefinementResultReceiver = null; |
| 1312 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1313 | if (selectedTarget == null) { |
| 1314 | Log.e(TAG, "Refinement result intent did not match any known targets; canceling"); |
| 1315 | } else if (!checkTargetSourceIntent(selectedTarget, matchingIntent)) { |
| 1316 | Log.e(TAG, "onRefinementResult: Selected target " + selectedTarget |
| 1317 | + " cannot match refined source intent " + matchingIntent); |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1318 | } else { |
| 1319 | TargetInfo clonedTarget = selectedTarget.cloneFilledIn(matchingIntent, 0); |
| 1320 | if (super.onTargetSelected(clonedTarget, false)) { |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 1321 | updateModelAndChooserCounts(clonedTarget); |
Kang Li | 53b4314 | 2016-11-14 14:38:25 -0800 | [diff] [blame] | 1322 | finish(); |
| 1323 | return; |
| 1324 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1325 | } |
| 1326 | onRefinementCanceled(); |
| 1327 | } |
| 1328 | |
| 1329 | void onRefinementCanceled() { |
| 1330 | if (mRefinementResultReceiver != null) { |
| 1331 | mRefinementResultReceiver.destroy(); |
| 1332 | mRefinementResultReceiver = null; |
| 1333 | } |
| 1334 | finish(); |
| 1335 | } |
| 1336 | |
| 1337 | boolean checkTargetSourceIntent(TargetInfo target, Intent matchingIntent) { |
| 1338 | final List<Intent> targetIntents = target.getAllSourceIntents(); |
| 1339 | for (int i = 0, N = targetIntents.size(); i < N; i++) { |
| 1340 | final Intent targetIntent = targetIntents.get(i); |
| 1341 | if (targetIntent.filterEquals(matchingIntent)) { |
| 1342 | return true; |
| 1343 | } |
| 1344 | } |
| 1345 | return false; |
| 1346 | } |
| 1347 | |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 1348 | void filterServiceTargets(String packageName, List<ChooserTarget> targets) { |
| 1349 | if (targets == null) { |
| 1350 | return; |
| 1351 | } |
| 1352 | |
| 1353 | final PackageManager pm = getPackageManager(); |
| 1354 | for (int i = targets.size() - 1; i >= 0; i--) { |
| 1355 | final ChooserTarget target = targets.get(i); |
| 1356 | final ComponentName targetName = target.getComponentName(); |
| 1357 | if (packageName != null && packageName.equals(targetName.getPackageName())) { |
| 1358 | // Anything from the original target's package is fine. |
| 1359 | continue; |
| 1360 | } |
| 1361 | |
| 1362 | boolean remove; |
| 1363 | try { |
| 1364 | final ActivityInfo ai = pm.getActivityInfo(targetName, 0); |
| 1365 | remove = !ai.exported || ai.permission != null; |
| 1366 | } catch (NameNotFoundException e) { |
| 1367 | Log.e(TAG, "Target " + target + " returned by " + packageName |
| 1368 | + " component not found"); |
| 1369 | remove = true; |
| 1370 | } |
| 1371 | |
| 1372 | if (remove) { |
| 1373 | targets.remove(i); |
| 1374 | } |
| 1375 | } |
| 1376 | } |
| 1377 | |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 1378 | protected MetricsLogger getMetricsLogger() { |
| 1379 | if (mMetricsLogger == null) { |
| 1380 | mMetricsLogger = new MetricsLogger(); |
| 1381 | } |
| 1382 | return mMetricsLogger; |
| 1383 | } |
| 1384 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1385 | public class ChooserListController extends ResolverListController { |
| 1386 | public ChooserListController(Context context, |
| 1387 | PackageManager pm, |
| 1388 | Intent targetIntent, |
| 1389 | String referrerPackageName, |
| 1390 | int launchedFromUid) { |
| 1391 | super(context, pm, targetIntent, referrerPackageName, launchedFromUid); |
| 1392 | } |
| 1393 | |
| 1394 | @Override |
| 1395 | boolean isComponentPinned(ComponentName name) { |
| 1396 | return mPinnedSharedPrefs.getBoolean(name.flattenToString(), false); |
| 1397 | } |
| 1398 | |
| 1399 | @Override |
| 1400 | boolean isComponentFiltered(ComponentName name) { |
| 1401 | if (mFilteredComponentNames == null) { |
| 1402 | return false; |
| 1403 | } |
| 1404 | for (ComponentName filteredComponentName : mFilteredComponentNames) { |
| 1405 | if (name.equals(filteredComponentName)) { |
| 1406 | return true; |
| 1407 | } |
| 1408 | } |
| 1409 | return false; |
| 1410 | } |
| 1411 | |
| 1412 | @Override |
| 1413 | public float getScore(DisplayResolveInfo target) { |
| 1414 | if (target == null) { |
| 1415 | return CALLER_TARGET_SCORE_BOOST; |
| 1416 | } |
| 1417 | float score = super.getScore(target); |
| 1418 | if (target.isPinned()) { |
| 1419 | score += PINNED_TARGET_SCORE_BOOST; |
| 1420 | } |
| 1421 | return score; |
| 1422 | } |
| 1423 | } |
| 1424 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1425 | @Override |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1426 | public ResolveListAdapter createAdapter(Context context, List<Intent> payloadIntents, |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1427 | Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid, |
| 1428 | boolean filterLastUsed) { |
| 1429 | final ChooserListAdapter adapter = new ChooserListAdapter(context, payloadIntents, |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1430 | initialIntents, rList, launchedFromUid, filterLastUsed, createListController()); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1431 | return adapter; |
| 1432 | } |
| 1433 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1434 | @VisibleForTesting |
| 1435 | protected ResolverListController createListController() { |
| 1436 | return new ChooserListController( |
| 1437 | this, |
| 1438 | mPm, |
| 1439 | getTargetIntent(), |
| 1440 | getReferrerPackageName(), |
| 1441 | mLaunchedFromUid); |
| 1442 | } |
| 1443 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1444 | @VisibleForTesting |
| 1445 | protected Bitmap loadThumbnail(Uri uri, Size size) { |
| 1446 | if (uri == null || size == null) { |
| 1447 | return null; |
| 1448 | } |
| 1449 | |
| 1450 | try { |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 1451 | return ImageUtils.loadThumbnail(getContentResolver(), uri, size); |
| 1452 | } catch (IOException | NullPointerException | SecurityException ex) { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1453 | Log.w(TAG, "Error loading preview thumbnail for uri: " + uri.toString(), ex); |
| 1454 | } |
| 1455 | return null; |
| 1456 | } |
| 1457 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1458 | interface ChooserTargetInfo extends TargetInfo { |
| 1459 | float getModifiedScore(); |
| 1460 | |
| 1461 | ChooserTarget getChooserTarget(); |
| 1462 | } |
| 1463 | |
| 1464 | /** |
| 1465 | * Distinguish between targets that selectable by the user, vs those that are |
| 1466 | * placeholders for the system while information is loading in an async manner. |
| 1467 | */ |
| 1468 | abstract class NotSelectableTargetInfo implements ChooserTargetInfo { |
| 1469 | |
| 1470 | public Intent getResolvedIntent() { |
| 1471 | return null; |
| 1472 | } |
| 1473 | |
| 1474 | public ComponentName getResolvedComponentName() { |
| 1475 | return null; |
| 1476 | } |
| 1477 | |
| 1478 | public boolean start(Activity activity, Bundle options) { |
| 1479 | return false; |
| 1480 | } |
| 1481 | |
| 1482 | public boolean startAsCaller(ResolverActivity activity, Bundle options, int userId) { |
| 1483 | return false; |
| 1484 | } |
| 1485 | |
| 1486 | public boolean startAsUser(Activity activity, Bundle options, UserHandle user) { |
| 1487 | return false; |
| 1488 | } |
| 1489 | |
| 1490 | public ResolveInfo getResolveInfo() { |
| 1491 | return null; |
| 1492 | } |
| 1493 | |
| 1494 | public CharSequence getDisplayLabel() { |
| 1495 | return null; |
| 1496 | } |
| 1497 | |
| 1498 | public CharSequence getExtendedInfo() { |
| 1499 | return null; |
| 1500 | } |
| 1501 | |
| 1502 | public Drawable getBadgeIcon() { |
| 1503 | return null; |
| 1504 | } |
| 1505 | |
| 1506 | public CharSequence getBadgeContentDescription() { |
| 1507 | return null; |
| 1508 | } |
| 1509 | |
| 1510 | public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) { |
| 1511 | return null; |
| 1512 | } |
| 1513 | |
| 1514 | public List<Intent> getAllSourceIntents() { |
| 1515 | return null; |
| 1516 | } |
| 1517 | |
| 1518 | public boolean isPinned() { |
| 1519 | return false; |
| 1520 | } |
| 1521 | |
| 1522 | public float getModifiedScore() { |
| 1523 | return 0.1f; |
| 1524 | } |
| 1525 | |
| 1526 | public ChooserTarget getChooserTarget() { |
| 1527 | return null; |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | final class PlaceHolderTargetInfo extends NotSelectableTargetInfo { |
| 1532 | public Drawable getDisplayIcon() { |
| 1533 | return getDrawable(R.drawable.resolver_icon_placeholder); |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | |
| 1538 | final class EmptyTargetInfo extends NotSelectableTargetInfo { |
| 1539 | public Drawable getDisplayIcon() { |
| 1540 | return null; |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | final class SelectableTargetInfo implements ChooserTargetInfo { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1545 | private final DisplayResolveInfo mSourceInfo; |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 1546 | private final ResolveInfo mBackupResolveInfo; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1547 | private final ChooserTarget mChooserTarget; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1548 | private Drawable mBadgeIcon = null; |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 1549 | private CharSequence mBadgeContentDescription; |
Adam Powell | 13036be | 2015-05-12 14:43:56 -0700 | [diff] [blame] | 1550 | private Drawable mDisplayIcon; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1551 | private final Intent mFillInIntent; |
| 1552 | private final int mFillInFlags; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1553 | private final float mModifiedScore; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1554 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1555 | SelectableTargetInfo(DisplayResolveInfo sourceInfo, ChooserTarget chooserTarget, |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1556 | float modifiedScore) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1557 | mSourceInfo = sourceInfo; |
| 1558 | mChooserTarget = chooserTarget; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1559 | mModifiedScore = modifiedScore; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1560 | if (sourceInfo != null) { |
| 1561 | final ResolveInfo ri = sourceInfo.getResolveInfo(); |
| 1562 | if (ri != null) { |
| 1563 | final ActivityInfo ai = ri.activityInfo; |
| 1564 | if (ai != null && ai.applicationInfo != null) { |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 1565 | final PackageManager pm = getPackageManager(); |
| 1566 | mBadgeIcon = pm.getApplicationIcon(ai.applicationInfo); |
| 1567 | mBadgeContentDescription = pm.getApplicationLabel(ai.applicationInfo); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | } |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1571 | // TODO(b/121287224): do this in the background thread, and only for selected targets |
| 1572 | mDisplayIcon = getChooserTargetIconDrawable(chooserTarget); |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 1573 | |
| 1574 | if (sourceInfo != null) { |
| 1575 | mBackupResolveInfo = null; |
| 1576 | } else { |
| 1577 | mBackupResolveInfo = getPackageManager().resolveActivity(getResolvedIntent(), 0); |
| 1578 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1579 | |
| 1580 | mFillInIntent = null; |
| 1581 | mFillInFlags = 0; |
| 1582 | } |
| 1583 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1584 | private SelectableTargetInfo(SelectableTargetInfo other, Intent fillInIntent, int flags) { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1585 | mSourceInfo = other.mSourceInfo; |
| 1586 | mBackupResolveInfo = other.mBackupResolveInfo; |
| 1587 | mChooserTarget = other.mChooserTarget; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1588 | mBadgeIcon = other.mBadgeIcon; |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 1589 | mBadgeContentDescription = other.mBadgeContentDescription; |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1590 | mDisplayIcon = other.mDisplayIcon; |
| 1591 | mFillInIntent = fillInIntent; |
| 1592 | mFillInFlags = flags; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1593 | mModifiedScore = other.mModifiedScore; |
| 1594 | } |
| 1595 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1596 | /** |
| 1597 | * Since ShortcutInfos are returned by ShortcutManager, we can cache the shortcuts and skip |
| 1598 | * the call to LauncherApps#getShortcuts(ShortcutQuery). |
| 1599 | */ |
| 1600 | // TODO(121287224): Refactor code to apply the suggestion above |
| 1601 | private Drawable getChooserTargetIconDrawable(ChooserTarget target) { |
| 1602 | final Icon icon = target.getIcon(); |
| 1603 | if (icon != null) { |
| 1604 | return icon.loadDrawable(ChooserActivity.this); |
| 1605 | } |
| 1606 | if (!USE_SHORTCUT_MANAGER_FOR_DIRECT_TARGETS) { |
| 1607 | return null; |
| 1608 | } |
| 1609 | |
| 1610 | Bundle extras = target.getIntentExtras(); |
| 1611 | if (extras == null || !extras.containsKey(Intent.EXTRA_SHORTCUT_ID)) { |
| 1612 | return null; |
| 1613 | } |
| 1614 | CharSequence shortcutId = extras.getCharSequence(Intent.EXTRA_SHORTCUT_ID); |
| 1615 | LauncherApps launcherApps = (LauncherApps) getSystemService( |
| 1616 | Context.LAUNCHER_APPS_SERVICE); |
| 1617 | final LauncherApps.ShortcutQuery q = new LauncherApps.ShortcutQuery(); |
| 1618 | q.setPackage(target.getComponentName().getPackageName()); |
| 1619 | q.setShortcutIds(Arrays.asList(shortcutId.toString())); |
| 1620 | q.setQueryFlags(LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC); |
| 1621 | final List<ShortcutInfo> shortcuts = launcherApps.getShortcuts(q, getUser()); |
| 1622 | if (shortcuts != null && shortcuts.size() > 0) { |
| 1623 | return launcherApps.getShortcutIconDrawable(shortcuts.get(0), 0); |
| 1624 | } |
| 1625 | |
| 1626 | return null; |
| 1627 | } |
| 1628 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1629 | public float getModifiedScore() { |
| 1630 | return mModifiedScore; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | @Override |
| 1634 | public Intent getResolvedIntent() { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1635 | if (mSourceInfo != null) { |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 1636 | return mSourceInfo.getResolvedIntent(); |
| 1637 | } |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 1638 | |
| 1639 | final Intent targetIntent = new Intent(getTargetIntent()); |
| 1640 | targetIntent.setComponent(mChooserTarget.getComponentName()); |
| 1641 | targetIntent.putExtras(mChooserTarget.getIntentExtras()); |
| 1642 | return targetIntent; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | @Override |
| 1646 | public ComponentName getResolvedComponentName() { |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 1647 | if (mSourceInfo != null) { |
| 1648 | return mSourceInfo.getResolvedComponentName(); |
| 1649 | } else if (mBackupResolveInfo != null) { |
| 1650 | return new ComponentName(mBackupResolveInfo.activityInfo.packageName, |
| 1651 | mBackupResolveInfo.activityInfo.name); |
| 1652 | } |
| 1653 | return null; |
| 1654 | } |
| 1655 | |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 1656 | private Intent getBaseIntentToSend() { |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 1657 | Intent result = getResolvedIntent(); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1658 | if (result == null) { |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 1659 | Log.e(TAG, "ChooserTargetInfo: no base intent available to send"); |
Adam Powell | 13036be | 2015-05-12 14:43:56 -0700 | [diff] [blame] | 1660 | } else { |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1661 | result = new Intent(result); |
Adam Powell | 13036be | 2015-05-12 14:43:56 -0700 | [diff] [blame] | 1662 | if (mFillInIntent != null) { |
| 1663 | result.fillIn(mFillInIntent, mFillInFlags); |
| 1664 | } |
| 1665 | result.fillIn(mReferrerFillInIntent, 0); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1666 | } |
| 1667 | return result; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | @Override |
| 1671 | public boolean start(Activity activity, Bundle options) { |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 1672 | throw new RuntimeException("ChooserTargets should be started as caller."); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | @Override |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 1676 | public boolean startAsCaller(ResolverActivity activity, Bundle options, int userId) { |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 1677 | final Intent intent = getBaseIntentToSend(); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1678 | if (intent == null) { |
| 1679 | return false; |
| 1680 | } |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 1681 | intent.setComponent(mChooserTarget.getComponentName()); |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 1682 | intent.putExtras(mChooserTarget.getIntentExtras()); |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 1683 | |
| 1684 | // Important: we will ignore the target security checks in ActivityManager |
| 1685 | // if and only if the ChooserTarget's target package is the same package |
| 1686 | // where we got the ChooserTargetService that provided it. This lets a |
| 1687 | // ChooserTargetService provide a non-exported or permission-guarded target |
| 1688 | // to the chooser for the user to pick. |
| 1689 | // |
| 1690 | // If mSourceInfo is null, we got this ChooserTarget from the caller or elsewhere |
| 1691 | // so we'll obey the caller's normal security checks. |
| 1692 | final boolean ignoreTargetSecurity = mSourceInfo != null |
| 1693 | && mSourceInfo.getResolvedComponentName().getPackageName() |
| 1694 | .equals(mChooserTarget.getComponentName().getPackageName()); |
Alison Cichowlas | 3e34050 | 2018-08-07 17:15:01 -0400 | [diff] [blame] | 1695 | return activity.startAsCallerImpl(intent, options, ignoreTargetSecurity, userId); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | @Override |
| 1699 | public boolean startAsUser(Activity activity, Bundle options, UserHandle user) { |
Adam Powell | 666d82a | 2015-07-15 20:14:57 -0700 | [diff] [blame] | 1700 | throw new RuntimeException("ChooserTargets should be started as caller."); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | @Override |
| 1704 | public ResolveInfo getResolveInfo() { |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 1705 | return mSourceInfo != null ? mSourceInfo.getResolveInfo() : mBackupResolveInfo; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | @Override |
| 1709 | public CharSequence getDisplayLabel() { |
| 1710 | return mChooserTarget.getTitle(); |
| 1711 | } |
| 1712 | |
| 1713 | @Override |
| 1714 | public CharSequence getExtendedInfo() { |
Adam Powell | 00f4aad | 2015-09-17 13:38:16 -0700 | [diff] [blame] | 1715 | // ChooserTargets have badge icons, so we won't show the extended info to disambiguate. |
| 1716 | return null; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | @Override |
| 1720 | public Drawable getDisplayIcon() { |
| 1721 | return mDisplayIcon; |
| 1722 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1723 | |
| 1724 | @Override |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1725 | public Drawable getBadgeIcon() { |
| 1726 | return mBadgeIcon; |
| 1727 | } |
| 1728 | |
| 1729 | @Override |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 1730 | public CharSequence getBadgeContentDescription() { |
| 1731 | return mBadgeContentDescription; |
| 1732 | } |
| 1733 | |
George Hodulik | f2b0d34 | 2019-01-25 12:43:54 -0800 | [diff] [blame] | 1734 | public ChooserTarget getChooserTarget() { |
| 1735 | return mChooserTarget; |
| 1736 | } |
| 1737 | |
Alan Viverette | ce5d92c | 2015-07-31 16:46:56 -0400 | [diff] [blame] | 1738 | @Override |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1739 | public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1740 | return new SelectableTargetInfo(this, fillInIntent, flags); |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | @Override |
| 1744 | public List<Intent> getAllSourceIntents() { |
| 1745 | final List<Intent> results = new ArrayList<>(); |
| 1746 | if (mSourceInfo != null) { |
| 1747 | // We only queried the service for the first one in our sourceinfo. |
| 1748 | results.add(mSourceInfo.getAllSourceIntents().get(0)); |
| 1749 | } |
| 1750 | return results; |
| 1751 | } |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1752 | |
| 1753 | @Override |
| 1754 | public boolean isPinned() { |
| 1755 | return mSourceInfo != null ? mSourceInfo.isPinned() : false; |
| 1756 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | public class ChooserListAdapter extends ResolveListAdapter { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1760 | public static final int TARGET_BAD = -1; |
| 1761 | public static final int TARGET_CALLER = 0; |
| 1762 | public static final int TARGET_SERVICE = 1; |
| 1763 | public static final int TARGET_STANDARD = 2; |
| 1764 | |
Dan Sandler | 62aad00 | 2018-05-23 02:13:51 -0400 | [diff] [blame] | 1765 | private static final int MAX_SERVICE_TARGETS = 4; |
Dan Sandler | f5e1769 | 2018-06-04 22:13:40 -0400 | [diff] [blame] | 1766 | private static final int MAX_TARGETS_PER_SERVICE = 2; |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1767 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1768 | // Reserve spots for incoming direct share targets by adding placeholders |
| 1769 | private ChooserTargetInfo mPlaceHolderTargetInfo = new PlaceHolderTargetInfo(); |
| 1770 | private List<ChooserTargetInfo> mServiceTargets; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1771 | private final List<TargetInfo> mCallerTargets = new ArrayList<>(); |
Adam Powell | 565943f | 2016-02-11 10:29:05 -0800 | [diff] [blame] | 1772 | private boolean mShowServiceTargets; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1773 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1774 | private float mLateFee = 1.f; |
| 1775 | |
Dan Sandler | f5e1769 | 2018-06-04 22:13:40 -0400 | [diff] [blame] | 1776 | private boolean mTargetsNeedPruning = false; |
| 1777 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1778 | private final BaseChooserTargetComparator mBaseTargetComparator |
| 1779 | = new BaseChooserTargetComparator(); |
| 1780 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1781 | public ChooserListAdapter(Context context, List<Intent> payloadIntents, |
| 1782 | Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid, |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1783 | boolean filterLastUsed, ResolverListController resolverListController) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1784 | // Don't send the initial intents through the shared ResolverActivity path, |
| 1785 | // we want to separate them into a different section. |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1786 | super(context, payloadIntents, null, rList, launchedFromUid, filterLastUsed, |
| 1787 | resolverListController); |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 1788 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1789 | mServiceTargets = createPlaceHolders(); |
| 1790 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1791 | if (initialIntents != null) { |
| 1792 | final PackageManager pm = getPackageManager(); |
| 1793 | for (int i = 0; i < initialIntents.length; i++) { |
| 1794 | final Intent ii = initialIntents[i]; |
| 1795 | if (ii == null) { |
| 1796 | continue; |
| 1797 | } |
Adam Powell | 86100d1 | 2016-05-12 16:13:17 -0700 | [diff] [blame] | 1798 | |
| 1799 | // We reimplement Intent#resolveActivityInfo here because if we have an |
| 1800 | // implicit intent, we want the ResolveInfo returned by PackageManager |
| 1801 | // instead of one we reconstruct ourselves. The ResolveInfo returned might |
| 1802 | // have extra metadata and resolvePackageName set and we want to respect that. |
| 1803 | ResolveInfo ri = null; |
| 1804 | ActivityInfo ai = null; |
| 1805 | final ComponentName cn = ii.getComponent(); |
| 1806 | if (cn != null) { |
| 1807 | try { |
| 1808 | ai = pm.getActivityInfo(ii.getComponent(), 0); |
| 1809 | ri = new ResolveInfo(); |
| 1810 | ri.activityInfo = ai; |
| 1811 | } catch (PackageManager.NameNotFoundException ignored) { |
| 1812 | // ai will == null below |
| 1813 | } |
| 1814 | } |
| 1815 | if (ai == null) { |
| 1816 | ri = pm.resolveActivity(ii, PackageManager.MATCH_DEFAULT_ONLY); |
| 1817 | ai = ri != null ? ri.activityInfo : null; |
| 1818 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1819 | if (ai == null) { |
| 1820 | Log.w(TAG, "No activity found for " + ii); |
| 1821 | continue; |
| 1822 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1823 | UserManager userManager = |
| 1824 | (UserManager) getSystemService(Context.USER_SERVICE); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1825 | if (ii instanceof LabeledIntent) { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1826 | LabeledIntent li = (LabeledIntent) ii; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1827 | ri.resolvePackageName = li.getSourcePackage(); |
| 1828 | ri.labelRes = li.getLabelResource(); |
| 1829 | ri.nonLocalizedLabel = li.getNonLocalizedLabel(); |
| 1830 | ri.icon = li.getIconResource(); |
Sudheer Shanka | 9ded760 | 2015-05-19 21:17:25 +0100 | [diff] [blame] | 1831 | ri.iconResourceId = ri.icon; |
| 1832 | } |
| 1833 | if (userManager.isManagedProfile()) { |
| 1834 | ri.noResourceId = true; |
| 1835 | ri.icon = 0; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1836 | } |
| 1837 | mCallerTargets.add(new DisplayResolveInfo(ii, ri, |
| 1838 | ri.loadLabel(pm), null, ii)); |
Adam Powell | d974c7b | 2015-04-28 15:41:46 -0700 | [diff] [blame] | 1839 | } |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 1840 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1843 | private List<ChooserTargetInfo> createPlaceHolders() { |
| 1844 | List<ChooserTargetInfo> list = new ArrayList<>(); |
| 1845 | for (int i = 0; i < MAX_SERVICE_TARGETS; i++) { |
| 1846 | list.add(mPlaceHolderTargetInfo); |
| 1847 | } |
| 1848 | return list; |
| 1849 | } |
| 1850 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1851 | @Override |
| 1852 | public boolean showsExtendedInfo(TargetInfo info) { |
Adam Powell | 00f4aad | 2015-09-17 13:38:16 -0700 | [diff] [blame] | 1853 | // We have badges so we don't need this text shown. |
| 1854 | return false; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | @Override |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 1858 | public boolean isComponentPinned(ComponentName name) { |
| 1859 | return mPinnedSharedPrefs.getBoolean(name.flattenToString(), false); |
| 1860 | } |
| 1861 | |
| 1862 | @Override |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1863 | public View onCreateView(ViewGroup parent) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1864 | return mInflater.inflate( |
| 1865 | com.android.internal.R.layout.resolve_grid_item, parent, false); |
| 1866 | } |
| 1867 | |
| 1868 | @Override |
| 1869 | public void onListRebuilt() { |
Ng Zhi An | d3ec5fc | 2018-05-10 09:13:00 -0700 | [diff] [blame] | 1870 | // don't support direct share on low ram devices |
| 1871 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 1872 | return; |
| 1873 | } |
| 1874 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1875 | if (mServiceTargets != null) { |
Dan Sandler | f5e1769 | 2018-06-04 22:13:40 -0400 | [diff] [blame] | 1876 | if (getDisplayInfoCount() == 0) { |
| 1877 | // b/109676071: When packages change, onListRebuilt() is called before |
| 1878 | // ResolverActivity.mDisplayList is re-populated; pruning now would cause the |
| 1879 | // list to disappear briefly, so instead we detect this case (the |
| 1880 | // set of targets suddenly dropping to zero) and remember to prune later. |
| 1881 | mTargetsNeedPruning = true; |
| 1882 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1883 | } |
Mehdi Alizadeh | 8e248ad | 2019-01-17 11:41:49 -0800 | [diff] [blame] | 1884 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1885 | if (USE_SHORTCUT_MANAGER_FOR_DIRECT_TARGETS) { |
| 1886 | if (DEBUG) { |
| 1887 | Log.d(TAG, "querying direct share targets from ShortcutManager"); |
| 1888 | } |
| 1889 | queryDirectShareTargets(this); |
Mehdi Alizadeh | 8e248ad | 2019-01-17 11:41:49 -0800 | [diff] [blame] | 1890 | } |
| 1891 | if (USE_CHOOSER_TARGET_SERVICE_FOR_DIRECT_TARGETS) { |
| 1892 | if (DEBUG) { |
| 1893 | Log.d(TAG, "List built querying services"); |
| 1894 | } |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 1895 | queryTargetServices(this); |
| 1896 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1897 | } |
| 1898 | |
| 1899 | @Override |
Adam Powell | c6d5e3a | 2015-04-23 12:22:20 -0700 | [diff] [blame] | 1900 | public boolean shouldGetResolvedFilter() { |
| 1901 | return true; |
| 1902 | } |
| 1903 | |
| 1904 | @Override |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1905 | public int getCount() { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1906 | return super.getCount() + getSelectableServiceTargetCount() + getCallerTargetCount(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1907 | } |
| 1908 | |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 1909 | @Override |
| 1910 | public int getUnfilteredCount() { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1911 | return super.getUnfilteredCount() + getSelectableServiceTargetCount() |
| 1912 | + getCallerTargetCount(); |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | public int getCallerTargetCount() { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1916 | return mCallerTargets.size(); |
| 1917 | } |
| 1918 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1919 | /** |
| 1920 | * Filter out placeholders and non-selectable service targets |
| 1921 | */ |
| 1922 | public int getSelectableServiceTargetCount() { |
| 1923 | int count = 0; |
| 1924 | for (ChooserTargetInfo info : mServiceTargets) { |
| 1925 | if (info instanceof SelectableTargetInfo) { |
| 1926 | count++; |
| 1927 | } |
Adam Powell | 565943f | 2016-02-11 10:29:05 -0800 | [diff] [blame] | 1928 | } |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1929 | return count; |
| 1930 | } |
| 1931 | |
| 1932 | public int getServiceTargetCount() { |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1933 | return Math.min(mServiceTargets.size(), MAX_SERVICE_TARGETS); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | public int getStandardTargetCount() { |
| 1937 | return super.getCount(); |
| 1938 | } |
| 1939 | |
| 1940 | public int getPositionTargetType(int position) { |
| 1941 | int offset = 0; |
| 1942 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1943 | final int callerTargetCount = getCallerTargetCount(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1944 | if (position < callerTargetCount) { |
| 1945 | return TARGET_CALLER; |
| 1946 | } |
| 1947 | offset += callerTargetCount; |
| 1948 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1949 | final int serviceTargetCount = getServiceTargetCount(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 1950 | if (position - offset < serviceTargetCount) { |
| 1951 | return TARGET_SERVICE; |
| 1952 | } |
| 1953 | offset += serviceTargetCount; |
| 1954 | |
| 1955 | final int standardTargetCount = super.getCount(); |
| 1956 | if (position - offset < standardTargetCount) { |
| 1957 | return TARGET_STANDARD; |
| 1958 | } |
| 1959 | |
| 1960 | return TARGET_BAD; |
| 1961 | } |
| 1962 | |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1963 | @Override |
| 1964 | public TargetInfo getItem(int position) { |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 1965 | return targetInfoForPosition(position, true); |
| 1966 | } |
| 1967 | |
| 1968 | @Override |
| 1969 | public TargetInfo targetInfoForPosition(int position, boolean filtered) { |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1970 | int offset = 0; |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 1971 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 1972 | final int callerTargetCount = getCallerTargetCount(); |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 1973 | if (position < callerTargetCount) { |
| 1974 | return mCallerTargets.get(position); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1975 | } |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 1976 | offset += callerTargetCount; |
| 1977 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1978 | final int serviceTargetCount = filtered ? getServiceTargetCount() : |
| 1979 | getSelectableServiceTargetCount(); |
Adam Powell | 0ccc0e9 | 2015-04-23 17:19:37 -0700 | [diff] [blame] | 1980 | if (position - offset < serviceTargetCount) { |
| 1981 | return mServiceTargets.get(position - offset); |
| 1982 | } |
| 1983 | offset += serviceTargetCount; |
| 1984 | |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 1985 | return filtered ? super.getItem(position - offset) |
| 1986 | : getDisplayInfoAt(position - offset); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | public void addServiceResults(DisplayResolveInfo origTarget, List<ChooserTarget> targets) { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1990 | if (DEBUG) { |
| 1991 | Log.d(TAG, "addServiceResults " + origTarget + ", " + targets.size() |
| 1992 | + " targets"); |
| 1993 | } |
Dan Sandler | f5e1769 | 2018-06-04 22:13:40 -0400 | [diff] [blame] | 1994 | |
| 1995 | if (mTargetsNeedPruning && targets.size() > 0) { |
| 1996 | // First proper update since we got an onListRebuilt() with (transient) 0 items. |
| 1997 | // Clear out the target list and rebuild. |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1998 | mServiceTargets = createPlaceHolders(); |
Dan Sandler | f5e1769 | 2018-06-04 22:13:40 -0400 | [diff] [blame] | 1999 | mTargetsNeedPruning = false; |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2000 | |
| 2001 | // Add back any app-supplied direct share targets that may have been |
| 2002 | // wiped by this clear |
| 2003 | if (mCallerChooserTargets != null) { |
| 2004 | addServiceResults(null, Lists.newArrayList(mCallerChooserTargets)); |
| 2005 | } |
Dan Sandler | f5e1769 | 2018-06-04 22:13:40 -0400 | [diff] [blame] | 2006 | } |
| 2007 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2008 | final float parentScore = getScore(origTarget); |
| 2009 | Collections.sort(targets, mBaseTargetComparator); |
| 2010 | float lastScore = 0; |
Adam Powell | bba0030 | 2016-02-03 16:01:09 -0800 | [diff] [blame] | 2011 | for (int i = 0, N = Math.min(targets.size(), MAX_TARGETS_PER_SERVICE); i < N; i++) { |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2012 | final ChooserTarget target = targets.get(i); |
| 2013 | float targetScore = target.getScore(); |
| 2014 | targetScore *= parentScore; |
| 2015 | targetScore *= mLateFee; |
| 2016 | if (i > 0 && targetScore >= lastScore) { |
| 2017 | // Apply a decay so that the top app can't crowd out everything else. |
| 2018 | // This incents ChooserTargetServices to define what's truly better. |
| 2019 | targetScore = lastScore * 0.95f; |
| 2020 | } |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2021 | insertServiceTarget(new SelectableTargetInfo(origTarget, target, targetScore)); |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2022 | |
| 2023 | if (DEBUG) { |
| 2024 | Log.d(TAG, " => " + target.toString() + " score=" + targetScore |
| 2025 | + " base=" + target.getScore() |
| 2026 | + " lastScore=" + lastScore |
| 2027 | + " parentScore=" + parentScore |
| 2028 | + " lateFee=" + mLateFee); |
| 2029 | } |
| 2030 | |
| 2031 | lastScore = targetScore; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2032 | } |
| 2033 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2034 | mLateFee *= 0.95f; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2035 | |
| 2036 | notifyDataSetChanged(); |
| 2037 | } |
| 2038 | |
Adam Powell | 565943f | 2016-02-11 10:29:05 -0800 | [diff] [blame] | 2039 | /** |
| 2040 | * Set to true to reveal all service targets at once. |
| 2041 | */ |
| 2042 | public void setShowServiceTargets(boolean show) { |
Susi Kharraz-Post | fcec993 | 2019-03-01 16:46:26 -0500 | [diff] [blame] | 2043 | // mShowServiceTargets is only flipped once to show direct share targets. But after the |
| 2044 | // initial display the list can be re-sorted and the user will see the target list |
| 2045 | // change. This will log the initial show and the subsequent shuffle to help us get |
| 2046 | // accurate timing of the UX. |
| 2047 | if (show) { |
| 2048 | getMetricsLogger().write( |
| 2049 | new LogMaker(MetricsEvent.ACTION_ACTIVITY_CHOOSER_SHOWN_DIRECT_TARGET) |
| 2050 | .setSubtype(mShowServiceTargets ? MetricsEvent.PREVIOUSLY_VISIBLE |
| 2051 | : MetricsEvent.PREVIOUSLY_HIDDEN)); |
| 2052 | } |
Adam Powell | 08adbfe | 2017-05-10 07:48:30 -0700 | [diff] [blame] | 2053 | if (show != mShowServiceTargets) { |
| 2054 | mShowServiceTargets = show; |
| 2055 | notifyDataSetChanged(); |
| 2056 | } |
Adam Powell | 565943f | 2016-02-11 10:29:05 -0800 | [diff] [blame] | 2057 | } |
| 2058 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2059 | /** |
| 2060 | * Calling this marks service target loading complete, and will attempt to no longer |
| 2061 | * update the direct share area. |
| 2062 | */ |
| 2063 | public void completeServiceTargetLoading() { |
| 2064 | mServiceTargets.removeIf(o -> o instanceof PlaceHolderTargetInfo); |
| 2065 | |
| 2066 | if (mServiceTargets.isEmpty()) { |
| 2067 | mServiceTargets.add(new EmptyTargetInfo()); |
| 2068 | } |
| 2069 | notifyDataSetChanged(); |
| 2070 | } |
| 2071 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2072 | private void insertServiceTarget(ChooserTargetInfo chooserTargetInfo) { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2073 | // Avoid inserting any potentially late results |
| 2074 | if (mServiceTargets.size() == 1 |
| 2075 | && mServiceTargets.get(0) instanceof EmptyTargetInfo) { |
| 2076 | return; |
| 2077 | } |
| 2078 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2079 | final float newScore = chooserTargetInfo.getModifiedScore(); |
| 2080 | for (int i = 0, N = mServiceTargets.size(); i < N; i++) { |
| 2081 | final ChooserTargetInfo serviceTarget = mServiceTargets.get(i); |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2082 | if (serviceTarget == null) { |
| 2083 | mServiceTargets.set(i, chooserTargetInfo); |
| 2084 | return; |
| 2085 | } else if (newScore > serviceTarget.getModifiedScore()) { |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2086 | mServiceTargets.add(i, chooserTargetInfo); |
| 2087 | return; |
| 2088 | } |
| 2089 | } |
| 2090 | mServiceTargets.add(chooserTargetInfo); |
| 2091 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2094 | static class BaseChooserTargetComparator implements Comparator<ChooserTarget> { |
| 2095 | @Override |
| 2096 | public int compare(ChooserTarget lhs, ChooserTarget rhs) { |
| 2097 | // Descending order |
Adam Powell | 77a533f | 2015-10-16 10:47:32 -0700 | [diff] [blame] | 2098 | return (int) Math.signum(rhs.getScore() - lhs.getScore()); |
Adam Powell | a182e45 | 2015-07-06 16:57:56 -0700 | [diff] [blame] | 2099 | } |
| 2100 | } |
| 2101 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2102 | class ChooserRowAdapter extends BaseAdapter { |
| 2103 | private ChooserListAdapter mChooserListAdapter; |
| 2104 | private final LayoutInflater mLayoutInflater; |
| 2105 | private final int mColumnCount = 4; |
Adam Powell | 08adbfe | 2017-05-10 07:48:30 -0700 | [diff] [blame] | 2106 | private int mAnimationCount = 0; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2107 | |
| 2108 | public ChooserRowAdapter(ChooserListAdapter wrappedAdapter) { |
| 2109 | mChooserListAdapter = wrappedAdapter; |
| 2110 | mLayoutInflater = LayoutInflater.from(ChooserActivity.this); |
| 2111 | |
| 2112 | wrappedAdapter.registerDataSetObserver(new DataSetObserver() { |
| 2113 | @Override |
| 2114 | public void onChanged() { |
| 2115 | super.onChanged(); |
| 2116 | notifyDataSetChanged(); |
| 2117 | } |
| 2118 | |
| 2119 | @Override |
| 2120 | public void onInvalidated() { |
| 2121 | super.onInvalidated(); |
| 2122 | notifyDataSetInvalidated(); |
| 2123 | } |
| 2124 | }); |
| 2125 | } |
| 2126 | |
| 2127 | @Override |
| 2128 | public int getCount() { |
| 2129 | return (int) ( |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2130 | getCallerTargetRowCount() |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 2131 | + getServiceTargetRowCount() |
| 2132 | + Math.ceil( |
| 2133 | (float) mChooserListAdapter.getStandardTargetCount() / mColumnCount) |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2134 | ); |
| 2135 | } |
| 2136 | |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2137 | public int getCallerTargetRowCount() { |
| 2138 | return (int) Math.ceil( |
| 2139 | (float) mChooserListAdapter.getCallerTargetCount() / mColumnCount); |
| 2140 | } |
| 2141 | |
Dan Sandler | 62aad00 | 2018-05-23 02:13:51 -0400 | [diff] [blame] | 2142 | // There can be at most one row of service targets. |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2143 | public int getServiceTargetRowCount() { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2144 | return 1; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2147 | @Override |
| 2148 | public Object getItem(int position) { |
| 2149 | // We have nothing useful to return here. |
| 2150 | return position; |
| 2151 | } |
| 2152 | |
| 2153 | @Override |
| 2154 | public long getItemId(int position) { |
| 2155 | return position; |
| 2156 | } |
| 2157 | |
| 2158 | @Override |
| 2159 | public View getView(int position, View convertView, ViewGroup parent) { |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2160 | final RowViewHolder holder; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2161 | if (convertView == null) { |
| 2162 | holder = createViewHolder(parent); |
| 2163 | } else { |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2164 | holder = (RowViewHolder) convertView.getTag(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2165 | } |
| 2166 | bindViewHolder(position, holder); |
| 2167 | |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2168 | return holder.row; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2171 | RowViewHolder createViewHolder(ViewGroup parent) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2172 | final ViewGroup row = (ViewGroup) mLayoutInflater.inflate(R.layout.chooser_row, |
| 2173 | parent, false); |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2174 | final RowViewHolder holder = new RowViewHolder(row, mColumnCount); |
| 2175 | final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 2176 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2177 | for (int i = 0; i < mColumnCount; i++) { |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2178 | final View v = mChooserListAdapter.createView(row); |
Adam Powell | 4eb9871 | 2015-10-14 13:10:18 -0700 | [diff] [blame] | 2179 | final int column = i; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2180 | v.setOnClickListener(new OnClickListener() { |
| 2181 | @Override |
| 2182 | public void onClick(View v) { |
Adam Powell | 4eb9871 | 2015-10-14 13:10:18 -0700 | [diff] [blame] | 2183 | startSelected(holder.itemIndices[column], false, true); |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2184 | } |
| 2185 | }); |
| 2186 | v.setOnLongClickListener(new OnLongClickListener() { |
| 2187 | @Override |
| 2188 | public boolean onLongClick(View v) { |
Adam Powell | 2388251 | 2016-01-29 10:21:00 -0800 | [diff] [blame] | 2189 | showTargetDetails( |
Adam Powell | 4eb9871 | 2015-10-14 13:10:18 -0700 | [diff] [blame] | 2190 | mChooserListAdapter.resolveInfoForPosition( |
| 2191 | holder.itemIndices[column], true)); |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2192 | return true; |
| 2193 | } |
| 2194 | }); |
| 2195 | row.addView(v); |
| 2196 | holder.cells[i] = v; |
| 2197 | |
| 2198 | // Force height to be a given so we don't have visual disruption during scaling. |
| 2199 | LayoutParams lp = v.getLayoutParams(); |
| 2200 | v.measure(spec, spec); |
| 2201 | if (lp == null) { |
| 2202 | lp = new LayoutParams(LayoutParams.MATCH_PARENT, v.getMeasuredHeight()); |
| 2203 | row.setLayoutParams(lp); |
| 2204 | } else { |
| 2205 | lp.height = v.getMeasuredHeight(); |
| 2206 | } |
Jason Monk | 027dcfa | 2017-06-27 18:37:35 -0400 | [diff] [blame] | 2207 | if (i != (mColumnCount - 1)) { |
| 2208 | row.addView(new Space(ChooserActivity.this), |
| 2209 | new LinearLayout.LayoutParams(0, 0, 1)); |
| 2210 | } |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | // Pre-measure so we can scale later. |
| 2214 | holder.measure(); |
| 2215 | LayoutParams lp = row.getLayoutParams(); |
| 2216 | if (lp == null) { |
| 2217 | lp = new LayoutParams(LayoutParams.MATCH_PARENT, holder.measuredRowHeight); |
| 2218 | row.setLayoutParams(lp); |
| 2219 | } else { |
| 2220 | lp.height = holder.measuredRowHeight; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2221 | } |
| 2222 | row.setTag(holder); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2223 | return holder; |
| 2224 | } |
| 2225 | |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2226 | void bindViewHolder(int rowPosition, RowViewHolder holder) { |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2227 | final int start = getFirstRowPosition(rowPosition); |
| 2228 | final int startType = mChooserListAdapter.getPositionTargetType(start); |
| 2229 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2230 | final int lastStartType = mChooserListAdapter.getPositionTargetType( |
| 2231 | getFirstRowPosition(rowPosition - 1)); |
| 2232 | |
| 2233 | if (startType != lastStartType || rowPosition == 0) { |
| 2234 | holder.row.setBackground(mChooserRowLayer); |
| 2235 | setVertPadding(holder, mChooserRowServiceSpacing, 0); |
| 2236 | } else { |
| 2237 | holder.row.setBackground(null); |
| 2238 | setVertPadding(holder, 0, 0); |
| 2239 | } |
| 2240 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2241 | int end = start + mColumnCount - 1; |
| 2242 | while (mChooserListAdapter.getPositionTargetType(end) != startType && end >= start) { |
| 2243 | end--; |
| 2244 | } |
| 2245 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2246 | if (end == start && mChooserListAdapter.getItem(start) instanceof EmptyTargetInfo) { |
| 2247 | final TextView textView = holder.row.findViewById(R.id.chooser_row_text_option); |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2248 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2249 | if (textView.getVisibility() != View.VISIBLE) { |
| 2250 | textView.setAlpha(0.0f); |
| 2251 | textView.setVisibility(View.VISIBLE); |
| 2252 | textView.setText(R.string.chooser_no_direct_share_targets); |
| 2253 | |
| 2254 | ValueAnimator fadeAnim = ObjectAnimator.ofFloat(textView, "alpha", 0.0f, 1.0f); |
| 2255 | fadeAnim.setInterpolator(new DecelerateInterpolator(1.0f)); |
| 2256 | |
| 2257 | float translationInPx = getResources().getDimensionPixelSize( |
| 2258 | R.dimen.chooser_row_text_option_translate); |
| 2259 | textView.setTranslationY(translationInPx); |
| 2260 | ValueAnimator translateAnim = ObjectAnimator.ofFloat(textView, "translationY", |
| 2261 | 0.0f); |
| 2262 | translateAnim.setInterpolator(new DecelerateInterpolator(1.0f)); |
| 2263 | |
| 2264 | AnimatorSet animSet = new AnimatorSet(); |
| 2265 | animSet.setDuration(NO_DIRECT_SHARE_ANIM_IN_MILLIS); |
| 2266 | animSet.setStartDelay(NO_DIRECT_SHARE_ANIM_IN_MILLIS); |
| 2267 | animSet.playTogether(fadeAnim, translateAnim); |
| 2268 | animSet.start(); |
| 2269 | } |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | for (int i = 0; i < mColumnCount; i++) { |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2273 | final View v = holder.cells[i]; |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2274 | if (start + i <= end) { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2275 | setCellVisibility(holder, i, View.VISIBLE); |
Adam Powell | 4eb9871 | 2015-10-14 13:10:18 -0700 | [diff] [blame] | 2276 | holder.itemIndices[i] = start + i; |
| 2277 | mChooserListAdapter.bindView(holder.itemIndices[i], v); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2278 | } else { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2279 | setCellVisibility(holder, i, View.INVISIBLE); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2280 | } |
| 2281 | } |
| 2282 | } |
| 2283 | |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2284 | private void setCellVisibility(RowViewHolder holder, int i, int visibility) { |
| 2285 | final View v = holder.cells[i]; |
| 2286 | if (visibility == View.VISIBLE) { |
| 2287 | holder.cellVisibility[i] = true; |
| 2288 | v.setVisibility(visibility); |
| 2289 | v.setAlpha(1.0f); |
| 2290 | } else if (visibility == View.INVISIBLE && holder.cellVisibility[i]) { |
| 2291 | holder.cellVisibility[i] = false; |
| 2292 | |
| 2293 | ValueAnimator fadeAnim = ObjectAnimator.ofFloat(v, "alpha", 1.0f, 0f); |
| 2294 | fadeAnim.setDuration(NO_DIRECT_SHARE_ANIM_IN_MILLIS); |
| 2295 | fadeAnim.setInterpolator(new AccelerateInterpolator(1.0f)); |
| 2296 | fadeAnim.addListener(new AnimatorListenerAdapter() { |
| 2297 | public void onAnimationEnd(Animator animation) { |
| 2298 | v.setVisibility(View.INVISIBLE); |
| 2299 | } |
| 2300 | }); |
| 2301 | fadeAnim.start(); |
| 2302 | } |
| 2303 | } |
| 2304 | |
Jason Monk | 027dcfa | 2017-06-27 18:37:35 -0400 | [diff] [blame] | 2305 | private void setVertPadding(RowViewHolder holder, int top, int bottom) { |
| 2306 | holder.row.setPadding(holder.row.getPaddingLeft(), top, |
| 2307 | holder.row.getPaddingRight(), bottom); |
| 2308 | } |
| 2309 | |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2310 | int getFirstRowPosition(int row) { |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 2311 | final int callerCount = mChooserListAdapter.getCallerTargetCount(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2312 | final int callerRows = (int) Math.ceil((float) callerCount / mColumnCount); |
| 2313 | |
| 2314 | if (row < callerRows) { |
| 2315 | return row * mColumnCount; |
| 2316 | } |
| 2317 | |
Adam Powell | 5007735 | 2015-05-26 18:01:55 -0700 | [diff] [blame] | 2318 | final int serviceCount = mChooserListAdapter.getServiceTargetCount(); |
Adam Powell | 7d75800 | 2015-05-06 17:49:36 -0700 | [diff] [blame] | 2319 | final int serviceRows = (int) Math.ceil((float) serviceCount / mColumnCount); |
| 2320 | |
| 2321 | if (row < callerRows + serviceRows) { |
| 2322 | return callerCount + (row - callerRows) * mColumnCount; |
| 2323 | } |
| 2324 | |
| 2325 | return callerCount + serviceCount |
| 2326 | + (row - callerRows - serviceRows) * mColumnCount; |
| 2327 | } |
| 2328 | } |
| 2329 | |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2330 | static class RowViewHolder { |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2331 | public final View[] cells; |
| 2332 | public final boolean [] cellVisibility; |
| 2333 | public final ViewGroup row; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2334 | int measuredRowHeight; |
Adam Powell | 4eb9871 | 2015-10-14 13:10:18 -0700 | [diff] [blame] | 2335 | int[] itemIndices; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2336 | |
| 2337 | public RowViewHolder(ViewGroup row, int cellCount) { |
| 2338 | this.row = row; |
| 2339 | this.cells = new View[cellCount]; |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 2340 | this.cellVisibility = new boolean[cellCount]; |
Adam Powell | 4eb9871 | 2015-10-14 13:10:18 -0700 | [diff] [blame] | 2341 | this.itemIndices = new int[cellCount]; |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | public void measure() { |
| 2345 | final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 2346 | row.measure(spec, spec); |
| 2347 | measuredRowHeight = row.getMeasuredHeight(); |
| 2348 | } |
| 2349 | } |
| 2350 | |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 2351 | static class ChooserTargetServiceConnection implements ServiceConnection { |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 2352 | private DisplayResolveInfo mOriginalTarget; |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 2353 | private ComponentName mConnectedComponent; |
| 2354 | private ChooserActivity mChooserActivity; |
| 2355 | private final Object mLock = new Object(); |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2356 | |
| 2357 | private final IChooserTargetResult mChooserTargetResult = new IChooserTargetResult.Stub() { |
| 2358 | @Override |
| 2359 | public void sendResult(List<ChooserTarget> targets) throws RemoteException { |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 2360 | synchronized (mLock) { |
| 2361 | if (mChooserActivity == null) { |
| 2362 | Log.e(TAG, "destroyed ChooserTargetServiceConnection received result from " |
| 2363 | + mConnectedComponent + "; ignoring..."); |
| 2364 | return; |
| 2365 | } |
| 2366 | mChooserActivity.filterServiceTargets( |
| 2367 | mOriginalTarget.getResolveInfo().activityInfo.packageName, targets); |
| 2368 | final Message msg = Message.obtain(); |
| 2369 | msg.what = CHOOSER_TARGET_SERVICE_RESULT; |
| 2370 | msg.obj = new ServiceResultInfo(mOriginalTarget, targets, |
| 2371 | ChooserTargetServiceConnection.this); |
| 2372 | mChooserActivity.mChooserHandler.sendMessage(msg); |
| 2373 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2374 | } |
| 2375 | }; |
| 2376 | |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 2377 | public ChooserTargetServiceConnection(ChooserActivity chooserActivity, |
| 2378 | DisplayResolveInfo dri) { |
| 2379 | mChooserActivity = chooserActivity; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2380 | mOriginalTarget = dri; |
| 2381 | } |
| 2382 | |
| 2383 | @Override |
| 2384 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 2385 | if (DEBUG) Log.d(TAG, "onServiceConnected: " + name); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 2386 | synchronized (mLock) { |
| 2387 | if (mChooserActivity == null) { |
| 2388 | Log.e(TAG, "destroyed ChooserTargetServiceConnection got onServiceConnected"); |
| 2389 | return; |
| 2390 | } |
| 2391 | |
| 2392 | final IChooserTargetService icts = IChooserTargetService.Stub.asInterface(service); |
| 2393 | try { |
| 2394 | icts.getChooserTargets(mOriginalTarget.getResolvedComponentName(), |
| 2395 | mOriginalTarget.getResolveInfo().filter, mChooserTargetResult); |
| 2396 | } catch (RemoteException e) { |
| 2397 | Log.e(TAG, "Querying ChooserTargetService " + name + " failed.", e); |
| 2398 | mChooserActivity.unbindService(this); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 2399 | mChooserActivity.mServiceConnections.remove(this); |
Dan Sandler | fcd7fae | 2017-09-25 17:40:04 -0400 | [diff] [blame] | 2400 | destroy(); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 2401 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2402 | } |
| 2403 | } |
| 2404 | |
| 2405 | @Override |
| 2406 | public void onServiceDisconnected(ComponentName name) { |
| 2407 | if (DEBUG) Log.d(TAG, "onServiceDisconnected: " + name); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 2408 | synchronized (mLock) { |
| 2409 | if (mChooserActivity == null) { |
| 2410 | Log.e(TAG, |
| 2411 | "destroyed ChooserTargetServiceConnection got onServiceDisconnected"); |
| 2412 | return; |
| 2413 | } |
| 2414 | |
| 2415 | mChooserActivity.unbindService(this); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 2416 | mChooserActivity.mServiceConnections.remove(this); |
| 2417 | if (mChooserActivity.mServiceConnections.isEmpty()) { |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 2418 | mChooserActivity.sendVoiceChoicesIfNeeded(); |
| 2419 | } |
| 2420 | mConnectedComponent = null; |
Dan Sandler | fcd7fae | 2017-09-25 17:40:04 -0400 | [diff] [blame] | 2421 | destroy(); |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | public void destroy() { |
| 2426 | synchronized (mLock) { |
| 2427 | mChooserActivity = null; |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 2428 | mOriginalTarget = null; |
Adam Powell | 4c470d6 | 2015-06-19 17:46:17 -0700 | [diff] [blame] | 2429 | } |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | @Override |
| 2433 | public String toString() { |
Adam Powell | 9761ab2 | 2015-09-08 17:01:49 -0700 | [diff] [blame] | 2434 | return "ChooserTargetServiceConnection{service=" |
| 2435 | + mConnectedComponent + ", activity=" |
Adam Powell | 52c3921 | 2016-04-07 15:14:18 -0700 | [diff] [blame] | 2436 | + (mOriginalTarget != null |
| 2437 | ? mOriginalTarget.getResolveInfo().activityInfo.toString() |
| 2438 | : "<connection destroyed>") + "}"; |
Adam Powell | 2442841 | 2015-04-01 17:19:56 -0700 | [diff] [blame] | 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | static class ServiceResultInfo { |
| 2443 | public final DisplayResolveInfo originalTarget; |
| 2444 | public final List<ChooserTarget> resultTargets; |
| 2445 | public final ChooserTargetServiceConnection connection; |
| 2446 | |
| 2447 | public ServiceResultInfo(DisplayResolveInfo ot, List<ChooserTarget> rt, |
| 2448 | ChooserTargetServiceConnection c) { |
| 2449 | originalTarget = ot; |
| 2450 | resultTargets = rt; |
| 2451 | connection = c; |
| 2452 | } |
| 2453 | } |
Adam Powell | 2ed547e | 2015-04-29 18:45:04 -0700 | [diff] [blame] | 2454 | |
| 2455 | static class RefinementResultReceiver extends ResultReceiver { |
| 2456 | private ChooserActivity mChooserActivity; |
| 2457 | private TargetInfo mSelectedTarget; |
| 2458 | |
| 2459 | public RefinementResultReceiver(ChooserActivity host, TargetInfo target, |
| 2460 | Handler handler) { |
| 2461 | super(handler); |
| 2462 | mChooserActivity = host; |
| 2463 | mSelectedTarget = target; |
| 2464 | } |
| 2465 | |
| 2466 | @Override |
| 2467 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 2468 | if (mChooserActivity == null) { |
| 2469 | Log.e(TAG, "Destroyed RefinementResultReceiver received a result"); |
| 2470 | return; |
| 2471 | } |
| 2472 | if (resultData == null) { |
| 2473 | Log.e(TAG, "RefinementResultReceiver received null resultData"); |
| 2474 | return; |
| 2475 | } |
| 2476 | |
| 2477 | switch (resultCode) { |
| 2478 | case RESULT_CANCELED: |
| 2479 | mChooserActivity.onRefinementCanceled(); |
| 2480 | break; |
| 2481 | case RESULT_OK: |
| 2482 | Parcelable intentParcelable = resultData.getParcelable(Intent.EXTRA_INTENT); |
| 2483 | if (intentParcelable instanceof Intent) { |
| 2484 | mChooserActivity.onRefinementResult(mSelectedTarget, |
| 2485 | (Intent) intentParcelable); |
| 2486 | } else { |
| 2487 | Log.e(TAG, "RefinementResultReceiver received RESULT_OK but no Intent" |
| 2488 | + " in resultData with key Intent.EXTRA_INTENT"); |
| 2489 | } |
| 2490 | break; |
| 2491 | default: |
| 2492 | Log.w(TAG, "Unknown result code " + resultCode |
| 2493 | + " sent to RefinementResultReceiver"); |
| 2494 | break; |
| 2495 | } |
| 2496 | } |
| 2497 | |
| 2498 | public void destroy() { |
| 2499 | mChooserActivity = null; |
| 2500 | mSelectedTarget = null; |
| 2501 | } |
| 2502 | } |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2503 | |
| 2504 | class OffsetDataSetObserver extends DataSetObserver { |
| 2505 | private final AbsListView mListView; |
| 2506 | private int mCachedViewType = -1; |
| 2507 | private View mCachedView; |
| 2508 | |
| 2509 | public OffsetDataSetObserver(AbsListView listView) { |
| 2510 | mListView = listView; |
| 2511 | } |
| 2512 | |
| 2513 | @Override |
| 2514 | public void onChanged() { |
| 2515 | if (mResolverDrawerLayout == null) { |
| 2516 | return; |
| 2517 | } |
| 2518 | |
| 2519 | final int chooserTargetRows = mChooserRowAdapter.getServiceTargetRowCount(); |
| 2520 | int offset = 0; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 2521 | for (int i = 0; i < chooserTargetRows; i++) { |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2522 | final int pos = mChooserRowAdapter.getCallerTargetRowCount() + i; |
| 2523 | final int vt = mChooserRowAdapter.getItemViewType(pos); |
| 2524 | if (vt != mCachedViewType) { |
| 2525 | mCachedView = null; |
| 2526 | } |
| 2527 | final View v = mChooserRowAdapter.getView(pos, mCachedView, mListView); |
| 2528 | int height = ((RowViewHolder) (v.getTag())).measuredRowHeight; |
| 2529 | |
Dan Sandler | 62aad00 | 2018-05-23 02:13:51 -0400 | [diff] [blame] | 2530 | offset += (int) (height); |
Adam Powell | 63b3169 | 2015-09-28 10:45:00 -0700 | [diff] [blame] | 2531 | |
| 2532 | if (vt >= 0) { |
| 2533 | mCachedViewType = vt; |
| 2534 | mCachedView = v; |
| 2535 | } else { |
| 2536 | mCachedViewType = -1; |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | mResolverDrawerLayout.setCollapsibleHeightReserved(offset); |
| 2541 | } |
| 2542 | } |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 2543 | |
| 2544 | |
| 2545 | /** |
| 2546 | * Used internally to round image corners while obeying view padding. |
| 2547 | */ |
| 2548 | public static class RoundedRectImageView extends ImageView { |
| 2549 | private int mRadius = 0; |
| 2550 | private Path mPath = new Path(); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 2551 | private Paint mOverlayPaint = new Paint(0); |
| 2552 | private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 2553 | private String mExtraImageCount = null; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 2554 | |
| 2555 | public RoundedRectImageView(Context context) { |
| 2556 | super(context); |
| 2557 | } |
| 2558 | |
| 2559 | public RoundedRectImageView(Context context, AttributeSet attrs) { |
| 2560 | this(context, attrs, 0); |
| 2561 | } |
| 2562 | |
| 2563 | public RoundedRectImageView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 2564 | this(context, attrs, defStyleAttr, 0); |
| 2565 | } |
| 2566 | |
| 2567 | public RoundedRectImageView(Context context, AttributeSet attrs, int defStyleAttr, |
| 2568 | int defStyleRes) { |
| 2569 | super(context, attrs, defStyleAttr, defStyleRes); |
| 2570 | mRadius = context.getResources().getDimensionPixelSize(R.dimen.chooser_corner_radius); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 2571 | |
| 2572 | mOverlayPaint.setColor(0x99000000); |
| 2573 | mOverlayPaint.setStyle(Paint.Style.FILL); |
| 2574 | |
| 2575 | mTextPaint.setColor(Color.WHITE); |
| 2576 | mTextPaint.setTextSize(context.getResources() |
| 2577 | .getDimensionPixelSize(R.dimen.chooser_preview_image_font_size)); |
| 2578 | mTextPaint.setTextAlign(Paint.Align.CENTER); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | private void updatePath(int width, int height) { |
| 2582 | mPath.reset(); |
| 2583 | |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 2584 | int imageWidth = width - getPaddingRight(); |
| 2585 | int imageHeight = height - getPaddingBottom(); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 2586 | mPath.addRoundRect(getPaddingLeft(), getPaddingTop(), imageWidth, imageHeight, mRadius, |
| 2587 | mRadius, Path.Direction.CW); |
| 2588 | } |
| 2589 | |
| 2590 | /** |
| 2591 | * Sets the corner radius on all corners |
| 2592 | * |
| 2593 | * param radius 0 for no radius, > 0 for a visible corner radius |
| 2594 | */ |
| 2595 | public void setRadius(int radius) { |
| 2596 | mRadius = radius; |
| 2597 | updatePath(getWidth(), getHeight()); |
| 2598 | } |
| 2599 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 2600 | /** |
| 2601 | * Display an overlay with extra image count on 3rd image |
| 2602 | */ |
| 2603 | public void setExtraImageCount(int count) { |
| 2604 | if (count > 0) { |
| 2605 | this.mExtraImageCount = "+" + count; |
| 2606 | } else { |
| 2607 | this.mExtraImageCount = null; |
| 2608 | } |
| 2609 | } |
| 2610 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 2611 | @Override |
| 2612 | protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) { |
| 2613 | super.onSizeChanged(width, height, oldWidth, oldHeight); |
| 2614 | updatePath(width, height); |
| 2615 | } |
| 2616 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 2617 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 2618 | @Override |
| 2619 | protected void onDraw(Canvas canvas) { |
| 2620 | if (mRadius != 0) { |
| 2621 | canvas.clipPath(mPath); |
| 2622 | } |
| 2623 | |
| 2624 | super.onDraw(canvas); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 2625 | |
| 2626 | if (mExtraImageCount != null) { |
| 2627 | canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mOverlayPaint); |
| 2628 | |
| 2629 | int xPos = canvas.getWidth() / 2; |
| 2630 | int yPos = (int) ((canvas.getHeight() / 2.0f) |
| 2631 | - ((mTextPaint.descent() + mTextPaint.ascent()) / 2.0f)); |
| 2632 | |
| 2633 | canvas.drawText(mExtraImageCount, xPos, yPos, mTextPaint); |
| 2634 | } |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 2635 | } |
| 2636 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2637 | } |