aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/ui/plans/PlanUpdateService.java
blob: 317a388100afb62a3a3d242a709f32d2847c1bba (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
package org.wordpress.android.ui.plans;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import com.android.volley.VolleyError;
import com.wordpress.rest.RestRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.wordpress.android.WordPress;
import org.wordpress.android.networking.RestClientUtils;
import org.wordpress.android.ui.plans.models.Plan;
import org.wordpress.android.ui.prefs.AppPrefs;
import org.wordpress.android.util.AppLog;

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

import de.greenrobot.event.EventBus;

/**
 * service which updates both global plans and available plans for a specific site
 */

public class PlanUpdateService extends Service {

    private static final String ARG_LOCAL_BLOG_ID = "local_blog_id";
    private int mNumActiveRequests;
    private int mLocalBlogId;
    private final List<Plan> mSitePlans = new ArrayList<>();

    public static void startService(Context context, int localTableBlogId) {
        Intent intent = new Intent(context, PlanUpdateService.class);
        intent.putExtra(ARG_LOCAL_BLOG_ID, localTableBlogId);
        context.startService(intent);
    }

    public static void stopService(Context context) {
        if (context == null) return;

        Intent intent = new Intent(context, PlanUpdateService.class);
        context.stopService(intent);
    }

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

    @Override
    public void onCreate() {
        super.onCreate();
        AppLog.i(AppLog.T.PLANS, "plan update service > created");
    }

    @Override
    public void onDestroy() {
        AppLog.i(AppLog.T.PLANS, "plan update service > destroyed");
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mLocalBlogId = intent.getIntExtra(ARG_LOCAL_BLOG_ID, 0);

        mNumActiveRequests = 2;
        downloadPlanFeatures();
        downloadAvailablePlansForSite();

        return START_NOT_STICKY;
    }

    /*
     * called when any plan data has been successfully updated
     */
    private void requestCompleted() {
        // send event once all requests have successfully completed
        mNumActiveRequests--;
        if (mNumActiveRequests == 0) {
            EventBus.getDefault().post(new PlanEvents.PlansUpdated(mLocalBlogId, mSitePlans));
        }
    }

    /*
     * called when updating any plan data fails
     */
    private void requestFailed() {
        EventBus.getDefault().post(new PlanEvents.PlansUpdateFailed());
    }

    /*
     * download features for the global plans
     */
    private void downloadPlanFeatures() {
        WordPress.getRestClientUtilsV1_2().get("plans/features/", RestClientUtils.getRestLocaleParams(PlanUpdateService.this), null, new RestRequest.Listener() {
            @Override
            public void onResponse(JSONObject response) {
                if (response != null) {
                    AppLog.d(AppLog.T.PLANS, response.toString());
                    // Store the response into App Prefs
                    AppPrefs.setGlobalPlansFeatures(response.toString());
                    requestCompleted();
                } else {
                    AppLog.w(AppLog.T.PLANS, "Unexpected empty response from server when downloading Features");
                    requestFailed();
                }
            }
        }, new RestRequest.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                AppLog.e(AppLog.T.PLANS, "Error Loading Plans/Features", volleyError);
                requestFailed();
            }
        });
    }

    /*
     * download plans for the specific site
     */
    private void downloadAvailablePlansForSite() {
        int remoteBlogId = WordPress.wpDB.getRemoteBlogIdForLocalTableBlogId(mLocalBlogId);
        WordPress.getRestClientUtilsV1_2().get("sites/" + remoteBlogId + "/plans", RestClientUtils.getRestLocaleParams(PlanUpdateService.this), null, new RestRequest.Listener() {
            @Override
            public void onResponse(JSONObject response) {
                if (response == null) {
                    AppLog.w(AppLog.T.PLANS, "Unexpected empty response from server");
                    requestFailed();
                    return;
                }

                AppLog.d(AppLog.T.PLANS, response.toString());
                mSitePlans.clear();

                try {
                    JSONArray plansArray = response.getJSONArray("originalResponse");
                    for (int i=0; i < plansArray.length(); i ++) {
                        JSONObject currentPlanJSON = plansArray.getJSONObject(i);
                        Plan currentPlan = new Plan(currentPlanJSON);
                        mSitePlans.add(currentPlan);
                    }
                    requestCompleted();
                } catch (JSONException e) {
                    AppLog.e(AppLog.T.PLANS, "Can't parse the plans list returned from the server", e);
                    requestFailed();
                }
            }
        }, new RestRequest.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                AppLog.e(AppLog.T.UTILS, "Error downloading site plans", volleyError);
                requestFailed();
            }
        });
    }
}