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 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.content.Context; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
| 22 | import android.graphics.drawable.Drawable; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 23 | import android.view.ContextMenu; |
| 24 | import android.view.MenuInflater; |
| 25 | import android.view.View; |
| 26 | import android.view.View.OnClickListener; |
| 27 | import android.view.View.OnFocusChangeListener; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 28 | import android.webkit.WebView; |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 29 | import android.widget.FrameLayout; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 30 | import android.widget.ImageView; |
| 31 | |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 32 | import com.android.browser.UrlInputView.StateListener; |
| 33 | import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher; |
| 34 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 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, |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 42 | OnClickListener, TextChangeWatcher, StateListener { |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 43 | |
| 44 | private Activity mActivity; |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame] | 45 | private ImageView mStopButton; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 46 | private ImageView mVoiceButton; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 47 | private Drawable mStopDrawable; |
| 48 | private Drawable mRefreshDrawable; |
John Reck | 8ac4290 | 2011-06-29 16:14:34 -0700 | [diff] [blame] | 49 | private View mTabSwitcher; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 50 | private View mComboIcon; |
| 51 | private View mTitleContainer; |
| 52 | private Drawable mTextfieldBgDrawable; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 53 | |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 54 | public TitleBarPhone(Activity activity, UiController controller, PhoneUi ui, |
| 55 | FrameLayout parent) { |
| 56 | super(activity, controller, ui, parent); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 57 | 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 Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 64 | mLockIcon = (ImageView) findViewById(R.id.lock); |
| 65 | mFavicon = (ImageView) findViewById(R.id.favicon); |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame] | 66 | mStopButton = (ImageView) findViewById(R.id.stop); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 67 | mStopButton.setOnClickListener(this); |
| 68 | mVoiceButton = (ImageView) findViewById(R.id.voice); |
| 69 | mVoiceButton.setOnClickListener(this); |
John Reck | 8ac4290 | 2011-06-29 16:14:34 -0700 | [diff] [blame] | 70 | mTabSwitcher = findViewById(R.id.tab_switcher); |
| 71 | mTabSwitcher.setOnClickListener(this); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 72 | mComboIcon = findViewById(R.id.iconcombo); |
| 73 | mTitleContainer = findViewById(R.id.title_bg); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 74 | setFocusState(false); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 75 | 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 Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 78 | mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark); |
| 79 | setUaSwitcher(mComboIcon); |
John Reck | 67f3363 | 2011-06-17 16:23:42 -0700 | [diff] [blame] | 80 | mUrlInput.setContainer(this); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 81 | mUrlInput.setStateListener(this); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 85 | 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 Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 105 | void setProgress(int progress) { |
| 106 | super.setProgress(progress); |
| 107 | if (progress == 100) { |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 108 | mStopButton.setVisibility(View.GONE); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 109 | mStopButton.setImageDrawable(mRefreshDrawable); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 110 | 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 Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 121 | } |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 124 | /** |
| 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 Reck | 67f3363 | 2011-06-17 16:23:42 -0700 | [diff] [blame] | 131 | 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 Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 138 | } |
| 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 Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 154 | if (mInLoad) { |
| 155 | mUiController.stopLoading(); |
| 156 | } else { |
| 157 | WebView web = mBaseUi.getWebView(); |
| 158 | if (web != null) { |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 159 | stopEditingUrl(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 160 | web.reload(); |
| 161 | } |
| 162 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 163 | } else if (v == mVoiceButton) { |
| 164 | mUiController.startVoiceSearch(); |
John Reck | 8ac4290 | 2011-06-29 16:14:34 -0700 | [diff] [blame] | 165 | } else if (v == mTabSwitcher) { |
| 166 | mBaseUi.onMenuKey(); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 167 | } else { |
| 168 | super.onClick(v); |
| 169 | } |
| 170 | } |
| 171 | |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 172 | @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 Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 199 | } |