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 | |
| 19 | import android.os.Handler; |
| 20 | import android.os.Message; |
| 21 | import android.text.Editable; |
| 22 | import android.text.Spannable; |
| 23 | import android.text.TextWatcher; |
| 24 | import android.view.KeyEvent; |
| 25 | import android.view.MotionEvent; |
| 26 | import android.view.View; |
| 27 | import android.view.ViewGroup; |
| 28 | import android.view.LayoutInflater; |
| 29 | import android.webkit.WebView; |
| 30 | import android.widget.EditText; |
| 31 | import android.widget.LinearLayout; |
| 32 | import android.widget.TextView; |
| 33 | |
| 34 | /* package */ class FindDialog extends LinearLayout implements TextWatcher { |
| 35 | private WebView mWebView; |
| 36 | private TextView mMatches; |
| 37 | private BrowserActivity mBrowserActivity; |
| 38 | |
| 39 | // Views with which the user can interact. |
| 40 | private View mOk; |
| 41 | private EditText mEditText; |
| 42 | private View mNextButton; |
| 43 | private View mPrevButton; |
| 44 | |
| 45 | // Tags for messages to be sent to the handler. |
| 46 | private final static int FIND_RESPONSE = 0; |
| 47 | private final static int NUM_FOUND = 1; |
| 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 | } |
| 68 | // Find is disabled for version 1.0, so find methods on WebView are |
| 69 | // currently private. |
| 70 | //mWebView.findPrevious(mEditText.getText().toString(), |
| 71 | // mFindHandler.obtainMessage(FIND_RESPONSE)); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | private Handler mFindHandler = new Handler() { |
| 76 | public void handleMessage(Message msg) { |
| 77 | if (NUM_FOUND == msg.what) { |
| 78 | mMatches.setText(Integer.toString(msg.arg1)); |
| 79 | if (0 == msg.arg1) { |
| 80 | disableButtons(); |
| 81 | } else { |
| 82 | mPrevButton.setFocusable(true); |
| 83 | mNextButton.setFocusable(true); |
| 84 | mPrevButton.setEnabled(true); |
| 85 | mNextButton.setEnabled(true); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | private void disableButtons() { |
| 92 | mPrevButton.setEnabled(false); |
| 93 | mNextButton.setEnabled(false); |
| 94 | mPrevButton.setFocusable(false); |
| 95 | mNextButton.setFocusable(false); |
| 96 | } |
| 97 | |
| 98 | public void setWebView(WebView webview) { |
| 99 | mWebView = webview; |
| 100 | } |
| 101 | |
| 102 | /* package */ FindDialog(BrowserActivity context) { |
| 103 | super(context); |
| 104 | mBrowserActivity = context; |
| 105 | LayoutInflater factory = LayoutInflater.from(context); |
| 106 | factory.inflate(R.layout.browser_find, this); |
| 107 | |
| 108 | setLayoutParams(new ViewGroup.LayoutParams( |
| 109 | ViewGroup.LayoutParams.FILL_PARENT, |
| 110 | ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 111 | |
| 112 | mEditText = (EditText) findViewById(R.id.edit); |
| 113 | |
| 114 | View button = findViewById(R.id.next); |
| 115 | button.setOnClickListener(mFindListener); |
| 116 | mNextButton = button; |
| 117 | |
| 118 | button = findViewById(R.id.previous); |
| 119 | button.setOnClickListener(mFindPreviousListener); |
| 120 | mPrevButton = button; |
| 121 | |
| 122 | button = findViewById(R.id.done); |
| 123 | button.setOnClickListener(mFindCancelListener); |
| 124 | mOk = button; |
| 125 | |
| 126 | mMatches = (TextView) findViewById(R.id.matches); |
| 127 | disableButtons(); |
| 128 | } |
| 129 | |
| 130 | public void dismiss() { |
| 131 | mBrowserActivity.closeFind(); |
| 132 | // If the nav buttons are highlighted, then there are matches |
| 133 | // highlighted in the WebView, and they should be cleared. |
| 134 | if (mPrevButton.isEnabled()) { |
| 135 | // Find is disabled for version 1.0, so find methods on WebView are |
| 136 | // currently private. |
| 137 | //mWebView.clearMatches(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 143 | // Make up and down find previous/next |
| 144 | int code = event.getKeyCode(); |
| 145 | boolean up = event.getAction() == KeyEvent.ACTION_UP; |
| 146 | switch (code) { |
| 147 | case KeyEvent.KEYCODE_BACK: |
| 148 | if (up) { |
| 149 | dismiss(); |
| 150 | } |
| 151 | return true; |
| 152 | case KeyEvent.KEYCODE_DPAD_UP: |
| 153 | if (event.getMetaState() != 0) { |
| 154 | break; |
| 155 | } |
| 156 | if (up) { |
| 157 | mFindPreviousListener.onClick(null); |
| 158 | } |
| 159 | return true; |
| 160 | case KeyEvent.KEYCODE_DPAD_DOWN: |
| 161 | if (event.getMetaState() != 0) { |
| 162 | break; |
| 163 | } |
| 164 | if (up) { |
| 165 | mFindListener.onClick(null); |
| 166 | } |
| 167 | return true; |
| 168 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 169 | case KeyEvent.KEYCODE_ENTER: |
| 170 | if (!mEditText.hasFocus()) { |
| 171 | break; |
| 172 | } |
| 173 | if (up) { |
| 174 | findNext(); |
| 175 | } |
| 176 | return true; |
| 177 | default: |
| 178 | break; |
| 179 | } |
| 180 | return super.dispatchKeyEvent(event); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 185 | super.dispatchTouchEvent(ev); |
| 186 | // Return true so that BrowserActivity thinks we handled it and does |
| 187 | // not dismiss us. |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | private void findNext() { |
| 192 | if (mWebView == null) { |
| 193 | throw new AssertionError("No WebView for FindDialog::findNext"); |
| 194 | } |
| 195 | // Find is disabled for version 1.0, so find methods on WebView are |
| 196 | // currently private. |
| 197 | //mWebView.findNext(mEditText.getText().toString(), |
| 198 | // mFindHandler.obtainMessage(FIND_RESPONSE)); |
| 199 | } |
| 200 | |
| 201 | public void show() { |
| 202 | mEditText.requestFocus(); |
| 203 | mEditText.setText(""); |
| 204 | Spannable span = (Spannable) mEditText.getText(); |
| 205 | span.setSpan(this, 0, span.length(), |
| 206 | Spannable.SPAN_INCLUSIVE_INCLUSIVE); |
| 207 | mMatches.setText(R.string.zero); |
| 208 | disableButtons(); |
| 209 | } |
| 210 | |
| 211 | // TextWatcher methods |
| 212 | public void beforeTextChanged(CharSequence s, |
| 213 | int start, |
| 214 | int count, |
| 215 | int after) { |
| 216 | } |
| 217 | |
| 218 | public void onTextChanged(CharSequence s, |
| 219 | int start, |
| 220 | int before, |
| 221 | int count) { |
| 222 | CharSequence find = mEditText.getText(); |
| 223 | if (0 == find.length()) { |
| 224 | disableButtons(); |
| 225 | // Find is disabled for version 1.0, so find methods on WebView are |
| 226 | // currently private. |
| 227 | //mWebView.clearMatches(); |
| 228 | mMatches.setText(R.string.zero); |
| 229 | } else { |
| 230 | if (mWebView == null) { |
| 231 | throw new AssertionError( |
| 232 | "No WebView for FindDialog::onTextChanged"); |
| 233 | } |
| 234 | // Find is disabled for version 1.0, so find methods on WebView are |
| 235 | // currently private. |
| 236 | //mWebView.findAll(find.toString(), |
| 237 | // mFindHandler.obtainMessage(NUM_FOUND)); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | public void afterTextChanged(Editable s) { |
| 242 | } |
| 243 | } |