mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-09 05:24:44 +01:00
check if permission in manifest
I got bitten when creating a new variant leaving the permission out of the manifest but using a config flag to claim it was there. Better to have just one place to configure that. So check for presence in the manifest and, for now, assert that that matches the isBanned flag. Soon the isBanned flag can be removed.
This commit is contained in:
parent
99eafd9c60
commit
62c0292045
6 changed files with 64 additions and 23 deletions
|
@ -1211,7 +1211,7 @@ public class BoardDelegate extends DelegateBase
|
||||||
SentInvitesInfo info = (SentInvitesInfo)params[1];
|
SentInvitesInfo info = (SentInvitesInfo)params[1];
|
||||||
launchPhoneNumberInvite( nMissing, info, InviteMeans.SMS_DATA,
|
launchPhoneNumberInvite( nMissing, info, InviteMeans.SMS_DATA,
|
||||||
RequestCode.SMS_DATA_INVITE_RESULT );
|
RequestCode.SMS_DATA_INVITE_RESULT );
|
||||||
} else if ( Perms23.anyBanned( perms ) ) {
|
} else if ( Perms23.anyBanned( m_activity, perms ) ) {
|
||||||
makeOkOnlyBuilder( R.string.sms_banned_ok_only )
|
makeOkOnlyBuilder( R.string.sms_banned_ok_only )
|
||||||
.setActionPair(new ActionPair( Action.PERMS_BANNED_INFO,
|
.setActionPair(new ActionPair( Action.PERMS_BANNED_INFO,
|
||||||
R.string.button_more_info ) )
|
R.string.button_more_info ) )
|
||||||
|
@ -2309,7 +2309,8 @@ public class BoardDelegate extends DelegateBase
|
||||||
} else {
|
} else {
|
||||||
// Make sure these can be treated the same!!!
|
// Make sure these can be treated the same!!!
|
||||||
Assert.assertTrue( nbsPerms.length == 2 &&
|
Assert.assertTrue( nbsPerms.length == 2 &&
|
||||||
nbsPerms[0].isBanned() == nbsPerms[1].isBanned() );
|
nbsPerms[0].isBanned(m_activity)
|
||||||
|
== nbsPerms[1].isBanned(m_activity) );
|
||||||
|
|
||||||
m_permCbck = new Perms23.PermCbck() {
|
m_permCbck = new Perms23.PermCbck() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -2320,7 +2321,7 @@ public class BoardDelegate extends DelegateBase
|
||||||
// Yay! nothing to do
|
// Yay! nothing to do
|
||||||
alertOrderIncrIfAt( thisOrder );
|
alertOrderIncrIfAt( thisOrder );
|
||||||
} else {
|
} else {
|
||||||
boolean banned = Perm.SEND_SMS.isBanned();
|
boolean banned = Perm.SEND_SMS.isBanned(m_activity);
|
||||||
int explID = banned
|
int explID = banned
|
||||||
? R.string.banned_nbs_perms : R.string.missing_sms_perms;
|
? R.string.banned_nbs_perms : R.string.missing_sms_perms;
|
||||||
DlgDelegate.ConfirmThenBuilder builder =
|
DlgDelegate.ConfirmThenBuilder builder =
|
||||||
|
|
|
@ -151,7 +151,7 @@ public class ConnViaViewLayout extends LinearLayout {
|
||||||
if ( Perms23.havePermissions( getContext(),
|
if ( Perms23.havePermissions( getContext(),
|
||||||
Perms23.Perm.SEND_SMS,
|
Perms23.Perm.SEND_SMS,
|
||||||
Perms23.Perm.RECEIVE_SMS )
|
Perms23.Perm.RECEIVE_SMS )
|
||||||
|| !Perms23.Perm.SEND_SMS.isBanned() ) {
|
|| !Perms23.Perm.SEND_SMS.isBanned(getContext()) ) {
|
||||||
msgID = R.string.not_again_comms_sms;
|
msgID = R.string.not_again_comms_sms;
|
||||||
keyID = R.string.key_na_comms_sms;
|
keyID = R.string.key_na_comms_sms;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -995,7 +995,7 @@ public class GamesListDelegate extends ListDelegateBase
|
||||||
// asking (OS will grant without user interaction) since they're in
|
// asking (OS will grant without user interaction) since they're in
|
||||||
// the same group. So just do it now. This code can be removed
|
// the same group. So just do it now. This code can be removed
|
||||||
// later...
|
// later...
|
||||||
if ( !Perm.RECEIVE_SMS.isBanned() ) {
|
if ( !Perm.RECEIVE_SMS.isBanned(m_activity) ) {
|
||||||
if ( Perms23.havePermissions( m_activity, Perm.SEND_SMS ) ) {
|
if ( Perms23.havePermissions( m_activity, Perm.SEND_SMS ) ) {
|
||||||
Perms23.tryGetPerms( this, Perm.RECEIVE_SMS, 0, Action.SKIP_CALLBACK );
|
Perms23.tryGetPerms( this, Perm.RECEIVE_SMS, 0, Action.SKIP_CALLBACK );
|
||||||
}
|
}
|
||||||
|
@ -1067,7 +1067,7 @@ public class GamesListDelegate extends ListDelegateBase
|
||||||
private void warnSMSBannedIf()
|
private void warnSMSBannedIf()
|
||||||
{
|
{
|
||||||
if ( !Perms23.havePermissions( m_activity, Perm.SEND_SMS, Perm.RECEIVE_SMS )
|
if ( !Perms23.havePermissions( m_activity, Perm.SEND_SMS, Perm.RECEIVE_SMS )
|
||||||
&& Perm.SEND_SMS.isBanned() ) {
|
&& Perm.SEND_SMS.isBanned(m_activity) ) {
|
||||||
int smsGameCount = DBUtils.countOpenGamesUsingNBS( m_activity );
|
int smsGameCount = DBUtils.countOpenGamesUsingNBS( m_activity );
|
||||||
if ( 0 < smsGameCount ) {
|
if ( 0 < smsGameCount ) {
|
||||||
String msg = LocUtils.getString( m_activity,
|
String msg = LocUtils.getString( m_activity,
|
||||||
|
|
|
@ -117,7 +117,7 @@ public class InviteChoicesAlert extends DlgDelegateAlert {
|
||||||
break;
|
break;
|
||||||
case SMS_DATA:
|
case SMS_DATA:
|
||||||
if ( !Perms23.havePermissions( activity, Perm.SEND_SMS, Perm.RECEIVE_SMS )
|
if ( !Perms23.havePermissions( activity, Perm.SEND_SMS, Perm.RECEIVE_SMS )
|
||||||
&& Perm.SEND_SMS.isBanned() ) {
|
&& Perm.SEND_SMS.isBanned(activity) ) {
|
||||||
activity
|
activity
|
||||||
.makeOkOnlyBuilder( R.string.sms_banned_ok_only )
|
.makeOkOnlyBuilder( R.string.sms_banned_ok_only )
|
||||||
.setActionPair(new ActionPair( Action.PERMS_BANNED_INFO,
|
.setActionPair(new ActionPair( Action.PERMS_BANNED_INFO,
|
||||||
|
|
|
@ -23,7 +23,9 @@ import android.Manifest;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.pm.PermissionInfo;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.support.v4.app.ActivityCompat;
|
import android.support.v4.app.ActivityCompat;
|
||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
|
@ -32,6 +34,7 @@ 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.Iterator;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -56,13 +59,21 @@ public class Perms23 {
|
||||||
private String m_str;
|
private String m_str;
|
||||||
private boolean m_banned;
|
private boolean m_banned;
|
||||||
private Perm(String str) { this(str, false); }
|
private Perm(String str) { this(str, false); }
|
||||||
private Perm(String str, boolean banned) {
|
private Perm( String str, boolean banned ) {
|
||||||
m_str = str;
|
m_str = str;
|
||||||
m_banned = banned;
|
m_banned = banned;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getString() { return m_str; }
|
public String getString() { return m_str; }
|
||||||
public boolean isBanned() { return m_banned; }
|
public boolean isBanned( Context context )
|
||||||
|
{
|
||||||
|
// PENDING... Once this has been here for a week or so, remove
|
||||||
|
// SMS_BANNED. That way absence of the permission from a variant's
|
||||||
|
// manifest is the only way being banned is expressed. It sucks
|
||||||
|
// keeping two things in sync.
|
||||||
|
Assert.assertFalse( m_banned == permInManifest( context, this ) );
|
||||||
|
return m_banned;
|
||||||
|
}
|
||||||
public static Perm getFor( String str ) {
|
public static Perm getFor( String str ) {
|
||||||
Perm result = null;
|
Perm result = null;
|
||||||
for ( Perm one : Perm.values() ) {
|
for ( Perm one : Perm.values() ) {
|
||||||
|
@ -75,6 +86,35 @@ public class Perms23 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Map<Perm, Boolean> sManifestMap = new HashMap<>();
|
||||||
|
private static boolean permInManifest( Context context, Perm perm )
|
||||||
|
{
|
||||||
|
boolean result = false;
|
||||||
|
if ( sManifestMap.containsKey( perm ) ) {
|
||||||
|
result = sManifestMap.get( perm );
|
||||||
|
} else {
|
||||||
|
PackageManager pm = context.getPackageManager();
|
||||||
|
try {
|
||||||
|
String[] pis = pm
|
||||||
|
.getPackageInfo( BuildConfig.APPLICATION_ID,
|
||||||
|
PackageManager.GET_PERMISSIONS )
|
||||||
|
.requestedPermissions;
|
||||||
|
if ( pis == null ) {
|
||||||
|
Assert.assertFalse( BuildConfig.DEBUG );
|
||||||
|
} else {
|
||||||
|
String manifestName = perm.getString();
|
||||||
|
for ( int ii = 0; !result && ii < pis.length; ++ii ) {
|
||||||
|
result = pis[ii].equals( manifestName );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch( PackageManager.NameNotFoundException nnfe ) {
|
||||||
|
Log.e(TAG, "permInManifest() nnfe: %s", nnfe.getMessage());
|
||||||
|
}
|
||||||
|
sManifestMap.put( perm, result );
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public interface PermCbck {
|
public interface PermCbck {
|
||||||
void onPermissionResult( boolean allGood, Map<Perm, Boolean> perms );
|
void onPermissionResult( boolean allGood, Map<Perm, Boolean> perms );
|
||||||
}
|
}
|
||||||
|
@ -83,7 +123,7 @@ public class Perms23 {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Builder {
|
public static class Builder {
|
||||||
private Set<Perm> m_perms = new HashSet<Perm>();
|
private Set<Perm> m_perms = new HashSet<>();
|
||||||
private OnShowRationale m_onShow;
|
private OnShowRationale m_onShow;
|
||||||
|
|
||||||
public Builder(Set<Perm> perms) {
|
public Builder(Set<Perm> perms) {
|
||||||
|
@ -117,18 +157,18 @@ public class Perms23 {
|
||||||
Log.d( TAG, "asyncQuery(%s)", m_perms );
|
Log.d( TAG, "asyncQuery(%s)", m_perms );
|
||||||
boolean haveAll = true;
|
boolean haveAll = true;
|
||||||
boolean shouldShow = false;
|
boolean shouldShow = false;
|
||||||
Set<Perm> needShow = new HashSet<Perm>();
|
Set<Perm> needShow = new HashSet<>();
|
||||||
|
|
||||||
ArrayList<String> askStrings = new ArrayList<String>();
|
List<String> askStrings = new ArrayList<>();
|
||||||
for ( Perm perm : m_perms ) {
|
for ( Perm perm : m_perms ) {
|
||||||
String permStr = perm.getString();
|
String permStr = perm.getString();
|
||||||
boolean haveIt = perm.isBanned() || PackageManager.PERMISSION_GRANTED
|
boolean haveIt = perm.isBanned(activity) || PackageManager.PERMISSION_GRANTED
|
||||||
== ContextCompat.checkSelfPermission( activity, permStr );
|
== ContextCompat.checkSelfPermission( activity, permStr );
|
||||||
|
|
||||||
if ( !haveIt ) {
|
if ( !haveIt ) {
|
||||||
// do not pass banned perms to the OS! They're not in
|
// do not pass banned perms to the OS! They're not in
|
||||||
// AndroidManifest.xml so may crash on some devices
|
// AndroidManifest.xml so may crash on some devices
|
||||||
Assert.assertFalse( perm.isBanned() );
|
Assert.assertFalse( perm.isBanned(activity) );
|
||||||
askStrings.add( permStr );
|
askStrings.add( permStr );
|
||||||
|
|
||||||
if ( null != m_onShow && ActivityCompat
|
if ( null != m_onShow && ActivityCompat
|
||||||
|
@ -146,7 +186,7 @@ public class Perms23 {
|
||||||
Map<Perm, Boolean> map = new HashMap<>();
|
Map<Perm, Boolean> map = new HashMap<>();
|
||||||
boolean allGood = true;
|
boolean allGood = true;
|
||||||
for ( Perm perm : m_perms ) {
|
for ( Perm perm : m_perms ) {
|
||||||
boolean banned = perm.isBanned();
|
boolean banned = perm.isBanned(activity);
|
||||||
map.put( perm, !banned );
|
map.put( perm, !banned );
|
||||||
allGood = allGood & !banned;
|
allGood = allGood & !banned;
|
||||||
}
|
}
|
||||||
|
@ -197,7 +237,7 @@ public class Perms23 {
|
||||||
Set<Perm> validPerms = new HashSet<>();
|
Set<Perm> validPerms = new HashSet<>();
|
||||||
Set<Perm> bannedPerms = new HashSet<>();
|
Set<Perm> bannedPerms = new HashSet<>();
|
||||||
for ( Perm perm : m_perms ) {
|
for ( Perm perm : m_perms ) {
|
||||||
if ( perm.isBanned() ) {
|
if ( perm.isBanned(m_delegate.getActivity()) ) {
|
||||||
bannedPerms.add( perm );
|
bannedPerms.add( perm );
|
||||||
} else {
|
} else {
|
||||||
validPerms.add( perm );
|
validPerms.add( perm );
|
||||||
|
@ -323,7 +363,7 @@ public class Perms23 {
|
||||||
info.handleButton( positive );
|
info.handleButton( positive );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Map<Integer, PermCbck> s_map = new HashMap<Integer, PermCbck>();
|
private static Map<Integer, PermCbck> s_map = new HashMap<>();
|
||||||
public static void gotPermissionResult( Context context, int code,
|
public static void gotPermissionResult( Context context, int code,
|
||||||
String[] perms, int[] granteds )
|
String[] perms, int[] granteds )
|
||||||
{
|
{
|
||||||
|
@ -333,7 +373,7 @@ public class Perms23 {
|
||||||
boolean allGood = true;
|
boolean allGood = true;
|
||||||
for ( int ii = 0; ii < perms.length; ++ii ) {
|
for ( int ii = 0; ii < perms.length; ++ii ) {
|
||||||
Perm perm = Perm.getFor( perms[ii] );
|
Perm perm = Perm.getFor( perms[ii] );
|
||||||
Assert.assertTrue( !perm.isBanned() || ! BuildConfig.DEBUG );
|
Assert.assertTrue( !perm.isBanned(context) || ! BuildConfig.DEBUG );
|
||||||
boolean granted = PackageManager.PERMISSION_GRANTED == granteds[ii];
|
boolean granted = PackageManager.PERMISSION_GRANTED == granteds[ii];
|
||||||
allGood = allGood && granted;
|
allGood = allGood && granted;
|
||||||
result.put( perm, granted );
|
result.put( perm, granted );
|
||||||
|
@ -367,7 +407,7 @@ public class Perms23 {
|
||||||
for ( int ii = 0; result && ii < perms.length; ++ii ) {
|
for ( int ii = 0; result && ii < perms.length; ++ii ) {
|
||||||
Perm perm = perms[ii];
|
Perm perm = perms[ii];
|
||||||
boolean thisResult;
|
boolean thisResult;
|
||||||
if ( perm.isBanned() ) {
|
if ( perm.isBanned(context) ) {
|
||||||
thisResult = bannedWithWorkaround( context, perm );
|
thisResult = bannedWithWorkaround( context, perm );
|
||||||
} else {
|
} else {
|
||||||
thisResult = PackageManager.PERMISSION_GRANTED
|
thisResult = PackageManager.PERMISSION_GRANTED
|
||||||
|
@ -379,11 +419,11 @@ public class Perms23 {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean anyBanned( Perms23.Perm... perms )
|
static boolean anyBanned( Context context, Perms23.Perm... perms )
|
||||||
{
|
{
|
||||||
boolean anyBanned = false;
|
boolean anyBanned = false;
|
||||||
for ( int ii = 0; !anyBanned && ii < perms.length; ++ii ) {
|
for ( int ii = 0; !anyBanned && ii < perms.length; ++ii ) {
|
||||||
anyBanned = perms[ii].isBanned();
|
anyBanned = perms[ii].isBanned( context );
|
||||||
}
|
}
|
||||||
return anyBanned;
|
return anyBanned;
|
||||||
}
|
}
|
||||||
|
@ -393,7 +433,7 @@ public class Perms23 {
|
||||||
boolean allBanned = true;
|
boolean allBanned = true;
|
||||||
boolean workaroundKnown = true;
|
boolean workaroundKnown = true;
|
||||||
for ( Perms23.Perm perm : perms ) {
|
for ( Perms23.Perm perm : perms ) {
|
||||||
allBanned = allBanned && perm.isBanned();
|
allBanned = allBanned && perm.isBanned(context);
|
||||||
|
|
||||||
switch ( perm ) {
|
switch ( perm ) {
|
||||||
case SEND_SMS:
|
case SEND_SMS:
|
||||||
|
|
|
@ -516,7 +516,7 @@ public class SMSService extends XWJIService {
|
||||||
short nbsPort = getNBSPort();
|
short nbsPort = getNBSPort();
|
||||||
try {
|
try {
|
||||||
SmsManager mgr = SmsManager.getDefault();
|
SmsManager mgr = SmsManager.getDefault();
|
||||||
boolean useProxy = Perms23.Perm.SEND_SMS.isBanned()
|
boolean useProxy = Perms23.Perm.SEND_SMS.isBanned( this )
|
||||||
&& NBSProxy.isInstalled( this );
|
&& NBSProxy.isInstalled( this );
|
||||||
PendingIntent sent = useProxy ? null : makeStatusIntent( MSG_SENT );
|
PendingIntent sent = useProxy ? null : makeStatusIntent( MSG_SENT );
|
||||||
PendingIntent delivery = useProxy ? null : makeStatusIntent( MSG_DELIVERED );
|
PendingIntent delivery = useProxy ? null : makeStatusIntent( MSG_DELIVERED );
|
||||||
|
|
Loading…
Reference in a new issue