blob: 127107fd630ce2e8f68fca441260cf0e9f6321ee [file] [log] [blame]
Steve Block2bc69912009-07-30 14:45:13 +01001/*
2 * Copyright (C) 2009 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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080018
Bijan Amirzada41242f22014-03-21 12:12:18 -070019import com.android.browser.R;
Steve Block2bc69912009-07-30 14:45:13 +010020
21import android.content.Context;
Steve Block8844d192009-09-29 13:14:41 +010022import android.net.Uri;
Steve Block2bc69912009-07-30 14:45:13 +010023import android.util.AttributeSet;
Steve Blockc9f6d7a2009-10-16 18:19:08 +010024import android.view.Gravity;
Steve Block2bc69912009-07-30 14:45:13 +010025import android.view.View;
Steve Block2bc69912009-07-30 14:45:13 +010026import android.webkit.GeolocationPermissions;
27import android.widget.Button;
28import android.widget.CheckBox;
John Reck7c6e1c92011-06-30 11:55:55 -070029import android.widget.RelativeLayout;
Steve Block2bc69912009-07-30 14:45:13 +010030import android.widget.TextView;
Steve Blockc9f6d7a2009-10-16 18:19:08 +010031import android.widget.Toast;
Steve Block2bc69912009-07-30 14:45:13 +010032
John Reck7c6e1c92011-06-30 11:55:55 -070033public class GeolocationPermissionsPrompt extends RelativeLayout {
Steve Block2bc69912009-07-30 14:45:13 +010034 private TextView mMessage;
35 private Button mShareButton;
36 private Button mDontShareButton;
37 private CheckBox mRemember;
38 private GeolocationPermissions.Callback mCallback;
39 private String mOrigin;
40
41 public GeolocationPermissionsPrompt(Context context) {
42 this(context, null);
43 }
44
45 public GeolocationPermissionsPrompt(Context context, AttributeSet attrs) {
46 super(context, attrs);
Grace Kloba50c241e2010-04-20 11:07:50 -070047 }
Steve Block2bc69912009-07-30 14:45:13 +010048
John Reck7c6e1c92011-06-30 11:55:55 -070049 @Override
50 protected void onFinishInflate() {
51 super.onFinishInflate();
52 init();
53 }
54
55 private void init() {
Steve Block2bc69912009-07-30 14:45:13 +010056 mMessage = (TextView) findViewById(R.id.message);
57 mShareButton = (Button) findViewById(R.id.share_button);
58 mDontShareButton = (Button) findViewById(R.id.dont_share_button);
59 mRemember = (CheckBox) findViewById(R.id.remember);
Grace Kloba50c241e2010-04-20 11:07:50 -070060
Grace Kloba50c241e2010-04-20 11:07:50 -070061 mShareButton.setOnClickListener(new View.OnClickListener() {
62 public void onClick(View v) {
John Reck7c6e1c92011-06-30 11:55:55 -070063 handleButtonClick(true);
Grace Kloba50c241e2010-04-20 11:07:50 -070064 }
65 });
66 mDontShareButton.setOnClickListener(new View.OnClickListener() {
67 public void onClick(View v) {
John Reck7c6e1c92011-06-30 11:55:55 -070068 handleButtonClick(false);
Grace Kloba50c241e2010-04-20 11:07:50 -070069 }
70 });
Steve Block2bc69912009-07-30 14:45:13 +010071 }
72
73 /**
74 * Shows the prompt for the given origin. When the user clicks on one of
75 * the buttons, the supplied callback is be called.
76 */
77 public void show(String origin, GeolocationPermissions.Callback callback) {
78 mOrigin = origin;
79 mCallback = callback;
Steve Block8844d192009-09-29 13:14:41 +010080 Uri uri = Uri.parse(mOrigin);
81 setMessage("http".equals(uri.getScheme()) ? mOrigin.substring(7) : mOrigin);
Steve Block97557cf2009-09-30 17:46:24 +010082 // The checkbox should always be intially checked.
83 mRemember.setChecked(true);
John Reck7c6e1c92011-06-30 11:55:55 -070084 setVisibility(View.VISIBLE);
Steve Block2bc69912009-07-30 14:45:13 +010085 }
86
87 /**
88 * Hides the prompt.
89 */
90 public void hide() {
John Reck7c6e1c92011-06-30 11:55:55 -070091 setVisibility(View.GONE);
Steve Block2bc69912009-07-30 14:45:13 +010092 }
93
94 /**
Steve Block2bc69912009-07-30 14:45:13 +010095 * Handles a click on one the buttons by invoking the callback.
96 */
97 private void handleButtonClick(boolean allow) {
John Reck7c6e1c92011-06-30 11:55:55 -070098 hide();
Steve Blockc9f6d7a2009-10-16 18:19:08 +010099
100 boolean remember = mRemember.isChecked();
101 if (remember) {
102 Toast toast = Toast.makeText(
103 getContext(),
104 allow ? R.string.geolocation_permissions_prompt_toast_allowed :
105 R.string.geolocation_permissions_prompt_toast_disallowed,
106 Toast.LENGTH_LONG);
107 toast.setGravity(Gravity.BOTTOM, 0, 0);
108 toast.show();
109 }
110
Steve Block2bc69912009-07-30 14:45:13 +0100111 mCallback.invoke(mOrigin, allow, remember);
112 }
113
114 /**
115 * Sets the prompt's message.
116 */
117 private void setMessage(CharSequence origin) {
118 mMessage.setText(String.format(
119 getResources().getString(R.string.geolocation_permissions_prompt_message),
120 origin));
121 }
Steve Block2bc69912009-07-30 14:45:13 +0100122}