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