PageRenderTime 201ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/xtremelabs/robolectric/shadows/ShadowRemoteViews.java

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