blob: f41eca783fec7a81fab3d693e17f0a0912ac5071 [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
Michael Kolb11d19782011-03-20 10:17:40 -070019import android.app.Activity;
20import android.content.Context;
Michael Kolb2814a362011-05-19 15:49:41 -070021import android.content.res.Resources;
22import android.graphics.drawable.Drawable;
Michael Kolb11d19782011-03-20 10:17:40 -070023import android.view.ContextMenu;
24import android.view.MenuInflater;
25import android.view.View;
26import android.view.View.OnClickListener;
27import android.view.View.OnFocusChangeListener;
Michael Kolb2814a362011-05-19 15:49:41 -070028import android.webkit.WebView;
Michael Kolbfdb70242011-03-24 09:41:11 -070029import android.widget.FrameLayout;
Michael Kolb11d19782011-03-20 10:17:40 -070030import android.widget.ImageView;
31
Michael Kolb305b1c52011-06-21 16:16:22 -070032import com.android.browser.UrlInputView.StateListener;
33import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
34
Michael Kolb11d19782011-03-20 10:17:40 -070035import 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,
Michael Kolb305b1c52011-06-21 16:16:22 -070042 OnClickListener, TextChangeWatcher, StateListener {
Michael Kolb11d19782011-03-20 10:17:40 -070043
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 Kolb2814a362011-05-19 15:49:41 -070047 private Drawable mStopDrawable;
48 private Drawable mRefreshDrawable;
John Reck8ac42902011-06-29 16:14:34 -070049 private View mTabSwitcher;
Michael Kolb305b1c52011-06-21 16:16:22 -070050 private View mComboIcon;
51 private View mTitleContainer;
52 private Drawable mTextfieldBgDrawable;
Michael Kolb11d19782011-03-20 10:17:40 -070053
Michael Kolb46f987e2011-04-05 10:39:10 -070054 public TitleBarPhone(Activity activity, UiController controller, PhoneUi ui,
55 FrameLayout parent) {
56 super(activity, controller, ui, parent);
Michael Kolb11d19782011-03-20 10:17:40 -070057 mActivity = activity;
58 initLayout(activity, R.layout.title_bar);
59 }
60
61 @Override
62 protected void initLayout(Context context, int layoutId) {
63 super.initLayout(context, layoutId);
Michael Kolb11d19782011-03-20 10:17:40 -070064 mLockIcon = (ImageView) findViewById(R.id.lock);
65 mFavicon = (ImageView) findViewById(R.id.favicon);
Michael Kolbc16c5952011-03-29 15:37:03 -070066 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070067 mStopButton.setOnClickListener(this);
68 mVoiceButton = (ImageView) findViewById(R.id.voice);
69 mVoiceButton.setOnClickListener(this);
John Reck8ac42902011-06-29 16:14:34 -070070 mTabSwitcher = findViewById(R.id.tab_switcher);
71 mTabSwitcher.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070072 mComboIcon = findViewById(R.id.iconcombo);
73 mTitleContainer = findViewById(R.id.title_bg);
Michael Kolb11d19782011-03-20 10:17:40 -070074 setFocusState(false);
Michael Kolb2814a362011-05-19 15:49:41 -070075 Resources res = context.getResources();
76 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
77 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb305b1c52011-06-21 16:16:22 -070078 mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
79 setUaSwitcher(mComboIcon);
John Reck67f33632011-06-17 16:23:42 -070080 mUrlInput.setContainer(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070081 mUrlInput.setStateListener(this);
Michael Kolb11d19782011-03-20 10:17:40 -070082 }
83
84 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070085 public void createContextMenu(ContextMenu menu) {
86 MenuInflater inflater = mActivity.getMenuInflater();
87 inflater.inflate(R.menu.title_context, menu);
88 mActivity.onCreateContextMenu(menu, this, null);
89 }
90
91 @Override
92 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
93 super.setInVoiceMode(voicemode, voiceResults);
94 }
95
96 @Override
97 protected void setSearchMode(boolean voiceSearchEnabled) {
98 boolean showvoicebutton = voiceSearchEnabled &&
99 mUiController.supportsVoiceSearch();
100 mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE :
101 View.GONE);
102 }
103
104 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700105 void setProgress(int progress) {
106 super.setProgress(progress);
107 if (progress == 100) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700108 mStopButton.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700109 mStopButton.setImageDrawable(mRefreshDrawable);
Michael Kolb305b1c52011-06-21 16:16:22 -0700110 if (!isEditingUrl()) {
111 mComboIcon.setVisibility(View.VISIBLE);
112 }
113 } else {
114 if (mStopButton.getDrawable() != mStopDrawable) {
115 mStopButton.setImageDrawable(mStopDrawable);
116 if (mStopButton.getVisibility() != View.VISIBLE) {
117 mComboIcon.setVisibility(View.GONE);
118 mStopButton.setVisibility(View.VISIBLE);
119 }
120 }
Michael Kolb2814a362011-05-19 15:49:41 -0700121 }
Michael Kolb2814a362011-05-19 15:49:41 -0700122 }
123
Michael Kolb11d19782011-03-20 10:17:40 -0700124 /**
125 * Update the text displayed in the title bar.
126 * @param title String to display. If null, the new tab string will be
127 * shown.
128 */
129 @Override
130 void setDisplayTitle(String title) {
John Reck67f33632011-06-17 16:23:42 -0700131 if (!isEditingUrl()) {
132 if (title == null) {
133 mUrlInput.setText(R.string.new_tab);
134 } else {
135 mUrlInput.setText(title);
136 }
137 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700138 }
139 }
140
141 @Override
142 public void onFocusChange(View v, boolean hasFocus) {
143 if (v == mUrlInput) {
144 if (hasFocus) {
145 mActivity.closeOptionsMenu();
146 }
147 }
148 super.onFocusChange(v, hasFocus);
149 }
150
151 @Override
152 public void onClick(View v) {
153 if (v == mStopButton) {
Michael Kolb2814a362011-05-19 15:49:41 -0700154 if (mInLoad) {
155 mUiController.stopLoading();
156 } else {
157 WebView web = mBaseUi.getWebView();
158 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700159 stopEditingUrl();
Michael Kolb2814a362011-05-19 15:49:41 -0700160 web.reload();
161 }
162 }
Michael Kolb11d19782011-03-20 10:17:40 -0700163 } else if (v == mVoiceButton) {
164 mUiController.startVoiceSearch();
John Reck8ac42902011-06-29 16:14:34 -0700165 } else if (v == mTabSwitcher) {
166 mBaseUi.onMenuKey();
Michael Kolb11d19782011-03-20 10:17:40 -0700167 } else {
168 super.onClick(v);
169 }
170 }
171
Michael Kolb305b1c52011-06-21 16:16:22 -0700172 @Override
173 public void onStateChanged(int state) {
174 switch(state) {
175 case StateListener.STATE_NORMAL:
176 mComboIcon.setVisibility(View.VISIBLE);
177 mStopButton.setVisibility(View.GONE);
178 setSearchMode(false);
179 mTabSwitcher.setVisibility(View.VISIBLE);
180 mTitleContainer.setBackgroundDrawable(null);
181 break;
182 case StateListener.STATE_HIGHLIGHTED:
183 mComboIcon.setVisibility(View.GONE);
184 mStopButton.setVisibility(View.VISIBLE);
185 setSearchMode(true);
186 mTabSwitcher.setVisibility(View.GONE);
187 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
188 break;
189 case StateListener.STATE_EDITED:
190 mComboIcon.setVisibility(View.GONE);
191 mStopButton.setVisibility(View.GONE);
192 setSearchMode(false);
193 mTabSwitcher.setVisibility(View.GONE);
194 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
195 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700196 }
197 }
198
Michael Kolb11d19782011-03-20 10:17:40 -0700199}