aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/posts/services/PostMediaService.java
blob: 4af0a2125c17daf844a9fd43e7a5bb93f0e108d1 (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
package org.wordpress.android.ui.posts.services;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import org.wordpress.android.WordPress;
import org.wordpress.android.models.Blog;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.helpers.MediaFile;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlrpc.android.ApiHelper.Method;
import org.xmlrpc.android.XMLRPCClientInterface;
import org.xmlrpc.android.XMLRPCException;
import org.xmlrpc.android.XMLRPCFactory;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

import de.greenrobot.event.EventBus;

/**
 * service which retrieves media info for a list of media IDs in a specific blog - currently used
 * only for featured images in the post list but could be used for any blog-specific media
 */

public class PostMediaService extends Service {

    private static final String ARG_BLOG_ID = "blog_id";
    private static final String ARG_MEDIA_IDS = "media_ids";

    private final ConcurrentLinkedQueue<Long> mMediaIdQueue = new ConcurrentLinkedQueue<>();
    private Blog mBlog;

    public static void startService(Context context, int blogId, ArrayList<Long> mediaIds) {
        if (context == null || mediaIds == null || mediaIds.size() == 0) {
            return;
        }

        Intent intent = new Intent(context, PostMediaService.class);
        intent.putExtra(ARG_BLOG_ID, blogId);
        intent.putExtra(ARG_MEDIA_IDS, mediaIds);
        context.startService(intent);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        AppLog.i(AppLog.T.POSTS, "PostMediaService > created");
    }

    @Override
    public void onDestroy() {
        AppLog.i(AppLog.T.POSTS, "PostMediaService > destroyed");
        super.onDestroy();
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent == null) return START_NOT_STICKY;

        int blogId = intent.getIntExtra(ARG_BLOG_ID, 0);
        mBlog = WordPress.getBlog(blogId);

        Serializable serializable = intent.getSerializableExtra(ARG_MEDIA_IDS);
        if (serializable != null && serializable instanceof List) {
            List list = (List) serializable;
            for (Object id : list) {
                if (id instanceof Long) {
                    mMediaIdQueue.add((Long) id);
                }
            }
        }

        if (mMediaIdQueue.size() > 0 && mBlog != null) {
            new Thread() {
                @Override
                public void run() {
                    while (!mMediaIdQueue.isEmpty()) {
                        long mediaId = mMediaIdQueue.poll();
                        downloadMediaItem(mediaId);
                    }
                }
            }.start();
        }

        return START_NOT_STICKY;
    }

    private void downloadMediaItem(long mediaId) {
        Object[] apiParams = {
                mBlog.getRemoteBlogId(),
                mBlog.getUsername(),
                mBlog.getPassword(),
                mediaId};

        XMLRPCClientInterface client = XMLRPCFactory.instantiate(
                mBlog.getUri(),
                mBlog.getHttpuser(),
                mBlog.getHttppassword());

        try {
            Map<?, ?> results = (Map<?, ?>) client.call(Method.GET_MEDIA_ITEM, apiParams);
            if (results != null) {
                String strBlogId = Integer.toString(mBlog.getLocalTableBlogId());
                MediaFile mediaFile = new MediaFile(strBlogId, results, mBlog.isDotcomFlag());
                WordPress.wpDB.saveMediaFile(mediaFile);
                AppLog.d(AppLog.T.POSTS, "PostMediaService > downloaded " + mediaFile.getFileURL());
                EventBus.getDefault().post(new PostEvents.PostMediaInfoUpdated(mediaId, mediaFile.getFileURL()));
            }
        } catch (ClassCastException | XMLRPCException | XmlPullParserException | IOException e) {
            AppLog.e(AppLog.T.POSTS, e);
        }
    }
}