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