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 
19 import android.content.Context;
20 import android.net.Uri;
21 import android.util.AttributeSet;
22 import android.view.Gravity;
23 import android.view.View;
24 import android.webkit.GeolocationPermissions;
25 import android.webkit.PermissionRequest;
26 import android.widget.Button;
27 import android.widget.CheckBox;
28 import android.widget.RelativeLayout;
29 import android.widget.TextView;
30 import android.widget.Toast;
31 
32 import java.util.Enumeration;
33 import java.util.Vector;
34 
35 public class PermissionsPrompt extends RelativeLayout {
36     private TextView mMessage;
37     private Button mAllowButton;
38     private Button mDenyButton;
39     private CheckBox mRemember;
40     private PermissionRequest mRequest;
41 
PermissionsPrompt(Context context)42     public PermissionsPrompt(Context context) {
43         this(context, null);
44     }
45 
PermissionsPrompt(Context context, AttributeSet attrs)46     public PermissionsPrompt(Context context, AttributeSet attrs) {
47         super(context, attrs);
48     }
49 
50     @Override
onFinishInflate()51     protected void onFinishInflate() {
52         super.onFinishInflate();
53         init();
54     }
55 
init()56     private void init() {
57         mMessage = (TextView) findViewById(R.id.message);
58         mAllowButton = (Button) findViewById(R.id.allow_button);
59         mDenyButton = (Button) findViewById(R.id.deny_button);
60         mRemember = (CheckBox) findViewById(R.id.remember);
61 
62         mAllowButton.setOnClickListener(new View.OnClickListener() {
63             @Override
64             public void onClick(View v) {
65                 handleButtonClick(true);
66             }
67         });
68         mDenyButton.setOnClickListener(new View.OnClickListener() {
69             @Override
70             public void onClick(View v) {
71                 handleButtonClick(false);
72             }
73         });
74     }
75 
show(PermissionRequest request)76     public void show(PermissionRequest request) {
77         mRequest = request;
78         setMessage();
79         mRemember.setChecked(true);
80         setVisibility(View.VISIBLE);
81     }
82 
setMessage()83     public void setMessage() {
84         String[] resources = mRequest.getResources();
85         Vector<String> strings = new Vector<String>();
86         for (String resource : resources) {
87             if (resource.equals(PermissionRequest.RESOURCE_VIDEO_CAPTURE))
88                 strings.add(getResources().getString(R.string.resource_video_capture));
89             else if (resource.equals(PermissionRequest.RESOURCE_AUDIO_CAPTURE))
90                 strings.add(getResources().getString(R.string.resource_audio_capture));
91             else if (resource.equals(PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID))
92                 strings.add(getResources().getString(R.string.resource_protected_media_id));
93         }
94         if (strings.isEmpty()) return;
95 
96         Enumeration<String> e = strings.elements();
97         StringBuilder sb = new StringBuilder(e.nextElement());
98         if (e.hasMoreElements()) {
99             sb.append(", ");
100             sb.append(e.nextElement());
101         }
102 
103         mMessage.setText(String.format(
104                 getResources().getString(R.string.permissions_prompt_message),
105                 mRequest.getOrigin(), sb.toString()));
106     }
107 
108     /**
109      * Hides the prompt.
110      */
hide()111     public void hide() {
112         setVisibility(View.GONE);
113     }
114 
115     /**
116      * Handles a click on one the buttons by invoking the callback.
117      */
handleButtonClick(boolean allow)118     private void handleButtonClick(boolean allow) {
119         hide();
120         if (allow)
121             mRequest.grant(mRequest.getResources());
122         else
123             mRequest.deny();
124     }
125 }
126