The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
| 17 | package android.widget; |
| 18 | |
Tor Norbye | 80756e3 | 2015-03-02 09:39:27 -0800 | [diff] [blame] | 19 | import android.annotation.ColorInt; |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 20 | import android.annotation.DimenRes; |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 21 | import android.annotation.IntDef; |
| 22 | import android.annotation.LayoutRes; |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 23 | import android.annotation.NonNull; |
Dake Gu | 36b86c2 | 2018-04-16 12:49:30 -0700 | [diff] [blame] | 24 | import android.annotation.StyleRes; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 25 | import android.annotation.UnsupportedAppUsage; |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 26 | import android.app.Activity; |
Winson Chung | dc6f79b | 2012-04-17 17:27:31 -0700 | [diff] [blame] | 27 | import android.app.ActivityOptions; |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 28 | import android.app.ActivityThread; |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 29 | import android.app.Application; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.app.PendingIntent; |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 31 | import android.app.RemoteInput; |
Adam Cohen | 1480fdd | 2010-08-25 17:24:53 -0700 | [diff] [blame] | 32 | import android.appwidget.AppWidgetHostView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.content.Context; |
Kenny Guy | 7732006 | 2014-08-27 21:37:15 +0100 | [diff] [blame] | 34 | import android.content.ContextWrapper; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 35 | import android.content.Intent; |
| 36 | import android.content.IntentSender; |
Adam Cohen | ffc46a5 | 2012-04-30 19:54:39 -0700 | [diff] [blame] | 37 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.content.pm.PackageManager.NameNotFoundException; |
Jorim Jaggi | ef72a19 | 2014-08-26 21:57:46 +0200 | [diff] [blame] | 39 | import android.content.res.ColorStateList; |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 40 | import android.content.res.Configuration; |
Kenny Guy | 7732006 | 2014-08-27 21:37:15 +0100 | [diff] [blame] | 41 | import android.content.res.Resources; |
Gus Prevas | 1ed322b | 2015-09-17 17:34:46 -0400 | [diff] [blame] | 42 | import android.content.res.TypedArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.graphics.Bitmap; |
| 44 | import android.graphics.PorterDuff; |
Joe Onorato | 7597065 | 2009-12-02 23:04:55 -0800 | [diff] [blame] | 45 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.graphics.drawable.Drawable; |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 47 | import android.graphics.drawable.Icon; |
Gus Prevas | 9cc9660 | 2018-10-11 11:24:23 -0400 | [diff] [blame] | 48 | import android.graphics.drawable.RippleDrawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.net.Uri; |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 50 | import android.os.AsyncTask; |
Sunny Goyal | 692f8c9 | 2016-11-11 09:19:14 -0800 | [diff] [blame] | 51 | import android.os.Binder; |
Adam Cohen | ffc46a5 | 2012-04-30 19:54:39 -0700 | [diff] [blame] | 52 | import android.os.Build; |
Bjorn Bringert | d755b06 | 2010-01-06 17:15:37 +0000 | [diff] [blame] | 53 | import android.os.Bundle; |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 54 | import android.os.CancellationSignal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.os.Parcel; |
| 56 | import android.os.Parcelable; |
Sunny Goyal | 692f8c9 | 2016-11-11 09:19:14 -0800 | [diff] [blame] | 57 | import android.os.Process; |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 58 | import android.os.StrictMode; |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 59 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.text.TextUtils; |
Romain Guy | e4d4e20 | 2013-07-22 13:02:02 -0700 | [diff] [blame] | 61 | import android.util.ArrayMap; |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 62 | import android.util.IntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.util.Log; |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 64 | import android.util.Pair; |
Dake Gu | 36b86c2 | 2018-04-16 12:49:30 -0700 | [diff] [blame] | 65 | import android.view.ContextThemeWrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.view.LayoutInflater; |
Winson Chung | dc6f79b | 2012-04-17 17:27:31 -0700 | [diff] [blame] | 67 | import android.view.LayoutInflater.Filter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import android.view.RemotableViewMethod; |
| 69 | import android.view.View; |
Winson Chung | dc6f79b | 2012-04-17 17:27:31 -0700 | [diff] [blame] | 70 | import android.view.ViewGroup; |
Sunny Goyal | 7b0e2c7 | 2016-11-03 14:48:05 -0700 | [diff] [blame] | 71 | import android.view.ViewStub; |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 72 | import android.widget.AdapterView.OnItemClickListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
Gus Prevas | 1ed322b | 2015-09-17 17:34:46 -0400 | [diff] [blame] | 74 | import com.android.internal.R; |
Lucas Dupin | a291d19 | 2018-06-07 13:59:42 -0700 | [diff] [blame] | 75 | import com.android.internal.util.ContrastColorUtil; |
Adrian Roos | 7da889d | 2016-03-16 18:38:58 -0700 | [diff] [blame] | 76 | import com.android.internal.util.Preconditions; |
Gus Prevas | 1ed322b | 2015-09-17 17:34:46 -0400 | [diff] [blame] | 77 | |
Winson Chung | dc6f79b | 2012-04-17 17:27:31 -0700 | [diff] [blame] | 78 | import java.lang.annotation.ElementType; |
| 79 | import java.lang.annotation.Retention; |
| 80 | import java.lang.annotation.RetentionPolicy; |
| 81 | import java.lang.annotation.Target; |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 82 | import java.lang.invoke.MethodHandle; |
| 83 | import java.lang.invoke.MethodHandles; |
| 84 | import java.lang.invoke.MethodType; |
Winson Chung | dc6f79b | 2012-04-17 17:27:31 -0700 | [diff] [blame] | 85 | import java.lang.reflect.Method; |
| 86 | import java.util.ArrayList; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 87 | import java.util.HashMap; |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 88 | import java.util.Map; |
Narayan Kamath | 607223f | 2018-02-19 14:09:02 +0000 | [diff] [blame] | 89 | import java.util.Objects; |
Selim Cinek | 87c3153 | 2017-08-18 18:53:44 -0700 | [diff] [blame] | 90 | import java.util.Stack; |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 91 | import java.util.concurrent.Executor; |
Jeff Sharkey | 23b3118 | 2018-04-18 21:32:12 -0600 | [diff] [blame] | 92 | import java.util.function.Consumer; |
Winson Chung | dc6f79b | 2012-04-17 17:27:31 -0700 | [diff] [blame] | 93 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | /** |
| 95 | * A class that describes a view hierarchy that can be displayed in |
| 96 | * another process. The hierarchy is inflated from a layout resource |
| 97 | * file, and this class provides some basic operations for modifying |
| 98 | * the content of the inflated hierarchy. |
Scott Main | 5b7903c | 2017-11-20 18:16:01 -0800 | [diff] [blame] | 99 | * |
| 100 | * <p>{@code RemoteViews} is limited to support for the following layouts:</p> |
| 101 | * <ul> |
| 102 | * <li>{@link android.widget.AdapterViewFlipper}</li> |
| 103 | * <li>{@link android.widget.FrameLayout}</li> |
| 104 | * <li>{@link android.widget.GridLayout}</li> |
| 105 | * <li>{@link android.widget.GridView}</li> |
| 106 | * <li>{@link android.widget.LinearLayout}</li> |
| 107 | * <li>{@link android.widget.ListView}</li> |
| 108 | * <li>{@link android.widget.RelativeLayout}</li> |
| 109 | * <li>{@link android.widget.StackView}</li> |
| 110 | * <li>{@link android.widget.ViewFlipper}</li> |
| 111 | * </ul> |
| 112 | * <p>And the following widgets:</p> |
| 113 | * <ul> |
| 114 | * <li>{@link android.widget.AnalogClock}</li> |
| 115 | * <li>{@link android.widget.Button}</li> |
| 116 | * <li>{@link android.widget.Chronometer}</li> |
| 117 | * <li>{@link android.widget.ImageButton}</li> |
| 118 | * <li>{@link android.widget.ImageView}</li> |
| 119 | * <li>{@link android.widget.ProgressBar}</li> |
| 120 | * <li>{@link android.widget.TextClock}</li> |
| 121 | * <li>{@link android.widget.TextView}</li> |
| 122 | * </ul> |
| 123 | * <p>Descendants of these classes are not supported.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | */ |
| 125 | public class RemoteViews implements Parcelable, Filter { |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 126 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | private static final String LOG_TAG = "RemoteViews"; |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | /** |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 130 | * The intent extra that contains the appWidgetId. |
| 131 | * @hide |
| 132 | */ |
| 133 | static final String EXTRA_REMOTEADAPTER_APPWIDGET_ID = "remoteAdapterAppWidgetId"; |
| 134 | |
| 135 | /** |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 136 | * The intent extra that contains {@code true} if inflating as dak text theme. |
| 137 | * @hide |
| 138 | */ |
| 139 | static final String EXTRA_REMOTEADAPTER_ON_LIGHT_BACKGROUND = "remoteAdapterOnLightBackground"; |
| 140 | |
| 141 | /** |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 142 | * The intent extra that contains the bounds for all shared elements. |
| 143 | */ |
| 144 | public static final String EXTRA_SHARED_ELEMENT_BOUNDS = |
| 145 | "android.widget.extra.SHARED_ELEMENT_BOUNDS"; |
| 146 | |
| 147 | /** |
Sunny Goyal | 692f8c9 | 2016-11-11 09:19:14 -0800 | [diff] [blame] | 148 | * Maximum depth of nested views calls from {@link #addView(int, RemoteViews)} and |
| 149 | * {@link #RemoteViews(RemoteViews, RemoteViews)}. |
| 150 | */ |
| 151 | private static final int MAX_NESTED_VIEWS = 10; |
| 152 | |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 153 | // The unique identifiers for each custom {@link Action}. |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 154 | private static final int SET_ON_CLICK_RESPONSE_TAG = 1; |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 155 | private static final int REFLECTION_ACTION_TAG = 2; |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 156 | private static final int SET_DRAWABLE_TINT_TAG = 3; |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 157 | private static final int VIEW_GROUP_ACTION_ADD_TAG = 4; |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 158 | private static final int VIEW_CONTENT_NAVIGATION_TAG = 5; |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 159 | private static final int SET_EMPTY_VIEW_ACTION_TAG = 6; |
| 160 | private static final int VIEW_GROUP_ACTION_REMOVE_TAG = 7; |
| 161 | private static final int SET_PENDING_INTENT_TEMPLATE_TAG = 8; |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 162 | private static final int SET_REMOTE_VIEW_ADAPTER_INTENT_TAG = 10; |
| 163 | private static final int TEXT_VIEW_DRAWABLE_ACTION_TAG = 11; |
| 164 | private static final int BITMAP_REFLECTION_ACTION_TAG = 12; |
| 165 | private static final int TEXT_VIEW_SIZE_ACTION_TAG = 13; |
| 166 | private static final int VIEW_PADDING_ACTION_TAG = 14; |
| 167 | private static final int SET_REMOTE_VIEW_ADAPTER_LIST_TAG = 15; |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 168 | private static final int SET_REMOTE_INPUTS_ACTION_TAG = 18; |
| 169 | private static final int LAYOUT_PARAM_ACTION_TAG = 19; |
Selim Cinek | 87c3153 | 2017-08-18 18:53:44 -0700 | [diff] [blame] | 170 | private static final int OVERRIDE_TEXT_COLORS_TAG = 20; |
Gus Prevas | 9cc9660 | 2018-10-11 11:24:23 -0400 | [diff] [blame] | 171 | private static final int SET_RIPPLE_DRAWABLE_COLOR_TAG = 21; |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 172 | private static final int SET_INT_TAG_TAG = 22; |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 173 | |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 174 | /** @hide **/ |
| 175 | @IntDef(flag = true, value = { |
| 176 | FLAG_REAPPLY_DISALLOWED, |
| 177 | FLAG_WIDGET_IS_COLLECTION_CHILD, |
| 178 | FLAG_USE_LIGHT_BACKGROUND_LAYOUT |
| 179 | }) |
| 180 | @Retention(RetentionPolicy.SOURCE) |
| 181 | public @interface ApplyFlags {} |
| 182 | /** |
| 183 | * Whether reapply is disallowed on this remoteview. This maybe be true if some actions modify |
| 184 | * the layout in a way that isn't recoverable, since views are being removed. |
| 185 | * @hide |
| 186 | */ |
| 187 | public static final int FLAG_REAPPLY_DISALLOWED = 1; |
| 188 | /** |
| 189 | * This flag indicates whether this RemoteViews object is being created from a |
| 190 | * RemoteViewsService for use as a child of a widget collection. This flag is used |
| 191 | * to determine whether or not certain features are available, in particular, |
| 192 | * setting on click extras and setting on click pending intents. The former is enabled, |
| 193 | * and the latter disabled when this flag is true. |
| 194 | * @hide |
| 195 | */ |
| 196 | public static final int FLAG_WIDGET_IS_COLLECTION_CHILD = 2; |
| 197 | /** |
| 198 | * When this flag is set, the views is inflated with {@link #mLightBackgroundLayoutId} instead |
| 199 | * of {link #mLayoutId} |
| 200 | * @hide |
| 201 | */ |
| 202 | public static final int FLAG_USE_LIGHT_BACKGROUND_LAYOUT = 4; |
| 203 | |
Sunny Goyal | 692f8c9 | 2016-11-11 09:19:14 -0800 | [diff] [blame] | 204 | /** |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 205 | * Application that hosts the remote views. |
| 206 | * |
| 207 | * @hide |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 208 | */ |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 209 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Sunny Goyal | dd60f4d | 2017-10-18 15:22:42 -0700 | [diff] [blame] | 210 | public ApplicationInfo mApplication; |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 211 | |
| 212 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | * The resource ID of the layout file. (Added to the parcel) |
| 214 | */ |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 215 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Gilles Debunne | 3030193 | 2010-06-16 18:32:00 -0700 | [diff] [blame] | 216 | private final int mLayoutId; |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 217 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | /** |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 219 | * The resource ID of the layout file in dark text mode. (Added to the parcel) |
| 220 | */ |
| 221 | private int mLightBackgroundLayoutId = 0; |
| 222 | |
| 223 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | * An array of actions to perform on the view tree once it has been |
| 225 | * inflated |
| 226 | */ |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 227 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | private ArrayList<Action> mActions; |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 229 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 230 | /** |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 231 | * Maps bitmaps to unique indicies to avoid Bitmap duplication. |
| 232 | */ |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 233 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 234 | private BitmapCache mBitmapCache; |
| 235 | |
| 236 | /** |
| 237 | * Indicates whether or not this RemoteViews object is contained as a child of any other |
| 238 | * RemoteViews. |
| 239 | */ |
| 240 | private boolean mIsRoot = true; |
| 241 | |
| 242 | /** |
| 243 | * Constants to whether or not this RemoteViews is composed of a landscape and portrait |
| 244 | * RemoteViews. |
| 245 | */ |
| 246 | private static final int MODE_NORMAL = 0; |
| 247 | private static final int MODE_HAS_LANDSCAPE_AND_PORTRAIT = 1; |
| 248 | |
| 249 | /** |
| 250 | * Used in conjunction with the special constructor |
| 251 | * {@link #RemoteViews(RemoteViews, RemoteViews)} to keep track of the landscape and portrait |
| 252 | * RemoteViews. |
| 253 | */ |
| 254 | private RemoteViews mLandscape = null; |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 255 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 256 | private RemoteViews mPortrait = null; |
| 257 | |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 258 | @ApplyFlags |
| 259 | private int mApplyFlags = 0; |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 260 | |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 261 | /** Class cookies of the Parcel this instance was read from. */ |
| 262 | private final Map<Class, Object> mClassCookies; |
| 263 | |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 264 | private static final OnClickHandler DEFAULT_ON_CLICK_HANDLER = (view, pendingIntent, response) |
| 265 | -> startPendingIntent(view, pendingIntent, response.getLaunchOptions(view)); |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 266 | |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 267 | private static final ArrayMap<MethodKey, MethodArgs> sMethods = new ArrayMap<>(); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 268 | |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 269 | /** |
| 270 | * This key is used to perform lookups in sMethods without causing allocations. |
| 271 | */ |
| 272 | private static final MethodKey sLookupKey = new MethodKey(); |
Romain Guy | e4d4e20 | 2013-07-22 13:02:02 -0700 | [diff] [blame] | 273 | |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 274 | /** |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 275 | * @hide |
| 276 | */ |
| 277 | public void setRemoteInputs(int viewId, RemoteInput[] remoteInputs) { |
| 278 | mActions.add(new SetRemoteInputsAction(viewId, remoteInputs)); |
| 279 | } |
| 280 | |
| 281 | /** |
Selim Cinek | d042662 | 2017-07-11 13:19:59 +0200 | [diff] [blame] | 282 | * Reduces all images and ensures that they are all below the given sizes. |
| 283 | * |
| 284 | * @param maxWidth the maximum width allowed |
| 285 | * @param maxHeight the maximum height allowed |
| 286 | * |
| 287 | * @hide |
| 288 | */ |
| 289 | public void reduceImageSizes(int maxWidth, int maxHeight) { |
| 290 | ArrayList<Bitmap> cache = mBitmapCache.mBitmaps; |
| 291 | for (int i = 0; i < cache.size(); i++) { |
| 292 | Bitmap bitmap = cache.get(i); |
| 293 | cache.set(i, Icon.scaleDownIfNecessary(bitmap, maxWidth, maxHeight)); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /** |
Selim Cinek | 87c3153 | 2017-08-18 18:53:44 -0700 | [diff] [blame] | 298 | * Override all text colors in this layout and replace them by the given text color. |
| 299 | * |
| 300 | * @param textColor The color to use. |
| 301 | * |
| 302 | * @hide |
| 303 | */ |
| 304 | public void overrideTextColors(int textColor) { |
| 305 | addAction(new OverrideTextColorsAction(textColor)); |
| 306 | } |
| 307 | |
| 308 | /** |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 309 | * Sets an integer tag to the view. |
| 310 | * |
| 311 | * @hide |
| 312 | */ |
| 313 | public void setIntTag(int viewId, int key, int tag) { |
| 314 | addAction(new SetIntTagAction(viewId, key, tag)); |
| 315 | } |
| 316 | |
| 317 | /** |
Selim Cinek | fc8073c | 2017-08-16 17:50:20 -0700 | [diff] [blame] | 318 | * Set that it is disallowed to reapply another remoteview with the same layout as this view. |
| 319 | * This should be done if an action is destroying the view tree of the base layout. |
| 320 | * |
| 321 | * @hide |
| 322 | */ |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 323 | public void addFlags(@ApplyFlags int flags) { |
| 324 | mApplyFlags = mApplyFlags | flags; |
Selim Cinek | fc8073c | 2017-08-16 17:50:20 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /** |
Selim Cinek | fc8073c | 2017-08-16 17:50:20 -0700 | [diff] [blame] | 328 | * @hide |
| 329 | */ |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 330 | public boolean hasFlags(@ApplyFlags int flag) { |
| 331 | return (mApplyFlags & flag) == flag; |
Selim Cinek | fc8073c | 2017-08-16 17:50:20 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /** |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 335 | * Stores information related to reflection method lookup. |
Romain Guy | 484f4d6 | 2013-07-22 16:39:16 -0700 | [diff] [blame] | 336 | */ |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 337 | static class MethodKey { |
| 338 | public Class targetClass; |
| 339 | public Class paramClass; |
| 340 | public String methodName; |
Romain Guy | 484f4d6 | 2013-07-22 16:39:16 -0700 | [diff] [blame] | 341 | |
| 342 | @Override |
| 343 | public boolean equals(Object o) { |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 344 | if (!(o instanceof MethodKey)) { |
Romain Guy | 484f4d6 | 2013-07-22 16:39:16 -0700 | [diff] [blame] | 345 | return false; |
| 346 | } |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 347 | MethodKey p = (MethodKey) o; |
Narayan Kamath | 607223f | 2018-02-19 14:09:02 +0000 | [diff] [blame] | 348 | return Objects.equals(p.targetClass, targetClass) |
| 349 | && Objects.equals(p.paramClass, paramClass) |
| 350 | && Objects.equals(p.methodName, methodName); |
Romain Guy | 484f4d6 | 2013-07-22 16:39:16 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public int hashCode() { |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 355 | return Objects.hashCode(targetClass) ^ Objects.hashCode(paramClass) |
| 356 | ^ Objects.hashCode(methodName); |
| 357 | } |
| 358 | |
| 359 | public void set(Class targetClass, Class paramClass, String methodName) { |
| 360 | this.targetClass = targetClass; |
| 361 | this.paramClass = paramClass; |
| 362 | this.methodName = methodName; |
Romain Guy | 484f4d6 | 2013-07-22 16:39:16 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 366 | |
Romain Guy | 484f4d6 | 2013-07-22 16:39:16 -0700 | [diff] [blame] | 367 | /** |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 368 | * Stores information related to reflection method lookup result. |
Romain Guy | 484f4d6 | 2013-07-22 16:39:16 -0700 | [diff] [blame] | 369 | */ |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 370 | static class MethodArgs { |
| 371 | public MethodHandle syncMethod; |
| 372 | public MethodHandle asyncMethod; |
| 373 | public String asyncMethodName; |
| 374 | } |
| 375 | |
Romain Guy | 484f4d6 | 2013-07-22 16:39:16 -0700 | [diff] [blame] | 376 | /** |
Kirill Grouchnikov | 9091df0 | 2016-06-28 11:24:04 -0400 | [diff] [blame] | 377 | * This annotation indicates that a subclass of View is allowed to be used |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 378 | * with the {@link RemoteViews} mechanism. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | */ |
| 380 | @Target({ ElementType.TYPE }) |
| 381 | @Retention(RetentionPolicy.RUNTIME) |
| 382 | public @interface RemoteView { |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Exception to send when something goes wrong executing an action |
| 387 | * |
| 388 | */ |
| 389 | public static class ActionException extends RuntimeException { |
| 390 | public ActionException(Exception ex) { |
| 391 | super(ex); |
| 392 | } |
| 393 | public ActionException(String message) { |
| 394 | super(message); |
| 395 | } |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 396 | /** |
| 397 | * @hide |
| 398 | */ |
| 399 | public ActionException(Throwable t) { |
| 400 | super(t); |
| 401 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | } |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 403 | |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 404 | /** @hide */ |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 405 | public interface OnClickHandler { |
Gus Prevas | 1ed322b | 2015-09-17 17:34:46 -0400 | [diff] [blame] | 406 | |
Sunny Goyal | 0279453 | 2018-08-22 15:18:37 -0700 | [diff] [blame] | 407 | /** @hide */ |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 408 | boolean onClickHandler(View view, PendingIntent pendingIntent, RemoteResponse response); |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Base class for all actions that can be performed on an |
| 413 | * inflated view. |
| 414 | * |
| 415 | * SUBCLASSES MUST BE IMMUTABLE SO CLONE WORKS!!!!! |
| 416 | */ |
| 417 | private abstract static class Action implements Parcelable { |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 418 | public abstract void apply(View root, ViewGroup rootParent, |
| 419 | OnClickHandler handler) throws ActionException; |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 420 | |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 421 | public static final int MERGE_REPLACE = 0; |
| 422 | public static final int MERGE_APPEND = 1; |
| 423 | public static final int MERGE_IGNORE = 2; |
| 424 | |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 425 | public int describeContents() { |
| 426 | return 0; |
| 427 | } |
| 428 | |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 429 | public void setBitmapCache(BitmapCache bitmapCache) { |
| 430 | // Do nothing |
| 431 | } |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 432 | |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 433 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 434 | public int mergeBehavior() { |
| 435 | return MERGE_REPLACE; |
| 436 | } |
| 437 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 438 | public abstract int getActionTag(); |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 439 | |
| 440 | public String getUniqueKey() { |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 441 | return (getActionTag() + "_" + viewId); |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 444 | /** |
| 445 | * This is called on the background thread. It should perform any non-ui computations |
| 446 | * and return the final action which will run on the UI thread. |
| 447 | * Override this if some of the tasks can be performed async. |
| 448 | */ |
| 449 | public Action initActionAsync(ViewTree root, ViewGroup rootParent, OnClickHandler handler) { |
| 450 | return this; |
| 451 | } |
| 452 | |
Sunny Goyal | 5c02263 | 2016-02-17 16:30:41 -0800 | [diff] [blame] | 453 | public boolean prefersAsyncApply() { |
| 454 | return false; |
| 455 | } |
| 456 | |
Sunny Goyal | 5d8bcdf | 2016-10-27 16:11:29 -0700 | [diff] [blame] | 457 | /** |
| 458 | * Overridden by subclasses which have (or inherit) an ApplicationInfo instance |
| 459 | * as member variable |
| 460 | */ |
| 461 | public boolean hasSameAppInfo(ApplicationInfo parentInfo) { |
| 462 | return true; |
| 463 | } |
| 464 | |
Jeff Sharkey | 23b3118 | 2018-04-18 21:32:12 -0600 | [diff] [blame] | 465 | public void visitUris(@NonNull Consumer<Uri> visitor) { |
| 466 | // Nothing to visit by default |
| 467 | } |
| 468 | |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 469 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 470 | int viewId; |
| 471 | } |
| 472 | |
Adam Cohen | bd1e007 | 2012-09-21 16:51:50 -0700 | [diff] [blame] | 473 | /** |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 474 | * Action class used during async inflation of RemoteViews. Subclasses are not parcelable. |
| 475 | */ |
| 476 | private static abstract class RuntimeAction extends Action { |
| 477 | @Override |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 478 | public final int getActionTag() { |
| 479 | return 0; |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public final void writeToParcel(Parcel dest, int flags) { |
| 484 | throw new UnsupportedOperationException(); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // Constant used during async execution. It is not parcelable. |
| 489 | private static final Action ACTION_NOOP = new RuntimeAction() { |
| 490 | @Override |
| 491 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { } |
| 492 | }; |
| 493 | |
| 494 | /** |
Adam Cohen | bd1e007 | 2012-09-21 16:51:50 -0700 | [diff] [blame] | 495 | * Merges the passed RemoteViews actions with this RemoteViews actions according to |
| 496 | * action-specific merge rules. |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 497 | * |
Adam Cohen | bd1e007 | 2012-09-21 16:51:50 -0700 | [diff] [blame] | 498 | * @param newRv |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 499 | * |
Adam Cohen | bd1e007 | 2012-09-21 16:51:50 -0700 | [diff] [blame] | 500 | * @hide |
| 501 | */ |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 502 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 503 | public void mergeRemoteViews(RemoteViews newRv) { |
Adam Cohen | 3ff2d86 | 2012-09-26 14:07:57 -0700 | [diff] [blame] | 504 | if (newRv == null) return; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 505 | // We first copy the new RemoteViews, as the process of merging modifies the way the actions |
| 506 | // reference the bitmap cache. We don't want to modify the object as it may need to |
| 507 | // be merged and applied multiple times. |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 508 | RemoteViews copy = new RemoteViews(newRv); |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 509 | |
| 510 | HashMap<String, Action> map = new HashMap<String, Action>(); |
| 511 | if (mActions == null) { |
| 512 | mActions = new ArrayList<Action>(); |
| 513 | } |
| 514 | |
| 515 | int count = mActions.size(); |
| 516 | for (int i = 0; i < count; i++) { |
| 517 | Action a = mActions.get(i); |
| 518 | map.put(a.getUniqueKey(), a); |
| 519 | } |
| 520 | |
| 521 | ArrayList<Action> newActions = copy.mActions; |
| 522 | if (newActions == null) return; |
| 523 | count = newActions.size(); |
| 524 | for (int i = 0; i < count; i++) { |
| 525 | Action a = newActions.get(i); |
| 526 | String key = newActions.get(i).getUniqueKey(); |
Adam Cohen | 3ff2d86 | 2012-09-26 14:07:57 -0700 | [diff] [blame] | 527 | int mergeBehavior = newActions.get(i).mergeBehavior(); |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 528 | if (map.containsKey(key) && mergeBehavior == Action.MERGE_REPLACE) { |
| 529 | mActions.remove(map.get(key)); |
| 530 | map.remove(key); |
| 531 | } |
| 532 | |
| 533 | // If the merge behavior is ignore, we don't bother keeping the extra action |
| 534 | if (mergeBehavior == Action.MERGE_REPLACE || mergeBehavior == Action.MERGE_APPEND) { |
| 535 | mActions.add(a); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | // Because pruning can remove the need for bitmaps, we reconstruct the bitmap cache |
| 540 | mBitmapCache = new BitmapCache(); |
| 541 | setBitmapCache(mBitmapCache); |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 542 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | |
Jeff Sharkey | 23b3118 | 2018-04-18 21:32:12 -0600 | [diff] [blame] | 544 | /** |
| 545 | * Note all {@link Uri} that are referenced internally, with the expectation |
| 546 | * that Uri permission grants will need to be issued to ensure the recipient |
| 547 | * of this object is able to render its contents. |
| 548 | * |
| 549 | * @hide |
| 550 | */ |
| 551 | public void visitUris(@NonNull Consumer<Uri> visitor) { |
| 552 | if (mActions != null) { |
| 553 | for (int i = 0; i < mActions.size(); i++) { |
| 554 | mActions.get(i).visitUris(visitor); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | private static void visitIconUri(Icon icon, @NonNull Consumer<Uri> visitor) { |
| 560 | if (icon != null && icon.getType() == Icon.TYPE_URI) { |
| 561 | visitor.accept(icon.getUri()); |
| 562 | } |
| 563 | } |
| 564 | |
Adrian Roos | 83fad0a | 2017-04-24 16:58:26 -0700 | [diff] [blame] | 565 | private static class RemoteViewsContextWrapper extends ContextWrapper { |
| 566 | private final Context mContextForResources; |
| 567 | |
| 568 | RemoteViewsContextWrapper(Context context, Context contextForResources) { |
| 569 | super(context); |
| 570 | mContextForResources = contextForResources; |
| 571 | } |
| 572 | |
| 573 | @Override |
| 574 | public Resources getResources() { |
| 575 | return mContextForResources.getResources(); |
| 576 | } |
| 577 | |
| 578 | @Override |
| 579 | public Resources.Theme getTheme() { |
| 580 | return mContextForResources.getTheme(); |
| 581 | } |
| 582 | |
| 583 | @Override |
| 584 | public String getPackageName() { |
| 585 | return mContextForResources.getPackageName(); |
| 586 | } |
| 587 | } |
| 588 | |
Adam Cohen | 1480fdd | 2010-08-25 17:24:53 -0700 | [diff] [blame] | 589 | private class SetEmptyView extends Action { |
Adam Cohen | 1480fdd | 2010-08-25 17:24:53 -0700 | [diff] [blame] | 590 | int emptyViewId; |
| 591 | |
Adam Cohen | 1480fdd | 2010-08-25 17:24:53 -0700 | [diff] [blame] | 592 | SetEmptyView(int viewId, int emptyViewId) { |
| 593 | this.viewId = viewId; |
| 594 | this.emptyViewId = emptyViewId; |
| 595 | } |
| 596 | |
| 597 | SetEmptyView(Parcel in) { |
| 598 | this.viewId = in.readInt(); |
| 599 | this.emptyViewId = in.readInt(); |
| 600 | } |
| 601 | |
| 602 | public void writeToParcel(Parcel out, int flags) { |
Adam Cohen | 1480fdd | 2010-08-25 17:24:53 -0700 | [diff] [blame] | 603 | out.writeInt(this.viewId); |
| 604 | out.writeInt(this.emptyViewId); |
| 605 | } |
| 606 | |
| 607 | @Override |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 608 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
Adam Cohen | 1480fdd | 2010-08-25 17:24:53 -0700 | [diff] [blame] | 609 | final View view = root.findViewById(viewId); |
| 610 | if (!(view instanceof AdapterView<?>)) return; |
| 611 | |
| 612 | AdapterView<?> adapterView = (AdapterView<?>) view; |
| 613 | |
| 614 | final View emptyView = root.findViewById(emptyViewId); |
| 615 | if (emptyView == null) return; |
| 616 | |
| 617 | adapterView.setEmptyView(emptyView); |
| 618 | } |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 619 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 620 | @Override |
| 621 | public int getActionTag() { |
| 622 | return SET_EMPTY_VIEW_ACTION_TAG; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 623 | } |
Adam Cohen | 1480fdd | 2010-08-25 17:24:53 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 626 | private class SetPendingIntentTemplate extends Action { |
| 627 | public SetPendingIntentTemplate(int id, PendingIntent pendingIntentTemplate) { |
| 628 | this.viewId = id; |
| 629 | this.pendingIntentTemplate = pendingIntentTemplate; |
| 630 | } |
| 631 | |
| 632 | public SetPendingIntentTemplate(Parcel parcel) { |
| 633 | viewId = parcel.readInt(); |
| 634 | pendingIntentTemplate = PendingIntent.readPendingIntentOrNullFromParcel(parcel); |
| 635 | } |
| 636 | |
| 637 | public void writeToParcel(Parcel dest, int flags) { |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 638 | dest.writeInt(viewId); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 639 | PendingIntent.writePendingIntentOrNullToParcel(pendingIntentTemplate, dest); |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | @Override |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 643 | public void apply(View root, ViewGroup rootParent, final OnClickHandler handler) { |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 644 | final View target = root.findViewById(viewId); |
Joe Onorato | 2b69ce4 | 2010-10-31 11:35:41 -0700 | [diff] [blame] | 645 | if (target == null) return; |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 646 | |
| 647 | // If the view isn't an AdapterView, setting a PendingIntent template doesn't make sense |
| 648 | if (target instanceof AdapterView<?>) { |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 649 | AdapterView<?> av = (AdapterView<?>) target; |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 650 | // The PendingIntent template is stored in the view's tag. |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 651 | OnItemClickListener listener = new OnItemClickListener() { |
| 652 | public void onItemClick(AdapterView<?> parent, View view, |
| 653 | int position, long id) { |
| 654 | // The view should be a frame layout |
| 655 | if (view instanceof ViewGroup) { |
| 656 | ViewGroup vg = (ViewGroup) view; |
| 657 | |
| 658 | // AdapterViews contain their children in a frame |
| 659 | // so we need to go one layer deeper here. |
| 660 | if (parent instanceof AdapterViewAnimator) { |
| 661 | vg = (ViewGroup) vg.getChildAt(0); |
| 662 | } |
| 663 | if (vg == null) return; |
| 664 | |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 665 | RemoteResponse response = null; |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 666 | int childCount = vg.getChildCount(); |
| 667 | for (int i = 0; i < childCount; i++) { |
| 668 | Object tag = vg.getChildAt(i).getTag(com.android.internal.R.id.fillInIntent); |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 669 | if (tag instanceof RemoteResponse) { |
| 670 | response = (RemoteResponse) tag; |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 671 | break; |
| 672 | } |
| 673 | } |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 674 | if (response == null) return; |
| 675 | response.handleViewClick(view, handler); |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | }; |
| 679 | av.setOnItemClickListener(listener); |
| 680 | av.setTag(pendingIntentTemplate); |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 681 | } else { |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 682 | Log.e(LOG_TAG, "Cannot setPendingIntentTemplate on a view which is not" + |
Adam Cohen | 35ae9ca | 2010-09-20 15:20:41 -0700 | [diff] [blame] | 683 | "an AdapterView (id: " + viewId + ")"); |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 684 | return; |
| 685 | } |
| 686 | } |
| 687 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 688 | @Override |
| 689 | public int getActionTag() { |
| 690 | return SET_PENDING_INTENT_TEMPLATE_TAG; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 693 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 694 | PendingIntent pendingIntentTemplate; |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 697 | private class SetRemoteViewsAdapterList extends Action { |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 698 | public SetRemoteViewsAdapterList(int id, ArrayList<RemoteViews> list, int viewTypeCount) { |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 699 | this.viewId = id; |
| 700 | this.list = list; |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 701 | this.viewTypeCount = viewTypeCount; |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | public SetRemoteViewsAdapterList(Parcel parcel) { |
| 705 | viewId = parcel.readInt(); |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 706 | viewTypeCount = parcel.readInt(); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 707 | list = parcel.createTypedArrayList(RemoteViews.CREATOR); |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | public void writeToParcel(Parcel dest, int flags) { |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 711 | dest.writeInt(viewId); |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 712 | dest.writeInt(viewTypeCount); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 713 | dest.writeTypedList(list, flags); |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | @Override |
| 717 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
| 718 | final View target = root.findViewById(viewId); |
| 719 | if (target == null) return; |
| 720 | |
| 721 | // Ensure that we are applying to an AppWidget root |
| 722 | if (!(rootParent instanceof AppWidgetHostView)) { |
| 723 | Log.e(LOG_TAG, "SetRemoteViewsAdapterIntent action can only be used for " + |
| 724 | "AppWidgets (root id: " + viewId + ")"); |
| 725 | return; |
| 726 | } |
| 727 | // Ensure that we are calling setRemoteAdapter on an AdapterView that supports it |
| 728 | if (!(target instanceof AbsListView) && !(target instanceof AdapterViewAnimator)) { |
| 729 | Log.e(LOG_TAG, "Cannot setRemoteViewsAdapter on a view which is not " + |
| 730 | "an AbsListView or AdapterViewAnimator (id: " + viewId + ")"); |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | if (target instanceof AbsListView) { |
| 735 | AbsListView v = (AbsListView) target; |
| 736 | Adapter a = v.getAdapter(); |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 737 | if (a instanceof RemoteViewsListAdapter && viewTypeCount <= a.getViewTypeCount()) { |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 738 | ((RemoteViewsListAdapter) a).setViewsList(list); |
| 739 | } else { |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 740 | v.setAdapter(new RemoteViewsListAdapter(v.getContext(), list, viewTypeCount)); |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 741 | } |
| 742 | } else if (target instanceof AdapterViewAnimator) { |
| 743 | AdapterViewAnimator v = (AdapterViewAnimator) target; |
| 744 | Adapter a = v.getAdapter(); |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 745 | if (a instanceof RemoteViewsListAdapter && viewTypeCount <= a.getViewTypeCount()) { |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 746 | ((RemoteViewsListAdapter) a).setViewsList(list); |
| 747 | } else { |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 748 | v.setAdapter(new RemoteViewsListAdapter(v.getContext(), list, viewTypeCount)); |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | } |
| 752 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 753 | @Override |
| 754 | public int getActionTag() { |
| 755 | return SET_REMOTE_VIEW_ADAPTER_LIST_TAG; |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 758 | int viewTypeCount; |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 759 | ArrayList<RemoteViews> list; |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 760 | } |
| 761 | |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 762 | private class SetRemoteViewsAdapterIntent extends Action { |
| 763 | public SetRemoteViewsAdapterIntent(int id, Intent intent) { |
| 764 | this.viewId = id; |
| 765 | this.intent = intent; |
| 766 | } |
| 767 | |
| 768 | public SetRemoteViewsAdapterIntent(Parcel parcel) { |
| 769 | viewId = parcel.readInt(); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 770 | intent = parcel.readTypedObject(Intent.CREATOR); |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | public void writeToParcel(Parcel dest, int flags) { |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 774 | dest.writeInt(viewId); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 775 | dest.writeTypedObject(intent, flags); |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | @Override |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 779 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 780 | final View target = root.findViewById(viewId); |
| 781 | if (target == null) return; |
| 782 | |
| 783 | // Ensure that we are applying to an AppWidget root |
| 784 | if (!(rootParent instanceof AppWidgetHostView)) { |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 785 | Log.e(LOG_TAG, "SetRemoteViewsAdapterIntent action can only be used for " + |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 786 | "AppWidgets (root id: " + viewId + ")"); |
| 787 | return; |
| 788 | } |
| 789 | // Ensure that we are calling setRemoteAdapter on an AdapterView that supports it |
| 790 | if (!(target instanceof AbsListView) && !(target instanceof AdapterViewAnimator)) { |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 791 | Log.e(LOG_TAG, "Cannot setRemoteViewsAdapter on a view which is not " + |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 792 | "an AbsListView or AdapterViewAnimator (id: " + viewId + ")"); |
| 793 | return; |
| 794 | } |
| 795 | |
| 796 | // Embed the AppWidget Id for use in RemoteViewsAdapter when connecting to the intent |
| 797 | // RemoteViewsService |
| 798 | AppWidgetHostView host = (AppWidgetHostView) rootParent; |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 799 | intent.putExtra(EXTRA_REMOTEADAPTER_APPWIDGET_ID, host.getAppWidgetId()) |
| 800 | .putExtra(EXTRA_REMOTEADAPTER_ON_LIGHT_BACKGROUND, |
| 801 | hasFlags(FLAG_USE_LIGHT_BACKGROUND_LAYOUT)); |
| 802 | |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 803 | if (target instanceof AbsListView) { |
| 804 | AbsListView v = (AbsListView) target; |
Sunny Goyal | 5c02263 | 2016-02-17 16:30:41 -0800 | [diff] [blame] | 805 | v.setRemoteViewsAdapter(intent, isAsync); |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 806 | v.setRemoteViewsOnClickHandler(handler); |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 807 | } else if (target instanceof AdapterViewAnimator) { |
| 808 | AdapterViewAnimator v = (AdapterViewAnimator) target; |
Sunny Goyal | 5c02263 | 2016-02-17 16:30:41 -0800 | [diff] [blame] | 809 | v.setRemoteViewsAdapter(intent, isAsync); |
Adam Cohen | a6a4cbc | 2012-09-26 17:36:40 -0700 | [diff] [blame] | 810 | v.setRemoteViewsOnClickHandler(handler); |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Sunny Goyal | 5c02263 | 2016-02-17 16:30:41 -0800 | [diff] [blame] | 814 | @Override |
| 815 | public Action initActionAsync(ViewTree root, ViewGroup rootParent, |
| 816 | OnClickHandler handler) { |
| 817 | SetRemoteViewsAdapterIntent copy = new SetRemoteViewsAdapterIntent(viewId, intent); |
| 818 | copy.isAsync = true; |
| 819 | return copy; |
| 820 | } |
| 821 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 822 | @Override |
| 823 | public int getActionTag() { |
| 824 | return SET_REMOTE_VIEW_ADAPTER_INTENT_TAG; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 827 | Intent intent; |
Sunny Goyal | 5c02263 | 2016-02-17 16:30:41 -0800 | [diff] [blame] | 828 | boolean isAsync = false; |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 829 | } |
| 830 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | /** |
| 832 | * Equivalent to calling |
| 833 | * {@link android.view.View#setOnClickListener(android.view.View.OnClickListener)} |
| 834 | * to launch the provided {@link PendingIntent}. |
| 835 | */ |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 836 | private class SetOnClickResponse extends Action { |
| 837 | |
| 838 | SetOnClickResponse(int id, RemoteResponse response) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 839 | this.viewId = id; |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 840 | this.mResponse = response; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | } |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 842 | |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 843 | SetOnClickResponse(Parcel parcel) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | viewId = parcel.readInt(); |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 845 | mResponse = new RemoteResponse(); |
| 846 | mResponse.readFromParcel(parcel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | } |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 848 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 849 | public void writeToParcel(Parcel dest, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | dest.writeInt(viewId); |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 851 | mResponse.writeToParcel(dest, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | } |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 853 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | @Override |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 855 | public void apply(View root, ViewGroup rootParent, final OnClickHandler handler) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | final View target = root.findViewById(viewId); |
Joe Onorato | 2b69ce4 | 2010-10-31 11:35:41 -0700 | [diff] [blame] | 857 | if (target == null) return; |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 858 | |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 859 | if (mResponse.mPendingIntent != null) { |
| 860 | // If the view is an AdapterView, setting a PendingIntent on click doesn't make |
| 861 | // much sense, do they mean to set a PendingIntent template for the |
| 862 | // AdapterView's children? |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 863 | if (hasFlags(FLAG_WIDGET_IS_COLLECTION_CHILD)) { |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 864 | Log.w(LOG_TAG, "Cannot SetOnClickResponse for collection item " |
| 865 | + "(id: " + viewId + ")"); |
| 866 | ApplicationInfo appInfo = root.getContext().getApplicationInfo(); |
Adam Cohen | ffc46a5 | 2012-04-30 19:54:39 -0700 | [diff] [blame] | 867 | |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 868 | // We let this slide for HC and ICS so as to not break compatibility. It should |
| 869 | // have been disabled from the outset, but was left open by accident. |
| 870 | if (appInfo != null |
| 871 | && appInfo.targetSdkVersion >= Build.VERSION_CODES.JELLY_BEAN) { |
| 872 | return; |
| 873 | } |
| 874 | } |
| 875 | target.setTagInternal(R.id.pending_intent_tag, mResponse.mPendingIntent); |
| 876 | } else if (mResponse.mFillIntent != null) { |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 877 | if (!hasFlags(FLAG_WIDGET_IS_COLLECTION_CHILD)) { |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 878 | Log.e(LOG_TAG, "The method setOnClickFillInIntent is available " |
| 879 | + "only from RemoteViewsFactory (ie. on collection items)."); |
Adam Cohen | ffc46a5 | 2012-04-30 19:54:39 -0700 | [diff] [blame] | 880 | return; |
| 881 | } |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 882 | if (target == root) { |
| 883 | // Target is a root node of an AdapterView child. Set the response in the tag. |
| 884 | // Actual click handling is done by OnItemClickListener in |
| 885 | // SetPendingIntentTemplate, which uses this tag information. |
| 886 | target.setTagInternal(com.android.internal.R.id.fillInIntent, mResponse); |
| 887 | return; |
| 888 | } |
| 889 | } else { |
| 890 | // No intent to apply |
| 891 | target.setOnClickListener(null); |
| 892 | return; |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 893 | } |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 894 | target.setOnClickListener(v -> mResponse.handleViewClick(v, handler)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | } |
Adam Cohen | c6151f2 | 2012-02-02 21:02:31 -0800 | [diff] [blame] | 896 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 897 | @Override |
| 898 | public int getActionTag() { |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 899 | return SET_ON_CLICK_RESPONSE_TAG; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 902 | final RemoteResponse mResponse; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | } |
| 904 | |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 905 | /** @hide **/ |
| 906 | public static Rect getSourceBounds(View v) { |
Romain Guy | e4d4e20 | 2013-07-22 13:02:02 -0700 | [diff] [blame] | 907 | final float appScale = v.getContext().getResources() |
| 908 | .getCompatibilityInfo().applicationScale; |
| 909 | final int[] pos = new int[2]; |
| 910 | v.getLocationOnScreen(pos); |
| 911 | |
| 912 | final Rect rect = new Rect(); |
| 913 | rect.left = (int) (pos[0] * appScale + 0.5f); |
| 914 | rect.top = (int) (pos[1] * appScale + 0.5f); |
| 915 | rect.right = (int) ((pos[0] + v.getWidth()) * appScale + 0.5f); |
| 916 | rect.bottom = (int) ((pos[1] + v.getHeight()) * appScale + 0.5f); |
| 917 | return rect; |
| 918 | } |
| 919 | |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 920 | private MethodHandle getMethod(View view, String methodName, Class<?> paramType, |
| 921 | boolean async) { |
| 922 | MethodArgs result; |
Romain Guy | e4d4e20 | 2013-07-22 13:02:02 -0700 | [diff] [blame] | 923 | Class<? extends View> klass = view.getClass(); |
| 924 | |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 925 | synchronized (sMethods) { |
| 926 | // The key is defined by the view class, param class and method name. |
| 927 | sLookupKey.set(klass, paramType, methodName); |
| 928 | result = sMethods.get(sLookupKey); |
Romain Guy | e4d4e20 | 2013-07-22 13:02:02 -0700 | [diff] [blame] | 929 | |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 930 | if (result == null) { |
| 931 | Method method; |
Romain Guy | e4d4e20 | 2013-07-22 13:02:02 -0700 | [diff] [blame] | 932 | try { |
Romain Guy | 9870e5c | 2013-07-23 13:09:51 -0700 | [diff] [blame] | 933 | if (paramType == null) { |
| 934 | method = klass.getMethod(methodName); |
| 935 | } else { |
| 936 | method = klass.getMethod(methodName, paramType); |
| 937 | } |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 938 | if (!method.isAnnotationPresent(RemotableViewMethod.class)) { |
| 939 | throw new ActionException("view: " + klass.getName() |
| 940 | + " can't use method with RemoteViews: " |
| 941 | + methodName + getParameters(paramType)); |
| 942 | } |
| 943 | |
| 944 | result = new MethodArgs(); |
| 945 | result.syncMethod = MethodHandles.publicLookup().unreflect(method); |
| 946 | result.asyncMethodName = |
| 947 | method.getAnnotation(RemotableViewMethod.class).asyncImpl(); |
| 948 | } catch (NoSuchMethodException | IllegalAccessException ex) { |
Romain Guy | e4d4e20 | 2013-07-22 13:02:02 -0700 | [diff] [blame] | 949 | throw new ActionException("view: " + klass.getName() + " doesn't have method: " |
| 950 | + methodName + getParameters(paramType)); |
| 951 | } |
| 952 | |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 953 | MethodKey key = new MethodKey(); |
| 954 | key.set(klass, paramType, methodName); |
| 955 | sMethods.put(key, result); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 956 | } |
| 957 | |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 958 | if (!async) { |
| 959 | return result.syncMethod; |
| 960 | } |
| 961 | // Check this so see if async method is implemented or not. |
| 962 | if (result.asyncMethodName.isEmpty()) { |
| 963 | return null; |
| 964 | } |
| 965 | // Async method is lazily loaded. If it is not yet loaded, load now. |
| 966 | if (result.asyncMethod == null) { |
| 967 | MethodType asyncType = result.syncMethod.type() |
| 968 | .dropParameterTypes(0, 1).changeReturnType(Runnable.class); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 969 | try { |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 970 | result.asyncMethod = MethodHandles.publicLookup().findVirtual( |
| 971 | klass, result.asyncMethodName, asyncType); |
| 972 | } catch (NoSuchMethodException | IllegalAccessException ex) { |
| 973 | throw new ActionException("Async implementation declared as " |
| 974 | + result.asyncMethodName + " but not defined for " + methodName |
| 975 | + ": public Runnable " + result.asyncMethodName + " (" |
| 976 | + TextUtils.join(",", asyncType.parameterArray()) + ")"); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 977 | } |
| 978 | } |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 979 | return result.asyncMethod; |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | |
Romain Guy | e4d4e20 | 2013-07-22 13:02:02 -0700 | [diff] [blame] | 983 | private static String getParameters(Class<?> paramType) { |
| 984 | if (paramType == null) return "()"; |
| 985 | return "(" + paramType + ")"; |
| 986 | } |
| 987 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | /** |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 989 | * Equivalent to calling |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | * {@link Drawable#setColorFilter(int, android.graphics.PorterDuff.Mode)}, |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 991 | * on the {@link Drawable} of a given view. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | * <p> |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 993 | * The operation will be performed on the {@link Drawable} returned by the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 994 | * target {@link View#getBackground()} by default. If targetBackground is false, |
| 995 | * we assume the target is an {@link ImageView} and try applying the operations |
| 996 | * to {@link ImageView#getDrawable()}. |
| 997 | * <p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | */ |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 999 | private class SetDrawableTint extends Action { |
| 1000 | SetDrawableTint(int id, boolean targetBackground, |
| 1001 | int colorFilter, @NonNull PorterDuff.Mode mode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | this.viewId = id; |
| 1003 | this.targetBackground = targetBackground; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | this.colorFilter = colorFilter; |
| 1005 | this.filterMode = mode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | } |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 1007 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1008 | SetDrawableTint(Parcel parcel) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | viewId = parcel.readInt(); |
| 1010 | targetBackground = parcel.readInt() != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | colorFilter = parcel.readInt(); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1012 | filterMode = PorterDuff.intToMode(parcel.readInt()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | } |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 1014 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | public void writeToParcel(Parcel dest, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1016 | dest.writeInt(viewId); |
| 1017 | dest.writeInt(targetBackground ? 1 : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | dest.writeInt(colorFilter); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1019 | dest.writeInt(PorterDuff.modeToInt(filterMode)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | } |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 1021 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | @Override |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 1023 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | final View target = root.findViewById(viewId); |
Joe Onorato | 2b69ce4 | 2010-10-31 11:35:41 -0700 | [diff] [blame] | 1025 | if (target == null) return; |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 1026 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1027 | // Pick the correct drawable to modify for this view |
| 1028 | Drawable targetDrawable = null; |
| 1029 | if (targetBackground) { |
| 1030 | targetDrawable = target.getBackground(); |
| 1031 | } else if (target instanceof ImageView) { |
| 1032 | ImageView imageView = (ImageView) target; |
| 1033 | targetDrawable = imageView.getDrawable(); |
| 1034 | } |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 1035 | |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 1036 | if (targetDrawable != null) { |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1037 | targetDrawable.mutate().setColorFilter(colorFilter, filterMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | } |
| 1039 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1040 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1041 | @Override |
| 1042 | public int getActionTag() { |
| 1043 | return SET_DRAWABLE_TINT_TAG; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1046 | boolean targetBackground; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1047 | int colorFilter; |
| 1048 | PorterDuff.Mode filterMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | } |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 1050 | |
Gus Prevas | 9cc9660 | 2018-10-11 11:24:23 -0400 | [diff] [blame] | 1051 | /** |
| 1052 | * Equivalent to calling |
| 1053 | * {@link RippleDrawable#setColor(ColorStateList)}, |
| 1054 | * on the {@link Drawable} of a given view. |
| 1055 | * <p> |
| 1056 | * The operation will be performed on the {@link Drawable} returned by the |
| 1057 | * target {@link View#getBackground()}. |
| 1058 | * <p> |
| 1059 | */ |
| 1060 | private class SetRippleDrawableColor extends Action { |
| 1061 | |
| 1062 | ColorStateList mColorStateList; |
| 1063 | |
| 1064 | SetRippleDrawableColor(int id, ColorStateList colorStateList) { |
| 1065 | this.viewId = id; |
| 1066 | this.mColorStateList = colorStateList; |
| 1067 | } |
| 1068 | |
| 1069 | SetRippleDrawableColor(Parcel parcel) { |
| 1070 | viewId = parcel.readInt(); |
| 1071 | mColorStateList = parcel.readParcelable(null); |
| 1072 | } |
| 1073 | |
| 1074 | public void writeToParcel(Parcel dest, int flags) { |
| 1075 | dest.writeInt(viewId); |
| 1076 | dest.writeParcelable(mColorStateList, 0); |
| 1077 | } |
| 1078 | |
| 1079 | @Override |
| 1080 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
| 1081 | final View target = root.findViewById(viewId); |
| 1082 | if (target == null) return; |
| 1083 | |
| 1084 | // Pick the correct drawable to modify for this view |
| 1085 | Drawable targetDrawable = target.getBackground(); |
| 1086 | |
| 1087 | if (targetDrawable instanceof RippleDrawable) { |
| 1088 | ((RippleDrawable) targetDrawable.mutate()).setColor(mColorStateList); |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | @Override |
| 1093 | public int getActionTag() { |
| 1094 | return SET_RIPPLE_DRAWABLE_COLOR_TAG; |
| 1095 | } |
| 1096 | } |
| 1097 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1098 | private final class ViewContentNavigation extends Action { |
| 1099 | final boolean mNext; |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 1100 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1101 | ViewContentNavigation(int viewId, boolean next) { |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 1102 | this.viewId = viewId; |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1103 | this.mNext = next; |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1106 | ViewContentNavigation(Parcel in) { |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 1107 | this.viewId = in.readInt(); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1108 | this.mNext = in.readBoolean(); |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | public void writeToParcel(Parcel out, int flags) { |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 1112 | out.writeInt(this.viewId); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1113 | out.writeBoolean(this.mNext); |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | @Override |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 1117 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 1118 | final View view = root.findViewById(viewId); |
Joe Onorato | 2b69ce4 | 2010-10-31 11:35:41 -0700 | [diff] [blame] | 1119 | if (view == null) return; |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 1120 | |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 1121 | try { |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1122 | getMethod(view, |
| 1123 | mNext ? "showNext" : "showPrevious", null, false /* async */).invoke(view); |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 1124 | } catch (Throwable ex) { |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 1125 | throw new ActionException(ex); |
| 1126 | } |
| 1127 | } |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1128 | |
| 1129 | public int mergeBehavior() { |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1130 | return MERGE_IGNORE; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1133 | @Override |
| 1134 | public int getActionTag() { |
| 1135 | return VIEW_CONTENT_NAVIGATION_TAG; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1136 | } |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1139 | private static class BitmapCache { |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 1140 | |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 1141 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1142 | ArrayList<Bitmap> mBitmaps; |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 1143 | int mBitmapMemory = -1; |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1144 | |
| 1145 | public BitmapCache() { |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 1146 | mBitmaps = new ArrayList<>(); |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | public BitmapCache(Parcel source) { |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1150 | mBitmaps = source.createTypedArrayList(Bitmap.CREATOR); |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | public int getBitmapId(Bitmap b) { |
| 1154 | if (b == null) { |
| 1155 | return -1; |
| 1156 | } else { |
| 1157 | if (mBitmaps.contains(b)) { |
| 1158 | return mBitmaps.indexOf(b); |
| 1159 | } else { |
| 1160 | mBitmaps.add(b); |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 1161 | mBitmapMemory = -1; |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1162 | return (mBitmaps.size() - 1); |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | public Bitmap getBitmapForId(int id) { |
| 1168 | if (id == -1 || id >= mBitmaps.size()) { |
| 1169 | return null; |
| 1170 | } else { |
| 1171 | return mBitmaps.get(id); |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | public void writeBitmapsToParcel(Parcel dest, int flags) { |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1176 | dest.writeTypedList(mBitmaps, flags); |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 1179 | public int getBitmapMemory() { |
| 1180 | if (mBitmapMemory < 0) { |
| 1181 | mBitmapMemory = 0; |
| 1182 | int count = mBitmaps.size(); |
| 1183 | for (int i = 0; i < count; i++) { |
| 1184 | mBitmapMemory += mBitmaps.get(i).getAllocationByteCount(); |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1185 | } |
| 1186 | } |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 1187 | return mBitmapMemory; |
Adrian Roos | 7da889d | 2016-03-16 18:38:58 -0700 | [diff] [blame] | 1188 | } |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | private class BitmapReflectionAction extends Action { |
| 1192 | int bitmapId; |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 1193 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1194 | Bitmap bitmap; |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 1195 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1196 | String methodName; |
| 1197 | |
| 1198 | BitmapReflectionAction(int viewId, String methodName, Bitmap bitmap) { |
| 1199 | this.bitmap = bitmap; |
| 1200 | this.viewId = viewId; |
| 1201 | this.methodName = methodName; |
| 1202 | bitmapId = mBitmapCache.getBitmapId(bitmap); |
| 1203 | } |
| 1204 | |
| 1205 | BitmapReflectionAction(Parcel in) { |
| 1206 | viewId = in.readInt(); |
| 1207 | methodName = in.readString(); |
| 1208 | bitmapId = in.readInt(); |
| 1209 | bitmap = mBitmapCache.getBitmapForId(bitmapId); |
| 1210 | } |
| 1211 | |
| 1212 | @Override |
| 1213 | public void writeToParcel(Parcel dest, int flags) { |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1214 | dest.writeInt(viewId); |
| 1215 | dest.writeString(methodName); |
| 1216 | dest.writeInt(bitmapId); |
| 1217 | } |
| 1218 | |
| 1219 | @Override |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 1220 | public void apply(View root, ViewGroup rootParent, |
| 1221 | OnClickHandler handler) throws ActionException { |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1222 | ReflectionAction ra = new ReflectionAction(viewId, methodName, ReflectionAction.BITMAP, |
| 1223 | bitmap); |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 1224 | ra.apply(root, rootParent, handler); |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | @Override |
| 1228 | public void setBitmapCache(BitmapCache bitmapCache) { |
| 1229 | bitmapId = bitmapCache.getBitmapId(bitmap); |
| 1230 | } |
| 1231 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1232 | @Override |
| 1233 | public int getActionTag() { |
| 1234 | return BITMAP_REFLECTION_ACTION_TAG; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1235 | } |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1238 | /** |
| 1239 | * Base class for the reflection actions. |
| 1240 | */ |
Romain Guy | 484f4d6 | 2013-07-22 16:39:16 -0700 | [diff] [blame] | 1241 | private final class ReflectionAction extends Action { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1242 | static final int BOOLEAN = 1; |
| 1243 | static final int BYTE = 2; |
| 1244 | static final int SHORT = 3; |
| 1245 | static final int INT = 4; |
| 1246 | static final int LONG = 5; |
| 1247 | static final int FLOAT = 6; |
| 1248 | static final int DOUBLE = 7; |
| 1249 | static final int CHAR = 8; |
| 1250 | static final int STRING = 9; |
| 1251 | static final int CHAR_SEQUENCE = 10; |
| 1252 | static final int URI = 11; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1253 | // BITMAP actions are never stored in the list of actions. They are only used locally |
| 1254 | // to implement BitmapReflectionAction, which eliminates duplicates using BitmapCache. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | static final int BITMAP = 12; |
Bjorn Bringert | d755b06 | 2010-01-06 17:15:37 +0000 | [diff] [blame] | 1256 | static final int BUNDLE = 13; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1257 | static final int INTENT = 14; |
Jorim Jaggi | ef72a19 | 2014-08-26 21:57:46 +0200 | [diff] [blame] | 1258 | static final int COLOR_STATE_LIST = 15; |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 1259 | static final int ICON = 16; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1260 | |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 1261 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1262 | String methodName; |
| 1263 | int type; |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 1264 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1265 | Object value; |
| 1266 | |
| 1267 | ReflectionAction(int viewId, String methodName, int type, Object value) { |
| 1268 | this.viewId = viewId; |
| 1269 | this.methodName = methodName; |
| 1270 | this.type = type; |
| 1271 | this.value = value; |
| 1272 | } |
| 1273 | |
| 1274 | ReflectionAction(Parcel in) { |
| 1275 | this.viewId = in.readInt(); |
| 1276 | this.methodName = in.readString(); |
| 1277 | this.type = in.readInt(); |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 1278 | //noinspection ConstantIfStatement |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1279 | if (false) { |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 1280 | Log.d(LOG_TAG, "read viewId=0x" + Integer.toHexString(this.viewId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1281 | + " methodName=" + this.methodName + " type=" + this.type); |
| 1282 | } |
Adam Cohen | c6151f2 | 2012-02-02 21:02:31 -0800 | [diff] [blame] | 1283 | |
| 1284 | // For some values that may have been null, we first check a flag to see if they were |
| 1285 | // written to the parcel. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1286 | switch (this.type) { |
| 1287 | case BOOLEAN: |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1288 | this.value = in.readBoolean(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1289 | break; |
| 1290 | case BYTE: |
| 1291 | this.value = in.readByte(); |
| 1292 | break; |
| 1293 | case SHORT: |
| 1294 | this.value = (short)in.readInt(); |
| 1295 | break; |
| 1296 | case INT: |
| 1297 | this.value = in.readInt(); |
| 1298 | break; |
| 1299 | case LONG: |
| 1300 | this.value = in.readLong(); |
| 1301 | break; |
| 1302 | case FLOAT: |
| 1303 | this.value = in.readFloat(); |
| 1304 | break; |
| 1305 | case DOUBLE: |
| 1306 | this.value = in.readDouble(); |
| 1307 | break; |
| 1308 | case CHAR: |
| 1309 | this.value = (char)in.readInt(); |
| 1310 | break; |
| 1311 | case STRING: |
| 1312 | this.value = in.readString(); |
| 1313 | break; |
| 1314 | case CHAR_SEQUENCE: |
| 1315 | this.value = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 1316 | break; |
| 1317 | case URI: |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1318 | this.value = in.readTypedObject(Uri.CREATOR); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1319 | break; |
| 1320 | case BITMAP: |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1321 | this.value = in.readTypedObject(Bitmap.CREATOR); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1322 | break; |
Bjorn Bringert | d755b06 | 2010-01-06 17:15:37 +0000 | [diff] [blame] | 1323 | case BUNDLE: |
| 1324 | this.value = in.readBundle(); |
| 1325 | break; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1326 | case INTENT: |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1327 | this.value = in.readTypedObject(Intent.CREATOR); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1328 | break; |
Jorim Jaggi | ef72a19 | 2014-08-26 21:57:46 +0200 | [diff] [blame] | 1329 | case COLOR_STATE_LIST: |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1330 | this.value = in.readTypedObject(ColorStateList.CREATOR); |
Jorim Jaggi | ef72a19 | 2014-08-26 21:57:46 +0200 | [diff] [blame] | 1331 | break; |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 1332 | case ICON: |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1333 | this.value = in.readTypedObject(Icon.CREATOR); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1334 | default: |
| 1335 | break; |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | public void writeToParcel(Parcel out, int flags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1340 | out.writeInt(this.viewId); |
| 1341 | out.writeString(this.methodName); |
| 1342 | out.writeInt(this.type); |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 1343 | //noinspection ConstantIfStatement |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1344 | if (false) { |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 1345 | Log.d(LOG_TAG, "write viewId=0x" + Integer.toHexString(this.viewId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1346 | + " methodName=" + this.methodName + " type=" + this.type); |
| 1347 | } |
Adam Cohen | c6151f2 | 2012-02-02 21:02:31 -0800 | [diff] [blame] | 1348 | |
| 1349 | // For some values which are null, we record an integer flag to indicate whether |
| 1350 | // we have written a valid value to the parcel. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1351 | switch (this.type) { |
| 1352 | case BOOLEAN: |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1353 | out.writeBoolean((Boolean) this.value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1354 | break; |
| 1355 | case BYTE: |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 1356 | out.writeByte((Byte) this.value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1357 | break; |
| 1358 | case SHORT: |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 1359 | out.writeInt((Short) this.value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1360 | break; |
| 1361 | case INT: |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 1362 | out.writeInt((Integer) this.value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | break; |
| 1364 | case LONG: |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 1365 | out.writeLong((Long) this.value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1366 | break; |
| 1367 | case FLOAT: |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 1368 | out.writeFloat((Float) this.value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1369 | break; |
| 1370 | case DOUBLE: |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 1371 | out.writeDouble((Double) this.value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1372 | break; |
| 1373 | case CHAR: |
| 1374 | out.writeInt((int)((Character)this.value).charValue()); |
| 1375 | break; |
| 1376 | case STRING: |
| 1377 | out.writeString((String)this.value); |
| 1378 | break; |
| 1379 | case CHAR_SEQUENCE: |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 1380 | TextUtils.writeToParcel((CharSequence)this.value, out, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1381 | break; |
Bjorn Bringert | d755b06 | 2010-01-06 17:15:37 +0000 | [diff] [blame] | 1382 | case BUNDLE: |
| 1383 | out.writeBundle((Bundle) this.value); |
| 1384 | break; |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 1385 | case URI: |
| 1386 | case BITMAP: |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1387 | case INTENT: |
Jorim Jaggi | ef72a19 | 2014-08-26 21:57:46 +0200 | [diff] [blame] | 1388 | case COLOR_STATE_LIST: |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 1389 | case ICON: |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1390 | out.writeTypedObject((Parcelable) this.value, flags); |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 1391 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1392 | default: |
| 1393 | break; |
| 1394 | } |
| 1395 | } |
| 1396 | |
Romain Guy | e4d4e20 | 2013-07-22 13:02:02 -0700 | [diff] [blame] | 1397 | private Class<?> getParameterType() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1398 | switch (this.type) { |
| 1399 | case BOOLEAN: |
| 1400 | return boolean.class; |
| 1401 | case BYTE: |
| 1402 | return byte.class; |
| 1403 | case SHORT: |
| 1404 | return short.class; |
| 1405 | case INT: |
| 1406 | return int.class; |
| 1407 | case LONG: |
| 1408 | return long.class; |
| 1409 | case FLOAT: |
| 1410 | return float.class; |
| 1411 | case DOUBLE: |
| 1412 | return double.class; |
| 1413 | case CHAR: |
| 1414 | return char.class; |
| 1415 | case STRING: |
| 1416 | return String.class; |
| 1417 | case CHAR_SEQUENCE: |
| 1418 | return CharSequence.class; |
| 1419 | case URI: |
| 1420 | return Uri.class; |
| 1421 | case BITMAP: |
| 1422 | return Bitmap.class; |
Bjorn Bringert | d755b06 | 2010-01-06 17:15:37 +0000 | [diff] [blame] | 1423 | case BUNDLE: |
| 1424 | return Bundle.class; |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 1425 | case INTENT: |
| 1426 | return Intent.class; |
Jorim Jaggi | ef72a19 | 2014-08-26 21:57:46 +0200 | [diff] [blame] | 1427 | case COLOR_STATE_LIST: |
| 1428 | return ColorStateList.class; |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 1429 | case ICON: |
| 1430 | return Icon.class; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1431 | default: |
| 1432 | return null; |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | @Override |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 1437 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1438 | final View view = root.findViewById(viewId); |
Joe Onorato | 2b69ce4 | 2010-10-31 11:35:41 -0700 | [diff] [blame] | 1439 | if (view == null) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1440 | |
Romain Guy | e4d4e20 | 2013-07-22 13:02:02 -0700 | [diff] [blame] | 1441 | Class<?> param = getParameterType(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1442 | if (param == null) { |
| 1443 | throw new ActionException("bad type: " + this.type); |
| 1444 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1445 | try { |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 1446 | getMethod(view, this.methodName, param, false /* async */).invoke(view, this.value); |
| 1447 | } catch (Throwable ex) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1448 | throw new ActionException(ex); |
| 1449 | } |
| 1450 | } |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1451 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1452 | @Override |
| 1453 | public Action initActionAsync(ViewTree root, ViewGroup rootParent, OnClickHandler handler) { |
| 1454 | final View view = root.findViewById(viewId); |
| 1455 | if (view == null) return ACTION_NOOP; |
| 1456 | |
| 1457 | Class<?> param = getParameterType(); |
| 1458 | if (param == null) { |
| 1459 | throw new ActionException("bad type: " + this.type); |
| 1460 | } |
| 1461 | |
| 1462 | try { |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 1463 | MethodHandle method = getMethod(view, this.methodName, param, true /* async */); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1464 | |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 1465 | if (method != null) { |
| 1466 | Runnable endAction = (Runnable) method.invoke(view, this.value); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1467 | if (endAction == null) { |
| 1468 | return ACTION_NOOP; |
| 1469 | } else { |
Sunny Goyal | 7b0e2c7 | 2016-11-03 14:48:05 -0700 | [diff] [blame] | 1470 | // Special case view stub |
| 1471 | if (endAction instanceof ViewStub.ViewReplaceRunnable) { |
| 1472 | root.createTree(); |
| 1473 | // Replace child tree |
| 1474 | root.findViewTreeById(viewId).replaceView( |
| 1475 | ((ViewStub.ViewReplaceRunnable) endAction).view); |
| 1476 | } |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1477 | return new RunnableAction(endAction); |
| 1478 | } |
| 1479 | } |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 1480 | } catch (Throwable ex) { |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1481 | throw new ActionException(ex); |
| 1482 | } |
| 1483 | |
| 1484 | return this; |
| 1485 | } |
| 1486 | |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1487 | public int mergeBehavior() { |
| 1488 | // smoothScrollBy is cumulative, everything else overwites. |
| 1489 | if (methodName.equals("smoothScrollBy")) { |
| 1490 | return MERGE_APPEND; |
| 1491 | } else { |
| 1492 | return MERGE_REPLACE; |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1493 | } |
| 1494 | } |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1495 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1496 | @Override |
| 1497 | public int getActionTag() { |
| 1498 | return REFLECTION_ACTION_TAG; |
| 1499 | } |
| 1500 | |
| 1501 | @Override |
| 1502 | public String getUniqueKey() { |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1503 | // Each type of reflection action corresponds to a setter, so each should be seen as |
| 1504 | // unique from the standpoint of merging. |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1505 | return super.getUniqueKey() + this.methodName + this.type; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1506 | } |
Sunny Goyal | 5c02263 | 2016-02-17 16:30:41 -0800 | [diff] [blame] | 1507 | |
| 1508 | @Override |
| 1509 | public boolean prefersAsyncApply() { |
| 1510 | return this.type == URI || this.type == ICON; |
| 1511 | } |
Jeff Sharkey | 23b3118 | 2018-04-18 21:32:12 -0600 | [diff] [blame] | 1512 | |
| 1513 | @Override |
| 1514 | public void visitUris(@NonNull Consumer<Uri> visitor) { |
| 1515 | switch (this.type) { |
| 1516 | case URI: |
| 1517 | final Uri uri = (Uri) this.value; |
| 1518 | visitor.accept(uri); |
| 1519 | break; |
| 1520 | case ICON: |
| 1521 | final Icon icon = (Icon) this.value; |
| 1522 | visitIconUri(icon, visitor); |
| 1523 | break; |
| 1524 | } |
| 1525 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1526 | } |
| 1527 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1528 | /** |
| 1529 | * This is only used for async execution of actions and it not parcelable. |
| 1530 | */ |
| 1531 | private static final class RunnableAction extends RuntimeAction { |
| 1532 | private final Runnable mRunnable; |
| 1533 | |
| 1534 | RunnableAction(Runnable r) { |
| 1535 | mRunnable = r; |
| 1536 | } |
| 1537 | |
| 1538 | @Override |
| 1539 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
| 1540 | mRunnable.run(); |
| 1541 | } |
| 1542 | } |
| 1543 | |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1544 | private void configureRemoteViewsAsChild(RemoteViews rv) { |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1545 | rv.setBitmapCache(mBitmapCache); |
| 1546 | rv.setNotRoot(); |
| 1547 | } |
| 1548 | |
| 1549 | void setNotRoot() { |
| 1550 | mIsRoot = false; |
| 1551 | } |
| 1552 | |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 1553 | /** |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1554 | * ViewGroup methods that are related to adding Views. |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 1555 | */ |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1556 | private class ViewGroupActionAdd extends Action { |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 1557 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1558 | private RemoteViews mNestedViews; |
| 1559 | private int mIndex; |
| 1560 | |
| 1561 | ViewGroupActionAdd(int viewId, RemoteViews nestedViews) { |
| 1562 | this(viewId, nestedViews, -1 /* index */); |
| 1563 | } |
| 1564 | |
| 1565 | ViewGroupActionAdd(int viewId, RemoteViews nestedViews, int index) { |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 1566 | this.viewId = viewId; |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1567 | mNestedViews = nestedViews; |
| 1568 | mIndex = index; |
Adam Cohen | c431c15 | 2012-04-26 18:42:17 -0700 | [diff] [blame] | 1569 | if (nestedViews != null) { |
| 1570 | configureRemoteViewsAsChild(nestedViews); |
| 1571 | } |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 1572 | } |
| 1573 | |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1574 | ViewGroupActionAdd(Parcel parcel, BitmapCache bitmapCache, ApplicationInfo info, |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 1575 | int depth, Map<Class, Object> classCookies) { |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 1576 | viewId = parcel.readInt(); |
Anthony Chen | ea202f6 | 2017-07-05 11:22:25 -0700 | [diff] [blame] | 1577 | mIndex = parcel.readInt(); |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 1578 | mNestedViews = new RemoteViews(parcel, bitmapCache, info, depth, classCookies); |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 1579 | mNestedViews.addFlags(mApplyFlags); |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | public void writeToParcel(Parcel dest, int flags) { |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 1583 | dest.writeInt(viewId); |
Anthony Chen | ea202f6 | 2017-07-05 11:22:25 -0700 | [diff] [blame] | 1584 | dest.writeInt(mIndex); |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1585 | mNestedViews.writeToParcel(dest, flags); |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | @Override |
Sunny Goyal | 5d8bcdf | 2016-10-27 16:11:29 -0700 | [diff] [blame] | 1589 | public boolean hasSameAppInfo(ApplicationInfo parentInfo) { |
Sunny Goyal | dd60f4d | 2017-10-18 15:22:42 -0700 | [diff] [blame] | 1590 | return mNestedViews.hasSameAppInfo(parentInfo); |
Sunny Goyal | 5d8bcdf | 2016-10-27 16:11:29 -0700 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | @Override |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 1594 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 1595 | final Context context = root.getContext(); |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 1596 | final ViewGroup target = root.findViewById(viewId); |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1597 | |
| 1598 | if (target == null) { |
| 1599 | return; |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 1600 | } |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1601 | |
| 1602 | // Inflate nested views and add as children |
| 1603 | target.addView(mNestedViews.apply(context, target, handler), mIndex); |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1606 | @Override |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1607 | public Action initActionAsync(ViewTree root, ViewGroup rootParent, OnClickHandler handler) { |
| 1608 | // In the async implementation, update the view tree so that subsequent calls to |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1609 | // findViewById return the current view. |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1610 | root.createTree(); |
| 1611 | ViewTree target = root.findViewTreeById(viewId); |
| 1612 | if ((target == null) || !(target.mRoot instanceof ViewGroup)) { |
| 1613 | return ACTION_NOOP; |
| 1614 | } |
Sunny Goyal | 7b0e2c7 | 2016-11-03 14:48:05 -0700 | [diff] [blame] | 1615 | final ViewGroup targetVg = (ViewGroup) target.mRoot; |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1616 | |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1617 | // Inflate nested views and perform all the async tasks for the child remoteView. |
| 1618 | final Context context = root.mRoot.getContext(); |
| 1619 | final AsyncApplyTask task = mNestedViews.getAsyncApplyTask( |
| 1620 | context, targetVg, null, handler); |
| 1621 | final ViewTree tree = task.doInBackground(); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1622 | |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1623 | if (tree == null) { |
| 1624 | throw new ActionException(task.mError); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1625 | } |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1626 | |
| 1627 | // Update the global view tree, so that next call to findViewTreeById |
| 1628 | // goes through the subtree as well. |
| 1629 | target.addChild(tree, mIndex); |
| 1630 | |
| 1631 | return new RuntimeAction() { |
| 1632 | @Override |
| 1633 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) |
| 1634 | throws ActionException { |
| 1635 | task.onPostExecute(tree); |
| 1636 | targetVg.addView(task.mResult, mIndex); |
| 1637 | } |
| 1638 | }; |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1639 | } |
| 1640 | |
| 1641 | @Override |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 1642 | public void setBitmapCache(BitmapCache bitmapCache) { |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1643 | mNestedViews.setBitmapCache(bitmapCache); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1646 | @Override |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1647 | public int mergeBehavior() { |
| 1648 | return MERGE_APPEND; |
| 1649 | } |
| 1650 | |
Sunny Goyal | 5c02263 | 2016-02-17 16:30:41 -0800 | [diff] [blame] | 1651 | @Override |
| 1652 | public boolean prefersAsyncApply() { |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1653 | return mNestedViews.prefersAsyncApply(); |
Sunny Goyal | 5c02263 | 2016-02-17 16:30:41 -0800 | [diff] [blame] | 1654 | } |
| 1655 | |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1656 | @Override |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1657 | public int getActionTag() { |
| 1658 | return VIEW_GROUP_ACTION_ADD_TAG; |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | /** |
| 1663 | * ViewGroup methods related to removing child views. |
| 1664 | */ |
| 1665 | private class ViewGroupActionRemove extends Action { |
| 1666 | /** |
| 1667 | * Id that indicates that all child views of the affected ViewGroup should be removed. |
| 1668 | * |
| 1669 | * <p>Using -2 because the default id is -1. This avoids accidentally matching that. |
| 1670 | */ |
| 1671 | private static final int REMOVE_ALL_VIEWS_ID = -2; |
| 1672 | |
| 1673 | private int mViewIdToKeep; |
| 1674 | |
| 1675 | ViewGroupActionRemove(int viewId) { |
| 1676 | this(viewId, REMOVE_ALL_VIEWS_ID); |
| 1677 | } |
| 1678 | |
| 1679 | ViewGroupActionRemove(int viewId, int viewIdToKeep) { |
| 1680 | this.viewId = viewId; |
| 1681 | mViewIdToKeep = viewIdToKeep; |
| 1682 | } |
| 1683 | |
| 1684 | ViewGroupActionRemove(Parcel parcel) { |
| 1685 | viewId = parcel.readInt(); |
| 1686 | mViewIdToKeep = parcel.readInt(); |
| 1687 | } |
| 1688 | |
| 1689 | public void writeToParcel(Parcel dest, int flags) { |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1690 | dest.writeInt(viewId); |
| 1691 | dest.writeInt(mViewIdToKeep); |
| 1692 | } |
| 1693 | |
| 1694 | @Override |
| 1695 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
| 1696 | final ViewGroup target = root.findViewById(viewId); |
| 1697 | |
| 1698 | if (target == null) { |
| 1699 | return; |
| 1700 | } |
| 1701 | |
| 1702 | if (mViewIdToKeep == REMOVE_ALL_VIEWS_ID) { |
| 1703 | target.removeAllViews(); |
| 1704 | return; |
| 1705 | } |
| 1706 | |
| 1707 | removeAllViewsExceptIdToKeep(target); |
| 1708 | } |
| 1709 | |
| 1710 | @Override |
| 1711 | public Action initActionAsync(ViewTree root, ViewGroup rootParent, OnClickHandler handler) { |
| 1712 | // In the async implementation, update the view tree so that subsequent calls to |
| 1713 | // findViewById return the current view. |
| 1714 | root.createTree(); |
| 1715 | ViewTree target = root.findViewTreeById(viewId); |
| 1716 | |
| 1717 | if ((target == null) || !(target.mRoot instanceof ViewGroup)) { |
| 1718 | return ACTION_NOOP; |
| 1719 | } |
| 1720 | |
| 1721 | final ViewGroup targetVg = (ViewGroup) target.mRoot; |
| 1722 | |
| 1723 | // Clear all children when nested views omitted |
| 1724 | target.mChildren = null; |
| 1725 | return new RuntimeAction() { |
| 1726 | @Override |
| 1727 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) |
| 1728 | throws ActionException { |
| 1729 | if (mViewIdToKeep == REMOVE_ALL_VIEWS_ID) { |
| 1730 | targetVg.removeAllViews(); |
| 1731 | return; |
| 1732 | } |
| 1733 | |
| 1734 | removeAllViewsExceptIdToKeep(targetVg); |
| 1735 | } |
| 1736 | }; |
| 1737 | } |
| 1738 | |
| 1739 | /** |
| 1740 | * Iterates through the children in the given ViewGroup and removes all the views that |
| 1741 | * do not have an id of {@link #mViewIdToKeep}. |
| 1742 | */ |
| 1743 | private void removeAllViewsExceptIdToKeep(ViewGroup viewGroup) { |
| 1744 | // Otherwise, remove all the views that do not match the id to keep. |
| 1745 | int index = viewGroup.getChildCount() - 1; |
| 1746 | while (index >= 0) { |
| 1747 | if (viewGroup.getChildAt(index).getId() != mViewIdToKeep) { |
| 1748 | viewGroup.removeViewAt(index); |
| 1749 | } |
| 1750 | index--; |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | @Override |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1755 | public int getActionTag() { |
| 1756 | return VIEW_GROUP_ACTION_REMOVE_TAG; |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | @Override |
| 1760 | public int mergeBehavior() { |
| 1761 | return MERGE_APPEND; |
| 1762 | } |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 1763 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1764 | |
| 1765 | /** |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 1766 | * Helper action to set compound drawables on a TextView. Supports relative |
| 1767 | * (s/t/e/b) or cardinal (l/t/r/b) arrangement. |
| 1768 | */ |
| 1769 | private class TextViewDrawableAction extends Action { |
| 1770 | public TextViewDrawableAction(int viewId, boolean isRelative, int d1, int d2, int d3, int d4) { |
| 1771 | this.viewId = viewId; |
| 1772 | this.isRelative = isRelative; |
Dan Sandler | 912282e | 2015-07-28 22:49:30 -0400 | [diff] [blame] | 1773 | this.useIcons = false; |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 1774 | this.d1 = d1; |
| 1775 | this.d2 = d2; |
| 1776 | this.d3 = d3; |
| 1777 | this.d4 = d4; |
| 1778 | } |
| 1779 | |
Dan Sandler | 912282e | 2015-07-28 22:49:30 -0400 | [diff] [blame] | 1780 | public TextViewDrawableAction(int viewId, boolean isRelative, |
| 1781 | Icon i1, Icon i2, Icon i3, Icon i4) { |
| 1782 | this.viewId = viewId; |
| 1783 | this.isRelative = isRelative; |
| 1784 | this.useIcons = true; |
| 1785 | this.i1 = i1; |
| 1786 | this.i2 = i2; |
| 1787 | this.i3 = i3; |
| 1788 | this.i4 = i4; |
| 1789 | } |
| 1790 | |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 1791 | public TextViewDrawableAction(Parcel parcel) { |
| 1792 | viewId = parcel.readInt(); |
| 1793 | isRelative = (parcel.readInt() != 0); |
Dan Sandler | 912282e | 2015-07-28 22:49:30 -0400 | [diff] [blame] | 1794 | useIcons = (parcel.readInt() != 0); |
| 1795 | if (useIcons) { |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1796 | i1 = parcel.readTypedObject(Icon.CREATOR); |
| 1797 | i2 = parcel.readTypedObject(Icon.CREATOR); |
| 1798 | i3 = parcel.readTypedObject(Icon.CREATOR); |
| 1799 | i4 = parcel.readTypedObject(Icon.CREATOR); |
Dan Sandler | 912282e | 2015-07-28 22:49:30 -0400 | [diff] [blame] | 1800 | } else { |
| 1801 | d1 = parcel.readInt(); |
| 1802 | d2 = parcel.readInt(); |
| 1803 | d3 = parcel.readInt(); |
| 1804 | d4 = parcel.readInt(); |
| 1805 | } |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | public void writeToParcel(Parcel dest, int flags) { |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 1809 | dest.writeInt(viewId); |
| 1810 | dest.writeInt(isRelative ? 1 : 0); |
Dan Sandler | 912282e | 2015-07-28 22:49:30 -0400 | [diff] [blame] | 1811 | dest.writeInt(useIcons ? 1 : 0); |
| 1812 | if (useIcons) { |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1813 | dest.writeTypedObject(i1, 0); |
| 1814 | dest.writeTypedObject(i2, 0); |
| 1815 | dest.writeTypedObject(i3, 0); |
| 1816 | dest.writeTypedObject(i4, 0); |
Dan Sandler | 912282e | 2015-07-28 22:49:30 -0400 | [diff] [blame] | 1817 | } else { |
| 1818 | dest.writeInt(d1); |
| 1819 | dest.writeInt(d2); |
| 1820 | dest.writeInt(d3); |
| 1821 | dest.writeInt(d4); |
| 1822 | } |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | @Override |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 1826 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 1827 | final TextView target = root.findViewById(viewId); |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 1828 | if (target == null) return; |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1829 | if (drawablesLoaded) { |
| 1830 | if (isRelative) { |
| 1831 | target.setCompoundDrawablesRelativeWithIntrinsicBounds(id1, id2, id3, id4); |
| 1832 | } else { |
| 1833 | target.setCompoundDrawablesWithIntrinsicBounds(id1, id2, id3, id4); |
| 1834 | } |
| 1835 | } else if (useIcons) { |
Dan Sandler | 912282e | 2015-07-28 22:49:30 -0400 | [diff] [blame] | 1836 | final Context ctx = target.getContext(); |
| 1837 | final Drawable id1 = i1 == null ? null : i1.loadDrawable(ctx); |
| 1838 | final Drawable id2 = i2 == null ? null : i2.loadDrawable(ctx); |
| 1839 | final Drawable id3 = i3 == null ? null : i3.loadDrawable(ctx); |
| 1840 | final Drawable id4 = i4 == null ? null : i4.loadDrawable(ctx); |
| 1841 | if (isRelative) { |
| 1842 | target.setCompoundDrawablesRelativeWithIntrinsicBounds(id1, id2, id3, id4); |
| 1843 | } else { |
| 1844 | target.setCompoundDrawablesWithIntrinsicBounds(id1, id2, id3, id4); |
| 1845 | } |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 1846 | } else { |
Dan Sandler | 912282e | 2015-07-28 22:49:30 -0400 | [diff] [blame] | 1847 | if (isRelative) { |
| 1848 | target.setCompoundDrawablesRelativeWithIntrinsicBounds(d1, d2, d3, d4); |
| 1849 | } else { |
| 1850 | target.setCompoundDrawablesWithIntrinsicBounds(d1, d2, d3, d4); |
| 1851 | } |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 1852 | } |
| 1853 | } |
| 1854 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1855 | @Override |
| 1856 | public Action initActionAsync(ViewTree root, ViewGroup rootParent, OnClickHandler handler) { |
Alan Viverette | 04fd470 | 2017-04-13 16:37:06 -0400 | [diff] [blame] | 1857 | final TextView target = root.findViewById(viewId); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1858 | if (target == null) return ACTION_NOOP; |
| 1859 | |
| 1860 | TextViewDrawableAction copy = useIcons ? |
| 1861 | new TextViewDrawableAction(viewId, isRelative, i1, i2, i3, i4) : |
| 1862 | new TextViewDrawableAction(viewId, isRelative, d1, d2, d3, d4); |
| 1863 | |
| 1864 | // Load the drawables on the background thread. |
| 1865 | copy.drawablesLoaded = true; |
| 1866 | final Context ctx = target.getContext(); |
| 1867 | |
| 1868 | if (useIcons) { |
| 1869 | copy.id1 = i1 == null ? null : i1.loadDrawable(ctx); |
| 1870 | copy.id2 = i2 == null ? null : i2.loadDrawable(ctx); |
| 1871 | copy.id3 = i3 == null ? null : i3.loadDrawable(ctx); |
| 1872 | copy.id4 = i4 == null ? null : i4.loadDrawable(ctx); |
| 1873 | } else { |
| 1874 | copy.id1 = d1 == 0 ? null : ctx.getDrawable(d1); |
| 1875 | copy.id2 = d2 == 0 ? null : ctx.getDrawable(d2); |
| 1876 | copy.id3 = d3 == 0 ? null : ctx.getDrawable(d3); |
| 1877 | copy.id4 = d4 == 0 ? null : ctx.getDrawable(d4); |
| 1878 | } |
| 1879 | return copy; |
| 1880 | } |
| 1881 | |
Sunny Goyal | 5c02263 | 2016-02-17 16:30:41 -0800 | [diff] [blame] | 1882 | @Override |
| 1883 | public boolean prefersAsyncApply() { |
| 1884 | return useIcons; |
| 1885 | } |
| 1886 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1887 | @Override |
| 1888 | public int getActionTag() { |
| 1889 | return TEXT_VIEW_DRAWABLE_ACTION_TAG; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
Jeff Sharkey | 23b3118 | 2018-04-18 21:32:12 -0600 | [diff] [blame] | 1892 | @Override |
| 1893 | public void visitUris(@NonNull Consumer<Uri> visitor) { |
| 1894 | if (useIcons) { |
| 1895 | visitIconUri(i1, visitor); |
| 1896 | visitIconUri(i2, visitor); |
| 1897 | visitIconUri(i3, visitor); |
| 1898 | visitIconUri(i4, visitor); |
| 1899 | } |
| 1900 | } |
| 1901 | |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 1902 | boolean isRelative = false; |
Dan Sandler | 912282e | 2015-07-28 22:49:30 -0400 | [diff] [blame] | 1903 | boolean useIcons = false; |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 1904 | int d1, d2, d3, d4; |
Dan Sandler | 912282e | 2015-07-28 22:49:30 -0400 | [diff] [blame] | 1905 | Icon i1, i2, i3, i4; |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 1906 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 1907 | boolean drawablesLoaded = false; |
| 1908 | Drawable id1, id2, id3, id4; |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | /** |
Daniel Sandler | 99d1f74 | 2012-05-21 16:14:14 -0400 | [diff] [blame] | 1912 | * Helper action to set text size on a TextView in any supported units. |
Daniel Sandler | 7264f71 | 2012-05-21 14:48:23 -0400 | [diff] [blame] | 1913 | */ |
| 1914 | private class TextViewSizeAction extends Action { |
| 1915 | public TextViewSizeAction(int viewId, int units, float size) { |
| 1916 | this.viewId = viewId; |
| 1917 | this.units = units; |
| 1918 | this.size = size; |
| 1919 | } |
| 1920 | |
| 1921 | public TextViewSizeAction(Parcel parcel) { |
| 1922 | viewId = parcel.readInt(); |
| 1923 | units = parcel.readInt(); |
| 1924 | size = parcel.readFloat(); |
| 1925 | } |
| 1926 | |
| 1927 | public void writeToParcel(Parcel dest, int flags) { |
Daniel Sandler | 7264f71 | 2012-05-21 14:48:23 -0400 | [diff] [blame] | 1928 | dest.writeInt(viewId); |
| 1929 | dest.writeInt(units); |
| 1930 | dest.writeFloat(size); |
| 1931 | } |
| 1932 | |
| 1933 | @Override |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 1934 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
Alan Viverette | 8e1a729 | 2017-02-27 10:57:58 -0500 | [diff] [blame] | 1935 | final TextView target = root.findViewById(viewId); |
Daniel Sandler | 7264f71 | 2012-05-21 14:48:23 -0400 | [diff] [blame] | 1936 | if (target == null) return; |
| 1937 | target.setTextSize(units, size); |
| 1938 | } |
| 1939 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1940 | @Override |
| 1941 | public int getActionTag() { |
| 1942 | return TEXT_VIEW_SIZE_ACTION_TAG; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1943 | } |
| 1944 | |
Daniel Sandler | 7264f71 | 2012-05-21 14:48:23 -0400 | [diff] [blame] | 1945 | int units; |
| 1946 | float size; |
Daniel Sandler | 7264f71 | 2012-05-21 14:48:23 -0400 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | /** |
Daniel Sandler | 99d1f74 | 2012-05-21 16:14:14 -0400 | [diff] [blame] | 1950 | * Helper action to set padding on a View. |
| 1951 | */ |
| 1952 | private class ViewPaddingAction extends Action { |
| 1953 | public ViewPaddingAction(int viewId, int left, int top, int right, int bottom) { |
| 1954 | this.viewId = viewId; |
| 1955 | this.left = left; |
| 1956 | this.top = top; |
| 1957 | this.right = right; |
| 1958 | this.bottom = bottom; |
| 1959 | } |
| 1960 | |
| 1961 | public ViewPaddingAction(Parcel parcel) { |
| 1962 | viewId = parcel.readInt(); |
| 1963 | left = parcel.readInt(); |
| 1964 | top = parcel.readInt(); |
| 1965 | right = parcel.readInt(); |
| 1966 | bottom = parcel.readInt(); |
| 1967 | } |
| 1968 | |
| 1969 | public void writeToParcel(Parcel dest, int flags) { |
Daniel Sandler | 99d1f74 | 2012-05-21 16:14:14 -0400 | [diff] [blame] | 1970 | dest.writeInt(viewId); |
| 1971 | dest.writeInt(left); |
| 1972 | dest.writeInt(top); |
| 1973 | dest.writeInt(right); |
| 1974 | dest.writeInt(bottom); |
| 1975 | } |
| 1976 | |
| 1977 | @Override |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 1978 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
Daniel Sandler | 99d1f74 | 2012-05-21 16:14:14 -0400 | [diff] [blame] | 1979 | final View target = root.findViewById(viewId); |
| 1980 | if (target == null) return; |
| 1981 | target.setPadding(left, top, right, bottom); |
| 1982 | } |
| 1983 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 1984 | @Override |
| 1985 | public int getActionTag() { |
| 1986 | return VIEW_PADDING_ACTION_TAG; |
Adam Cohen | fbe44b7 | 2012-09-19 20:36:23 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
Daniel Sandler | 99d1f74 | 2012-05-21 16:14:14 -0400 | [diff] [blame] | 1989 | int left, top, right, bottom; |
Daniel Sandler | 99d1f74 | 2012-05-21 16:14:14 -0400 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | /** |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 1993 | * Helper action to set layout params on a View. |
Selim Cinek | 65b2e7c | 2015-10-26 14:11:31 -0700 | [diff] [blame] | 1994 | */ |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 1995 | private static class LayoutParamAction extends Action { |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 1996 | |
| 1997 | /** Set marginEnd */ |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 1998 | public static final int LAYOUT_MARGIN_END_DIMEN = 1; |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 1999 | /** Set width */ |
| 2000 | public static final int LAYOUT_WIDTH = 2; |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 2001 | public static final int LAYOUT_MARGIN_BOTTOM_DIMEN = 3; |
Selim Cinek | 384804b | 2018-04-18 14:31:07 +0800 | [diff] [blame] | 2002 | public static final int LAYOUT_MARGIN_END = 4; |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 2003 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2004 | final int mProperty; |
| 2005 | final int mValue; |
| 2006 | |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 2007 | /** |
| 2008 | * @param viewId ID of the view alter |
| 2009 | * @param property which layout parameter to alter |
| 2010 | * @param value new value of the layout parameter |
| 2011 | */ |
| 2012 | public LayoutParamAction(int viewId, int property, int value) { |
Selim Cinek | 65b2e7c | 2015-10-26 14:11:31 -0700 | [diff] [blame] | 2013 | this.viewId = viewId; |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2014 | this.mProperty = property; |
| 2015 | this.mValue = value; |
Selim Cinek | 65b2e7c | 2015-10-26 14:11:31 -0700 | [diff] [blame] | 2016 | } |
| 2017 | |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 2018 | public LayoutParamAction(Parcel parcel) { |
Selim Cinek | 65b2e7c | 2015-10-26 14:11:31 -0700 | [diff] [blame] | 2019 | viewId = parcel.readInt(); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2020 | mProperty = parcel.readInt(); |
| 2021 | mValue = parcel.readInt(); |
Selim Cinek | 65b2e7c | 2015-10-26 14:11:31 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | public void writeToParcel(Parcel dest, int flags) { |
Selim Cinek | 65b2e7c | 2015-10-26 14:11:31 -0700 | [diff] [blame] | 2025 | dest.writeInt(viewId); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2026 | dest.writeInt(mProperty); |
| 2027 | dest.writeInt(mValue); |
Selim Cinek | 65b2e7c | 2015-10-26 14:11:31 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | @Override |
| 2031 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
| 2032 | final View target = root.findViewById(viewId); |
| 2033 | if (target == null) { |
| 2034 | return; |
| 2035 | } |
| 2036 | ViewGroup.LayoutParams layoutParams = target.getLayoutParams(); |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 2037 | if (layoutParams == null) { |
| 2038 | return; |
| 2039 | } |
Selim Cinek | 384804b | 2018-04-18 14:31:07 +0800 | [diff] [blame] | 2040 | int value = mValue; |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2041 | switch (mProperty) { |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 2042 | case LAYOUT_MARGIN_END_DIMEN: |
Selim Cinek | 384804b | 2018-04-18 14:31:07 +0800 | [diff] [blame] | 2043 | value = resolveDimenPixelOffset(target, mValue); |
| 2044 | // fall-through |
| 2045 | case LAYOUT_MARGIN_END: |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 2046 | if (layoutParams instanceof ViewGroup.MarginLayoutParams) { |
Selim Cinek | 384804b | 2018-04-18 14:31:07 +0800 | [diff] [blame] | 2047 | ((ViewGroup.MarginLayoutParams) layoutParams).setMarginEnd(value); |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 2048 | target.setLayoutParams(layoutParams); |
| 2049 | } |
| 2050 | break; |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 2051 | case LAYOUT_MARGIN_BOTTOM_DIMEN: |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 2052 | if (layoutParams instanceof ViewGroup.MarginLayoutParams) { |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2053 | int resolved = resolveDimenPixelOffset(target, mValue); |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 2054 | ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin = resolved; |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 2055 | target.setLayoutParams(layoutParams); |
| 2056 | } |
| 2057 | break; |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 2058 | case LAYOUT_WIDTH: |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2059 | layoutParams.width = mValue; |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 2060 | target.setLayoutParams(layoutParams); |
| 2061 | break; |
| 2062 | default: |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2063 | throw new IllegalArgumentException("Unknown property " + mProperty); |
Selim Cinek | 65b2e7c | 2015-10-26 14:11:31 -0700 | [diff] [blame] | 2064 | } |
| 2065 | } |
| 2066 | |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 2067 | private static int resolveDimenPixelOffset(View target, int value) { |
| 2068 | if (value == 0) { |
| 2069 | return 0; |
| 2070 | } |
| 2071 | return target.getContext().getResources().getDimensionPixelOffset(value); |
| 2072 | } |
| 2073 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2074 | @Override |
| 2075 | public int getActionTag() { |
| 2076 | return LAYOUT_PARAM_ACTION_TAG; |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | @Override |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2080 | public String getUniqueKey() { |
| 2081 | return super.getUniqueKey() + mProperty; |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2082 | } |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | /** |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 2086 | * Helper action to add a view tag with RemoteInputs. |
| 2087 | */ |
| 2088 | private class SetRemoteInputsAction extends Action { |
| 2089 | |
| 2090 | public SetRemoteInputsAction(int viewId, RemoteInput[] remoteInputs) { |
| 2091 | this.viewId = viewId; |
| 2092 | this.remoteInputs = remoteInputs; |
| 2093 | } |
| 2094 | |
| 2095 | public SetRemoteInputsAction(Parcel parcel) { |
| 2096 | viewId = parcel.readInt(); |
Adrian Roos | 5dd685f | 2016-02-24 12:05:51 -0800 | [diff] [blame] | 2097 | remoteInputs = parcel.createTypedArray(RemoteInput.CREATOR); |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | public void writeToParcel(Parcel dest, int flags) { |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 2101 | dest.writeInt(viewId); |
Adrian Roos | 5dd685f | 2016-02-24 12:05:51 -0800 | [diff] [blame] | 2102 | dest.writeTypedArray(remoteInputs, flags); |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | @Override |
| 2106 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
Selim Cinek | 5d6ef8d | 2017-05-18 22:16:00 -0700 | [diff] [blame] | 2107 | final View target = root.findViewById(viewId); |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 2108 | if (target == null) return; |
| 2109 | |
| 2110 | target.setTagInternal(R.id.remote_input_tag, remoteInputs); |
| 2111 | } |
| 2112 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2113 | @Override |
| 2114 | public int getActionTag() { |
| 2115 | return SET_REMOTE_INPUTS_ACTION_TAG; |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | final Parcelable[] remoteInputs; |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | /** |
Selim Cinek | 87c3153 | 2017-08-18 18:53:44 -0700 | [diff] [blame] | 2122 | * Helper action to override all textViewColors |
| 2123 | */ |
| 2124 | private class OverrideTextColorsAction extends Action { |
| 2125 | |
| 2126 | private final int textColor; |
| 2127 | |
| 2128 | public OverrideTextColorsAction(int textColor) { |
| 2129 | this.textColor = textColor; |
| 2130 | } |
| 2131 | |
| 2132 | public OverrideTextColorsAction(Parcel parcel) { |
| 2133 | textColor = parcel.readInt(); |
| 2134 | } |
| 2135 | |
| 2136 | public void writeToParcel(Parcel dest, int flags) { |
Selim Cinek | 87c3153 | 2017-08-18 18:53:44 -0700 | [diff] [blame] | 2137 | dest.writeInt(textColor); |
| 2138 | } |
| 2139 | |
| 2140 | @Override |
| 2141 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
| 2142 | // Let's traverse the viewtree and override all textColors! |
| 2143 | Stack<View> viewsToProcess = new Stack<>(); |
| 2144 | viewsToProcess.add(root); |
| 2145 | while (!viewsToProcess.isEmpty()) { |
| 2146 | View v = viewsToProcess.pop(); |
| 2147 | if (v instanceof TextView) { |
| 2148 | TextView textView = (TextView) v; |
Lucas Dupin | a291d19 | 2018-06-07 13:59:42 -0700 | [diff] [blame] | 2149 | textView.setText(ContrastColorUtil.clearColorSpans(textView.getText())); |
Selim Cinek | 87c3153 | 2017-08-18 18:53:44 -0700 | [diff] [blame] | 2150 | textView.setTextColor(textColor); |
| 2151 | } |
| 2152 | if (v instanceof ViewGroup) { |
| 2153 | ViewGroup viewGroup = (ViewGroup) v; |
| 2154 | for (int i = 0; i < viewGroup.getChildCount(); i++) { |
| 2155 | viewsToProcess.push(viewGroup.getChildAt(i)); |
| 2156 | } |
| 2157 | } |
| 2158 | } |
| 2159 | } |
| 2160 | |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2161 | @Override |
| 2162 | public int getActionTag() { |
| 2163 | return OVERRIDE_TEXT_COLORS_TAG; |
Selim Cinek | 87c3153 | 2017-08-18 18:53:44 -0700 | [diff] [blame] | 2164 | } |
| 2165 | } |
| 2166 | |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 2167 | private class SetIntTagAction extends Action { |
| 2168 | private final int mViewId; |
| 2169 | private final int mKey; |
| 2170 | private final int mTag; |
| 2171 | |
| 2172 | SetIntTagAction(int viewId, int key, int tag) { |
| 2173 | mViewId = viewId; |
| 2174 | mKey = key; |
| 2175 | mTag = tag; |
| 2176 | } |
| 2177 | |
| 2178 | SetIntTagAction(Parcel parcel) { |
| 2179 | mViewId = parcel.readInt(); |
| 2180 | mKey = parcel.readInt(); |
| 2181 | mTag = parcel.readInt(); |
| 2182 | } |
| 2183 | |
| 2184 | public void writeToParcel(Parcel dest, int flags) { |
| 2185 | dest.writeInt(mViewId); |
| 2186 | dest.writeInt(mKey); |
| 2187 | dest.writeInt(mTag); |
| 2188 | } |
| 2189 | |
| 2190 | @Override |
| 2191 | public void apply(View root, ViewGroup rootParent, OnClickHandler handler) { |
| 2192 | final View target = root.findViewById(mViewId); |
| 2193 | if (target == null) return; |
| 2194 | |
| 2195 | target.setTagInternal(mKey, mTag); |
| 2196 | } |
| 2197 | |
| 2198 | @Override |
| 2199 | public int getActionTag() { |
| 2200 | return SET_INT_TAG_TAG; |
| 2201 | } |
| 2202 | } |
| 2203 | |
Selim Cinek | 87c3153 | 2017-08-18 18:53:44 -0700 | [diff] [blame] | 2204 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2205 | * Create a new RemoteViews object that will display the views contained |
| 2206 | * in the specified layout file. |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2207 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2208 | * @param packageName Name of the package that contains the layout resource |
| 2209 | * @param layoutId The id of the layout resource |
| 2210 | */ |
| 2211 | public RemoteViews(String packageName, int layoutId) { |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 2212 | this(getApplicationInfo(packageName, UserHandle.myUserId()), layoutId); |
| 2213 | } |
| 2214 | |
| 2215 | /** |
| 2216 | * Create a new RemoteViews object that will display the views contained |
| 2217 | * in the specified layout file. |
| 2218 | * |
| 2219 | * @param packageName Name of the package that contains the layout resource. |
| 2220 | * @param userId The user under which the package is running. |
| 2221 | * @param layoutId The id of the layout resource. |
| 2222 | * |
| 2223 | * @hide |
| 2224 | */ |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 2225 | public RemoteViews(String packageName, int userId, @LayoutRes int layoutId) { |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 2226 | this(getApplicationInfo(packageName, userId), layoutId); |
| 2227 | } |
| 2228 | |
| 2229 | /** |
| 2230 | * Create a new RemoteViews object that will display the views contained |
| 2231 | * in the specified layout file. |
| 2232 | * |
| 2233 | * @param application The application whose content is shown by the views. |
| 2234 | * @param layoutId The id of the layout resource. |
Kenny Guy | 7732006 | 2014-08-27 21:37:15 +0100 | [diff] [blame] | 2235 | * |
| 2236 | * @hide |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 2237 | */ |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 2238 | protected RemoteViews(ApplicationInfo application, @LayoutRes int layoutId) { |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 2239 | mApplication = application; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2240 | mLayoutId = layoutId; |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2241 | mBitmapCache = new BitmapCache(); |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 2242 | mClassCookies = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2243 | } |
| 2244 | |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2245 | private boolean hasLandscapeAndPortraitLayouts() { |
| 2246 | return (mLandscape != null) && (mPortrait != null); |
| 2247 | } |
| 2248 | |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2249 | /** |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2250 | * Create a new RemoteViews object that will inflate as the specified |
| 2251 | * landspace or portrait RemoteViews, depending on the current configuration. |
| 2252 | * |
| 2253 | * @param landscape The RemoteViews to inflate in landscape configuration |
| 2254 | * @param portrait The RemoteViews to inflate in portrait configuration |
| 2255 | */ |
| 2256 | public RemoteViews(RemoteViews landscape, RemoteViews portrait) { |
| 2257 | if (landscape == null || portrait == null) { |
| 2258 | throw new RuntimeException("Both RemoteViews must be non-null"); |
| 2259 | } |
Sunny Goyal | dd60f4d | 2017-10-18 15:22:42 -0700 | [diff] [blame] | 2260 | if (!landscape.hasSameAppInfo(portrait.mApplication)) { |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 2261 | throw new RuntimeException("Both RemoteViews must share the same package and user"); |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2262 | } |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 2263 | mApplication = portrait.mApplication; |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 2264 | mLayoutId = portrait.mLayoutId; |
| 2265 | mLightBackgroundLayoutId = portrait.mLightBackgroundLayoutId; |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2266 | |
| 2267 | mLandscape = landscape; |
| 2268 | mPortrait = portrait; |
| 2269 | |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2270 | mBitmapCache = new BitmapCache(); |
| 2271 | configureRemoteViewsAsChild(landscape); |
| 2272 | configureRemoteViewsAsChild(portrait); |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 2273 | |
| 2274 | mClassCookies = (portrait.mClassCookies != null) |
| 2275 | ? portrait.mClassCookies : landscape.mClassCookies; |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2276 | } |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2277 | |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2278 | /** |
| 2279 | * Creates a copy of another RemoteViews. |
| 2280 | */ |
| 2281 | public RemoteViews(RemoteViews src) { |
| 2282 | mBitmapCache = src.mBitmapCache; |
| 2283 | mApplication = src.mApplication; |
| 2284 | mIsRoot = src.mIsRoot; |
| 2285 | mLayoutId = src.mLayoutId; |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 2286 | mLightBackgroundLayoutId = src.mLightBackgroundLayoutId; |
| 2287 | mApplyFlags = src.mApplyFlags; |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 2288 | mClassCookies = src.mClassCookies; |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2289 | |
| 2290 | if (src.hasLandscapeAndPortraitLayouts()) { |
| 2291 | mLandscape = new RemoteViews(src.mLandscape); |
| 2292 | mPortrait = new RemoteViews(src.mPortrait); |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | if (src.mActions != null) { |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2296 | Parcel p = Parcel.obtain(); |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 2297 | p.putClassCookies(mClassCookies); |
Sunny Goyal | 0ebf881 | 2017-09-25 10:02:59 -0700 | [diff] [blame] | 2298 | src.writeActionsToParcel(p); |
Sunny Goyal | 5b7689f | 2017-09-21 11:08:34 -0700 | [diff] [blame] | 2299 | p.setDataPosition(0); |
| 2300 | // Since src is already in memory, we do not care about stack overflow as it has |
| 2301 | // already been read once. |
| 2302 | readActionsFromParcel(p, 0); |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2303 | p.recycle(); |
| 2304 | } |
| 2305 | |
| 2306 | // Now that everything is initialized and duplicated, setting a new BitmapCache will |
| 2307 | // re-initialize the cache. |
| 2308 | setBitmapCache(new BitmapCache()); |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2309 | } |
| 2310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2311 | /** |
| 2312 | * Reads a RemoteViews object from a parcel. |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2313 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2314 | * @param parcel |
| 2315 | */ |
| 2316 | public RemoteViews(Parcel parcel) { |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 2317 | this(parcel, null, null, 0, null); |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2318 | } |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 2319 | |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 2320 | private RemoteViews(Parcel parcel, BitmapCache bitmapCache, ApplicationInfo info, int depth, |
| 2321 | Map<Class, Object> classCookies) { |
Sunny Goyal | 692f8c9 | 2016-11-11 09:19:14 -0800 | [diff] [blame] | 2322 | if (depth > MAX_NESTED_VIEWS |
| 2323 | && (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID)) { |
| 2324 | throw new IllegalArgumentException("Too many nested views."); |
| 2325 | } |
| 2326 | depth++; |
| 2327 | |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2328 | int mode = parcel.readInt(); |
| 2329 | |
| 2330 | // We only store a bitmap cache in the root of the RemoteViews. |
| 2331 | if (bitmapCache == null) { |
| 2332 | mBitmapCache = new BitmapCache(parcel); |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 2333 | // Store the class cookies such that they are available when we clone this RemoteView. |
| 2334 | mClassCookies = parcel.copyClassCookies(); |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2335 | } else { |
| 2336 | setBitmapCache(bitmapCache); |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 2337 | mClassCookies = classCookies; |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2338 | setNotRoot(); |
| 2339 | } |
| 2340 | |
| 2341 | if (mode == MODE_NORMAL) { |
Sunny Goyal | 5d8bcdf | 2016-10-27 16:11:29 -0700 | [diff] [blame] | 2342 | mApplication = parcel.readInt() == 0 ? info : |
| 2343 | ApplicationInfo.CREATOR.createFromParcel(parcel); |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2344 | mLayoutId = parcel.readInt(); |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 2345 | mLightBackgroundLayoutId = parcel.readInt(); |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2346 | |
Sunny Goyal | 5b7689f | 2017-09-21 11:08:34 -0700 | [diff] [blame] | 2347 | readActionsFromParcel(parcel, depth); |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2348 | } else { |
| 2349 | // MODE_HAS_LANDSCAPE_AND_PORTRAIT |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 2350 | mLandscape = new RemoteViews(parcel, mBitmapCache, info, depth, mClassCookies); |
| 2351 | mPortrait = new RemoteViews(parcel, mBitmapCache, mLandscape.mApplication, depth, |
| 2352 | mClassCookies); |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 2353 | mApplication = mPortrait.mApplication; |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 2354 | mLayoutId = mPortrait.mLayoutId; |
| 2355 | mLightBackgroundLayoutId = mPortrait.mLightBackgroundLayoutId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2356 | } |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 2357 | mApplyFlags = parcel.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2358 | } |
| 2359 | |
Sunny Goyal | 5b7689f | 2017-09-21 11:08:34 -0700 | [diff] [blame] | 2360 | private void readActionsFromParcel(Parcel parcel, int depth) { |
| 2361 | int count = parcel.readInt(); |
| 2362 | if (count > 0) { |
| 2363 | mActions = new ArrayList<>(count); |
| 2364 | for (int i = 0; i < count; i++) { |
| 2365 | mActions.add(getActionFromParcel(parcel, depth)); |
| 2366 | } |
| 2367 | } |
| 2368 | } |
| 2369 | |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2370 | private Action getActionFromParcel(Parcel parcel, int depth) { |
| 2371 | int tag = parcel.readInt(); |
| 2372 | switch (tag) { |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 2373 | case SET_ON_CLICK_RESPONSE_TAG: |
| 2374 | return new SetOnClickResponse(parcel); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2375 | case SET_DRAWABLE_TINT_TAG: |
| 2376 | return new SetDrawableTint(parcel); |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2377 | case REFLECTION_ACTION_TAG: |
| 2378 | return new ReflectionAction(parcel); |
| 2379 | case VIEW_GROUP_ACTION_ADD_TAG: |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 2380 | return new ViewGroupActionAdd(parcel, mBitmapCache, mApplication, depth, |
| 2381 | mClassCookies); |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2382 | case VIEW_GROUP_ACTION_REMOVE_TAG: |
| 2383 | return new ViewGroupActionRemove(parcel); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2384 | case VIEW_CONTENT_NAVIGATION_TAG: |
| 2385 | return new ViewContentNavigation(parcel); |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2386 | case SET_EMPTY_VIEW_ACTION_TAG: |
| 2387 | return new SetEmptyView(parcel); |
| 2388 | case SET_PENDING_INTENT_TEMPLATE_TAG: |
| 2389 | return new SetPendingIntentTemplate(parcel); |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2390 | case SET_REMOTE_VIEW_ADAPTER_INTENT_TAG: |
| 2391 | return new SetRemoteViewsAdapterIntent(parcel); |
| 2392 | case TEXT_VIEW_DRAWABLE_ACTION_TAG: |
| 2393 | return new TextViewDrawableAction(parcel); |
| 2394 | case TEXT_VIEW_SIZE_ACTION_TAG: |
| 2395 | return new TextViewSizeAction(parcel); |
| 2396 | case VIEW_PADDING_ACTION_TAG: |
| 2397 | return new ViewPaddingAction(parcel); |
| 2398 | case BITMAP_REFLECTION_ACTION_TAG: |
| 2399 | return new BitmapReflectionAction(parcel); |
| 2400 | case SET_REMOTE_VIEW_ADAPTER_LIST_TAG: |
| 2401 | return new SetRemoteViewsAdapterList(parcel); |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2402 | case SET_REMOTE_INPUTS_ACTION_TAG: |
| 2403 | return new SetRemoteInputsAction(parcel); |
| 2404 | case LAYOUT_PARAM_ACTION_TAG: |
| 2405 | return new LayoutParamAction(parcel); |
| 2406 | case OVERRIDE_TEXT_COLORS_TAG: |
| 2407 | return new OverrideTextColorsAction(parcel); |
Gus Prevas | 9cc9660 | 2018-10-11 11:24:23 -0400 | [diff] [blame] | 2408 | case SET_RIPPLE_DRAWABLE_COLOR_TAG: |
| 2409 | return new SetRippleDrawableColor(parcel); |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 2410 | case SET_INT_TAG_TAG: |
| 2411 | return new SetIntTagAction(parcel); |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2412 | default: |
| 2413 | throw new ActionException("Tag " + tag + " not found"); |
| 2414 | } |
| 2415 | }; |
| 2416 | |
Alan Viverette | 0a14ba5 | 2017-06-14 16:54:05 -0400 | [diff] [blame] | 2417 | /** |
| 2418 | * Returns a deep copy of the RemoteViews object. The RemoteView may not be |
| 2419 | * attached to another RemoteView -- it must be the root of a hierarchy. |
| 2420 | * |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2421 | * @deprecated use {@link #RemoteViews(RemoteViews)} instead. |
Alan Viverette | 0a14ba5 | 2017-06-14 16:54:05 -0400 | [diff] [blame] | 2422 | * @throws IllegalStateException if this is not the root of a RemoteView |
| 2423 | * hierarchy |
| 2424 | */ |
| 2425 | @Override |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2426 | @Deprecated |
Alan Viverette | 0a14ba5 | 2017-06-14 16:54:05 -0400 | [diff] [blame] | 2427 | public RemoteViews clone() { |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2428 | Preconditions.checkState(mIsRoot, "RemoteView has been attached to another RemoteView. " |
| 2429 | + "May only clone the root of a RemoteView hierarchy."); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 2430 | |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2431 | return new RemoteViews(this); |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 2432 | } |
| 2433 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2434 | public String getPackage() { |
Svetoslav | b624244 | 2014-09-19 13:21:55 -0700 | [diff] [blame] | 2435 | return (mApplication != null) ? mApplication.packageName : null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2436 | } |
| 2437 | |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2438 | /** |
Adrian Roos | 7da889d | 2016-03-16 18:38:58 -0700 | [diff] [blame] | 2439 | * Returns the layout id of the root layout associated with this RemoteViews. In the case |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2440 | * that the RemoteViews has both a landscape and portrait root, this will return the layout |
| 2441 | * id associated with the portrait layout. |
| 2442 | * |
| 2443 | * @return the layout id. |
| 2444 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2445 | public int getLayoutId() { |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 2446 | return hasFlags(FLAG_USE_LIGHT_BACKGROUND_LAYOUT) && (mLightBackgroundLayoutId != 0) |
| 2447 | ? mLightBackgroundLayoutId : mLayoutId; |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | /** |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2451 | * Recursively sets BitmapCache in the hierarchy and update the bitmap ids. |
| 2452 | */ |
| 2453 | private void setBitmapCache(BitmapCache bitmapCache) { |
| 2454 | mBitmapCache = bitmapCache; |
| 2455 | if (!hasLandscapeAndPortraitLayouts()) { |
| 2456 | if (mActions != null) { |
| 2457 | final int count = mActions.size(); |
| 2458 | for (int i= 0; i < count; ++i) { |
| 2459 | mActions.get(i).setBitmapCache(bitmapCache); |
| 2460 | } |
| 2461 | } |
| 2462 | } else { |
| 2463 | mLandscape.setBitmapCache(bitmapCache); |
| 2464 | mPortrait.setBitmapCache(bitmapCache); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 2465 | } |
| 2466 | } |
| 2467 | |
| 2468 | /** |
| 2469 | * Returns an estimate of the bitmap heap memory usage for this RemoteViews. |
| 2470 | */ |
Adam Cohen | 311c79c | 2012-05-10 14:44:38 -0700 | [diff] [blame] | 2471 | /** @hide */ |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 2472 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Adam Cohen | 311c79c | 2012-05-10 14:44:38 -0700 | [diff] [blame] | 2473 | public int estimateMemoryUsage() { |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2474 | return mBitmapCache.getBitmapMemory(); |
Winson Chung | 3ec9a45 | 2010-09-23 16:40:28 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2478 | * Add an action to be executed on the remote side when apply is called. |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2479 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2480 | * @param a The action to add |
| 2481 | */ |
| 2482 | private void addAction(Action a) { |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 2483 | if (hasLandscapeAndPortraitLayouts()) { |
| 2484 | throw new RuntimeException("RemoteViews specifying separate landscape and portrait" + |
| 2485 | " layouts cannot be modified. Instead, fully configure the landscape and" + |
| 2486 | " portrait layouts individually before constructing the combined layout."); |
| 2487 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2488 | if (mActions == null) { |
Sunny Goyal | 56333a8 | 2017-08-29 13:46:29 -0700 | [diff] [blame] | 2489 | mActions = new ArrayList<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2490 | } |
| 2491 | mActions.add(a); |
| 2492 | } |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 2493 | |
| 2494 | /** |
| 2495 | * Equivalent to calling {@link ViewGroup#addView(View)} after inflating the |
| 2496 | * given {@link RemoteViews}. This allows users to build "nested" |
| 2497 | * {@link RemoteViews}. In cases where consumers of {@link RemoteViews} may |
| 2498 | * recycle layouts, use {@link #removeAllViews(int)} to clear any existing |
| 2499 | * children. |
| 2500 | * |
| 2501 | * @param viewId The id of the parent {@link ViewGroup} to add child into. |
| 2502 | * @param nestedView {@link RemoteViews} that describes the child. |
| 2503 | */ |
| 2504 | public void addView(int viewId, RemoteViews nestedView) { |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 2505 | addAction(nestedView == null |
| 2506 | ? new ViewGroupActionRemove(viewId) |
| 2507 | : new ViewGroupActionAdd(viewId, nestedView)); |
| 2508 | } |
| 2509 | |
| 2510 | /** |
| 2511 | * Equivalent to calling {@link ViewGroup#addView(View, int)} after inflating the |
| 2512 | * given {@link RemoteViews}. |
| 2513 | * |
| 2514 | * @param viewId The id of the parent {@link ViewGroup} to add the child into. |
Sunny Goyal | 271e322 | 2017-08-29 16:05:47 -0700 | [diff] [blame] | 2515 | * @param nestedView {@link RemoteViews} of the child to add. |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 2516 | * @param index The position at which to add the child. |
| 2517 | * |
| 2518 | * @hide |
| 2519 | */ |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 2520 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 2521 | public void addView(int viewId, RemoteViews nestedView, int index) { |
| 2522 | addAction(new ViewGroupActionAdd(viewId, nestedView, index)); |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | /** |
| 2526 | * Equivalent to calling {@link ViewGroup#removeAllViews()}. |
| 2527 | * |
| 2528 | * @param viewId The id of the parent {@link ViewGroup} to remove all |
| 2529 | * children from. |
| 2530 | */ |
| 2531 | public void removeAllViews(int viewId) { |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 2532 | addAction(new ViewGroupActionRemove(viewId)); |
| 2533 | } |
| 2534 | |
| 2535 | /** |
| 2536 | * Removes all views in the {@link ViewGroup} specified by the {@code viewId} except for any |
| 2537 | * child that has the {@code viewIdToKeep} as its id. |
| 2538 | * |
| 2539 | * @param viewId The id of the parent {@link ViewGroup} to remove children from. |
| 2540 | * @param viewIdToKeep The id of a child that should not be removed. |
| 2541 | * |
| 2542 | * @hide |
| 2543 | */ |
| 2544 | public void removeAllViewsExceptId(int viewId, int viewIdToKeep) { |
| 2545 | addAction(new ViewGroupActionRemove(viewId, viewIdToKeep)); |
Jeff Sharkey | 1162fd7 | 2009-11-04 17:58:08 -0800 | [diff] [blame] | 2546 | } |
| 2547 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2548 | /** |
Adam Cohen | 0b96a57 | 2011-02-10 15:56:16 -0800 | [diff] [blame] | 2549 | * Equivalent to calling {@link AdapterViewAnimator#showNext()} |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 2550 | * |
Adam Cohen | 0b96a57 | 2011-02-10 15:56:16 -0800 | [diff] [blame] | 2551 | * @param viewId The id of the view on which to call {@link AdapterViewAnimator#showNext()} |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 2552 | */ |
| 2553 | public void showNext(int viewId) { |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2554 | addAction(new ViewContentNavigation(viewId, true /* next */)); |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | /** |
Adam Cohen | 0b96a57 | 2011-02-10 15:56:16 -0800 | [diff] [blame] | 2558 | * Equivalent to calling {@link AdapterViewAnimator#showPrevious()} |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 2559 | * |
Adam Cohen | 0b96a57 | 2011-02-10 15:56:16 -0800 | [diff] [blame] | 2560 | * @param viewId The id of the view on which to call {@link AdapterViewAnimator#showPrevious()} |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 2561 | */ |
| 2562 | public void showPrevious(int viewId) { |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2563 | addAction(new ViewContentNavigation(viewId, false /* next */)); |
Adam Cohen | 2dd2197 | 2010-08-15 18:20:04 -0700 | [diff] [blame] | 2564 | } |
| 2565 | |
| 2566 | /** |
Adam Cohen | 0b96a57 | 2011-02-10 15:56:16 -0800 | [diff] [blame] | 2567 | * Equivalent to calling {@link AdapterViewAnimator#setDisplayedChild(int)} |
| 2568 | * |
| 2569 | * @param viewId The id of the view on which to call |
| 2570 | * {@link AdapterViewAnimator#setDisplayedChild(int)} |
| 2571 | */ |
| 2572 | public void setDisplayedChild(int viewId, int childIndex) { |
| 2573 | setInt(viewId, "setDisplayedChild", childIndex); |
| 2574 | } |
| 2575 | |
| 2576 | /** |
Kirill Grouchnikov | 40a3978 | 2016-07-01 17:24:32 -0400 | [diff] [blame] | 2577 | * Equivalent to calling {@link View#setVisibility(int)} |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2578 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2579 | * @param viewId The id of the view whose visibility should change |
| 2580 | * @param visibility The new visibility for the view |
| 2581 | */ |
| 2582 | public void setViewVisibility(int viewId, int visibility) { |
| 2583 | setInt(viewId, "setVisibility", visibility); |
| 2584 | } |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 2585 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2586 | /** |
Kirill Grouchnikov | 40a3978 | 2016-07-01 17:24:32 -0400 | [diff] [blame] | 2587 | * Equivalent to calling {@link TextView#setText(CharSequence)} |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2588 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2589 | * @param viewId The id of the view whose text should change |
| 2590 | * @param text The new text for the view |
| 2591 | */ |
| 2592 | public void setTextViewText(int viewId, CharSequence text) { |
| 2593 | setCharSequence(viewId, "setText", text); |
| 2594 | } |
Daniel Sandler | 7264f71 | 2012-05-21 14:48:23 -0400 | [diff] [blame] | 2595 | |
| 2596 | /** |
Daniel Sandler | 7264f71 | 2012-05-21 14:48:23 -0400 | [diff] [blame] | 2597 | * Equivalent to calling {@link TextView#setTextSize(int, float)} |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2598 | * |
Daniel Sandler | 7264f71 | 2012-05-21 14:48:23 -0400 | [diff] [blame] | 2599 | * @param viewId The id of the view whose text size should change |
| 2600 | * @param units The units of size (e.g. COMPLEX_UNIT_SP) |
| 2601 | * @param size The size of the text |
| 2602 | */ |
| 2603 | public void setTextViewTextSize(int viewId, int units, float size) { |
| 2604 | addAction(new TextViewSizeAction(viewId, units, size)); |
| 2605 | } |
| 2606 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2607 | /** |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2608 | * Equivalent to calling |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 2609 | * {@link TextView#setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)}. |
| 2610 | * |
| 2611 | * @param viewId The id of the view whose text should change |
| 2612 | * @param left The id of a drawable to place to the left of the text, or 0 |
| 2613 | * @param top The id of a drawable to place above the text, or 0 |
| 2614 | * @param right The id of a drawable to place to the right of the text, or 0 |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2615 | * @param bottom The id of a drawable to place below the text, or 0 |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 2616 | */ |
| 2617 | public void setTextViewCompoundDrawables(int viewId, int left, int top, int right, int bottom) { |
| 2618 | addAction(new TextViewDrawableAction(viewId, false, left, top, right, bottom)); |
| 2619 | } |
| 2620 | |
| 2621 | /** |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2622 | * Equivalent to calling {@link |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 2623 | * TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(int, int, int, int)}. |
| 2624 | * |
| 2625 | * @param viewId The id of the view whose text should change |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2626 | * @param start The id of a drawable to place before the text (relative to the |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 2627 | * layout direction), or 0 |
| 2628 | * @param top The id of a drawable to place above the text, or 0 |
| 2629 | * @param end The id of a drawable to place after the text, or 0 |
Fabrice Di Meglio | 66388dc | 2012-05-03 18:51:57 -0700 | [diff] [blame] | 2630 | * @param bottom The id of a drawable to place below the text, or 0 |
Daniel Sandler | 820ba32 | 2012-03-23 16:36:00 -0500 | [diff] [blame] | 2631 | */ |
| 2632 | public void setTextViewCompoundDrawablesRelative(int viewId, int start, int top, int end, int bottom) { |
| 2633 | addAction(new TextViewDrawableAction(viewId, true, start, top, end, bottom)); |
| 2634 | } |
| 2635 | |
| 2636 | /** |
Dan Sandler | 912282e | 2015-07-28 22:49:30 -0400 | [diff] [blame] | 2637 | * Equivalent to calling {@link |
| 2638 | * TextView#setCompoundDrawablesWithIntrinsicBounds(Drawable, Drawable, Drawable, Drawable)} |
| 2639 | * using the drawables yielded by {@link Icon#loadDrawable(Context)}. |
| 2640 | * |
| 2641 | * @param viewId The id of the view whose text should change |
| 2642 | * @param left an Icon to place to the left of the text, or 0 |
| 2643 | * @param top an Icon to place above the text, or 0 |
| 2644 | * @param right an Icon to place to the right of the text, or 0 |
| 2645 | * @param bottom an Icon to place below the text, or 0 |
| 2646 | * |
| 2647 | * @hide |
| 2648 | */ |
| 2649 | public void setTextViewCompoundDrawables(int viewId, Icon left, Icon top, Icon right, Icon bottom) { |
| 2650 | addAction(new TextViewDrawableAction(viewId, false, left, top, right, bottom)); |
| 2651 | } |
| 2652 | |
| 2653 | /** |
| 2654 | * Equivalent to calling {@link |
| 2655 | * TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable, Drawable, Drawable, Drawable)} |
| 2656 | * using the drawables yielded by {@link Icon#loadDrawable(Context)}. |
| 2657 | * |
| 2658 | * @param viewId The id of the view whose text should change |
| 2659 | * @param start an Icon to place before the text (relative to the |
| 2660 | * layout direction), or 0 |
| 2661 | * @param top an Icon to place above the text, or 0 |
| 2662 | * @param end an Icon to place after the text, or 0 |
| 2663 | * @param bottom an Icon to place below the text, or 0 |
| 2664 | * |
| 2665 | * @hide |
| 2666 | */ |
| 2667 | public void setTextViewCompoundDrawablesRelative(int viewId, Icon start, Icon top, Icon end, Icon bottom) { |
| 2668 | addAction(new TextViewDrawableAction(viewId, true, start, top, end, bottom)); |
| 2669 | } |
| 2670 | |
| 2671 | /** |
Kirill Grouchnikov | 40a3978 | 2016-07-01 17:24:32 -0400 | [diff] [blame] | 2672 | * Equivalent to calling {@link ImageView#setImageResource(int)} |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2673 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2674 | * @param viewId The id of the view whose drawable should change |
| 2675 | * @param srcId The new resource id for the drawable |
| 2676 | */ |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2677 | public void setImageViewResource(int viewId, int srcId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2678 | setInt(viewId, "setImageResource", srcId); |
| 2679 | } |
| 2680 | |
| 2681 | /** |
Kirill Grouchnikov | 40a3978 | 2016-07-01 17:24:32 -0400 | [diff] [blame] | 2682 | * Equivalent to calling {@link ImageView#setImageURI(Uri)} |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2683 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2684 | * @param viewId The id of the view whose drawable should change |
| 2685 | * @param uri The Uri for the image |
| 2686 | */ |
| 2687 | public void setImageViewUri(int viewId, Uri uri) { |
| 2688 | setUri(viewId, "setImageURI", uri); |
| 2689 | } |
| 2690 | |
| 2691 | /** |
Kirill Grouchnikov | 40a3978 | 2016-07-01 17:24:32 -0400 | [diff] [blame] | 2692 | * Equivalent to calling {@link ImageView#setImageBitmap(Bitmap)} |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2693 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 2694 | * @param viewId The id of the view whose bitmap should change |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2695 | * @param bitmap The new Bitmap for the drawable |
| 2696 | */ |
| 2697 | public void setImageViewBitmap(int viewId, Bitmap bitmap) { |
| 2698 | setBitmap(viewId, "setImageBitmap", bitmap); |
| 2699 | } |
| 2700 | |
| 2701 | /** |
Kirill Grouchnikov | 40a3978 | 2016-07-01 17:24:32 -0400 | [diff] [blame] | 2702 | * Equivalent to calling {@link ImageView#setImageIcon(Icon)} |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 2703 | * |
| 2704 | * @param viewId The id of the view whose bitmap should change |
| 2705 | * @param icon The new Icon for the ImageView |
| 2706 | */ |
| 2707 | public void setImageViewIcon(int viewId, Icon icon) { |
| 2708 | setIcon(viewId, "setImageIcon", icon); |
| 2709 | } |
| 2710 | |
| 2711 | /** |
Kirill Grouchnikov | 40a3978 | 2016-07-01 17:24:32 -0400 | [diff] [blame] | 2712 | * Equivalent to calling {@link AdapterView#setEmptyView(View)} |
Adam Cohen | 1480fdd | 2010-08-25 17:24:53 -0700 | [diff] [blame] | 2713 | * |
| 2714 | * @param viewId The id of the view on which to set the empty view |
| 2715 | * @param emptyViewId The view id of the empty view |
| 2716 | */ |
| 2717 | public void setEmptyView(int viewId, int emptyViewId) { |
| 2718 | addAction(new SetEmptyView(viewId, emptyViewId)); |
| 2719 | } |
| 2720 | |
| 2721 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2722 | * Equivalent to calling {@link Chronometer#setBase Chronometer.setBase}, |
| 2723 | * {@link Chronometer#setFormat Chronometer.setFormat}, |
| 2724 | * and {@link Chronometer#start Chronometer.start()} or |
| 2725 | * {@link Chronometer#stop Chronometer.stop()}. |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2726 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 2727 | * @param viewId The id of the {@link Chronometer} to change |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2728 | * @param base The time at which the timer would have read 0:00. This |
| 2729 | * time should be based off of |
| 2730 | * {@link android.os.SystemClock#elapsedRealtime SystemClock.elapsedRealtime()}. |
| 2731 | * @param format The Chronometer format string, or null to |
| 2732 | * simply display the timer value. |
| 2733 | * @param started True if you want the clock to be started, false if not. |
Selim Cinek | 81c23aa | 2016-02-25 16:23:13 -0800 | [diff] [blame] | 2734 | * |
Selim Cinek | c3b752e | 2016-04-20 16:13:59 -0700 | [diff] [blame] | 2735 | * @see #setChronometerCountDown(int, boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2736 | */ |
| 2737 | public void setChronometer(int viewId, long base, String format, boolean started) { |
| 2738 | setLong(viewId, "setBase", base); |
| 2739 | setString(viewId, "setFormat", format); |
| 2740 | setBoolean(viewId, "setStarted", started); |
| 2741 | } |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2742 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2743 | /** |
Selim Cinek | 81c23aa | 2016-02-25 16:23:13 -0800 | [diff] [blame] | 2744 | * Equivalent to calling {@link Chronometer#setCountDown(boolean) Chronometer.setCountDown} on |
| 2745 | * the chronometer with the given viewId. |
| 2746 | * |
| 2747 | * @param viewId The id of the {@link Chronometer} to change |
| 2748 | * @param isCountDown True if you want the chronometer to count down to base instead of |
| 2749 | * counting up. |
| 2750 | */ |
Selim Cinek | c3b752e | 2016-04-20 16:13:59 -0700 | [diff] [blame] | 2751 | public void setChronometerCountDown(int viewId, boolean isCountDown) { |
Selim Cinek | 81c23aa | 2016-02-25 16:23:13 -0800 | [diff] [blame] | 2752 | setBoolean(viewId, "setCountDown", isCountDown); |
| 2753 | } |
| 2754 | |
| 2755 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2756 | * Equivalent to calling {@link ProgressBar#setMax ProgressBar.setMax}, |
| 2757 | * {@link ProgressBar#setProgress ProgressBar.setProgress}, and |
| 2758 | * {@link ProgressBar#setIndeterminate ProgressBar.setIndeterminate} |
| 2759 | * |
| 2760 | * If indeterminate is true, then the values for max and progress are ignored. |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2761 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 2762 | * @param viewId The id of the {@link ProgressBar} to change |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2763 | * @param max The 100% value for the progress bar |
| 2764 | * @param progress The current value of the progress bar. |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2765 | * @param indeterminate True if the progress bar is indeterminate, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2766 | * false if not. |
| 2767 | */ |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2768 | public void setProgressBar(int viewId, int max, int progress, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2769 | boolean indeterminate) { |
| 2770 | setBoolean(viewId, "setIndeterminate", indeterminate); |
| 2771 | if (!indeterminate) { |
| 2772 | setInt(viewId, "setMax", max); |
| 2773 | setInt(viewId, "setProgress", progress); |
| 2774 | } |
| 2775 | } |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2776 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2777 | /** |
| 2778 | * Equivalent to calling |
| 2779 | * {@link android.view.View#setOnClickListener(android.view.View.OnClickListener)} |
Sunny Goyal | d7d0c69 | 2017-10-12 12:05:14 -0700 | [diff] [blame] | 2780 | * to launch the provided {@link PendingIntent}. The source bounds |
| 2781 | * ({@link Intent#getSourceBounds()}) of the intent will be set to the bounds of the clicked |
| 2782 | * view in screen space. |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 2783 | * Note that any activity options associated with the mPendingIntent may get overridden |
Sunny Goyal | d7d0c69 | 2017-10-12 12:05:14 -0700 | [diff] [blame] | 2784 | * before starting the intent. |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2785 | * |
Adam Cohen | 35ae9ca | 2010-09-20 15:20:41 -0700 | [diff] [blame] | 2786 | * When setting the on-click action of items within collections (eg. {@link ListView}, |
| 2787 | * {@link StackView} etc.), this method will not work. Instead, use {@link |
Kirill Grouchnikov | c5b3010 | 2016-06-28 15:34:13 -0400 | [diff] [blame] | 2788 | * RemoteViews#setPendingIntentTemplate(int, PendingIntent)} in conjunction with |
| 2789 | * {@link RemoteViews#setOnClickFillInIntent(int, Intent)}. |
Adam Cohen | 35ae9ca | 2010-09-20 15:20:41 -0700 | [diff] [blame] | 2790 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2791 | * @param viewId The id of the view that will trigger the {@link PendingIntent} when clicked |
| 2792 | * @param pendingIntent The {@link PendingIntent} to send when user clicks |
| 2793 | */ |
| 2794 | public void setOnClickPendingIntent(int viewId, PendingIntent pendingIntent) { |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 2795 | setOnClickResponse(viewId, RemoteResponse.fromPendingIntent(pendingIntent)); |
| 2796 | } |
| 2797 | |
| 2798 | /** |
| 2799 | * Equivalent of calling |
| 2800 | * {@link android.view.View#setOnClickListener(android.view.View.OnClickListener)} |
| 2801 | * to launch the provided {@link RemoteResponse}. |
| 2802 | * |
| 2803 | * @param viewId The id of the view that will trigger the {@link RemoteResponse} when clicked |
| 2804 | * @param response The {@link RemoteResponse} to send when user clicks |
| 2805 | */ |
| 2806 | public void setOnClickResponse(int viewId, RemoteResponse response) { |
| 2807 | addAction(new SetOnClickResponse(viewId, response)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | /** |
Adam Cohen | 35ae9ca | 2010-09-20 15:20:41 -0700 | [diff] [blame] | 2811 | * When using collections (eg. {@link ListView}, {@link StackView} etc.) in widgets, it is very |
| 2812 | * costly to set PendingIntents on the individual items, and is hence not permitted. Instead |
| 2813 | * this method should be used to set a single PendingIntent template on the collection, and |
| 2814 | * individual items can differentiate their on-click behavior using |
| 2815 | * {@link RemoteViews#setOnClickFillInIntent(int, Intent)}. |
Adam Cohen | ca6fd84 | 2010-09-03 18:10:35 -0700 | [diff] [blame] | 2816 | * |
| 2817 | * @param viewId The id of the collection who's children will use this PendingIntent template |
| 2818 | * when clicked |
| 2819 | * @param pendingIntentTemplate The {@link PendingIntent} to be combined with extras specified |
| 2820 | * by a child of viewId and executed when that child is clicked |
| 2821 | */ |
| 2822 | public void setPendingIntentTemplate(int viewId, PendingIntent pendingIntentTemplate) { |
| 2823 | addAction(new SetPendingIntentTemplate(viewId, pendingIntentTemplate)); |
| 2824 | } |
| 2825 | |
| 2826 | /** |
Adam Cohen | 35ae9ca | 2010-09-20 15:20:41 -0700 | [diff] [blame] | 2827 | * When using collections (eg. {@link ListView}, {@link StackView} etc.) in widgets, it is very |
| 2828 | * costly to set PendingIntents on the individual items, and is hence not permitted. Instead |
| 2829 | * a single PendingIntent template can be set on the collection, see {@link |
| 2830 | * RemoteViews#setPendingIntentTemplate(int, PendingIntent)}, and the individual on-click |
| 2831 | * action of a given item can be distinguished by setting a fillInIntent on that item. The |
| 2832 | * fillInIntent is then combined with the PendingIntent template in order to determine the final |
| 2833 | * intent which will be executed when the item is clicked. This works as follows: any fields |
| 2834 | * which are left blank in the PendingIntent template, but are provided by the fillInIntent |
Kirill Grouchnikov | c5b3010 | 2016-06-28 15:34:13 -0400 | [diff] [blame] | 2835 | * will be overwritten, and the resulting PendingIntent will be used. The rest |
Adam Cohen | 35ae9ca | 2010-09-20 15:20:41 -0700 | [diff] [blame] | 2836 | * of the PendingIntent template will then be filled in with the associated fields that are |
| 2837 | * set in fillInIntent. See {@link Intent#fillIn(Intent, int)} for more details. |
| 2838 | * |
| 2839 | * @param viewId The id of the view on which to set the fillInIntent |
| 2840 | * @param fillInIntent The intent which will be combined with the parent's PendingIntent |
| 2841 | * in order to determine the on-click behavior of the view specified by viewId |
| 2842 | */ |
| 2843 | public void setOnClickFillInIntent(int viewId, Intent fillInIntent) { |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 2844 | setOnClickResponse(viewId, RemoteResponse.fromFillInIntent(fillInIntent)); |
Adam Cohen | 35ae9ca | 2010-09-20 15:20:41 -0700 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2848 | * @hide |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2849 | * Equivalent to calling |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2850 | * {@link Drawable#setColorFilter(int, android.graphics.PorterDuff.Mode)}, |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2851 | * on the {@link Drawable} of a given view. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2852 | * <p> |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2853 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2854 | * @param viewId The id of the view that contains the target |
| 2855 | * {@link Drawable} |
| 2856 | * @param targetBackground If true, apply these parameters to the |
| 2857 | * {@link Drawable} returned by |
| 2858 | * {@link android.view.View#getBackground()}. Otherwise, assume |
| 2859 | * the target view is an {@link ImageView} and apply them to |
| 2860 | * {@link ImageView#getDrawable()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2861 | * @param colorFilter Specify a color for a |
Jorim Jaggi | 17ee3ec | 2014-08-29 03:47:31 +0200 | [diff] [blame] | 2862 | * {@link android.graphics.ColorFilter} for this drawable. This will be ignored if |
| 2863 | * {@code mode} is {@code null}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2864 | * @param mode Specify a PorterDuff mode for this drawable, or null to leave |
| 2865 | * unchanged. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2866 | */ |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 2867 | public void setDrawableTint(int viewId, boolean targetBackground, |
| 2868 | int colorFilter, @NonNull PorterDuff.Mode mode) { |
| 2869 | addAction(new SetDrawableTint(viewId, targetBackground, colorFilter, mode)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | /** |
Jorim Jaggi | ef72a19 | 2014-08-26 21:57:46 +0200 | [diff] [blame] | 2873 | * @hide |
Gus Prevas | 9cc9660 | 2018-10-11 11:24:23 -0400 | [diff] [blame] | 2874 | * Equivalent to calling |
| 2875 | * {@link RippleDrawable#setColor(ColorStateList)} on the {@link Drawable} of a given view, |
| 2876 | * assuming it's a {@link RippleDrawable}. |
| 2877 | * <p> |
| 2878 | * |
| 2879 | * @param viewId The id of the view that contains the target |
| 2880 | * {@link RippleDrawable} |
| 2881 | * @param colorStateList Specify a color for a |
| 2882 | * {@link ColorStateList} for this drawable. |
| 2883 | */ |
| 2884 | public void setRippleDrawableColor(int viewId, ColorStateList colorStateList) { |
| 2885 | addAction(new SetRippleDrawableColor(viewId, colorStateList)); |
| 2886 | } |
| 2887 | |
| 2888 | /** |
| 2889 | * @hide |
Jorim Jaggi | ef72a19 | 2014-08-26 21:57:46 +0200 | [diff] [blame] | 2890 | * Equivalent to calling {@link android.widget.ProgressBar#setProgressTintList}. |
| 2891 | * |
| 2892 | * @param viewId The id of the view whose tint should change |
| 2893 | * @param tint the tint to apply, may be {@code null} to clear tint |
| 2894 | */ |
| 2895 | public void setProgressTintList(int viewId, ColorStateList tint) { |
| 2896 | addAction(new ReflectionAction(viewId, "setProgressTintList", |
| 2897 | ReflectionAction.COLOR_STATE_LIST, tint)); |
| 2898 | } |
| 2899 | |
| 2900 | /** |
| 2901 | * @hide |
| 2902 | * Equivalent to calling {@link android.widget.ProgressBar#setProgressBackgroundTintList}. |
| 2903 | * |
| 2904 | * @param viewId The id of the view whose tint should change |
| 2905 | * @param tint the tint to apply, may be {@code null} to clear tint |
| 2906 | */ |
| 2907 | public void setProgressBackgroundTintList(int viewId, ColorStateList tint) { |
| 2908 | addAction(new ReflectionAction(viewId, "setProgressBackgroundTintList", |
| 2909 | ReflectionAction.COLOR_STATE_LIST, tint)); |
| 2910 | } |
| 2911 | |
| 2912 | /** |
| 2913 | * @hide |
| 2914 | * Equivalent to calling {@link android.widget.ProgressBar#setIndeterminateTintList}. |
| 2915 | * |
| 2916 | * @param viewId The id of the view whose tint should change |
| 2917 | * @param tint the tint to apply, may be {@code null} to clear tint |
| 2918 | */ |
| 2919 | public void setProgressIndeterminateTintList(int viewId, ColorStateList tint) { |
| 2920 | addAction(new ReflectionAction(viewId, "setIndeterminateTintList", |
| 2921 | ReflectionAction.COLOR_STATE_LIST, tint)); |
| 2922 | } |
| 2923 | |
| 2924 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2925 | * Equivalent to calling {@link android.widget.TextView#setTextColor(int)}. |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 2926 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 2927 | * @param viewId The id of the view whose text color should change |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2928 | * @param color Sets the text color for all the states (normal, selected, |
| 2929 | * focused) to be this color. |
| 2930 | */ |
Tor Norbye | 80756e3 | 2015-03-02 09:39:27 -0800 | [diff] [blame] | 2931 | public void setTextColor(int viewId, @ColorInt int color) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2932 | setInt(viewId, "setTextColor", color); |
| 2933 | } |
| 2934 | |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 2935 | /** |
Selim Cinek | 981962e | 2016-07-20 20:41:58 -0700 | [diff] [blame] | 2936 | * @hide |
| 2937 | * Equivalent to calling {@link android.widget.TextView#setTextColor(ColorStateList)}. |
| 2938 | * |
| 2939 | * @param viewId The id of the view whose text color should change |
| 2940 | * @param colors the text colors to set |
| 2941 | */ |
| 2942 | public void setTextColor(int viewId, @ColorInt ColorStateList colors) { |
| 2943 | addAction(new ReflectionAction(viewId, "setTextColor", ReflectionAction.COLOR_STATE_LIST, |
| 2944 | colors)); |
| 2945 | } |
| 2946 | |
| 2947 | /** |
Adam Cohen | 3b4ca10 | 2012-12-14 12:00:41 -0800 | [diff] [blame] | 2948 | * Equivalent to calling {@link android.widget.AbsListView#setRemoteViewsAdapter(Intent)}. |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 2949 | * |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 2950 | * @param appWidgetId The id of the app widget which contains the specified view. (This |
| 2951 | * parameter is ignored in this deprecated method) |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 2952 | * @param viewId The id of the {@link AdapterView} |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 2953 | * @param intent The intent of the service which will be |
| 2954 | * providing data to the RemoteViewsAdapter |
| 2955 | * @deprecated This method has been deprecated. See |
| 2956 | * {@link android.widget.RemoteViews#setRemoteAdapter(int, Intent)} |
| 2957 | */ |
| 2958 | @Deprecated |
| 2959 | public void setRemoteAdapter(int appWidgetId, int viewId, Intent intent) { |
| 2960 | setRemoteAdapter(viewId, intent); |
| 2961 | } |
| 2962 | |
| 2963 | /** |
| 2964 | * Equivalent to calling {@link android.widget.AbsListView#setRemoteViewsAdapter(Intent)}. |
| 2965 | * Can only be used for App Widgets. |
| 2966 | * |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 2967 | * @param viewId The id of the {@link AdapterView} |
Winson Chung | 81f39eb | 2011-01-11 18:05:01 -0800 | [diff] [blame] | 2968 | * @param intent The intent of the service which will be |
| 2969 | * providing data to the RemoteViewsAdapter |
| 2970 | */ |
Winson Chung | 037300b | 2011-03-29 15:40:16 -0700 | [diff] [blame] | 2971 | public void setRemoteAdapter(int viewId, Intent intent) { |
| 2972 | addAction(new SetRemoteViewsAdapterIntent(viewId, intent)); |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 2973 | } |
| 2974 | |
| 2975 | /** |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 2976 | * Creates a simple Adapter for the viewId specified. The viewId must point to an AdapterView, |
| 2977 | * ie. {@link ListView}, {@link GridView}, {@link StackView} or {@link AdapterViewAnimator}. |
| 2978 | * This is a simpler but less flexible approach to populating collection widgets. Its use is |
| 2979 | * encouraged for most scenarios, as long as the total memory within the list of RemoteViews |
| 2980 | * is relatively small (ie. doesn't contain large or numerous Bitmaps, see {@link |
| 2981 | * RemoteViews#setImageViewBitmap}). In the case of numerous images, the use of API is still |
| 2982 | * possible by setting image URIs instead of Bitmaps, see {@link RemoteViews#setImageViewUri}. |
| 2983 | * |
| 2984 | * This API is supported in the compatibility library for previous API levels, see |
| 2985 | * RemoteViewsCompat. |
| 2986 | * |
| 2987 | * @param viewId The id of the {@link AdapterView} |
| 2988 | * @param list The list of RemoteViews which will populate the view specified by viewId. |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 2989 | * @param viewTypeCount The maximum number of unique layout id's used to construct the list of |
| 2990 | * RemoteViews. This count cannot change during the life-cycle of a given widget, so this |
| 2991 | * parameter should account for the maximum possible number of types that may appear in the |
| 2992 | * See {@link Adapter#getViewTypeCount()}. |
Adam Cohen | 33f3aab | 2013-04-17 13:48:17 -0700 | [diff] [blame] | 2993 | * |
| 2994 | * @hide |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 2995 | * @deprecated this appears to have no users outside of UnsupportedAppUsage? |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 2996 | */ |
Adam Powell | f4d604f | 2019-02-04 15:28:42 -0800 | [diff] [blame] | 2997 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Adam Cohen | b00d9f0 | 2013-01-10 14:12:52 -0800 | [diff] [blame] | 2998 | public void setRemoteAdapter(int viewId, ArrayList<RemoteViews> list, int viewTypeCount) { |
| 2999 | addAction(new SetRemoteViewsAdapterList(viewId, list, viewTypeCount)); |
Adam Cohen | 50f3d1b | 2012-12-11 18:36:07 -0800 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | /** |
Kirill Grouchnikov | 40a3978 | 2016-07-01 17:24:32 -0400 | [diff] [blame] | 3003 | * Equivalent to calling {@link ListView#smoothScrollToPosition(int)}. |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 3004 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3005 | * @param viewId The id of the view to change |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 3006 | * @param position Scroll to this adapter position |
| 3007 | */ |
| 3008 | public void setScrollPosition(int viewId, int position) { |
| 3009 | setInt(viewId, "smoothScrollToPosition", position); |
| 3010 | } |
| 3011 | |
| 3012 | /** |
Kirill Grouchnikov | 40a3978 | 2016-07-01 17:24:32 -0400 | [diff] [blame] | 3013 | * Equivalent to calling {@link ListView#smoothScrollByOffset(int)}. |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 3014 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3015 | * @param viewId The id of the view to change |
Winson Chung | 9536259 | 2010-07-19 16:05:50 -0700 | [diff] [blame] | 3016 | * @param offset Scroll by this adapter position offset |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 3017 | */ |
| 3018 | public void setRelativeScrollPosition(int viewId, int offset) { |
| 3019 | setInt(viewId, "smoothScrollByOffset", offset); |
| 3020 | } |
| 3021 | |
| 3022 | /** |
Daniel Sandler | d5353b4 | 2012-06-21 09:28:07 -0400 | [diff] [blame] | 3023 | * Equivalent to calling {@link android.view.View#setPadding(int, int, int, int)}. |
Daniel Sandler | 99d1f74 | 2012-05-21 16:14:14 -0400 | [diff] [blame] | 3024 | * |
| 3025 | * @param viewId The id of the view to change |
| 3026 | * @param left the left padding in pixels |
| 3027 | * @param top the top padding in pixels |
| 3028 | * @param right the right padding in pixels |
| 3029 | * @param bottom the bottom padding in pixels |
| 3030 | */ |
| 3031 | public void setViewPadding(int viewId, int left, int top, int right, int bottom) { |
| 3032 | addAction(new ViewPaddingAction(viewId, left, top, right, bottom)); |
| 3033 | } |
| 3034 | |
| 3035 | /** |
Selim Cinek | 65b2e7c | 2015-10-26 14:11:31 -0700 | [diff] [blame] | 3036 | * @hide |
| 3037 | * Equivalent to calling {@link android.view.ViewGroup.MarginLayoutParams#setMarginEnd(int)}. |
| 3038 | * Only works if the {@link View#getLayoutParams()} supports margins. |
| 3039 | * Hidden for now since we don't want to support this for all different layout margins yet. |
| 3040 | * |
| 3041 | * @param viewId The id of the view to change |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 3042 | * @param endMarginDimen a dimen resource to read the margin from or 0 to clear the margin. |
Selim Cinek | 65b2e7c | 2015-10-26 14:11:31 -0700 | [diff] [blame] | 3043 | */ |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 3044 | public void setViewLayoutMarginEndDimen(int viewId, @DimenRes int endMarginDimen) { |
| 3045 | addAction(new LayoutParamAction(viewId, LayoutParamAction.LAYOUT_MARGIN_END_DIMEN, |
| 3046 | endMarginDimen)); |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | /** |
Selim Cinek | 384804b | 2018-04-18 14:31:07 +0800 | [diff] [blame] | 3050 | * Equivalent to calling {@link android.view.ViewGroup.MarginLayoutParams#setMarginEnd(int)}. |
| 3051 | * Only works if the {@link View#getLayoutParams()} supports margins. |
| 3052 | * Hidden for now since we don't want to support this for all different layout margins yet. |
| 3053 | * |
| 3054 | * @param viewId The id of the view to change |
| 3055 | * @param endMargin a value in pixels for the end margin. |
| 3056 | * @hide |
| 3057 | */ |
| 3058 | public void setViewLayoutMarginEnd(int viewId, @DimenRes int endMargin) { |
| 3059 | addAction(new LayoutParamAction(viewId, LayoutParamAction.LAYOUT_MARGIN_END, |
| 3060 | endMargin)); |
| 3061 | } |
| 3062 | |
| 3063 | /** |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 3064 | * Equivalent to setting {@link android.view.ViewGroup.MarginLayoutParams#bottomMargin}. |
| 3065 | * |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 3066 | * @param bottomMarginDimen a dimen resource to read the margin from or 0 to clear the margin. |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 3067 | * @hide |
| 3068 | */ |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 3069 | public void setViewLayoutMarginBottomDimen(int viewId, @DimenRes int bottomMarginDimen) { |
| 3070 | addAction(new LayoutParamAction(viewId, LayoutParamAction.LAYOUT_MARGIN_BOTTOM_DIMEN, |
| 3071 | bottomMarginDimen)); |
Adrian Roos | c1a80b0 | 2016-04-05 14:54:55 -0700 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | /** |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 3075 | * Equivalent to setting {@link android.view.ViewGroup.LayoutParams#width}. |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 3076 | * |
| 3077 | * @param layoutWidth one of 0, MATCH_PARENT or WRAP_CONTENT. Other sizes are not allowed |
| 3078 | * because they behave poorly when the density changes. |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 3079 | * @hide |
| 3080 | */ |
| 3081 | public void setViewLayoutWidth(int viewId, int layoutWidth) { |
Adrian Roos | 2d5dbba | 2016-06-08 17:11:53 -0700 | [diff] [blame] | 3082 | if (layoutWidth != 0 && layoutWidth != ViewGroup.LayoutParams.MATCH_PARENT |
| 3083 | && layoutWidth != ViewGroup.LayoutParams.WRAP_CONTENT) { |
| 3084 | throw new IllegalArgumentException("Only supports 0, WRAP_CONTENT and MATCH_PARENT"); |
| 3085 | } |
Adrian Roos | 9b123cf | 2016-02-04 14:55:57 -0800 | [diff] [blame] | 3086 | mActions.add(new LayoutParamAction(viewId, LayoutParamAction.LAYOUT_WIDTH, layoutWidth)); |
Selim Cinek | 65b2e7c | 2015-10-26 14:11:31 -0700 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | /** |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3090 | * Call a method taking one boolean on a view in the layout for this RemoteViews. |
| 3091 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3092 | * @param viewId The id of the view on which to call the method. |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3093 | * @param methodName The name of the method to call. |
| 3094 | * @param value The value to pass to the method. |
| 3095 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3096 | public void setBoolean(int viewId, String methodName, boolean value) { |
| 3097 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.BOOLEAN, value)); |
| 3098 | } |
| 3099 | |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3100 | /** |
| 3101 | * Call a method taking one byte on a view in the layout for this RemoteViews. |
| 3102 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3103 | * @param viewId The id of the view on which to call the method. |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3104 | * @param methodName The name of the method to call. |
| 3105 | * @param value The value to pass to the method. |
| 3106 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3107 | public void setByte(int viewId, String methodName, byte value) { |
| 3108 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.BYTE, value)); |
| 3109 | } |
| 3110 | |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3111 | /** |
| 3112 | * Call a method taking one short on a view in the layout for this RemoteViews. |
| 3113 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3114 | * @param viewId The id of the view on which to call the method. |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3115 | * @param methodName The name of the method to call. |
| 3116 | * @param value The value to pass to the method. |
| 3117 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3118 | public void setShort(int viewId, String methodName, short value) { |
| 3119 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.SHORT, value)); |
| 3120 | } |
| 3121 | |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3122 | /** |
| 3123 | * Call a method taking one int on a view in the layout for this RemoteViews. |
| 3124 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3125 | * @param viewId The id of the view on which to call the method. |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3126 | * @param methodName The name of the method to call. |
| 3127 | * @param value The value to pass to the method. |
| 3128 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3129 | public void setInt(int viewId, String methodName, int value) { |
| 3130 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.INT, value)); |
| 3131 | } |
| 3132 | |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3133 | /** |
Selim Cinek | 396caca | 2018-04-10 17:46:46 -0700 | [diff] [blame] | 3134 | * Call a method taking one ColorStateList on a view in the layout for this RemoteViews. |
| 3135 | * |
| 3136 | * @param viewId The id of the view on which to call the method. |
| 3137 | * @param methodName The name of the method to call. |
| 3138 | * @param value The value to pass to the method. |
| 3139 | * |
| 3140 | * @hide |
| 3141 | */ |
| 3142 | public void setColorStateList(int viewId, String methodName, ColorStateList value) { |
| 3143 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.COLOR_STATE_LIST, |
| 3144 | value)); |
| 3145 | } |
| 3146 | |
| 3147 | |
| 3148 | /** |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3149 | * Call a method taking one long on a view in the layout for this RemoteViews. |
| 3150 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3151 | * @param viewId The id of the view on which to call the method. |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3152 | * @param methodName The name of the method to call. |
| 3153 | * @param value The value to pass to the method. |
| 3154 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3155 | public void setLong(int viewId, String methodName, long value) { |
| 3156 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.LONG, value)); |
| 3157 | } |
| 3158 | |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3159 | /** |
| 3160 | * Call a method taking one float on a view in the layout for this RemoteViews. |
| 3161 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3162 | * @param viewId The id of the view on which to call the method. |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3163 | * @param methodName The name of the method to call. |
| 3164 | * @param value The value to pass to the method. |
| 3165 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3166 | public void setFloat(int viewId, String methodName, float value) { |
| 3167 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.FLOAT, value)); |
| 3168 | } |
| 3169 | |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3170 | /** |
| 3171 | * Call a method taking one double on a view in the layout for this RemoteViews. |
| 3172 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3173 | * @param viewId The id of the view on which to call the method. |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3174 | * @param methodName The name of the method to call. |
| 3175 | * @param value The value to pass to the method. |
| 3176 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3177 | public void setDouble(int viewId, String methodName, double value) { |
| 3178 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.DOUBLE, value)); |
| 3179 | } |
| 3180 | |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3181 | /** |
| 3182 | * Call a method taking one char on a view in the layout for this RemoteViews. |
| 3183 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3184 | * @param viewId The id of the view on which to call the method. |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3185 | * @param methodName The name of the method to call. |
| 3186 | * @param value The value to pass to the method. |
| 3187 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3188 | public void setChar(int viewId, String methodName, char value) { |
| 3189 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.CHAR, value)); |
| 3190 | } |
| 3191 | |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3192 | /** |
| 3193 | * Call a method taking one String on a view in the layout for this RemoteViews. |
| 3194 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3195 | * @param viewId The id of the view on which to call the method. |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3196 | * @param methodName The name of the method to call. |
| 3197 | * @param value The value to pass to the method. |
| 3198 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3199 | public void setString(int viewId, String methodName, String value) { |
| 3200 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.STRING, value)); |
| 3201 | } |
| 3202 | |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3203 | /** |
| 3204 | * Call a method taking one CharSequence on a view in the layout for this RemoteViews. |
| 3205 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3206 | * @param viewId The id of the view on which to call the method. |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3207 | * @param methodName The name of the method to call. |
| 3208 | * @param value The value to pass to the method. |
| 3209 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3210 | public void setCharSequence(int viewId, String methodName, CharSequence value) { |
| 3211 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.CHAR_SEQUENCE, value)); |
| 3212 | } |
| 3213 | |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3214 | /** |
| 3215 | * Call a method taking one Uri on a view in the layout for this RemoteViews. |
| 3216 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3217 | * @param viewId The id of the view on which to call the method. |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3218 | * @param methodName The name of the method to call. |
| 3219 | * @param value The value to pass to the method. |
| 3220 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3221 | public void setUri(int viewId, String methodName, Uri value) { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 3222 | if (value != null) { |
| 3223 | // Resolve any filesystem path before sending remotely |
| 3224 | value = value.getCanonicalUri(); |
| 3225 | if (StrictMode.vmFileUriExposureEnabled()) { |
| 3226 | value.checkFileUriExposed("RemoteViews.setUri()"); |
| 3227 | } |
| 3228 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3229 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.URI, value)); |
| 3230 | } |
| 3231 | |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3232 | /** |
| 3233 | * Call a method taking one Bitmap on a view in the layout for this RemoteViews. |
| 3234 | * @more |
| 3235 | * <p class="note">The bitmap will be flattened into the parcel if this object is |
| 3236 | * sent across processes, so it may end up using a lot of memory, and may be fairly slow.</p> |
| 3237 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3238 | * @param viewId The id of the view on which to call the method. |
Joe Onorato | 592d065 | 2009-03-24 22:25:52 -0700 | [diff] [blame] | 3239 | * @param methodName The name of the method to call. |
| 3240 | * @param value The value to pass to the method. |
| 3241 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3242 | public void setBitmap(int viewId, String methodName, Bitmap value) { |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 3243 | addAction(new BitmapReflectionAction(viewId, methodName, value)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | /** |
Bjorn Bringert | d755b06 | 2010-01-06 17:15:37 +0000 | [diff] [blame] | 3247 | * Call a method taking one Bundle on a view in the layout for this RemoteViews. |
| 3248 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3249 | * @param viewId The id of the view on which to call the method. |
Bjorn Bringert | d755b06 | 2010-01-06 17:15:37 +0000 | [diff] [blame] | 3250 | * @param methodName The name of the method to call. |
| 3251 | * @param value The value to pass to the method. |
| 3252 | */ |
| 3253 | public void setBundle(int viewId, String methodName, Bundle value) { |
| 3254 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.BUNDLE, value)); |
| 3255 | } |
| 3256 | |
| 3257 | /** |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3258 | * Call a method taking one Intent on a view in the layout for this RemoteViews. |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 3259 | * |
Scott Main | 93dc642 | 2012-02-24 12:04:06 -0800 | [diff] [blame] | 3260 | * @param viewId The id of the view on which to call the method. |
| 3261 | * @param methodName The name of the method to call. |
| 3262 | * @param value The {@link android.content.Intent} to pass the method. |
Winson Chung | 499cb9f | 2010-07-16 11:18:17 -0700 | [diff] [blame] | 3263 | */ |
| 3264 | public void setIntent(int viewId, String methodName, Intent value) { |
| 3265 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.INTENT, value)); |
| 3266 | } |
| 3267 | |
| 3268 | /** |
Dan Sandler | a22a380 | 2015-05-13 00:12:47 -0400 | [diff] [blame] | 3269 | * Call a method taking one Icon on a view in the layout for this RemoteViews. |
| 3270 | * |
| 3271 | * @param viewId The id of the view on which to call the method. |
| 3272 | * @param methodName The name of the method to call. |
| 3273 | * @param value The {@link android.graphics.drawable.Icon} to pass the method. |
| 3274 | */ |
| 3275 | public void setIcon(int viewId, String methodName, Icon value) { |
| 3276 | addAction(new ReflectionAction(viewId, methodName, ReflectionAction.ICON, value)); |
| 3277 | } |
| 3278 | |
| 3279 | /** |
Svetoslav Ganov | 33aef98 | 2012-09-13 12:49:03 -0700 | [diff] [blame] | 3280 | * Equivalent to calling View.setContentDescription(CharSequence). |
Svetoslav Ganov | e261e28 | 2011-10-18 17:47:04 -0700 | [diff] [blame] | 3281 | * |
Svetoslav Ganov | 33aef98 | 2012-09-13 12:49:03 -0700 | [diff] [blame] | 3282 | * @param viewId The id of the view whose content description should change. |
| 3283 | * @param contentDescription The new content description for the view. |
Svetoslav Ganov | e261e28 | 2011-10-18 17:47:04 -0700 | [diff] [blame] | 3284 | */ |
| 3285 | public void setContentDescription(int viewId, CharSequence contentDescription) { |
| 3286 | setCharSequence(viewId, "setContentDescription", contentDescription); |
| 3287 | } |
| 3288 | |
Svetoslav Ganov | 33aef98 | 2012-09-13 12:49:03 -0700 | [diff] [blame] | 3289 | /** |
Svetoslav | 6c70290 | 2014-10-09 18:40:56 -0700 | [diff] [blame] | 3290 | * Equivalent to calling {@link android.view.View#setAccessibilityTraversalBefore(int)}. |
| 3291 | * |
| 3292 | * @param viewId The id of the view whose before view in accessibility traversal to set. |
| 3293 | * @param nextId The id of the next in the accessibility traversal. |
| 3294 | **/ |
| 3295 | public void setAccessibilityTraversalBefore(int viewId, int nextId) { |
| 3296 | setInt(viewId, "setAccessibilityTraversalBefore", nextId); |
| 3297 | } |
| 3298 | |
| 3299 | /** |
| 3300 | * Equivalent to calling {@link android.view.View#setAccessibilityTraversalAfter(int)}. |
| 3301 | * |
| 3302 | * @param viewId The id of the view whose after view in accessibility traversal to set. |
| 3303 | * @param nextId The id of the next in the accessibility traversal. |
| 3304 | **/ |
| 3305 | public void setAccessibilityTraversalAfter(int viewId, int nextId) { |
| 3306 | setInt(viewId, "setAccessibilityTraversalAfter", nextId); |
| 3307 | } |
| 3308 | |
| 3309 | /** |
Kirill Grouchnikov | 40a3978 | 2016-07-01 17:24:32 -0400 | [diff] [blame] | 3310 | * Equivalent to calling {@link View#setLabelFor(int)}. |
Svetoslav Ganov | 33aef98 | 2012-09-13 12:49:03 -0700 | [diff] [blame] | 3311 | * |
| 3312 | * @param viewId The id of the view whose property to set. |
| 3313 | * @param labeledId The id of a view for which this view serves as a label. |
| 3314 | */ |
| 3315 | public void setLabelFor(int viewId, int labeledId) { |
| 3316 | setInt(viewId, "setLabelFor", labeledId); |
| 3317 | } |
| 3318 | |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 3319 | /** |
| 3320 | * Provides an alternate layout ID, which can be used to inflate this view. This layout will be |
| 3321 | * used by the host when the widgets displayed on a light-background where foreground elements |
| 3322 | * and text can safely draw using a dark color without any additional background protection. |
| 3323 | */ |
| 3324 | public void setLightBackgroundLayoutId(@LayoutRes int layoutId) { |
| 3325 | mLightBackgroundLayoutId = layoutId; |
| 3326 | } |
| 3327 | |
| 3328 | /** |
| 3329 | * If this view supports dark text versions, creates a copy representing that version, |
| 3330 | * otherwise returns itself. |
| 3331 | * @hide |
| 3332 | */ |
| 3333 | public RemoteViews getDarkTextViews() { |
| 3334 | if (hasFlags(FLAG_USE_LIGHT_BACKGROUND_LAYOUT)) { |
| 3335 | return this; |
| 3336 | } |
| 3337 | |
| 3338 | try { |
| 3339 | addFlags(FLAG_USE_LIGHT_BACKGROUND_LAYOUT); |
| 3340 | return new RemoteViews(this); |
| 3341 | } finally { |
| 3342 | mApplyFlags &= ~FLAG_USE_LIGHT_BACKGROUND_LAYOUT; |
| 3343 | } |
| 3344 | } |
| 3345 | |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 3346 | private RemoteViews getRemoteViewsToApply(Context context) { |
| 3347 | if (hasLandscapeAndPortraitLayouts()) { |
| 3348 | int orientation = context.getResources().getConfiguration().orientation; |
| 3349 | if (orientation == Configuration.ORIENTATION_LANDSCAPE) { |
| 3350 | return mLandscape; |
| 3351 | } else { |
| 3352 | return mPortrait; |
| 3353 | } |
| 3354 | } |
| 3355 | return this; |
| 3356 | } |
| 3357 | |
Svetoslav Ganov | e261e28 | 2011-10-18 17:47:04 -0700 | [diff] [blame] | 3358 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3359 | * Inflates the view hierarchy represented by this object and applies |
| 3360 | * all of the actions. |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 3361 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3362 | * <p><strong>Caller beware: this may throw</strong> |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 3363 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3364 | * @param context Default context to use |
| 3365 | * @param parent Parent that the resulting view hierarchy will be attached to. This method |
| 3366 | * does <strong>not</strong> attach the hierarchy. The caller should do so when appropriate. |
| 3367 | * @return The inflated view hierarchy |
| 3368 | */ |
| 3369 | public View apply(Context context, ViewGroup parent) { |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 3370 | return apply(context, parent, null); |
Dianne Hackborn | 1927ae8 | 2012-06-22 15:21:36 -0700 | [diff] [blame] | 3371 | } |
| 3372 | |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 3373 | /** @hide */ |
| 3374 | public View apply(Context context, ViewGroup parent, OnClickHandler handler) { |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 3375 | RemoteViews rvToApply = getRemoteViewsToApply(context); |
| 3376 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3377 | View result = inflateView(context, rvToApply, parent); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3378 | rvToApply.performApply(result, parent, handler); |
Sunny Goyal | 40f589c | 2018-11-12 11:34:32 -0800 | [diff] [blame] | 3379 | return result; |
| 3380 | } |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3381 | |
Sunny Goyal | 40f589c | 2018-11-12 11:34:32 -0800 | [diff] [blame] | 3382 | /** @hide */ |
| 3383 | public View applyWithTheme(Context context, ViewGroup parent, OnClickHandler handler, |
| 3384 | @StyleRes int applyThemeResId) { |
| 3385 | RemoteViews rvToApply = getRemoteViewsToApply(context); |
| 3386 | |
| 3387 | View result = inflateView(context, rvToApply, parent, applyThemeResId); |
| 3388 | rvToApply.performApply(result, parent, handler); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3389 | return result; |
| 3390 | } |
| 3391 | |
| 3392 | private View inflateView(Context context, RemoteViews rv, ViewGroup parent) { |
Sunny Goyal | 40f589c | 2018-11-12 11:34:32 -0800 | [diff] [blame] | 3393 | return inflateView(context, rv, parent, 0); |
| 3394 | } |
| 3395 | |
| 3396 | private View inflateView(Context context, RemoteViews rv, ViewGroup parent, |
| 3397 | @StyleRes int applyThemeResId) { |
Kenny Guy | 7732006 | 2014-08-27 21:37:15 +0100 | [diff] [blame] | 3398 | // RemoteViews may be built by an application installed in another |
| 3399 | // user. So build a context that loads resources from that user but |
| 3400 | // still returns the current users userId so settings like data / time formats |
| 3401 | // are loaded without requiring cross user persmissions. |
| 3402 | final Context contextForResources = getContextForResources(context); |
Adrian Roos | 83fad0a | 2017-04-24 16:58:26 -0700 | [diff] [blame] | 3403 | Context inflationContext = new RemoteViewsContextWrapper(context, contextForResources); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3404 | |
Dake Gu | 36b86c2 | 2018-04-16 12:49:30 -0700 | [diff] [blame] | 3405 | // If mApplyThemeResId is not given, Theme.DeviceDefault will be used. |
Sunny Goyal | 40f589c | 2018-11-12 11:34:32 -0800 | [diff] [blame] | 3406 | if (applyThemeResId != 0) { |
| 3407 | inflationContext = new ContextThemeWrapper(inflationContext, applyThemeResId); |
Dake Gu | 36b86c2 | 2018-04-16 12:49:30 -0700 | [diff] [blame] | 3408 | } |
Romain Guy | a547559 | 2009-07-01 17:20:08 -0700 | [diff] [blame] | 3409 | LayoutInflater inflater = (LayoutInflater) |
Kenny Guy | 7732006 | 2014-08-27 21:37:15 +0100 | [diff] [blame] | 3410 | context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3411 | |
Kenny Guy | 7732006 | 2014-08-27 21:37:15 +0100 | [diff] [blame] | 3412 | // Clone inflater so we load resources from correct context and |
| 3413 | // we don't add a filter to the static version returned by getSystemService. |
| 3414 | inflater = inflater.cloneInContext(inflationContext); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3415 | inflater.setFilter(this); |
Sunny Goyal | 49e6695 | 2016-05-17 11:57:53 -0700 | [diff] [blame] | 3416 | View v = inflater.inflate(rv.getLayoutId(), parent, false); |
| 3417 | v.setTagInternal(R.id.widget_frame, rv.getLayoutId()); |
| 3418 | return v; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3419 | } |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 3420 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3421 | /** |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3422 | * Implement this interface to receive a callback when |
| 3423 | * {@link #applyAsync} or {@link #reapplyAsync} is finished. |
| 3424 | * @hide |
| 3425 | */ |
| 3426 | public interface OnViewAppliedListener { |
Ahan Wu | de396fa | 2018-05-08 20:42:24 +0800 | [diff] [blame] | 3427 | /** |
| 3428 | * Callback when the RemoteView has finished inflating, |
| 3429 | * but no actions have been applied yet. |
| 3430 | */ |
| 3431 | default void onViewInflated(View v) {}; |
| 3432 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3433 | void onViewApplied(View v); |
| 3434 | |
| 3435 | void onError(Exception e); |
| 3436 | } |
| 3437 | |
| 3438 | /** |
| 3439 | * Applies the views asynchronously, moving as much of the task on the background |
| 3440 | * thread as possible. |
| 3441 | * |
Elliot Waite | 54de774 | 2017-01-11 15:30:35 -0800 | [diff] [blame] | 3442 | * @see #apply(Context, ViewGroup) |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3443 | * @param context Default context to use |
| 3444 | * @param parent Parent that the resulting view hierarchy will be attached to. This method |
| 3445 | * does <strong>not</strong> attach the hierarchy. The caller should do so when appropriate. |
| 3446 | * @param listener the callback to run when all actions have been applied. May be null. |
| 3447 | * @param executor The executor to use. If null {@link AsyncTask#THREAD_POOL_EXECUTOR} is used. |
| 3448 | * @return CancellationSignal |
| 3449 | * @hide |
| 3450 | */ |
| 3451 | public CancellationSignal applyAsync( |
| 3452 | Context context, ViewGroup parent, Executor executor, OnViewAppliedListener listener) { |
| 3453 | return applyAsync(context, parent, executor, listener, null); |
| 3454 | } |
| 3455 | |
| 3456 | private CancellationSignal startTaskOnExecutor(AsyncApplyTask task, Executor executor) { |
| 3457 | CancellationSignal cancelSignal = new CancellationSignal(); |
| 3458 | cancelSignal.setOnCancelListener(task); |
| 3459 | |
| 3460 | task.executeOnExecutor(executor == null ? AsyncTask.THREAD_POOL_EXECUTOR : executor); |
| 3461 | return cancelSignal; |
| 3462 | } |
| 3463 | |
| 3464 | /** @hide */ |
| 3465 | public CancellationSignal applyAsync(Context context, ViewGroup parent, |
| 3466 | Executor executor, OnViewAppliedListener listener, OnClickHandler handler) { |
| 3467 | return startTaskOnExecutor(getAsyncApplyTask(context, parent, listener, handler), executor); |
| 3468 | } |
| 3469 | |
| 3470 | private AsyncApplyTask getAsyncApplyTask(Context context, ViewGroup parent, |
| 3471 | OnViewAppliedListener listener, OnClickHandler handler) { |
| 3472 | return new AsyncApplyTask(getRemoteViewsToApply(context), parent, context, listener, |
| 3473 | handler, null); |
| 3474 | } |
| 3475 | |
| 3476 | private class AsyncApplyTask extends AsyncTask<Void, Void, ViewTree> |
| 3477 | implements CancellationSignal.OnCancelListener { |
| 3478 | final RemoteViews mRV; |
| 3479 | final ViewGroup mParent; |
| 3480 | final Context mContext; |
| 3481 | final OnViewAppliedListener mListener; |
| 3482 | final OnClickHandler mHandler; |
| 3483 | |
| 3484 | private View mResult; |
| 3485 | private ViewTree mTree; |
| 3486 | private Action[] mActions; |
| 3487 | private Exception mError; |
| 3488 | |
| 3489 | private AsyncApplyTask( |
| 3490 | RemoteViews rv, ViewGroup parent, Context context, OnViewAppliedListener listener, |
| 3491 | OnClickHandler handler, View result) { |
| 3492 | mRV = rv; |
| 3493 | mParent = parent; |
| 3494 | mContext = context; |
| 3495 | mListener = listener; |
| 3496 | mHandler = handler; |
| 3497 | |
| 3498 | mResult = result; |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3499 | } |
| 3500 | |
| 3501 | @Override |
| 3502 | protected ViewTree doInBackground(Void... params) { |
| 3503 | try { |
| 3504 | if (mResult == null) { |
| 3505 | mResult = inflateView(mContext, mRV, mParent); |
| 3506 | } |
| 3507 | |
| 3508 | mTree = new ViewTree(mResult); |
| 3509 | if (mRV.mActions != null) { |
| 3510 | int count = mRV.mActions.size(); |
| 3511 | mActions = new Action[count]; |
| 3512 | for (int i = 0; i < count && !isCancelled(); i++) { |
Sunny Goyal | 7b0e2c7 | 2016-11-03 14:48:05 -0700 | [diff] [blame] | 3513 | // TODO: check if isCancelled in nested views. |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3514 | mActions[i] = mRV.mActions.get(i).initActionAsync(mTree, mParent, mHandler); |
| 3515 | } |
| 3516 | } else { |
| 3517 | mActions = null; |
| 3518 | } |
| 3519 | return mTree; |
| 3520 | } catch (Exception e) { |
| 3521 | mError = e; |
| 3522 | return null; |
| 3523 | } |
| 3524 | } |
| 3525 | |
| 3526 | @Override |
| 3527 | protected void onPostExecute(ViewTree viewTree) { |
| 3528 | if (mError == null) { |
Ahan Wu | de396fa | 2018-05-08 20:42:24 +0800 | [diff] [blame] | 3529 | if (mListener != null) { |
| 3530 | mListener.onViewInflated(viewTree.mRoot); |
| 3531 | } |
| 3532 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3533 | try { |
| 3534 | if (mActions != null) { |
| 3535 | OnClickHandler handler = mHandler == null |
| 3536 | ? DEFAULT_ON_CLICK_HANDLER : mHandler; |
| 3537 | for (Action a : mActions) { |
| 3538 | a.apply(viewTree.mRoot, mParent, handler); |
| 3539 | } |
| 3540 | } |
| 3541 | } catch (Exception e) { |
| 3542 | mError = e; |
| 3543 | } |
| 3544 | } |
| 3545 | |
| 3546 | if (mListener != null) { |
| 3547 | if (mError != null) { |
| 3548 | mListener.onError(mError); |
| 3549 | } else { |
| 3550 | mListener.onViewApplied(viewTree.mRoot); |
| 3551 | } |
| 3552 | } else if (mError != null) { |
| 3553 | if (mError instanceof ActionException) { |
| 3554 | throw (ActionException) mError; |
| 3555 | } else { |
| 3556 | throw new ActionException(mError); |
| 3557 | } |
| 3558 | } |
| 3559 | } |
| 3560 | |
| 3561 | @Override |
| 3562 | public void onCancel() { |
| 3563 | cancel(true); |
| 3564 | } |
| 3565 | } |
| 3566 | |
| 3567 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3568 | * Applies all of the actions to the provided view. |
| 3569 | * |
| 3570 | * <p><strong>Caller beware: this may throw</strong> |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 3571 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3572 | * @param v The view to apply the actions to. This should be the result of |
| 3573 | * the {@link #apply(Context,ViewGroup)} call. |
| 3574 | */ |
| 3575 | public void reapply(Context context, View v) { |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 3576 | reapply(context, v, null); |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | /** @hide */ |
| 3580 | public void reapply(Context context, View v, OnClickHandler handler) { |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 3581 | RemoteViews rvToApply = getRemoteViewsToApply(context); |
| 3582 | |
| 3583 | // In the case that a view has this RemoteViews applied in one orientation, is persisted |
| 3584 | // across orientation change, and has the RemoteViews re-applied in the new orientation, |
| 3585 | // we throw an exception, since the layouts may be completely unrelated. |
| 3586 | if (hasLandscapeAndPortraitLayouts()) { |
Sunny Goyal | 49e6695 | 2016-05-17 11:57:53 -0700 | [diff] [blame] | 3587 | if ((Integer) v.getTag(R.id.widget_frame) != rvToApply.getLayoutId()) { |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 3588 | throw new RuntimeException("Attempting to re-apply RemoteViews to a view that" + |
| 3589 | " that does not share the same root layout id."); |
| 3590 | } |
| 3591 | } |
| 3592 | |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 3593 | rvToApply.performApply(v, (ViewGroup) v.getParent(), handler); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3594 | } |
| 3595 | |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3596 | /** |
| 3597 | * Applies all the actions to the provided view, moving as much of the task on the background |
| 3598 | * thread as possible. |
| 3599 | * |
Elliot Waite | 54de774 | 2017-01-11 15:30:35 -0800 | [diff] [blame] | 3600 | * @see #reapply(Context, View) |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3601 | * @param context Default context to use |
| 3602 | * @param v The view to apply the actions to. This should be the result of |
| 3603 | * the {@link #apply(Context,ViewGroup)} call. |
| 3604 | * @param listener the callback to run when all actions have been applied. May be null. |
| 3605 | * @param executor The executor to use. If null {@link AsyncTask#THREAD_POOL_EXECUTOR} is used |
| 3606 | * @return CancellationSignal |
| 3607 | * @hide |
| 3608 | */ |
| 3609 | public CancellationSignal reapplyAsync( |
| 3610 | Context context, View v, Executor executor, OnViewAppliedListener listener) { |
| 3611 | return reapplyAsync(context, v, executor, listener, null); |
| 3612 | } |
| 3613 | |
| 3614 | /** @hide */ |
| 3615 | public CancellationSignal reapplyAsync(Context context, View v, Executor executor, |
| 3616 | OnViewAppliedListener listener, OnClickHandler handler) { |
| 3617 | RemoteViews rvToApply = getRemoteViewsToApply(context); |
| 3618 | |
| 3619 | // In the case that a view has this RemoteViews applied in one orientation, is persisted |
| 3620 | // across orientation change, and has the RemoteViews re-applied in the new orientation, |
| 3621 | // we throw an exception, since the layouts may be completely unrelated. |
| 3622 | if (hasLandscapeAndPortraitLayouts()) { |
Sunny Goyal | 49e6695 | 2016-05-17 11:57:53 -0700 | [diff] [blame] | 3623 | if ((Integer) v.getTag(R.id.widget_frame) != rvToApply.getLayoutId()) { |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3624 | throw new RuntimeException("Attempting to re-apply RemoteViews to a view that" + |
| 3625 | " that does not share the same root layout id."); |
| 3626 | } |
| 3627 | } |
| 3628 | |
| 3629 | return startTaskOnExecutor(new AsyncApplyTask(rvToApply, (ViewGroup) v.getParent(), |
| 3630 | context, listener, handler, v), executor); |
| 3631 | } |
| 3632 | |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 3633 | private void performApply(View v, ViewGroup parent, OnClickHandler handler) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3634 | if (mActions != null) { |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 3635 | handler = handler == null ? DEFAULT_ON_CLICK_HANDLER : handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3636 | final int count = mActions.size(); |
| 3637 | for (int i = 0; i < count; i++) { |
| 3638 | Action a = mActions.get(i); |
Dianne Hackborn | a194021 | 2012-06-28 16:07:22 -0700 | [diff] [blame] | 3639 | a.apply(v, parent, handler); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3640 | } |
| 3641 | } |
| 3642 | } |
| 3643 | |
Sunny Goyal | 5c02263 | 2016-02-17 16:30:41 -0800 | [diff] [blame] | 3644 | /** |
| 3645 | * Returns true if the RemoteViews contains potentially costly operations and should be |
| 3646 | * applied asynchronously. |
| 3647 | * |
| 3648 | * @hide |
| 3649 | */ |
| 3650 | public boolean prefersAsyncApply() { |
| 3651 | if (mActions != null) { |
| 3652 | final int count = mActions.size(); |
| 3653 | for (int i = 0; i < count; i++) { |
| 3654 | if (mActions.get(i).prefersAsyncApply()) { |
| 3655 | return true; |
| 3656 | } |
| 3657 | } |
| 3658 | } |
| 3659 | return false; |
| 3660 | } |
| 3661 | |
Kenny Guy | 7732006 | 2014-08-27 21:37:15 +0100 | [diff] [blame] | 3662 | private Context getContextForResources(Context context) { |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 3663 | if (mApplication != null) { |
| 3664 | if (context.getUserId() == UserHandle.getUserId(mApplication.uid) |
| 3665 | && context.getPackageName().equals(mApplication.packageName)) { |
| 3666 | return context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3667 | } |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 3668 | try { |
| 3669 | return context.createApplicationContext(mApplication, |
| 3670 | Context.CONTEXT_RESTRICTED); |
| 3671 | } catch (NameNotFoundException e) { |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 3672 | Log.e(LOG_TAG, "Package name " + mApplication.packageName + " not found"); |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 3673 | } |
| 3674 | } |
| 3675 | |
| 3676 | return context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3677 | } |
| 3678 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3679 | /** |
| 3680 | * Returns the number of actions in this RemoteViews. Can be used as a sequence number. |
| 3681 | * |
| 3682 | * @hide |
| 3683 | */ |
| 3684 | public int getSequenceNumber() { |
| 3685 | return (mActions == null) ? 0 : mActions.size(); |
| 3686 | } |
| 3687 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3688 | /* (non-Javadoc) |
| 3689 | * Used to restrict the views which can be inflated |
Jim Miller | e667a7a | 2012-08-09 19:22:32 -0700 | [diff] [blame] | 3690 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3691 | * @see android.view.LayoutInflater.Filter#onLoadClass(java.lang.Class) |
| 3692 | */ |
Gilles Debunne | e6ac8b9 | 2010-06-17 10:55:04 -0700 | [diff] [blame] | 3693 | public boolean onLoadClass(Class clazz) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3694 | return clazz.isAnnotationPresent(RemoteView.class); |
| 3695 | } |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 3696 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3697 | public int describeContents() { |
| 3698 | return 0; |
| 3699 | } |
| 3700 | |
| 3701 | public void writeToParcel(Parcel dest, int flags) { |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 3702 | if (!hasLandscapeAndPortraitLayouts()) { |
| 3703 | dest.writeInt(MODE_NORMAL); |
| 3704 | // We only write the bitmap cache if we are the root RemoteViews, as this cache |
| 3705 | // is shared by all children. |
| 3706 | if (mIsRoot) { |
| 3707 | mBitmapCache.writeBitmapsToParcel(dest, flags); |
| 3708 | } |
Sunny Goyal | 5d8bcdf | 2016-10-27 16:11:29 -0700 | [diff] [blame] | 3709 | if (!mIsRoot && (flags & PARCELABLE_ELIDE_DUPLICATES) != 0) { |
| 3710 | dest.writeInt(0); |
| 3711 | } else { |
| 3712 | dest.writeInt(1); |
| 3713 | mApplication.writeToParcel(dest, flags); |
| 3714 | } |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 3715 | dest.writeInt(mLayoutId); |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 3716 | dest.writeInt(mLightBackgroundLayoutId); |
Sunny Goyal | 5b7689f | 2017-09-21 11:08:34 -0700 | [diff] [blame] | 3717 | writeActionsToParcel(dest); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3718 | } else { |
Adam Cohen | 5d20064 | 2012-04-24 10:43:31 -0700 | [diff] [blame] | 3719 | dest.writeInt(MODE_HAS_LANDSCAPE_AND_PORTRAIT); |
| 3720 | // We only write the bitmap cache if we are the root RemoteViews, as this cache |
| 3721 | // is shared by all children. |
| 3722 | if (mIsRoot) { |
| 3723 | mBitmapCache.writeBitmapsToParcel(dest, flags); |
| 3724 | } |
| 3725 | mLandscape.writeToParcel(dest, flags); |
Sunny Goyal | 5d8bcdf | 2016-10-27 16:11:29 -0700 | [diff] [blame] | 3726 | // Both RemoteViews already share the same package and user |
| 3727 | mPortrait.writeToParcel(dest, flags | PARCELABLE_ELIDE_DUPLICATES); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3728 | } |
Sunny Goyal | c12d31c | 2018-11-12 16:29:18 -0800 | [diff] [blame] | 3729 | dest.writeInt(mApplyFlags); |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 3730 | } |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 3731 | |
Sunny Goyal | 5b7689f | 2017-09-21 11:08:34 -0700 | [diff] [blame] | 3732 | private void writeActionsToParcel(Parcel parcel) { |
| 3733 | int count; |
| 3734 | if (mActions != null) { |
| 3735 | count = mActions.size(); |
| 3736 | } else { |
| 3737 | count = 0; |
| 3738 | } |
| 3739 | parcel.writeInt(count); |
| 3740 | for (int i = 0; i < count; i++) { |
| 3741 | Action a = mActions.get(i); |
Sunny Goyal | 5b15392 | 2017-09-21 21:00:36 -0700 | [diff] [blame] | 3742 | parcel.writeInt(a.getActionTag()); |
Sunny Goyal | 5b7689f | 2017-09-21 11:08:34 -0700 | [diff] [blame] | 3743 | a.writeToParcel(parcel, a.hasSameAppInfo(mApplication) |
| 3744 | ? PARCELABLE_ELIDE_DUPLICATES : 0); |
| 3745 | } |
| 3746 | } |
| 3747 | |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 3748 | private static ApplicationInfo getApplicationInfo(String packageName, int userId) { |
Svetoslav | b624244 | 2014-09-19 13:21:55 -0700 | [diff] [blame] | 3749 | if (packageName == null) { |
| 3750 | return null; |
| 3751 | } |
| 3752 | |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 3753 | // Get the application for the passed in package and user. |
| 3754 | Application application = ActivityThread.currentApplication(); |
| 3755 | if (application == null) { |
| 3756 | throw new IllegalStateException("Cannot create remote views out of an aplication."); |
| 3757 | } |
| 3758 | |
| 3759 | ApplicationInfo applicationInfo = application.getApplicationInfo(); |
| 3760 | if (UserHandle.getUserId(applicationInfo.uid) != userId |
| 3761 | || !applicationInfo.packageName.equals(packageName)) { |
| 3762 | try { |
Svetoslav | 14494a8 | 2014-08-18 10:43:27 -0700 | [diff] [blame] | 3763 | Context context = application.getBaseContext().createPackageContextAsUser( |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 3764 | packageName, 0, new UserHandle(userId)); |
| 3765 | applicationInfo = context.getApplicationInfo(); |
| 3766 | } catch (NameNotFoundException nnfe) { |
| 3767 | throw new IllegalArgumentException("No such package " + packageName); |
| 3768 | } |
| 3769 | } |
| 3770 | |
| 3771 | return applicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3772 | } |
| 3773 | |
| 3774 | /** |
Sunny Goyal | dd60f4d | 2017-10-18 15:22:42 -0700 | [diff] [blame] | 3775 | * Returns true if the {@link #mApplication} is same as the provided info. |
| 3776 | * |
| 3777 | * @hide |
| 3778 | */ |
| 3779 | public boolean hasSameAppInfo(ApplicationInfo info) { |
| 3780 | return mApplication.packageName.equals(info.packageName) && mApplication.uid == info.uid; |
| 3781 | } |
| 3782 | |
| 3783 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3784 | * Parcelable.Creator that instantiates RemoteViews objects |
| 3785 | */ |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 3786 | public static final @android.annotation.NonNull Parcelable.Creator<RemoteViews> CREATOR = new Parcelable.Creator<RemoteViews>() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3787 | public RemoteViews createFromParcel(Parcel parcel) { |
| 3788 | return new RemoteViews(parcel); |
| 3789 | } |
| 3790 | |
| 3791 | public RemoteViews[] newArray(int size) { |
| 3792 | return new RemoteViews[size]; |
| 3793 | } |
| 3794 | }; |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3795 | |
| 3796 | /** |
| 3797 | * A representation of the view hierarchy. Only views which have a valid ID are added |
| 3798 | * and can be searched. |
| 3799 | */ |
| 3800 | private static class ViewTree { |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 3801 | private static final int INSERT_AT_END_INDEX = -1; |
Sunny Goyal | 7b0e2c7 | 2016-11-03 14:48:05 -0700 | [diff] [blame] | 3802 | private View mRoot; |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3803 | private ArrayList<ViewTree> mChildren; |
| 3804 | |
| 3805 | private ViewTree(View root) { |
| 3806 | mRoot = root; |
| 3807 | } |
| 3808 | |
| 3809 | public void createTree() { |
| 3810 | if (mChildren != null) { |
| 3811 | return; |
| 3812 | } |
| 3813 | |
| 3814 | mChildren = new ArrayList<>(); |
Sunny Goyal | 7b0e2c7 | 2016-11-03 14:48:05 -0700 | [diff] [blame] | 3815 | if (mRoot instanceof ViewGroup) { |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3816 | ViewGroup vg = (ViewGroup) mRoot; |
| 3817 | int count = vg.getChildCount(); |
| 3818 | for (int i = 0; i < count; i++) { |
| 3819 | addViewChild(vg.getChildAt(i)); |
| 3820 | } |
| 3821 | } |
| 3822 | } |
| 3823 | |
| 3824 | public ViewTree findViewTreeById(int id) { |
| 3825 | if (mRoot.getId() == id) { |
| 3826 | return this; |
| 3827 | } |
| 3828 | if (mChildren == null) { |
| 3829 | return null; |
| 3830 | } |
| 3831 | for (ViewTree tree : mChildren) { |
| 3832 | ViewTree result = tree.findViewTreeById(id); |
| 3833 | if (result != null) { |
| 3834 | return result; |
| 3835 | } |
| 3836 | } |
| 3837 | return null; |
| 3838 | } |
| 3839 | |
Sunny Goyal | 7b0e2c7 | 2016-11-03 14:48:05 -0700 | [diff] [blame] | 3840 | public void replaceView(View v) { |
| 3841 | mRoot = v; |
| 3842 | mChildren = null; |
| 3843 | createTree(); |
| 3844 | } |
| 3845 | |
Alan Viverette | 04fd470 | 2017-04-13 16:37:06 -0400 | [diff] [blame] | 3846 | public <T extends View> T findViewById(int id) { |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3847 | if (mChildren == null) { |
| 3848 | return mRoot.findViewById(id); |
| 3849 | } |
| 3850 | ViewTree tree = findViewTreeById(id); |
Alan Viverette | 04fd470 | 2017-04-13 16:37:06 -0400 | [diff] [blame] | 3851 | return tree == null ? null : (T) tree.mRoot; |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3852 | } |
| 3853 | |
| 3854 | public void addChild(ViewTree child) { |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 3855 | addChild(child, INSERT_AT_END_INDEX); |
| 3856 | } |
| 3857 | |
| 3858 | /** |
| 3859 | * Adds the given {@link ViewTree} as a child at the given index. |
| 3860 | * |
| 3861 | * @param index The position at which to add the child or -1 to add last. |
| 3862 | */ |
| 3863 | public void addChild(ViewTree child, int index) { |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3864 | if (mChildren == null) { |
| 3865 | mChildren = new ArrayList<>(); |
| 3866 | } |
| 3867 | child.createTree(); |
Anthony Chen | 8f5f358 | 2017-04-11 11:18:37 -0700 | [diff] [blame] | 3868 | |
| 3869 | if (index == INSERT_AT_END_INDEX) { |
| 3870 | mChildren.add(child); |
| 3871 | return; |
| 3872 | } |
| 3873 | |
| 3874 | mChildren.add(index, child); |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | private void addViewChild(View v) { |
Sunny Goyal | 7b0e2c7 | 2016-11-03 14:48:05 -0700 | [diff] [blame] | 3878 | // ViewTree only contains Views which can be found using findViewById. |
| 3879 | // If isRootNamespace is true, this view is skipped. |
| 3880 | // @see ViewGroup#findViewTraversal(int) |
| 3881 | if (v.isRootNamespace()) { |
| 3882 | return; |
| 3883 | } |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3884 | final ViewTree target; |
| 3885 | |
| 3886 | // If the view has a valid id, i.e., if can be found using findViewById, add it to the |
| 3887 | // tree, otherwise skip this view and add its children instead. |
| 3888 | if (v.getId() != 0) { |
| 3889 | ViewTree tree = new ViewTree(v); |
| 3890 | mChildren.add(tree); |
| 3891 | target = tree; |
| 3892 | } else { |
| 3893 | target = this; |
| 3894 | } |
| 3895 | |
Sunny Goyal | 7b0e2c7 | 2016-11-03 14:48:05 -0700 | [diff] [blame] | 3896 | if (v instanceof ViewGroup) { |
Sunny Goyal | dd292f4 | 2015-12-02 14:29:27 -0800 | [diff] [blame] | 3897 | if (target.mChildren == null) { |
| 3898 | target.mChildren = new ArrayList<>(); |
| 3899 | ViewGroup vg = (ViewGroup) v; |
| 3900 | int count = vg.getChildCount(); |
| 3901 | for (int i = 0; i < count; i++) { |
| 3902 | target.addViewChild(vg.getChildAt(i)); |
| 3903 | } |
| 3904 | } |
| 3905 | } |
| 3906 | } |
| 3907 | } |
Sunny Goyal | 43c9704 | 2018-08-23 15:21:26 -0700 | [diff] [blame] | 3908 | |
| 3909 | /** |
| 3910 | * Class representing a response to an action performed on any element of a RemoteViews. |
| 3911 | */ |
| 3912 | public static class RemoteResponse { |
| 3913 | |
| 3914 | private PendingIntent mPendingIntent; |
| 3915 | private Intent mFillIntent; |
| 3916 | |
| 3917 | private IntArray mViewIds; |
| 3918 | private ArrayList<String> mElementNames; |
| 3919 | |
| 3920 | /** |
| 3921 | * Creates a response which sends a pending intent as part of the response. The source |
| 3922 | * bounds ({@link Intent#getSourceBounds()}) of the intent will be set to the bounds of the |
| 3923 | * target view in screen space. |
| 3924 | * Note that any activity options associated with the mPendingIntent may get overridden |
| 3925 | * before starting the intent. |
| 3926 | * |
| 3927 | * @param pendingIntent The {@link PendingIntent} to send as part of the response |
| 3928 | */ |
| 3929 | public static RemoteResponse fromPendingIntent(PendingIntent pendingIntent) { |
| 3930 | RemoteResponse response = new RemoteResponse(); |
| 3931 | response.mPendingIntent = pendingIntent; |
| 3932 | return response; |
| 3933 | } |
| 3934 | |
| 3935 | /** |
| 3936 | * When using collections (eg. {@link ListView}, {@link StackView} etc.) in widgets, it is |
| 3937 | * very costly to set PendingIntents on the individual items, and is hence not permitted. |
| 3938 | * Instead a single PendingIntent template can be set on the collection, see {@link |
| 3939 | * RemoteViews#setPendingIntentTemplate(int, PendingIntent)}, and the individual on-click |
| 3940 | * action of a given item can be distinguished by setting a fillInIntent on that item. The |
| 3941 | * fillInIntent is then combined with the PendingIntent template in order to determine the |
| 3942 | * final intent which will be executed when the item is clicked. This works as follows: any |
| 3943 | * fields which are left blank in the PendingIntent template, but are provided by the |
| 3944 | * fillInIntent will be overwritten, and the resulting PendingIntent will be used. The rest |
| 3945 | * of the PendingIntent template will then be filled in with the associated fields that are |
| 3946 | * set in fillInIntent. See {@link Intent#fillIn(Intent, int)} for more details. |
| 3947 | * Creates a response which sends a pending intent as part of the response. The source |
| 3948 | * bounds ({@link Intent#getSourceBounds()}) of the intent will be set to the bounds of the |
| 3949 | * target view in screen space. |
| 3950 | * Note that any activity options associated with the mPendingIntent may get overridden |
| 3951 | * before starting the intent. |
| 3952 | * |
| 3953 | * @param fillIntent The intent which will be combined with the parent's PendingIntent in |
| 3954 | * order to determine the behavior of the response |
| 3955 | * |
| 3956 | * @see RemoteViews#setPendingIntentTemplate(int, PendingIntent) |
| 3957 | * @see RemoteViews#setOnClickFillInIntent(int, Intent) |
| 3958 | * @return |
| 3959 | */ |
| 3960 | public static RemoteResponse fromFillInIntent(Intent fillIntent) { |
| 3961 | RemoteResponse response = new RemoteResponse(); |
| 3962 | response.mFillIntent = fillIntent; |
| 3963 | return response; |
| 3964 | } |
| 3965 | |
| 3966 | /** |
| 3967 | * Adds a shared element to be transferred as part of the transition between Activities |
| 3968 | * using cross-Activity scene animations. The position of the first element will be used as |
| 3969 | * the epicenter for the exit Transition. The position of the associated shared element in |
| 3970 | * the launched Activity will be the epicenter of its entering Transition. |
| 3971 | * |
| 3972 | * @param viewId The id of the view to be shared as part of the transition |
| 3973 | * @param sharedElementName The shared element name for this view |
| 3974 | * |
| 3975 | * @see ActivityOptions#makeSceneTransitionAnimation(Activity, Pair[]) |
| 3976 | */ |
| 3977 | public RemoteResponse addSharedElement(int viewId, String sharedElementName) { |
| 3978 | if (mViewIds == null) { |
| 3979 | mViewIds = new IntArray(); |
| 3980 | mElementNames = new ArrayList<>(); |
| 3981 | } |
| 3982 | mViewIds.add(viewId); |
| 3983 | mElementNames.add(sharedElementName); |
| 3984 | return this; |
| 3985 | } |
| 3986 | |
| 3987 | private void writeToParcel(Parcel dest, int flags) { |
| 3988 | PendingIntent.writePendingIntentOrNullToParcel(mPendingIntent, dest); |
| 3989 | if (mPendingIntent == null) { |
| 3990 | // Only write the intent if pending intent is null |
| 3991 | dest.writeTypedObject(mFillIntent, flags); |
| 3992 | } |
| 3993 | dest.writeIntArray(mViewIds == null ? null : mViewIds.toArray()); |
| 3994 | dest.writeStringList(mElementNames); |
| 3995 | } |
| 3996 | |
| 3997 | private void readFromParcel(Parcel parcel) { |
| 3998 | mPendingIntent = PendingIntent.readPendingIntentOrNullFromParcel(parcel); |
| 3999 | if (mPendingIntent == null) { |
| 4000 | mFillIntent = parcel.readTypedObject(Intent.CREATOR); |
| 4001 | } |
| 4002 | int[] viewIds = parcel.createIntArray(); |
| 4003 | mViewIds = viewIds == null ? null : IntArray.wrap(viewIds); |
| 4004 | mElementNames = parcel.createStringArrayList(); |
| 4005 | } |
| 4006 | |
| 4007 | private void handleViewClick(View v, OnClickHandler handler) { |
| 4008 | final PendingIntent pi; |
| 4009 | if (mPendingIntent != null) { |
| 4010 | pi = mPendingIntent; |
| 4011 | } else if (mFillIntent != null) { |
| 4012 | // Insure that this view is a child of an AdapterView |
| 4013 | View parent = (View) v.getParent(); |
| 4014 | // Break the for loop on the first encounter of: |
| 4015 | // 1) an AdapterView, |
| 4016 | // 2) an AppWidgetHostView that is not a RemoteViewsFrameLayout, or |
| 4017 | // 3) a null parent. |
| 4018 | // 2) and 3) are unexpected and catch the case where a child is not |
| 4019 | // correctly parented in an AdapterView. |
| 4020 | while (parent != null && !(parent instanceof AdapterView<?>) |
| 4021 | && !((parent instanceof AppWidgetHostView) |
| 4022 | && !(parent instanceof RemoteViewsAdapter.RemoteViewsFrameLayout))) { |
| 4023 | parent = (View) parent.getParent(); |
| 4024 | } |
| 4025 | |
| 4026 | if (!(parent instanceof AdapterView<?>)) { |
| 4027 | // Somehow they've managed to get this far without having |
| 4028 | // and AdapterView as a parent. |
| 4029 | Log.e(LOG_TAG, "Collection item doesn't have AdapterView parent"); |
| 4030 | return; |
| 4031 | } |
| 4032 | // Insure that a template pending intent has been set on an ancestor |
| 4033 | if (!(parent.getTag() instanceof PendingIntent)) { |
| 4034 | Log.e(LOG_TAG, "Attempting setOnClickFillInIntent without" |
| 4035 | + " calling setPendingIntentTemplate on parent."); |
| 4036 | return; |
| 4037 | } |
| 4038 | |
| 4039 | pi = (PendingIntent) parent.getTag(); |
| 4040 | } else { |
| 4041 | Log.e(LOG_TAG, "Response has neither pendingIntent nor fillInIntent"); |
| 4042 | return; |
| 4043 | } |
| 4044 | |
| 4045 | handler.onClickHandler(v, pi, this); |
| 4046 | } |
| 4047 | |
| 4048 | /** @hide */ |
| 4049 | public Pair<Intent, ActivityOptions> getLaunchOptions(View view) { |
| 4050 | Intent intent = mPendingIntent != null ? new Intent() : new Intent(mFillIntent); |
| 4051 | intent.setSourceBounds(getSourceBounds(view)); |
| 4052 | |
| 4053 | ActivityOptions opts = null; |
| 4054 | |
| 4055 | Context context = view.getContext(); |
| 4056 | if (context.getResources().getBoolean( |
| 4057 | com.android.internal.R.bool.config_overrideRemoteViewsActivityTransition)) { |
| 4058 | TypedArray windowStyle = context.getTheme().obtainStyledAttributes( |
| 4059 | com.android.internal.R.styleable.Window); |
| 4060 | int windowAnimations = windowStyle.getResourceId( |
| 4061 | com.android.internal.R.styleable.Window_windowAnimationStyle, 0); |
| 4062 | TypedArray windowAnimationStyle = context.obtainStyledAttributes( |
| 4063 | windowAnimations, com.android.internal.R.styleable.WindowAnimation); |
| 4064 | int enterAnimationId = windowAnimationStyle.getResourceId(com.android.internal.R |
| 4065 | .styleable.WindowAnimation_activityOpenRemoteViewsEnterAnimation, 0); |
| 4066 | windowStyle.recycle(); |
| 4067 | windowAnimationStyle.recycle(); |
| 4068 | |
| 4069 | if (enterAnimationId != 0) { |
| 4070 | opts = ActivityOptions.makeCustomAnimation(context, |
| 4071 | enterAnimationId, 0); |
| 4072 | opts.setPendingIntentLaunchFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4073 | } |
| 4074 | } |
| 4075 | |
| 4076 | if (opts == null && mViewIds != null && mElementNames != null) { |
| 4077 | View parent = (View) view.getParent(); |
| 4078 | while (parent != null && !(parent instanceof AppWidgetHostView)) { |
| 4079 | parent = (View) parent.getParent(); |
| 4080 | } |
| 4081 | if (parent instanceof AppWidgetHostView) { |
| 4082 | opts = ((AppWidgetHostView) parent).createSharedElementActivityOptions( |
| 4083 | mViewIds.toArray(), |
| 4084 | mElementNames.toArray(new String[mElementNames.size()]), intent); |
| 4085 | } |
| 4086 | } |
| 4087 | |
| 4088 | if (opts == null) { |
| 4089 | opts = ActivityOptions.makeBasic(); |
| 4090 | opts.setPendingIntentLaunchFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4091 | } |
| 4092 | return Pair.create(intent, opts); |
| 4093 | } |
| 4094 | } |
| 4095 | |
| 4096 | /** @hide */ |
| 4097 | public static boolean startPendingIntent(View view, PendingIntent pendingIntent, |
| 4098 | Pair<Intent, ActivityOptions> options) { |
| 4099 | try { |
| 4100 | // TODO: Unregister this handler if PendingIntent.FLAG_ONE_SHOT? |
| 4101 | Context context = view.getContext(); |
| 4102 | // The NEW_TASK flags are applied through the activity options and not as a part of |
| 4103 | // the call to startIntentSender() to ensure that they are consistently applied to |
| 4104 | // both mutable and immutable PendingIntents. |
| 4105 | context.startIntentSender( |
| 4106 | pendingIntent.getIntentSender(), options.first, |
| 4107 | 0, 0, 0, options.second.toBundle()); |
| 4108 | } catch (IntentSender.SendIntentException e) { |
| 4109 | Log.e(LOG_TAG, "Cannot send pending intent: ", e); |
| 4110 | return false; |
| 4111 | } catch (Exception e) { |
| 4112 | Log.e(LOG_TAG, "Cannot send pending intent due to unknown exception: ", e); |
| 4113 | return false; |
| 4114 | } |
| 4115 | return true; |
| 4116 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4117 | } |