blob: 97fe7476868a424d9ec94fa71df8b3950c82c9e9 [file] [log] [blame]
Michael Kolb11d19782011-03-20 10:17:40 -07001/*
2 * Copyright (C) 2009 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.android.browser;
18
19import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
Michael Kolb11d19782011-03-20 10:17:40 -070020
21import android.app.Activity;
22import android.content.Context;
Michael Kolb2814a362011-05-19 15:49:41 -070023import android.content.res.Resources;
24import android.graphics.drawable.Drawable;
Michael Kolb11d19782011-03-20 10:17:40 -070025import android.view.ContextMenu;
26import android.view.MenuInflater;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.view.View.OnFocusChangeListener;
Michael Kolb2814a362011-05-19 15:49:41 -070030import android.webkit.WebView;
Michael Kolbfdb70242011-03-24 09:41:11 -070031import android.widget.FrameLayout;
Michael Kolb2814a362011-05-19 15:49:41 -070032import android.widget.ImageButton;
Michael Kolb11d19782011-03-20 10:17:40 -070033import android.widget.ImageView;
34
35import java.util.List;
36
37/**
38 * This class represents a title bar for a particular "tab" or "window" in the
39 * browser.
40 */
41public class TitleBarPhone extends TitleBarBase implements OnFocusChangeListener,
42 OnClickListener, TextChangeWatcher {
43
44 private Activity mActivity;
Michael Kolbc16c5952011-03-29 15:37:03 -070045 private ImageView mStopButton;
Michael Kolb11d19782011-03-20 10:17:40 -070046 private ImageView mVoiceButton;
Michael Kolb11d19782011-03-20 10:17:40 -070047 private boolean mHasLockIcon;
Michael Kolb2814a362011-05-19 15:49:41 -070048 private ImageButton mForward;
49 private Drawable mStopDrawable;
50 private Drawable mRefreshDrawable;
Michael Kolb11d19782011-03-20 10:17:40 -070051
Michael Kolb46f987e2011-04-05 10:39:10 -070052 public TitleBarPhone(Activity activity, UiController controller, PhoneUi ui,
53 FrameLayout parent) {
54 super(activity, controller, ui, parent);
Michael Kolb11d19782011-03-20 10:17:40 -070055 mActivity = activity;
56 initLayout(activity, R.layout.title_bar);
57 }
58
59 @Override
60 protected void initLayout(Context context, int layoutId) {
61 super.initLayout(context, layoutId);
Michael Kolb11d19782011-03-20 10:17:40 -070062 mLockIcon = (ImageView) findViewById(R.id.lock);
63 mFavicon = (ImageView) findViewById(R.id.favicon);
Michael Kolbc16c5952011-03-29 15:37:03 -070064 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070065 mStopButton.setOnClickListener(this);
66 mVoiceButton = (ImageView) findViewById(R.id.voice);
67 mVoiceButton.setOnClickListener(this);
Michael Kolb2814a362011-05-19 15:49:41 -070068 mForward = (ImageButton) findViewById(R.id.forward);
69 mForward.setOnClickListener(this);
Michael Kolb11d19782011-03-20 10:17:40 -070070 setFocusState(false);
Michael Kolb2814a362011-05-19 15:49:41 -070071 Resources res = context.getResources();
72 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
73 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb11d19782011-03-20 10:17:40 -070074 }
75
76 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070077 public void createContextMenu(ContextMenu menu) {
78 MenuInflater inflater = mActivity.getMenuInflater();
79 inflater.inflate(R.menu.title_context, menu);
80 mActivity.onCreateContextMenu(menu, this, null);
81 }
82
83 @Override
84 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
85 super.setInVoiceMode(voicemode, voiceResults);
86 }
87
88 @Override
89 protected void setSearchMode(boolean voiceSearchEnabled) {
90 boolean showvoicebutton = voiceSearchEnabled &&
91 mUiController.supportsVoiceSearch();
92 mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE :
93 View.GONE);
94 }
95
96 @Override
97 protected void setFocusState(boolean focus) {
98 super.setFocusState(focus);
99 if (focus) {
100 mHasLockIcon = (mLockIcon.getVisibility() == View.VISIBLE);
Michael Kolb11d19782011-03-20 10:17:40 -0700101 mLockIcon.setVisibility(View.GONE);
102 mStopButton.setVisibility(View.GONE);
103 mVoiceButton.setVisibility(View.VISIBLE);
104 } else {
Michael Kolb11d19782011-03-20 10:17:40 -0700105 mLockIcon.setVisibility(mHasLockIcon ? View.VISIBLE : View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700106 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb11d19782011-03-20 10:17:40 -0700107 mVoiceButton.setVisibility(View.GONE);
108 }
109 }
110
Michael Kolb11d19782011-03-20 10:17:40 -0700111 @Override
Michael Kolb46f987e2011-04-05 10:39:10 -0700112 protected void onProgressStarted() {
113 setFocusState(mUrlInput.hasFocus());
114 }
115
116 @Override
117 protected void onProgressStopped() {
118 setFocusState(mUrlInput.hasFocus());
Michael Kolb11d19782011-03-20 10:17:40 -0700119 }
120
Michael Kolb2814a362011-05-19 15:49:41 -0700121 @Override
122 void setProgress(int progress) {
123 super.setProgress(progress);
124 if (progress == 100) {
125 mStopButton.setImageDrawable(mRefreshDrawable);
126 } else if (mStopButton.getDrawable() != mStopDrawable) {
127 mStopButton.setImageDrawable(mStopDrawable);
128 }
129 updateNavigationState();
130 }
131
Michael Kolb11d19782011-03-20 10:17:40 -0700132 /**
133 * Update the text displayed in the title bar.
134 * @param title String to display. If null, the new tab string will be
135 * shown.
136 */
137 @Override
138 void setDisplayTitle(String title) {
139 if (title == null) {
140 mUrlInput.setText(R.string.new_tab);
141 } else {
142 mUrlInput.setText(title);
143 }
Michael Kolb2814a362011-05-19 15:49:41 -0700144 mUrlInput.setSelection(0);
145 updateNavigationState();
Michael Kolb11d19782011-03-20 10:17:40 -0700146 }
147
148 @Override
149 public void onFocusChange(View v, boolean hasFocus) {
150 if (v == mUrlInput) {
151 if (hasFocus) {
152 mActivity.closeOptionsMenu();
153 }
154 }
155 super.onFocusChange(v, hasFocus);
Michael Kolbf2055602011-04-09 17:20:03 -0700156 if (!hasFocus) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700157 mBaseUi.hideTitleBar();
158 }
Michael Kolb11d19782011-03-20 10:17:40 -0700159 }
160
161 @Override
162 public void onClick(View v) {
163 if (v == mStopButton) {
Michael Kolb2814a362011-05-19 15:49:41 -0700164 if (mInLoad) {
165 mUiController.stopLoading();
166 } else {
167 WebView web = mBaseUi.getWebView();
168 if (web != null) {
169 web.reload();
170 }
171 }
Michael Kolb11d19782011-03-20 10:17:40 -0700172 } else if (v == mVoiceButton) {
173 mUiController.startVoiceSearch();
Michael Kolb2814a362011-05-19 15:49:41 -0700174 } else if (v == mForward) {
175 WebView web = mBaseUi.getWebView();
176 if (web != null) {
177 web.goForward();
178 }
Michael Kolb11d19782011-03-20 10:17:40 -0700179 } else {
180 super.onClick(v);
181 }
182 }
183
Michael Kolb2814a362011-05-19 15:49:41 -0700184 private void updateNavigationState() {
185 WebView web = mBaseUi.getWebView();
186 if (web != null) {
187 mForward.setVisibility(web.canGoForward() ? View.VISIBLE : View.GONE);
188 }
189 }
190
Michael Kolb11d19782011-03-20 10:17:40 -0700191}