aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowNotification.java
blob: 164f5bd3afff57ce5d095f9139ce9859b87fd5a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.N;
import static org.robolectric.RuntimeEnvironment.getApiLevel;
import static org.robolectric.shadows.ResourceHelper.getInternalResourceId;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.shadow.api.Shadow;

@Implements(Notification.class)
@SuppressLint("NewApi")
public class ShadowNotification {

  @RealObject
  Notification realNotification;

  public CharSequence getContentTitle() {
    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
        ? realNotification.extras.getCharSequence(Notification.EXTRA_TITLE)
        : findText(applyContentView(), "title");
  }

  public CharSequence getContentText() {
    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
        ? realNotification.extras.getCharSequence(Notification.EXTRA_TEXT)
        : findText(applyContentView(), "text");
  }

  public CharSequence getContentInfo() {
    if (getApiLevel() >= N) {
      return realNotification.extras.getCharSequence(Notification.EXTRA_INFO_TEXT);
    } else {
      return findText(applyContentView(), "info");
    }
  }

  public boolean isOngoing() {
    return ((realNotification.flags & Notification.FLAG_ONGOING_EVENT) == Notification.FLAG_ONGOING_EVENT);
  }

  public CharSequence getBigText() {
    if (getApiLevel() >= N) {
      return realNotification.extras.getCharSequence(Notification.EXTRA_BIG_TEXT);
    } else {
      return findText(applyBigContentView(), "big_text");
    }
  }

  public CharSequence getBigContentTitle() {
    if (getApiLevel() >= N) {
      return realNotification.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
    } else {
      return findText(applyBigContentView(), "title");
    }
  }

  public CharSequence getBigContentText() {
    if (getApiLevel() >= N) {
      return realNotification.extras.getCharSequence(Notification.EXTRA_SUMMARY_TEXT);
    } else {
      return findText(applyBigContentView(),  "text");
    }
  }

  public Bitmap getBigPicture() {
    if (RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N) {
      return realNotification.extras.getParcelable(Notification.EXTRA_PICTURE);
    } else {
      ImageView imageView =
          (ImageView) applyBigContentView().findViewById(getInternalResourceId("big_picture"));
      return imageView != null && imageView.getDrawable() != null
          ? ((BitmapDrawable) imageView.getDrawable()).getBitmap() : null;
    }
  }

  public boolean isWhenShown() {
    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
        ? realNotification.extras.getBoolean(Notification.EXTRA_SHOW_WHEN)
        : findView(applyContentView(), "chronometer").getVisibility() == View.VISIBLE
        || findView(applyContentView(), "time").getVisibility() == View.VISIBLE;
  }

  private ProgressBar getProgressBar_PreN() {
    return ((ProgressBar) findView(applyContentView(), "progress"));
  }

  public boolean isIndeterminate() {
    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
        ? realNotification.extras.getBoolean(Notification.EXTRA_PROGRESS_INDETERMINATE)
        : getProgressBar_PreN().isIndeterminate();
  }

  public int getMax() {
    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
        ? realNotification.extras.getInt(Notification.EXTRA_PROGRESS_MAX)
        : getProgressBar_PreN().getMax();
  }

  public int getProgress() {
    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
        ? realNotification.extras.getInt(Notification.EXTRA_PROGRESS)
        : getProgressBar_PreN().getProgress();
  }

  public boolean usesChronometer() {
    return RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.N
        ? realNotification.extras.getBoolean(Notification.EXTRA_SHOW_CHRONOMETER)
        : findView(applyContentView(), "chronometer").getVisibility() == View.VISIBLE;
  }

  private View applyContentView() {
    return realNotification.contentView.apply(
        RuntimeEnvironment.getApplication(), new FrameLayout(RuntimeEnvironment.getApplication()));
  }

  private View applyBigContentView() {
    return realNotification.bigContentView.apply(
        RuntimeEnvironment.getApplication(), new FrameLayout(RuntimeEnvironment.getApplication()));
  }

  private CharSequence findText(View view, String resourceName) {
    TextView textView = (TextView) findView(view, resourceName);
    return textView.getText();
  }

  private View findView(View view, String resourceName) {
    View subView = view.findViewById(getInternalResourceId(resourceName));
    if (subView == null) {
      ByteArrayOutputStream buf = new ByteArrayOutputStream();
      ShadowView shadowView = Shadow.extract(view);
      shadowView.dump(new PrintStream(buf), 4);
      throw new IllegalArgumentException(
          "no id." + resourceName + " found in view:\n" + buf.toString());
    }
    return subView;
  }
}