summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/service/PlayerWidgetService.java
blob: b5bfb1ae4ad9a62f9b2a5bf47690cffeb4dd7540 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package de.danoeh.antennapod.service;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RemoteViews;

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.receiver.MediaButtonReceiver;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.fragment.QueueFragment;
import de.danoeh.antennapod.receiver.PlayerWidget;

/**
 * Updates the state of the player widget
 */
public class PlayerWidgetService extends Service {
    private static final String TAG = "PlayerWidgetService";

    private PlaybackService playbackService;

    /**
     * Controls write access to playbackservice reference
     */
    private Object psLock;

    /**
     * True while service is updating the widget
     */
    private volatile boolean isUpdating;

    public PlayerWidgetService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "Service created");
        isUpdating = false;
        psLock = new Object();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "Service is about to be destroyed");
        if (playbackService != null) {
            Playable playable = playbackService.getPlayable();
            if (playable != null && playable instanceof FeedMedia) {
                FeedMedia media = (FeedMedia) playable;
                if (media.hasAlmostEnded()) {
                    Log.d(TAG, "smart mark as read");
                    FeedItem item = media.getItem();
                    DBWriter.markItemPlayed(item, FeedItem.PLAYED, false);
                    DBWriter.removeQueueItem(this, item, false);
                    DBWriter.addItemToPlaybackHistory(media);
                    if (item.getFeed().getPreferences().getCurrentAutoDelete() &&
                            (!item.isTagged(FeedItem.TAG_FAVORITE) || !UserPreferences.shouldFavoriteKeepEpisode())) {
                        Log.d(TAG, "Delete " + media.toString());
                        DBWriter.deleteFeedMediaOfItem(this, media.getId());
                    }
                }
            }
        }

        try {
            unbindService(mConnection);
        } catch (IllegalArgumentException e) {
            Log.w(TAG, "IllegalArgumentException when trying to unbind service");
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (!isUpdating) {
            if (playbackService == null && PlaybackService.isRunning) {
                bindService(new Intent(this, PlaybackService.class),
                        mConnection, 0);
            } else {
                startViewUpdaterIfNotRunning();
            }
        } else {
            Log.d(TAG, "Service was called while updating. Ignoring update request");
        }
        return Service.START_NOT_STICKY;
    }

    private void updateViews() {
        isUpdating = true;

        ComponentName playerWidget = new ComponentName(this, PlayerWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        RemoteViews views = new RemoteViews(getPackageName(),
                R.layout.player_widget);
        PendingIntent startMediaplayer = PendingIntent.getActivity(this, 0,
                PlaybackService.getPlayerActivityIntent(this), 0);

        Intent startApp = new Intent(getBaseContext(), MainActivity.class);
        startApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startApp.putExtra(MainActivity.EXTRA_FRAGMENT_TAG, QueueFragment.TAG);
        PendingIntent startAppPending = PendingIntent.getActivity(getBaseContext(), 0, startApp, PendingIntent.FLAG_UPDATE_CURRENT);

        boolean nothingPlaying = false;
        if (playbackService != null) {
            final Playable media = playbackService.getPlayable();
            if (media != null) {
                PlayerStatus status = playbackService.getStatus();
                views.setOnClickPendingIntent(R.id.layout_left, startMediaplayer);

                views.setTextViewText(R.id.txtvTitle, media.getEpisodeTitle());

                String progressString = getProgressString();
                if (progressString != null) {
                    views.setViewVisibility(R.id.txtvProgress, View.VISIBLE);
                    views.setTextViewText(R.id.txtvProgress, progressString);
                }

                if (status == PlayerStatus.PLAYING) {
                    views.setImageViewResource(R.id.butPlay, R.drawable.ic_pause_white_24dp);
                    if (Build.VERSION.SDK_INT >= 15) {
                        views.setContentDescription(R.id.butPlay, getString(R.string.pause_label));
                    }
                } else {
                    views.setImageViewResource(R.id.butPlay, R.drawable.ic_play_arrow_white_24dp);
                    if (Build.VERSION.SDK_INT >= 15) {
                        views.setContentDescription(R.id.butPlay, getString(R.string.play_label));
                    }
                }
                views.setOnClickPendingIntent(R.id.butPlay,
                        createMediaButtonIntent());
            } else {
                nothingPlaying = true;
            }
        } else {
            nothingPlaying = true;
        }

        if (nothingPlaying) {
            // start the app if they click anything
            views.setOnClickPendingIntent(R.id.layout_left, startAppPending);
            views.setOnClickPendingIntent(R.id.butPlay, startAppPending);
            views.setViewVisibility(R.id.txtvProgress, View.INVISIBLE);
            views.setTextViewText(R.id.txtvTitle,
                    this.getString(R.string.no_media_playing_label));
            views.setImageViewResource(R.id.butPlay, R.drawable.ic_play_arrow_white_24dp);
        }

        manager.updateAppWidget(playerWidget, views);
        isUpdating = false;
    }

    /**
     * Creates an intent which fakes a mediabutton press
     */
    private PendingIntent createMediaButtonIntent() {
        KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN,
                KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
        Intent startingIntent = new Intent(
                MediaButtonReceiver.NOTIFY_BUTTON_RECEIVER);
        startingIntent.putExtra(Intent.EXTRA_KEY_EVENT, event);

        return PendingIntent.getBroadcast(this, 0, startingIntent, 0);
    }

    private String getProgressString() {
        int position = playbackService.getCurrentPosition();
        int duration = playbackService.getDuration();
        if (position > 0 && duration > 0) {
            return Converter.getDurationStringLong(position) + " / "
                    + Converter.getDurationStringLong(duration);
        } else {
            return null;
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            Log.d(TAG, "Connection to service established");
            synchronized (psLock) {
                if(service instanceof PlaybackService.LocalBinder) {
                    playbackService = ((PlaybackService.LocalBinder) service).getService();
                    startViewUpdaterIfNotRunning();
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            synchronized (psLock) {
                playbackService = null;
                Log.d(TAG, "Disconnected from service");
            }
        }

    };

    private void startViewUpdaterIfNotRunning() {
        if (!isUpdating) {
            ViewUpdater updateThread = new ViewUpdater(this);
            updateThread.start();
        }
    }

    class ViewUpdater extends Thread {
        private static final String THREAD_NAME = "ViewUpdater";
        private PlayerWidgetService service;

        public ViewUpdater(PlayerWidgetService service) {
            super();
            setName(THREAD_NAME);
            this.service = service;

        }

        @Override
        public void run() {
            synchronized (psLock) {
                service.updateViews();
            }
        }

    }

}