Search in sources :

Example 1 with GpodnetEpisodeAction

use of de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction in project AntennaPod by AntennaPod.

the class DBWriter method deleteFeedMediaOfItem.

/**
     * Deletes a downloaded FeedMedia file from the storage device.
     *
     * @param context A context that is used for opening a database connection.
     * @param mediaId ID of the FeedMedia object whose downloaded file should be deleted.
     */
public static Future<?> deleteFeedMediaOfItem(final Context context, final long mediaId) {
    return dbExec.submit(() -> {
        final FeedMedia media = DBReader.getFeedMedia(mediaId);
        if (media != null) {
            Log.i(TAG, String.format("Requested to delete FeedMedia [id=%d, title=%s, downloaded=%s", media.getId(), media.getEpisodeTitle(), String.valueOf(media.isDownloaded())));
            boolean result = false;
            if (media.isDownloaded()) {
                // delete downloaded media file
                File mediaFile = new File(media.getFile_url());
                if (mediaFile.exists()) {
                    result = mediaFile.delete();
                }
                media.setDownloaded(false);
                media.setFile_url(null);
                media.setHasEmbeddedPicture(false);
                PodDBAdapter adapter = PodDBAdapter.getInstance();
                adapter.open();
                adapter.setMedia(media);
                adapter.close();
                // If media is currently being played, change playback
                // type to 'stream' and shutdown playback service
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                if (PlaybackPreferences.getCurrentlyPlayingMedia() == FeedMedia.PLAYABLE_TYPE_FEEDMEDIA) {
                    if (media.getId() == PlaybackPreferences.getCurrentlyPlayingFeedMediaId()) {
                        SharedPreferences.Editor editor = prefs.edit();
                        editor.putBoolean(PlaybackPreferences.PREF_CURRENT_EPISODE_IS_STREAM, true);
                        editor.commit();
                    }
                    if (PlaybackPreferences.getCurrentlyPlayingFeedMediaId() == media.getId()) {
                        context.sendBroadcast(new Intent(PlaybackService.ACTION_SHUTDOWN_PLAYBACK_SERVICE));
                    }
                }
                // Gpodder: queue delete action for synchronization
                if (GpodnetPreferences.loggedIn()) {
                    FeedItem item = media.getItem();
                    GpodnetEpisodeAction action = new GpodnetEpisodeAction.Builder(item, GpodnetEpisodeAction.Action.DELETE).currentDeviceId().currentTimestamp().build();
                    GpodnetPreferences.enqueueEpisodeAction(action);
                }
            }
            Log.d(TAG, "Deleting File. Result: " + result);
            EventBus.getDefault().post(FeedItemEvent.deletedMedia(Collections.singletonList(media.getItem())));
            EventDistributor.getInstance().sendUnreadItemsUpdateBroadcast();
        }
    });
}
Also used : FeedItem(de.danoeh.antennapod.core.feed.FeedItem) GpodnetEpisodeAction(de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) File(java.io.File)

Example 2 with GpodnetEpisodeAction

use of de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction in project AntennaPod by AntennaPod.

the class GpodnetSyncService method syncEpisodeActions.

private synchronized void syncEpisodeActions() {
    final long timestamp = GpodnetPreferences.getLastEpisodeActionsSyncTimestamp();
    Log.d(TAG, "last episode actions sync timestamp: " + timestamp);
    try {
        GpodnetService service = tryLogin();
        // download episode actions
        GpodnetEpisodeActionGetResponse getResponse = service.getEpisodeChanges(timestamp);
        long lastUpdate = getResponse.getTimestamp();
        Log.d(TAG, "Downloaded episode actions: " + getResponse);
        List<GpodnetEpisodeAction> remoteActions = getResponse.getEpisodeActions();
        List<GpodnetEpisodeAction> localActions = GpodnetPreferences.getQueuedEpisodeActions();
        processEpisodeActions(localActions, remoteActions);
        // upload local actions
        if (localActions.size() > 0) {
            Log.d(TAG, "Uploading episode actions: " + localActions);
            GpodnetEpisodeActionPostResponse postResponse = service.uploadEpisodeActions(localActions);
            lastUpdate = postResponse.timestamp;
            Log.d(TAG, "Upload episode response: " + postResponse);
            GpodnetPreferences.removeQueuedEpisodeActions(localActions);
        }
        GpodnetPreferences.setLastEpisodeActionsSyncTimestamp(lastUpdate);
        GpodnetPreferences.setLastSyncAttempt(true, System.currentTimeMillis());
        clearErrorNotifications();
    } catch (GpodnetServiceException e) {
        e.printStackTrace();
        updateErrorNotification(e);
    } catch (DownloadRequestException e) {
        e.printStackTrace();
    }
}
Also used : GpodnetServiceException(de.danoeh.antennapod.core.gpoddernet.GpodnetServiceException) GpodnetService(de.danoeh.antennapod.core.gpoddernet.GpodnetService) GpodnetEpisodeAction(de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction) GpodnetEpisodeActionGetResponse(de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeActionGetResponse) DownloadRequestException(de.danoeh.antennapod.core.storage.DownloadRequestException) GpodnetEpisodeActionPostResponse(de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeActionPostResponse)

Example 3 with GpodnetEpisodeAction

use of de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction in project AntennaPod by AntennaPod.

the class GpodnetService method uploadEpisodeActions.

/**
     * Updates the episode actions
     * <p/>
     * This method requires authentication.
     *
     * @param episodeActions    Collection of episode actions.
     * @return a GpodnetUploadChangesResponse. See {@link de.danoeh.antennapod.core.gpoddernet.model.GpodnetUploadChangesResponse}
     * for details.
     * @throws java.lang.IllegalArgumentException                           if username, deviceId, added or removed is null.
     * @throws de.danoeh.antennapod.core.gpoddernet.GpodnetServiceException if added or removed contain duplicates or if there
     *                                                                      is an authentication error.
     */
public GpodnetEpisodeActionPostResponse uploadEpisodeActions(@NonNull Collection<GpodnetEpisodeAction> episodeActions) throws GpodnetServiceException {
    String username = GpodnetPreferences.getUsername();
    try {
        URL url = new URI(BASE_SCHEME, BASE_HOST, String.format("/api/2/episodes/%s.json", username), null).toURL();
        final JSONArray list = new JSONArray();
        for (GpodnetEpisodeAction episodeAction : episodeActions) {
            JSONObject obj = episodeAction.writeToJSONObject();
            if (obj != null) {
                list.put(obj);
            }
        }
        RequestBody body = RequestBody.create(JSON, list.toString());
        Request.Builder request = new Request.Builder().post(body).url(url);
        final String response = executeRequest(request);
        return GpodnetEpisodeActionPostResponse.fromJSONObject(response);
    } catch (JSONException | MalformedURLException | URISyntaxException e) {
        e.printStackTrace();
        throw new GpodnetServiceException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) JSONArray(org.json.JSONArray) Request(okhttp3.Request) JSONException(org.json.JSONException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) GpodnetEpisodeAction(de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction) JSONObject(org.json.JSONObject) RequestBody(okhttp3.RequestBody)

Example 4 with GpodnetEpisodeAction

use of de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction in project AntennaPod by AntennaPod.

the class GpodnetPreferences method readEpisodeActionsFromString.

private static List<GpodnetEpisodeAction> readEpisodeActionsFromString(String s) {
    String[] lines = s.split("\n");
    List<GpodnetEpisodeAction> result = new ArrayList<>(lines.length);
    for (String line : lines) {
        if (TextUtils.isEmpty(line)) {
            GpodnetEpisodeAction action = GpodnetEpisodeAction.readFromString(line);
            if (action != null) {
                result.add(GpodnetEpisodeAction.readFromString(line));
            }
        }
    }
    return result;
}
Also used : GpodnetEpisodeAction(de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction) ArrayList(java.util.ArrayList)

Example 5 with GpodnetEpisodeAction

use of de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction in project AntennaPod by AntennaPod.

the class GpodnetService method readEpisodeActionsFromJSONObject.

private GpodnetEpisodeActionGetResponse readEpisodeActionsFromJSONObject(@NonNull JSONObject object) throws JSONException {
    List<GpodnetEpisodeAction> episodeActions = new ArrayList<>();
    long timestamp = object.getLong("timestamp");
    JSONArray jsonActions = object.getJSONArray("actions");
    for (int i = 0; i < jsonActions.length(); i++) {
        JSONObject jsonAction = jsonActions.getJSONObject(i);
        GpodnetEpisodeAction episodeAction = GpodnetEpisodeAction.readFromJSONObject(jsonAction);
        if (episodeAction != null) {
            episodeActions.add(episodeAction);
        }
    }
    return new GpodnetEpisodeActionGetResponse(episodeActions, timestamp);
}
Also used : GpodnetEpisodeAction(de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction) JSONObject(org.json.JSONObject) GpodnetEpisodeActionGetResponse(de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeActionGetResponse) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray)

Aggregations

GpodnetEpisodeAction (de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction)9 FeedMedia (de.danoeh.antennapod.core.feed.FeedMedia)3 Intent (android.content.Intent)2 FeedItem (de.danoeh.antennapod.core.feed.FeedItem)2 GpodnetEpisodeActionGetResponse (de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeActionGetResponse)2 ArrayList (java.util.ArrayList)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 SharedPreferences (android.content.SharedPreferences)1 Uri (android.net.Uri)1 ArrayMap (android.support.v4.util.ArrayMap)1 Pair (android.util.Pair)1 GpodnetService (de.danoeh.antennapod.core.gpoddernet.GpodnetService)1 GpodnetServiceException (de.danoeh.antennapod.core.gpoddernet.GpodnetServiceException)1 GpodnetEpisodeActionPostResponse (de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeActionPostResponse)1 DownloadRequestException (de.danoeh.antennapod.core.storage.DownloadRequestException)1 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1