From 9c501b19a89d512f2d2aed4f29cc31085f0a4004 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 7 Dec 2018 11:12:49 -0800 Subject: [PATCH] get RECEIVE_SMS permission with SEND_SMS Apparently one of the newer Android SDK levels adds the requirement to have RECEIVE_SMS permission in order for a broadcast receiver to get called. Meaning receipt didn't work even if SEND_SMS had been granted. Since they're both in the same group (for now) the OS will grant the second silently if the first has been granted, but it still has to be requested. So request both at the same time. This still leaves the problem that a user who's never tried to create an SMS game won't have been asked for either permission and so won't receive SMS invitations, but fixing that is for a later release. --- .../eehouse/android/xw4/BoardDelegate.java | 2 ++ .../java/org/eehouse/android/xw4/Perms23.java | 24 ++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java index ba5e64e8f..3269eb9a3 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java @@ -1218,6 +1218,8 @@ public class BoardDelegate extends DelegateBase case SMS: Perms23.tryGetPerms( this, Perm.SEND_SMS, R.string.sms_invite_rationale, Action.INVITE_SMS, m_mySIS.nMissing, info ); + Perms23.tryGetPerms( this, Perm.RECEIVE_SMS, R.string.sms_invite_rationale, + Action.INVITE_SMS, m_mySIS.nMissing, info ); break; case RELAY: RelayInviteDelegate.launchForResult( m_activity, m_mySIS.nMissing, diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Perms23.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Perms23.java index f8b3be7bf..710809192 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Perms23.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Perms23.java @@ -19,6 +19,7 @@ package org.eehouse.android.xw4; +import android.Manifest; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; @@ -43,14 +44,15 @@ public class Perms23 { private static final String TAG = Perms23.class.getSimpleName(); public static enum Perm { - READ_PHONE_STATE("android.permission.READ_PHONE_STATE"), - STORAGE("android.permission.WRITE_EXTERNAL_STORAGE"), - SEND_SMS("android.permission.SEND_SMS"), - READ_CONTACTS("android.permission.READ_CONTACTS") - ; + READ_PHONE_STATE(Manifest.permission.READ_PHONE_STATE), + STORAGE(Manifest.permission.WRITE_EXTERNAL_STORAGE), + SEND_SMS(Manifest.permission.SEND_SMS), + RECEIVE_SMS(Manifest.permission.RECEIVE_SMS), + READ_CONTACTS(Manifest.permission.READ_CONTACTS); private String m_str; private Perm(String str) { m_str = str; } + public String getString() { return m_str; } public static Perm getFor( String str ) { Perm result = null; @@ -261,6 +263,7 @@ public class Perms23 { { // Log.d( TAG, "gotPermissionResult(%s)", perms.toString() ); Map result = new HashMap(); + boolean shouldResend = false; for ( int ii = 0; ii < perms.length; ++ii ) { Perm perm = Perm.getFor( perms[ii] ); boolean granted = PackageManager.PERMISSION_GRANTED == granteds[ii]; @@ -269,9 +272,8 @@ public class Perms23 { // Hack. If SMS has been granted, resend all moves. This should be // replaced with an api allowing listeners to register // Perm-by-Perm, but I'm in a hurry. - if ( granted && perm == Perm.SEND_SMS ) { - GameUtils.resendAllIf( context, CommsConnType.COMMS_CONN_SMS, - true, true ); + if ( granted && (perm == Perm.SEND_SMS || perm == Perm.RECEIVE_SMS) ) { + shouldResend = true; } // Log.d( TAG, "calling %s.onPermissionResult(%s, %b)", @@ -279,6 +281,11 @@ public class Perms23 { // granted ); } + if ( shouldResend ) { + GameUtils.resendAllIf( context, CommsConnType.COMMS_CONN_SMS, + true, true ); + } + PermCbck cbck = s_map.remove( code ); if ( null != cbck ) { cbck.onPermissionResult( result ); @@ -290,6 +297,7 @@ public class Perms23 { String permString = perm.getString(); boolean result = PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission( XWApp.getContext(), permString ); + // Log.d( TAG, "havePermission(%s) => %b", permString, result ); return result; }