Auto enable again (draft)

This commit is contained in:
M66B 2015-11-06 23:49:03 +01:00
parent c8950331a1
commit 5ed4728a24
7 changed files with 183 additions and 49 deletions

View File

@ -19,6 +19,7 @@ package eu.faircode.netguard;
Copyright 2015 by Marcel Bokhorst (M66B)
*/
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
@ -37,6 +38,7 @@ import android.net.VpnService;
import android.os.AsyncTask;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.MenuItemCompat;
@ -55,6 +57,7 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
@ -84,6 +87,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
private IInAppBillingService IABService = null;
private AlertDialog dialogFirst = null;
private AlertDialog dialogVpn = null;
private AlertDialog dialogDelay = null;
private AlertDialog dialogAbout = null;
private static final int REQUEST_VPN = 1;
@ -126,6 +130,10 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
swEnabled.setChecked(enabled);
swEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
final PendingIntent pi = SinkholeService.getStartIntent(ActivityMain.this);
am.cancel(pi);
if (isChecked) {
Log.i(TAG, "Switch on");
final Intent prepare = VpnService.prepare(ActivityMain.this);
@ -141,7 +149,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
.setCancelable(false)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
public void onClick(DialogInterface dialog, int which) {
if (running) {
Log.i(TAG, "Start intent=" + prepare);
try {
@ -168,6 +176,43 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
Log.i(TAG, "Switch off");
prefs.edit().putBoolean("enabled", false).apply();
SinkholeService.stop(ActivityMain.this);
// Delayed auto enable
final int minutes = Integer.parseInt(prefs.getString("delay_time", "5"));
if (minutes > 0) {
LayoutInflater inflater = LayoutInflater.from(ActivityMain.this);
View view = inflater.inflate(R.layout.delay, null);
TextView tvDelay = (TextView) view.findViewById(R.id.tvDelay);
final CheckBox cbDontAskAgain = (CheckBox) view.findViewById(R.id.cbDontAskAgain);
tvDelay.setText(getString(R.string.setting_delay, minutes));
dialogDelay = new AlertDialog.Builder(ActivityMain.this)
.setView(view)
.setCancelable(true)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
long time = SystemClock.elapsedRealtime() + minutes * 60 * 1000L;
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, pi);
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (cbDontAskAgain.isChecked())
prefs.edit().putString("delay_time", "0").apply();
}
})
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
dialogDelay = null;
}
})
.create();
dialogDelay.show();
}
}
}
});
@ -242,14 +287,14 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
.setCancelable(false)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
public void onClick(DialogInterface dialog, int which) {
if (running)
prefs.edit().putBoolean("initialized", true).apply();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
@ -288,6 +333,10 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
dialogVpn.dismiss();
dialogVpn = null;
}
if (dialogDelay != null) {
dialogDelay.dismiss();
dialogDelay = null;
}
if (dialogAbout != null) {
dialogAbout.dismiss();
dialogAbout = null;

View File

@ -24,7 +24,10 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.Xml;
@ -49,8 +52,11 @@ import javax.xml.parsers.SAXParserFactory;
public class ActivitySettings extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "NetGuard.Settings";
public static final int REQUEST_EXPORT = 1;
public static final int REQUEST_IMPORT = 2;
private PreferenceScreen settings;
private static final int REQUEST_EXPORT = 1;
private static final int REQUEST_IMPORT = 2;
private static final Intent INTENT_VPN_SETTINGS = new Intent("android.net.vpn.SETTINGS");
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
@ -59,8 +65,6 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new FragmentSettings()).commit();
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
@Override
@ -69,6 +73,42 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
super.onDestroy();
}
public void setup(PreferenceScreen screen) {
this.settings = screen;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int minutes = Integer.parseInt(prefs.getString("delay_time", "5"));
EditTextPreference pref_delay = (EditTextPreference) screen.findPreference("delay_time");
pref_delay.setTitle(getString(R.string.setting_delay, minutes));
Preference pref_export = screen.findPreference("export");
pref_export.setEnabled(getIntentCreateDocument().resolveActivity(getPackageManager()) != null);
pref_export.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
startActivityForResult(getIntentCreateDocument(), ActivitySettings.REQUEST_EXPORT);
return true;
}
});
Preference pref_import = screen.findPreference("import");
pref_import.setEnabled(getIntentCreateDocument().resolveActivity(getPackageManager()) != null);
pref_import.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
startActivityForResult(getIntentOpenDocument(), ActivitySettings.REQUEST_IMPORT);
return true;
}
});
Preference pref_vpn = screen.findPreference("vpn");
pref_vpn.setEnabled(INTENT_VPN_SETTINGS.resolveActivity(this.getPackageManager()) != null);
pref_vpn.setIntent(INTENT_VPN_SETTINGS);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
if ("whitelist_wifi".equals(name))
@ -85,6 +125,13 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
else if ("dark_theme".equals(name))
recreate();
else if ("delay_time".equals(name)) {
if (settings != null) {
EditTextPreference pref_delay = (EditTextPreference) settings.findPreference("delay_time");
pref_delay.setTitle(getString(R.string.setting_delay, Integer.parseInt(prefs.getString(name, "0"))));
}
}
}
@Override
@ -137,6 +184,21 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere
}.execute();
}
private static Intent getIntentCreateDocument() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/xml");
intent.putExtra(Intent.EXTRA_TITLE, "netguard.xml");
return intent;
}
private static Intent getIntentOpenDocument() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/xml");
return intent;
}
private void handleImport(final Intent data) {
new AsyncTask<Object, Object, Throwable>() {
@Override

View File

@ -19,57 +19,16 @@ package eu.faircode.netguard;
Copyright 2015 by Marcel Bokhorst (M66B)
*/
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
public class FragmentSettings extends PreferenceFragment {
private static final Intent INTENT_VPN_SETTINGS = new Intent("android.net.vpn.SETTINGS");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Preference pref_export = getPreferenceScreen().findPreference("export");
pref_export.setEnabled(getIntentCreateDocument().resolveActivity(getActivity().getPackageManager()) != null);
pref_export.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
getActivity().startActivityForResult(getIntentCreateDocument(), ActivitySettings.REQUEST_EXPORT);
return true;
}
});
Preference pref_import = getPreferenceScreen().findPreference("import");
pref_import.setEnabled(getIntentCreateDocument().resolveActivity(getActivity().getPackageManager()) != null);
pref_import.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
getActivity().startActivityForResult(getIntentOpenDocument(), ActivitySettings.REQUEST_IMPORT);
return true;
}
});
Preference pref_vpn = getPreferenceScreen().findPreference("vpn");
pref_vpn.setEnabled(INTENT_VPN_SETTINGS.resolveActivity(getActivity().getPackageManager()) != null);
pref_vpn.setIntent(INTENT_VPN_SETTINGS);
}
private static Intent getIntentCreateDocument() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/xml");
intent.putExtra(Intent.EXTRA_TITLE, "netguard.xml");
return intent;
}
private static Intent getIntentOpenDocument() {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/xml");
return intent;
((ActivitySettings) getActivity()).setup(getPreferenceScreen());
}
}

View File

@ -377,6 +377,13 @@ public class SinkholeService extends VpnService {
context.startService(intent);
}
public static PendingIntent getStartIntent(Context context) {
Intent intent = new Intent(context, SinkholeService.class);
intent.putExtra(EXTRA_COMMAND, Command.start);
intent.putExtra(EXTRA_UPDATE, true);
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void reload(String network, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (prefs.getBoolean("enabled", false))

View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="40dp"
android:paddingEnd="20dp"
android:paddingStart="20dp"
android:paddingTop="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="8dp"
android:text="@string/app_name"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="@+id/tvDelay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="8dp"
android:textAppearance="@android:style/TextAppearance.Material.Medium" />
<CheckBox
android:id="@+id/cbDontAskAgain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/msg_again"
android:textAppearance="@android:style/TextAppearance.Material.Medium" />
</LinearLayout>

View File

@ -19,12 +19,14 @@ This is caused by bugs in Android, or in the software provided by the manufactur
<string name="setting_system">Manage system applications</string>
<string name="setting_dark">Use dark theme</string>
<string name="setting_indicators">Show state indicators</string>
<string name="setting_delay">Enable again after %d minutes</string>
<string name="setting_export">Export settings</string>
<string name="setting_import">Import settings</string>
<string name="setting_vpn">Open Android VPN settings</string>
<string name="summary_system">Define rules for system applications, for experts only</string>
<string name="summary_indicators">Display the network type and if the network is metered in the action bar</string>
<string name="summary_delay">After disabling, enable again after the set number of minutes; zero means don\'t ask to enable again</string>
<string name="msg_sure">Are you sure?</string>
<string name="msg_disabled">NetGuard is disabled, use the switch above to enable NetGuard</string>
@ -32,6 +34,7 @@ This is caused by bugs in Android, or in the software provided by the manufactur
<string name="msg_completed">Action completed</string>
<string name="msg_vpn">NetGuard uses a local VPN as a sinkhole for blocked internet traffic,
for this reason you should allow a VPN connection in the next dialog</string>
<string name="msg_again">Don\'t ask me again</string>
<string name="title_using">Allow when device is in use</string>
<string name="title_roaming">Block when roaming</string>

View File

@ -26,6 +26,11 @@
android:key="indicators"
android:summary="@string/summary_indicators"
android:title="@string/setting_indicators" />
<EditTextPreference
android:defaultValue="5"
android:inputType="number"
android:key="delay_time"
android:summary="@string/summary_delay" />
<Preference
android:key="export"
android:title="@string/setting_export" />