Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
<intent-filter>
<action android:name="android.intent.action.MANAGE_NETWORK_USAGE" />
<category android:name="android.intent.category.DEFAULT" />
Expand Down Expand Up @@ -291,5 +293,26 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
<activity
android:name="eu.faircode.netguard.ActivityShortcut"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:exported="true"
android:excludeFromRecents="true"
android:noHistory="true">
<intent-filter>
<action android:name="eu.faircode.netguard.SHORTCUT_ON" />
<action android:name="eu.faircode.netguard.SHORTCUT_OFF" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<receiver
android:name="eu.faircode.netguard.ReceiverShortcut"
android:exported="true">
<intent-filter>
<action android:name="eu.faircode.netguard.SHORTCUT_ON" />
<action android:name="eu.faircode.netguard.SHORTCUT_OFF" />
</intent-filter>
</receiver>
</application>
</manifest>
40 changes: 40 additions & 0 deletions app/src/main/java/eu/faircode/netguard/ActivityShortcut.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package eu.faircode.netguard;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import net.kollnig.missioncontrol.R;

public class ActivityShortcut extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String action = getIntent().getAction();
String nextAction = null;
String message = null;

if ("eu.faircode.netguard.SHORTCUT_ON".equals(action)) {
nextAction = WidgetAdmin.INTENT_ON;
message = getString(R.string.shortcut_on);
} else if ("eu.faircode.netguard.SHORTCUT_OFF".equals(action)) {
nextAction = WidgetAdmin.INTENT_OFF;
message = getString(R.string.shortcut_off);
}

if (nextAction != null) {
Intent toggleIntent = new Intent(nextAction);
toggleIntent.setPackage(getPackageName());
sendBroadcast(toggleIntent);

if (message != null) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}

finish();
}
}
45 changes: 45 additions & 0 deletions app/src/main/java/eu/faircode/netguard/ReceiverShortcut.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package eu.faircode.netguard;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

import androidx.preference.PreferenceManager;

import net.kollnig.missioncontrol.R;

public class ReceiverShortcut extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String nextAction = null;
String message = null;

if ("eu.faircode.netguard.SHORTCUT_ON".equals(action)) {
nextAction = WidgetAdmin.INTENT_ON;
message = context.getString(R.string.shortcut_on);
} else if ("eu.faircode.netguard.SHORTCUT_OFF".equals(action)) {
nextAction = WidgetAdmin.INTENT_OFF;
message = context.getString(R.string.shortcut_off);
} else {
// Fallback to toggle if needed
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean enabled = prefs.getBoolean("enabled", false);
nextAction = (enabled ? WidgetAdmin.INTENT_OFF : WidgetAdmin.INTENT_ON);
message = context.getString(enabled ? R.string.shortcut_off : R.string.shortcut_on);
}

if (nextAction != null) {
Intent toggleIntent = new Intent(nextAction);
toggleIntent.setPackage(context.getPackageName());
context.sendBroadcast(toggleIntent);

if (message != null) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
}
}
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -652,4 +652,7 @@ Sincerely,\n\n]]></string>
<string name="msg_invalid_url">Invalid URL</string>
<string name="msg_apply_blocklists_failed">Failed to apply host files</string>
<string name="msg_last_update">Last update: %s</string>

<string name="shortcut_on">Protection ON</string>
<string name="shortcut_off">Protection OFF</string>
</resources>
23 changes: 23 additions & 0 deletions app/src/main/res/xml/shortcuts.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="protection_on"
android:enabled="true"
android:icon="@drawable/ic_rocket_white"
android:shortcutShortLabel="@string/shortcut_on"
android:shortcutLongLabel="@string/shortcut_on">
<intent
android:action="eu.faircode.netguard.SHORTCUT_ON"
android:targetClass="eu.faircode.netguard.ActivityShortcut" />
</shortcut>
<shortcut
android:shortcutId="protection_off"
android:enabled="true"
android:icon="@drawable/ic_rocket_white"
android:shortcutShortLabel="@string/shortcut_off"
android:shortcutLongLabel="@string/shortcut_off">
<intent
android:action="eu.faircode.netguard.SHORTCUT_OFF"
android:targetClass="eu.faircode.netguard.ActivityShortcut" />
</shortcut>
</shortcuts>
Loading