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