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; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 20 | |
| 21 | import android.app.Activity; |
| 22 | import android.content.Context; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 23 | import android.content.res.Resources; |
| 24 | import android.graphics.drawable.Drawable; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 25 | import android.view.ContextMenu; |
| 26 | import android.view.MenuInflater; |
| 27 | import android.view.View; |
| 28 | import android.view.View.OnClickListener; |
| 29 | import android.view.View.OnFocusChangeListener; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 30 | import android.webkit.WebView; |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 31 | import android.widget.FrameLayout; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 32 | import android.widget.ImageButton; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 33 | import android.widget.ImageView; |
| 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; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 46 | private ImageView mVoiceButton; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 47 | private boolean mHasLockIcon; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 48 | private ImageButton mForward; |
| 49 | private Drawable mStopDrawable; |
| 50 | private Drawable mRefreshDrawable; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 51 | |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 52 | public TitleBarPhone(Activity activity, UiController controller, PhoneUi ui, |
| 53 | FrameLayout parent) { |
| 54 | super(activity, controller, ui, parent); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 55 | 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 Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 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); |
| 66 | mVoiceButton = (ImageView) findViewById(R.id.voice); |
| 67 | mVoiceButton.setOnClickListener(this); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 68 | mForward = (ImageButton) findViewById(R.id.forward); |
| 69 | mForward.setOnClickListener(this); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 70 | setFocusState(false); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 71 | Resources res = context.getResources(); |
| 72 | mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark); |
| 73 | mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark); |
John Reck | 4650033 | 2011-06-07 14:36:10 -0700 | [diff] [blame^] | 74 | setUaSwitcher(mFavicon); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 78 | public void createContextMenu(ContextMenu menu) { |
| 79 | MenuInflater inflater = mActivity.getMenuInflater(); |
| 80 | inflater.inflate(R.menu.title_context, menu); |
| 81 | mActivity.onCreateContextMenu(menu, this, null); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void setInVoiceMode(boolean voicemode, List<String> voiceResults) { |
| 86 | super.setInVoiceMode(voicemode, voiceResults); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected void setSearchMode(boolean voiceSearchEnabled) { |
| 91 | boolean showvoicebutton = voiceSearchEnabled && |
| 92 | mUiController.supportsVoiceSearch(); |
| 93 | mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE : |
| 94 | View.GONE); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | protected void setFocusState(boolean focus) { |
| 99 | super.setFocusState(focus); |
| 100 | if (focus) { |
| 101 | mHasLockIcon = (mLockIcon.getVisibility() == View.VISIBLE); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 102 | mLockIcon.setVisibility(View.GONE); |
| 103 | mStopButton.setVisibility(View.GONE); |
| 104 | mVoiceButton.setVisibility(View.VISIBLE); |
| 105 | } else { |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 106 | mLockIcon.setVisibility(mHasLockIcon ? View.VISIBLE : View.GONE); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 107 | mStopButton.setVisibility(View.VISIBLE); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 108 | mVoiceButton.setVisibility(View.GONE); |
| 109 | } |
| 110 | } |
| 111 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 112 | @Override |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 113 | protected void onProgressStarted() { |
| 114 | setFocusState(mUrlInput.hasFocus()); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | protected void onProgressStopped() { |
| 119 | setFocusState(mUrlInput.hasFocus()); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 122 | @Override |
| 123 | void setProgress(int progress) { |
| 124 | super.setProgress(progress); |
| 125 | if (progress == 100) { |
| 126 | mStopButton.setImageDrawable(mRefreshDrawable); |
| 127 | } else if (mStopButton.getDrawable() != mStopDrawable) { |
| 128 | mStopButton.setImageDrawable(mStopDrawable); |
| 129 | } |
| 130 | updateNavigationState(); |
| 131 | } |
| 132 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Update the text displayed in the title bar. |
| 135 | * @param title String to display. If null, the new tab string will be |
| 136 | * shown. |
| 137 | */ |
| 138 | @Override |
| 139 | void setDisplayTitle(String title) { |
| 140 | if (title == null) { |
| 141 | mUrlInput.setText(R.string.new_tab); |
| 142 | } else { |
| 143 | mUrlInput.setText(title); |
| 144 | } |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 145 | mUrlInput.setSelection(0); |
| 146 | updateNavigationState(); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void onFocusChange(View v, boolean hasFocus) { |
| 151 | if (v == mUrlInput) { |
| 152 | if (hasFocus) { |
| 153 | mActivity.closeOptionsMenu(); |
| 154 | } |
| 155 | } |
| 156 | super.onFocusChange(v, hasFocus); |
Michael Kolb | f205560 | 2011-04-09 17:20:03 -0700 | [diff] [blame] | 157 | if (!hasFocus) { |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 158 | mBaseUi.hideTitleBar(); |
| 159 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void onClick(View v) { |
| 164 | if (v == mStopButton) { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 165 | if (mInLoad) { |
| 166 | mUiController.stopLoading(); |
| 167 | } else { |
| 168 | WebView web = mBaseUi.getWebView(); |
| 169 | if (web != null) { |
| 170 | web.reload(); |
| 171 | } |
| 172 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 173 | } else if (v == mVoiceButton) { |
| 174 | mUiController.startVoiceSearch(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 175 | } else if (v == mForward) { |
| 176 | WebView web = mBaseUi.getWebView(); |
| 177 | if (web != null) { |
| 178 | web.goForward(); |
| 179 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 180 | } else { |
| 181 | super.onClick(v); |
| 182 | } |
| 183 | } |
| 184 | |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 185 | private void updateNavigationState() { |
| 186 | WebView web = mBaseUi.getWebView(); |
| 187 | if (web != null) { |
| 188 | mForward.setVisibility(web.canGoForward() ? View.VISIBLE : View.GONE); |
| 189 | } |
| 190 | } |
| 191 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 192 | } |