diff --git a/app/src/main/java/org/itxtech/daedalus/Daedalus.java b/app/src/main/java/org/itxtech/daedalus/Daedalus.java index 57b6c11..8b9c3d6 100644 --- a/app/src/main/java/org/itxtech/daedalus/Daedalus.java +++ b/app/src/main/java/org/itxtech/daedalus/Daedalus.java @@ -9,6 +9,7 @@ import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutManager; import android.graphics.drawable.Icon; import android.net.Uri; +import android.net.VpnService; import android.os.Build; import android.preference.PreferenceManager; import android.util.Log; @@ -24,6 +25,7 @@ import org.itxtech.daedalus.util.Logger; import org.itxtech.daedalus.util.Rule; import org.itxtech.daedalus.util.RuleResolver; import org.itxtech.daedalus.util.server.DNSServer; +import org.itxtech.daedalus.util.server.DNSServerHelper; import java.io.File; import java.util.ArrayList; @@ -203,6 +205,28 @@ public class Daedalus extends Application { return new Intent(this, DaedalusVpnService.class); } + public static boolean switchService() { + if (DaedalusVpnService.isActivated()) { + instance.deactivateService(); + return false; + } else { + instance.activateService(); + return true; + } + } + + public boolean activateService() { + Intent intent = VpnService.prepare(Daedalus.getInstance()); + if (intent != null) { + return false; + } else { + DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary()); + DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary()); + startService(Daedalus.getInstance().getServiceIntent().setAction(DaedalusVpnService.ACTION_ACTIVATE)); + return true; + } + } + public void deactivateService() { startService(getServiceIntent().setAction(DaedalusVpnService.ACTION_DEACTIVATE)); stopService(getServiceIntent()); diff --git a/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java b/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java index 81e98c7..4e670e8 100644 --- a/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java +++ b/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java @@ -46,7 +46,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On public static final int LAUNCH_ACTION_NONE = 0; public static final int LAUNCH_ACTION_ACTIVATE = 1; public static final int LAUNCH_ACTION_DEACTIVATE = 2; - public static final int LAUNCH_ACTION_AFTER_DEACTIVATE = 3; + public static final int LAUNCH_ACTION_SERVICE_DONE = 3; public static final String LAUNCH_FRAGMENT = "org.itxtech.daedalus.activity.MainActivity.LAUNCH_FRAGMENT"; public static final int FRAGMENT_NONE = -1; @@ -198,9 +198,13 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On this.activateService(); } else if (launchAction == LAUNCH_ACTION_DEACTIVATE) { Daedalus.getInstance().deactivateService(); - } else if (launchAction == LAUNCH_ACTION_AFTER_DEACTIVATE) { + } else if (launchAction == LAUNCH_ACTION_SERVICE_DONE) { Daedalus.updateShortcut(this.getApplicationContext()); - updateMainButton(R.string.button_text_activate); + if (DaedalusVpnService.isActivated()) { + updateMainButton(R.string.button_text_deactivate); + } else { + updateMainButton(R.string.button_text_activate); + } } int fragment = intent.getIntExtra(LAUNCH_FRAGMENT, FRAGMENT_NONE); diff --git a/app/src/main/java/org/itxtech/daedalus/service/DaedalusTileService.java b/app/src/main/java/org/itxtech/daedalus/service/DaedalusTileService.java index 08326f1..1dd4bc3 100644 --- a/app/src/main/java/org/itxtech/daedalus/service/DaedalusTileService.java +++ b/app/src/main/java/org/itxtech/daedalus/service/DaedalusTileService.java @@ -1,12 +1,11 @@ package org.itxtech.daedalus.service; -import android.content.Intent; import android.os.Build; import android.service.quicksettings.Tile; import android.service.quicksettings.TileService; import android.support.annotation.RequiresApi; +import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.R; -import org.itxtech.daedalus.activity.MainActivity; /** * Daedalus Project @@ -24,13 +23,11 @@ public class DaedalusTileService extends TileService { @Override public void onClick() { - boolean activate = DaedalusVpnService.isActivated(); - - Intent intent = new Intent(getApplicationContext(), MainActivity.class) - .setAction(Intent.ACTION_VIEW) - .putExtra(MainActivity.LAUNCH_ACTION, activate ? MainActivity.LAUNCH_ACTION_DEACTIVATE : MainActivity.LAUNCH_ACTION_ACTIVATE); - - startActivity(intent); + Tile tile = getQsTile(); + tile.setLabel(getString(R.string.quick_toggle)); + tile.setContentDescription(getString(R.string.app_name)); + tile.setState(Daedalus.switchService() ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE); + tile.updateTile(); } @Override @@ -40,7 +37,6 @@ public class DaedalusTileService extends TileService { private void updateTile() { boolean activate = DaedalusVpnService.isActivated(); - Tile tile = getQsTile(); tile.setLabel(getString(R.string.quick_toggle)); tile.setContentDescription(getString(R.string.app_name)); diff --git a/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java b/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java index ffd68a1..a5958d6 100644 --- a/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java +++ b/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java @@ -120,6 +120,10 @@ public class DaedalusVpnService extends VpnService implements Runnable { this.mThread.start(); } Daedalus.updateShortcut(this.getApplicationContext()); + if (MainActivity.getInstance() != null) { + MainActivity.getInstance().startActivity(new Intent(getApplicationContext(), MainActivity.class) + .putExtra(MainActivity.LAUNCH_ACTION, MainActivity.LAUNCH_ACTION_SERVICE_DONE)); + } return START_STICKY; case ACTION_DEACTIVATE: stopThread(); @@ -175,7 +179,7 @@ public class DaedalusVpnService extends VpnService implements Runnable { if (shouldRefresh && MainActivity.getInstance() != null) { MainActivity.getInstance().startActivity(new Intent(getApplicationContext(), MainActivity.class) - .putExtra(MainActivity.LAUNCH_ACTION, MainActivity.LAUNCH_ACTION_AFTER_DEACTIVATE)); + .putExtra(MainActivity.LAUNCH_ACTION, MainActivity.LAUNCH_ACTION_SERVICE_DONE)); } else if (shouldRefresh) { Daedalus.updateShortcut(getApplicationContext()); }