1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 3 Copyright 2014 The Android Open Source Project 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16 --> 17 <sample> 18 <name>PermissionRequest</name> 19 <group>Content</group> 20 <package>com.example.android.permissionrequest</package> 21 22 <minSdk>21</minSdk> 23 24 <strings> 25 <intro> 26 <![CDATA[ 27 This sample shows how to handle PermissionRequest coming from web apps inside of a 28 WebView. 29 ]]> 30 </intro> 31 </strings> 32 33 <template src="base"/> 34 <template src="FragmentView"/> 35 <common src="logger"/> 36 <common src="activities"/> 37 38 <metadata> 39 <status>PUBLISHED</status> 40 <categories>Content</categories> 41 <technologies>Android</technologies> 42 <languages>Java</languages> 43 <solutions>Mobile</solutions> 44 <level>INTERMEDIATE</level> 45 <icon>screenshots/icon_web.png</icon> 46 <screenshots> 47 <img>screenshots/image1.png</img> 48 <img>screenshots/image2.png</img> 49 </screenshots> 50 <api_refs> 51 <android>android.webkit.PermissionRequest</android> 52 <android>android.webkit.WebView</android> 53 <android>android.webkit.WebChromeClient</android> 54 </api_refs> 55 56 <description> 57 <![CDATA[ 58 This sample demonstrates how to use the PermissionRequest API to 59 securely provide access to restricted system features (such as a 60 camera or microphone) from within a WebView. In this example, a dialog 61 is created to allow users to explicitly approve or reject each 62 request. 63 ]]> 64 </description> 65 66 <intro> 67 <![CDATA[ 68 PermissionRequest can be used by setting up a custom WebChromeClient. 69 70 ```java 71 mWebView.setWebChromeClient(mWebChromeClient); 72 ``` 73 74 In you WebChromeClient implementation, you need to override 75 [onPermissionRequest][1]. This method is called when the web content 76 is requesting permission to access some resources, providing an 77 opportunity to approve or reject the request. In this implementation, 78 we display a dialog to allow the user to approve or reject any 79 request. In other applications, you may want to implement a whitelist 80 of allowed APIs. Also, override [onPermissionRequestCanceled][2] for 81 handling cancellation of the PermissionRequest by the web content. 82 83 When the user confirms or denies the request, you can respond back to 84 the web content by [grant][3] or [deny][4] respectively. 85 86 ```java 87 mPermissionRequest.grant(mPermissionRequest.getResources()); 88 ``` 89 90 This sample provides the web content from the assets folder in the 91 app. Since WebView is not allowed to use getUserMedia from a "file://" 92 URL, the app uses the SimpleWebServer class to provide the content via 93 "http://localhost". 94 95 [1]: http://developer.android.com/reference/android/webkit/WebChromeClient.html#onPermissionRequest(android.webkit.PermissionRequest) 96 [2]: http://developer.android.com/reference/android/webkit/WebChromeClient.html#onPermissionRequestCanceled(android.webkit.PermissionRequest) 97 [3]: http://developer.android.com/reference/android/webkit/PermissionRequest.html#grant(java.lang.String[]) 98 [4]: http://developer.android.com/reference/android/webkit/PermissionRequest.html#deny() 99 ]]> 100 </intro> 101 </metadata> 102 103 </sample> 104