19d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrishpackage org.robolectric.shadows;
29d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
3851f2a9519be23c73a9e2929128179b405e2e7a6Christian Williamsimport static android.os.Build.VERSION_CODES.N;
4851f2a9519be23c73a9e2929128179b405e2e7a6Christian Williamsimport static org.robolectric.RuntimeEnvironment.getApiLevel;
5851f2a9519be23c73a9e2929128179b405e2e7a6Christian Williamsimport static org.robolectric.shadows.ResourceHelper.getInternalResourceId;
6851f2a9519be23c73a9e2929128179b405e2e7a6Christian Williams
79d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrishimport android.app.Notification;
8e07896813c99dccd0e27e837fc74b02ef00b7360Jonathan Gerrishimport android.graphics.Bitmap;
9e07896813c99dccd0e27e837fc74b02ef00b7360Jonathan Gerrishimport android.graphics.drawable.BitmapDrawable;
100dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrishimport android.os.Build;
119d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrishimport android.view.View;
129d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrishimport android.widget.FrameLayout;
13e07896813c99dccd0e27e837fc74b02ef00b7360Jonathan Gerrishimport android.widget.ImageView;
149d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrishimport android.widget.ProgressBar;
159d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrishimport android.widget.TextView;
16851f2a9519be23c73a9e2929128179b405e2e7a6Christian Williamsimport java.io.ByteArrayOutputStream;
17851f2a9519be23c73a9e2929128179b405e2e7a6Christian Williamsimport java.io.PrintStream;
189d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrishimport org.robolectric.RuntimeEnvironment;
19719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrishimport org.robolectric.Shadows;
209d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrishimport org.robolectric.annotation.Implements;
219d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrishimport org.robolectric.annotation.RealObject;
229d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
239d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish@Implements(Notification.class)
249d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrishpublic class ShadowNotification {
259d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
269d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  @RealObject
279d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  Notification realNotification;
289d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
299d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  public CharSequence getContentTitle() {
300dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
310dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish        ? realNotification.extras.getString(Notification.EXTRA_TITLE)
320dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish        : findText(applyContentView(), "title");
339d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  }
349d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
359d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  public CharSequence getContentText() {
360dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
370dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish        ? realNotification.extras.getString(Notification.EXTRA_TEXT)
380dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish        : findText(applyContentView(), "text");
399d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  }
409d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
419d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  public CharSequence getContentInfo() {
420dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    if (getApiLevel() >= N) {
430dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish      return realNotification.extras.getCharSequence(Notification.EXTRA_INFO_TEXT);
440dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    } else {
455263c73e2eadfb9cabdf52d3cfcf1dc7e2832e44James Zhang-Yao Cheng      return findText(applyContentView(), "info");
460dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    }
479d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  }
489d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
499d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  public boolean isOngoing() {
509d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish    return ((realNotification.flags & Notification.FLAG_ONGOING_EVENT) == Notification.FLAG_ONGOING_EVENT);
519d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  }
529d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
539d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  public CharSequence getBigText() {
540dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    if (getApiLevel() >= N) {
550dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish      return realNotification.extras.getCharSequence(Notification.EXTRA_BIG_TEXT);
560dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    } else {
570dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish      return findText(applyBigContentView(), "big_text");
580dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    }
599d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  }
609d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
619d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  public CharSequence getBigContentTitle() {
620dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    if (getApiLevel() >= N) {
630dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish      return realNotification.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
640dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    } else {
650dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish      return findText(applyBigContentView(), "title");
660dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    }
679d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  }
689d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
699d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  public CharSequence getBigContentText() {
700dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    if (getApiLevel() >= N) {
710dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish      return realNotification.extras.getCharSequence(Notification.EXTRA_SUMMARY_TEXT);
720dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    } else {
735263c73e2eadfb9cabdf52d3cfcf1dc7e2832e44James Zhang-Yao Cheng      return findText(applyBigContentView(),  "text");
740dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    }
759d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  }
769d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
77e07896813c99dccd0e27e837fc74b02ef00b7360Jonathan Gerrish  public Bitmap getBigPicture() {
780dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    if (RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N) {
790dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish      return realNotification.extras.getParcelable(Notification.EXTRA_PICTURE);
800dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    } else {
810dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish      ImageView imageView = (ImageView) applyBigContentView().findViewById(getInternalResourceId("big_picture"));
820dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish      return imageView != null && imageView.getDrawable() != null
830dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish          ? ((BitmapDrawable) imageView.getDrawable()).getBitmap() : null;
840dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    }
85e07896813c99dccd0e27e837fc74b02ef00b7360Jonathan Gerrish  }
86e07896813c99dccd0e27e837fc74b02ef00b7360Jonathan Gerrish
879d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  public boolean isWhenShown() {
880dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
890dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish        ? realNotification.extras.getBoolean(Notification.EXTRA_SHOW_WHEN)
900dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish        : findView(applyContentView(), "chronometer").getVisibility() == View.VISIBLE
91719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish        || findView(applyContentView(), "time").getVisibility() == View.VISIBLE;
929d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  }
939d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
94c622c2114bcb7b33045844e19f268c845a9dd750Christian Williams  private ProgressBar getProgressBar_PreN() {
95719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish    return ((ProgressBar) findView(applyContentView(), "progress"));
969d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  }
979d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
980dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish  public boolean isIndeterminate() {
990dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
1000dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish        ? realNotification.extras.getBoolean(Notification.EXTRA_PROGRESS_INDETERMINATE)
101c622c2114bcb7b33045844e19f268c845a9dd750Christian Williams        : getProgressBar_PreN().isIndeterminate();
1020dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish  }
1030dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish
1040dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish  public int getMax() {
1050dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
1060dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish        ? realNotification.extras.getInt(Notification.EXTRA_PROGRESS_MAX)
107c622c2114bcb7b33045844e19f268c845a9dd750Christian Williams        : getProgressBar_PreN().getMax();
1080dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish  }
1090dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish
1100dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish  public int getProgress() {
1110dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
1120dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish        ? realNotification.extras.getInt(Notification.EXTRA_PROGRESS)
113c622c2114bcb7b33045844e19f268c845a9dd750Christian Williams        : getProgressBar_PreN().getProgress();
1140dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish  }
1150dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish
1169d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  public boolean usesChronometer() {
1170dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
1180dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish        ? realNotification.extras.getBoolean(Notification.EXTRA_SHOW_CHRONOMETER)
1190dd2588e6c3b6317dc23eb41c7242bb0638856c6Jonathan Gerrish        : findView(applyContentView(), "chronometer").getVisibility() == View.VISIBLE;
1209d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  }
1219d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
1229d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  private View applyContentView() {
1239d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish    return realNotification.contentView.apply(RuntimeEnvironment.application, new FrameLayout(RuntimeEnvironment.application));
1249d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  }
1259d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish
1269d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  private View applyBigContentView() {
1279d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish    return realNotification.bigContentView.apply(RuntimeEnvironment.application, new FrameLayout(RuntimeEnvironment.application));
1289d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish  }
129719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish
130719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish  private CharSequence findText(View view, String resourceName) {
131719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish    TextView textView = (TextView) findView(view, resourceName);
132719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish    return textView.getText();
133719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish  }
134719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish
135719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish  private View findView(View view, String resourceName) {
136719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish    View subView = view.findViewById(getInternalResourceId(resourceName));
137719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish    if (subView == null) {
138719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish      ByteArrayOutputStream buf = new ByteArrayOutputStream();
139719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish      Shadows.shadowOf(view).dump(new PrintStream(buf), 4);
140719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish      throw new IllegalArgumentException("no id." + resourceName + " found in view:\n" + buf.toString());
141719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish    }
142719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish    return subView;
143719c517e8c0c3b05dcb70e7def70faf70e3ec36bJonathan Gerrish  }
1449d68c9a82b0c22bb716270222ec8174a1da48eafJonathan Gerrish}
145