1 2 Android PermissionRequest Sample 3 =================================== 4 5 This sample demonstrates how to use the PermissionRequest API to 6 securely provide access to restricted system features (such as a 7 camera or microphone) from within a WebView. In this example, a dialog 8 is created to allow users to explicitly approve or reject each 9 request. 10 11 Introduction 12 ------------ 13 14 PermissionRequest can be used by setting up a custom WebChromeClient. 15 16 ```java 17 mWebView.setWebChromeClient(mWebChromeClient); 18 ``` 19 20 In you WebChromeClient implementation, you need to override 21 [onPermissionRequest][1]. This method is called when the web content 22 is requesting permission to access some resources, providing an 23 opportunity to approve or reject the request. In this implementation, 24 we display a dialog to allow the user to approve or reject any 25 request. In other applications, you may want to implement a whitelist 26 of allowed APIs. Also, override [onPermissionRequestCanceled][2] for 27 handling cancellation of the PermissionRequest by the web content. 28 29 When the user confirms or denies the request, you can respond back to 30 the web content by [grant][3] or [deny][4] respectively. 31 32 ```java 33 mPermissionRequest.grant(mPermissionRequest.getResources()); 34 ``` 35 36 This sample provides the web content from the assets folder in the 37 app. Since WebView is not allowed to use getUserMedia from a "file://" 38 URL, the app uses the SimpleWebServer class to provide the content via 39 "http://localhost". 40 41 [1]: http://developer.android.com/reference/android/webkit/WebChromeClient.html#onPermissionRequest(android.webkit.PermissionRequest) 42 [2]: http://developer.android.com/reference/android/webkit/WebChromeClient.html#onPermissionRequestCanceled(android.webkit.PermissionRequest) 43 [3]: http://developer.android.com/reference/android/webkit/PermissionRequest.html#grant(java.lang.String[]) 44 [4]: http://developer.android.com/reference/android/webkit/PermissionRequest.html#deny() 45 46 Pre-requisites 47 -------------- 48 49 - Android SDK 24 50 - Android Build Tools v24.0.1 51 - Android Support Repository 52 53 Screenshots 54 ------------- 55 56 <img src="screenshots/image1.png" height="400" alt="Screenshot"/> <img src="screenshots/image2.png" height="400" alt="Screenshot"/> 57 58 Getting Started 59 --------------- 60 61 This sample uses the Gradle build system. To build this project, use the 62 "gradlew build" command or use "Import Project" in Android Studio. 63 64 Support 65 ------- 66 67 - Google+ Community: https://plus.google.com/communities/105153134372062985968 68 - Stack Overflow: http://stackoverflow.com/questions/tagged/android 69 70 If you've found an error in this sample, please file an issue: 71 https://github.com/googlesamples/android-PermissionRequest 72 73 Patches are encouraged, and may be submitted by forking this project and 74 submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details. 75 76 License 77 ------- 78 79 Copyright 2016 The Android Open Source Project, Inc. 80 81 Licensed to the Apache Software Foundation (ASF) under one or more contributor 82 license agreements. See the NOTICE file distributed with this work for 83 additional information regarding copyright ownership. The ASF licenses this 84 file to you under the Apache License, Version 2.0 (the "License"); you may not 85 use this file except in compliance with the License. You may obtain a copy of 86 the License at 87 88 http://www.apache.org/licenses/LICENSE-2.0 89 90 Unless required by applicable law or agreed to in writing, software 91 distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 92 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 93 License for the specific language governing permissions and limitations under 94 the License. 95