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