Fixed main button text issue for good

This commit is contained in:
PeratX 2017-11-13 18:42:42 +08:00
parent c98f8875f6
commit 308a0a08d3
8 changed files with 28 additions and 50 deletions

View File

@ -177,7 +177,7 @@ public class Daedalus extends Application {
} }
public static void setRulesChanged() { public static void setRulesChanged() {
if (instance.isServiceActivated() && if (DaedalusVpnService.isActivated() &&
getPrefs().getBoolean("settings_allow_dynamic_rule_reload", false)) { getPrefs().getBoolean("settings_allow_dynamic_rule_reload", false)) {
initHostsResolver(); initHostsResolver();
} }
@ -209,44 +209,11 @@ public class Daedalus extends Application {
return new Intent(this, DaedalusVpnService.class); return new Intent(this, DaedalusVpnService.class);
} }
public boolean isAppOnForeground() {
// Returns a list of application processes that are running on the
// device
ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
String packageName = getApplicationContext().getPackageName();
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
if (appProcesses == null)
return false;
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
// The name of the process that this object is associated with.
if (appProcess.processName.equals(packageName)
&& appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return true;
}
}
return false;
}
public void deactivateService() { public void deactivateService() {
startService(getServiceIntent().setAction(DaedalusVpnService.ACTION_DEACTIVATE)); startService(getServiceIntent().setAction(DaedalusVpnService.ACTION_DEACTIVATE));
stopService(getServiceIntent()); stopService(getServiceIntent());
} }
public boolean isServiceActivated() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (DaedalusVpnService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
public static void updateShortcut(Context context) { public static void updateShortcut(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
Log.d("Daedalus", "Updating shortcut"); Log.d("Daedalus", "Updating shortcut");

View File

@ -178,7 +178,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
if (result == Activity.RESULT_OK) { if (result == Activity.RESULT_OK) {
DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary()); DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary());
DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary()); DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary());
Daedalus.getInstance().startService(Daedalus.getInstance().getServiceIntent().setAction(DaedalusVpnService.ACTION_ACTIVATE)); Daedalus.getInstance().startService(Daedalus.getInstance().getServiceIntent().setAction(DaedalusVpnService.ACTION_ACTIVATE));
updateMainButton(R.string.button_text_deactivate); updateMainButton(R.string.button_text_deactivate);
Daedalus.updateShortcut(Daedalus.getInstance()); Daedalus.updateShortcut(Daedalus.getInstance());

View File

@ -10,6 +10,7 @@ import android.widget.Button;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.MainActivity; import org.itxtech.daedalus.activity.MainActivity;
import org.itxtech.daedalus.service.DaedalusVpnService;
/** /**
* Daedalus Project * Daedalus Project
@ -28,11 +29,11 @@ public class HomeFragment extends ToolbarFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false); View view = inflater.inflate(R.layout.fragment_main, container, false);
Button but = (Button) view.findViewById(R.id.button_activate); Button but = view.findViewById(R.id.button_activate);
but.setOnClickListener(new View.OnClickListener() { but.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (Daedalus.getInstance().isServiceActivated()) { if (DaedalusVpnService.isActivated()) {
Daedalus.getInstance().deactivateService(); Daedalus.getInstance().deactivateService();
} else { } else {
startActivity(new Intent(getActivity(), MainActivity.class) startActivity(new Intent(getActivity(), MainActivity.class)
@ -51,15 +52,17 @@ public class HomeFragment extends ToolbarFragment {
} }
@Override @Override
public void onResume() { public void setUserVisibleHint(boolean isVisibleToUser) {
super.onResume(); super.setUserVisibleHint(isVisibleToUser);
updateUserInterface(); if (isVisibleToUser) {
updateUserInterface();
}
} }
private void updateUserInterface() { private void updateUserInterface() {
Log.d("DMainFragment", "updateInterface"); Log.d("DMainFragment", "updateInterface");
Button but = (Button) getView().findViewById(R.id.button_activate); Button but = getView().findViewById(R.id.button_activate);
if (Daedalus.getInstance().isServiceActivated()) { if (DaedalusVpnService.isActivated()) {
but.setText(R.string.button_text_deactivate); but.setText(R.string.button_text_deactivate);
} else { } else {
but.setText(R.string.button_text_activate); but.setText(R.string.button_text_activate);

View File

@ -16,6 +16,7 @@ import android.widget.TextView;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.ConfigActivity; import org.itxtech.daedalus.activity.ConfigActivity;
import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.Rule; import org.itxtech.daedalus.util.Rule;
import java.io.File; import java.io.File;
@ -236,7 +237,7 @@ public class RulesFragment extends ToolbarFragment implements Toolbar.OnMenuItem
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if ((!Daedalus.getPrefs().getBoolean("settings_allow_dynamic_rule_reload", false) && if ((!Daedalus.getPrefs().getBoolean("settings_allow_dynamic_rule_reload", false) &&
!Daedalus.getInstance().isServiceActivated()) || !DaedalusVpnService.isActivated()) ||
Daedalus.getPrefs().getBoolean("settings_allow_dynamic_rule_reload", false)) { Daedalus.getPrefs().getBoolean("settings_allow_dynamic_rule_reload", false)) {
Rule rule = Rule.getRuleById(id); Rule rule = Rule.getRuleById(id);
if (rule != null) { if (rule != null) {

View File

@ -5,7 +5,6 @@ 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; import org.itxtech.daedalus.activity.MainActivity;
@ -25,7 +24,7 @@ public class DaedalusTileService extends TileService {
@Override @Override
public void onClick() { public void onClick() {
boolean activate = Daedalus.getInstance().isServiceActivated(); boolean activate = DaedalusVpnService.isActivated();
Intent intent = new Intent(getApplicationContext(), MainActivity.class) Intent intent = new Intent(getApplicationContext(), MainActivity.class)
.setAction(Intent.ACTION_VIEW) .setAction(Intent.ACTION_VIEW)
@ -40,7 +39,7 @@ public class DaedalusTileService extends TileService {
} }
private void updateTile() { private void updateTile() {
boolean activate = Daedalus.getInstance().isServiceActivated(); boolean activate = DaedalusVpnService.isActivated();
Tile tile = getQsTile(); Tile tile = getQsTile();
tile.setLabel(getString(R.string.quick_toggle)); tile.setLabel(getString(R.string.quick_toggle));

View File

@ -63,6 +63,12 @@ public class DaedalusVpnService extends VpnService implements Runnable {
public HashMap<String, String> dnsServers; public HashMap<String, String> dnsServers;
private static boolean activated = false;
public static boolean isActivated() {
return activated;
}
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
@ -73,6 +79,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
if (intent != null) { if (intent != null) {
switch (intent.getAction()) { switch (intent.getAction()) {
case ACTION_ACTIVATE: case ACTION_ACTIVATE:
activated = true;
if (Daedalus.getPrefs().getBoolean("settings_notification", true)) { if (Daedalus.getPrefs().getBoolean("settings_notification", true)) {
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
@ -130,6 +137,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void stopThread() { private void stopThread() {
Log.d(TAG, "stopThread"); Log.d(TAG, "stopThread");
activated = false;
boolean shouldRefresh = false; boolean shouldRefresh = false;
try { try {
if (this.descriptor != null) { if (this.descriptor != null) {
@ -165,7 +173,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
Logger.info("Daedalus VPN service has stopped"); Logger.info("Daedalus VPN service has stopped");
} }
if (shouldRefresh && MainActivity.getInstance() != null && Daedalus.getInstance().isAppOnForeground()) { 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_AFTER_DEACTIVATE));
} else if (shouldRefresh) { } else if (shouldRefresh) {
@ -173,7 +181,6 @@ public class DaedalusVpnService extends VpnService implements Runnable {
} }
} }
@Override @Override
public void onRevoke() { public void onRevoke() {
stopThread(); stopThread();

View File

@ -1,6 +1,7 @@
package org.itxtech.daedalus.util; package org.itxtech.daedalus.util;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.service.DaedalusVpnService;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -104,7 +105,7 @@ public class Rule {
} }
public boolean isServiceAndUsing() { public boolean isServiceAndUsing() {
return Daedalus.getInstance().isServiceActivated() && isUsing(); return DaedalusVpnService.isActivated() && isUsing();
} }
public void addToConfig() { public void addToConfig() {

View File

@ -2,6 +2,7 @@ package org.itxtech.daedalus.util.server;
import android.content.Context; import android.content.Context;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.service.DaedalusVpnService;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.ArrayList; import java.util.ArrayList;
@ -141,6 +142,6 @@ public class DNSServerHelper {
} }
public static boolean isInUsing(CustomDNSServer server) { public static boolean isInUsing(CustomDNSServer server) {
return Daedalus.getInstance().isServiceActivated() && (server.getId().equals(getPrimary()) || server.getId().equals(getSecondary())); return DaedalusVpnService.isActivated() && (server.getId().equals(getPrimary()) || server.getId().equals(getSecondary()));
} }
} }