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