Implemented Launcher shortcuts
This commit is contained in:
parent
33a74b2ada
commit
139ed2d77a
@ -47,6 +47,10 @@
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts"/>
|
||||
</activity>
|
||||
<activity
|
||||
android:name=".activity.SettingsActivity"
|
||||
|
@ -1,9 +1,20 @@
|
||||
package org.itxtech.daedalus;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
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 java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -17,6 +28,8 @@ import java.util.List;
|
||||
* the Free Software Foundation, version 3.
|
||||
*/
|
||||
public class Daedalus extends Application {
|
||||
public static final String SHORTCUT_ID_ACTIVATE = "activate";
|
||||
|
||||
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("1", "120.27.103.230", R.string.server_east_china));
|
||||
@ -47,6 +60,31 @@ public class Daedalus extends Application {
|
||||
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() {
|
||||
return instance;
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package org.itxtech.daedalus.activity;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
@ -14,10 +13,13 @@ import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import org.itxtech.daedalus.Daedalus;
|
||||
import org.itxtech.daedalus.R;
|
||||
import org.itxtech.daedalus.service.DaedalusVpnService;
|
||||
import org.itxtech.daedalus.util.DnsServer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Daedalus Project
|
||||
*
|
||||
@ -29,6 +31,11 @@ import org.itxtech.daedalus.util.DnsServer;
|
||||
* the Free Software Foundation, version 3.
|
||||
*/
|
||||
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 SharedPreferences prefs;
|
||||
|
||||
@ -67,13 +74,14 @@ public class MainActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (isServiceActivated()) {
|
||||
but.setText(R.string.button_text_activate);
|
||||
deactivateService();
|
||||
} else {
|
||||
activateService();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
updateUserInterface(getIntent());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -84,17 +92,62 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onRestart() {
|
||||
super.onRestart();
|
||||
protected void onNewIntent(Intent intent) {
|
||||
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()) {
|
||||
but.setText(R.string.button_text_deactivate);
|
||||
} else {
|
||||
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() {
|
||||
|
@ -7,6 +7,7 @@ import android.content.SharedPreferences;
|
||||
import android.net.VpnService;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
import org.itxtech.daedalus.Daedalus;
|
||||
import org.itxtech.daedalus.R;
|
||||
import org.itxtech.daedalus.service.DaedalusVpnService;
|
||||
import org.itxtech.daedalus.util.DnsServer;
|
||||
@ -40,5 +41,7 @@ public class BootBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
Log.d("DBootRecv", "Triggered boot receiver");
|
||||
}
|
||||
|
||||
Daedalus.updateShortcut(context);
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import android.system.StructPollfd;
|
||||
import android.util.Log;
|
||||
import de.measite.minidns.DNSMessage;
|
||||
import de.measite.minidns.util.InetAddressUtil;
|
||||
import org.itxtech.daedalus.Daedalus;
|
||||
import org.itxtech.daedalus.R;
|
||||
import org.itxtech.daedalus.activity.MainActivity;
|
||||
import org.itxtech.daedalus.activity.SettingsActivity;
|
||||
@ -122,6 +123,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
|
||||
this.running = true;
|
||||
this.mThread.start();
|
||||
}
|
||||
Daedalus.updateShortcut(this);
|
||||
return START_STICKY;
|
||||
case ACTION_DEACTIVATE:
|
||||
stopThread();
|
||||
@ -163,8 +165,15 @@ public class DaedalusVpnService extends VpnService implements Runnable {
|
||||
Log.d(TAG, e.toString());
|
||||
}
|
||||
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
|
||||
public void onRevoke() {
|
||||
stopThread();
|
||||
|
BIN
app/src/main/res/mipmap-hdpi/ic_security_black_18dp.png
Normal file
BIN
app/src/main/res/mipmap-hdpi/ic_security_black_18dp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 443 B |
BIN
app/src/main/res/mipmap-mdpi/ic_security_black_18dp.png
Normal file
BIN
app/src/main/res/mipmap-mdpi/ic_security_black_18dp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 324 B |
BIN
app/src/main/res/mipmap-xhdpi/ic_security_black_18dp.png
Normal file
BIN
app/src/main/res/mipmap-xhdpi/ic_security_black_18dp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 523 B |
BIN
app/src/main/res/mipmap-xxhdpi/ic_security_black_18dp.png
Normal file
BIN
app/src/main/res/mipmap-xxhdpi/ic_security_black_18dp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 675 B |
BIN
app/src/main/res/mipmap-xxxhdpi/ic_security_black_18dp.png
Normal file
BIN
app/src/main/res/mipmap-xxxhdpi/ic_security_black_18dp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 840 B |
15
app/src/main/res/xml/shortcuts.xml
Normal file
15
app/src/main/res/xml/shortcuts.xml
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user