• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4 import static android.os.Build.VERSION_CODES.LOLLIPOP_MR1;
5 
6 import android.app.PendingIntent;
7 import android.telephony.SmsManager;
8 import android.text.TextUtils;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.Map;
12 import org.robolectric.RuntimeEnvironment;
13 import org.robolectric.annotation.Implementation;
14 import org.robolectric.annotation.Implements;
15 import org.robolectric.annotation.Resetter;
16 import org.robolectric.util.ReflectionHelpers;
17 
18 @Implements(value = SmsManager.class, minSdk = JELLY_BEAN_MR2)
19 public class ShadowSmsManager {
20 
21   @Resetter
reset()22   public static void reset() {
23     if (RuntimeEnvironment.getApiLevel() >= LOLLIPOP_MR1) {
24       Map<String, Object> sSubInstances =
25           ReflectionHelpers.getStaticField(SmsManager.class, "sSubInstances");
26       sSubInstances.clear();
27     }
28   }
29 
30   private TextSmsParams lastTextSmsParams;
31   private TextMultipartParams lastTextMultipartParams;
32   private DataMessageParams lastDataParams;
33 
34   @Implementation
sendDataMessage( String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent)35   protected void sendDataMessage(
36       String destinationAddress,
37       String scAddress,
38       short destinationPort,
39       byte[] data,
40       PendingIntent sentIntent,
41       PendingIntent deliveryIntent) {
42     if (TextUtils.isEmpty(destinationAddress)) {
43       throw new IllegalArgumentException("Invalid destinationAddress");
44     }
45 
46     lastDataParams = new DataMessageParams(destinationAddress, scAddress, destinationPort, data, sentIntent, deliveryIntent);
47   }
48 
49   @Implementation
sendTextMessage( String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)50   protected void sendTextMessage(
51       String destinationAddress,
52       String scAddress,
53       String text,
54       PendingIntent sentIntent,
55       PendingIntent deliveryIntent) {
56     if (TextUtils.isEmpty(destinationAddress)) {
57       throw new IllegalArgumentException("Invalid destinationAddress");
58     }
59 
60     if (TextUtils.isEmpty(text)) {
61       throw new IllegalArgumentException("Invalid message body");
62     }
63 
64     lastTextSmsParams = new TextSmsParams(destinationAddress, scAddress, text, sentIntent, deliveryIntent);
65   }
66 
67   @Implementation
sendMultipartTextMessage( String destinationAddress, String scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents)68   protected void sendMultipartTextMessage(
69       String destinationAddress,
70       String scAddress,
71       ArrayList<String> parts,
72       ArrayList<PendingIntent> sentIntents,
73       ArrayList<PendingIntent> deliveryIntents) {
74     if (TextUtils.isEmpty(destinationAddress)) {
75       throw new IllegalArgumentException("Invalid destinationAddress");
76     }
77 
78     if (parts == null) {
79       throw new IllegalArgumentException("Invalid message parts");
80     }
81 
82     lastTextMultipartParams = new TextMultipartParams(destinationAddress, scAddress, parts, sentIntents, deliveryIntents);
83   }
84 
85   /**
86    * @return Parameters for last call to {@code sendDataMessage}.
87    */
getLastSentDataMessageParams()88   public DataMessageParams getLastSentDataMessageParams() {
89     return lastDataParams;
90   }
91 
92   /**
93    * Clear last recorded parameters for {@code sendDataMessage}.
94    */
clearLastSentDataMessageParams()95   public void clearLastSentDataMessageParams() {
96     lastDataParams = null;
97   }
98 
99   /**
100    * @return Parameters for last call to {@code sendTextMessage}.
101    */
getLastSentTextMessageParams()102   public TextSmsParams getLastSentTextMessageParams() {
103     return lastTextSmsParams;
104   }
105 
106   /**
107    * Clear last recorded parameters for {@code sendTextMessage}.
108    */
clearLastSentTextMessageParams()109   public void clearLastSentTextMessageParams() {
110     lastTextSmsParams = null;
111   }
112 
113   /**
114    * @return Parameters for last call to {@code sendMultipartTextMessage}.
115    */
getLastSentMultipartTextMessageParams()116   public TextMultipartParams getLastSentMultipartTextMessageParams() {
117     return lastTextMultipartParams;
118   }
119 
120   /**
121    * Clear last recorded parameters for {@code sendMultipartTextMessage}.
122    */
clearLastSentMultipartTextMessageParams()123   public void clearLastSentMultipartTextMessageParams() {
124     lastTextMultipartParams = null;
125   }
126 
127   public static class DataMessageParams {
128     private final String destinationAddress;
129     private final String scAddress;
130     private final short destinationPort;
131     private final byte[] data;
132     private final PendingIntent sentIntent;
133     private final PendingIntent deliveryIntent;
134 
DataMessageParams(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent)135     public DataMessageParams(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent) {
136       this.destinationAddress = destinationAddress;
137       this.scAddress = scAddress;
138       this.destinationPort = destinationPort;
139       this.data = data;
140       this.sentIntent = sentIntent;
141       this.deliveryIntent = deliveryIntent;
142     }
143 
getDestinationAddress()144     public String getDestinationAddress() {
145       return destinationAddress;
146     }
147 
getScAddress()148     public String getScAddress() {
149       return scAddress;
150     }
151 
getDestinationPort()152     public short getDestinationPort() {
153       return destinationPort;
154     }
155 
getData()156     public byte[] getData() {
157       return data;
158     }
159 
getSentIntent()160     public PendingIntent getSentIntent() {
161       return sentIntent;
162     }
163 
getDeliveryIntent()164     public PendingIntent getDeliveryIntent() {
165       return deliveryIntent;
166     }
167   }
168 
169   public static class TextSmsParams {
170     private final String destinationAddress;
171     private final String scAddress;
172     private final String text;
173     private final PendingIntent sentIntent;
174     private final PendingIntent deliveryIntent;
175 
TextSmsParams(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)176     public TextSmsParams(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent) {
177       this.destinationAddress = destinationAddress;
178       this.scAddress = scAddress;
179       this.text = text;
180       this.sentIntent = sentIntent;
181       this.deliveryIntent = deliveryIntent;
182     }
183 
getDestinationAddress()184     public String getDestinationAddress() {
185       return destinationAddress;
186     }
187 
getScAddress()188     public String getScAddress() {
189       return scAddress;
190     }
191 
getText()192     public String getText() {
193       return text;
194     }
195 
getSentIntent()196     public PendingIntent getSentIntent() {
197       return sentIntent;
198     }
199 
getDeliveryIntent()200     public PendingIntent getDeliveryIntent() {
201       return deliveryIntent;
202     }
203   }
204 
205   public static class TextMultipartParams {
206     private final String destinationAddress;
207     private final String scAddress;
208     private final ArrayList<String> parts;
209     private final ArrayList<PendingIntent> sentIntents;
210     private final ArrayList<PendingIntent> deliveryIntents;
211 
TextMultipartParams(String destinationAddress, String scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents)212     public TextMultipartParams(String destinationAddress, String scAddress, ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents) {
213       this.destinationAddress = destinationAddress;
214       this.scAddress = scAddress;
215       this.parts = parts;
216       this.sentIntents = sentIntents;
217       this.deliveryIntents = deliveryIntents;
218     }
219 
getDestinationAddress()220     public String getDestinationAddress() {
221       return destinationAddress;
222     }
223 
getScAddress()224     public String getScAddress() {
225       return scAddress;
226     }
227 
getParts()228     public List<String> getParts() {
229       return parts;
230     }
231 
getSentIntents()232     public List<android.app.PendingIntent> getSentIntents() {
233       return sentIntents;
234     }
235 
getDeliveryIntents()236     public List<android.app.PendingIntent> getDeliveryIntents() {
237       return deliveryIntents;
238     }
239   }
240 }
241