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