From aec70782f44fe046ef46fb18e8b4265d4a8b6203 Mon Sep 17 00:00:00 2001 From: Eric House Date: Thu, 22 Jan 2015 07:37:06 -0800 Subject: [PATCH] tweaks to get game initialization working, including not looking on startup for incoming text messages if on KITKAT or above. --- .../eehouse/android/xw4/NetLaunchInfo.java | 25 ++++----- .../org/eehouse/android/xw4/SMSService.java | 56 +++++++++++++++---- .../eehouse/android/xw4/jni/CommsAddrRec.java | 14 +++-- 3 files changed, 63 insertions(+), 32 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetLaunchInfo.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetLaunchInfo.java index e54e7c3e4..9adca1699 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetLaunchInfo.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NetLaunchInfo.java @@ -25,7 +25,6 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; -import android.telephony.TelephonyManager; import java.io.InputStream; import java.util.Iterator; import org.json.JSONException; @@ -100,17 +99,17 @@ public class NetLaunchInfo { for ( CommsConnType typ : m_addrs.getTypes() ) { switch ( typ ) { case COMMS_CONN_BT: - btAddress = json.optString( MultiService.BT_ADDRESS ); - btName = json.optString( MultiService.BT_NAME ); + btAddress = json.getString( MultiService.BT_ADDRESS ); + btName = json.getString( MultiService.BT_NAME ); break; case COMMS_CONN_RELAY: - room = json.optString( MultiService.ROOM ); - m_inviteID = json.optString( MultiService.INVITEID ); + room = json.getString( MultiService.ROOM ); + m_inviteID = json.getString( MultiService.INVITEID ); break; case COMMS_CONN_SMS: - phone = json.optString( PHONE_KEY ); - isGSM = json.optBoolean( GSM_KEY, false ); - osVers = json.optInt( OSVERS_KEY ); + phone = json.getString( PHONE_KEY ); + isGSM = json.getBoolean( GSM_KEY ); + osVers = json.getInt( OSVERS_KEY ); break; default: DbgUtils.logf( "Unexpected typ %s", typ.toString() ); @@ -418,14 +417,10 @@ public class NetLaunchInfo { public void addSMSInfo( Context context ) { - // look up own phone number, which will require new permission - TelephonyManager mgr = (TelephonyManager) - context.getSystemService(Context.TELEPHONY_SERVICE); - phone = mgr.getLine1Number(); - DbgUtils.logf( "addSMSInfo(): got phone: %s", phone ); + SMSService.SMSPhoneInfo pi = SMSService.getPhoneInfo( context ); + phone = pi.number; + isGSM = pi.isGSM; - int type = mgr.getPhoneType(); - isGSM = TelephonyManager.PHONE_TYPE_GSM == type; osVers = Integer.valueOf( android.os.Build.VERSION.SDK ); m_addrs.add( CommsConnType.COMMS_CONN_SMS ); 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 93deb4ad3..c96fd9c3a 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java @@ -20,6 +20,7 @@ package org.eehouse.android.xw4; +import android.telephony.TelephonyManager; import android.app.Activity; import android.app.PendingIntent; import android.app.Service; @@ -65,6 +66,7 @@ public class SMSService extends XWService { private static final String INSTALL_URL = "http://eehouse.org/_/a.py/a "; private static final int MAX_SMS_LEN = 140; // ??? differs by network + private static final int KITKAT = 19; private static final String MSG_SENT = "MSG_SENT"; private static final String MSG_DELIVERED = "MSG_DELIVERED"; @@ -108,18 +110,42 @@ public class SMSService extends XWService { = new HashMap>(); private static Set s_sentDied = new HashSet(); + public static class SMSPhoneInfo { + public SMSPhoneInfo( String num, boolean gsm ) { + number = num; + isGSM = gsm; + } + public String number; + public boolean isGSM; + } + + public static SMSPhoneInfo getPhoneInfo( Context context ) + { + TelephonyManager mgr = (TelephonyManager) + context.getSystemService(Context.TELEPHONY_SERVICE); + String number = mgr.getLine1Number(); + + int type = mgr.getPhoneType(); + boolean isGSM = TelephonyManager.PHONE_TYPE_GSM == type; + return new SMSPhoneInfo( number, isGSM ); + } + public static void smsToastEnable( boolean newVal ) { s_showToasts = newVal; } - public static void registerPhone( Context context, String phone, boolean isGSM ) + public static void registerPhone( Context context, String phone, + boolean isGSM ) { + DbgUtils.logf( "SMSService.registerPhone(%s, isGSM=%b)", phone, isGSM ); + Assert.assertTrue( isGSM ); Map phoneRecs = getPhoneRecs( context ); phone = matchKeyIf( phoneRecs, phone ); Boolean val = phoneRecs.get( phone ); if ( null == val || val != isGSM ) { - DbgUtils.logf( "SMSService.registerPhone: making rec for %s", phone ); + DbgUtils.logf( "SMSService.registerPhone: making rec for %s (isGSM=%b)", + phone, isGSM ); val = new Boolean( isGSM ); phoneRecs.put( phone, val ); saveRecs( context, phoneRecs ); @@ -135,7 +161,9 @@ public class SMSService extends XWService { public static void checkForInvites( Context context ) { - if ( XWApp.SMSSUPPORTED && Utils.deviceSupportsSMS( context ) ) { + if ( XWApp.SMSSUPPORTED && Utils.deviceSupportsSMS( context ) + // Earlier than kitkat... + && KITKAT > Integer.valueOf( android.os.Build.VERSION.SDK ) ) { Intent intent = getIntentTo( context, CHECK_MSGDB ); context.startService( intent ); } @@ -473,7 +501,7 @@ public class SMSService extends XWService { if ( null == result ) { DbgUtils.logf( "getPhoneDoesData: no record for phone %s", phone ); } - DbgUtils.logf( "getPhoneDoesData(%s) => %b", phone, result ); + DbgUtils.logf( "getPhoneDoesData(%s) => %H/%b", phone, result, result ); return result; } @@ -862,7 +890,6 @@ public class SMSService extends XWService { private static void saveRecs( Context context, Map recs ) { - DbgUtils.logf( "SMSService.saveRecs()" ); Assert.assertNotNull( recs ); ByteArrayOutputStream bas = new ByteArrayOutputStream(); try { @@ -977,16 +1004,21 @@ public class SMSService extends XWService { return success; } - private static String matchKeyIf( Map map, String phone ) + private static String matchKeyIf( Map map, final String phone ) { - for ( Iterator iter = map.keySet().iterator(); iter.hasNext(); ) { - String key = iter.next(); - if ( PhoneNumberUtils.compare( key, phone ) ) { - phone = key; - break; + String result = phone; + Set keys = map.keySet(); + if ( ! keys.contains( result ) ) { + for ( Iterator iter = keys.iterator(); iter.hasNext(); ) { + String key = iter.next(); + if ( PhoneNumberUtils.compare( key, phone ) ) { + result = key; + break; + } } } - return phone; + DbgUtils.logf( "matchKeyIf(%s) => %s", phone, result ); + return result; } private class SMSMsgSink extends MultiMsgSink { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/CommsAddrRec.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/CommsAddrRec.java index 0ec519bf7..475fe4895 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/CommsAddrRec.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/CommsAddrRec.java @@ -28,13 +28,14 @@ import java.util.Iterator; import junit.framework.Assert; -import org.eehouse.android.xw4.Utils; -import org.eehouse.android.xw4.R; -import org.eehouse.android.xw4.loc.LocUtils; +import org.eehouse.android.xw4.BTService; import org.eehouse.android.xw4.DbgUtils; import org.eehouse.android.xw4.GameUtils; +import org.eehouse.android.xw4.R; +import org.eehouse.android.xw4.SMSService; +import org.eehouse.android.xw4.Utils; import org.eehouse.android.xw4.XWPrefs; -import org.eehouse.android.xw4.BTService; +import org.eehouse.android.xw4.loc.LocUtils; public class CommsAddrRec { @@ -204,7 +205,10 @@ public class CommsAddrRec { } break; case COMMS_CONN_SMS: - // FIXME + SMSService.SMSPhoneInfo pi = SMSService.getPhoneInfo( context ); + sms_phone = pi.number; + sms_port = 3; // fix comms already... + break; default: Assert.fail(); }