Search in sources :

Example 16 with EpisodeAction

use of de.danoeh.antennapod.net.sync.model.EpisodeAction in project AntennaPod by AntennaPod.

the class SynchronizationQueueStorage method getQueuedEpisodeActions.

public ArrayList<EpisodeAction> getQueuedEpisodeActions() {
    ArrayList<EpisodeAction> actions = new ArrayList<>();
    try {
        String json = getSharedPreferences().getString(QUEUED_EPISODE_ACTIONS, "[]");
        JSONArray queue = new JSONArray(json);
        for (int i = 0; i < queue.length(); i++) {
            actions.add(EpisodeAction.readFromJsonObject(queue.getJSONObject(i)));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return actions;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) EpisodeAction(de.danoeh.antennapod.net.sync.model.EpisodeAction)

Example 17 with EpisodeAction

use of de.danoeh.antennapod.net.sync.model.EpisodeAction in project AntennaPod by AntennaPod.

the class MediaDownloadedHandler method run.

@Override
public void run() {
    updatedStatus = status;
    FeedMedia media = DBReader.getFeedMedia(request.getFeedfileId());
    if (media == null) {
        Log.e(TAG, "Could not find downloaded media object in database");
        return;
    }
    // media.setDownloaded modifies played state
    boolean broadcastUnreadStateUpdate = media.getItem() != null && media.getItem().isNew();
    media.setDownloaded(true);
    media.setFile_url(request.getDestination());
    media.setSize(new File(request.getDestination()).length());
    // enforce check
    media.checkEmbeddedPicture();
    // check if file has chapters
    if (media.getItem() != null && !media.getItem().hasChapters()) {
        media.setChapters(ChapterUtils.loadChaptersFromMediaFile(media, context));
    }
    // Get duration
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    String durationStr = null;
    try {
        mmr.setDataSource(media.getFile_url());
        durationStr = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        media.setDuration(Integer.parseInt(durationStr));
        Log.d(TAG, "Duration of file is " + media.getDuration());
    } catch (NumberFormatException e) {
        Log.d(TAG, "Invalid file duration: " + durationStr);
    } catch (Exception e) {
        Log.e(TAG, "Get duration failed", e);
    } finally {
        mmr.release();
    }
    final FeedItem item = media.getItem();
    try {
        DBWriter.setFeedMedia(media).get();
        // we've received the media, we don't want to autodownload it again
        if (item != null) {
            item.disableAutoDownload();
            // setFeedItem() signals (via EventBus) that the item has been updated,
            // so we do it after the enclosing media has been updated above,
            // to ensure subscribers will get the updated FeedMedia as well
            DBWriter.setFeedItem(item).get();
            if (broadcastUnreadStateUpdate) {
                EventBus.getDefault().post(new UnreadItemsUpdateEvent());
            }
        }
    } catch (InterruptedException e) {
        Log.e(TAG, "MediaHandlerThread was interrupted");
    } catch (ExecutionException e) {
        Log.e(TAG, "ExecutionException in MediaHandlerThread: " + e.getMessage());
        updatedStatus = new DownloadStatus(media, media.getEpisodeTitle(), DownloadError.ERROR_DB_ACCESS_ERROR, false, e.getMessage(), request.isInitiatedByUser());
    }
    if (item != null) {
        EpisodeAction action = new EpisodeAction.Builder(item, EpisodeAction.DOWNLOAD).currentTimestamp().build();
        SynchronizationQueueSink.enqueueEpisodeActionIfSynchronizationIsActive(context, action);
    }
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) EpisodeAction(de.danoeh.antennapod.net.sync.model.EpisodeAction) FeedItem(de.danoeh.antennapod.model.feed.FeedItem) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) UnreadItemsUpdateEvent(de.danoeh.antennapod.event.UnreadItemsUpdateEvent) MediaMetadataRetriever(android.media.MediaMetadataRetriever) DownloadStatus(de.danoeh.antennapod.core.service.download.DownloadStatus) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Aggregations

EpisodeAction (de.danoeh.antennapod.net.sync.model.EpisodeAction)17 ArrayList (java.util.ArrayList)8 Pair (androidx.core.util.Pair)7 FeedItem (de.danoeh.antennapod.model.feed.FeedItem)5 Date (java.util.Date)5 SimpleDateFormat (java.text.SimpleDateFormat)4 JSONArray (org.json.JSONArray)4 FeedMedia (de.danoeh.antennapod.model.feed.FeedMedia)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 DownloadStatus (de.danoeh.antennapod.core.service.download.DownloadStatus)2 EpisodeActionChanges (de.danoeh.antennapod.net.sync.model.EpisodeActionChanges)2 SyncServiceException (de.danoeh.antennapod.net.sync.model.SyncServiceException)2 File (java.io.File)2 MalformedURLException (java.net.MalformedURLException)2 ExecutionException (java.util.concurrent.ExecutionException)2 RequestBody (okhttp3.RequestBody)2 Context (android.content.Context)1 MediaMetadataRetriever (android.media.MediaMetadataRetriever)1 NonNull (androidx.annotation.NonNull)1