PageRenderTime 53ms CodeModel.GetById 27ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 0ms

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