mirror of
https://github.com/M66B/NetGuard.git
synced 2025-03-15 08:29:02 +00:00
Protect widget intents with permission
This commit is contained in:
parent
f5014c34d0
commit
92ce19211a
4 changed files with 103 additions and 56 deletions
|
@ -13,6 +13,14 @@
|
|||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<!-- http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous -->
|
||||
|
||||
<permission
|
||||
android:name="eu.faircode.netguard.permission.ADMIN"
|
||||
android:description="@string/app_description"
|
||||
android:label="@string/app_name"
|
||||
android:protectionLevel="signature" />
|
||||
|
||||
<uses-permission android:name="eu.faircode.netguard.permission.ADMIN" />
|
||||
|
||||
<!-- Firebase -->
|
||||
<uses-permission
|
||||
android:name="com.google.android.c2dm.permission.RECEIVE"
|
||||
|
@ -169,12 +177,20 @@
|
|||
android:label="@string/app_name">
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
||||
<action android:name="eu.faircode.netguard.APPWIDGET_ON" />
|
||||
<action android:name="eu.faircode.netguard.APPWIDGET_OFF" />
|
||||
</intent-filter>
|
||||
<meta-data
|
||||
android:name="android.appwidget.provider"
|
||||
android:resource="@xml/widget" />
|
||||
</receiver>
|
||||
|
||||
<receiver
|
||||
android:name=".WidgetAdmin"
|
||||
android:label="@string/app_name"
|
||||
android:permission="eu.faircode.netguard.permission.ADMIN">
|
||||
<intent-filter>
|
||||
<action android:name="eu.faircode.netguard.APPWIDGET_ON" />
|
||||
<action android:name="eu.faircode.netguard.APPWIDGET_OFF" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
</application>
|
||||
</manifest>
|
||||
|
|
|
@ -69,7 +69,7 @@ public class ServiceTileMain extends TileService implements SharedPreferences.On
|
|||
|
||||
// Cancel set alarm
|
||||
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
||||
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(Widget.INTENT_ON), PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(WidgetAdmin.INTENT_ON), PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
am.cancel(pi);
|
||||
|
||||
// Check state
|
||||
|
|
|
@ -19,7 +19,6 @@ package eu.faircode.netguard;
|
|||
Copyright 2015-2016 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.appwidget.AppWidgetProvider;
|
||||
|
@ -28,69 +27,18 @@ import android.content.Context;
|
|||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.VpnService;
|
||||
import android.os.Build;
|
||||
import android.os.Vibrator;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Widget extends AppWidgetProvider {
|
||||
private static final String TAG = "NetGuard.Widget";
|
||||
|
||||
public static final String INTENT_ON = "eu.faircode.netguard.APPWIDGET_ON";
|
||||
private static final String INTENT_OFF = "eu.faircode.netguard.APPWIDGET_OFF";
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
update(appWidgetIds, appWidgetManager, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
super.onReceive(context, intent);
|
||||
|
||||
Log.i(TAG, "Received " + intent);
|
||||
Util.logExtras(intent);
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
||||
// Cancel set alarm
|
||||
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
PendingIntent pi = PendingIntent.getBroadcast(context, 0, new Intent(INTENT_ON), PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
am.cancel(pi);
|
||||
|
||||
// Vibrate
|
||||
if (INTENT_OFF.equals(intent.getAction()) || INTENT_ON.equals(intent.getAction())) {
|
||||
Vibrator vs = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
|
||||
if (vs.hasVibrator())
|
||||
vs.vibrate(50);
|
||||
}
|
||||
|
||||
if (INTENT_OFF.equals(intent.getAction())) {
|
||||
prefs.edit().putBoolean("enabled", false).apply();
|
||||
ServiceSinkhole.stop("widget", context);
|
||||
|
||||
// Auto enable
|
||||
int auto = Integer.parseInt(prefs.getString("auto_enable", "0"));
|
||||
if (auto > 0) {
|
||||
Log.i(TAG, "Scheduling enabled after minutes=" + auto);
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
|
||||
am.set(AlarmManager.RTC_WAKEUP, new Date().getTime() + auto * 60 * 1000L, pi);
|
||||
else
|
||||
am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, new Date().getTime() + auto * 60 * 1000L, pi);
|
||||
}
|
||||
|
||||
} else if (INTENT_ON.equals(intent.getAction()))
|
||||
try {
|
||||
prefs.edit().putBoolean("enabled", true).apply();
|
||||
ServiceSinkhole.start("widget", context);
|
||||
} catch (Throwable ex) {
|
||||
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
||||
}
|
||||
}
|
||||
|
||||
private static void update(int[] appWidgetIds, AppWidgetManager appWidgetManager, Context context) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
boolean enabled = prefs.getBoolean("enabled", false);
|
||||
|
@ -99,7 +47,7 @@ public class Widget extends AppWidgetProvider {
|
|||
try {
|
||||
PendingIntent pi;
|
||||
if (VpnService.prepare(context) == null)
|
||||
pi = PendingIntent.getBroadcast(context, 0, new Intent(enabled ? INTENT_OFF : INTENT_ON), PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
pi = PendingIntent.getBroadcast(context, 0, new Intent(enabled ? WidgetAdmin.INTENT_OFF : WidgetAdmin.INTENT_ON), PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
else
|
||||
pi = PendingIntent.getActivity(context, 0, new Intent(context, ActivityMain.class), PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
|
|
83
app/src/main/java/eu/faircode/netguard/WidgetAdmin.java
Normal file
83
app/src/main/java/eu/faircode/netguard/WidgetAdmin.java
Normal file
|
@ -0,0 +1,83 @@
|
|||
package eu.faircode.netguard;
|
||||
|
||||
/*
|
||||
This file is part of NetGuard.
|
||||
|
||||
NetGuard is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
NetGuard is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2015-2016 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Build;
|
||||
import android.os.Vibrator;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class WidgetAdmin extends Receiver {
|
||||
private static final String TAG = "NetGuard.Widget";
|
||||
|
||||
public static final String INTENT_ON = "eu.faircode.netguard.APPWIDGET_ON";
|
||||
public static final String INTENT_OFF = "eu.faircode.netguard.APPWIDGET_OFF";
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
super.onReceive(context, intent);
|
||||
|
||||
Log.i(TAG, "Received " + intent);
|
||||
Util.logExtras(intent);
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
||||
// Cancel set alarm
|
||||
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
PendingIntent pi = PendingIntent.getBroadcast(context, 0, new Intent(INTENT_ON), PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
am.cancel(pi);
|
||||
|
||||
// Vibrate
|
||||
if (INTENT_OFF.equals(intent.getAction()) || INTENT_ON.equals(intent.getAction())) {
|
||||
Vibrator vs = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
|
||||
if (vs.hasVibrator())
|
||||
vs.vibrate(50);
|
||||
}
|
||||
|
||||
if (INTENT_OFF.equals(intent.getAction())) {
|
||||
prefs.edit().putBoolean("enabled", false).apply();
|
||||
ServiceSinkhole.stop("widget", context);
|
||||
|
||||
// Auto enable
|
||||
int auto = Integer.parseInt(prefs.getString("auto_enable", "0"));
|
||||
if (auto > 0) {
|
||||
Log.i(TAG, "Scheduling enabled after minutes=" + auto);
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
|
||||
am.set(AlarmManager.RTC_WAKEUP, new Date().getTime() + auto * 60 * 1000L, pi);
|
||||
else
|
||||
am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, new Date().getTime() + auto * 60 * 1000L, pi);
|
||||
}
|
||||
|
||||
} else if (INTENT_ON.equals(intent.getAction()))
|
||||
try {
|
||||
prefs.edit().putBoolean("enabled", true).apply();
|
||||
ServiceSinkhole.start("widget", context);
|
||||
} catch (Throwable ex) {
|
||||
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue