RemoteViews.java revision eb818188aadfe3d0208d96837618deaa68ea6973
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 17package android.widget; 18 19import android.app.ActivityOptions; 20import android.app.PendingIntent; 21import android.appwidget.AppWidgetHostView; 22import android.content.Context; 23import android.content.Intent; 24import android.content.IntentSender; 25import android.content.pm.ApplicationInfo; 26import android.content.pm.PackageManager; 27import android.content.pm.PackageManager.NameNotFoundException; 28import android.content.res.Configuration; 29import android.graphics.Bitmap; 30import android.graphics.PorterDuff; 31import android.graphics.Rect; 32import android.graphics.drawable.Drawable; 33import android.net.Uri; 34import android.os.Build; 35import android.os.Bundle; 36import android.os.Parcel; 37import android.os.Parcelable; 38import android.text.TextUtils; 39import android.util.Log; 40import android.util.TypedValue; 41import android.view.LayoutInflater; 42import android.view.LayoutInflater.Filter; 43import android.view.RemotableViewMethod; 44import android.view.View; 45import android.view.View.OnClickListener; 46import android.view.ViewGroup; 47import android.widget.AdapterView.OnItemClickListener; 48 49import java.lang.annotation.ElementType; 50import java.lang.annotation.Retention; 51import java.lang.annotation.RetentionPolicy; 52import java.lang.annotation.Target; 53import java.lang.reflect.Method; 54import java.util.ArrayList; 55 56 57/** 58 * A class that describes a view hierarchy that can be displayed in 59 * another process. The hierarchy is inflated from a layout resource 60 * file, and this class provides some basic operations for modifying 61 * the content of the inflated hierarchy. 62 */ 63public class RemoteViews implements Parcelable, Filter { 64 65 private static final String LOG_TAG = "RemoteViews"; 66 67 /** 68 * The intent extra that contains the appWidgetId. 69 * @hide 70 */ 71 static final String EXTRA_REMOTEADAPTER_APPWIDGET_ID = "remoteAdapterAppWidgetId"; 72 73 /** 74 * The package name of the package containing the layout 75 * resource. (Added to the parcel) 76 */ 77 private final String mPackage; 78 79 /** 80 * The resource ID of the layout file. (Added to the parcel) 81 */ 82 private final int mLayoutId; 83 84 /** 85 * An array of actions to perform on the view tree once it has been 86 * inflated 87 */ 88 private ArrayList<Action> mActions; 89 90 /** 91 * A class to keep track of memory usage by this RemoteViews 92 */ 93 private MemoryUsageCounter mMemoryUsageCounter; 94 95 /** 96 * Maps bitmaps to unique indicies to avoid Bitmap duplication. 97 */ 98 private BitmapCache mBitmapCache; 99 100 /** 101 * Indicates whether or not this RemoteViews object is contained as a child of any other 102 * RemoteViews. 103 */ 104 private boolean mIsRoot = true; 105 106 /** 107 * Constants to whether or not this RemoteViews is composed of a landscape and portrait 108 * RemoteViews. 109 */ 110 private static final int MODE_NORMAL = 0; 111 private static final int MODE_HAS_LANDSCAPE_AND_PORTRAIT = 1; 112 113 /** 114 * Used in conjunction with the special constructor 115 * {@link #RemoteViews(RemoteViews, RemoteViews)} to keep track of the landscape and portrait 116 * RemoteViews. 117 */ 118 private RemoteViews mLandscape = null; 119 private RemoteViews mPortrait = null; 120 121 /** 122 * This flag indicates whether this RemoteViews object is being created from a 123 * RemoteViewsService for use as a child of a widget collection. This flag is used 124 * to determine whether or not certain features are available, in particular, 125 * setting on click extras and setting on click pending intents. The former is enabled, 126 * and the latter disabled when this flag is true. 127 */ 128 private boolean mIsWidgetCollectionChild = false; 129 130 /** 131 * This annotation indicates that a subclass of View is alllowed to be used 132 * with the {@link RemoteViews} mechanism. 133 */ 134 @Target({ ElementType.TYPE }) 135 @Retention(RetentionPolicy.RUNTIME) 136 public @interface RemoteView { 137 } 138 139 /** 140 * Exception to send when something goes wrong executing an action 141 * 142 */ 143 public static class ActionException extends RuntimeException { 144 public ActionException(Exception ex) { 145 super(ex); 146 } 147 public ActionException(String message) { 148 super(message); 149 } 150 } 151 152 /** 153 * Base class for all actions that can be performed on an 154 * inflated view. 155 * 156 * SUBCLASSES MUST BE IMMUTABLE SO CLONE WORKS!!!!! 157 */ 158 private abstract static class Action implements Parcelable { 159 public abstract void apply(View root, ViewGroup rootParent) throws ActionException; 160 161 public int describeContents() { 162 return 0; 163 } 164 165 /** 166 * Overridden by each class to report on it's own memory usage 167 */ 168 public void updateMemoryUsageEstimate(MemoryUsageCounter counter) { 169 // We currently only calculate Bitmap memory usage, so by default, don't do anything 170 // here 171 return; 172 } 173 174 protected boolean startIntentSafely(View view, PendingIntent pendingIntent, 175 Intent fillInIntent) { 176 try { 177 // TODO: Unregister this handler if PendingIntent.FLAG_ONE_SHOT? 178 Context context = view.getContext(); 179 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(view, 180 0, 0, 181 view.getMeasuredWidth(), view.getMeasuredHeight()); 182 context.startIntentSender( 183 pendingIntent.getIntentSender(), fillInIntent, 184 Intent.FLAG_ACTIVITY_NEW_TASK, 185 Intent.FLAG_ACTIVITY_NEW_TASK, 0, opts.toBundle()); 186 } catch (IntentSender.SendIntentException e) { 187 android.util.Log.e(LOG_TAG, "Cannot send pending intent: ", e); 188 return false; 189 } catch (Exception e) { 190 android.util.Log.e(LOG_TAG, "Cannot send pending intent due to " + 191 "unknown exception: ", e); 192 return false; 193 } 194 return true; 195 } 196 197 public void setBitmapCache(BitmapCache bitmapCache) { 198 // Do nothing 199 } 200 } 201 202 private class SetEmptyView extends Action { 203 int viewId; 204 int emptyViewId; 205 206 public final static int TAG = 6; 207 208 SetEmptyView(int viewId, int emptyViewId) { 209 this.viewId = viewId; 210 this.emptyViewId = emptyViewId; 211 } 212 213 SetEmptyView(Parcel in) { 214 this.viewId = in.readInt(); 215 this.emptyViewId = in.readInt(); 216 } 217 218 public void writeToParcel(Parcel out, int flags) { 219 out.writeInt(TAG); 220 out.writeInt(this.viewId); 221 out.writeInt(this.emptyViewId); 222 } 223 224 @Override 225 public void apply(View root, ViewGroup rootParent) { 226 final View view = root.findViewById(viewId); 227 if (!(view instanceof AdapterView<?>)) return; 228 229 AdapterView<?> adapterView = (AdapterView<?>) view; 230 231 final View emptyView = root.findViewById(emptyViewId); 232 if (emptyView == null) return; 233 234 adapterView.setEmptyView(emptyView); 235 } 236 } 237 238 private class SetOnClickFillInIntent extends Action { 239 public SetOnClickFillInIntent(int id, Intent fillInIntent) { 240 this.viewId = id; 241 this.fillInIntent = fillInIntent; 242 } 243 244 public SetOnClickFillInIntent(Parcel parcel) { 245 viewId = parcel.readInt(); 246 fillInIntent = Intent.CREATOR.createFromParcel(parcel); 247 } 248 249 public void writeToParcel(Parcel dest, int flags) { 250 dest.writeInt(TAG); 251 dest.writeInt(viewId); 252 fillInIntent.writeToParcel(dest, 0 /* no flags */); 253 } 254 255 @Override 256 public void apply(View root, ViewGroup rootParent) { 257 final View target = root.findViewById(viewId); 258 if (target == null) return; 259 260 if (!mIsWidgetCollectionChild) { 261 Log.e("RemoteViews", "The method setOnClickFillInIntent is available " + 262 "only from RemoteViewsFactory (ie. on collection items)."); 263 return; 264 } 265 if (target == root) { 266 target.setTagInternal(com.android.internal.R.id.fillInIntent, fillInIntent); 267 } else if (target != null && fillInIntent != null) { 268 OnClickListener listener = new OnClickListener() { 269 public void onClick(View v) { 270 // Insure that this view is a child of an AdapterView 271 View parent = (View) v.getParent(); 272 while (!(parent instanceof AdapterView<?>) 273 && !(parent instanceof AppWidgetHostView)) { 274 parent = (View) parent.getParent(); 275 } 276 277 if (parent instanceof AppWidgetHostView) { 278 // Somehow they've managed to get this far without having 279 // and AdapterView as a parent. 280 Log.e("RemoteViews", "Collection item doesn't have AdapterView parent"); 281 return; 282 } 283 284 // Insure that a template pending intent has been set on an ancestor 285 if (!(parent.getTag() instanceof PendingIntent)) { 286 Log.e("RemoteViews", "Attempting setOnClickFillInIntent without" + 287 " calling setPendingIntentTemplate on parent."); 288 return; 289 } 290 291 PendingIntent pendingIntent = (PendingIntent) parent.getTag(); 292 293 final float appScale = v.getContext().getResources() 294 .getCompatibilityInfo().applicationScale; 295 final int[] pos = new int[2]; 296 v.getLocationOnScreen(pos); 297 298 final Rect rect = new Rect(); 299 rect.left = (int) (pos[0] * appScale + 0.5f); 300 rect.top = (int) (pos[1] * appScale + 0.5f); 301 rect.right = (int) ((pos[0] + v.getWidth()) * appScale + 0.5f); 302 rect.bottom = (int) ((pos[1] + v.getHeight()) * appScale + 0.5f); 303 304 fillInIntent.setSourceBounds(rect); 305 startIntentSafely(v, pendingIntent, fillInIntent); 306 } 307 308 }; 309 target.setOnClickListener(listener); 310 } 311 } 312 313 int viewId; 314 Intent fillInIntent; 315 316 public final static int TAG = 9; 317 } 318 319 private class SetPendingIntentTemplate extends Action { 320 public SetPendingIntentTemplate(int id, PendingIntent pendingIntentTemplate) { 321 this.viewId = id; 322 this.pendingIntentTemplate = pendingIntentTemplate; 323 } 324 325 public SetPendingIntentTemplate(Parcel parcel) { 326 viewId = parcel.readInt(); 327 pendingIntentTemplate = PendingIntent.readPendingIntentOrNullFromParcel(parcel); 328 } 329 330 public void writeToParcel(Parcel dest, int flags) { 331 dest.writeInt(TAG); 332 dest.writeInt(viewId); 333 pendingIntentTemplate.writeToParcel(dest, 0 /* no flags */); 334 } 335 336 @Override 337 public void apply(View root, ViewGroup rootParent) { 338 final View target = root.findViewById(viewId); 339 if (target == null) return; 340 341 // If the view isn't an AdapterView, setting a PendingIntent template doesn't make sense 342 if (target instanceof AdapterView<?>) { 343 AdapterView<?> av = (AdapterView<?>) target; 344 // The PendingIntent template is stored in the view's tag. 345 OnItemClickListener listener = new OnItemClickListener() { 346 public void onItemClick(AdapterView<?> parent, View view, 347 int position, long id) { 348 // The view should be a frame layout 349 if (view instanceof ViewGroup) { 350 ViewGroup vg = (ViewGroup) view; 351 352 // AdapterViews contain their children in a frame 353 // so we need to go one layer deeper here. 354 if (parent instanceof AdapterViewAnimator) { 355 vg = (ViewGroup) vg.getChildAt(0); 356 } 357 if (vg == null) return; 358 359 Intent fillInIntent = null; 360 int childCount = vg.getChildCount(); 361 for (int i = 0; i < childCount; i++) { 362 Object tag = vg.getChildAt(i).getTag(com.android.internal.R.id.fillInIntent); 363 if (tag instanceof Intent) { 364 fillInIntent = (Intent) tag; 365 break; 366 } 367 } 368 if (fillInIntent == null) return; 369 370 final float appScale = view.getContext().getResources() 371 .getCompatibilityInfo().applicationScale; 372 final int[] pos = new int[2]; 373 view.getLocationOnScreen(pos); 374 375 final Rect rect = new Rect(); 376 rect.left = (int) (pos[0] * appScale + 0.5f); 377 rect.top = (int) (pos[1] * appScale + 0.5f); 378 rect.right = (int) ((pos[0] + view.getWidth()) * appScale + 0.5f); 379 rect.bottom = (int) ((pos[1] + view.getHeight()) * appScale + 0.5f); 380 381 final Intent intent = new Intent(); 382 intent.setSourceBounds(rect); 383 startIntentSafely(view, pendingIntentTemplate, fillInIntent); 384 } 385 } 386 }; 387 av.setOnItemClickListener(listener); 388 av.setTag(pendingIntentTemplate); 389 } else { 390 Log.e("RemoteViews", "Cannot setPendingIntentTemplate on a view which is not" + 391 "an AdapterView (id: " + viewId + ")"); 392 return; 393 } 394 } 395 396 int viewId; 397 PendingIntent pendingIntentTemplate; 398 399 public final static int TAG = 8; 400 } 401 402 private class SetRemoteViewsAdapterIntent extends Action { 403 public SetRemoteViewsAdapterIntent(int id, Intent intent) { 404 this.viewId = id; 405 this.intent = intent; 406 } 407 408 public SetRemoteViewsAdapterIntent(Parcel parcel) { 409 viewId = parcel.readInt(); 410 intent = Intent.CREATOR.createFromParcel(parcel); 411 } 412 413 public void writeToParcel(Parcel dest, int flags) { 414 dest.writeInt(TAG); 415 dest.writeInt(viewId); 416 intent.writeToParcel(dest, flags); 417 } 418 419 @Override 420 public void apply(View root, ViewGroup rootParent) { 421 final View target = root.findViewById(viewId); 422 if (target == null) return; 423 424 // Ensure that we are applying to an AppWidget root 425 if (!(rootParent instanceof AppWidgetHostView)) { 426 Log.e("RemoteViews", "SetRemoteViewsAdapterIntent action can only be used for " + 427 "AppWidgets (root id: " + viewId + ")"); 428 return; 429 } 430 // Ensure that we are calling setRemoteAdapter on an AdapterView that supports it 431 if (!(target instanceof AbsListView) && !(target instanceof AdapterViewAnimator)) { 432 Log.e("RemoteViews", "Cannot setRemoteViewsAdapter on a view which is not " + 433 "an AbsListView or AdapterViewAnimator (id: " + viewId + ")"); 434 return; 435 } 436 437 // Embed the AppWidget Id for use in RemoteViewsAdapter when connecting to the intent 438 // RemoteViewsService 439 AppWidgetHostView host = (AppWidgetHostView) rootParent; 440 intent.putExtra(EXTRA_REMOTEADAPTER_APPWIDGET_ID, host.getAppWidgetId()); 441 if (target instanceof AbsListView) { 442 AbsListView v = (AbsListView) target; 443 v.setRemoteViewsAdapter(intent); 444 } else if (target instanceof AdapterViewAnimator) { 445 AdapterViewAnimator v = (AdapterViewAnimator) target; 446 v.setRemoteViewsAdapter(intent); 447 } 448 } 449 450 int viewId; 451 Intent intent; 452 453 public final static int TAG = 10; 454 } 455 456 /** 457 * Equivalent to calling 458 * {@link android.view.View#setOnClickListener(android.view.View.OnClickListener)} 459 * to launch the provided {@link PendingIntent}. 460 */ 461 private class SetOnClickPendingIntent extends Action { 462 public SetOnClickPendingIntent(int id, PendingIntent pendingIntent) { 463 this.viewId = id; 464 this.pendingIntent = pendingIntent; 465 } 466 467 public SetOnClickPendingIntent(Parcel parcel) { 468 viewId = parcel.readInt(); 469 470 // We check a flag to determine if the parcel contains a PendingIntent. 471 if (parcel.readInt() != 0) { 472 pendingIntent = PendingIntent.readPendingIntentOrNullFromParcel(parcel); 473 } 474 } 475 476 public void writeToParcel(Parcel dest, int flags) { 477 dest.writeInt(TAG); 478 dest.writeInt(viewId); 479 480 // We use a flag to indicate whether the parcel contains a valid object. 481 dest.writeInt(pendingIntent != null ? 1 : 0); 482 if (pendingIntent != null) { 483 pendingIntent.writeToParcel(dest, 0 /* no flags */); 484 } 485 } 486 487 @Override 488 public void apply(View root, ViewGroup rootParent) { 489 final View target = root.findViewById(viewId); 490 if (target == null) return; 491 492 // If the view is an AdapterView, setting a PendingIntent on click doesn't make much 493 // sense, do they mean to set a PendingIntent template for the AdapterView's children? 494 if (mIsWidgetCollectionChild) { 495 Log.e("RemoteViews", "Cannot setOnClickPendingIntent for collection item " + 496 "(id: " + viewId + ")"); 497 ApplicationInfo appInfo = root.getContext().getApplicationInfo(); 498 499 // We let this slide for HC and ICS so as to not break compatibility. It should have 500 // been disabled from the outset, but was left open by accident. 501 if (appInfo != null && 502 appInfo.targetSdkVersion >= Build.VERSION_CODES.JELLY_BEAN) { 503 return; 504 } 505 } 506 507 if (target != null) { 508 // If the pendingIntent is null, we clear the onClickListener 509 OnClickListener listener = null; 510 if (pendingIntent != null) { 511 listener = new OnClickListener() { 512 public void onClick(View v) { 513 // Find target view location in screen coordinates and 514 // fill into PendingIntent before sending. 515 final float appScale = v.getContext().getResources() 516 .getCompatibilityInfo().applicationScale; 517 final int[] pos = new int[2]; 518 v.getLocationOnScreen(pos); 519 520 final Rect rect = new Rect(); 521 rect.left = (int) (pos[0] * appScale + 0.5f); 522 rect.top = (int) (pos[1] * appScale + 0.5f); 523 rect.right = (int) ((pos[0] + v.getWidth()) * appScale + 0.5f); 524 rect.bottom = (int) ((pos[1] + v.getHeight()) * appScale + 0.5f); 525 526 final Intent intent = new Intent(); 527 intent.setSourceBounds(rect); 528 startIntentSafely(v, pendingIntent, intent); 529 } 530 }; 531 } 532 target.setOnClickListener(listener); 533 } 534 } 535 536 int viewId; 537 PendingIntent pendingIntent; 538 539 public final static int TAG = 1; 540 } 541 542 /** 543 * Equivalent to calling a combination of {@link Drawable#setAlpha(int)}, 544 * {@link Drawable#setColorFilter(int, android.graphics.PorterDuff.Mode)}, 545 * and/or {@link Drawable#setLevel(int)} on the {@link Drawable} of a given view. 546 * <p> 547 * These operations will be performed on the {@link Drawable} returned by the 548 * target {@link View#getBackground()} by default. If targetBackground is false, 549 * we assume the target is an {@link ImageView} and try applying the operations 550 * to {@link ImageView#getDrawable()}. 551 * <p> 552 * You can omit specific calls by marking their values with null or -1. 553 */ 554 private class SetDrawableParameters extends Action { 555 public SetDrawableParameters(int id, boolean targetBackground, int alpha, 556 int colorFilter, PorterDuff.Mode mode, int level) { 557 this.viewId = id; 558 this.targetBackground = targetBackground; 559 this.alpha = alpha; 560 this.colorFilter = colorFilter; 561 this.filterMode = mode; 562 this.level = level; 563 } 564 565 public SetDrawableParameters(Parcel parcel) { 566 viewId = parcel.readInt(); 567 targetBackground = parcel.readInt() != 0; 568 alpha = parcel.readInt(); 569 colorFilter = parcel.readInt(); 570 boolean hasMode = parcel.readInt() != 0; 571 if (hasMode) { 572 filterMode = PorterDuff.Mode.valueOf(parcel.readString()); 573 } else { 574 filterMode = null; 575 } 576 level = parcel.readInt(); 577 } 578 579 public void writeToParcel(Parcel dest, int flags) { 580 dest.writeInt(TAG); 581 dest.writeInt(viewId); 582 dest.writeInt(targetBackground ? 1 : 0); 583 dest.writeInt(alpha); 584 dest.writeInt(colorFilter); 585 if (filterMode != null) { 586 dest.writeInt(1); 587 dest.writeString(filterMode.toString()); 588 } else { 589 dest.writeInt(0); 590 } 591 dest.writeInt(level); 592 } 593 594 @Override 595 public void apply(View root, ViewGroup rootParent) { 596 final View target = root.findViewById(viewId); 597 if (target == null) return; 598 599 // Pick the correct drawable to modify for this view 600 Drawable targetDrawable = null; 601 if (targetBackground) { 602 targetDrawable = target.getBackground(); 603 } else if (target instanceof ImageView) { 604 ImageView imageView = (ImageView) target; 605 targetDrawable = imageView.getDrawable(); 606 } 607 608 if (targetDrawable != null) { 609 // Perform modifications only if values are set correctly 610 if (alpha != -1) { 611 targetDrawable.setAlpha(alpha); 612 } 613 if (colorFilter != -1 && filterMode != null) { 614 targetDrawable.setColorFilter(colorFilter, filterMode); 615 } 616 if (level != -1) { 617 targetDrawable.setLevel(level); 618 } 619 } 620 } 621 622 int viewId; 623 boolean targetBackground; 624 int alpha; 625 int colorFilter; 626 PorterDuff.Mode filterMode; 627 int level; 628 629 public final static int TAG = 3; 630 } 631 632 private class ReflectionActionWithoutParams extends Action { 633 int viewId; 634 String methodName; 635 636 public final static int TAG = 5; 637 638 ReflectionActionWithoutParams(int viewId, String methodName) { 639 this.viewId = viewId; 640 this.methodName = methodName; 641 } 642 643 ReflectionActionWithoutParams(Parcel in) { 644 this.viewId = in.readInt(); 645 this.methodName = in.readString(); 646 } 647 648 public void writeToParcel(Parcel out, int flags) { 649 out.writeInt(TAG); 650 out.writeInt(this.viewId); 651 out.writeString(this.methodName); 652 } 653 654 @Override 655 public void apply(View root, ViewGroup rootParent) { 656 final View view = root.findViewById(viewId); 657 if (view == null) return; 658 659 Class klass = view.getClass(); 660 Method method; 661 try { 662 method = klass.getMethod(this.methodName); 663 } catch (NoSuchMethodException ex) { 664 throw new ActionException("view: " + klass.getName() + " doesn't have method: " 665 + this.methodName + "()"); 666 } 667 668 if (!method.isAnnotationPresent(RemotableViewMethod.class)) { 669 throw new ActionException("view: " + klass.getName() 670 + " can't use method with RemoteViews: " 671 + this.methodName + "()"); 672 } 673 674 try { 675 //noinspection ConstantIfStatement 676 if (false) { 677 Log.d("RemoteViews", "view: " + klass.getName() + " calling method: " 678 + this.methodName + "()"); 679 } 680 method.invoke(view); 681 } catch (Exception ex) { 682 throw new ActionException(ex); 683 } 684 } 685 } 686 687 private static class BitmapCache { 688 ArrayList<Bitmap> mBitmaps; 689 690 public BitmapCache() { 691 mBitmaps = new ArrayList<Bitmap>(); 692 } 693 694 public BitmapCache(Parcel source) { 695 int count = source.readInt(); 696 mBitmaps = new ArrayList<Bitmap>(); 697 for (int i = 0; i < count; i++) { 698 Bitmap b = Bitmap.CREATOR.createFromParcel(source); 699 mBitmaps.add(b); 700 } 701 } 702 703 public int getBitmapId(Bitmap b) { 704 if (b == null) { 705 return -1; 706 } else { 707 if (mBitmaps.contains(b)) { 708 return mBitmaps.indexOf(b); 709 } else { 710 mBitmaps.add(b); 711 return (mBitmaps.size() - 1); 712 } 713 } 714 } 715 716 public Bitmap getBitmapForId(int id) { 717 if (id == -1 || id >= mBitmaps.size()) { 718 return null; 719 } else { 720 return mBitmaps.get(id); 721 } 722 } 723 724 public void writeBitmapsToParcel(Parcel dest, int flags) { 725 int count = mBitmaps.size(); 726 dest.writeInt(count); 727 for (int i = 0; i < count; i++) { 728 mBitmaps.get(i).writeToParcel(dest, flags); 729 } 730 } 731 732 public void assimilate(BitmapCache bitmapCache) { 733 ArrayList<Bitmap> bitmapsToBeAdded = bitmapCache.mBitmaps; 734 int count = bitmapsToBeAdded.size(); 735 for (int i = 0; i < count; i++) { 736 Bitmap b = bitmapsToBeAdded.get(i); 737 if (!mBitmaps.contains(b)) { 738 mBitmaps.add(b); 739 } 740 } 741 } 742 743 public void addBitmapMemory(MemoryUsageCounter memoryCounter) { 744 for (int i = 0; i < mBitmaps.size(); i++) { 745 memoryCounter.addBitmapMemory(mBitmaps.get(i)); 746 } 747 } 748 } 749 750 private class BitmapReflectionAction extends Action { 751 int bitmapId; 752 int viewId; 753 Bitmap bitmap; 754 String methodName; 755 756 BitmapReflectionAction(int viewId, String methodName, Bitmap bitmap) { 757 this.bitmap = bitmap; 758 this.viewId = viewId; 759 this.methodName = methodName; 760 bitmapId = mBitmapCache.getBitmapId(bitmap); 761 } 762 763 BitmapReflectionAction(Parcel in) { 764 viewId = in.readInt(); 765 methodName = in.readString(); 766 bitmapId = in.readInt(); 767 bitmap = mBitmapCache.getBitmapForId(bitmapId); 768 } 769 770 @Override 771 public void writeToParcel(Parcel dest, int flags) { 772 dest.writeInt(TAG); 773 dest.writeInt(viewId); 774 dest.writeString(methodName); 775 dest.writeInt(bitmapId); 776 } 777 778 @Override 779 public void apply(View root, ViewGroup rootParent) throws ActionException { 780 ReflectionAction ra = new ReflectionAction(viewId, methodName, ReflectionAction.BITMAP, 781 bitmap); 782 ra.apply(root, rootParent); 783 } 784 785 @Override 786 public void setBitmapCache(BitmapCache bitmapCache) { 787 bitmapId = bitmapCache.getBitmapId(bitmap); 788 } 789 790 public final static int TAG = 12; 791 } 792 793 /** 794 * Base class for the reflection actions. 795 */ 796 private class ReflectionAction extends Action { 797 static final int TAG = 2; 798 799 static final int BOOLEAN = 1; 800 static final int BYTE = 2; 801 static final int SHORT = 3; 802 static final int INT = 4; 803 static final int LONG = 5; 804 static final int FLOAT = 6; 805 static final int DOUBLE = 7; 806 static final int CHAR = 8; 807 static final int STRING = 9; 808 static final int CHAR_SEQUENCE = 10; 809 static final int URI = 11; 810 static final int BITMAP = 12; 811 static final int BUNDLE = 13; 812 static final int INTENT = 14; 813 814 int viewId; 815 String methodName; 816 int type; 817 Object value; 818 819 ReflectionAction(int viewId, String methodName, int type, Object value) { 820 this.viewId = viewId; 821 this.methodName = methodName; 822 this.type = type; 823 this.value = value; 824 } 825 826 ReflectionAction(Parcel in) { 827 this.viewId = in.readInt(); 828 this.methodName = in.readString(); 829 this.type = in.readInt(); 830 //noinspection ConstantIfStatement 831 if (false) { 832 Log.d("RemoteViews", "read viewId=0x" + Integer.toHexString(this.viewId) 833 + " methodName=" + this.methodName + " type=" + this.type); 834 } 835 836 // For some values that may have been null, we first check a flag to see if they were 837 // written to the parcel. 838 switch (this.type) { 839 case BOOLEAN: 840 this.value = in.readInt() != 0; 841 break; 842 case BYTE: 843 this.value = in.readByte(); 844 break; 845 case SHORT: 846 this.value = (short)in.readInt(); 847 break; 848 case INT: 849 this.value = in.readInt(); 850 break; 851 case LONG: 852 this.value = in.readLong(); 853 break; 854 case FLOAT: 855 this.value = in.readFloat(); 856 break; 857 case DOUBLE: 858 this.value = in.readDouble(); 859 break; 860 case CHAR: 861 this.value = (char)in.readInt(); 862 break; 863 case STRING: 864 this.value = in.readString(); 865 break; 866 case CHAR_SEQUENCE: 867 this.value = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 868 break; 869 case URI: 870 if (in.readInt() != 0) { 871 this.value = Uri.CREATOR.createFromParcel(in); 872 } 873 break; 874 case BITMAP: 875 if (in.readInt() != 0) { 876 this.value = Bitmap.CREATOR.createFromParcel(in); 877 } 878 break; 879 case BUNDLE: 880 this.value = in.readBundle(); 881 break; 882 case INTENT: 883 if (in.readInt() != 0) { 884 this.value = Intent.CREATOR.createFromParcel(in); 885 } 886 break; 887 default: 888 break; 889 } 890 } 891 892 public void writeToParcel(Parcel out, int flags) { 893 out.writeInt(TAG); 894 out.writeInt(this.viewId); 895 out.writeString(this.methodName); 896 out.writeInt(this.type); 897 //noinspection ConstantIfStatement 898 if (false) { 899 Log.d("RemoteViews", "write viewId=0x" + Integer.toHexString(this.viewId) 900 + " methodName=" + this.methodName + " type=" + this.type); 901 } 902 903 // For some values which are null, we record an integer flag to indicate whether 904 // we have written a valid value to the parcel. 905 switch (this.type) { 906 case BOOLEAN: 907 out.writeInt((Boolean) this.value ? 1 : 0); 908 break; 909 case BYTE: 910 out.writeByte((Byte) this.value); 911 break; 912 case SHORT: 913 out.writeInt((Short) this.value); 914 break; 915 case INT: 916 out.writeInt((Integer) this.value); 917 break; 918 case LONG: 919 out.writeLong((Long) this.value); 920 break; 921 case FLOAT: 922 out.writeFloat((Float) this.value); 923 break; 924 case DOUBLE: 925 out.writeDouble((Double) this.value); 926 break; 927 case CHAR: 928 out.writeInt((int)((Character)this.value).charValue()); 929 break; 930 case STRING: 931 out.writeString((String)this.value); 932 break; 933 case CHAR_SEQUENCE: 934 TextUtils.writeToParcel((CharSequence)this.value, out, flags); 935 break; 936 case URI: 937 out.writeInt(this.value != null ? 1 : 0); 938 if (this.value != null) { 939 ((Uri)this.value).writeToParcel(out, flags); 940 } 941 break; 942 case BITMAP: 943 out.writeInt(this.value != null ? 1 : 0); 944 if (this.value != null) { 945 ((Bitmap)this.value).writeToParcel(out, flags); 946 } 947 break; 948 case BUNDLE: 949 out.writeBundle((Bundle) this.value); 950 break; 951 case INTENT: 952 out.writeInt(this.value != null ? 1 : 0); 953 if (this.value != null) { 954 ((Intent)this.value).writeToParcel(out, flags); 955 } 956 break; 957 default: 958 break; 959 } 960 } 961 962 private Class getParameterType() { 963 switch (this.type) { 964 case BOOLEAN: 965 return boolean.class; 966 case BYTE: 967 return byte.class; 968 case SHORT: 969 return short.class; 970 case INT: 971 return int.class; 972 case LONG: 973 return long.class; 974 case FLOAT: 975 return float.class; 976 case DOUBLE: 977 return double.class; 978 case CHAR: 979 return char.class; 980 case STRING: 981 return String.class; 982 case CHAR_SEQUENCE: 983 return CharSequence.class; 984 case URI: 985 return Uri.class; 986 case BITMAP: 987 return Bitmap.class; 988 case BUNDLE: 989 return Bundle.class; 990 case INTENT: 991 return Intent.class; 992 default: 993 return null; 994 } 995 } 996 997 @Override 998 public void apply(View root, ViewGroup rootParent) { 999 final View view = root.findViewById(viewId); 1000 if (view == null) return; 1001 1002 Class param = getParameterType(); 1003 if (param == null) { 1004 throw new ActionException("bad type: " + this.type); 1005 } 1006 1007 Class klass = view.getClass(); 1008 Method method; 1009 try { 1010 method = klass.getMethod(this.methodName, getParameterType()); 1011 } 1012 catch (NoSuchMethodException ex) { 1013 throw new ActionException("view: " + klass.getName() + " doesn't have method: " 1014 + this.methodName + "(" + param.getName() + ")"); 1015 } 1016 1017 if (!method.isAnnotationPresent(RemotableViewMethod.class)) { 1018 throw new ActionException("view: " + klass.getName() 1019 + " can't use method with RemoteViews: " 1020 + this.methodName + "(" + param.getName() + ")"); 1021 } 1022 1023 try { 1024 //noinspection ConstantIfStatement 1025 if (false) { 1026 Log.d("RemoteViews", "view: " + klass.getName() + " calling method: " 1027 + this.methodName + "(" + param.getName() + ") with " 1028 + (this.value == null ? "null" : this.value.getClass().getName())); 1029 } 1030 method.invoke(view, this.value); 1031 } 1032 catch (Exception ex) { 1033 throw new ActionException(ex); 1034 } 1035 } 1036 1037 @Override 1038 public void updateMemoryUsageEstimate(MemoryUsageCounter counter) { 1039 // We currently only calculate Bitmap memory usage 1040 switch (this.type) { 1041 case BITMAP: 1042 if (this.value != null) { 1043 final Bitmap b = (Bitmap) this.value; 1044 counter.addBitmapMemory(b); 1045 } 1046 break; 1047 default: 1048 break; 1049 } 1050 } 1051 } 1052 1053 private void configureRemoteViewsAsChild(RemoteViews rv) { 1054 mBitmapCache.assimilate(rv.mBitmapCache); 1055 rv.setBitmapCache(mBitmapCache); 1056 rv.setNotRoot(); 1057 } 1058 1059 void setNotRoot() { 1060 mIsRoot = false; 1061 } 1062 1063 /** 1064 * Equivalent to calling {@link ViewGroup#addView(View)} after inflating the 1065 * given {@link RemoteViews}, or calling {@link ViewGroup#removeAllViews()} 1066 * when null. This allows users to build "nested" {@link RemoteViews}. 1067 */ 1068 private class ViewGroupAction extends Action { 1069 public ViewGroupAction(int viewId, RemoteViews nestedViews) { 1070 this.viewId = viewId; 1071 this.nestedViews = nestedViews; 1072 if (nestedViews != null) { 1073 configureRemoteViewsAsChild(nestedViews); 1074 } 1075 } 1076 1077 public ViewGroupAction(Parcel parcel, BitmapCache bitmapCache) { 1078 viewId = parcel.readInt(); 1079 boolean nestedViewsNull = parcel.readInt() == 0; 1080 if (!nestedViewsNull) { 1081 nestedViews = new RemoteViews(parcel, bitmapCache); 1082 } else { 1083 nestedViews = null; 1084 } 1085 } 1086 1087 public void writeToParcel(Parcel dest, int flags) { 1088 dest.writeInt(TAG); 1089 dest.writeInt(viewId); 1090 if (nestedViews != null) { 1091 dest.writeInt(1); 1092 nestedViews.writeToParcel(dest, flags); 1093 } else { 1094 // signifies null 1095 dest.writeInt(0); 1096 } 1097 } 1098 1099 @Override 1100 public void apply(View root, ViewGroup rootParent) { 1101 final Context context = root.getContext(); 1102 final ViewGroup target = (ViewGroup) root.findViewById(viewId); 1103 if (target == null) return; 1104 if (nestedViews != null) { 1105 // Inflate nested views and add as children 1106 target.addView(nestedViews.apply(context, target)); 1107 } else { 1108 // Clear all children when nested views omitted 1109 target.removeAllViews(); 1110 } 1111 } 1112 1113 @Override 1114 public void updateMemoryUsageEstimate(MemoryUsageCounter counter) { 1115 if (nestedViews != null) { 1116 counter.increment(nestedViews.estimateMemoryUsage()); 1117 } 1118 } 1119 1120 @Override 1121 public void setBitmapCache(BitmapCache bitmapCache) { 1122 if (nestedViews != null) { 1123 nestedViews.setBitmapCache(bitmapCache); 1124 } 1125 } 1126 1127 int viewId; 1128 RemoteViews nestedViews; 1129 1130 public final static int TAG = 4; 1131 } 1132 1133 /** 1134 * Helper action to set compound drawables on a TextView. Supports relative 1135 * (s/t/e/b) or cardinal (l/t/r/b) arrangement. 1136 */ 1137 private class TextViewDrawableAction extends Action { 1138 public TextViewDrawableAction(int viewId, boolean isRelative, int d1, int d2, int d3, int d4) { 1139 this.viewId = viewId; 1140 this.isRelative = isRelative; 1141 this.d1 = d1; 1142 this.d2 = d2; 1143 this.d3 = d3; 1144 this.d4 = d4; 1145 } 1146 1147 public TextViewDrawableAction(Parcel parcel) { 1148 viewId = parcel.readInt(); 1149 isRelative = (parcel.readInt() != 0); 1150 d1 = parcel.readInt(); 1151 d2 = parcel.readInt(); 1152 d3 = parcel.readInt(); 1153 d4 = parcel.readInt(); 1154 } 1155 1156 public void writeToParcel(Parcel dest, int flags) { 1157 dest.writeInt(TAG); 1158 dest.writeInt(viewId); 1159 dest.writeInt(isRelative ? 1 : 0); 1160 dest.writeInt(d1); 1161 dest.writeInt(d2); 1162 dest.writeInt(d3); 1163 dest.writeInt(d4); 1164 } 1165 1166 @Override 1167 public void apply(View root, ViewGroup rootParent) { 1168 final Context context = root.getContext(); 1169 final TextView target = (TextView) root.findViewById(viewId); 1170 if (target == null) return; 1171 if (isRelative) { 1172 target.setCompoundDrawablesRelativeWithIntrinsicBounds(d1, d2, d3, d4); 1173 } else { 1174 target.setCompoundDrawablesWithIntrinsicBounds(d1, d2, d3, d4); 1175 } 1176 } 1177 1178 int viewId; 1179 boolean isRelative = false; 1180 int d1, d2, d3, d4; 1181 1182 public final static int TAG = 11; 1183 } 1184 1185 /** 1186 * Helper action to set compound drawables on a TextView. Supports relative 1187 * (s/t/e/b) or cardinal (l/t/r/b) arrangement. 1188 */ 1189 private class TextViewSizeAction extends Action { 1190 public TextViewSizeAction(int viewId, int units, float size) { 1191 this.viewId = viewId; 1192 this.units = units; 1193 this.size = size; 1194 } 1195 1196 public TextViewSizeAction(Parcel parcel) { 1197 viewId = parcel.readInt(); 1198 units = parcel.readInt(); 1199 size = parcel.readFloat(); 1200 } 1201 1202 public void writeToParcel(Parcel dest, int flags) { 1203 dest.writeInt(TAG); 1204 dest.writeInt(viewId); 1205 dest.writeInt(units); 1206 dest.writeFloat(size); 1207 } 1208 1209 @Override 1210 public void apply(View root, ViewGroup rootParent) { 1211 final Context context = root.getContext(); 1212 final TextView target = (TextView) root.findViewById(viewId); 1213 if (target == null) return; 1214 target.setTextSize(units, size); 1215 } 1216 1217 int viewId; 1218 int units; 1219 float size; 1220 1221 public final static int TAG = 13; 1222 } 1223 1224 /** 1225 * Simple class used to keep track of memory usage in a RemoteViews. 1226 * 1227 */ 1228 private class MemoryUsageCounter { 1229 public void clear() { 1230 mMemoryUsage = 0; 1231 } 1232 1233 public void increment(int numBytes) { 1234 mMemoryUsage += numBytes; 1235 } 1236 1237 public int getMemoryUsage() { 1238 return mMemoryUsage; 1239 } 1240 1241 public void addBitmapMemory(Bitmap b) { 1242 final Bitmap.Config c = b.getConfig(); 1243 // If we don't know, be pessimistic and assume 4 1244 int bpp = 4; 1245 if (c != null) { 1246 switch (c) { 1247 case ALPHA_8: 1248 bpp = 1; 1249 break; 1250 case RGB_565: 1251 case ARGB_4444: 1252 bpp = 2; 1253 break; 1254 case ARGB_8888: 1255 bpp = 4; 1256 break; 1257 } 1258 } 1259 increment(b.getWidth() * b.getHeight() * bpp); 1260 } 1261 1262 int mMemoryUsage; 1263 } 1264 1265 /** 1266 * Create a new RemoteViews object that will display the views contained 1267 * in the specified layout file. 1268 * 1269 * @param packageName Name of the package that contains the layout resource 1270 * @param layoutId The id of the layout resource 1271 */ 1272 public RemoteViews(String packageName, int layoutId) { 1273 mPackage = packageName; 1274 mLayoutId = layoutId; 1275 mBitmapCache = new BitmapCache(); 1276 1277 // setup the memory usage statistics 1278 mMemoryUsageCounter = new MemoryUsageCounter(); 1279 recalculateMemoryUsage(); 1280 } 1281 1282 private boolean hasLandscapeAndPortraitLayouts() { 1283 return (mLandscape != null) && (mPortrait != null); 1284 } 1285 1286 /** 1287 * Create a new RemoteViews object that will inflate as the specified 1288 * landspace or portrait RemoteViews, depending on the current configuration. 1289 * 1290 * @param landscape The RemoteViews to inflate in landscape configuration 1291 * @param portrait The RemoteViews to inflate in portrait configuration 1292 */ 1293 public RemoteViews(RemoteViews landscape, RemoteViews portrait) { 1294 if (landscape == null || portrait == null) { 1295 throw new RuntimeException("Both RemoteViews must be non-null"); 1296 } 1297 if (landscape.getPackage().compareTo(portrait.getPackage()) != 0) { 1298 throw new RuntimeException("Both RemoteViews must share the same package"); 1299 } 1300 mPackage = portrait.getPackage(); 1301 mLayoutId = portrait.getLayoutId(); 1302 1303 mLandscape = landscape; 1304 mPortrait = portrait; 1305 1306 // setup the memory usage statistics 1307 mMemoryUsageCounter = new MemoryUsageCounter(); 1308 1309 mBitmapCache = new BitmapCache(); 1310 configureRemoteViewsAsChild(landscape); 1311 configureRemoteViewsAsChild(portrait); 1312 1313 recalculateMemoryUsage(); 1314 } 1315 1316 /** 1317 * Reads a RemoteViews object from a parcel. 1318 * 1319 * @param parcel 1320 */ 1321 public RemoteViews(Parcel parcel) { 1322 this(parcel, null); 1323 } 1324 1325 private RemoteViews(Parcel parcel, BitmapCache bitmapCache) { 1326 int mode = parcel.readInt(); 1327 1328 // We only store a bitmap cache in the root of the RemoteViews. 1329 if (bitmapCache == null) { 1330 mBitmapCache = new BitmapCache(parcel); 1331 } else { 1332 setBitmapCache(bitmapCache); 1333 setNotRoot(); 1334 } 1335 1336 if (mode == MODE_NORMAL) { 1337 mPackage = parcel.readString(); 1338 mLayoutId = parcel.readInt(); 1339 mIsWidgetCollectionChild = parcel.readInt() == 1 ? true : false; 1340 1341 int count = parcel.readInt(); 1342 if (count > 0) { 1343 mActions = new ArrayList<Action>(count); 1344 for (int i=0; i<count; i++) { 1345 int tag = parcel.readInt(); 1346 switch (tag) { 1347 case SetOnClickPendingIntent.TAG: 1348 mActions.add(new SetOnClickPendingIntent(parcel)); 1349 break; 1350 case SetDrawableParameters.TAG: 1351 mActions.add(new SetDrawableParameters(parcel)); 1352 break; 1353 case ReflectionAction.TAG: 1354 mActions.add(new ReflectionAction(parcel)); 1355 break; 1356 case ViewGroupAction.TAG: 1357 mActions.add(new ViewGroupAction(parcel, mBitmapCache)); 1358 break; 1359 case ReflectionActionWithoutParams.TAG: 1360 mActions.add(new ReflectionActionWithoutParams(parcel)); 1361 break; 1362 case SetEmptyView.TAG: 1363 mActions.add(new SetEmptyView(parcel)); 1364 break; 1365 case SetPendingIntentTemplate.TAG: 1366 mActions.add(new SetPendingIntentTemplate(parcel)); 1367 break; 1368 case SetOnClickFillInIntent.TAG: 1369 mActions.add(new SetOnClickFillInIntent(parcel)); 1370 break; 1371 case SetRemoteViewsAdapterIntent.TAG: 1372 mActions.add(new SetRemoteViewsAdapterIntent(parcel)); 1373 break; 1374 case TextViewDrawableAction.TAG: 1375 mActions.add(new TextViewDrawableAction(parcel)); 1376 break; 1377 case TextViewSizeAction.TAG: 1378 mActions.add(new TextViewSizeAction(parcel)); 1379 break; 1380 case BitmapReflectionAction.TAG: 1381 mActions.add(new BitmapReflectionAction(parcel)); 1382 break; 1383 default: 1384 throw new ActionException("Tag " + tag + " not found"); 1385 } 1386 } 1387 } 1388 } else { 1389 // MODE_HAS_LANDSCAPE_AND_PORTRAIT 1390 mLandscape = new RemoteViews(parcel, mBitmapCache); 1391 mPortrait = new RemoteViews(parcel, mBitmapCache); 1392 mPackage = mPortrait.getPackage(); 1393 mLayoutId = mPortrait.getLayoutId(); 1394 } 1395 1396 // setup the memory usage statistics 1397 mMemoryUsageCounter = new MemoryUsageCounter(); 1398 recalculateMemoryUsage(); 1399 } 1400 1401 @Override 1402 public RemoteViews clone() { 1403 RemoteViews that; 1404 if (!hasLandscapeAndPortraitLayouts()) { 1405 that = new RemoteViews(mPackage, mLayoutId); 1406 1407 if (mActions != null) { 1408 that.mActions = (ArrayList<Action>)mActions.clone(); 1409 } 1410 } else { 1411 RemoteViews land = mLandscape.clone(); 1412 RemoteViews port = mPortrait.clone(); 1413 that = new RemoteViews(land, port); 1414 } 1415 // update the memory usage stats of the cloned RemoteViews 1416 that.recalculateMemoryUsage(); 1417 return that; 1418 } 1419 1420 public String getPackage() { 1421 return mPackage; 1422 } 1423 1424 /** 1425 * Reutrns the layout id of the root layout associated with this RemoteViews. In the case 1426 * that the RemoteViews has both a landscape and portrait root, this will return the layout 1427 * id associated with the portrait layout. 1428 * 1429 * @return the layout id. 1430 */ 1431 public int getLayoutId() { 1432 return mLayoutId; 1433 } 1434 1435 /* 1436 * This flag indicates whether this RemoteViews object is being created from a 1437 * RemoteViewsService for use as a child of a widget collection. This flag is used 1438 * to determine whether or not certain features are available, in particular, 1439 * setting on click extras and setting on click pending intents. The former is enabled, 1440 * and the latter disabled when this flag is true. 1441 */ 1442 void setIsWidgetCollectionChild(boolean isWidgetCollectionChild) { 1443 mIsWidgetCollectionChild = isWidgetCollectionChild; 1444 } 1445 1446 /** 1447 * Updates the memory usage statistics. 1448 */ 1449 private void recalculateMemoryUsage() { 1450 mMemoryUsageCounter.clear(); 1451 1452 if (!hasLandscapeAndPortraitLayouts()) { 1453 // Accumulate the memory usage for each action 1454 if (mActions != null) { 1455 final int count = mActions.size(); 1456 for (int i= 0; i < count; ++i) { 1457 mActions.get(i).updateMemoryUsageEstimate(mMemoryUsageCounter); 1458 } 1459 } 1460 if (mIsRoot) { 1461 mBitmapCache.addBitmapMemory(mMemoryUsageCounter); 1462 } 1463 } else { 1464 mMemoryUsageCounter.increment(mLandscape.estimateMemoryUsage()); 1465 mMemoryUsageCounter.increment(mPortrait.estimateMemoryUsage()); 1466 mBitmapCache.addBitmapMemory(mMemoryUsageCounter); 1467 } 1468 } 1469 1470 /** 1471 * Recursively sets BitmapCache in the hierarchy and update the bitmap ids. 1472 */ 1473 private void setBitmapCache(BitmapCache bitmapCache) { 1474 mBitmapCache = bitmapCache; 1475 if (!hasLandscapeAndPortraitLayouts()) { 1476 if (mActions != null) { 1477 final int count = mActions.size(); 1478 for (int i= 0; i < count; ++i) { 1479 mActions.get(i).setBitmapCache(bitmapCache); 1480 } 1481 } 1482 } else { 1483 mLandscape.setBitmapCache(bitmapCache); 1484 mPortrait.setBitmapCache(bitmapCache); 1485 } 1486 } 1487 1488 /** 1489 * Returns an estimate of the bitmap heap memory usage for this RemoteViews. 1490 */ 1491 /** @hide */ 1492 public int estimateMemoryUsage() { 1493 return mMemoryUsageCounter.getMemoryUsage(); 1494 } 1495 1496 /** 1497 * Add an action to be executed on the remote side when apply is called. 1498 * 1499 * @param a The action to add 1500 */ 1501 private void addAction(Action a) { 1502 if (hasLandscapeAndPortraitLayouts()) { 1503 throw new RuntimeException("RemoteViews specifying separate landscape and portrait" + 1504 " layouts cannot be modified. Instead, fully configure the landscape and" + 1505 " portrait layouts individually before constructing the combined layout."); 1506 } 1507 if (mActions == null) { 1508 mActions = new ArrayList<Action>(); 1509 } 1510 mActions.add(a); 1511 1512 // update the memory usage stats 1513 a.updateMemoryUsageEstimate(mMemoryUsageCounter); 1514 } 1515 1516 /** 1517 * Equivalent to calling {@link ViewGroup#addView(View)} after inflating the 1518 * given {@link RemoteViews}. This allows users to build "nested" 1519 * {@link RemoteViews}. In cases where consumers of {@link RemoteViews} may 1520 * recycle layouts, use {@link #removeAllViews(int)} to clear any existing 1521 * children. 1522 * 1523 * @param viewId The id of the parent {@link ViewGroup} to add child into. 1524 * @param nestedView {@link RemoteViews} that describes the child. 1525 */ 1526 public void addView(int viewId, RemoteViews nestedView) { 1527 addAction(new ViewGroupAction(viewId, nestedView)); 1528 } 1529 1530 /** 1531 * Equivalent to calling {@link ViewGroup#removeAllViews()}. 1532 * 1533 * @param viewId The id of the parent {@link ViewGroup} to remove all 1534 * children from. 1535 */ 1536 public void removeAllViews(int viewId) { 1537 addAction(new ViewGroupAction(viewId, null)); 1538 } 1539 1540 /** 1541 * Equivalent to calling {@link AdapterViewAnimator#showNext()} 1542 * 1543 * @param viewId The id of the view on which to call {@link AdapterViewAnimator#showNext()} 1544 */ 1545 public void showNext(int viewId) { 1546 addAction(new ReflectionActionWithoutParams(viewId, "showNext")); 1547 } 1548 1549 /** 1550 * Equivalent to calling {@link AdapterViewAnimator#showPrevious()} 1551 * 1552 * @param viewId The id of the view on which to call {@link AdapterViewAnimator#showPrevious()} 1553 */ 1554 public void showPrevious(int viewId) { 1555 addAction(new ReflectionActionWithoutParams(viewId, "showPrevious")); 1556 } 1557 1558 /** 1559 * Equivalent to calling {@link AdapterViewAnimator#setDisplayedChild(int)} 1560 * 1561 * @param viewId The id of the view on which to call 1562 * {@link AdapterViewAnimator#setDisplayedChild(int)} 1563 */ 1564 public void setDisplayedChild(int viewId, int childIndex) { 1565 setInt(viewId, "setDisplayedChild", childIndex); 1566 } 1567 1568 /** 1569 * Equivalent to calling View.setVisibility 1570 * 1571 * @param viewId The id of the view whose visibility should change 1572 * @param visibility The new visibility for the view 1573 */ 1574 public void setViewVisibility(int viewId, int visibility) { 1575 setInt(viewId, "setVisibility", visibility); 1576 } 1577 1578 /** 1579 * Equivalent to calling TextView.setText 1580 * 1581 * @param viewId The id of the view whose text should change 1582 * @param text The new text for the view 1583 */ 1584 public void setTextViewText(int viewId, CharSequence text) { 1585 setCharSequence(viewId, "setText", text); 1586 } 1587 1588 /** 1589 * @hide 1590 * Equivalent to calling {@link TextView#setTextSize(int, float)} 1591 * 1592 * @param viewId The id of the view whose text size should change 1593 * @param units The units of size (e.g. COMPLEX_UNIT_SP) 1594 * @param size The size of the text 1595 */ 1596 public void setTextViewTextSize(int viewId, int units, float size) { 1597 addAction(new TextViewSizeAction(viewId, units, size)); 1598 } 1599 1600 /** 1601 * Equivalent to calling 1602 * {@link TextView#setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)}. 1603 * 1604 * @param viewId The id of the view whose text should change 1605 * @param left The id of a drawable to place to the left of the text, or 0 1606 * @param top The id of a drawable to place above the text, or 0 1607 * @param right The id of a drawable to place to the right of the text, or 0 1608 * @param bottom The id of a drawable to place below the text, or 0 1609 */ 1610 public void setTextViewCompoundDrawables(int viewId, int left, int top, int right, int bottom) { 1611 addAction(new TextViewDrawableAction(viewId, false, left, top, right, bottom)); 1612 } 1613 1614 /** 1615 * Equivalent to calling {@link 1616 * TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(int, int, int, int)}. 1617 * 1618 * @param viewId The id of the view whose text should change 1619 * @param start The id of a drawable to place before the text (relative to the 1620 * layout direction), or 0 1621 * @param top The id of a drawable to place above the text, or 0 1622 * @param end The id of a drawable to place after the text, or 0 1623 * @param bottom The id of a drawable to place below the text, or 0 1624 */ 1625 public void setTextViewCompoundDrawablesRelative(int viewId, int start, int top, int end, int bottom) { 1626 addAction(new TextViewDrawableAction(viewId, true, start, top, end, bottom)); 1627 } 1628 1629 /** 1630 * Equivalent to calling ImageView.setImageResource 1631 * 1632 * @param viewId The id of the view whose drawable should change 1633 * @param srcId The new resource id for the drawable 1634 */ 1635 public void setImageViewResource(int viewId, int srcId) { 1636 setInt(viewId, "setImageResource", srcId); 1637 } 1638 1639 /** 1640 * Equivalent to calling ImageView.setImageURI 1641 * 1642 * @param viewId The id of the view whose drawable should change 1643 * @param uri The Uri for the image 1644 */ 1645 public void setImageViewUri(int viewId, Uri uri) { 1646 setUri(viewId, "setImageURI", uri); 1647 } 1648 1649 /** 1650 * Equivalent to calling ImageView.setImageBitmap 1651 * 1652 * @param viewId The id of the view whose bitmap should change 1653 * @param bitmap The new Bitmap for the drawable 1654 */ 1655 public void setImageViewBitmap(int viewId, Bitmap bitmap) { 1656 setBitmap(viewId, "setImageBitmap", bitmap); 1657 } 1658 1659 /** 1660 * Equivalent to calling AdapterView.setEmptyView 1661 * 1662 * @param viewId The id of the view on which to set the empty view 1663 * @param emptyViewId The view id of the empty view 1664 */ 1665 public void setEmptyView(int viewId, int emptyViewId) { 1666 addAction(new SetEmptyView(viewId, emptyViewId)); 1667 } 1668 1669 /** 1670 * Equivalent to calling {@link Chronometer#setBase Chronometer.setBase}, 1671 * {@link Chronometer#setFormat Chronometer.setFormat}, 1672 * and {@link Chronometer#start Chronometer.start()} or 1673 * {@link Chronometer#stop Chronometer.stop()}. 1674 * 1675 * @param viewId The id of the {@link Chronometer} to change 1676 * @param base The time at which the timer would have read 0:00. This 1677 * time should be based off of 1678 * {@link android.os.SystemClock#elapsedRealtime SystemClock.elapsedRealtime()}. 1679 * @param format The Chronometer format string, or null to 1680 * simply display the timer value. 1681 * @param started True if you want the clock to be started, false if not. 1682 */ 1683 public void setChronometer(int viewId, long base, String format, boolean started) { 1684 setLong(viewId, "setBase", base); 1685 setString(viewId, "setFormat", format); 1686 setBoolean(viewId, "setStarted", started); 1687 } 1688 1689 /** 1690 * Equivalent to calling {@link ProgressBar#setMax ProgressBar.setMax}, 1691 * {@link ProgressBar#setProgress ProgressBar.setProgress}, and 1692 * {@link ProgressBar#setIndeterminate ProgressBar.setIndeterminate} 1693 * 1694 * If indeterminate is true, then the values for max and progress are ignored. 1695 * 1696 * @param viewId The id of the {@link ProgressBar} to change 1697 * @param max The 100% value for the progress bar 1698 * @param progress The current value of the progress bar. 1699 * @param indeterminate True if the progress bar is indeterminate, 1700 * false if not. 1701 */ 1702 public void setProgressBar(int viewId, int max, int progress, 1703 boolean indeterminate) { 1704 setBoolean(viewId, "setIndeterminate", indeterminate); 1705 if (!indeterminate) { 1706 setInt(viewId, "setMax", max); 1707 setInt(viewId, "setProgress", progress); 1708 } 1709 } 1710 1711 /** 1712 * Equivalent to calling 1713 * {@link android.view.View#setOnClickListener(android.view.View.OnClickListener)} 1714 * to launch the provided {@link PendingIntent}. 1715 * 1716 * When setting the on-click action of items within collections (eg. {@link ListView}, 1717 * {@link StackView} etc.), this method will not work. Instead, use {@link 1718 * RemoteViews#setPendingIntentTemplate(int, PendingIntent) in conjunction with 1719 * RemoteViews#setOnClickFillInIntent(int, Intent). 1720 * 1721 * @param viewId The id of the view that will trigger the {@link PendingIntent} when clicked 1722 * @param pendingIntent The {@link PendingIntent} to send when user clicks 1723 */ 1724 public void setOnClickPendingIntent(int viewId, PendingIntent pendingIntent) { 1725 addAction(new SetOnClickPendingIntent(viewId, pendingIntent)); 1726 } 1727 1728 /** 1729 * When using collections (eg. {@link ListView}, {@link StackView} etc.) in widgets, it is very 1730 * costly to set PendingIntents on the individual items, and is hence not permitted. Instead 1731 * this method should be used to set a single PendingIntent template on the collection, and 1732 * individual items can differentiate their on-click behavior using 1733 * {@link RemoteViews#setOnClickFillInIntent(int, Intent)}. 1734 * 1735 * @param viewId The id of the collection who's children will use this PendingIntent template 1736 * when clicked 1737 * @param pendingIntentTemplate The {@link PendingIntent} to be combined with extras specified 1738 * by a child of viewId and executed when that child is clicked 1739 */ 1740 public void setPendingIntentTemplate(int viewId, PendingIntent pendingIntentTemplate) { 1741 addAction(new SetPendingIntentTemplate(viewId, pendingIntentTemplate)); 1742 } 1743 1744 /** 1745 * When using collections (eg. {@link ListView}, {@link StackView} etc.) in widgets, it is very 1746 * costly to set PendingIntents on the individual items, and is hence not permitted. Instead 1747 * a single PendingIntent template can be set on the collection, see {@link 1748 * RemoteViews#setPendingIntentTemplate(int, PendingIntent)}, and the individual on-click 1749 * action of a given item can be distinguished by setting a fillInIntent on that item. The 1750 * fillInIntent is then combined with the PendingIntent template in order to determine the final 1751 * intent which will be executed when the item is clicked. This works as follows: any fields 1752 * which are left blank in the PendingIntent template, but are provided by the fillInIntent 1753 * will be overwritten, and the resulting PendingIntent will be used. 1754 * 1755 * 1756 * of the PendingIntent template will then be filled in with the associated fields that are 1757 * set in fillInIntent. See {@link Intent#fillIn(Intent, int)} for more details. 1758 * 1759 * @param viewId The id of the view on which to set the fillInIntent 1760 * @param fillInIntent The intent which will be combined with the parent's PendingIntent 1761 * in order to determine the on-click behavior of the view specified by viewId 1762 */ 1763 public void setOnClickFillInIntent(int viewId, Intent fillInIntent) { 1764 addAction(new SetOnClickFillInIntent(viewId, fillInIntent)); 1765 } 1766 1767 /** 1768 * @hide 1769 * Equivalent to calling a combination of {@link Drawable#setAlpha(int)}, 1770 * {@link Drawable#setColorFilter(int, android.graphics.PorterDuff.Mode)}, 1771 * and/or {@link Drawable#setLevel(int)} on the {@link Drawable} of a given 1772 * view. 1773 * <p> 1774 * You can omit specific calls by marking their values with null or -1. 1775 * 1776 * @param viewId The id of the view that contains the target 1777 * {@link Drawable} 1778 * @param targetBackground If true, apply these parameters to the 1779 * {@link Drawable} returned by 1780 * {@link android.view.View#getBackground()}. Otherwise, assume 1781 * the target view is an {@link ImageView} and apply them to 1782 * {@link ImageView#getDrawable()}. 1783 * @param alpha Specify an alpha value for the drawable, or -1 to leave 1784 * unchanged. 1785 * @param colorFilter Specify a color for a 1786 * {@link android.graphics.ColorFilter} for this drawable, or -1 1787 * to leave unchanged. 1788 * @param mode Specify a PorterDuff mode for this drawable, or null to leave 1789 * unchanged. 1790 * @param level Specify the level for the drawable, or -1 to leave 1791 * unchanged. 1792 */ 1793 public void setDrawableParameters(int viewId, boolean targetBackground, int alpha, 1794 int colorFilter, PorterDuff.Mode mode, int level) { 1795 addAction(new SetDrawableParameters(viewId, targetBackground, alpha, 1796 colorFilter, mode, level)); 1797 } 1798 1799 /** 1800 * Equivalent to calling {@link android.widget.TextView#setTextColor(int)}. 1801 * 1802 * @param viewId The id of the view whose text color should change 1803 * @param color Sets the text color for all the states (normal, selected, 1804 * focused) to be this color. 1805 */ 1806 public void setTextColor(int viewId, int color) { 1807 setInt(viewId, "setTextColor", color); 1808 } 1809 1810 /** 1811 * Equivalent to calling {@link android.widget.AbsListView#setRemoteViewsAdapter(Intent)}. 1812 * 1813 * @param appWidgetId The id of the app widget which contains the specified view. (This 1814 * parameter is ignored in this deprecated method) 1815 * @param viewId The id of the {@link AbsListView} 1816 * @param intent The intent of the service which will be 1817 * providing data to the RemoteViewsAdapter 1818 * @deprecated This method has been deprecated. See 1819 * {@link android.widget.RemoteViews#setRemoteAdapter(int, Intent)} 1820 */ 1821 @Deprecated 1822 public void setRemoteAdapter(int appWidgetId, int viewId, Intent intent) { 1823 setRemoteAdapter(viewId, intent); 1824 } 1825 1826 /** 1827 * Equivalent to calling {@link android.widget.AbsListView#setRemoteViewsAdapter(Intent)}. 1828 * Can only be used for App Widgets. 1829 * 1830 * @param viewId The id of the {@link AbsListView} 1831 * @param intent The intent of the service which will be 1832 * providing data to the RemoteViewsAdapter 1833 */ 1834 public void setRemoteAdapter(int viewId, Intent intent) { 1835 addAction(new SetRemoteViewsAdapterIntent(viewId, intent)); 1836 } 1837 1838 /** 1839 * Equivalent to calling {@link android.widget.AbsListView#smoothScrollToPosition(int, int)}. 1840 * 1841 * @param viewId The id of the view to change 1842 * @param position Scroll to this adapter position 1843 */ 1844 public void setScrollPosition(int viewId, int position) { 1845 setInt(viewId, "smoothScrollToPosition", position); 1846 } 1847 1848 /** 1849 * Equivalent to calling {@link android.widget.AbsListView#smoothScrollToPosition(int, int)}. 1850 * 1851 * @param viewId The id of the view to change 1852 * @param offset Scroll by this adapter position offset 1853 */ 1854 public void setRelativeScrollPosition(int viewId, int offset) { 1855 setInt(viewId, "smoothScrollByOffset", offset); 1856 } 1857 1858 /** 1859 * Call a method taking one boolean on a view in the layout for this RemoteViews. 1860 * 1861 * @param viewId The id of the view on which to call the method. 1862 * @param methodName The name of the method to call. 1863 * @param value The value to pass to the method. 1864 */ 1865 public void setBoolean(int viewId, String methodName, boolean value) { 1866 addAction(new ReflectionAction(viewId, methodName, ReflectionAction.BOOLEAN, value)); 1867 } 1868 1869 /** 1870 * Call a method taking one byte on a view in the layout for this RemoteViews. 1871 * 1872 * @param viewId The id of the view on which to call the method. 1873 * @param methodName The name of the method to call. 1874 * @param value The value to pass to the method. 1875 */ 1876 public void setByte(int viewId, String methodName, byte value) { 1877 addAction(new ReflectionAction(viewId, methodName, ReflectionAction.BYTE, value)); 1878 } 1879 1880 /** 1881 * Call a method taking one short on a view in the layout for this RemoteViews. 1882 * 1883 * @param viewId The id of the view on which to call the method. 1884 * @param methodName The name of the method to call. 1885 * @param value The value to pass to the method. 1886 */ 1887 public void setShort(int viewId, String methodName, short value) { 1888 addAction(new ReflectionAction(viewId, methodName, ReflectionAction.SHORT, value)); 1889 } 1890 1891 /** 1892 * Call a method taking one int on a view in the layout for this RemoteViews. 1893 * 1894 * @param viewId The id of the view on which to call the method. 1895 * @param methodName The name of the method to call. 1896 * @param value The value to pass to the method. 1897 */ 1898 public void setInt(int viewId, String methodName, int value) { 1899 addAction(new ReflectionAction(viewId, methodName, ReflectionAction.INT, value)); 1900 } 1901 1902 /** 1903 * Call a method taking one long on a view in the layout for this RemoteViews. 1904 * 1905 * @param viewId The id of the view on which to call the method. 1906 * @param methodName The name of the method to call. 1907 * @param value The value to pass to the method. 1908 */ 1909 public void setLong(int viewId, String methodName, long value) { 1910 addAction(new ReflectionAction(viewId, methodName, ReflectionAction.LONG, value)); 1911 } 1912 1913 /** 1914 * Call a method taking one float on a view in the layout for this RemoteViews. 1915 * 1916 * @param viewId The id of the view on which to call the method. 1917 * @param methodName The name of the method to call. 1918 * @param value The value to pass to the method. 1919 */ 1920 public void setFloat(int viewId, String methodName, float value) { 1921 addAction(new ReflectionAction(viewId, methodName, ReflectionAction.FLOAT, value)); 1922 } 1923 1924 /** 1925 * Call a method taking one double on a view in the layout for this RemoteViews. 1926 * 1927 * @param viewId The id of the view on which to call the method. 1928 * @param methodName The name of the method to call. 1929 * @param value The value to pass to the method. 1930 */ 1931 public void setDouble(int viewId, String methodName, double value) { 1932 addAction(new ReflectionAction(viewId, methodName, ReflectionAction.DOUBLE, value)); 1933 } 1934 1935 /** 1936 * Call a method taking one char on a view in the layout for this RemoteViews. 1937 * 1938 * @param viewId The id of the view on which to call the method. 1939 * @param methodName The name of the method to call. 1940 * @param value The value to pass to the method. 1941 */ 1942 public void setChar(int viewId, String methodName, char value) { 1943 addAction(new ReflectionAction(viewId, methodName, ReflectionAction.CHAR, value)); 1944 } 1945 1946 /** 1947 * Call a method taking one String on a view in the layout for this RemoteViews. 1948 * 1949 * @param viewId The id of the view on which to call the method. 1950 * @param methodName The name of the method to call. 1951 * @param value The value to pass to the method. 1952 */ 1953 public void setString(int viewId, String methodName, String value) { 1954 addAction(new ReflectionAction(viewId, methodName, ReflectionAction.STRING, value)); 1955 } 1956 1957 /** 1958 * Call a method taking one CharSequence on a view in the layout for this RemoteViews. 1959 * 1960 * @param viewId The id of the view on which to call the method. 1961 * @param methodName The name of the method to call. 1962 * @param value The value to pass to the method. 1963 */ 1964 public void setCharSequence(int viewId, String methodName, CharSequence value) { 1965 addAction(new ReflectionAction(viewId, methodName, ReflectionAction.CHAR_SEQUENCE, value)); 1966 } 1967 1968 /** 1969 * Call a method taking one Uri on a view in the layout for this RemoteViews. 1970 * 1971 * @param viewId The id of the view on which to call the method. 1972 * @param methodName The name of the method to call. 1973 * @param value The value to pass to the method. 1974 */ 1975 public void setUri(int viewId, String methodName, Uri value) { 1976 addAction(new ReflectionAction(viewId, methodName, ReflectionAction.URI, value)); 1977 } 1978 1979 /** 1980 * Call a method taking one Bitmap on a view in the layout for this RemoteViews. 1981 * @more 1982 * <p class="note">The bitmap will be flattened into the parcel if this object is 1983 * sent across processes, so it may end up using a lot of memory, and may be fairly slow.</p> 1984 * 1985 * @param viewId The id of the view on which to call the method. 1986 * @param methodName The name of the method to call. 1987 * @param value The value to pass to the method. 1988 */ 1989 public void setBitmap(int viewId, String methodName, Bitmap value) { 1990 addAction(new BitmapReflectionAction(viewId, methodName, value)); 1991 } 1992 1993 /** 1994 * Call a method taking one Bundle on a view in the layout for this RemoteViews. 1995 * 1996 * @param viewId The id of the view on which to call the method. 1997 * @param methodName The name of the method to call. 1998 * @param value The value to pass to the method. 1999 */ 2000 public void setBundle(int viewId, String methodName, Bundle value) { 2001 addAction(new ReflectionAction(viewId, methodName, ReflectionAction.BUNDLE, value)); 2002 } 2003 2004 /** 2005 * Call a method taking one Intent on a view in the layout for this RemoteViews. 2006 * 2007 * @param viewId The id of the view on which to call the method. 2008 * @param methodName The name of the method to call. 2009 * @param value The {@link android.content.Intent} to pass the method. 2010 */ 2011 public void setIntent(int viewId, String methodName, Intent value) { 2012 addAction(new ReflectionAction(viewId, methodName, ReflectionAction.INTENT, value)); 2013 } 2014 2015 /** 2016 * Equivalent to calling View.setContentDescription 2017 * 2018 * @param viewId The id of the view whose content description should change 2019 * @param contentDescription The new content description for the view 2020 */ 2021 public void setContentDescription(int viewId, CharSequence contentDescription) { 2022 setCharSequence(viewId, "setContentDescription", contentDescription); 2023 } 2024 2025 private RemoteViews getRemoteViewsToApply(Context context) { 2026 if (hasLandscapeAndPortraitLayouts()) { 2027 int orientation = context.getResources().getConfiguration().orientation; 2028 if (orientation == Configuration.ORIENTATION_LANDSCAPE) { 2029 return mLandscape; 2030 } else { 2031 return mPortrait; 2032 } 2033 } 2034 return this; 2035 } 2036 2037 /** 2038 * Inflates the view hierarchy represented by this object and applies 2039 * all of the actions. 2040 * 2041 * <p><strong>Caller beware: this may throw</strong> 2042 * 2043 * @param context Default context to use 2044 * @param parent Parent that the resulting view hierarchy will be attached to. This method 2045 * does <strong>not</strong> attach the hierarchy. The caller should do so when appropriate. 2046 * @return The inflated view hierarchy 2047 */ 2048 public View apply(Context context, ViewGroup parent) { 2049 RemoteViews rvToApply = getRemoteViewsToApply(context); 2050 2051 View result; 2052 2053 Context c = prepareContext(context); 2054 2055 LayoutInflater inflater = (LayoutInflater) 2056 c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 2057 2058 inflater = inflater.cloneInContext(c); 2059 inflater.setFilter(this); 2060 2061 result = inflater.inflate(rvToApply.getLayoutId(), parent, false); 2062 2063 rvToApply.performApply(result, parent); 2064 2065 return result; 2066 } 2067 2068 /** 2069 * Applies all of the actions to the provided view. 2070 * 2071 * <p><strong>Caller beware: this may throw</strong> 2072 * 2073 * @param v The view to apply the actions to. This should be the result of 2074 * the {@link #apply(Context,ViewGroup)} call. 2075 */ 2076 public void reapply(Context context, View v) { 2077 RemoteViews rvToApply = getRemoteViewsToApply(context); 2078 2079 // In the case that a view has this RemoteViews applied in one orientation, is persisted 2080 // across orientation change, and has the RemoteViews re-applied in the new orientation, 2081 // we throw an exception, since the layouts may be completely unrelated. 2082 if (hasLandscapeAndPortraitLayouts()) { 2083 if (v.getId() != rvToApply.getLayoutId()) { 2084 throw new RuntimeException("Attempting to re-apply RemoteViews to a view that" + 2085 " that does not share the same root layout id."); 2086 } 2087 } 2088 2089 prepareContext(context); 2090 rvToApply.performApply(v, (ViewGroup) v.getParent()); 2091 } 2092 2093 private void performApply(View v, ViewGroup parent) { 2094 if (mActions != null) { 2095 final int count = mActions.size(); 2096 for (int i = 0; i < count; i++) { 2097 Action a = mActions.get(i); 2098 a.apply(v, parent); 2099 } 2100 } 2101 } 2102 2103 private Context prepareContext(Context context) { 2104 Context c; 2105 String packageName = mPackage; 2106 2107 if (packageName != null) { 2108 try { 2109 c = context.createPackageContext(packageName, Context.CONTEXT_RESTRICTED); 2110 } catch (NameNotFoundException e) { 2111 Log.e(LOG_TAG, "Package name " + packageName + " not found"); 2112 c = context; 2113 } 2114 } else { 2115 c = context; 2116 } 2117 2118 return c; 2119 } 2120 2121 /* (non-Javadoc) 2122 * Used to restrict the views which can be inflated 2123 * 2124 * @see android.view.LayoutInflater.Filter#onLoadClass(java.lang.Class) 2125 */ 2126 public boolean onLoadClass(Class clazz) { 2127 return clazz.isAnnotationPresent(RemoteView.class); 2128 } 2129 2130 public int describeContents() { 2131 return 0; 2132 } 2133 2134 public void writeToParcel(Parcel dest, int flags) { 2135 if (!hasLandscapeAndPortraitLayouts()) { 2136 dest.writeInt(MODE_NORMAL); 2137 // We only write the bitmap cache if we are the root RemoteViews, as this cache 2138 // is shared by all children. 2139 if (mIsRoot) { 2140 mBitmapCache.writeBitmapsToParcel(dest, flags); 2141 } 2142 dest.writeString(mPackage); 2143 dest.writeInt(mLayoutId); 2144 dest.writeInt(mIsWidgetCollectionChild ? 1 : 0); 2145 int count; 2146 if (mActions != null) { 2147 count = mActions.size(); 2148 } else { 2149 count = 0; 2150 } 2151 dest.writeInt(count); 2152 for (int i=0; i<count; i++) { 2153 Action a = mActions.get(i); 2154 a.writeToParcel(dest, 0); 2155 } 2156 } else { 2157 dest.writeInt(MODE_HAS_LANDSCAPE_AND_PORTRAIT); 2158 // We only write the bitmap cache if we are the root RemoteViews, as this cache 2159 // is shared by all children. 2160 if (mIsRoot) { 2161 mBitmapCache.writeBitmapsToParcel(dest, flags); 2162 } 2163 mLandscape.writeToParcel(dest, flags); 2164 mPortrait.writeToParcel(dest, flags); 2165 } 2166 } 2167 2168 /** 2169 * Parcelable.Creator that instantiates RemoteViews objects 2170 */ 2171 public static final Parcelable.Creator<RemoteViews> CREATOR = new Parcelable.Creator<RemoteViews>() { 2172 public RemoteViews createFromParcel(Parcel parcel) { 2173 return new RemoteViews(parcel); 2174 } 2175 2176 public RemoteViews[] newArray(int size) { 2177 return new RemoteViews[size]; 2178 } 2179 }; 2180} 2181