1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.app.PendingIntent;
4 import android.content.Context;
5 import android.graphics.Bitmap;
6 import android.view.View;
7 import android.widget.ImageView;
8 import android.widget.RemoteViews;
9 import android.widget.TextView;
10 import com.xtremelabs.robolectric.internal.Implementation;
11 import com.xtremelabs.robolectric.internal.Implements;
12 import com.xtremelabs.robolectric.res.ResourceLoader;
13 
14 import java.util.ArrayList;
15 import 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)
23 public class ShadowRemoteViews {
24     private String packageName;
25     private int layoutId;
26     private List<ViewUpdater> viewUpdaters = new ArrayList<ViewUpdater>();
27 
__constructor__(String packageName, int layoutId)28     public void __constructor__(String packageName, int layoutId) {
29         this.packageName = packageName;
30         this.layoutId = layoutId;
31     }
32 
33     @Implementation
getPackage()34     public String getPackage() {
35         return packageName;
36     }
37 
38     @Implementation
getLayoutId()39     public int getLayoutId() {
40         return layoutId;
41     }
42 
43     @Implementation
setTextViewText(int viewId, final CharSequence text)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
setOnClickPendingIntent(int viewId, final PendingIntent pendingIntent)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
setViewVisibility(int viewId, final int visibility)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
setImageViewResource(int viewId, final int resourceId)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
setImageViewBitmap(int viewId, final Bitmap bitmap)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
reapply(Context context, View v)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 
ViewUpdater(int viewId)111         public ViewUpdater(int viewId) {
112             this.viewId = viewId;
113         }
114 
update(View parent)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 
doUpdate(View view)125         abstract void doUpdate(View view);
126     }
127 }
128