2015-11-07 09:19:47 +00:00
|
|
|
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/>.
|
|
|
|
|
2016-01-01 13:52:09 +00:00
|
|
|
Copyright 2015-2016 by Marcel Bokhorst (M66B)
|
2015-11-07 09:19:47 +00:00
|
|
|
*/
|
|
|
|
|
2015-12-03 13:05:38 +00:00
|
|
|
import android.app.AlarmManager;
|
2015-11-07 09:19:47 +00:00
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
import android.appwidget.AppWidgetProvider;
|
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2015-11-07 10:08:30 +00:00
|
|
|
import android.net.VpnService;
|
2015-12-03 14:39:01 +00:00
|
|
|
import android.os.Build;
|
2015-11-07 09:19:47 +00:00
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.widget.RemoteViews;
|
|
|
|
|
2015-12-03 13:05:38 +00:00
|
|
|
import java.util.Date;
|
|
|
|
|
2015-11-07 09:19:47 +00:00
|
|
|
public class Widget extends AppWidgetProvider {
|
|
|
|
private static final String TAG = "NetGuard.Widget";
|
|
|
|
|
|
|
|
private 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);
|
2015-11-20 10:34:23 +00:00
|
|
|
Util.logExtras(intent);
|
2015-11-07 09:19:47 +00:00
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
2015-12-03 13:05:38 +00:00
|
|
|
// 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);
|
|
|
|
|
2015-11-07 09:19:47 +00:00
|
|
|
if (INTENT_OFF.equals(intent.getAction())) {
|
|
|
|
prefs.edit().putBoolean("enabled", false).apply();
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.stop("widget", context);
|
2015-11-07 09:19:47 +00:00
|
|
|
|
2015-12-03 13:05:38 +00:00
|
|
|
// Auto enable
|
|
|
|
int auto = Integer.parseInt(prefs.getString("auto_enable", "0"));
|
|
|
|
if (auto > 0) {
|
|
|
|
Log.i(TAG, "Scheduling enabled after minutes=" + auto);
|
2015-12-03 14:39:01 +00:00
|
|
|
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);
|
2015-12-03 13:05:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 12:32:17 +00:00
|
|
|
} else if (INTENT_ON.equals(intent.getAction()))
|
|
|
|
try {
|
2016-01-10 09:14:32 +00:00
|
|
|
prefs.edit().putBoolean("enabled", true).apply();
|
2016-03-11 06:47:52 +00:00
|
|
|
ServiceSinkhole.start("widget", context);
|
2015-11-27 12:32:17 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
Util.sendCrashReport(ex, context);
|
2015-11-24 15:33:18 +00:00
|
|
|
}
|
2015-11-07 09:19:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void update(int[] appWidgetIds, AppWidgetManager appWidgetManager, Context context) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean enabled = prefs.getBoolean("enabled", false);
|
2015-11-07 10:08:30 +00:00
|
|
|
|
2015-11-22 19:30:29 +00:00
|
|
|
try {
|
2015-11-27 12:44:22 +00:00
|
|
|
try {
|
2016-01-10 09:14:32 +00:00
|
|
|
PendingIntent pi;
|
2015-11-27 12:44:22 +00:00
|
|
|
if (VpnService.prepare(context) == null)
|
|
|
|
pi = PendingIntent.getBroadcast(context, 0, new Intent(enabled ? INTENT_OFF : INTENT_ON), PendingIntent.FLAG_UPDATE_CURRENT);
|
2016-01-10 09:14:32 +00:00
|
|
|
else
|
|
|
|
pi = PendingIntent.getActivity(context, 0, new Intent(context, ActivityMain.class), PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
for (int id : appWidgetIds) {
|
|
|
|
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
|
|
|
|
views.setOnClickPendingIntent(R.id.ivEnabled, pi);
|
|
|
|
views.setImageViewResource(R.id.ivEnabled, enabled ? R.mipmap.ic_launcher : R.drawable.ic_security_white_24dp_60);
|
|
|
|
appWidgetManager.updateAppWidget(id, views);
|
|
|
|
}
|
2015-11-27 12:44:22 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
Util.sendCrashReport(ex, context);
|
|
|
|
}
|
2015-11-22 19:30:29 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
|
|
|
|
Util.sendCrashReport(ex, context);
|
2015-11-07 09:19:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateWidgets(Context context) {
|
|
|
|
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
|
|
|
int appWidgetIds[] = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, Widget.class));
|
|
|
|
update(appWidgetIds, appWidgetManager, context);
|
|
|
|
}
|
|
|
|
}
|