Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14792 manas 1
/**
2
 * Copyright 2010-present Facebook.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *    http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.facebook;
18
 
19
import android.annotation.TargetApi;
20
import android.os.AsyncTask;
21
import android.os.Handler;
22
import android.util.Log;
23
 
24
import java.lang.reflect.InvocationTargetException;
25
import java.lang.reflect.Method;
26
import java.net.HttpURLConnection;
27
import java.util.Collection;
28
import java.util.List;
29
import java.util.concurrent.Executor;
30
 
31
/**
32
 * Defines an AsyncTask suitable for executing a Request in the background. May be subclassed
33
 * by applications having unique threading model needs.
34
 */
35
public class RequestAsyncTask extends AsyncTask<Void, Void, List<Response>> {
36
    private static final String TAG = RequestAsyncTask.class.getCanonicalName();
37
    private static Method executeOnExecutorMethod;
38
 
39
    private final HttpURLConnection connection;
40
    private final RequestBatch requests;
41
 
42
    private Exception exception;
43
 
44
    static {
45
        for (Method method : AsyncTask.class.getMethods()) {
46
            if ("executeOnExecutor".equals(method.getName())) {
47
                Class<?>[] parameters = method.getParameterTypes();
48
                if ((parameters.length == 2) && (parameters[0] == Executor.class) && parameters[1].isArray()) {
49
                    executeOnExecutorMethod = method;
50
                    break;
51
                }
52
            }
53
        }
54
    }
55
 
56
    /**
57
     * Constructor. Serialization of the requests will be done in the background, so any serialization-
58
     * related errors will be returned via the Response.getException() method.
59
     *
60
     * @param requests the requests to execute
61
     */
62
    public RequestAsyncTask(Request... requests) {
63
        this(null, new RequestBatch(requests));
64
    }
65
 
66
    /**
67
     * Constructor. Serialization of the requests will be done in the background, so any serialization-
68
     * related errors will be returned via the Response.getException() method.
69
     *
70
     * @param requests the requests to execute
71
     */
72
    public RequestAsyncTask(Collection<Request> requests) {
73
        this(null, new RequestBatch(requests));
74
    }
75
 
76
    /**
77
     * Constructor. Serialization of the requests will be done in the background, so any serialization-
78
     * related errors will be returned via the Response.getException() method.
79
     *
80
     * @param requests the requests to execute
81
     */
82
    public RequestAsyncTask(RequestBatch requests) {
83
        this(null, requests);
84
    }
85
 
86
    /**
87
     * Constructor that allows specification of an HTTP connection to use for executing
88
     * the requests. No validation is done that the contents of the connection actually
89
     * reflect the serialized requests, so it is the caller's responsibility to ensure
90
     * that it will correctly generate the desired responses.
91
     *
92
     * @param connection the HTTP connection to use to execute the requests
93
     * @param requests   the requests to execute
94
     */
95
    public RequestAsyncTask(HttpURLConnection connection, Request... requests) {
96
        this(connection, new RequestBatch(requests));
97
    }
98
 
99
    /**
100
     * Constructor that allows specification of an HTTP connection to use for executing
101
     * the requests. No validation is done that the contents of the connection actually
102
     * reflect the serialized requests, so it is the caller's responsibility to ensure
103
     * that it will correctly generate the desired responses.
104
     *
105
     * @param connection the HTTP connection to use to execute the requests
106
     * @param requests   the requests to execute
107
     */
108
    public RequestAsyncTask(HttpURLConnection connection, Collection<Request> requests) {
109
        this(connection, new RequestBatch(requests));
110
    }
111
 
112
    /**
113
     * Constructor that allows specification of an HTTP connection to use for executing
114
     * the requests. No validation is done that the contents of the connection actually
115
     * reflect the serialized requests, so it is the caller's responsibility to ensure
116
     * that it will correctly generate the desired responses.
117
     *
118
     * @param connection the HTTP connection to use to execute the requests
119
     * @param requests   the requests to execute
120
     */
121
    public RequestAsyncTask(HttpURLConnection connection, RequestBatch requests) {
122
        this.requests = requests;
123
        this.connection = connection;
124
    }
125
 
126
    protected final Exception getException() {
127
        return exception;
128
    }
129
 
130
    protected final RequestBatch getRequests() {
131
        return requests;
132
    }
133
 
134
    @Override
135
    public String toString() {
136
        return new StringBuilder().append("{RequestAsyncTask: ").append(" connection: ").append(connection)
137
                .append(", requests: ").append(requests).append("}").toString();
138
    }
139
 
140
    @Override
141
    protected void onPreExecute() {
142
        super.onPreExecute();
143
 
144
        if (requests.getCallbackHandler() == null) {
145
            // We want any callbacks to go to a handler on this thread unless a handler has already been specified.
146
            requests.setCallbackHandler(new Handler());
147
        }
148
    }
149
 
150
    @Override
151
    protected void onPostExecute(List<Response> result) {
152
        super.onPostExecute(result);
153
 
154
        if (exception != null) {
155
            Log.d(TAG, String.format("onPostExecute: exception encountered during request: %s", exception.getMessage()));
156
        }
157
    }
158
 
159
    @Override
160
    protected List<Response> doInBackground(Void... params) {
161
        try {
162
            if (connection == null) {
163
                return requests.executeAndWait();
164
            } else {
165
                return Request.executeConnectionAndWait(connection, requests);
166
            }
167
        } catch (Exception e) {
168
            exception = e;
169
            return null;
170
        }
171
    }
172
 
173
    RequestAsyncTask executeOnSettingsExecutor() {
174
        if (executeOnExecutorMethod != null) {
175
            try {
176
                executeOnExecutorMethod.invoke(this, Settings.getExecutor(), null);
177
            } catch (InvocationTargetException e) {
178
                // fall-through
179
            } catch (IllegalAccessException e) {
180
                // fall-through
181
            }
182
        } else {
183
          this.execute();
184
        }
185
 
186
        return this;
187
    }
188
}