blob: cab6deb16c8b99cf8abdee04bae6d5c52f81f2f1 [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
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070019import org.codeaurora.swe.GeolocationPermissions;
20import org.json.JSONArray;
21
Steve Block2bc69912009-07-30 14:45:13 +010022import android.content.Context;
Steve Block8844d192009-09-29 13:14:41 +010023import android.net.Uri;
Steve Block2bc69912009-07-30 14:45:13 +010024import android.util.AttributeSet;
Steve Blockc9f6d7a2009-10-16 18:19:08 +010025import android.view.Gravity;
Steve Block2bc69912009-07-30 14:45:13 +010026import android.view.View;
Steve Block2bc69912009-07-30 14:45:13 +010027import android.widget.Button;
28import android.widget.CheckBox;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070029import android.widget.CompoundButton;
Enrico Ros1f5a0952014-11-18 20:15:48 -080030import android.widget.LinearLayout;
Steve Block2bc69912009-07-30 14:45:13 +010031import android.widget.TextView;
Steve Blockc9f6d7a2009-10-16 18:19:08 +010032import android.widget.Toast;
Steve Block2bc69912009-07-30 14:45:13 +010033
Enrico Ros1f5a0952014-11-18 20:15:48 -080034public class GeolocationPermissionsPrompt extends LinearLayout {
Steve Block2bc69912009-07-30 14:45:13 +010035 private TextView mMessage;
36 private Button mShareButton;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070037 private Button mShareForLimitedTimeButton;
Steve Block2bc69912009-07-30 14:45:13 +010038 private Button mDontShareButton;
39 private CheckBox mRemember;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070040 private android.webkit.GeolocationPermissions.Callback mCallback;
Steve Block2bc69912009-07-30 14:45:13 +010041 private String mOrigin;
42
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070043 private static final long MILLIS_PER_DAY = 86400000;
44
Steve Block2bc69912009-07-30 14:45:13 +010045 public GeolocationPermissionsPrompt(Context context) {
46 this(context, null);
47 }
48
49 public GeolocationPermissionsPrompt(Context context, AttributeSet attrs) {
50 super(context, attrs);
Grace Kloba50c241e2010-04-20 11:07:50 -070051 }
Steve Block2bc69912009-07-30 14:45:13 +010052
John Reck7c6e1c92011-06-30 11:55:55 -070053 @Override
54 protected void onFinishInflate() {
55 super.onFinishInflate();
56 init();
57 }
58
59 private void init() {
Steve Block2bc69912009-07-30 14:45:13 +010060 mMessage = (TextView) findViewById(R.id.message);
61 mShareButton = (Button) findViewById(R.id.share_button);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070062 mShareForLimitedTimeButton = (Button)
63 findViewById(R.id.share_for_limited_time_button);
Steve Block2bc69912009-07-30 14:45:13 +010064 mDontShareButton = (Button) findViewById(R.id.dont_share_button);
65 mRemember = (CheckBox) findViewById(R.id.remember);
Grace Kloba50c241e2010-04-20 11:07:50 -070066
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070067 mRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
68 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
69 mShareForLimitedTimeButton.setEnabled(isChecked);
70 }
71 });
72
Grace Kloba50c241e2010-04-20 11:07:50 -070073 mShareButton.setOnClickListener(new View.OnClickListener() {
74 public void onClick(View v) {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070075 handleButtonClick(true, GeolocationPermissions.DO_NOT_EXPIRE);
76 }
77 });
78 mShareForLimitedTimeButton.setOnClickListener(new View.OnClickListener() {
79 public void onClick(View v) {
80 handleButtonClick(true, System.currentTimeMillis() + MILLIS_PER_DAY);
Grace Kloba50c241e2010-04-20 11:07:50 -070081 }
82 });
83 mDontShareButton.setOnClickListener(new View.OnClickListener() {
84 public void onClick(View v) {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070085 handleButtonClick(false, GeolocationPermissions.DO_NOT_EXPIRE);
Grace Kloba50c241e2010-04-20 11:07:50 -070086 }
87 });
Steve Block2bc69912009-07-30 14:45:13 +010088 }
89
90 /**
91 * Shows the prompt for the given origin. When the user clicks on one of
92 * the buttons, the supplied callback is be called.
93 */
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070094 public void show(String origin,
95 android.webkit.GeolocationPermissions.Callback callback) {
Steve Block2bc69912009-07-30 14:45:13 +010096 mOrigin = origin;
97 mCallback = callback;
Steve Block8844d192009-09-29 13:14:41 +010098 Uri uri = Uri.parse(mOrigin);
99 setMessage("http".equals(uri.getScheme()) ? mOrigin.substring(7) : mOrigin);
Steve Block97557cf2009-09-30 17:46:24 +0100100 // The checkbox should always be intially checked.
101 mRemember.setChecked(true);
John Reck7c6e1c92011-06-30 11:55:55 -0700102 setVisibility(View.VISIBLE);
Steve Block2bc69912009-07-30 14:45:13 +0100103 }
104
105 /**
106 * Hides the prompt.
107 */
108 public void hide() {
John Reck7c6e1c92011-06-30 11:55:55 -0700109 setVisibility(View.GONE);
Steve Block2bc69912009-07-30 14:45:13 +0100110 }
111
112 /**
Steve Block2bc69912009-07-30 14:45:13 +0100113 * Handles a click on one the buttons by invoking the callback.
114 */
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700115 private void handleButtonClick(boolean allow, long expirationTime) {
John Reck7c6e1c92011-06-30 11:55:55 -0700116 hide();
Steve Blockc9f6d7a2009-10-16 18:19:08 +0100117
118 boolean remember = mRemember.isChecked();
119 if (remember) {
120 Toast toast = Toast.makeText(
121 getContext(),
122 allow ? R.string.geolocation_permissions_prompt_toast_allowed :
123 R.string.geolocation_permissions_prompt_toast_disallowed,
124 Toast.LENGTH_LONG);
125 toast.setGravity(Gravity.BOTTOM, 0, 0);
126 toast.show();
127 }
128
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700129 // Encode the expirationTime and origin as a JSON string.
130 JSONArray jsonArray = new JSONArray();
131 jsonArray.put(expirationTime);
132 jsonArray.put(mOrigin);
133 mCallback.invoke(jsonArray.toString(), allow, remember);
Steve Block2bc69912009-07-30 14:45:13 +0100134 }
135
136 /**
137 * Sets the prompt's message.
138 */
139 private void setMessage(CharSequence origin) {
140 mMessage.setText(String.format(
141 getResources().getString(R.string.geolocation_permissions_prompt_message),
142 origin));
143 }
Steve Block2bc69912009-07-30 14:45:13 +0100144}