The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [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 | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 19 | import android.app.Dialog; |
| 20 | import android.content.res.Configuration; |
| 21 | import android.os.Bundle; |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 27 | import android.view.Gravity; |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | import android.view.KeyEvent; |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | import android.view.View; |
| 30 | import android.view.ViewGroup; |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 31 | import android.view.Window; |
The Android Open Source Project | b7775e1 | 2009-02-10 15:44:04 -0800 | [diff] [blame] | 32 | import android.view.WindowManager; |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | import android.webkit.WebView; |
| 34 | import android.widget.EditText; |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | import android.widget.TextView; |
| 36 | |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 37 | /* package */ class FindDialog extends Dialog implements TextWatcher { |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | private WebView mWebView; |
| 39 | private TextView mMatches; |
| 40 | private BrowserActivity mBrowserActivity; |
| 41 | |
| 42 | // Views with which the user can interact. |
| 43 | private View mOk; |
| 44 | private EditText mEditText; |
| 45 | private View mNextButton; |
| 46 | private View mPrevButton; |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 47 | private View mMatchesView; |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 | |
| 49 | private View.OnClickListener mFindListener = new View.OnClickListener() { |
| 50 | public void onClick(View v) { |
| 51 | findNext(); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | private View.OnClickListener mFindCancelListener = |
| 56 | new View.OnClickListener() { |
| 57 | public void onClick(View v) { |
| 58 | dismiss(); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | private View.OnClickListener mFindPreviousListener = |
| 63 | new View.OnClickListener() { |
| 64 | public void onClick(View v) { |
| 65 | if (mWebView == null) { |
| 66 | throw new AssertionError("No WebView for FindDialog::onClick"); |
| 67 | } |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 68 | mWebView.findNext(false); |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 69 | } |
| 70 | }; |
The Android Open Source Project | 1658a9b | 2009-03-03 14:04:29 -0800 | [diff] [blame^] | 71 | |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | private void disableButtons() { |
| 73 | mPrevButton.setEnabled(false); |
| 74 | mNextButton.setEnabled(false); |
| 75 | mPrevButton.setFocusable(false); |
| 76 | mNextButton.setFocusable(false); |
| 77 | } |
| 78 | |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 79 | /* package */ void setWebView(WebView webview) { |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 80 | mWebView = webview; |
| 81 | } |
| 82 | |
| 83 | /* package */ FindDialog(BrowserActivity context) { |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 84 | super(context, R.style.FindDialogTheme); |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 85 | mBrowserActivity = context; |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 86 | setCanceledOnTouchOutside(true); |
| 87 | } |
| 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 | |
| 96 | @Override |
| 97 | protected void onCreate(Bundle savedInstanceState) { |
| 98 | super.onCreate(savedInstanceState); |
| 99 | |
| 100 | Window theWindow = getWindow(); |
| 101 | theWindow.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL); |
| 102 | |
| 103 | setContentView(R.layout.browser_find); |
| 104 | |
| 105 | theWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT, |
| 106 | ViewGroup.LayoutParams.WRAP_CONTENT); |
| 107 | |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 108 | mEditText = (EditText) findViewById(R.id.edit); |
| 109 | |
| 110 | View button = findViewById(R.id.next); |
| 111 | button.setOnClickListener(mFindListener); |
| 112 | mNextButton = button; |
| 113 | |
| 114 | button = findViewById(R.id.previous); |
| 115 | button.setOnClickListener(mFindPreviousListener); |
| 116 | mPrevButton = button; |
| 117 | |
| 118 | button = findViewById(R.id.done); |
| 119 | button.setOnClickListener(mFindCancelListener); |
| 120 | mOk = button; |
| 121 | |
| 122 | mMatches = (TextView) findViewById(R.id.matches); |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 123 | mMatchesView = findViewById(R.id.matches_view); |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | disableButtons(); |
The Android Open Source Project | b7775e1 | 2009-02-10 15:44:04 -0800 | [diff] [blame] | 125 | theWindow.setSoftInputMode( |
| 126 | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | public void dismiss() { |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 130 | super.dismiss(); |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 131 | mBrowserActivity.closeFind(); |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 132 | mWebView.clearMatches(); |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public boolean dispatchKeyEvent(KeyEvent event) { |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 137 | int code = event.getKeyCode(); |
| 138 | boolean up = event.getAction() == KeyEvent.ACTION_UP; |
| 139 | switch (code) { |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 140 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 141 | case KeyEvent.KEYCODE_ENTER: |
| 142 | if (!mEditText.hasFocus()) { |
| 143 | break; |
| 144 | } |
| 145 | if (up) { |
| 146 | findNext(); |
| 147 | } |
| 148 | return true; |
| 149 | default: |
| 150 | break; |
| 151 | } |
| 152 | return super.dispatchKeyEvent(event); |
| 153 | } |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 154 | |
| 155 | private void findNext() { |
| 156 | if (mWebView == null) { |
| 157 | throw new AssertionError("No WebView for FindDialog::findNext"); |
| 158 | } |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 159 | mWebView.findNext(true); |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 160 | } |
The Android Open Source Project | 1658a9b | 2009-03-03 14:04:29 -0800 | [diff] [blame^] | 161 | |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | public void show() { |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 163 | super.show(); |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 164 | mEditText.requestFocus(); |
| 165 | mEditText.setText(""); |
| 166 | Spannable span = (Spannable) mEditText.getText(); |
| 167 | span.setSpan(this, 0, span.length(), |
| 168 | Spannable.SPAN_INCLUSIVE_INCLUSIVE); |
| 169 | mMatches.setText(R.string.zero); |
| 170 | disableButtons(); |
| 171 | } |
| 172 | |
| 173 | // TextWatcher methods |
| 174 | public void beforeTextChanged(CharSequence s, |
| 175 | int start, |
| 176 | int count, |
| 177 | int after) { |
| 178 | } |
| 179 | |
| 180 | public void onTextChanged(CharSequence s, |
| 181 | int start, |
| 182 | int before, |
| 183 | int count) { |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 184 | if (mWebView == null) { |
| 185 | throw new AssertionError( |
| 186 | "No WebView for FindDialog::onTextChanged"); |
| 187 | } |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 188 | CharSequence find = mEditText.getText(); |
| 189 | if (0 == find.length()) { |
| 190 | disableButtons(); |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 191 | mWebView.clearMatches(); |
| 192 | mMatchesView.setVisibility(View.INVISIBLE); |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 193 | } else { |
The Android Open Source Project | ed217d9 | 2008-12-17 18:05:52 -0800 | [diff] [blame] | 194 | mMatchesView.setVisibility(View.VISIBLE); |
| 195 | int found = mWebView.findAll(find.toString()); |
| 196 | mMatches.setText(Integer.toString(found)); |
| 197 | if (found < 2) { |
| 198 | disableButtons(); |
| 199 | if (found == 0) { |
| 200 | mMatches.setText(R.string.zero); |
| 201 | } |
| 202 | } else { |
| 203 | mPrevButton.setFocusable(true); |
| 204 | mNextButton.setFocusable(true); |
| 205 | mPrevButton.setEnabled(true); |
| 206 | mNextButton.setEnabled(true); |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 207 | } |
The Android Open Source Project | ba6d7b8 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | public void afterTextChanged(Editable s) { |
| 212 | } |
| 213 | } |