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