OSDN Git Service

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