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 | |
| 21 | import android.content.Context; |
Steve Block | 8844d19 | 2009-09-29 13:14:41 +0100 | [diff] [blame] | 22 | import android.net.Uri; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 23 | import android.util.AttributeSet; |
Steve Block | c9f6d7a | 2009-10-16 18:19:08 +0100 | [diff] [blame] | 24 | import android.view.Gravity; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 25 | import android.view.View; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 26 | import android.webkit.GeolocationPermissions; |
| 27 | import android.widget.Button; |
| 28 | import android.widget.CheckBox; |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 29 | import android.widget.RelativeLayout; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 30 | import android.widget.TextView; |
Steve Block | c9f6d7a | 2009-10-16 18:19:08 +0100 | [diff] [blame] | 31 | import android.widget.Toast; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 32 | |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 33 | public class GeolocationPermissionsPrompt extends RelativeLayout { |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 34 | 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 Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 47 | } |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 48 | |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 49 | @Override |
| 50 | protected void onFinishInflate() { |
| 51 | super.onFinishInflate(); |
| 52 | init(); |
| 53 | } |
| 54 | |
| 55 | private void init() { |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 56 | 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 Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 60 | |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 61 | mShareButton.setOnClickListener(new View.OnClickListener() { |
| 62 | public void onClick(View v) { |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 63 | handleButtonClick(true); |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 64 | } |
| 65 | }); |
| 66 | mDontShareButton.setOnClickListener(new View.OnClickListener() { |
| 67 | public void onClick(View v) { |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 68 | handleButtonClick(false); |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 69 | } |
| 70 | }); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 71 | } |
| 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 Block | 8844d19 | 2009-09-29 13:14:41 +0100 | [diff] [blame] | 80 | Uri uri = Uri.parse(mOrigin); |
| 81 | setMessage("http".equals(uri.getScheme()) ? mOrigin.substring(7) : mOrigin); |
Steve Block | 97557cf | 2009-09-30 17:46:24 +0100 | [diff] [blame] | 82 | // The checkbox should always be intially checked. |
| 83 | mRemember.setChecked(true); |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 84 | setVisibility(View.VISIBLE); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Hides the prompt. |
| 89 | */ |
| 90 | public void hide() { |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 91 | setVisibility(View.GONE); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 95 | * Handles a click on one the buttons by invoking the callback. |
| 96 | */ |
| 97 | private void handleButtonClick(boolean allow) { |
John Reck | 7c6e1c9 | 2011-06-30 11:55:55 -0700 | [diff] [blame] | 98 | hide(); |
Steve Block | c9f6d7a | 2009-10-16 18:19:08 +0100 | [diff] [blame] | 99 | |
| 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 Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 111 | 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 Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 122 | } |