OSDN Git Service

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