aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/google/android/mobly
diff options
context:
space:
mode:
authorarammelk <arammelk@gmail.com>2017-07-31 18:29:31 -0700
committerAng Li <angli@google.com>2017-07-31 18:29:31 -0700
commit084eb9326917b953900fd6dc61b61d13cf6175b1 (patch)
tree9ed13a541b4f27c16325515f443a1289791d87b7 /src/main/java/com/google/android/mobly
parent4dbbe064143465c11a5bd0e78386bf6f06532934 (diff)
downloadmobly-bundled-snippets-084eb9326917b953900fd6dc61b61d13cf6175b1.tar.gz
SmsSnippet cleanup (#72)
* Cleanups in SmsSnippet * Wrap InterruptedException from polling for SMS sent confirmation event. Now throw SmsException with message that we didn't get the confirmation. * Don't use StringBuilder.
Diffstat (limited to 'src/main/java/com/google/android/mobly')
-rw-r--r--src/main/java/com/google/android/mobly/snippet/bundled/SmsSnippet.java36
1 files changed, 21 insertions, 15 deletions
diff --git a/src/main/java/com/google/android/mobly/snippet/bundled/SmsSnippet.java b/src/main/java/com/google/android/mobly/snippet/bundled/SmsSnippet.java
index 1fdb409..2eb8f38 100644
--- a/src/main/java/com/google/android/mobly/snippet/bundled/SmsSnippet.java
+++ b/src/main/java/com/google/android/mobly/snippet/bundled/SmsSnippet.java
@@ -34,12 +34,8 @@ import com.google.android.mobly.snippet.Snippet;
import com.google.android.mobly.snippet.event.EventCache;
import com.google.android.mobly.snippet.event.SnippetEvent;
import com.google.android.mobly.snippet.rpc.AsyncRpc;
-import com.google.android.mobly.snippet.rpc.JsonBuilder;
import com.google.android.mobly.snippet.rpc.Rpc;
-import org.json.JSONException;
-import org.json.JSONObject;
-
import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
@@ -77,21 +73,18 @@ public class SmsSnippet implements Snippet {
*
* @param phoneNumber A String representing phone number with country code.
* @param message A String representing the message to send.
- * @throws InterruptedException
- * @throws SmsSnippetException
- * @throws JSONException
+ * @throws SmsSnippetException on SMS send error.
*/
@Rpc(description = "Send SMS to a specified phone number.")
public void sendSms(String phoneNumber, String message)
- throws InterruptedException, SmsSnippetException, JSONException {
- String callbackId = new StringBuilder().append(SMS_CALLBACK_ID_PREFIX)
- .append(++mCallbackCounter).toString();
+ throws SmsSnippetException {
+ String callbackId = SMS_CALLBACK_ID_PREFIX + (++mCallbackCounter);
OutboundSmsReceiver receiver = new OutboundSmsReceiver(mContext, callbackId);
if (message.length() > MAX_CHAR_COUNT_PER_SMS) {
ArrayList<String> parts = mSmsManager.divideMessage(message);
ArrayList<PendingIntent> sIntents = new ArrayList<>();
- for (String part : parts) {
+ for (int i = 0; i < parts.size(); i++) {
sIntents.add(PendingIntent.getBroadcast(
mContext, 0, new Intent(SMS_SENT_ACTION), 0));
}
@@ -108,9 +101,16 @@ public class SmsSnippet implements Snippet {
String qId = EventCache.getQueueId(callbackId, SMS_SENT_EVENT_NAME);
LinkedBlockingDeque<SnippetEvent> q = EventCache.getInstance().getEventDeque(qId);
- SnippetEvent result = q.pollFirst(DEFAULT_TIMEOUT_MILLISECOND, TimeUnit.MILLISECONDS);
+ SnippetEvent result;
+
+ try {
+ result = q.pollFirst(DEFAULT_TIMEOUT_MILLISECOND, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ throw new SmsSnippetException("Did not receive SMS sent confirmation event.");
+ }
+
if (result == null) {
- throw new SmsSnippetException("Timed out waiting for SMS sent confirmation.");
+ throw new SmsSnippetException("Timed out waiting for SMS sent confirmation event.");
} else if (result.getData().containsKey("error")) {
throw new SmsSnippetException(
"Failed to send SMS, error code: " + result.getData().getInt("error"));
@@ -127,7 +127,7 @@ public class SmsSnippet implements Snippet {
@Override
public void shutdown() {}
- private class OutboundSmsReceiver extends BroadcastReceiver {
+ private static class OutboundSmsReceiver extends BroadcastReceiver {
private final String mCallbackId;
private Context mContext;
private final EventCache mEventCache;
@@ -169,12 +169,18 @@ public class SmsSnippet implements Snippet {
mEventCache.postEvent(event);
mContext.unregisterReceiver(this);
break;
+ default:
+ event.getData().putBoolean("sent", false);
+ event.getData().putInt("error_code", -1 /* Unknown */);
+ mEventCache.postEvent(event);
+ mContext.unregisterReceiver(this);
+ break;
}
}
}
}
- private class SmsReceiver extends BroadcastReceiver {
+ private static class SmsReceiver extends BroadcastReceiver {
private final String mCallbackId;
private Context mContext;
private final EventCache mEventCache;