blob: d0e94ad2ac4982ac55c3f6b3d955a796ca4b1c99 [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
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070021import org.codeaurora.swe.GeolocationPermissions;
22import org.json.JSONArray;
23
Steve Block2bc69912009-07-30 14:45:13 +010024import android.content.Context;
Steve Block8844d192009-09-29 13:14:41 +010025import android.net.Uri;
Steve Block2bc69912009-07-30 14:45:13 +010026import android.util.AttributeSet;
Steve Blockc9f6d7a2009-10-16 18:19:08 +010027import android.view.Gravity;
Steve Block2bc69912009-07-30 14:45:13 +010028import android.view.View;
Steve Block2bc69912009-07-30 14:45:13 +010029import android.widget.Button;
30import android.widget.CheckBox;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070031import android.widget.CompoundButton;
John Reck7c6e1c92011-06-30 11:55:55 -070032import android.widget.RelativeLayout;
Steve Block2bc69912009-07-30 14:45:13 +010033import android.widget.TextView;
Steve Blockc9f6d7a2009-10-16 18:19:08 +010034import android.widget.Toast;
Steve Block2bc69912009-07-30 14:45:13 +010035
John Reck7c6e1c92011-06-30 11:55:55 -070036public class GeolocationPermissionsPrompt extends RelativeLayout {
Steve Block2bc69912009-07-30 14:45:13 +010037 private TextView mMessage;
38 private Button mShareButton;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070039 private Button mShareForLimitedTimeButton;
Steve Block2bc69912009-07-30 14:45:13 +010040 private Button mDontShareButton;
41 private CheckBox mRemember;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070042 private android.webkit.GeolocationPermissions.Callback mCallback;
Steve Block2bc69912009-07-30 14:45:13 +010043 private String mOrigin;
44
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070045 private static final long MILLIS_PER_DAY = 86400000;
46
Steve Block2bc69912009-07-30 14:45:13 +010047 public GeolocationPermissionsPrompt(Context context) {
48 this(context, null);
49 }
50
51 public GeolocationPermissionsPrompt(Context context, AttributeSet attrs) {
52 super(context, attrs);
Grace Kloba50c241e2010-04-20 11:07:50 -070053 }
Steve Block2bc69912009-07-30 14:45:13 +010054
John Reck7c6e1c92011-06-30 11:55:55 -070055 @Override
56 protected void onFinishInflate() {
57 super.onFinishInflate();
58 init();
59 }
60
61 private void init() {
Steve Block2bc69912009-07-30 14:45:13 +010062 mMessage = (TextView) findViewById(R.id.message);
63 mShareButton = (Button) findViewById(R.id.share_button);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070064 mShareForLimitedTimeButton = (Button)
65 findViewById(R.id.share_for_limited_time_button);
Steve Block2bc69912009-07-30 14:45:13 +010066 mDontShareButton = (Button) findViewById(R.id.dont_share_button);
67 mRemember = (CheckBox) findViewById(R.id.remember);
Grace Kloba50c241e2010-04-20 11:07:50 -070068
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070069 mRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
70 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
71 mShareForLimitedTimeButton.setEnabled(isChecked);
72 }
73 });
74
Grace Kloba50c241e2010-04-20 11:07:50 -070075 mShareButton.setOnClickListener(new View.OnClickListener() {
76 public void onClick(View v) {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070077 handleButtonClick(true, GeolocationPermissions.DO_NOT_EXPIRE);
78 }
79 });
80 mShareForLimitedTimeButton.setOnClickListener(new View.OnClickListener() {
81 public void onClick(View v) {
82 handleButtonClick(true, System.currentTimeMillis() + MILLIS_PER_DAY);
Grace Kloba50c241e2010-04-20 11:07:50 -070083 }
84 });
85 mDontShareButton.setOnClickListener(new View.OnClickListener() {
86 public void onClick(View v) {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070087 handleButtonClick(false, GeolocationPermissions.DO_NOT_EXPIRE);
Grace Kloba50c241e2010-04-20 11:07:50 -070088 }
89 });
Steve Block2bc69912009-07-30 14:45:13 +010090 }
91
92 /**
93 * Shows the prompt for the given origin. When the user clicks on one of
94 * the buttons, the supplied callback is be called.
95 */
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070096 public void show(String origin,
97 android.webkit.GeolocationPermissions.Callback callback) {
Steve Block2bc69912009-07-30 14:45:13 +010098 mOrigin = origin;
99 mCallback = callback;
Steve Block8844d192009-09-29 13:14:41 +0100100 Uri uri = Uri.parse(mOrigin);
101 setMessage("http".equals(uri.getScheme()) ? mOrigin.substring(7) : mOrigin);
Steve Block97557cf2009-09-30 17:46:24 +0100102 // The checkbox should always be intially checked.
103 mRemember.setChecked(true);
John Reck7c6e1c92011-06-30 11:55:55 -0700104 setVisibility(View.VISIBLE);
Steve Block2bc69912009-07-30 14:45:13 +0100105 }
106
107 /**
108 * Hides the prompt.
109 */
110 public void hide() {
John Reck7c6e1c92011-06-30 11:55:55 -0700111 setVisibility(View.GONE);
Steve Block2bc69912009-07-30 14:45:13 +0100112 }
113
114 /**
Steve Block2bc69912009-07-30 14:45:13 +0100115 * Handles a click on one the buttons by invoking the callback.
116 */
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700117 private void handleButtonClick(boolean allow, long expirationTime) {
John Reck7c6e1c92011-06-30 11:55:55 -0700118 hide();
Steve Blockc9f6d7a2009-10-16 18:19:08 +0100119
120 boolean remember = mRemember.isChecked();
121 if (remember) {
122 Toast toast = Toast.makeText(
123 getContext(),
124 allow ? R.string.geolocation_permissions_prompt_toast_allowed :
125 R.string.geolocation_permissions_prompt_toast_disallowed,
126 Toast.LENGTH_LONG);
127 toast.setGravity(Gravity.BOTTOM, 0, 0);
128 toast.show();
129 }
130
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700131 // Encode the expirationTime and origin as a JSON string.
132 JSONArray jsonArray = new JSONArray();
133 jsonArray.put(expirationTime);
134 jsonArray.put(mOrigin);
135 mCallback.invoke(jsonArray.toString(), allow, remember);
Steve Block2bc69912009-07-30 14:45:13 +0100136 }
137
138 /**
139 * Sets the prompt's message.
140 */
141 private void setMessage(CharSequence origin) {
142 mMessage.setText(String.format(
143 getResources().getString(R.string.geolocation_permissions_prompt_message),
144 origin));
145 }
Steve Block2bc69912009-07-30 14:45:13 +0100146}