Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 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 android.webkit; |
| 18 | |
| 19 | import android.net.http.Headers; |
| 20 | |
| 21 | import java.io.InputStream; |
| 22 | |
| 23 | /** |
Steve Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 24 | * Encapsulates a resource response. Applications can return an instance of this |
| 25 | * class from {@link WebViewClient#shouldInterceptRequest} to provide a custom |
| 26 | * response when the WebView requests a particular resource. |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 27 | */ |
| 28 | public class WebResourceResponse { |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 29 | // Accessed by jni, do not rename without modifying the jni code. |
| 30 | private String mMimeType; |
| 31 | private String mEncoding; |
| 32 | private InputStream mInputStream; |
| 33 | |
| 34 | /** |
Steve Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 35 | * Constructs a resource response with the given MIME type, encoding, and |
| 36 | * input stream. Callers must implement |
| 37 | * {@link InputStream#read(byte[]) InputStream.read(byte[])} for the input |
| 38 | * stream. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 39 | * |
| 40 | * @param mimeType the resource response's MIME type, for example text/html |
| 41 | * @param encoding the resource response's encoding |
| 42 | * @param data the input stream that provides the resource response's data |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 43 | */ |
| 44 | public WebResourceResponse(String mimeType, String encoding, |
| 45 | InputStream data) { |
| 46 | mMimeType = mimeType; |
| 47 | mEncoding = encoding; |
| 48 | mInputStream = data; |
| 49 | } |
| 50 | |
| 51 | /** |
Steve Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 52 | * Sets the resource response's MIME type, for example text/html. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 53 | * |
| 54 | * @param mimeType the resource response's MIME type |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 55 | */ |
| 56 | public void setMimeType(String mimeType) { |
| 57 | mMimeType = mimeType; |
| 58 | } |
| 59 | |
| 60 | /** |
Steve Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 61 | * Gets the resource response's MIME type. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 62 | * |
| 63 | * @return the resource response's MIME type |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 64 | */ |
| 65 | public String getMimeType() { |
| 66 | return mMimeType; |
| 67 | } |
| 68 | |
| 69 | /** |
Steve Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 70 | * Sets the resource response's encoding, for example UTF-8. This is used |
| 71 | * to decode the data from the input stream. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 72 | * |
| 73 | * @param encoding the resource response's encoding |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 74 | */ |
| 75 | public void setEncoding(String encoding) { |
| 76 | mEncoding = encoding; |
| 77 | } |
| 78 | |
| 79 | /** |
Steve Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 80 | * Gets the resource response's encoding. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 81 | * |
| 82 | * @return the resource response's encoding |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 83 | */ |
| 84 | public String getEncoding() { |
| 85 | return mEncoding; |
| 86 | } |
| 87 | |
| 88 | /** |
Steve Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 89 | * Sets the input stream that provides the resource respone's data. Callers |
| 90 | * must implement {@link InputStream#read(byte[]) InputStream.read(byte[])}. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 91 | * |
| 92 | * @param data the input stream that provides the resource response's data |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 93 | */ |
| 94 | public void setData(InputStream data) { |
| 95 | mInputStream = data; |
| 96 | } |
| 97 | |
| 98 | /** |
Steve Block | 938d2fb | 2012-02-21 02:10:10 +0000 | [diff] [blame] | 99 | * Gets the input stream that provides the resource respone's data. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 100 | * |
| 101 | * @return the input stream that provides the resource response's data |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 102 | */ |
| 103 | public InputStream getData() { |
| 104 | return mInputStream; |
| 105 | } |
Patrick Scott | c12544a | 2010-11-11 13:16:44 -0500 | [diff] [blame] | 106 | } |