aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/media/services/MediaDeleteService.java
blob: af0497946627525ec4ad1017547658543c284b6a (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
package org.wordpress.android.ui.media.services;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Handler;
import android.os.IBinder;

import org.wordpress.android.WordPress;
import org.wordpress.android.models.MediaUploadState;
import org.xmlrpc.android.ApiHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * A service for deleting media files from the media browser.
 * Only one file is deleted at a time.
 */
public class MediaDeleteService extends Service {
    // time to wait before trying to delete the next file
    private static final int DELETE_WAIT_TIME = 1000;

    private Context mContext;
    private Handler mHandler = new Handler();
    private boolean mDeleteInProgress;

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

    @Override
    public void onCreate() {
        super.onCreate();

        mContext = this.getApplicationContext();
        mDeleteInProgress = false;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        mHandler.post(mFetchQueueTask);
    }

    private Runnable mFetchQueueTask = new Runnable() {
        @Override
        public void run() {
            Cursor cursor = getQueueItem();
            try {
                if ((cursor == null || cursor.getCount() == 0 || mContext == null) && !mDeleteInProgress) {
                    MediaDeleteService.this.stopSelf();
                    return;
                } else {
                    if (mDeleteInProgress) {
                        mHandler.postDelayed(this, DELETE_WAIT_TIME);
                    } else {
                        deleteMediaFile(cursor);
                    }
                }
            } finally {
                if (cursor != null)
                    cursor.close();
            }

        }
    };

    private Cursor getQueueItem() {
        if (WordPress.getCurrentBlog() == null)
            return null;

        String blogId = String.valueOf(WordPress.getCurrentBlog().getLocalTableBlogId());
        return WordPress.wpDB.getMediaDeleteQueueItem(blogId);
    }

    private void deleteMediaFile(Cursor cursor) {
        if (!cursor.moveToFirst())
            return;

        mDeleteInProgress = true;

        final String blogId = cursor.getString((cursor.getColumnIndex("blogId")));
        final String mediaId = cursor.getString(cursor.getColumnIndex("mediaId"));

        ApiHelper.DeleteMediaTask task = new ApiHelper.DeleteMediaTask(mediaId,
                new ApiHelper.GenericCallback() {
            @Override
            public void onSuccess() {
                // only delete them once we get an ok from the server
                if (WordPress.getCurrentBlog() != null && mediaId != null) {
                    WordPress.wpDB.deleteMediaFile(blogId, mediaId);
                }

                mDeleteInProgress = false;
                mHandler.post(mFetchQueueTask);
            }

            @Override
            public void onFailure(ApiHelper.ErrorType errorType, String errorMessage, Throwable throwable) {
                // Ideally we would do handle the 401 (unauthorized) and 404 (not found) errors,
                // but the XMLRPCExceptions don't seem to give messages when they are thrown.

                // Instead we'll just set them as "deleted" so they don't show up in the delete queue.
                // Otherwise the service will continuously try to delete an item they can't delete.

                WordPress.wpDB.updateMediaUploadState(blogId, mediaId, MediaUploadState.DELETED);

                mDeleteInProgress = false;
                mHandler.post(mFetchQueueTask);
            }
        });

        List<Object> apiArgs = new ArrayList<Object>();
        apiArgs.add(WordPress.getCurrentBlog());
        task.execute(apiArgs) ;

        mHandler.post(mFetchQueueTask);
    }
}