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

http://eyes-free.googlecode.com/ · Java · 226 lines · 170 code · 31 blank · 25 comment · 18 complexity · 2ebd843309d0ac1c942098e3175c00c8 MD5 · raw file

  1. /*
  2. * Copyright (C) 2007 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.app.Dialog;
  19. import android.content.Context;
  20. import android.content.res.Configuration;
  21. import android.os.Bundle;
  22. import android.os.Handler;
  23. import android.os.Message;
  24. import android.text.Editable;
  25. import android.text.Spannable;
  26. import android.text.TextWatcher;
  27. import android.view.Gravity;
  28. import android.view.KeyEvent;
  29. import android.view.View;
  30. import android.view.ViewGroup;
  31. import android.view.Window;
  32. import android.view.WindowManager;
  33. import android.view.inputmethod.InputMethodManager;
  34. import android.webkit.WebView;
  35. import android.widget.EditText;
  36. import android.widget.TextView;
  37. /* package */ class FindDialog extends Dialog implements TextWatcher {
  38. private WebView mWebView;
  39. private TextView mMatches;
  40. private BrowserActivity mBrowserActivity;
  41. // Views with which the user can interact.
  42. private EditText mEditText;
  43. private View mNextButton;
  44. private View mPrevButton;
  45. private View mMatchesView;
  46. private View.OnClickListener mFindListener = new View.OnClickListener() {
  47. public void onClick(View v) {
  48. findNext();
  49. }
  50. };
  51. private View.OnClickListener mFindCancelListener =
  52. new View.OnClickListener() {
  53. public void onClick(View v) {
  54. dismiss();
  55. }
  56. };
  57. private View.OnClickListener mFindPreviousListener =
  58. new View.OnClickListener() {
  59. public void onClick(View v) {
  60. if (mWebView == null) {
  61. throw new AssertionError("No WebView for FindDialog::onClick");
  62. }
  63. mWebView.findNext(false);
  64. hideSoftInput();
  65. }
  66. };
  67. /*
  68. * Remove the soft keyboard from the screen.
  69. */
  70. private void hideSoftInput() {
  71. InputMethodManager imm = (InputMethodManager)
  72. mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
  73. imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
  74. }
  75. private void disableButtons() {
  76. mPrevButton.setEnabled(false);
  77. mNextButton.setEnabled(false);
  78. mPrevButton.setFocusable(false);
  79. mNextButton.setFocusable(false);
  80. }
  81. /* package */ void setWebView(WebView webview) {
  82. mWebView = webview;
  83. }
  84. /* package */ FindDialog(BrowserActivity context) {
  85. super(context, R.style.FindDialogTheme);
  86. mBrowserActivity = context;
  87. setCanceledOnTouchOutside(true);
  88. }
  89. /* package */ void onConfigurationChanged(Configuration newConfig) {
  90. // FIXME: Would like to call mWebView.findAll again, so that the
  91. // matches would refresh, but the new picture has not yet been
  92. // created, so it is too soon.
  93. mEditText.getText().clear();
  94. }
  95. @Override
  96. protected void onCreate(Bundle savedInstanceState) {
  97. super.onCreate(savedInstanceState);
  98. Window theWindow = getWindow();
  99. theWindow.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL);
  100. setContentView(R.layout.browser_find);
  101. theWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
  102. ViewGroup.LayoutParams.WRAP_CONTENT);
  103. mEditText = (EditText) findViewById(R.id.edit);
  104. View button = findViewById(R.id.next);
  105. button.setOnClickListener(mFindListener);
  106. mNextButton = button;
  107. button = findViewById(R.id.previous);
  108. button.setOnClickListener(mFindPreviousListener);
  109. mPrevButton = button;
  110. button = findViewById(R.id.done);
  111. button.setOnClickListener(mFindCancelListener);
  112. mMatches = (TextView) findViewById(R.id.matches);
  113. mMatchesView = findViewById(R.id.matches_view);
  114. disableButtons();
  115. theWindow.setSoftInputMode(
  116. WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  117. }
  118. public void dismiss() {
  119. super.dismiss();
  120. mBrowserActivity.closeFind();
  121. mWebView.clearMatches();
  122. }
  123. @Override
  124. public boolean dispatchKeyEvent(KeyEvent event) {
  125. if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER
  126. && event.getAction() == KeyEvent.ACTION_UP
  127. && mEditText.hasFocus()) {
  128. findNext();
  129. return true;
  130. }
  131. return super.dispatchKeyEvent(event);
  132. }
  133. private void findNext() {
  134. if (mWebView == null) {
  135. throw new AssertionError("No WebView for FindDialog::findNext");
  136. }
  137. mWebView.findNext(true);
  138. hideSoftInput();
  139. }
  140. public void show() {
  141. super.show();
  142. mEditText.requestFocus();
  143. mEditText.setText("");
  144. Spannable span = (Spannable) mEditText.getText();
  145. span.setSpan(this, 0, span.length(),
  146. Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  147. setMatchesFound(0);
  148. disableButtons();
  149. }
  150. // TextWatcher methods
  151. public void beforeTextChanged(CharSequence s,
  152. int start,
  153. int count,
  154. int after) {
  155. }
  156. public void onTextChanged(CharSequence s,
  157. int start,
  158. int before,
  159. int count) {
  160. if (mWebView == null) {
  161. throw new AssertionError(
  162. "No WebView for FindDialog::onTextChanged");
  163. }
  164. CharSequence find = mEditText.getText();
  165. if (0 == find.length()) {
  166. disableButtons();
  167. mWebView.clearMatches();
  168. mMatchesView.setVisibility(View.INVISIBLE);
  169. } else {
  170. mMatchesView.setVisibility(View.VISIBLE);
  171. // mWebView.setFindDialogHeight(
  172. // getWindow().getDecorView().getHeight());
  173. int found = mWebView.findAll(find.toString());
  174. setMatchesFound(found);
  175. if (found < 2) {
  176. disableButtons();
  177. if (found == 0) {
  178. setMatchesFound(0);
  179. }
  180. } else {
  181. mPrevButton.setFocusable(true);
  182. mNextButton.setFocusable(true);
  183. mPrevButton.setEnabled(true);
  184. mNextButton.setEnabled(true);
  185. }
  186. }
  187. }
  188. private void setMatchesFound(int found) {
  189. String template = mBrowserActivity.getResources().
  190. getQuantityString(R.plurals.matches_found, found, found);
  191. mMatches.setText(template);
  192. }
  193. public void afterTextChanged(Editable s) {
  194. }
  195. }