/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. package com.marvin.webvox;
  17. import com.marvin.webvox.R;
  18. import android.content.Context;
  19. import android.graphics.drawable.Drawable;
  20. import android.net.Uri;
  21. import android.util.AttributeSet;
  22. import android.view.LayoutInflater;
  23. import android.view.View;
  24. import android.webkit.WebView;
  25. import android.webkit.GeolocationPermissions;
  26. import android.widget.Button;
  27. import android.widget.CheckBox;
  28. import android.widget.LinearLayout;
  29. import android.widget.TextView;
  30. public class GeolocationPermissionsPrompt extends LinearLayout {
  31. private LinearLayout mInner;
  32. private TextView mMessage;
  33. private Button mShareButton;
  34. private Button mDontShareButton;
  35. private CheckBox mRemember;
  36. private GeolocationPermissions.Callback mCallback;
  37. private String mOrigin;
  38. public GeolocationPermissionsPrompt(Context context) {
  39. this(context, null);
  40. }
  41. public GeolocationPermissionsPrompt(Context context, AttributeSet attrs) {
  42. super(context, attrs);
  43. LayoutInflater factory = LayoutInflater.from(context);
  44. factory.inflate(R.layout.geolocation_permissions_prompt, this);
  45. mInner = (LinearLayout) findViewById(R.id.inner);
  46. mMessage = (TextView) findViewById(R.id.message);
  47. mShareButton = (Button) findViewById(R.id.share_button);
  48. mDontShareButton = (Button) findViewById(R.id.dont_share_button);
  49. mRemember = (CheckBox) findViewById(R.id.remember);
  50. setButtonClickListeners();
  51. }
  52. /**
  53. * Shows the prompt for the given origin. When the user clicks on one of
  54. * the buttons, the supplied callback is be called.
  55. */
  56. public void show(String origin, GeolocationPermissions.Callback callback) {
  57. mOrigin = origin;
  58. mCallback = callback;
  59. Uri uri = Uri.parse(mOrigin);
  60. setMessage("http".equals(uri.getScheme()) ? mOrigin.substring(7) : mOrigin);
  61. // The checkbox should always be intially checked.
  62. mRemember.setChecked(true);
  63. showDialog(true);
  64. }
  65. /**
  66. * Hides the prompt.
  67. */
  68. public void hide() {
  69. showDialog(false);
  70. }
  71. /**
  72. * Sets the on click listeners for the buttons.
  73. */
  74. private void setButtonClickListeners() {
  75. final GeolocationPermissionsPrompt me = this;
  76. mShareButton.setOnClickListener(new View.OnClickListener() {
  77. public void onClick(View v) {
  78. me.handleButtonClick(true);
  79. }
  80. });
  81. mDontShareButton.setOnClickListener(new View.OnClickListener() {
  82. public void onClick(View v) {
  83. me.handleButtonClick(false);
  84. }
  85. });
  86. }
  87. /**
  88. * Handles a click on one the buttons by invoking the callback.
  89. */
  90. private void handleButtonClick(boolean allow) {
  91. boolean remember = mRemember.isChecked();
  92. showDialog(false);
  93. mCallback.invoke(mOrigin, allow, remember);
  94. }
  95. /**
  96. * Sets the prompt's message.
  97. */
  98. private void setMessage(CharSequence origin) {
  99. mMessage.setText(String.format(
  100. getResources().getString(R.string.geolocation_permissions_prompt_message),
  101. origin));
  102. }
  103. /**
  104. * Shows or hides the prompt.
  105. */
  106. private void showDialog(boolean shown) {
  107. mInner.setVisibility(shown ? View.VISIBLE : View.GONE);
  108. }
  109. }