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