From 47a5ab65dfe48a5610ebb9c8828336d03dee72ce Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 18 May 2012 07:55:10 -0700 Subject: [PATCH] use broadcast receiver for status PendingIntents from SendTextMessage, and pass up new MultiService messages on success or failure. In response to these, post SMS status icons on board. Still need to come up with an initial status, notice radio ups/downs, etc. And design icons! --- .../eehouse/android/xw4/BoardActivity.java | 12 ++++ .../org/eehouse/android/xw4/MultiService.java | 5 ++ .../org/eehouse/android/xw4/SMSService.java | 70 ++++++++++++++++++- .../eehouse/android/xw4/jni/JNIThread.java | 8 +++ 4 files changed, 92 insertions(+), 3 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java index 4f6dd4bdd..af1ffbdf9 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java @@ -859,6 +859,18 @@ public class BoardActivity extends XWActivity } ); break; + case SMS_SEND_OK: + case SMS_RECEIVE_OK: + case SMS_SEND_FAILED: + case SMS_SEND_FAILED_NORADIO: + if ( null != m_jniThread ) { + boolean accepted = + MultiService.MultiEvent.SMS_RECEIVE_OK == event + || MultiService.MultiEvent.SMS_SEND_OK == event; + m_jniThread.handle( JNICmd.CMD_DRAW_SMS_STATUS, accepted ); + } + break; + default: super.eventOccurred( event, args ); break; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/MultiService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/MultiService.java index 38aef16f1..1a488dff8 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/MultiService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/MultiService.java @@ -37,6 +37,11 @@ public class MultiService { , MESSAGE_RESEND , MESSAGE_FAILOUT , MESSAGE_DROPPED + + , SMS_RECEIVE_OK + , SMS_SEND_OK + , SMS_SEND_FAILED + , SMS_SEND_FAILED_NORADIO }; public interface BTEventListener { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java index 9c4d79332..369658a8a 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java @@ -20,10 +20,13 @@ package org.eehouse.android.xw4; +import android.app.Activity; import android.app.PendingIntent; import android.app.Service; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.database.Cursor; import android.net.Uri; @@ -52,6 +55,9 @@ public class SMSService extends Service { private static final String INSTALL_URL = "http://eehouse.org/_/aa.htm "; private static final int MAX_SMS_LEN = 140; // ??? differs by network + private static final String MSG_SENT = "MSG_SENT"; + private static final String MSG_DELIVERED = "MSG_DELIVERED"; + private static final int SMS_PROTO_VERSION = 0; private static final int MAX_LEN_TEXT = 100; private static final int HANDLE = 1; @@ -203,6 +209,7 @@ public class SMSService extends Service { public void onCreate() { if ( XWApp.SMSSUPPORTED ) { + registerReceivers(); } else { stopSelf(); } @@ -416,11 +423,11 @@ public class SMSService extends Service { break; case DEATH: gameID = dis.readInt(); - s_srcMgr.sendResult( MultiEvent.MESSAGE_NOGAME, gameID ); + sendResult( MultiEvent.MESSAGE_NOGAME, gameID ); break; case ACK: gameID = dis.readInt(); - s_srcMgr.sendResult( MultiEvent.NEWGAME_SUCCESS, + sendResult( MultiEvent.NEWGAME_SUCCESS, gameID ); break; default: @@ -442,6 +449,8 @@ public class SMSService extends Service { int index = Integer.valueOf( parts[2], 16 ); int count = Integer.valueOf( parts[3], 16 ); tryAssemble( senderPhone, id, index, count, parts[4] ); + + sendResult( MultiEvent.SMS_RECEIVE_OK ); } } @@ -498,17 +507,25 @@ public class SMSService extends Service { } } + private PendingIntent makeStatusIntent( String msg ) + { + Intent intent = new Intent( msg ); + return PendingIntent.getBroadcast( this, 0, intent, 0 ); + } + private boolean sendBuffers( String[] fragments, String phone ) { DbgUtils.logf( "SMSService.sendBuffers()" ); boolean success = false; try { SmsManager mgr = SmsManager.getDefault(); + PendingIntent sent = makeStatusIntent( MSG_SENT ); + PendingIntent delivery = makeStatusIntent( MSG_DELIVERED ); for ( String fragment : fragments ) { DbgUtils.logf( "sending len %d packet: %s", fragment.length(), fragment ); String asPublic = toPublicFmt( fragment ); - mgr.sendTextMessage( phone, null, asPublic, null, null ); + mgr.sendTextMessage( phone, null, asPublic, sent, delivery ); DbgUtils.logf( "Message \"%s\" of %d bytes sent to %s.", asPublic, asPublic.length(), phone ); } @@ -574,6 +591,53 @@ public class SMSService extends Service { } } + private void sendResult( MultiEvent event, Object ... args ) + { + if ( null != s_srcMgr ) { + s_srcMgr.sendResult( event, args ); + } + } + + private void registerReceivers() + { + registerReceiver( new BroadcastReceiver() { + @Override + public void onReceive(Context arg0, Intent arg1) + { + DbgUtils.logf( "got MSG_DELIVERED" ); + switch ( getResultCode() ) { + case Activity.RESULT_OK: + sendResult( MultiEvent.SMS_SEND_OK ); + DbgUtils.logf( "SUCCESS!!!" ); + break; + case SmsManager.RESULT_ERROR_RADIO_OFF: + DbgUtils.showf( SMSService.this, "NO RADIO!!!" ); + sendResult( MultiEvent.SMS_SEND_FAILED_NORADIO ); + break; + case SmsManager.RESULT_ERROR_NO_SERVICE: + DbgUtils.showf( SMSService.this, "NO SERVICE!!!" ); + default: + DbgUtils.logf( "FAILURE!!!" ); + sendResult( MultiEvent.SMS_SEND_FAILED ); + break; + } + } + }, new IntentFilter(MSG_SENT) ); + + registerReceiver( new BroadcastReceiver() { + @Override + public void onReceive(Context arg0, Intent arg1) + { + DbgUtils.logf( "got MSG_DELIVERED" ); + if ( Activity.RESULT_OK == getResultCode() ) { + DbgUtils.logf( "SUCCESS!!!" ); + } else { + DbgUtils.logf( "FAILURE!!!" ); + } + } + }, new IntentFilter(MSG_DELIVERED) ); + } + private class SMSMsgSink extends MultiMsgSink { private Context m_context; public SMSMsgSink( Context context ) { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIThread.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIThread.java index 465e218fc..50c1a9f8f 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIThread.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/JNIThread.java @@ -84,6 +84,7 @@ public class JNIThread extends Thread { CMD_SENDCHAT, CMD_DRAW_CONNS_STATUS, CMD_DRAW_BT_STATUS, + CMD_DRAW_SMS_STATUS, }; public static final int RUNNING = 1; @@ -549,6 +550,13 @@ public class JNIThread extends Thread { draw = true; break; + case CMD_DRAW_SMS_STATUS: + boolean smsWorking = ((Boolean)args[0]).booleanValue(); + m_connsIconID = smsWorking ? R.drawable.sms_allconn + : R.drawable.sms_disabled; + draw = true; + break; + case CMD_TIMER_FIRED: draw = XwJNI.timerFired( m_jniGamePtr, ((Integer)args[0]).intValue(),