Refactoring

This commit is contained in:
M66B 2015-10-26 17:23:41 +01:00
parent eac51e9e45
commit 4bc5ddde78
3 changed files with 19 additions and 15 deletions

View File

@ -79,9 +79,7 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
} else {
Log.i(TAG, "Switch off");
prefs.edit().putBoolean("enabled", false).apply();
Intent intent = new Intent(ActivityMain.this, BlackHoleService.class);
intent.putExtra(BlackHoleService.EXTRA_COMMAND, BlackHoleService.Command.stop);
startService(intent);
BlackHoleService.stop(ActivityMain.this);
}
}
});
@ -327,11 +325,8 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
prefs.edit().putBoolean("enabled", resultCode == RESULT_OK).apply();
// Start service
if (resultCode == RESULT_OK) {
Intent intent = new Intent(ActivityMain.this, BlackHoleService.class);
intent.putExtra(BlackHoleService.EXTRA_COMMAND, BlackHoleService.Command.start);
startService(intent);
}
if (resultCode == RESULT_OK)
BlackHoleService.start(this);
} else
super.onActivityResult(requestCode, resultCode, data);
}

View File

@ -20,9 +20,9 @@ public class BlackHoleService extends VpnService {
private static final String TAG = "NetGuard.Service";
private ParcelFileDescriptor vpn = null;
public static final String EXTRA_COMMAND = "Command";
private static final String EXTRA_COMMAND = "Command";
public enum Command {start, reload, stop}
private enum Command {start, reload, stop}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
@ -188,6 +188,12 @@ public class BlackHoleService extends VpnService {
super.onRevoke();
}
public static void start(Context context) {
Intent intent = new Intent(context, BlackHoleService.class);
intent.putExtra(EXTRA_COMMAND, Command.start);
context.startService(intent);
}
public static void reload(String name, Context context) {
if (name == null || ("wifi".equals(name) ? Util.isWifiActive(context) : !Util.isWifiActive(context))) {
Intent intent = new Intent(context, BlackHoleService.class);
@ -195,4 +201,10 @@ public class BlackHoleService extends VpnService {
context.startService(intent);
}
}
public static void stop(Context context) {
Intent intent = new Intent(context, BlackHoleService.class);
intent.putExtra(EXTRA_COMMAND, Command.stop);
context.startService(intent);
}
}

View File

@ -15,10 +15,7 @@ public class Receiver extends BroadcastReceiver {
Util.logExtras(TAG, intent);
// Start service
if (VpnService.prepare(context) == null) {
Intent service = new Intent(context, BlackHoleService.class);
service.putExtra(BlackHoleService.EXTRA_COMMAND, BlackHoleService.Command.start);
context.startService(service);
}
if (VpnService.prepare(context) == null)
BlackHoleService.start(context);
}
}