1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
K9DataKiller/src/org/r3pek/k9datakiller/K9DataKillerWidget.java

55 lines
2.4 KiB

package org.r3pek.k9datakiller;
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;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;
public class K9DataKillerWidget extends AppWidgetProvider {
private static final String WIDGET_STATUS = "org.r3pek.k9datakiller.k9syncenable";
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals("org.r3pek.k9datakiller.STATUS_CHANGED")) {
/* Status updated. Update the Widget */
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, K9DataKillerWidget.class));
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean syncEnable = prefs.getBoolean(WIDGET_STATUS, true);
appWidgetManager.updateAppWidget(appWidgetIds, getRemoteViews(context, appWidgetManager, appWidgetIds, syncEnable));
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
/* Get status from preferences */
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean syncEnable = prefs.getBoolean(WIDGET_STATUS, true);
/* Update the widget */
appWidgetManager.updateAppWidget(appWidgetIds, getRemoteViews(context, appWidgetManager, appWidgetIds, syncEnable));
}
private RemoteViews getRemoteViews(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, boolean syncEnable) {
/* Update the image */
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setImageViewResource(R.id.button, syncEnable ? R.drawable.icon_on : R.drawable.icon_off);
/* Set the onClickListenner */
Intent i = new Intent("org.r3pek.k9datakiller.CHANGE_STATUS");
PendingIntent intent = PendingIntent.getBroadcast(context, 0, i, 0);
views.setOnClickPendingIntent(R.id.button, intent);
return views;
}
}