1
0
Fork 0
mirror of https://github.com/M66B/NetGuard.git synced 2025-02-23 14:51:06 +00:00

Request phone state permission for international roaming rules

This commit is contained in:
M66B 2015-12-02 10:42:24 +01:00
parent 0b43b3ad28
commit 462860a705
3 changed files with 44 additions and 9 deletions

View file

@ -27,6 +27,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.net.VpnService;
@ -71,6 +72,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
private static final int REQUEST_IAB = 2;
private static final int REQUEST_INVITE = 3;
private static final int REQUEST_LOGCAT = 4;
public static final int REQUEST_ROAMING = 5;
private static final int MIN_SDK = Build.VERSION_CODES.LOLLIPOP;
@ -312,6 +314,13 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_ROAMING)
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
SinkholeService.reload("other", this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
Log.i(TAG, "Preference " + name + "=" + prefs.getAll().get(name));

View file

@ -37,7 +37,6 @@ import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
@ -71,7 +70,8 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
private static final int REQUEST_EXPORT = 1;
private static final int REQUEST_IMPORT = 2;
private static final int REQUEST_READ_PHONE_STATE = 3;
private static final int REQUEST_ROAMING_NATIONAL = 3;
private static final int REQUEST_ROAMING_INTERNATIONAL = 4;
private static final Intent INTENT_VPN_SETTINGS = new Intent("android.net.vpn.SETTINGS");
protected void onCreate(Bundle savedInstanceState) {
@ -193,16 +193,24 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
SinkholeService.reload("wifi", this);
else if ("whitelist_other".equals(name) ||
"screen_other".equals(name) ||
"whitelist_roaming".equals(name))
"screen_other".equals(name))
SinkholeService.reload("other", this);
else if ("national_roaming".equals(name)) {
else if ("whitelist_roaming".equals(name)) {
if (prefs.getBoolean(name, false)) {
if (Util.hasPhoneStatePermission(this))
SinkholeService.reload("other", this);
else
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_ROAMING_INTERNATIONAL);
} else
SinkholeService.reload("other", this);
} else if ("national_roaming".equals(name)) {
if (prefs.getBoolean(name, false)) {
if (Util.hasPhoneStatePermission(this))
SinkholeService.reload("other", this);
else
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_ROAMING_NATIONAL);
} else
SinkholeService.reload("other", this);
@ -216,7 +224,7 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_READ_PHONE_STATE)
if (requestCode == REQUEST_ROAMING_NATIONAL)
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
SinkholeService.reload("other", this);
else {
@ -224,6 +232,15 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
prefs.edit().putBoolean("national_roaming", false).apply();
refreshScreen();
}
else if (requestCode == REQUEST_ROAMING_INTERNATIONAL)
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
SinkholeService.reload("other", this);
else {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("whitelist_roaming", false).apply();
refreshScreen();
}
}
private BroadcastReceiver interactiveStateReceiver = new BroadcastReceiver() {

View file

@ -19,6 +19,9 @@ package eu.faircode.netguard;
Copyright 2015 by Marcel Bokhorst (M66B)
*/
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
@ -26,6 +29,7 @@ import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
@ -52,7 +56,7 @@ import java.util.List;
public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> implements Filterable {
private static final String TAG = "NetGuard.Adapter";
private Context context;
private Activity context;
private boolean telephony;
private boolean debuggable;
private int colorText;
@ -150,7 +154,7 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
}
}
public RuleAdapter(Context context) {
public RuleAdapter(Activity context) {
this.context = context;
this.telephony = Util.hasTelephony(context);
this.debuggable = Util.isDebuggable(context);
@ -354,6 +358,7 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
holder.cbRoaming.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
@TargetApi(Build.VERSION_CODES.M)
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Update rule
updateRoaming(rule, isChecked);
@ -374,6 +379,10 @@ public class RuleAdapter extends RecyclerView.Adapter<RuleAdapter.ViewHolder> im
// Apply updated rule
SinkholeService.reload(null, context);
// Request permissions
if (isChecked && !Util.hasPhoneStatePermission(context))
context.requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, ActivityMain.REQUEST_ROAMING);
}
});