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; |
| 23 | import android.view.LayoutInflater; |
| 24 | import android.view.View; |
| 25 | import android.webkit.WebView; |
| 26 | import android.webkit.GeolocationPermissions; |
| 27 | import android.widget.Button; |
| 28 | import android.widget.CheckBox; |
| 29 | import android.widget.LinearLayout; |
| 30 | import android.widget.TextView; |
| 31 | |
| 32 | public class GeolocationPermissionsPrompt extends LinearLayout { |
| 33 | private LinearLayout mInner; |
| 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); |
| 47 | LayoutInflater factory = LayoutInflater.from(context); |
| 48 | factory.inflate(R.layout.geolocation_permissions_prompt, this); |
| 49 | |
| 50 | mInner = (LinearLayout) findViewById(R.id.inner); |
| 51 | mMessage = (TextView) findViewById(R.id.message); |
| 52 | mShareButton = (Button) findViewById(R.id.share_button); |
| 53 | mDontShareButton = (Button) findViewById(R.id.dont_share_button); |
| 54 | mRemember = (CheckBox) findViewById(R.id.remember); |
| 55 | setButtonClickListeners(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Shows the prompt for the given origin. When the user clicks on one of |
| 60 | * the buttons, the supplied callback is be called. |
| 61 | */ |
| 62 | public void show(String origin, GeolocationPermissions.Callback callback) { |
| 63 | mOrigin = origin; |
| 64 | mCallback = callback; |
Steve Block | 8844d19 | 2009-09-29 13:14:41 +0100 | [diff] [blame^] | 65 | Uri uri = Uri.parse(mOrigin); |
| 66 | setMessage("http".equals(uri.getScheme()) ? mOrigin.substring(7) : mOrigin); |
Steve Block | 2bc6991 | 2009-07-30 14:45:13 +0100 | [diff] [blame] | 67 | showDialog(true); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Hides the prompt. |
| 72 | */ |
| 73 | public void hide() { |
| 74 | showDialog(false); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Sets the on click listeners for the buttons. |
| 79 | */ |
| 80 | private void setButtonClickListeners() { |
| 81 | final GeolocationPermissionsPrompt me = this; |
| 82 | mShareButton.setOnClickListener(new View.OnClickListener() { |
| 83 | public void onClick(View v) { |
| 84 | me.handleButtonClick(true); |
| 85 | } |
| 86 | }); |
| 87 | mDontShareButton.setOnClickListener(new View.OnClickListener() { |
| 88 | public void onClick(View v) { |
| 89 | me.handleButtonClick(false); |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Handles a click on one the buttons by invoking the callback. |
| 96 | */ |
| 97 | private void handleButtonClick(boolean allow) { |
| 98 | boolean remember = mRemember.isChecked(); |
| 99 | showDialog(false); |
| 100 | mCallback.invoke(mOrigin, allow, remember); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Sets the prompt's message. |
| 105 | */ |
| 106 | private void setMessage(CharSequence origin) { |
| 107 | mMessage.setText(String.format( |
| 108 | getResources().getString(R.string.geolocation_permissions_prompt_message), |
| 109 | origin)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Shows or hides the prompt. |
| 114 | */ |
| 115 | private void showDialog(boolean shown) { |
| 116 | mInner.setVisibility(shown ? View.VISIBLE : View.GONE); |
| 117 | } |
| 118 | } |