| Index: chrome/android/java/src/org/chromium/chrome/browser/permissions/AndroidPermissionRequester.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/infobar/PermissionInfoBar.java b/chrome/android/java/src/org/chromium/chrome/browser/permissions/AndroidPermissionRequester.java
|
| similarity index 54%
|
| copy from chrome/android/java/src/org/chromium/chrome/browser/infobar/PermissionInfoBar.java
|
| copy to chrome/android/java/src/org/chromium/chrome/browser/permissions/AndroidPermissionRequester.java
|
| index bb5880d610d1dee826d058064ae5e5b324c612f1..7dac3946b3dbc59e02eddfeb712804b41b78c103 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/infobar/PermissionInfoBar.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/permissions/AndroidPermissionRequester.java
|
| @@ -2,37 +2,30 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -package org.chromium.chrome.browser.infobar;
|
| +package org.chromium.chrome.browser.permissions;
|
|
|
| import android.app.Activity;
|
| import android.content.DialogInterface;
|
| import android.content.pm.PackageManager;
|
| -import android.graphics.Bitmap;
|
| import android.support.v7.app.AlertDialog;
|
| -import android.support.v7.widget.SwitchCompat;
|
| import android.util.SparseArray;
|
| import android.view.View;
|
| import android.widget.TextView;
|
|
|
| -import org.chromium.base.annotations.CalledByNative;
|
| import org.chromium.chrome.R;
|
| import org.chromium.chrome.browser.ContentSettingsType;
|
| -import org.chromium.chrome.browser.ResourceId;
|
| import org.chromium.chrome.browser.preferences.PrefServiceBridge;
|
| -import org.chromium.chrome.browser.tab.Tab;
|
| import org.chromium.ui.base.WindowAndroid;
|
| import org.chromium.ui.base.WindowAndroid.PermissionCallback;
|
|
|
| /**
|
| - * An infobar used for prompting the user to grant a web API permission.
|
| - *
|
| + * An object to handle requesting native permissions from Android when the user grants a website
|
| + * a permission.
|
| */
|
| -public class PermissionInfoBar extends ConfirmInfoBar {
|
| -
|
| - /** Whether or not to show a toggle for opting out of persisting the decision. */
|
| - private boolean mShowPersistenceToggle;
|
| +public class AndroidPermissionRequester {
|
|
|
| private WindowAndroid mWindowAndroid;
|
| + private RequestDelegate mDelegate;
|
|
|
| /**
|
| * Mapping between the required {@link ContentSettingsType}s and their associated Android
|
| @@ -41,47 +34,24 @@ public class PermissionInfoBar extends ConfirmInfoBar {
|
| */
|
| private SparseArray<String> mContentSettingsToPermissionsMap;
|
|
|
| - protected PermissionInfoBar(int iconDrawableId, Bitmap iconBitmap, String message,
|
| - String linkText, String primaryButtonText, String secondaryButtonText,
|
| - boolean showPersistenceToggle) {
|
| - super(iconDrawableId, iconBitmap, message, linkText, primaryButtonText,
|
| - secondaryButtonText);
|
| - mShowPersistenceToggle = showPersistenceToggle;
|
| - }
|
| -
|
| - @Override
|
| - public void createContent(InfoBarLayout layout) {
|
| - super.createContent(layout);
|
| -
|
| - if (mShowPersistenceToggle) {
|
| - InfoBarControlLayout controlLayout = layout.addControlLayout();
|
| - String description =
|
| - layout.getContext().getString(R.string.permission_prompt_persist_text);
|
| - controlLayout.addSwitch(
|
| - 0, 0, description, R.id.permission_infobar_persist_toggle, true);
|
| - }
|
| + /**
|
| + * An interface for classes which need to be informed of the outcome of asking a user to grant an
|
| + * Android permission.
|
| + */
|
| + public interface RequestDelegate {
|
| + void onAndroidPermissionAccepted();
|
| + void onAndroidPermissionCanceled();
|
| }
|
|
|
| - @Override
|
| - public void onTabReparented(Tab tab) {
|
| - mWindowAndroid = tab.getWindowAndroid();
|
| + public AndroidPermissionRequester(WindowAndroid windowAndroid,
|
| + RequestDelegate delegate, int[] contentSettings) {
|
| + mWindowAndroid = windowAndroid;
|
| + mDelegate = delegate;
|
| + mContentSettingsToPermissionsMap = generatePermissionsMapping(contentSettings);
|
| }
|
|
|
| - /**
|
| - * Specifies the {@link ContentSettingsType}s that are controlled by this InfoBar.
|
| - *
|
| - * @param windowAndroid The WindowAndroid that will be used to check for the necessary
|
| - * permissions.
|
| - * @param contentSettings The list of {@link ContentSettingsType}s whose access is guarded
|
| - * by this InfoBar.
|
| - */
|
| - protected void setContentSettings(
|
| - WindowAndroid windowAndroid, int[] contentSettings) {
|
| + public void setWindowAndroid(WindowAndroid windowAndroid) {
|
| mWindowAndroid = windowAndroid;
|
| - assert windowAndroid != null
|
| - : "A WindowAndroid must be specified to request access to content settings";
|
| -
|
| - mContentSettingsToPermissionsMap = generatePermissionsMapping(contentSettings);
|
| }
|
|
|
| private SparseArray<String> generatePermissionsMapping(int[] contentSettings) {
|
| @@ -119,19 +89,12 @@ public class PermissionInfoBar extends ConfirmInfoBar {
|
| return R.string.infobar_missing_multiple_permissions_text;
|
| }
|
|
|
| - @Override
|
| - public void onButtonClicked(final boolean isPrimaryButton) {
|
| - if (mWindowAndroid == null || !isPrimaryButton || getContext() == null
|
| - || mContentSettingsToPermissionsMap == null
|
| - || mContentSettingsToPermissionsMap.size() == 0) {
|
| - onButtonClickedInternal(isPrimaryButton);
|
| - return;
|
| - }
|
| -
|
| - requestAndroidPermissions();
|
| + public boolean shouldSkipPermissionRequest() {
|
| + return (mWindowAndroid == null || mContentSettingsToPermissionsMap == null
|
| + || mContentSettingsToPermissionsMap.size() == 0);
|
| }
|
|
|
| - private void requestAndroidPermissions() {
|
| + public void requestAndroidPermissions() {
|
| PermissionCallback callback = new PermissionCallback() {
|
| @Override
|
| public void onRequestPermissionsResult(
|
| @@ -174,14 +137,14 @@ public class PermissionInfoBar extends ConfirmInfoBar {
|
| .setOnCancelListener(new DialogInterface.OnCancelListener() {
|
| @Override
|
| public void onCancel(DialogInterface dialog) {
|
| - onCloseButtonClicked();
|
| + mDelegate.onAndroidPermissionCanceled();
|
| }
|
| });
|
| builder.create().show();
|
| } else if (deniedCount > 0) {
|
| - onCloseButtonClicked();
|
| + mDelegate.onAndroidPermissionCanceled();
|
| } else {
|
| - onButtonClickedInternal(true);
|
| + mDelegate.onAndroidPermissionAccepted();
|
| }
|
| }
|
| };
|
| @@ -193,49 +156,4 @@ public class PermissionInfoBar extends ConfirmInfoBar {
|
| mWindowAndroid.requestPermissions(permissionsToRequest, callback);
|
| }
|
|
|
| - private void onButtonClickedInternal(boolean isPrimaryButton) {
|
| - super.onButtonClicked(isPrimaryButton);
|
| - }
|
| -
|
| - /**
|
| - * Returns true if the persist switch exists and is toggled on.
|
| - */
|
| - @CalledByNative
|
| - protected boolean isPersistSwitchOn() {
|
| - SwitchCompat persistSwitch = (SwitchCompat) getView().findViewById(
|
| - R.id.permission_infobar_persist_toggle);
|
| - if (mShowPersistenceToggle && persistSwitch != null) {
|
| - return persistSwitch.isChecked();
|
| - }
|
| - return false;
|
| - }
|
| -
|
| - /**
|
| - * Creates and begins the process for showing a PermissionInfoBar.
|
| - * @param windowAndroid The owning window for the infobar.
|
| - * @param enumeratedIconId ID corresponding to the icon that will be shown for the infobar.
|
| - * The ID must have been mapped using the ResourceMapper class before
|
| - * passing it to this function.
|
| - * @param iconBitmap Bitmap to use if there is no equivalent Java resource for
|
| - * enumeratedIconId.
|
| - * @param message Message to display to the user indicating what the infobar is for.
|
| - * @param linkText Link text to display in addition to the message.
|
| - * @param buttonOk String to display on the OK button.
|
| - * @param buttonCancel String to display on the Cancel button.
|
| - * @param contentSettings The list of ContentSettingTypes being requested by this infobar.
|
| - * @param showPersistenceToggle Whether or not a toggle to opt-out of persisting a decision
|
| - * should be displayed.
|
| - */
|
| - @CalledByNative
|
| - private static PermissionInfoBar create(WindowAndroid windowAndroid, int enumeratedIconId,
|
| - Bitmap iconBitmap, String message, String linkText, String buttonOk,
|
| - String buttonCancel, int[] contentSettings, boolean showPersistenceToggle) {
|
| - int drawableId = ResourceId.mapToDrawableId(enumeratedIconId);
|
| -
|
| - PermissionInfoBar infoBar = new PermissionInfoBar(drawableId, iconBitmap, message, linkText,
|
| - buttonOk, buttonCancel, showPersistenceToggle);
|
| - infoBar.setContentSettings(windowAndroid, contentSettings);
|
| -
|
| - return infoBar;
|
| - }
|
| }
|
|
|