Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 1 | /* |
| 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 Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 18 | |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 19 | import org.codeaurora.swe.GeolocationPermissions; |
| 20 | import org.json.JSONArray; |
| 21 | |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 22 | import android.content.Context; |
Steve Block | 8844d19 | 2009-09-29 13:14:41 +0100 | [diff] [blame] | 23 | import android.net.Uri; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Steve Block | c9f6d7a | 2009-10-16 18:19:08 +0100 | [diff] [blame] | 25 | import android.view.Gravity; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 26 | import android.view.View; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 27 | import android.widget.Button; |
| 28 | import android.widget.CheckBox; |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 29 | import android.widget.CompoundButton; |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 30 | import android.widget.LinearLayout; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 31 | import android.widget.TextView; |
Steve Block | c9f6d7a | 2009-10-16 18:19:08 +0100 | [diff] [blame] | 32 | import android.widget.Toast; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 33 | |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 34 | public class GeolocationPermissionsPrompt extends LinearLayout { |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 35 | private TextView mMessage; |
| 36 | private Button mShareButton; |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 37 | private Button mShareForLimitedTimeButton; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 38 | private Button mDontShareButton; |
| 39 | private CheckBox mRemember; |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 40 | private android.webkit.GeolocationPermissions.Callback mCallback; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 41 | private String mOrigin; |
| 42 | |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 43 | private static final long MILLIS_PER_DAY = 86400000; |
| 44 | |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 45 | public GeolocationPermissionsPrompt(Context context) { |
| 46 | this(context, null); |
| 47 | } |
| 48 | |
| 49 | public GeolocationPermissionsPrompt(Context context, AttributeSet attrs) { |
| 50 | super(context, attrs); |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 51 | } |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 52 | |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 53 | @Override |
| 54 | protected void onFinishInflate() { |
| 55 | super.onFinishInflate(); |
| 56 | init(); |
| 57 | } |
| 58 | |
| 59 | private void init() { |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 60 | mMessage = (TextView) findViewById(R.id.message); |
| 61 | mShareButton = (Button) findViewById(R.id.share_button); |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 62 | mShareForLimitedTimeButton = (Button) |
| 63 | findViewById(R.id.share_for_limited_time_button); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 64 | mDontShareButton = (Button) findViewById(R.id.dont_share_button); |
| 65 | mRemember = (CheckBox) findViewById(R.id.remember); |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 66 | |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 67 | mRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { |
| 68 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 69 | mShareForLimitedTimeButton.setEnabled(isChecked); |
| 70 | } |
| 71 | }); |
| 72 | |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 73 | mShareButton.setOnClickListener(new View.OnClickListener() { |
| 74 | public void onClick(View v) { |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 75 | 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 Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 81 | } |
| 82 | }); |
| 83 | mDontShareButton.setOnClickListener(new View.OnClickListener() { |
| 84 | public void onClick(View v) { |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 85 | handleButtonClick(false, GeolocationPermissions.DO_NOT_EXPIRE); |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 86 | } |
| 87 | }); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 88 | } |
| 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. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 94 | public void show(String origin, |
| 95 | android.webkit.GeolocationPermissions.Callback callback) { |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 96 | mOrigin = origin; |
| 97 | mCallback = callback; |
Steve Block | 8844d19 | 2009-09-29 13:14:41 +0100 | [diff] [blame] | 98 | Uri uri = Uri.parse(mOrigin); |
| 99 | setMessage("http".equals(uri.getScheme()) ? mOrigin.substring(7) : mOrigin); |
Steve Block | 97557cf | 2009-09-30 17:46:24 +0100 | [diff] [blame] | 100 | // The checkbox should always be intially checked. |
| 101 | mRemember.setChecked(true); |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 102 | setVisibility(View.VISIBLE); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Hides the prompt. |
| 107 | */ |
| 108 | public void hide() { |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 109 | setVisibility(View.GONE); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 113 | * Handles a click on one the buttons by invoking the callback. |
| 114 | */ |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 115 | private void handleButtonClick(boolean allow, long expirationTime) { |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 116 | hide(); |
Steve Block | c9f6d7a | 2009-10-16 18:19:08 +0100 | [diff] [blame] | 117 | |
| 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. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 129 | // 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 Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 134 | } |
| 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 Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 144 | } |