fix double-launch of SMSInvite activity

Adding a second permission a while back resulted in two Actions being
received and acted on. Needed instead to group the two and receive a
single Action IFF both were granted.
This commit is contained in:
Eric House 2019-01-06 10:58:12 -08:00
parent 5a64b3b92f
commit 5da32d93f7
3 changed files with 50 additions and 19 deletions

View file

@ -535,7 +535,7 @@ public class BoardDelegate extends DelegateBase
finish(); finish();
} }
}; };
alert.setNoDismissListenerNeg( ab, R.string.button_wait, lstnr ); alert.setNoDismissListenerNeg( ab, R.string.button_close, lstnr );
} }
dialog = ab.create(); dialog = ab.create();
@ -1220,9 +1220,9 @@ public class BoardDelegate extends DelegateBase
RequestCode.BT_INVITE_RESULT ); RequestCode.BT_INVITE_RESULT );
break; break;
case SMS: case SMS:
Perms23.tryGetPerms( this, Perm.SEND_SMS, R.string.sms_invite_rationale, Perms23.tryGetPerms( this, new Perm[] { Perm.SEND_SMS,
Action.INVITE_SMS, m_mySIS.nMissing, info ); Perm.RECEIVE_SMS },
Perms23.tryGetPerms( this, Perm.RECEIVE_SMS, R.string.sms_invite_rationale, R.string.sms_invite_rationale,
Action.INVITE_SMS, m_mySIS.nMissing, info ); Action.INVITE_SMS, m_mySIS.nMissing, info );
break; break;
case RELAY: case RELAY:

View file

@ -31,6 +31,7 @@ import android.support.v4.content.ContextCompat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -81,6 +82,12 @@ public class Perms23 {
m_perms.addAll( perms ); m_perms.addAll( perms );
} }
public Builder( Perm[] perms ) {
for ( Perm perm : perms ) {
m_perms.add( perm );
}
}
public Builder( Perm perm ) { public Builder( Perm perm ) {
m_perms.add( perm ); m_perms.add( perm );
} }
@ -129,7 +136,7 @@ public class Perms23 {
if ( haveAll ) { if ( haveAll ) {
if ( null != cbck ) { if ( null != cbck ) {
Map<Perm, Boolean> map = new HashMap<Perm, Boolean>(); Map<Perm, Boolean> map = new HashMap<>();
for ( Perm perm : m_perms ) { for ( Perm perm : m_perms ) {
map.put( perm, true ); map.put( perm, true );
} }
@ -150,36 +157,37 @@ public class Perms23 {
private static class QueryInfo { private static class QueryInfo {
private Action m_action; private Action m_action;
private Perm m_perm; private Perm[] m_perms;
private DelegateBase m_delegate; private DelegateBase m_delegate;
private String m_rationaleMsg; private String m_rationaleMsg;
private Object[] m_params; private Object[] m_params;
private QueryInfo( DelegateBase delegate, Action action, private QueryInfo( DelegateBase delegate, Action action,
Perm perm, String msg, Object[] params ) { Perm[] perms, String msg, Object[] params ) {
m_delegate = delegate; m_delegate = delegate;
m_action = action; m_action = action;
m_perm = perm; m_perms = perms;
m_rationaleMsg = msg; m_rationaleMsg = msg;
m_params = params; m_params = params;
} }
private QueryInfo( DelegateBase delegate, Object[] params ) private QueryInfo( DelegateBase delegate, Object[] params )
{ {
this( delegate, (Action)params[0], (Perm)params[1], (String)params[2], this( delegate, (Action)params[0], (Perm[])params[1], (String)params[2],
(Object[])params[3] ); (Object[])params[3] );
} }
private Object[] getParams() private Object[] getParams()
{ {
return new Object[] { m_action, m_perm, m_rationaleMsg, m_params }; return new Object[] { m_action, m_perms, m_rationaleMsg, m_params };
} }
private void doIt( boolean showRationale ) private void doIt( boolean showRationale )
{ {
Builder builder = new Builder( m_perm ); Builder builder = new Builder( m_perms );
if ( showRationale && null != m_rationaleMsg ) { if ( showRationale && null != m_rationaleMsg ) {
builder.setOnShowRationale( new OnShowRationale() { builder.setOnShowRationale( new OnShowRationale() {
@Override
public void onShouldShowRationale( Set<Perm> perms ) { public void onShouldShowRationale( Set<Perm> perms ) {
m_delegate.makeConfirmThenBuilder( m_rationaleMsg, m_delegate.makeConfirmThenBuilder( m_rationaleMsg,
Action.PERMS_QUERY ) Action.PERMS_QUERY )
@ -195,8 +203,18 @@ public class Perms23 {
@Override @Override
public void onPermissionResult( Map<Perm, Boolean> perms ) { public void onPermissionResult( Map<Perm, Boolean> perms ) {
if ( Action.SKIP_CALLBACK != m_action ) { if ( Action.SKIP_CALLBACK != m_action ) {
Boolean got = perms.get( m_perm ); Set<Perm> keys = perms.keySet();
if ( null != got && got ) {
// We need all the sought perms to have been granted
boolean allGood = keys.size() == m_params.length;
for ( Iterator<Perm> iter = keys.iterator();
allGood && iter.hasNext(); ) {
if ( !perms.get(iter.next()) ) {
allGood = false;
}
}
if ( allGood ) {
m_delegate.onPosButton( m_action, m_params ); m_delegate.onPosButton( m_action, m_params );
} else { } else {
m_delegate.onNegButton( m_action, m_params ); m_delegate.onNegButton( m_action, m_params );
@ -234,23 +252,36 @@ public class Perms23 {
* Request permissions, giving rationale once, then call with action and * Request permissions, giving rationale once, then call with action and
* either positive or negative, the former if permission granted. * either positive or negative, the former if permission granted.
*/ */
public static void tryGetPerms( DelegateBase delegate, Perm perm, int rationaleId, public static void tryGetPerms( DelegateBase delegate, Perm[] perms, int rationaleId,
final Action action, Object... params ) final Action action, Object... params )
{ {
// Log.d( TAG, "tryGetPerms(%s)", perm.toString() ); // Log.d( TAG, "tryGetPerms(%s)", perm.toString() );
Context context = XWApp.getContext(); Context context = XWApp.getContext();
String msg = rationaleId == 0 String msg = rationaleId == 0
? null : LocUtils.getString( context, rationaleId ); ? null : LocUtils.getString( context, rationaleId );
tryGetPerms( delegate, perm, msg, action, params ); tryGetPerms( delegate, perms, msg, action, params );
}
public static void tryGetPerms( DelegateBase delegate, Perm[] perms,
String rationaleMsg, final Action action,
Object... params )
{
// Log.d( TAG, "tryGetPerms(%s)", perm.toString() );
new QueryInfo( delegate, action, perms, rationaleMsg, params )
.doIt( true );
} }
public static void tryGetPerms( DelegateBase delegate, Perm perm, public static void tryGetPerms( DelegateBase delegate, Perm perm,
String rationaleMsg, final Action action, String rationaleMsg, final Action action,
Object... params ) Object... params )
{ {
// Log.d( TAG, "tryGetPerms(%s)", perm.toString() ); tryGetPerms( delegate, new Perm[]{ perm }, rationaleMsg, action, params );
new QueryInfo( delegate, action, perm, rationaleMsg, params ) }
.doIt( true );
public static void tryGetPerms( DelegateBase delegate, Perm perm, int rationaleId,
final Action action, Object... params )
{
tryGetPerms( delegate, new Perm[]{perm}, rationaleId, action, params );
} }
public static void onGotPermsAction( DelegateBase delegate, boolean positive, public static void onGotPermsAction( DelegateBase delegate, boolean positive,

View file

@ -2462,7 +2462,7 @@
<string name="waiting_invite_title">Waiting for response</string> <string name="waiting_invite_title">Waiting for response</string>
<string name="waiting_rematch_title">Rematch in progress</string> <string name="waiting_rematch_title">Rematch in progress</string>
<!-- Button for alert with title above --> <!-- Button for alert with title above -->
<string name="button_wait">Wait</string> <string name="button_close">Close</string>
<string name="button_reinvite">Re-invite</string> <string name="button_reinvite">Re-invite</string>
<string name="invite_stays">(This dialog will stay up until all <string name="invite_stays">(This dialog will stay up until all