1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.browser;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.PaintDrawable;
import android.os.Handler;
import android.os.Message;
import android.speech.RecognizerIntent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.android.common.speech.LoggingEvents;
/**
* This class represents a title bar for a particular "tab" or "window" in the
* browser.
*/
public class TitleBar extends LinearLayout {
private TextView mTitle;
private Drawable mCloseDrawable;
private ImageView mRtButton;
private Drawable mCircularProgress;
private ProgressBar mHorizontalProgress;
private ImageView mFavicon;
private ImageView mLockIcon;
private ImageView mStopButton;
private Drawable mBookmarkDrawable;
private Drawable mVoiceDrawable;
private boolean mInLoad;
private BrowserActivity mBrowserActivity;
private Drawable mGenericFavicon;
private int mIconDimension;
private View mTitleBg;
private MyHandler mHandler;
private Intent mVoiceSearchIntent;
private boolean mInVoiceMode;
private Drawable mVoiceModeBackground;
private Drawable mNormalBackground;
private Drawable mLoadingBackground;
private ImageSpan mArcsSpan;
private int mLeftMargin;
private int mRightMargin;
private static int LONG_PRESS = 1;
public TitleBar(BrowserActivity context) {
super(context, null);
mHandler = new MyHandler();
LayoutInflater factory = LayoutInflater.from(context);
factory.inflate(R.layout.title_bar, this);
mBrowserActivity = context;
mTitle = (TextView) findViewById(R.id.title);
mTitle.setCompoundDrawablePadding(5);
mTitleBg = findViewById(R.id.title_bg);
mLockIcon = (ImageView) findViewById(R.id.lock);
mFavicon = (ImageView) findViewById(R.id.favicon);
mStopButton = (ImageView) findViewById(R.id.stop);
mRtButton = (ImageView) findViewById(R.id.rt_btn);
Resources resources = context.getResources();
mCircularProgress = (Drawable) resources.getDrawable(
com.android.internal.R.drawable.search_spinner);
DisplayMetrics metrics = resources.getDisplayMetrics();
mLeftMargin = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 8f, metrics);
mRightMargin = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 6f, metrics);
mIconDimension = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 20f, metrics);
mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension);
mHorizontalProgress = (ProgressBar) findViewById(
R.id.progress_horizontal);
mGenericFavicon = context.getResources().getDrawable(
R.drawable.app_web_browser_sm);
mVoiceSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
mVoiceSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
// This extra tells voice search not to send the application id in its
// results intent - http://b/2546173
//
// TODO: Make a constant for this extra.
mVoiceSearchIntent.putExtra("android.speech.extras.SEND_APPLICATION_ID_EXTRA", false);
PackageManager pm = context.getPackageManager();
ResolveInfo ri = pm.resolveActivity(mVoiceSearchIntent,
PackageManager.MATCH_DEFAULT_ONLY);
if (ri == null) {
mVoiceSearchIntent = null;
} else {
mVoiceDrawable = resources.getDrawable(
android.R.drawable.ic_btn_speak_now);
}
mBookmarkDrawable = mRtButton.getDrawable();
mVoiceModeBackground = resources.getDrawable(
R.drawable.title_voice);
mNormalBackground = mTitleBg.getBackground();
mLoadingBackground = resources.getDrawable(R.drawable.title_loading);
mArcsSpan = new ImageSpan(context, R.drawable.arcs,
ImageSpan.ALIGN_BASELINE);
}
private class MyHandler extends Handler {
public void handleMessage(Message msg) {
if (msg.what == LONG_PRESS) {
// Prevent the normal action from happening by setting the title
// bar's state to false.
mTitleBg.setPressed(false);
// Need to call a special method on BrowserActivity for when the
// fake title bar is up, because its ViewGroup does not show a
// context menu.
mBrowserActivity.showTitleBarContextMenu();
}
}
};
@Override
public void createContextMenu(ContextMenu menu) {
MenuInflater inflater = mBrowserActivity.getMenuInflater();
inflater.inflate(R.menu.title_context, menu);
mBrowserActivity.onCreateContextMenu(menu, this, null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
ImageView button = mInLoad ? mStopButton : mRtButton;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Make all touches hit either the textfield or the button,
// depending on which side of the right edge of the textfield
// they hit.
if ((int) event.getX() > mTitleBg.getRight()) {
button.setPressed(true);
} else {
mTitleBg.setPressed(true);
mHandler.sendMessageDelayed(mHandler.obtainMessage(
LONG_PRESS),
ViewConfiguration.getLongPressTimeout());
}
break;
case MotionEvent.ACTION_MOVE:
int slop = ViewConfiguration.get(mBrowserActivity)
.getScaledTouchSlop();
if ((int) event.getY() > getHeight() + slop) {
// We only trigger the actions in ACTION_UP if one or the
// other is pressed. Since the user moved off the title
// bar, mark both as not pressed.
mTitleBg.setPressed(false);
button.setPressed(false);
mHandler.removeMessages(LONG_PRESS);
break;
}
int x = (int) event.getX();
int titleRight = mTitleBg.getRight();
if (mTitleBg.isPressed() && x > titleRight + slop) {
mTitleBg.setPressed(false);
mHandler.removeMessages(LONG_PRESS);
} else if (button.isPressed() && x < titleRight - slop) {
button.setPressed(false);
}
break;
case MotionEvent.ACTION_CANCEL:
button.setPressed(false);
mTitleBg.setPressed(false);
mHandler.removeMessages(LONG_PRESS);
break;
case MotionEvent.ACTION_UP:
if (button.isPressed()) {
if (mInVoiceMode) {
if (mBrowserActivity.getTabControl().getCurrentTab()
.voiceSearchSourceIsGoogle()) {
Intent i = new Intent(
LoggingEvents.ACTION_LOG_EVENT);
i.putExtra(LoggingEvents.EXTRA_EVENT,
LoggingEvents.VoiceSearch.RETRY);
mBrowserActivity.sendBroadcast(i);
}
mBrowserActivity.startActivity(mVoiceSearchIntent);
} else if (mInLoad) {
mBrowserActivity.stopLoading();
} else {
mBrowserActivity.bookmarksOrHistoryPicker(false);
}
button.setPressed(false);
} else if (mTitleBg.isPressed()) {
mHandler.removeMessages(LONG_PRESS);
if (mInVoiceMode) {
if (mBrowserActivity.getTabControl().getCurrentTab()
.voiceSearchSourceIsGoogle()) {
Intent i = new Intent(
LoggingEvents.ACTION_LOG_EVENT);
i.putExtra(LoggingEvents.EXTRA_EVENT,
LoggingEvents.VoiceSearch.N_BEST_REVEAL);
mBrowserActivity.sendBroadcast(i);
}
mBrowserActivity.showVoiceSearchResults(
mTitle.getText().toString().trim());
} else {
mBrowserActivity.editUrl();
}
mTitleBg.setPressed(false);
}
break;
default:
break;
}
return true;
}
/**
* Set a new Bitmap for the Favicon.
*/
/* package */ void setFavicon(Bitmap icon) {
Drawable[] array = new Drawable[3];
array[0] = new PaintDrawable(Color.BLACK);
PaintDrawable p = new PaintDrawable(Color.WHITE);
array[1] = p;
if (icon == null) {
array[2] = mGenericFavicon;
} else {
array[2] = new BitmapDrawable(icon);
}
LayerDrawable d = new LayerDrawable(array);
d.setLayerInset(1, 1, 1, 1, 1);
d.setLayerInset(2, 2, 2, 2, 2);
mFavicon.setImageDrawable(d);
}
/**
* Change the TitleBar to or from voice mode. If there is no package to
* handle voice search, the TitleBar cannot be set to voice mode.
*/
/* package */ void setInVoiceMode(boolean inVoiceMode) {
if (mInVoiceMode == inVoiceMode) return;
mInVoiceMode = inVoiceMode && mVoiceSearchIntent != null;
Drawable titleDrawable;
if (mInVoiceMode) {
mRtButton.setImageDrawable(mVoiceDrawable);
titleDrawable = mVoiceModeBackground;
mTitle.setEllipsize(null);
mRtButton.setVisibility(View.VISIBLE);
mStopButton.setVisibility(View.GONE);
mTitleBg.setBackgroundDrawable(titleDrawable);
mTitleBg.setPadding(mLeftMargin, mTitleBg.getPaddingTop(),
mRightMargin, mTitleBg.getPaddingBottom());
} else {
if (mInLoad) {
titleDrawable = mLoadingBackground;
mRtButton.setVisibility(View.GONE);
mStopButton.setVisibility(View.VISIBLE);
} else {
titleDrawable = mNormalBackground;
mRtButton.setVisibility(View.VISIBLE);
mStopButton.setVisibility(View.GONE);
mRtButton.setImageDrawable(mBookmarkDrawable);
}
mTitle.setEllipsize(TextUtils.TruncateAt.END);
mTitleBg.setBackgroundDrawable(titleDrawable);
mTitleBg.setPadding(mLeftMargin, 0, mRightMargin, 0);
}
mTitle.setSingleLine(!mInVoiceMode);
}
/**
* Set the Drawable for the lock icon, or null to hide it.
*/
/* package */ void setLock(Drawable d) {
if (null == d) {
mLockIcon.setVisibility(View.GONE);
} else {
mLockIcon.setImageDrawable(d);
mLockIcon.setVisibility(View.VISIBLE);
}
}
/**
* Update the progress, from 0 to 100.
*/
/* package */ void setProgress(int newProgress) {
if (newProgress >= mHorizontalProgress.getMax()) {
mTitle.setCompoundDrawables(null, null, null, null);
((Animatable) mCircularProgress).stop();
mHorizontalProgress.setVisibility(View.INVISIBLE);
if (!mInVoiceMode) {
mRtButton.setImageDrawable(mBookmarkDrawable);
mRtButton.setVisibility(View.VISIBLE);
mStopButton.setVisibility(View.GONE);
mTitleBg.setBackgroundDrawable(mNormalBackground);
mTitleBg.setPadding(mLeftMargin, 0, mRightMargin, 0);
}
mInLoad = false;
} else {
mHorizontalProgress.setProgress(newProgress);
if (!mInLoad && getWindowToken() != null) {
// checking the window token lets us be sure that we
// are attached to a window before starting the animation,
// preventing a potential race condition
// (fix for bug http://b/2115736)
mTitle.setCompoundDrawables(null, null, mCircularProgress,
null);
((Animatable) mCircularProgress).start();
mHorizontalProgress.setVisibility(View.VISIBLE);
if (!mInVoiceMode) {
mTitleBg.setBackgroundDrawable(mLoadingBackground);
mTitleBg.setPadding(mLeftMargin, 0, mRightMargin, 0);
mRtButton.setVisibility(View.GONE);
mStopButton.setVisibility(View.VISIBLE);
}
mInLoad = true;
}
}
}
/**
* Update the text displayed in the title bar.
* @param title String to display. If null, the loading string will be
* shown.
*/
/* package */ void setDisplayTitle(String title) {
if (title == null) {
mTitle.setText(R.string.title_bar_loading);
} else {
if (mInVoiceMode) {
// Add two spaces. The second one will be replaced with an
// image, and the first one will put space between it and the
// text
SpannableString spannable = new SpannableString(title + " ");
int end = spannable.length();
spannable.setSpan(mArcsSpan, end - 1, end,
Spanned.SPAN_MARK_POINT);
mTitle.setText(spannable);
} else {
mTitle.setText(title);
}
}
}
/* package */ void setToTabPicker() {
mTitle.setText(R.string.tab_picker_title);
setFavicon(null);
setLock(null);
mHorizontalProgress.setVisibility(View.GONE);
}
}
|