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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.drawable.Drawable; |
Steve Block | 8844d19 | 2009-09-29 13:14:41 +0100 | [diff] [blame] | 21 | import android.net.Uri; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 22 | import android.util.AttributeSet; |
Steve Block | c9f6d7a | 2009-10-16 18:19:08 +0100 | [diff] [blame] | 23 | import android.view.Gravity; |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
| 25 | import android.view.View; |
| 26 | import android.webkit.WebView; |
| 27 | import android.webkit.GeolocationPermissions; |
| 28 | import android.widget.Button; |
| 29 | import android.widget.CheckBox; |
| 30 | import android.widget.LinearLayout; |
| 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 | |
| 34 | public class GeolocationPermissionsPrompt extends LinearLayout { |
| 35 | private LinearLayout mInner; |
| 36 | private TextView mMessage; |
| 37 | private Button mShareButton; |
| 38 | private Button mDontShareButton; |
| 39 | private CheckBox mRemember; |
| 40 | private GeolocationPermissions.Callback mCallback; |
| 41 | private String mOrigin; |
| 42 | |
| 43 | public GeolocationPermissionsPrompt(Context context) { |
| 44 | this(context, null); |
| 45 | } |
| 46 | |
| 47 | public GeolocationPermissionsPrompt(Context context, AttributeSet attrs) { |
| 48 | super(context, attrs); |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 49 | } |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 50 | |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 51 | void init() { |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 52 | mInner = (LinearLayout) findViewById(R.id.inner); |
| 53 | mMessage = (TextView) findViewById(R.id.message); |
| 54 | mShareButton = (Button) findViewById(R.id.share_button); |
| 55 | mDontShareButton = (Button) findViewById(R.id.dont_share_button); |
| 56 | mRemember = (CheckBox) findViewById(R.id.remember); |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 57 | |
| 58 | final GeolocationPermissionsPrompt me = this; |
| 59 | mShareButton.setOnClickListener(new View.OnClickListener() { |
| 60 | public void onClick(View v) { |
| 61 | me.handleButtonClick(true); |
| 62 | } |
| 63 | }); |
| 64 | mDontShareButton.setOnClickListener(new View.OnClickListener() { |
| 65 | public void onClick(View v) { |
| 66 | me.handleButtonClick(false); |
| 67 | } |
| 68 | }); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Shows the prompt for the given origin. When the user clicks on one of |
| 73 | * the buttons, the supplied callback is be called. |
| 74 | */ |
| 75 | public void show(String origin, GeolocationPermissions.Callback callback) { |
| 76 | mOrigin = origin; |
| 77 | mCallback = callback; |
Steve Block | 8844d19 | 2009-09-29 13:14:41 +0100 | [diff] [blame] | 78 | Uri uri = Uri.parse(mOrigin); |
| 79 | setMessage("http".equals(uri.getScheme()) ? mOrigin.substring(7) : mOrigin); |
Steve Block | 97557cf | 2009-09-30 17:46:24 +0100 | [diff] [blame] | 80 | // The checkbox should always be intially checked. |
| 81 | mRemember.setChecked(true); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 82 | showDialog(true); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Hides the prompt. |
| 87 | */ |
| 88 | public void hide() { |
| 89 | showDialog(false); |
| 90 | } |
| 91 | |
| 92 | /** |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 93 | * Handles a click on one the buttons by invoking the callback. |
| 94 | */ |
| 95 | private void handleButtonClick(boolean allow) { |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 96 | showDialog(false); |
Steve Block | c9f6d7a | 2009-10-16 18:19:08 +0100 | [diff] [blame] | 97 | |
| 98 | boolean remember = mRemember.isChecked(); |
| 99 | if (remember) { |
| 100 | Toast toast = Toast.makeText( |
| 101 | getContext(), |
| 102 | allow ? R.string.geolocation_permissions_prompt_toast_allowed : |
| 103 | R.string.geolocation_permissions_prompt_toast_disallowed, |
| 104 | Toast.LENGTH_LONG); |
| 105 | toast.setGravity(Gravity.BOTTOM, 0, 0); |
| 106 | toast.show(); |
| 107 | } |
| 108 | |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 109 | mCallback.invoke(mOrigin, allow, remember); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Sets the prompt's message. |
| 114 | */ |
| 115 | private void setMessage(CharSequence origin) { |
| 116 | mMessage.setText(String.format( |
| 117 | getResources().getString(R.string.geolocation_permissions_prompt_message), |
| 118 | origin)); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Shows or hides the prompt. |
| 123 | */ |
| 124 | private void showDialog(boolean shown) { |
| 125 | mInner.setVisibility(shown ? View.VISIBLE : View.GONE); |
| 126 | } |
| 127 | } |