1package com.xtremelabs.robolectric.shadows; 2 3import android.app.PendingIntent; 4import android.content.Context; 5import android.graphics.Bitmap; 6import android.view.View; 7import android.widget.ImageView; 8import android.widget.RemoteViews; 9import android.widget.TextView; 10import com.xtremelabs.robolectric.internal.Implementation; 11import com.xtremelabs.robolectric.internal.Implements; 12import com.xtremelabs.robolectric.res.ResourceLoader; 13 14import java.util.ArrayList; 15import java.util.List; 16 17/** 18 * Shadow for {@code RemoteViews} that simulates its implementation. Supports pending intents for events, changing 19 * visibility, setting text, updating etc... 20 */ 21@SuppressWarnings({"UnusedDeclaration"}) 22@Implements(RemoteViews.class) 23public class ShadowRemoteViews { 24 private String packageName; 25 private int layoutId; 26 private List<ViewUpdater> viewUpdaters = new ArrayList<ViewUpdater>(); 27 28 public void __constructor__(String packageName, int layoutId) { 29 this.packageName = packageName; 30 this.layoutId = layoutId; 31 } 32 33 @Implementation 34 public String getPackage() { 35 return packageName; 36 } 37 38 @Implementation 39 public int getLayoutId() { 40 return layoutId; 41 } 42 43 @Implementation 44 public void setTextViewText(int viewId, final CharSequence text) { 45 viewUpdaters.add(new ViewUpdater(viewId) { 46 @Override 47 public void doUpdate(View view) { 48 ((TextView) view).setText(text); 49 } 50 }); 51 } 52 53 @Implementation 54 public void setOnClickPendingIntent(int viewId, final PendingIntent pendingIntent) { 55 viewUpdaters.add(new ViewUpdater(viewId) { 56 @Override void doUpdate(final View view) { 57 view.setOnClickListener(new View.OnClickListener() { 58 @Override 59 public void onClick(View v) { 60 try { 61 pendingIntent.send(view.getContext(), 0, null); 62 } catch (PendingIntent.CanceledException e) { 63 throw new RuntimeException(e); 64 } 65 } 66 }); 67 } 68 }); 69 } 70 71 @Implementation 72 public void setViewVisibility(int viewId, final int visibility) { 73 viewUpdaters.add(new ViewUpdater(viewId) { 74 @Override 75 public void doUpdate(View view) { 76 view.setVisibility(visibility); 77 } 78 }); 79 } 80 81 @Implementation 82 public void setImageViewResource(int viewId, final int resourceId) { 83 viewUpdaters.add(new ViewUpdater(viewId) { 84 @Override 85 public void doUpdate(View view) { 86 ((ImageView) view).setImageResource(resourceId); 87 } 88 }); 89 } 90 91 @Implementation 92 public void setImageViewBitmap(int viewId, final Bitmap bitmap) { 93 viewUpdaters.add(new ViewUpdater(viewId) { 94 @Override 95 public void doUpdate(View view) { 96 ((ImageView) view).setImageBitmap(bitmap); 97 } 98 }); 99 } 100 101 @Implementation 102 public void reapply(Context context, View v) { 103 for (ViewUpdater viewUpdater : viewUpdaters) { 104 viewUpdater.update(v); 105 } 106 } 107 108 private abstract class ViewUpdater { 109 private int viewId; 110 111 public ViewUpdater(int viewId) { 112 this.viewId = viewId; 113 } 114 115 final void update(View parent) { 116 117 View view = parent.findViewById(viewId); 118 if (view == null) { 119 throw new NullPointerException("couldn't find view " + viewId 120 + " (" + ResourceLoader.getFrom(parent.getContext()).getNameForId(viewId) + ")"); 121 } 122 doUpdate(view); 123 } 124 125 abstract void doUpdate(View view); 126 } 127} 128