PageRenderTime 73ms CodeModel.GetById 48ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/WebVox/src/com/marvin/webvox/GeolocationPermissionsPrompt.java

http://eyes-free.googlecode.com/
Java | 122 lines | 74 code | 13 blank | 35 comment | 0 complexity | bd51f8020d1a9adabe7a56f6863d19e5 MD5 | raw file
  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.marvin.webvox;
 18
 19import com.marvin.webvox.R;
 20
 21import android.content.Context;
 22import android.graphics.drawable.Drawable;
 23import android.net.Uri;
 24import android.util.AttributeSet;
 25import android.view.LayoutInflater;
 26import android.view.View;
 27import android.webkit.WebView;
 28import android.webkit.GeolocationPermissions;
 29import android.widget.Button;
 30import android.widget.CheckBox;
 31import android.widget.LinearLayout;
 32import android.widget.TextView;
 33
 34public 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);
 49        LayoutInflater factory = LayoutInflater.from(context);
 50        factory.inflate(R.layout.geolocation_permissions_prompt, this);
 51
 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);
 57        setButtonClickListeners();
 58    }
 59
 60    /**
 61     * Shows the prompt for the given origin. When the user clicks on one of
 62     * the buttons, the supplied callback is be called.
 63     */
 64    public void show(String origin, GeolocationPermissions.Callback callback) {
 65        mOrigin = origin;
 66        mCallback = callback;
 67        Uri uri = Uri.parse(mOrigin);
 68        setMessage("http".equals(uri.getScheme()) ?  mOrigin.substring(7) : mOrigin);
 69        // The checkbox should always be intially checked.
 70        mRemember.setChecked(true);
 71        showDialog(true);
 72    }
 73
 74    /**
 75     * Hides the prompt.
 76     */
 77    public void hide() {
 78        showDialog(false);
 79    }
 80
 81    /**
 82     * Sets the on click listeners for the buttons.
 83     */
 84    private void setButtonClickListeners() {
 85        final GeolocationPermissionsPrompt me = this;
 86        mShareButton.setOnClickListener(new View.OnClickListener() {
 87            public void onClick(View v) {
 88                me.handleButtonClick(true);
 89            }
 90        });
 91        mDontShareButton.setOnClickListener(new View.OnClickListener() {
 92            public void onClick(View v) {
 93                me.handleButtonClick(false);
 94            }
 95        });
 96    }
 97
 98    /**
 99     * Handles a click on one the buttons by invoking the callback.
100     */
101    private void handleButtonClick(boolean allow) {
102        boolean remember = mRemember.isChecked();
103        showDialog(false);
104        mCallback.invoke(mOrigin, allow, remember);
105    }
106
107    /**
108     * Sets the prompt's message.
109     */
110    private void setMessage(CharSequence origin) {
111        mMessage.setText(String.format(
112            getResources().getString(R.string.geolocation_permissions_prompt_message),
113            origin));
114    }
115
116    /**
117     * Shows or hides the prompt.
118     */
119    private void showDialog(boolean shown) {
120        mInner.setVisibility(shown ? View.VISIBLE : View.GONE);
121    }
122}