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/StatusChanger.java

60 lines
2.3 KiB

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();
}
}