Initial import. Version 1.0
This commit is contained in:
commit
97afaf3667
21 changed files with 585 additions and 0 deletions
12
src/org/r3pek/k9datakiller/Constants.java
Normal file
12
src/org/r3pek/k9datakiller/Constants.java
Normal file
|
@ -0,0 +1,12 @@
|
|||
package org.r3pek.k9datakiller;
|
||||
|
||||
public final class Constants {
|
||||
/* Preferences */
|
||||
public static final String PREF_WIDGET_STATUS = "org.r3pek.k9datakiller.k9syncenable";
|
||||
|
||||
/* Intents */
|
||||
public static final String INTENT_K9_SERVICE = "com.fsck.k9.K9RemoteControl.set";
|
||||
public static final String INTENT_K9_SERVICE_EXTRA = "com.fsck.k9.K9RemoteControl.backgroundOperations";
|
||||
public static final String INTENT_K9DATAKILLER_CHANGESTATUS = "org.r3pek.k9datakiller.CHANGE_STATUS";
|
||||
public static final String INTENT_K9DATAKILLER_STATUSCHANGED = "org.r3pek.k9datakiller.STATUS_CHANGED";
|
||||
}
|
55
src/org/r3pek/k9datakiller/K9DataKillerWidget.java
Normal file
55
src/org/r3pek/k9datakiller/K9DataKillerWidget.java
Normal file
|
@ -0,0 +1,55 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
60
src/org/r3pek/k9datakiller/StatusChanger.java
Normal file
60
src/org/r3pek/k9datakiller/StatusChanger.java
Normal file
|
@ -0,0 +1,60 @@
|
|||
package org.r3pek.k9datakiller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class StatusChanger extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getAction().equals(Constants.INTENT_K9DATAKILLER_CHANGESTATUS))
|
||||
changeStatus(context);
|
||||
}
|
||||
|
||||
private static boolean isReceiverAvailable(Context context, String action) {
|
||||
final PackageManager packageManager = context.getPackageManager();
|
||||
final Intent intent = new Intent(action);
|
||||
List<ResolveInfo> list = packageManager.queryBroadcastReceivers(intent, 0);
|
||||
return list.size() > 0;
|
||||
}
|
||||
|
||||
private void changeStatus(Context context) {
|
||||
/* Check if K-9 is installed */
|
||||
if (!isReceiverAvailable(context, Constants.INTENT_K9_SERVICE)) {
|
||||
Toast.makeText(context, context.getString(R.string.k9notinstalled), Toast.LENGTH_LONG).show();
|
||||
return;
|
||||
}
|
||||
|
||||
/* Get status from preferences as K-9 doesn't support reading the status */
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
boolean syncEnable = prefs.getBoolean(Constants.PREF_WIDGET_STATUS, true);
|
||||
|
||||
/* Ask K-9 to change the setting */
|
||||
Intent i = new Intent(Constants.INTENT_K9_SERVICE);
|
||||
i.putExtra(Constants.INTENT_K9_SERVICE_EXTRA, syncEnable ? "NEVER" : "WHEN_CHECKED");
|
||||
context.sendBroadcast(i);
|
||||
|
||||
/* Update our preferences to reflect the new value */
|
||||
syncEnable = !syncEnable;
|
||||
Editor ed = prefs.edit();
|
||||
ed.putBoolean(Constants.PREF_WIDGET_STATUS, syncEnable);
|
||||
ed.commit();
|
||||
|
||||
/* Notify our widget that the changed has been made */
|
||||
i = new Intent(Constants.INTENT_K9DATAKILLER_STATUSCHANGED);
|
||||
context.sendBroadcast(i);
|
||||
|
||||
/* Tell the user about it */
|
||||
String status = syncEnable ? context.getString(R.string.enabled) : context.getString(R.string.disabled);
|
||||
Toast.makeText(context, context.getString(R.string.toast_text) + " " + status, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue