1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package de.danoeh.antennapod.receiver;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import org.apache.commons.lang3.StringUtils;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.service.PlayerWidgetService;
public class PlayerWidget extends AppWidgetProvider {
private static final String TAG = "PlayerWidget";
private static final String PREFS_NAME = "PlayerWidgetPrefs";
private static final String KEY_ENABLED = "WidgetEnabled";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
super.onReceive(context, intent);
// don't do anything if we're not enabled
if (!isEnabled(context)) {
return;
}
// these come from the PlaybackService when things should get updated
if (StringUtils.equals(intent.getAction(), PlaybackService.FORCE_WIDGET_UPDATE)) {
startUpdate(context);
} else if (StringUtils.equals(intent.getAction(), PlaybackService.STOP_WIDGET_UPDATE)) {
stopUpdate(context);
}
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.d(TAG, "Widget enabled");
setEnabled(context, true);
startUpdate(context);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.d(TAG, "onUpdate() called with: " + "context = [" + context + "], appWidgetManager = [" + appWidgetManager + "], appWidgetIds = [" + appWidgetIds + "]");
startUpdate(context);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.d(TAG, "Widet disabled");
setEnabled(context, false);
stopUpdate(context);
}
private void startUpdate(Context context) {
Log.d(TAG, "startUpdate() called with: " + "context = [" + context + "]");
context.startService(new Intent(context, PlayerWidgetService.class));
}
private void stopUpdate(Context context) {
Log.d(TAG, "stopUpdate() called with: " + "context = [" + context + "]");
context.stopService(new Intent(context, PlayerWidgetService.class));
}
private boolean isEnabled(Context context) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
return prefs.getBoolean(KEY_ENABLED, false);
}
private void setEnabled(Context context, boolean enabled) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
prefs.edit().putBoolean(KEY_ENABLED, enabled);
}
}
|