-
Notifications
You must be signed in to change notification settings - Fork 13
/
HttpUtils.java
262 lines (224 loc) · 9.37 KB
/
HttpUtils.java
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright (C) 2014 Murray Cumming
*
* This file is part of android-galaxyzoo
*
* android-galaxyzoo is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* android-galaxyzoo is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with android-galaxyzoo. If not, see <http://www.gnu.org/licenses/>.
*/
package com.murrayc.galaxyzoo.app.provider;
import android.content.Context;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.RequestFuture;
import com.murrayc.galaxyzoo.app.Log;
import com.murrayc.galaxyzoo.app.LoginUtils;
import com.murrayc.galaxyzoo.app.Utils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* Don't use any of these methods from the main thread.
*
* Created by murrayc on 8/25/14.
*/
public class HttpUtils {
public static final String HTTP_REQUEST_HEADER_PARAM_USER_AGENT = "User-Agent";
public static final String USER_AGENT_MURRAYC = "murrayc.com-android-galaxyzoo";
public static final int TIMEOUT_MILLIS = 20000; //20 seconds. Long but not too short for GPRS connections and not endless.
public static void throwIfNoNetwork(final Context context) {
if(!getNetworkIsConnected(context)) {
//Throw an exception so the caller knows.
throw new NoNetworkException();
}
}
/**
* Don't call this from the main thread because it uses the Account.
*
* @param context
* @return
*/
public static boolean getNetworkIsConnected(final Context context) {
final Utils.NetworkConnected networkConnected =
Utils.getNetworkIsConnected(context, LoginUtils.getUseWifiOnly(context));
return networkConnected.connected;
}
public static HttpURLConnection openConnection(final String strURL) {
final URL url;
try {
url = new URL(strURL);
} catch (MalformedURLException e) {
Log.error("openConnection(): exception while parsing URL", e);
return null;
}
final HttpURLConnection conn;
try {
conn = (HttpURLConnection) url.openConnection();
setConnectionUserAgent(conn);
} catch (final IOException e) {
Log.error("openConnection(): exception during HTTP connection", e);
return null;
}
//Set a reasonable timeout.
//Otherwise there is not timeout so we might never know if it fails,
//so never have the chance to try again.
conn.setConnectTimeout(TIMEOUT_MILLIS);
conn.setReadTimeout(TIMEOUT_MILLIS);
return conn;
}
public static boolean cacheUriToFileSync(final Context context, final RequestQueue requestQueue, final String uriFileToCache, final String cacheFileUri) {
final RequestFuture<Boolean> futureListener = RequestFuture.newFuture();
final Request<Boolean> request = new FileCacheRequest(context, uriFileToCache, cacheFileUri,
futureListener, futureListener);
//We won't request the same image again if it succeeded once,
//so don't waste memory or storage caching it.
//(We are downloading it to our own cache, of course.)
request.setShouldCache(false);
requestQueue.add(request);
boolean response = false;
try {
//Note: If we don't provider the RequestFuture as the errorListener too,
//then this won't return until after the timeout, even if an error happen earlier.
response = futureListener.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
} catch (final InterruptedException | ExecutionException e) {
Log.error("cacheUriToFile(): Exception from request.", e);
} catch (TimeoutException e) {
Log.error("cacheUriToFile(): Timeout Exception from request.", e);
}
return response;
}
public static class FileCacheRequest extends Request<Boolean> {
private final String mCacheFileUri;
private final WeakReference<Context> mContext;
private final Response.Listener<Boolean> mListener;
public FileCacheRequest(final Context context, final String uriFileToCache, final String cacheFileUri, final Response.Listener<Boolean> listener, final Response.ErrorListener errorListener) {
super(Method.GET, uriFileToCache, errorListener);
mCacheFileUri = cacheFileUri;
mContext = new WeakReference<>(context);
mListener = listener;
}
@Override
protected Response<Boolean> parseNetworkResponse(final NetworkResponse response) {
boolean result = false;
if (mContext != null) {
final Context context = mContext.get();
if (context != null) {
result = parseGetFileResponseContent(context, response.data, mCacheFileUri);
} else {
Log.error("parseNetworkResponse(): context is null.");
}
}
return Response.success(result, HttpHeaderParser.parseCacheHeaders(response));
}
@Override
protected void deliverResponse(final Boolean response) {
mListener.onResponse(response);
}
@Override
public Map<String, String> getHeaders(){
final Map<String, String> headers = new HashMap<>();
headers.put(HttpUtils.HTTP_REQUEST_HEADER_PARAM_USER_AGENT, HttpUtils.USER_AGENT_MURRAYC);
return headers;
}
}
/*
public static long getLatestLastModified(final String[] urls) {
long latest = 0;
for (final String url : urls) {
final long lastMod = getUriLastModified(url);
if (lastMod > latest) {
latest = lastMod;
}
}
return latest;
}
private static long getUriLastModified(final String strUrl) {
final URL url;
try {
url = new URL(strUrl);
} catch (final MalformedURLException e) {
Log.error("getUriLastModified(): can't instantiate URL", e);
return 0;
}
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con;
try {
//Note: This seems to succeed even if there is no internet connection,
//for instance if the device is in airplane mode.
//getLastModified() then returns 0;
con = (HttpURLConnection) url.openConnection();
} catch (final IOException e) {
Log.error("getUriLastModified(): can't openConnection", e);
return 0;
}
final long result = con.getLastModified();
con.disconnect(); //Otherwise StrictMode says (with an exception) that we leak.
return result;
}
*/
private static void setConnectionUserAgent(final HttpURLConnection connection) {
connection.setRequestProperty(HTTP_REQUEST_HEADER_PARAM_USER_AGENT, USER_AGENT_MURRAYC);
}
private static boolean parseGetFileResponseContent(final Context context, final byte[] data, final String cacheFileContentUri) {
//Write the content to the file:
ParcelFileDescriptor pfd = null;
FileOutputStream fout = null;
try {
//FileOutputStream doesn't seem to understand content provider URIs:
pfd = context.getContentResolver().
openFileDescriptor(Uri.parse(cacheFileContentUri), "w");
fout = new FileOutputStream(pfd.getFileDescriptor());
fout.write(data);
} catch (final IOException e) {
Log.error("parseGetFileResponseContent(): Exception while writing to FileOutputStream", e);
return false;
} finally {
if (fout != null) {
try {
fout.close();
} catch (final IOException e) {
Log.error("parseGetFileResponseContent(): Exception while closing fout", e);
}
}
if (pfd != null) {
try {
pfd.close();
} catch (final IOException e) {
Log.error("parseGetFileResponseContent(): Exception while closing pfd", e);
}
}
}
return true;
}
/**
* This is a RuntimeException because only RuntimeExceptions may be thrown by
* a ContentProvider.
*/
public static class NoNetworkException extends RuntimeException {
public NoNetworkException() {
}
}
}