1 package org.robolectric.shadows; 2 3 import static android.os.Build.VERSION_CODES.N; 4 import static org.robolectric.RuntimeEnvironment.getApiLevel; 5 import static org.robolectric.shadows.ResourceHelper.getInternalResourceId; 6 7 import android.app.Notification; 8 import android.graphics.Bitmap; 9 import android.graphics.drawable.BitmapDrawable; 10 import android.os.Build; 11 import android.view.View; 12 import android.widget.FrameLayout; 13 import android.widget.ImageView; 14 import android.widget.ProgressBar; 15 import android.widget.TextView; 16 import java.io.ByteArrayOutputStream; 17 import java.io.PrintStream; 18 import org.robolectric.RuntimeEnvironment; 19 import org.robolectric.Shadows; 20 import org.robolectric.annotation.Implements; 21 import org.robolectric.annotation.RealObject; 22 23 @Implements(Notification.class) 24 public class ShadowNotification { 25 26 @RealObject 27 Notification realNotification; 28 getContentTitle()29 public CharSequence getContentTitle() { 30 return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N 31 ? realNotification.extras.getString(Notification.EXTRA_TITLE) 32 : findText(applyContentView(), "title"); 33 } 34 getContentText()35 public CharSequence getContentText() { 36 return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N 37 ? realNotification.extras.getString(Notification.EXTRA_TEXT) 38 : findText(applyContentView(), "text"); 39 } 40 getContentInfo()41 public CharSequence getContentInfo() { 42 if (getApiLevel() >= N) { 43 return realNotification.extras.getCharSequence(Notification.EXTRA_INFO_TEXT); 44 } else { 45 return findText(applyContentView(), "info"); 46 } 47 } 48 isOngoing()49 public boolean isOngoing() { 50 return ((realNotification.flags & Notification.FLAG_ONGOING_EVENT) == Notification.FLAG_ONGOING_EVENT); 51 } 52 getBigText()53 public CharSequence getBigText() { 54 if (getApiLevel() >= N) { 55 return realNotification.extras.getCharSequence(Notification.EXTRA_BIG_TEXT); 56 } else { 57 return findText(applyBigContentView(), "big_text"); 58 } 59 } 60 getBigContentTitle()61 public CharSequence getBigContentTitle() { 62 if (getApiLevel() >= N) { 63 return realNotification.extras.getCharSequence(Notification.EXTRA_TITLE_BIG); 64 } else { 65 return findText(applyBigContentView(), "title"); 66 } 67 } 68 getBigContentText()69 public CharSequence getBigContentText() { 70 if (getApiLevel() >= N) { 71 return realNotification.extras.getCharSequence(Notification.EXTRA_SUMMARY_TEXT); 72 } else { 73 return findText(applyBigContentView(), "text"); 74 } 75 } 76 getBigPicture()77 public Bitmap getBigPicture() { 78 if (RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N) { 79 return realNotification.extras.getParcelable(Notification.EXTRA_PICTURE); 80 } else { 81 ImageView imageView = (ImageView) applyBigContentView().findViewById(getInternalResourceId("big_picture")); 82 return imageView != null && imageView.getDrawable() != null 83 ? ((BitmapDrawable) imageView.getDrawable()).getBitmap() : null; 84 } 85 } 86 isWhenShown()87 public boolean isWhenShown() { 88 return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N 89 ? realNotification.extras.getBoolean(Notification.EXTRA_SHOW_WHEN) 90 : findView(applyContentView(), "chronometer").getVisibility() == View.VISIBLE 91 || findView(applyContentView(), "time").getVisibility() == View.VISIBLE; 92 } 93 getProgressBar_PreN()94 private ProgressBar getProgressBar_PreN() { 95 return ((ProgressBar) findView(applyContentView(), "progress")); 96 } 97 isIndeterminate()98 public boolean isIndeterminate() { 99 return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N 100 ? realNotification.extras.getBoolean(Notification.EXTRA_PROGRESS_INDETERMINATE) 101 : getProgressBar_PreN().isIndeterminate(); 102 } 103 getMax()104 public int getMax() { 105 return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N 106 ? realNotification.extras.getInt(Notification.EXTRA_PROGRESS_MAX) 107 : getProgressBar_PreN().getMax(); 108 } 109 getProgress()110 public int getProgress() { 111 return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N 112 ? realNotification.extras.getInt(Notification.EXTRA_PROGRESS) 113 : getProgressBar_PreN().getProgress(); 114 } 115 usesChronometer()116 public boolean usesChronometer() { 117 return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N 118 ? realNotification.extras.getBoolean(Notification.EXTRA_SHOW_CHRONOMETER) 119 : findView(applyContentView(), "chronometer").getVisibility() == View.VISIBLE; 120 } 121 applyContentView()122 private View applyContentView() { 123 return realNotification.contentView.apply(RuntimeEnvironment.application, new FrameLayout(RuntimeEnvironment.application)); 124 } 125 applyBigContentView()126 private View applyBigContentView() { 127 return realNotification.bigContentView.apply(RuntimeEnvironment.application, new FrameLayout(RuntimeEnvironment.application)); 128 } 129 findText(View view, String resourceName)130 private CharSequence findText(View view, String resourceName) { 131 TextView textView = (TextView) findView(view, resourceName); 132 return textView.getText(); 133 } 134 findView(View view, String resourceName)135 private View findView(View view, String resourceName) { 136 View subView = view.findViewById(getInternalResourceId(resourceName)); 137 if (subView == null) { 138 ByteArrayOutputStream buf = new ByteArrayOutputStream(); 139 Shadows.shadowOf(view).dump(new PrintStream(buf), 4); 140 throw new IllegalArgumentException("no id." + resourceName + " found in view:\n" + buf.toString()); 141 } 142 return subView; 143 } 144 } 145