summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/receiver/PlayerWidget.java
blob: c6c65f6da708d08312f4b8ad2df0efd05bf957cd (plain)
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
package de.danoeh.antennapod.receiver;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
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";

    // static because there should only ever be one widget...
    // and otherwise it just keeps getting reset when it gets messages
    private static boolean enabled = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "onReceive");
        super.onReceive(context, intent);
        if (!enabled) {
            // do nothing
            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");
        enabled = 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");
        enabled = 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));
    }
}