Updated service activation related methods
This commit is contained in:
parent
6ebbd937e7
commit
6a0d7c0f3c
@ -1,6 +1,5 @@
|
||||
package org.itxtech.daedalus;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@ -201,50 +200,42 @@ public class Daedalus extends Application {
|
||||
Logger.shutdown();
|
||||
}
|
||||
|
||||
public Intent getServiceIntent() {
|
||||
return new Intent(this, DaedalusVpnService.class);
|
||||
public static Intent getServiceIntent(Context context) {
|
||||
return new Intent(context, DaedalusVpnService.class);
|
||||
}
|
||||
|
||||
public static boolean switchService() {
|
||||
if (DaedalusVpnService.isActivated()) {
|
||||
instance.deactivateService();
|
||||
deactivateService(instance);
|
||||
return false;
|
||||
} else {
|
||||
instance.activateService();
|
||||
activateService(instance);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean activateService() {
|
||||
Intent intent = VpnService.prepare(Daedalus.getInstance());
|
||||
public static boolean activateService(Context context) {
|
||||
Intent intent = VpnService.prepare(context);
|
||||
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));
|
||||
context.startService(Daedalus.getServiceIntent(context).setAction(DaedalusVpnService.ACTION_ACTIVATE));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void deactivateService() {
|
||||
startService(getServiceIntent().setAction(DaedalusVpnService.ACTION_DEACTIVATE));
|
||||
stopService(getServiceIntent());
|
||||
public static void deactivateService(Context context) {
|
||||
context.startService(getServiceIntent(context).setAction(DaedalusVpnService.ACTION_DEACTIVATE));
|
||||
context.stopService(getServiceIntent(context));
|
||||
}
|
||||
|
||||
public static void updateShortcut(Context context) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
|
||||
Log.d("Daedalus", "Updating shortcut");
|
||||
//shortcut!
|
||||
String notice = context.getString(R.string.button_text_activate);
|
||||
boolean activate = true;
|
||||
ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
|
||||
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
|
||||
if (DaedalusVpnService.class.getName().equals(service.service.getClassName())) {
|
||||
notice = context.getString(R.string.button_text_deactivate);
|
||||
activate = false;
|
||||
}
|
||||
}
|
||||
boolean activate = DaedalusVpnService.isActivated();
|
||||
String notice = activate ? context.getString(R.string.button_text_deactivate) : context.getString(R.string.button_text_activate);
|
||||
ShortcutInfo info = new ShortcutInfo.Builder(context, Daedalus.SHORTCUT_ID_ACTIVATE)
|
||||
.setLongLabel(notice)
|
||||
.setShortLabel(notice)
|
||||
@ -252,7 +243,6 @@ public class Daedalus extends Application {
|
||||
.setIntent(new Intent(context, MainActivity.class).setAction(Intent.ACTION_VIEW)
|
||||
.putExtra(MainActivity.LAUNCH_ACTION, activate ? MainActivity.LAUNCH_ACTION_ACTIVATE : MainActivity.LAUNCH_ACTION_DEACTIVATE))
|
||||
.build();
|
||||
|
||||
ShortcutManager shortcutManager = (ShortcutManager) context.getSystemService(SHORTCUT_SERVICE);
|
||||
shortcutManager.addDynamicShortcuts(Collections.singletonList(info));
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
if (result == Activity.RESULT_OK) {
|
||||
DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary());
|
||||
DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary());
|
||||
Daedalus.getInstance().startService(Daedalus.getInstance().getServiceIntent().setAction(DaedalusVpnService.ACTION_ACTIVATE));
|
||||
Daedalus.getInstance().startService(Daedalus.getServiceIntent(getApplicationContext()).setAction(DaedalusVpnService.ACTION_ACTIVATE));
|
||||
updateMainButton(R.string.button_text_deactivate);
|
||||
Daedalus.updateShortcut(Daedalus.getInstance());
|
||||
}
|
||||
@ -197,7 +197,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
if (launchAction == LAUNCH_ACTION_ACTIVATE) {
|
||||
this.activateService();
|
||||
} else if (launchAction == LAUNCH_ACTION_DEACTIVATE) {
|
||||
Daedalus.getInstance().deactivateService();
|
||||
Daedalus.deactivateService(getApplicationContext());
|
||||
} else if (launchAction == LAUNCH_ACTION_SERVICE_DONE) {
|
||||
Daedalus.updateShortcut(this.getApplicationContext());
|
||||
if (DaedalusVpnService.isActivated()) {
|
||||
|
@ -34,7 +34,7 @@ public class HomeFragment extends ToolbarFragment {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (DaedalusVpnService.isActivated()) {
|
||||
Daedalus.getInstance().deactivateService();
|
||||
Daedalus.deactivateService(getActivity().getApplicationContext());
|
||||
} else {
|
||||
startActivity(new Intent(getActivity(), MainActivity.class)
|
||||
.putExtra(MainActivity.LAUNCH_ACTION, MainActivity.LAUNCH_ACTION_ACTIVATE));
|
||||
|
@ -3,11 +3,8 @@ package org.itxtech.daedalus.receiver;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.VpnService;
|
||||
import org.itxtech.daedalus.Daedalus;
|
||||
import org.itxtech.daedalus.service.DaedalusVpnService;
|
||||
import org.itxtech.daedalus.util.Logger;
|
||||
import org.itxtech.daedalus.util.server.DNSServerHelper;
|
||||
|
||||
/**
|
||||
* Daedalus Project
|
||||
@ -24,20 +21,8 @@ public class BootBroadcastReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (Daedalus.getPrefs().getBoolean("settings_boot", false)) {
|
||||
Intent vIntent = VpnService.prepare(context);
|
||||
if (vIntent != null) {
|
||||
vIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(vIntent);
|
||||
}
|
||||
|
||||
DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary());
|
||||
DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary());
|
||||
|
||||
context.startService((new Intent(context, DaedalusVpnService.class)).setAction(DaedalusVpnService.ACTION_ACTIVATE));
|
||||
|
||||
Daedalus.activateService(context);
|
||||
Logger.info("Triggered boot receiver");
|
||||
}
|
||||
|
||||
Daedalus.updateShortcut(context);
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ public class StatusBarBroadcastReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getAction().equals(STATUS_BAR_BTN_DEACTIVATE_CLICK_ACTION)) {
|
||||
Daedalus.getInstance().deactivateService();
|
||||
Daedalus.deactivateService(context);
|
||||
}
|
||||
if (intent.getAction().equals(STATUS_BAR_BTN_SETTINGS_CLICK_ACTION)) {
|
||||
Intent settingsIntent = new Intent(context, MainActivity.class).putExtra(MainActivity.LAUNCH_FRAGMENT, MainActivity.FRAGMENT_SETTINGS);
|
||||
|
Loading…
Reference in New Issue
Block a user