Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher; |
| 20 | import com.android.browser.view.StopProgressView; |
| 21 | |
| 22 | import android.app.Activity; |
| 23 | import android.content.Context; |
| 24 | import android.view.ContextMenu; |
| 25 | import android.view.MenuInflater; |
| 26 | import android.view.View; |
| 27 | import android.view.View.OnClickListener; |
| 28 | import android.view.View.OnFocusChangeListener; |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 29 | import android.view.ViewGroup; |
| 30 | import android.widget.AbsoluteLayout; |
| 31 | import android.widget.FrameLayout; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 32 | import android.widget.ImageView; |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 33 | import android.widget.RelativeLayout.LayoutParams; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 34 | |
| 35 | import java.util.List; |
| 36 | |
| 37 | /** |
| 38 | * This class represents a title bar for a particular "tab" or "window" in the |
| 39 | * browser. |
| 40 | */ |
| 41 | public class TitleBarPhone extends TitleBarBase implements OnFocusChangeListener, |
| 42 | OnClickListener, TextChangeWatcher { |
| 43 | |
| 44 | private Activity mActivity; |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame^] | 45 | private ImageView mStopButton; |
| 46 | private PageProgressView mProgress; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 47 | private ImageView mVoiceButton; |
| 48 | private boolean mInLoad; |
| 49 | private View mContainer; |
| 50 | private boolean mHasLockIcon; |
| 51 | |
| 52 | public TitleBarPhone(Activity activity, UiController controller, PhoneUi ui) { |
| 53 | super(activity, controller, ui); |
| 54 | mActivity = activity; |
| 55 | initLayout(activity, R.layout.title_bar); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected void initLayout(Context context, int layoutId) { |
| 60 | super.initLayout(context, layoutId); |
| 61 | mContainer = findViewById(R.id.taburlbar); |
| 62 | mLockIcon = (ImageView) findViewById(R.id.lock); |
| 63 | mFavicon = (ImageView) findViewById(R.id.favicon); |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame^] | 64 | mStopButton = (ImageView) findViewById(R.id.stop); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 65 | mStopButton.setOnClickListener(this); |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame^] | 66 | mProgress = (PageProgressView) findViewById(R.id.progress); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 67 | mVoiceButton = (ImageView) findViewById(R.id.voice); |
| 68 | mVoiceButton.setOnClickListener(this); |
| 69 | setFocusState(false); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public int getEmbeddedHeight() { |
| 74 | int height = mContainer.getHeight(); |
| 75 | return height; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void createContextMenu(ContextMenu menu) { |
| 80 | MenuInflater inflater = mActivity.getMenuInflater(); |
| 81 | inflater.inflate(R.menu.title_context, menu); |
| 82 | mActivity.onCreateContextMenu(menu, this, null); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void setInVoiceMode(boolean voicemode, List<String> voiceResults) { |
| 87 | super.setInVoiceMode(voicemode, voiceResults); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | protected void setSearchMode(boolean voiceSearchEnabled) { |
| 92 | boolean showvoicebutton = voiceSearchEnabled && |
| 93 | mUiController.supportsVoiceSearch(); |
| 94 | mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE : |
| 95 | View.GONE); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | protected void setFocusState(boolean focus) { |
| 100 | super.setFocusState(focus); |
| 101 | if (focus) { |
| 102 | mHasLockIcon = (mLockIcon.getVisibility() == View.VISIBLE); |
| 103 | mFavicon.setVisibility(View.GONE); |
| 104 | mLockIcon.setVisibility(View.GONE); |
| 105 | mStopButton.setVisibility(View.GONE); |
| 106 | mVoiceButton.setVisibility(View.VISIBLE); |
| 107 | } else { |
| 108 | mFavicon.setVisibility(View.VISIBLE); |
| 109 | mLockIcon.setVisibility(mHasLockIcon ? View.VISIBLE : View.GONE); |
| 110 | if (mInLoad) { |
| 111 | mStopButton.setVisibility(View.VISIBLE); |
| 112 | } else { |
| 113 | mStopButton.setVisibility(View.GONE); |
| 114 | } |
| 115 | mVoiceButton.setVisibility(View.GONE); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Update the progress, from 0 to 100. |
| 121 | */ |
| 122 | @Override |
| 123 | void setProgress(int newProgress) { |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame^] | 124 | boolean blockvisuals = mUseQuickControls && isEditingUrl(); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 125 | if (newProgress >= PROGRESS_MAX) { |
| 126 | mInLoad = false; |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame^] | 127 | if (!blockvisuals) { |
| 128 | mProgress.setProgress(PageProgressView.MAX_PROGRESS); |
| 129 | mProgress.setVisibility(View.GONE); |
| 130 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 131 | setFocusState(mUrlInput.hasFocus()); |
| 132 | } else { |
| 133 | if (!mInLoad) { |
| 134 | mInLoad = true; |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame^] | 135 | if (!blockvisuals) { |
| 136 | mProgress.setVisibility(View.VISIBLE); |
| 137 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 138 | setFocusState(mUrlInput.hasFocus()); |
| 139 | } |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame^] | 140 | mProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS |
| 141 | / PROGRESS_MAX); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Update the text displayed in the title bar. |
| 147 | * @param title String to display. If null, the new tab string will be |
| 148 | * shown. |
| 149 | */ |
| 150 | @Override |
| 151 | void setDisplayTitle(String title) { |
| 152 | if (title == null) { |
| 153 | mUrlInput.setText(R.string.new_tab); |
| 154 | } else { |
| 155 | mUrlInput.setText(title); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onFocusChange(View v, boolean hasFocus) { |
| 161 | if (v == mUrlInput) { |
| 162 | if (hasFocus) { |
| 163 | mActivity.closeOptionsMenu(); |
| 164 | } |
| 165 | } |
| 166 | super.onFocusChange(v, hasFocus); |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 167 | if (mUseQuickControls && !hasFocus) { |
| 168 | mBaseUi.hideTitleBar(); |
| 169 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void onClick(View v) { |
| 174 | if (v == mStopButton) { |
| 175 | mUiController.stopLoading(); |
| 176 | } else if (v == mVoiceButton) { |
| 177 | mUiController.startVoiceSearch(); |
| 178 | } else { |
| 179 | super.onClick(v); |
| 180 | } |
| 181 | } |
| 182 | |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 183 | @Override |
| 184 | void startEditingUrl(boolean clearInput) { |
| 185 | // editing takes preference of progress |
| 186 | mContainer.setVisibility(View.VISIBLE); |
| 187 | if (!mUrlInput.hasFocus()) { |
| 188 | mUrlInput.requestFocus(); |
| 189 | } |
| 190 | if (clearInput) { |
| 191 | mUrlInput.setText(""); |
| 192 | } else if (mInVoiceMode) { |
| 193 | mUrlInput.showDropDown(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | void setTitleGravity(int gravity) { |
| 199 | if (mUseQuickControls) { |
| 200 | FrameLayout.LayoutParams lp = |
| 201 | (FrameLayout.LayoutParams) getLayoutParams(); |
| 202 | lp.gravity = gravity; |
| 203 | setLayoutParams(lp); |
| 204 | } else { |
| 205 | super.setTitleGravity(gravity); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | protected void setUseQuickControls(boolean useQuickControls) { |
| 211 | mUseQuickControls = useQuickControls; |
| 212 | setLayoutParams(makeLayoutParams()); |
| 213 | } |
| 214 | |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame^] | 215 | void setShowProgressOnly(boolean progress) { |
| 216 | if (progress && !inAutoLogin()) { |
| 217 | mContainer.setVisibility(View.GONE); |
| 218 | } else { |
| 219 | mContainer.setVisibility(View.VISIBLE); |
| 220 | } |
| 221 | } |
| 222 | |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 223 | private ViewGroup.LayoutParams makeLayoutParams() { |
| 224 | if (mUseQuickControls) { |
| 225 | return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, |
| 226 | LayoutParams.WRAP_CONTENT); |
| 227 | } else { |
| 228 | return new AbsoluteLayout.LayoutParams( |
| 229 | LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, |
| 230 | 0, 0); |
| 231 | } |
| 232 | } |
| 233 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 234 | } |