summaryrefslogtreecommitdiffstats
path: root/samples/browseable/PermissionRequest
diff options
context:
space:
mode:
Diffstat (limited to 'samples/browseable/PermissionRequest')
-rw-r--r--samples/browseable/PermissionRequest/res/values/strings.xml1
-rw-r--r--samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/ConfirmationDialogFragment.java20
-rw-r--r--samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/MessageDialogFragment.java61
-rw-r--r--samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/PermissionRequestFragment.java67
-rw-r--r--samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/SimpleWebServer.java4
5 files changed, 137 insertions, 16 deletions
diff --git a/samples/browseable/PermissionRequest/res/values/strings.xml b/samples/browseable/PermissionRequest/res/values/strings.xml
index c3e5574dc..248876601 100644
--- a/samples/browseable/PermissionRequest/res/values/strings.xml
+++ b/samples/browseable/PermissionRequest/res/values/strings.xml
@@ -15,6 +15,7 @@
limitations under the License.
-->
<resources>
+ <string name="permission_message">This sample app uses camera.</string>
<string name="confirmation">This web page wants to use following resources:\n\n%s</string>
<string name="allow">Allow</string>
<string name="deny">Deny</string>
diff --git a/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/ConfirmationDialogFragment.java b/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/ConfirmationDialogFragment.java
index 7dae56efd..ca173b424 100644
--- a/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/ConfirmationDialogFragment.java
+++ b/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/ConfirmationDialogFragment.java
@@ -13,12 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package com.example.android.permissionrequest;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
+import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
@@ -32,7 +34,7 @@ public class ConfirmationDialogFragment extends DialogFragment {
/**
* Creates a new instance of ConfirmationDialogFragment.
*
- * @param resources The list of resources requested by PermissionRequeste.
+ * @param resources The list of resources requested by PermissionRequest.
* @return A new instance.
*/
public static ConfirmationDialogFragment newInstance(String[] resources) {
@@ -43,21 +45,22 @@ public class ConfirmationDialogFragment extends DialogFragment {
return fragment;
}
+ @NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
- String[] resources = getArguments().getStringArray(ARG_RESOURCES);
+ final String[] resources = getArguments().getStringArray(ARG_RESOURCES);
return new AlertDialog.Builder(getActivity())
.setMessage(getString(R.string.confirmation, TextUtils.join("\n", resources)))
.setNegativeButton(R.string.deny, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
- ((Listener) getParentFragment()).onConfirmation(false);
+ ((Listener) getParentFragment()).onConfirmation(false, resources);
}
})
.setPositiveButton(R.string.allow, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
- ((Listener) getParentFragment()).onConfirmation(true);
+ ((Listener) getParentFragment()).onConfirmation(true, resources);
}
})
.create();
@@ -66,14 +69,15 @@ public class ConfirmationDialogFragment extends DialogFragment {
/**
* Callback for the user's response.
*/
- public interface Listener {
+ interface Listener {
/**
- * Called when the PermissoinRequest is allowed or denied by the user.
+ * Called when the PermissionRequest is allowed or denied by the user.
*
- * @param allowed True if the user allowed the request.
+ * @param allowed True if the user allowed the request.
+ * @param resources The resources to be granted.
*/
- public void onConfirmation(boolean allowed);
+ void onConfirmation(boolean allowed, String[] resources);
}
}
diff --git a/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/MessageDialogFragment.java b/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/MessageDialogFragment.java
new file mode 100644
index 000000000..31d0bcbf7
--- /dev/null
+++ b/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/MessageDialogFragment.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.permissionrequest;
+
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.content.DialogInterface;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.annotation.StringRes;
+import android.support.v4.app.DialogFragment;
+
+/**
+ * Shows a dialog with a brief message.
+ */
+public class MessageDialogFragment extends DialogFragment {
+
+ private static final String ARG_MESSAGE_RES_ID = "message_res_id";
+
+ public static MessageDialogFragment newInstance(@StringRes int message) {
+ MessageDialogFragment fragment = new MessageDialogFragment();
+ Bundle args = new Bundle();
+ args.putInt(ARG_MESSAGE_RES_ID, message);
+ fragment.setArguments(args);
+ return fragment;
+ }
+
+ @NonNull
+ @Override
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ return new AlertDialog.Builder(getContext())
+ .setMessage(getArguments().getInt(ARG_MESSAGE_RES_ID))
+ .setCancelable(false)
+ .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ ((Listener) getParentFragment()).onOkClicked();
+ }
+ })
+ .create();
+ }
+
+ interface Listener {
+ void onOkClicked();
+ }
+
+}
diff --git a/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/PermissionRequestFragment.java b/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/PermissionRequestFragment.java
index 44f1d6ec5..418c90d34 100644
--- a/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/PermissionRequestFragment.java
+++ b/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/PermissionRequestFragment.java
@@ -16,12 +16,15 @@
package com.example.android.permissionrequest;
+import android.Manifest;
import android.annotation.SuppressLint;
+import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
+import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -37,12 +40,14 @@ import com.example.android.common.logger.Log;
* This fragment shows a {@link WebView} and loads a web app from the {@link SimpleWebServer}.
*/
public class PermissionRequestFragment extends Fragment
- implements ConfirmationDialogFragment.Listener {
+ implements ConfirmationDialogFragment.Listener, MessageDialogFragment.Listener {
private static final String TAG = PermissionRequestFragment.class.getSimpleName();
private static final String FRAGMENT_DIALOG = "dialog";
+ private static final int REQUEST_CAMERA_PERMISSION = 1;
+
/**
* We use this web server to serve HTML files in the assets folder. This is because we cannot
* use the JavaScript method "getUserMedia" from "file:///android_assets/..." URLs.
@@ -67,7 +72,7 @@ public class PermissionRequestFragment extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
- @Nullable Bundle savedInstanceState) {
+ @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_permission_request, container, false);
}
@@ -85,7 +90,14 @@ public class PermissionRequestFragment extends Fragment
final int port = 8080;
mWebServer = new SimpleWebServer(port, getResources().getAssets());
mWebServer.start();
- mWebView.loadUrl("http://localhost:" + port + "/sample.html");
+ // This is for runtime permission on Marshmallow and above; It is not directly related to
+ // PermissionRequest API.
+ if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
+ != PackageManager.PERMISSION_GRANTED) {
+ requestCameraPermission();
+ } else {
+ mWebView.loadUrl("http://localhost:" + port + "/sample.html");
+ }
}
@Override
@@ -94,6 +106,32 @@ public class PermissionRequestFragment extends Fragment
super.onPause();
}
+ @Override
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
+ @NonNull int[] grantResults) {
+ // This is for runtime permission on Marshmallow and above; It is not directly related to
+ // PermissionRequest API.
+ if (requestCode == REQUEST_CAMERA_PERMISSION) {
+ if (permissions.length != 1 || grantResults.length != 1 ||
+ grantResults[0] != PackageManager.PERMISSION_GRANTED) {
+ Log.e(TAG, "Camera permission not granted.");
+ } else if (mWebView != null && mWebServer != null) {
+ mWebView.loadUrl("http://localhost:" + mWebServer.getPort() + "/sample.html");
+ }
+ } else {
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
+ }
+ }
+
+ private void requestCameraPermission() {
+ if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
+ MessageDialogFragment.newInstance(R.string.permission_message)
+ .show(getChildFragmentManager(), FRAGMENT_DIALOG);
+ } else {
+ requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
+ }
+ }
+
@SuppressLint("SetJavaScriptEnabled")
private static void configureWebSettings(WebSettings settings) {
settings.setJavaScriptEnabled(true);
@@ -110,8 +148,16 @@ public class PermissionRequestFragment extends Fragment
public void onPermissionRequest(PermissionRequest request) {
Log.i(TAG, "onPermissionRequest");
mPermissionRequest = request;
- ConfirmationDialogFragment.newInstance(request.getResources())
- .show(getChildFragmentManager(), FRAGMENT_DIALOG);
+ final String[] requestedResources = request.getResources();
+ for (String r : requestedResources) {
+ if (r.equals(PermissionRequest.RESOURCE_VIDEO_CAPTURE)) {
+ // In this sample, we only accept video capture request.
+ ConfirmationDialogFragment
+ .newInstance(new String[]{PermissionRequest.RESOURCE_VIDEO_CAPTURE})
+ .show(getChildFragmentManager(), FRAGMENT_DIALOG);
+ break;
+ }
+ }
}
// This method is called when the permission request is canceled by the web content.
@@ -155,9 +201,14 @@ public class PermissionRequestFragment extends Fragment
};
@Override
- public void onConfirmation(boolean allowed) {
+ public void onOkClicked() {
+ requestPermissions(new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
+ }
+
+ @Override
+ public void onConfirmation(boolean allowed, String[] resources) {
if (allowed) {
- mPermissionRequest.grant(mPermissionRequest.getResources());
+ mPermissionRequest.grant(resources);
Log.d(TAG, "Permission granted.");
} else {
mPermissionRequest.deny();
@@ -174,7 +225,7 @@ public class PermissionRequestFragment extends Fragment
* For testing.
*/
public interface ConsoleMonitor {
- public void onConsoleMessage(ConsoleMessage message);
+ void onConsoleMessage(ConsoleMessage message);
}
}
diff --git a/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/SimpleWebServer.java b/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/SimpleWebServer.java
index 36b7c4693..b02275af5 100644
--- a/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/SimpleWebServer.java
+++ b/samples/browseable/PermissionRequest/src/com.example.android.permissionrequest/SimpleWebServer.java
@@ -90,6 +90,10 @@ public class SimpleWebServer implements Runnable {
}
}
+ public int getPort() {
+ return mPort;
+ }
+
@Override
public void run() {
try {