blob: 528ea43efe8cda585318b26161c99a31aca55437 [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;
John Reck8ac42902011-06-29 16:14:34 -070051 private View mTabSwitcher;
Michael Kolb11d19782011-03-20 10:17:40 -070052
Michael Kolb46f987e2011-04-05 10:39:10 -070053 public TitleBarPhone(Activity activity, UiController controller, PhoneUi ui,
54 FrameLayout parent) {
55 super(activity, controller, ui, parent);
Michael Kolb11d19782011-03-20 10:17:40 -070056 mActivity = activity;
57 initLayout(activity, R.layout.title_bar);
58 }
59
60 @Override
61 protected void initLayout(Context context, int layoutId) {
62 super.initLayout(context, layoutId);
Michael Kolb11d19782011-03-20 10:17:40 -070063 mLockIcon = (ImageView) findViewById(R.id.lock);
64 mFavicon = (ImageView) findViewById(R.id.favicon);
Michael Kolbc16c5952011-03-29 15:37:03 -070065 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070066 mStopButton.setOnClickListener(this);
67 mVoiceButton = (ImageView) findViewById(R.id.voice);
68 mVoiceButton.setOnClickListener(this);
Michael Kolb2814a362011-05-19 15:49:41 -070069 mForward = (ImageButton) findViewById(R.id.forward);
70 mForward.setOnClickListener(this);
John Reck8ac42902011-06-29 16:14:34 -070071 mTabSwitcher = findViewById(R.id.tab_switcher);
72 mTabSwitcher.setOnClickListener(this);
Michael Kolb11d19782011-03-20 10:17:40 -070073 setFocusState(false);
Michael Kolb2814a362011-05-19 15:49:41 -070074 Resources res = context.getResources();
75 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
76 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
John Reck46500332011-06-07 14:36:10 -070077 setUaSwitcher(mFavicon);
John Reck67f33632011-06-17 16:23:42 -070078 mUrlInput.setContainer(this);
Michael Kolb11d19782011-03-20 10:17:40 -070079 }
80
81 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070082 public void createContextMenu(ContextMenu menu) {
83 MenuInflater inflater = mActivity.getMenuInflater();
84 inflater.inflate(R.menu.title_context, menu);
85 mActivity.onCreateContextMenu(menu, this, null);
86 }
87
88 @Override
89 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
90 super.setInVoiceMode(voicemode, voiceResults);
91 }
92
93 @Override
94 protected void setSearchMode(boolean voiceSearchEnabled) {
95 boolean showvoicebutton = voiceSearchEnabled &&
96 mUiController.supportsVoiceSearch();
97 mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE :
98 View.GONE);
99 }
100
101 @Override
102 protected void setFocusState(boolean focus) {
103 super.setFocusState(focus);
104 if (focus) {
105 mHasLockIcon = (mLockIcon.getVisibility() == View.VISIBLE);
Michael Kolb11d19782011-03-20 10:17:40 -0700106 mLockIcon.setVisibility(View.GONE);
107 mStopButton.setVisibility(View.GONE);
108 mVoiceButton.setVisibility(View.VISIBLE);
109 } else {
Michael Kolb11d19782011-03-20 10:17:40 -0700110 mLockIcon.setVisibility(mHasLockIcon ? View.VISIBLE : View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700111 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb11d19782011-03-20 10:17:40 -0700112 mVoiceButton.setVisibility(View.GONE);
113 }
114 }
115
Michael Kolb11d19782011-03-20 10:17:40 -0700116 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700117 void setProgress(int progress) {
118 super.setProgress(progress);
119 if (progress == 100) {
120 mStopButton.setImageDrawable(mRefreshDrawable);
121 } else if (mStopButton.getDrawable() != mStopDrawable) {
122 mStopButton.setImageDrawable(mStopDrawable);
123 }
124 updateNavigationState();
125 }
126
Michael Kolb11d19782011-03-20 10:17:40 -0700127 /**
128 * Update the text displayed in the title bar.
129 * @param title String to display. If null, the new tab string will be
130 * shown.
131 */
132 @Override
133 void setDisplayTitle(String title) {
John Reck67f33632011-06-17 16:23:42 -0700134 if (!isEditingUrl()) {
135 if (title == null) {
136 mUrlInput.setText(R.string.new_tab);
137 } else {
138 mUrlInput.setText(title);
139 }
140 mUrlInput.setSelection(0);
141 updateNavigationState();
Michael Kolb11d19782011-03-20 10:17:40 -0700142 }
143 }
144
145 @Override
146 public void onFocusChange(View v, boolean hasFocus) {
147 if (v == mUrlInput) {
148 if (hasFocus) {
149 mActivity.closeOptionsMenu();
150 }
151 }
152 super.onFocusChange(v, hasFocus);
153 }
154
155 @Override
156 public void onClick(View v) {
157 if (v == mStopButton) {
Michael Kolb2814a362011-05-19 15:49:41 -0700158 if (mInLoad) {
159 mUiController.stopLoading();
160 } else {
161 WebView web = mBaseUi.getWebView();
162 if (web != null) {
163 web.reload();
164 }
165 }
Michael Kolb11d19782011-03-20 10:17:40 -0700166 } else if (v == mVoiceButton) {
167 mUiController.startVoiceSearch();
Michael Kolb2814a362011-05-19 15:49:41 -0700168 } else if (v == mForward) {
169 WebView web = mBaseUi.getWebView();
170 if (web != null) {
171 web.goForward();
172 }
John Reck8ac42902011-06-29 16:14:34 -0700173 } else if (v == mTabSwitcher) {
174 mBaseUi.onMenuKey();
Michael Kolb11d19782011-03-20 10:17:40 -0700175 } else {
176 super.onClick(v);
177 }
178 }
179
Michael Kolb2814a362011-05-19 15:49:41 -0700180 private void updateNavigationState() {
181 WebView web = mBaseUi.getWebView();
182 if (web != null) {
183 mForward.setVisibility(web.canGoForward() ? View.VISIBLE : View.GONE);
184 }
185 }
186
Michael Kolb11d19782011-03-20 10:17:40 -0700187}