The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.browser; |
| 18 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 19 | import android.content.Context; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 20 | import android.text.Editable; |
| 21 | import android.text.Spannable; |
| 22 | import android.text.TextWatcher; |
| 23 | import android.view.Gravity; |
| 24 | import android.view.KeyEvent; |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame^] | 25 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 26 | import android.view.View; |
| 27 | import android.view.ViewGroup; |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame^] | 28 | import android.view.animation.AnimationUtils; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 29 | import android.view.inputmethod.InputMethodManager; |
| 30 | import android.webkit.WebView; |
| 31 | import android.widget.EditText; |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame^] | 32 | import android.widget.LinearLayout; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 33 | import android.widget.TextView; |
| 34 | |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame^] | 35 | /* package */ class FindDialog extends LinearLayout implements TextWatcher { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 36 | private WebView mWebView; |
| 37 | private TextView mMatches; |
| 38 | private BrowserActivity mBrowserActivity; |
| 39 | |
| 40 | // Views with which the user can interact. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 41 | private EditText mEditText; |
| 42 | private View mNextButton; |
| 43 | private View mPrevButton; |
| 44 | private View mMatchesView; |
| 45 | |
| 46 | private View.OnClickListener mFindListener = new View.OnClickListener() { |
| 47 | public void onClick(View v) { |
| 48 | findNext(); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | private View.OnClickListener mFindCancelListener = |
| 53 | new View.OnClickListener() { |
| 54 | public void onClick(View v) { |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame^] | 55 | mBrowserActivity.closeFind(); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 56 | } |
| 57 | }; |
| 58 | |
| 59 | private View.OnClickListener mFindPreviousListener = |
| 60 | new View.OnClickListener() { |
| 61 | public void onClick(View v) { |
| 62 | if (mWebView == null) { |
| 63 | throw new AssertionError("No WebView for FindDialog::onClick"); |
| 64 | } |
| 65 | mWebView.findNext(false); |
| 66 | hideSoftInput(); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | /* |
| 71 | * Remove the soft keyboard from the screen. |
| 72 | */ |
| 73 | private void hideSoftInput() { |
| 74 | InputMethodManager imm = (InputMethodManager) |
| 75 | mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE); |
| 76 | imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); |
| 77 | } |
| 78 | |
| 79 | private void disableButtons() { |
| 80 | mPrevButton.setEnabled(false); |
| 81 | mNextButton.setEnabled(false); |
| 82 | mPrevButton.setFocusable(false); |
| 83 | mNextButton.setFocusable(false); |
| 84 | } |
| 85 | |
| 86 | /* package */ void setWebView(WebView webview) { |
| 87 | mWebView = webview; |
| 88 | } |
| 89 | |
| 90 | /* package */ FindDialog(BrowserActivity context) { |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame^] | 91 | super(context); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 92 | mBrowserActivity = context; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 93 | |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame^] | 94 | LayoutInflater factory = LayoutInflater.from(context); |
| 95 | factory.inflate(R.layout.browser_find, this); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 96 | |
| 97 | mEditText = (EditText) findViewById(R.id.edit); |
| 98 | |
| 99 | View button = findViewById(R.id.next); |
| 100 | button.setOnClickListener(mFindListener); |
| 101 | mNextButton = button; |
| 102 | |
| 103 | button = findViewById(R.id.previous); |
| 104 | button.setOnClickListener(mFindPreviousListener); |
| 105 | mPrevButton = button; |
| 106 | |
| 107 | button = findViewById(R.id.done); |
| 108 | button.setOnClickListener(mFindCancelListener); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 109 | |
| 110 | mMatches = (TextView) findViewById(R.id.matches); |
| 111 | mMatchesView = findViewById(R.id.matches_view); |
| 112 | disableButtons(); |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame^] | 113 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 114 | } |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame^] | 115 | |
| 116 | /** |
| 117 | * Called by BrowserActivity.closeFind. Start the animation to hide |
| 118 | * the dialog, inform the WebView that the dialog is being dismissed, |
| 119 | * and hide the soft keyboard. |
| 120 | */ |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 121 | public void dismiss() { |
Cary Clark | 7d3ac79 | 2010-03-03 10:11:44 -0500 | [diff] [blame] | 122 | mWebView.notifyFindDialogDismissed(); |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame^] | 123 | startAnimation(AnimationUtils.loadAnimation(mBrowserActivity, |
| 124 | R.anim.find_dialog_exit)); |
| 125 | InputMethodManager imm = (InputMethodManager) |
| 126 | getContext().getSystemService(Context.INPUT_METHOD_SERVICE); |
| 127 | |
| 128 | imm.hideSoftInputFromWindow(this.getWindowToken(), 0); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 129 | } |
Leon Scroggins | 354bbd0 | 2009-04-16 15:24:07 -0400 | [diff] [blame] | 130 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 131 | @Override |
| 132 | public boolean dispatchKeyEvent(KeyEvent event) { |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame^] | 133 | int keyCode = event.getKeyCode(); |
| 134 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
| 135 | if (event.getAction() == KeyEvent.ACTION_UP) { |
| 136 | mBrowserActivity.closeFind(); |
| 137 | return true; |
| 138 | } |
| 139 | } else if (event.getAction() == KeyEvent.ACTION_UP) { |
| 140 | if (keyCode == KeyEvent.KEYCODE_ENTER |
| 141 | && mEditText.hasFocus()) { |
| 142 | findNext(); |
| 143 | return true; |
| 144 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 145 | } |
| 146 | return super.dispatchKeyEvent(event); |
| 147 | } |
| 148 | |
| 149 | private void findNext() { |
| 150 | if (mWebView == null) { |
| 151 | throw new AssertionError("No WebView for FindDialog::findNext"); |
| 152 | } |
| 153 | mWebView.findNext(true); |
| 154 | hideSoftInput(); |
| 155 | } |
| 156 | |
| 157 | public void show() { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 158 | mEditText.requestFocus(); |
| 159 | mEditText.setText(""); |
| 160 | Spannable span = (Spannable) mEditText.getText(); |
| 161 | span.setSpan(this, 0, span.length(), |
| 162 | Spannable.SPAN_INCLUSIVE_INCLUSIVE); |
Eric Fischer | 8963b29 | 2009-03-24 17:53:52 -0700 | [diff] [blame] | 163 | setMatchesFound(0); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 164 | disableButtons(); |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame^] | 165 | startAnimation(AnimationUtils.loadAnimation(mBrowserActivity, |
| 166 | R.anim.find_dialog_enter)); |
| 167 | InputMethodManager imm = (InputMethodManager) |
| 168 | mBrowserActivity.getSystemService(Context.INPUT_METHOD_SERVICE); |
| 169 | imm.showSoftInput(mEditText, 0); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // TextWatcher methods |
| 173 | public void beforeTextChanged(CharSequence s, |
| 174 | int start, |
| 175 | int count, |
| 176 | int after) { |
| 177 | } |
| 178 | |
| 179 | public void onTextChanged(CharSequence s, |
| 180 | int start, |
| 181 | int before, |
| 182 | int count) { |
| 183 | if (mWebView == null) { |
| 184 | throw new AssertionError( |
| 185 | "No WebView for FindDialog::onTextChanged"); |
| 186 | } |
| 187 | CharSequence find = mEditText.getText(); |
| 188 | if (0 == find.length()) { |
| 189 | disableButtons(); |
| 190 | mWebView.clearMatches(); |
| 191 | mMatchesView.setVisibility(View.INVISIBLE); |
| 192 | } else { |
| 193 | mMatchesView.setVisibility(View.VISIBLE); |
| 194 | int found = mWebView.findAll(find.toString()); |
Eric Fischer | 8963b29 | 2009-03-24 17:53:52 -0700 | [diff] [blame] | 195 | setMatchesFound(found); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 196 | if (found < 2) { |
| 197 | disableButtons(); |
| 198 | if (found == 0) { |
Eric Fischer | 8963b29 | 2009-03-24 17:53:52 -0700 | [diff] [blame] | 199 | setMatchesFound(0); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 200 | } |
| 201 | } else { |
| 202 | mPrevButton.setFocusable(true); |
| 203 | mNextButton.setFocusable(true); |
| 204 | mPrevButton.setEnabled(true); |
| 205 | mNextButton.setEnabled(true); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
Eric Fischer | 8963b29 | 2009-03-24 17:53:52 -0700 | [diff] [blame] | 210 | private void setMatchesFound(int found) { |
| 211 | String template = mBrowserActivity.getResources(). |
| 212 | getQuantityString(R.plurals.matches_found, found, found); |
| 213 | |
| 214 | mMatches.setText(template); |
| 215 | } |
| 216 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 217 | public void afterTextChanged(Editable s) { |
| 218 | } |
| 219 | } |