Launch by quick tile won't show the MainActivity. Close #60

This commit is contained in:
PeratX 2018-01-13 16:01:18 +08:00
parent f696d3c7bd
commit 6ebbd937e7
4 changed files with 42 additions and 14 deletions

View File

@ -9,6 +9,7 @@ import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager; import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon; import android.graphics.drawable.Icon;
import android.net.Uri; import android.net.Uri;
import android.net.VpnService;
import android.os.Build; import android.os.Build;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; 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.Rule;
import org.itxtech.daedalus.util.RuleResolver; import org.itxtech.daedalus.util.RuleResolver;
import org.itxtech.daedalus.util.server.DNSServer; import org.itxtech.daedalus.util.server.DNSServer;
import org.itxtech.daedalus.util.server.DNSServerHelper;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -203,6 +205,28 @@ public class Daedalus extends Application {
return new Intent(this, DaedalusVpnService.class); 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() { public void deactivateService() {
startService(getServiceIntent().setAction(DaedalusVpnService.ACTION_DEACTIVATE)); startService(getServiceIntent().setAction(DaedalusVpnService.ACTION_DEACTIVATE));
stopService(getServiceIntent()); stopService(getServiceIntent());

View File

@ -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_NONE = 0;
public static final int LAUNCH_ACTION_ACTIVATE = 1; public static final int LAUNCH_ACTION_ACTIVATE = 1;
public static final int LAUNCH_ACTION_DEACTIVATE = 2; 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 String LAUNCH_FRAGMENT = "org.itxtech.daedalus.activity.MainActivity.LAUNCH_FRAGMENT";
public static final int FRAGMENT_NONE = -1; public static final int FRAGMENT_NONE = -1;
@ -198,9 +198,13 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
this.activateService(); this.activateService();
} else if (launchAction == LAUNCH_ACTION_DEACTIVATE) { } else if (launchAction == LAUNCH_ACTION_DEACTIVATE) {
Daedalus.getInstance().deactivateService(); Daedalus.getInstance().deactivateService();
} else if (launchAction == LAUNCH_ACTION_AFTER_DEACTIVATE) { } else if (launchAction == LAUNCH_ACTION_SERVICE_DONE) {
Daedalus.updateShortcut(this.getApplicationContext()); 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); int fragment = intent.getIntExtra(LAUNCH_FRAGMENT, FRAGMENT_NONE);

View File

@ -1,12 +1,11 @@
package org.itxtech.daedalus.service; package org.itxtech.daedalus.service;
import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.service.quicksettings.Tile; import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService; import android.service.quicksettings.TileService;
import android.support.annotation.RequiresApi; import android.support.annotation.RequiresApi;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.MainActivity;
/** /**
* Daedalus Project * Daedalus Project
@ -24,13 +23,11 @@ public class DaedalusTileService extends TileService {
@Override @Override
public void onClick() { public void onClick() {
boolean activate = DaedalusVpnService.isActivated(); Tile tile = getQsTile();
tile.setLabel(getString(R.string.quick_toggle));
Intent intent = new Intent(getApplicationContext(), MainActivity.class) tile.setContentDescription(getString(R.string.app_name));
.setAction(Intent.ACTION_VIEW) tile.setState(Daedalus.switchService() ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
.putExtra(MainActivity.LAUNCH_ACTION, activate ? MainActivity.LAUNCH_ACTION_DEACTIVATE : MainActivity.LAUNCH_ACTION_ACTIVATE); tile.updateTile();
startActivity(intent);
} }
@Override @Override
@ -40,7 +37,6 @@ public class DaedalusTileService extends TileService {
private void updateTile() { private void updateTile() {
boolean activate = DaedalusVpnService.isActivated(); boolean activate = DaedalusVpnService.isActivated();
Tile tile = getQsTile(); Tile tile = getQsTile();
tile.setLabel(getString(R.string.quick_toggle)); tile.setLabel(getString(R.string.quick_toggle));
tile.setContentDescription(getString(R.string.app_name)); tile.setContentDescription(getString(R.string.app_name));

View File

@ -120,6 +120,10 @@ public class DaedalusVpnService extends VpnService implements Runnable {
this.mThread.start(); this.mThread.start();
} }
Daedalus.updateShortcut(this.getApplicationContext()); 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; return START_STICKY;
case ACTION_DEACTIVATE: case ACTION_DEACTIVATE:
stopThread(); stopThread();
@ -175,7 +179,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
if (shouldRefresh && MainActivity.getInstance() != null) { if (shouldRefresh && MainActivity.getInstance() != null) {
MainActivity.getInstance().startActivity(new Intent(getApplicationContext(), MainActivity.class) 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) { } else if (shouldRefresh) {
Daedalus.updateShortcut(getApplicationContext()); Daedalus.updateShortcut(getApplicationContext());
} }