OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 package org.chromium.chrome.browser.physicalweb; | 4 package org.chromium.chrome.browser.physicalweb; |
5 | 5 |
6 import android.graphics.Bitmap; | 6 import android.graphics.Bitmap; |
7 import android.graphics.BitmapFactory; | 7 import android.graphics.BitmapFactory; |
8 | 8 |
9 import java.io.ByteArrayOutputStream; | 9 import java.io.ByteArrayOutputStream; |
10 import java.io.IOException; | 10 import java.io.IOException; |
11 import java.io.InputStream; | 11 import java.io.InputStream; |
12 import java.net.HttpURLConnection; | 12 import java.net.HttpURLConnection; |
13 import java.net.MalformedURLException; | 13 import java.net.MalformedURLException; |
14 | 14 |
15 /** | 15 /** |
16 * A class that represents an HTTP request for an image. | 16 * A class that represents an HTTP request for an image. |
17 * The response is a Bitmap. | 17 * The response is a Bitmap. |
18 */ | 18 */ |
19 class BitmapHttpRequest extends HttpRequest<Bitmap> { | 19 class BitmapHttpRequest extends HttpRequest<Bitmap> { |
20 /** | 20 /** |
21 * Construct a bitmap HTTP request. | 21 * Construct a bitmap HTTP request. |
22 * @param url The url to make this HTTP request to. | 22 * @param url The url to make this HTTP request to. |
| 23 * @param userAgent The string to set as the User-Agent request header. |
| 24 * @param acceptLanguage The string to set as the Accept-Language request he
ader. |
23 * @param callback The callback run when the HTTP response is received. | 25 * @param callback The callback run when the HTTP response is received. |
24 * The callback can be called with a null bitmap if the image | 26 * The callback can be called with a null bitmap if the image |
25 * couldn't be decoded. | 27 * couldn't be decoded. |
26 * @throws MalformedURLException on invalid url | 28 * @throws MalformedURLException on invalid url |
27 */ | 29 */ |
28 public BitmapHttpRequest(String url, RequestCallback callback) | 30 public BitmapHttpRequest(String url, String userAgent, String acceptLanguage
, |
| 31 RequestCallback callback) |
29 throws MalformedURLException { | 32 throws MalformedURLException { |
30 super(url, callback); | 33 super(url, userAgent, acceptLanguage, callback); |
31 } | 34 } |
32 | 35 |
33 /** | 36 /** |
34 * The callback that gets run after the request is made. | 37 * The callback that gets run after the request is made. |
35 */ | 38 */ |
36 public interface RequestCallback extends HttpRequest.HttpRequestCallback<Bit
map> {} | 39 public interface RequestCallback extends HttpRequest.HttpRequestCallback<Bit
map> {} |
37 | 40 |
38 /** | 41 /** |
39 * Helper method to make an HTTP request. | 42 * Helper method to make an HTTP request. |
40 * @param urlConnection The HTTP connection. | 43 * @param urlConnection The HTTP connection. |
(...skipping 10 matching lines...) Expand all Loading... |
51 byte[] buffer = new byte[1024]; | 54 byte[] buffer = new byte[1024]; |
52 int len; | 55 int len; |
53 while ((len = is.read(buffer)) != -1) { | 56 while ((len = is.read(buffer)) != -1) { |
54 os.write(buffer, 0, len); | 57 os.write(buffer, 0, len); |
55 } | 58 } |
56 byte[] bitmapData = os.toByteArray(); | 59 byte[] bitmapData = os.toByteArray(); |
57 return BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length); | 60 return BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length); |
58 } | 61 } |
59 } | 62 } |
60 | 63 |
OLD | NEW |