Skip to content

Commit 043db77

Browse files
committed
Add shortcut
1 parent 68fcd53 commit 043db77

5 files changed

Lines changed: 134 additions & 0 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
<action android:name="android.intent.action.MAIN" />
8080
<category android:name="android.intent.category.LAUNCHER" />
8181
</intent-filter>
82+
<meta-data android:name="android.app.shortcuts"
83+
android:resource="@xml/shortcuts" />
8284
<intent-filter>
8385
<action android:name="android.intent.action.MANAGE_NETWORK_USAGE" />
8486
<category android:name="android.intent.category.DEFAULT" />
@@ -291,5 +293,26 @@
291293
android:name="android.support.FILE_PROVIDER_PATHS"
292294
android:resource="@xml/provider_paths" />
293295
</provider>
296+
<activity
297+
android:name="eu.faircode.netguard.ActivityShortcut"
298+
android:theme="@android:style/Theme.Translucent.NoTitleBar"
299+
android:exported="true"
300+
android:excludeFromRecents="true"
301+
android:noHistory="true">
302+
<intent-filter>
303+
<action android:name="eu.faircode.netguard.SHORTCUT_ON" />
304+
<action android:name="eu.faircode.netguard.SHORTCUT_OFF" />
305+
<category android:name="android.intent.category.DEFAULT" />
306+
</intent-filter>
307+
</activity>
308+
309+
<receiver
310+
android:name="eu.faircode.netguard.ReceiverShortcut"
311+
android:exported="true">
312+
<intent-filter>
313+
<action android:name="eu.faircode.netguard.SHORTCUT_ON" />
314+
<action android:name="eu.faircode.netguard.SHORTCUT_OFF" />
315+
</intent-filter>
316+
</receiver>
294317
</application>
295318
</manifest>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package eu.faircode.netguard;
2+
3+
import android.app.Activity;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
import android.widget.Toast;
7+
8+
import net.kollnig.missioncontrol.R;
9+
10+
public class ActivityShortcut extends Activity {
11+
12+
@Override
13+
protected void onCreate(Bundle savedInstanceState) {
14+
super.onCreate(savedInstanceState);
15+
16+
String action = getIntent().getAction();
17+
String nextAction = null;
18+
String message = null;
19+
20+
if ("eu.faircode.netguard.SHORTCUT_ON".equals(action)) {
21+
nextAction = WidgetAdmin.INTENT_ON;
22+
message = getString(R.string.shortcut_on);
23+
} else if ("eu.faircode.netguard.SHORTCUT_OFF".equals(action)) {
24+
nextAction = WidgetAdmin.INTENT_OFF;
25+
message = getString(R.string.shortcut_off);
26+
}
27+
28+
if (nextAction != null) {
29+
Intent toggleIntent = new Intent(nextAction);
30+
toggleIntent.setPackage(getPackageName());
31+
sendBroadcast(toggleIntent);
32+
33+
if (message != null) {
34+
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
35+
}
36+
}
37+
38+
finish();
39+
}
40+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package eu.faircode.netguard;
2+
3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.content.SharedPreferences;
7+
import android.widget.Toast;
8+
9+
import androidx.preference.PreferenceManager;
10+
11+
import net.kollnig.missioncontrol.R;
12+
13+
public class ReceiverShortcut extends BroadcastReceiver {
14+
15+
@Override
16+
public void onReceive(Context context, Intent intent) {
17+
String action = intent.getAction();
18+
String nextAction = null;
19+
String message = null;
20+
21+
if ("eu.faircode.netguard.SHORTCUT_ON".equals(action)) {
22+
nextAction = WidgetAdmin.INTENT_ON;
23+
message = context.getString(R.string.shortcut_on);
24+
} else if ("eu.faircode.netguard.SHORTCUT_OFF".equals(action)) {
25+
nextAction = WidgetAdmin.INTENT_OFF;
26+
message = context.getString(R.string.shortcut_off);
27+
} else {
28+
// Fallback to toggle if needed
29+
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
30+
boolean enabled = prefs.getBoolean("enabled", false);
31+
nextAction = (enabled ? WidgetAdmin.INTENT_OFF : WidgetAdmin.INTENT_ON);
32+
message = context.getString(enabled ? R.string.shortcut_off : R.string.shortcut_on);
33+
}
34+
35+
if (nextAction != null) {
36+
Intent toggleIntent = new Intent(nextAction);
37+
toggleIntent.setPackage(context.getPackageName());
38+
context.sendBroadcast(toggleIntent);
39+
40+
if (message != null) {
41+
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
42+
}
43+
}
44+
}
45+
}

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,4 +652,7 @@ Sincerely,\n\n]]></string>
652652
<string name="msg_invalid_url">Invalid URL</string>
653653
<string name="msg_apply_blocklists_failed">Failed to apply host files</string>
654654
<string name="msg_last_update">Last update: %s</string>
655+
656+
<string name="shortcut_on">Protection ON</string>
657+
<string name="shortcut_off">Protection OFF</string>
655658
</resources>

app/src/main/res/xml/shortcuts.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
3+
<shortcut
4+
android:shortcutId="protection_on"
5+
android:enabled="true"
6+
android:icon="@drawable/ic_rocket_white"
7+
android:shortcutShortLabel="@string/shortcut_on"
8+
android:shortcutLongLabel="@string/shortcut_on">
9+
<intent
10+
android:action="eu.faircode.netguard.SHORTCUT_ON"
11+
android:targetClass="eu.faircode.netguard.ActivityShortcut" />
12+
</shortcut>
13+
<shortcut
14+
android:shortcutId="protection_off"
15+
android:enabled="true"
16+
android:icon="@drawable/ic_rocket_white"
17+
android:shortcutShortLabel="@string/shortcut_off"
18+
android:shortcutLongLabel="@string/shortcut_off">
19+
<intent
20+
android:action="eu.faircode.netguard.SHORTCUT_OFF"
21+
android:targetClass="eu.faircode.netguard.ActivityShortcut" />
22+
</shortcut>
23+
</shortcuts>

0 commit comments

Comments
 (0)