Refactoring

This commit is contained in:
M66B 2015-10-30 08:57:36 +01:00
parent b05c7a0164
commit 7aa4613830
3 changed files with 124 additions and 82 deletions

View File

@ -226,14 +226,14 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
menuNetwork = menu.findItem(R.id.menu_network);
menuNetwork.setIcon(Util.isWifiActive(this) ? R.drawable.ic_network_wifi_white_24dp : R.drawable.ic_network_cell_white_24dp);
MenuItem wifi = menu.findItem(R.id.menu_whitelist_wifi);
wifi.setChecked(prefs.getBoolean("whitelist_wifi", true));
MenuItem menuWwifi = menu.findItem(R.id.menu_whitelist_wifi);
menuWwifi.setChecked(prefs.getBoolean("whitelist_wifi", true));
MenuItem other = menu.findItem(R.id.menu_whitelist_other);
other.setChecked(prefs.getBoolean("whitelist_other", true));
MenuItem menuOther = menu.findItem(R.id.menu_whitelist_other);
menuOther.setChecked(prefs.getBoolean("whitelist_other", true));
MenuItem dark = menu.findItem(R.id.menu_dark);
dark.setChecked(prefs.getBoolean("dark_theme", false));
MenuItem menuTheme = menu.findItem(R.id.menu_theme);
menuTheme.setChecked(prefs.getBoolean("dark_theme", false));
return super.onPrepareOptionsMenu(menu);
}
@ -245,12 +245,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_network:
Intent settings = new Intent(Util.isWifiActive(this)
? Settings.ACTION_WIFI_SETTINGS : Settings.ACTION_WIRELESS_SETTINGS);
if (settings.resolveActivity(getPackageManager()) != null)
startActivity(settings);
else
Log.w(TAG, settings + " not available");
menu_network();
return true;
case R.id.menu_refresh:
@ -258,79 +253,35 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
return true;
case R.id.menu_whitelist_wifi:
prefs.edit().putBoolean("whitelist_wifi", !prefs.getBoolean("whitelist_wifi", true)).apply();
fillApplicationList();
SinkholeService.reload("wifi", this);
menu_whitelist_wifi(prefs);
return true;
case R.id.menu_whitelist_other:
prefs.edit().putBoolean("whitelist_other", !prefs.getBoolean("whitelist_other", true)).apply();
fillApplicationList();
SinkholeService.reload("other", this);
menu_whitelist_other(prefs);
return true;
case R.id.menu_reset_wifi:
new AlertDialog.Builder(this)
.setMessage(R.string.msg_sure)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
reset("wifi");
}
})
.setNegativeButton(android.R.string.no, null)
.show();
menu_reset_wifi();
return true;
case R.id.menu_reset_other:
new AlertDialog.Builder(this)
.setMessage(R.string.msg_sure)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
reset("other");
}
})
.setNegativeButton(android.R.string.no, null)
.show();
menu_reset_other();
return true;
case R.id.menu_dark:
prefs.edit().putBoolean("dark_theme", !prefs.getBoolean("dark_theme", false)).apply();
recreate();
case R.id.menu_theme:
menu_theme(prefs);
return true;
case R.id.menu_vpn_settings:
// Open VPN settings
Intent vpn = new Intent("android.net.vpn.SETTINGS");
if (vpn.resolveActivity(getPackageManager()) != null)
startActivity(vpn);
else
Log.w(TAG, vpn + " not available");
menu_vpn_settings();
return true;
case R.id.menu_support:
Intent xda = new Intent(Intent.ACTION_VIEW);
xda.setData(Uri.parse("http://forum.xda-developers.com/showthread.php?t=3233012"));
if (xda.resolveActivity(getPackageManager()) != null)
startActivity(xda);
else
Log.w(TAG, xda + " not available");
menu_support();
return true;
case R.id.menu_about:
// Show about
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.about, null);
TextView tvVersion = (TextView) view.findViewById(R.id.tvVersion);
tvVersion.setText(Util.getSelfVersionName(this));
AlertDialog dialog = new AlertDialog.Builder(this)
.setView(view)
.setCancelable(true).create();
dialog.show();
menu_about();
return true;
default:
@ -338,6 +289,97 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
}
}
private void menu_network() {
Intent settings = new Intent(Util.isWifiActive(this)
? Settings.ACTION_WIFI_SETTINGS : Settings.ACTION_WIRELESS_SETTINGS);
if (settings.resolveActivity(getPackageManager()) != null)
startActivity(settings);
else
Log.w(TAG, settings + " not available");
}
private void menu_whitelist_wifi(SharedPreferences prefs) {
prefs.edit().putBoolean("whitelist_wifi", !prefs.getBoolean("whitelist_wifi", true)).apply();
fillApplicationList();
SinkholeService.reload("wifi", this);
}
private void menu_whitelist_other(SharedPreferences prefs) {
prefs.edit().putBoolean("whitelist_other", !prefs.getBoolean("whitelist_other", true)).apply();
fillApplicationList();
SinkholeService.reload("other", this);
}
private void menu_reset_wifi() {
new AlertDialog.Builder(this)
.setMessage(R.string.msg_sure)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
reset("wifi");
}
})
.setNegativeButton(android.R.string.no, null)
.show();
}
private void menu_reset_other() {
new AlertDialog.Builder(this)
.setMessage(R.string.msg_sure)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
reset("other");
}
})
.setNegativeButton(android.R.string.no, null)
.show();
}
private void menu_theme(SharedPreferences prefs) {
prefs.edit().putBoolean("dark_theme", !prefs.getBoolean("dark_theme", false)).apply();
recreate();
}
private void menu_vpn_settings() {
Intent vpn = new Intent("android.net.vpn.SETTINGS");
if (vpn.resolveActivity(getPackageManager()) != null)
startActivity(vpn);
else
Log.w(TAG, vpn + " not available");
}
private void menu_support() {
Intent xda = new Intent(Intent.ACTION_VIEW);
xda.setData(Uri.parse("http://forum.xda-developers.com/showthread.php?t=3233012"));
if (xda.resolveActivity(getPackageManager()) != null)
startActivity(xda);
else
Log.w(TAG, xda + " not available");
}
private void menu_about() {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.about, null);
TextView tvVersion = (TextView) view.findViewById(R.id.tvVersion);
tvVersion.setText(Util.getSelfVersionName(this));
view.setOnClickListener(new View.OnClickListener() {
private short tap = 0;
@Override
public void onClick(View view) {
if (++tap == 7) {
tap = 0;
Util.sendLogcat(TAG, ActivityMain.this);
}
}
});
AlertDialog dialog = new AlertDialog.Builder(this)
.setView(view)
.setCancelable(true).create();
dialog.show();
}
private void reset(String network) {
SharedPreferences other = getSharedPreferences(network, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = other.edit();

View File

@ -45,8 +45,8 @@ public class SinkholeService extends VpnService {
switch (cmd) {
case start:
if (enabled && vpn == null) {
vpn = vpnStart();
receiveStart(vpn);
vpn = startVPN();
startDebug(vpn);
}
break;
@ -54,18 +54,18 @@ public class SinkholeService extends VpnService {
// Seamless handover
ParcelFileDescriptor prev = vpn;
if (enabled) {
vpn = vpnStart();
receivedEnd();
receiveStart(vpn);
vpn = startVPN();
stopDebug();
startDebug(vpn);
}
if (prev != null)
vpnStop(prev);
stopVPN(prev);
break;
case stop:
if (vpn != null) {
receivedEnd();
vpnStop(vpn);
stopDebug();
stopVPN(vpn);
vpn = null;
}
stopSelf();
@ -75,7 +75,7 @@ public class SinkholeService extends VpnService {
return START_STICKY;
}
private ParcelFileDescriptor vpnStart() {
private ParcelFileDescriptor startVPN() {
Log.i(TAG, "Starting");
// Check if Wi-Fi
@ -131,7 +131,7 @@ public class SinkholeService extends VpnService {
}
}
private void vpnStop(ParcelFileDescriptor pfd) {
private void stopVPN(ParcelFileDescriptor pfd) {
Log.i(TAG, "Stopping");
try {
pfd.close();
@ -140,7 +140,7 @@ public class SinkholeService extends VpnService {
}
}
private void receiveStart(final ParcelFileDescriptor pfd) {
private void startDebug(final ParcelFileDescriptor pfd) {
if (!debug)
return;
@ -198,7 +198,7 @@ public class SinkholeService extends VpnService {
thread.start();
}
private void receivedEnd() {
private void stopDebug() {
if (thread != null)
thread.interrupt();
}
@ -260,8 +260,8 @@ public class SinkholeService extends VpnService {
Log.i(TAG, "Destroy");
if (vpn != null) {
receivedEnd();
vpnStop(vpn);
stopDebug();
stopVPN(vpn);
vpn = null;
}
@ -277,8 +277,8 @@ public class SinkholeService extends VpnService {
Log.i(TAG, "Revoke");
if (vpn != null) {
receivedEnd();
vpnStop(vpn);
stopDebug();
stopVPN(vpn);
vpn = null;
}

View File

@ -29,7 +29,7 @@
android:id="@+id/menu_reset_other"
android:title="@string/menu_reset_other" />
<item
android:id="@+id/menu_dark"
android:id="@+id/menu_theme"
android:checkable="true"
android:title="@string/menu_dark" />
<item