Implemented Launcher shortcuts

This commit is contained in:
PeratX 2017-04-11 14:42:30 +08:00
parent 33a74b2ada
commit 139ed2d77a
11 changed files with 129 additions and 7 deletions

View File

@ -47,6 +47,10 @@
<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter> </intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts"/>
</activity> </activity>
<activity <activity
android:name=".activity.SettingsActivity" android:name=".activity.SettingsActivity"

View File

@ -1,9 +1,20 @@
package org.itxtech.daedalus; package org.itxtech.daedalus;
import android.app.ActivityManager;
import android.app.Application; import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.util.Log;
import org.itxtech.daedalus.activity.MainActivity;
import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.DnsServer; import org.itxtech.daedalus.util.DnsServer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
@ -17,6 +28,8 @@ import java.util.List;
* the Free Software Foundation, version 3. * the Free Software Foundation, version 3.
*/ */
public class Daedalus extends Application { public class Daedalus extends Application {
public static final String SHORTCUT_ID_ACTIVATE = "activate";
public static final List<DnsServer> DNS_SERVERS = new ArrayList<DnsServer>() {{ public static final List<DnsServer> DNS_SERVERS = new ArrayList<DnsServer>() {{
add(new DnsServer("0", "113.107.249.56", R.string.server_north_china)); add(new DnsServer("0", "113.107.249.56", R.string.server_north_china));
add(new DnsServer("1", "120.27.103.230", R.string.server_east_china)); add(new DnsServer("1", "120.27.103.230", R.string.server_east_china));
@ -47,6 +60,31 @@ public class Daedalus extends Application {
instance = null; instance = null;
} }
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;
}
}
ShortcutInfo info = new ShortcutInfo.Builder(context, Daedalus.SHORTCUT_ID_ACTIVATE)
.setLongLabel(notice)
.setShortLabel(notice)
.setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher))
.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(Arrays.asList(info));
}
}
public static Daedalus getInstance() { public static Daedalus getInstance() {
return instance; return instance;
} }

View File

@ -1,7 +1,6 @@
package org.itxtech.daedalus.activity; package org.itxtech.daedalus.activity;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -14,10 +13,13 @@ import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
import org.itxtech.daedalus.service.DaedalusVpnService; import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.DnsServer; import org.itxtech.daedalus.util.DnsServer;
import java.util.List;
/** /**
* Daedalus Project * Daedalus Project
* *
@ -29,6 +31,11 @@ import org.itxtech.daedalus.util.DnsServer;
* the Free Software Foundation, version 3. * the Free Software Foundation, version 3.
*/ */
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
public static final String LAUNCH_ACTION = "org.itxtech.daedalus.activity.MainActivity.LAUNCH_ACTION";
public static final int LAUNCH_ACTION_NONE = 0;
public static final int LAUNCH_ACTION_ACTIVATE = 1;
public static final int LAUNCH_ACTION_DEACTIVATE = 2;
private static MainActivity instance = null; private static MainActivity instance = null;
private SharedPreferences prefs; private SharedPreferences prefs;
@ -67,13 +74,14 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (isServiceActivated()) { if (isServiceActivated()) {
but.setText(R.string.button_text_activate);
deactivateService(); deactivateService();
} else { } else {
activateService(); activateService();
} }
} }
}); });
updateUserInterface(getIntent());
} }
@Override @Override
@ -84,17 +92,62 @@ public class MainActivity extends AppCompatActivity {
} }
@Override @Override
protected void onRestart() { protected void onNewIntent(Intent intent) {
super.onRestart(); super.onNewIntent(intent);
final Button but = (Button) findViewById(R.id.button_activate); updateUserInterface(intent);
}
private void updateUserInterface(Intent intent) {
int launchAction = intent.getIntExtra(LAUNCH_ACTION, LAUNCH_ACTION_NONE);
if (launchAction == LAUNCH_ACTION_ACTIVATE) {
Daedalus.updateShortcut(this);
activateService();
} else if (launchAction == LAUNCH_ACTION_DEACTIVATE) {
deactivateService();
} else {
updateUserInterface();
Daedalus.updateShortcut(this);
}
}
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;
}
private void updateUserInterface() {
Button but = (Button) findViewById(R.id.button_activate);
if (isServiceActivated()) { if (isServiceActivated()) {
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);
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
} }
}
@Override
protected void onRestart() {
super.onRestart();
updateUserInterface();
} }
private void initConfig() { private void initConfig() {

View File

@ -7,6 +7,7 @@ import android.content.SharedPreferences;
import android.net.VpnService; import android.net.VpnService;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
import org.itxtech.daedalus.service.DaedalusVpnService; import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.DnsServer; import org.itxtech.daedalus.util.DnsServer;
@ -40,5 +41,7 @@ public class BootBroadcastReceiver extends BroadcastReceiver {
Log.d("DBootRecv", "Triggered boot receiver"); Log.d("DBootRecv", "Triggered boot receiver");
} }
Daedalus.updateShortcut(context);
} }
} }

View File

@ -18,6 +18,7 @@ import android.system.StructPollfd;
import android.util.Log; import android.util.Log;
import de.measite.minidns.DNSMessage; import de.measite.minidns.DNSMessage;
import de.measite.minidns.util.InetAddressUtil; import de.measite.minidns.util.InetAddressUtil;
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.activity.SettingsActivity; import org.itxtech.daedalus.activity.SettingsActivity;
@ -122,6 +123,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
this.running = true; this.running = true;
this.mThread.start(); this.mThread.start();
} }
Daedalus.updateShortcut(this);
return START_STICKY; return START_STICKY;
case ACTION_DEACTIVATE: case ACTION_DEACTIVATE:
stopThread(); stopThread();
@ -163,8 +165,15 @@ public class DaedalusVpnService extends VpnService implements Runnable {
Log.d(TAG, e.toString()); Log.d(TAG, e.toString());
} }
stopSelf(); stopSelf();
if (MainActivity.getInstance() != null && MainActivity.getInstance().isAppOnForeground()) {
startActivity(new Intent(this, MainActivity.class).putExtra(MainActivity.LAUNCH_ACTION, MainActivity.LAUNCH_ACTION_NONE));
} else {
Daedalus.updateShortcut(this);
}
} }
@Override @Override
public void onRevoke() { public void onRevoke() {
stopThread(); stopThread();

Binary file not shown.

After

Width:  |  Height:  |  Size: 443 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 324 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 523 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 675 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 840 B

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@mipmap/ic_settings_applications_grey600_36dp"
android:shortcutDisabledMessage="@string/action_settings"
android:shortcutId="settings"
android:shortcutLongLabel="@string/action_settings"
android:shortcutShortLabel="@string/action_settings">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="org.itxtech.daedalus.activity.SettingsActivity"
android:targetPackage="org.itxtech.daedalus"/>
</shortcut>
</shortcuts>