GeolocationPermissionsPrompt.java revision 8844d19f6879802ba02463df6cf2b8bfbaf68b0a
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 17package com.android.browser; 18 19import android.content.Context; 20import android.graphics.drawable.Drawable; 21import android.net.Uri; 22import android.util.AttributeSet; 23import android.view.LayoutInflater; 24import android.view.View; 25import android.webkit.WebView; 26import android.webkit.GeolocationPermissions; 27import android.widget.Button; 28import android.widget.CheckBox; 29import android.widget.LinearLayout; 30import android.widget.TextView; 31 32public 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; 65 Uri uri = Uri.parse(mOrigin); 66 setMessage("http".equals(uri.getScheme()) ? mOrigin.substring(7) : mOrigin); 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} 119