Tao Bai | e1b6b3b | 2014-05-13 13:55:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
LuK1337 | 2be2de9 | 2015-12-17 21:32:46 +0800 | [diff] [blame] | 19 | import android.Manifest; |
| 20 | import android.app.Activity; |
Tao Bai | e1b6b3b | 2014-05-13 13:55:52 -0700 | [diff] [blame] | 21 | import android.content.Context; |
LuK1337 | 2be2de9 | 2015-12-17 21:32:46 +0800 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.content.pm.PackageManager; |
Tao Bai | e1b6b3b | 2014-05-13 13:55:52 -0700 | [diff] [blame] | 24 | import android.net.Uri; |
| 25 | import android.util.AttributeSet; |
| 26 | import android.view.Gravity; |
| 27 | import android.view.View; |
| 28 | import android.webkit.GeolocationPermissions; |
| 29 | import android.webkit.PermissionRequest; |
| 30 | import android.widget.Button; |
| 31 | import android.widget.CheckBox; |
| 32 | import android.widget.RelativeLayout; |
| 33 | import android.widget.TextView; |
| 34 | import android.widget.Toast; |
| 35 | |
LuK1337 | 2be2de9 | 2015-12-17 21:32:46 +0800 | [diff] [blame] | 36 | import java.util.ArrayList; |
Tao Bai | e1b6b3b | 2014-05-13 13:55:52 -0700 | [diff] [blame] | 37 | import java.util.Enumeration; |
LuK1337 | 2be2de9 | 2015-12-17 21:32:46 +0800 | [diff] [blame] | 38 | import java.util.List; |
Tao Bai | e1b6b3b | 2014-05-13 13:55:52 -0700 | [diff] [blame] | 39 | import java.util.Vector; |
| 40 | |
| 41 | public class PermissionsPrompt extends RelativeLayout { |
| 42 | private TextView mMessage; |
| 43 | private Button mAllowButton; |
| 44 | private Button mDenyButton; |
| 45 | private CheckBox mRemember; |
LuK1337 | 2be2de9 | 2015-12-17 21:32:46 +0800 | [diff] [blame] | 46 | private Context mContext; |
Tao Bai | e1b6b3b | 2014-05-13 13:55:52 -0700 | [diff] [blame] | 47 | private PermissionRequest mRequest; |
| 48 | |
| 49 | public PermissionsPrompt(Context context) { |
| 50 | this(context, null); |
LuK1337 | 2be2de9 | 2015-12-17 21:32:46 +0800 | [diff] [blame] | 51 | mContext = context; |
Tao Bai | e1b6b3b | 2014-05-13 13:55:52 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public PermissionsPrompt(Context context, AttributeSet attrs) { |
| 55 | super(context, attrs); |
LuK1337 | 2be2de9 | 2015-12-17 21:32:46 +0800 | [diff] [blame] | 56 | mContext = context; |
Tao Bai | e1b6b3b | 2014-05-13 13:55:52 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | protected void onFinishInflate() { |
| 61 | super.onFinishInflate(); |
| 62 | init(); |
| 63 | } |
| 64 | |
| 65 | private void init() { |
| 66 | mMessage = (TextView) findViewById(R.id.message); |
| 67 | mAllowButton = (Button) findViewById(R.id.allow_button); |
| 68 | mDenyButton = (Button) findViewById(R.id.deny_button); |
| 69 | mRemember = (CheckBox) findViewById(R.id.remember); |
Tao Bai | e1b6b3b | 2014-05-13 13:55:52 -0700 | [diff] [blame] | 70 | |
| 71 | mAllowButton.setOnClickListener(new View.OnClickListener() { |
| 72 | @Override |
| 73 | public void onClick(View v) { |
| 74 | handleButtonClick(true); |
| 75 | } |
| 76 | }); |
| 77 | mDenyButton.setOnClickListener(new View.OnClickListener() { |
| 78 | @Override |
| 79 | public void onClick(View v) { |
| 80 | handleButtonClick(false); |
| 81 | } |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | public void show(PermissionRequest request) { |
| 86 | mRequest = request; |
| 87 | setMessage(); |
| 88 | mRemember.setChecked(true); |
| 89 | setVisibility(View.VISIBLE); |
| 90 | } |
| 91 | |
| 92 | public void setMessage() { |
Tao Bai | 9971e3f | 2014-07-16 14:10:22 -0700 | [diff] [blame] | 93 | String[] resources = mRequest.getResources(); |
Tao Bai | e1b6b3b | 2014-05-13 13:55:52 -0700 | [diff] [blame] | 94 | Vector<String> strings = new Vector<String>(); |
Tao Bai | 9971e3f | 2014-07-16 14:10:22 -0700 | [diff] [blame] | 95 | for (String resource : resources) { |
| 96 | if (resource.equals(PermissionRequest.RESOURCE_VIDEO_CAPTURE)) |
| 97 | strings.add(getResources().getString(R.string.resource_video_capture)); |
| 98 | else if (resource.equals(PermissionRequest.RESOURCE_AUDIO_CAPTURE)) |
| 99 | strings.add(getResources().getString(R.string.resource_audio_capture)); |
| 100 | else if (resource.equals(PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID)) |
| 101 | strings.add(getResources().getString(R.string.resource_protected_media_id)); |
| 102 | } |
Tao Bai | e1b6b3b | 2014-05-13 13:55:52 -0700 | [diff] [blame] | 103 | if (strings.isEmpty()) return; |
| 104 | |
| 105 | Enumeration<String> e = strings.elements(); |
| 106 | StringBuilder sb = new StringBuilder(e.nextElement()); |
| 107 | if (e.hasMoreElements()) { |
| 108 | sb.append(", "); |
| 109 | sb.append(e.nextElement()); |
| 110 | } |
| 111 | |
| 112 | mMessage.setText(String.format( |
| 113 | getResources().getString(R.string.permissions_prompt_message), |
| 114 | mRequest.getOrigin(), sb.toString())); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Hides the prompt. |
| 119 | */ |
| 120 | public void hide() { |
| 121 | setVisibility(View.GONE); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Handles a click on one the buttons by invoking the callback. |
| 126 | */ |
| 127 | private void handleButtonClick(boolean allow) { |
| 128 | hide(); |
LuK1337 | 2be2de9 | 2015-12-17 21:32:46 +0800 | [diff] [blame] | 129 | if (allow) { |
| 130 | String[] resources = mRequest.getResources(); |
| 131 | List<String> permissionsToRequest = new ArrayList<String>(); |
| 132 | |
| 133 | for (String resource : resources) { |
| 134 | if (resource.equals(PermissionRequest.RESOURCE_VIDEO_CAPTURE) && |
| 135 | mContext.checkSelfPermission(Manifest.permission.CAMERA) != |
| 136 | PackageManager.PERMISSION_GRANTED) { |
| 137 | permissionsToRequest.add(Manifest.permission.CAMERA); |
| 138 | } else if (resource.equals(PermissionRequest.RESOURCE_AUDIO_CAPTURE) && |
| 139 | mContext.checkSelfPermission(Manifest.permission.RECORD_AUDIO) != |
| 140 | PackageManager.PERMISSION_GRANTED) { |
| 141 | permissionsToRequest.add(Manifest.permission.RECORD_AUDIO); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (permissionsToRequest.size() > 0) { |
| 146 | String[] permissions = permissionsToRequest.toArray( |
| 147 | new String[permissionsToRequest.size()]); |
| 148 | ((Activity) mContext).requestPermissions(permissions, 1); |
| 149 | Intent intent = new Intent(mContext, PermissionsActivity.class); |
| 150 | mContext.startActivity(intent); |
| 151 | } else { |
| 152 | mRequest.grant(mRequest.getResources()); |
| 153 | } |
| 154 | } else { |
Tao Bai | e1b6b3b | 2014-05-13 13:55:52 -0700 | [diff] [blame] | 155 | mRequest.deny(); |
LuK1337 | 2be2de9 | 2015-12-17 21:32:46 +0800 | [diff] [blame] | 156 | } |
Tao Bai | e1b6b3b | 2014-05-13 13:55:52 -0700 | [diff] [blame] | 157 | } |
| 158 | } |