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; |
| 29 | import android.widget.ImageView; |
| 30 | |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
| 34 | * This class represents a title bar for a particular "tab" or "window" in the |
| 35 | * browser. |
| 36 | */ |
| 37 | public class TitleBarPhone extends TitleBarBase implements OnFocusChangeListener, |
| 38 | OnClickListener, TextChangeWatcher { |
| 39 | |
| 40 | private Activity mActivity; |
| 41 | private StopProgressView mStopButton; |
| 42 | private ImageView mVoiceButton; |
| 43 | private boolean mInLoad; |
| 44 | private View mContainer; |
| 45 | private boolean mHasLockIcon; |
| 46 | |
| 47 | public TitleBarPhone(Activity activity, UiController controller, PhoneUi ui) { |
| 48 | super(activity, controller, ui); |
| 49 | mActivity = activity; |
| 50 | initLayout(activity, R.layout.title_bar); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected void initLayout(Context context, int layoutId) { |
| 55 | super.initLayout(context, layoutId); |
| 56 | mContainer = findViewById(R.id.taburlbar); |
| 57 | mLockIcon = (ImageView) findViewById(R.id.lock); |
| 58 | mFavicon = (ImageView) findViewById(R.id.favicon); |
| 59 | mStopButton = (StopProgressView) findViewById(R.id.stop); |
| 60 | mStopButton.setOnClickListener(this); |
| 61 | mVoiceButton = (ImageView) findViewById(R.id.voice); |
| 62 | mVoiceButton.setOnClickListener(this); |
| 63 | setFocusState(false); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public int getEmbeddedHeight() { |
| 68 | int height = mContainer.getHeight(); |
| 69 | return height; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void createContextMenu(ContextMenu menu) { |
| 74 | MenuInflater inflater = mActivity.getMenuInflater(); |
| 75 | inflater.inflate(R.menu.title_context, menu); |
| 76 | mActivity.onCreateContextMenu(menu, this, null); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void setInVoiceMode(boolean voicemode, List<String> voiceResults) { |
| 81 | super.setInVoiceMode(voicemode, voiceResults); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected void setSearchMode(boolean voiceSearchEnabled) { |
| 86 | boolean showvoicebutton = voiceSearchEnabled && |
| 87 | mUiController.supportsVoiceSearch(); |
| 88 | mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE : |
| 89 | View.GONE); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | protected void setFocusState(boolean focus) { |
| 94 | super.setFocusState(focus); |
| 95 | if (focus) { |
| 96 | mHasLockIcon = (mLockIcon.getVisibility() == View.VISIBLE); |
| 97 | mFavicon.setVisibility(View.GONE); |
| 98 | mLockIcon.setVisibility(View.GONE); |
| 99 | mStopButton.setVisibility(View.GONE); |
| 100 | mVoiceButton.setVisibility(View.VISIBLE); |
| 101 | } else { |
| 102 | mFavicon.setVisibility(View.VISIBLE); |
| 103 | mLockIcon.setVisibility(mHasLockIcon ? View.VISIBLE : View.GONE); |
| 104 | if (mInLoad) { |
| 105 | mStopButton.setVisibility(View.VISIBLE); |
| 106 | } else { |
| 107 | mStopButton.setVisibility(View.GONE); |
| 108 | } |
| 109 | mVoiceButton.setVisibility(View.GONE); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Update the progress, from 0 to 100. |
| 115 | */ |
| 116 | @Override |
| 117 | void setProgress(int newProgress) { |
| 118 | if (newProgress >= PROGRESS_MAX) { |
| 119 | mInLoad = false; |
| 120 | setFocusState(mUrlInput.hasFocus()); |
| 121 | } else { |
| 122 | if (!mInLoad) { |
| 123 | mInLoad = true; |
| 124 | setFocusState(mUrlInput.hasFocus()); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Update the text displayed in the title bar. |
| 131 | * @param title String to display. If null, the new tab string will be |
| 132 | * shown. |
| 133 | */ |
| 134 | @Override |
| 135 | void setDisplayTitle(String title) { |
| 136 | if (title == null) { |
| 137 | mUrlInput.setText(R.string.new_tab); |
| 138 | } else { |
| 139 | mUrlInput.setText(title); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void onFocusChange(View v, boolean hasFocus) { |
| 145 | if (v == mUrlInput) { |
| 146 | if (hasFocus) { |
| 147 | mActivity.closeOptionsMenu(); |
| 148 | } |
| 149 | } |
| 150 | super.onFocusChange(v, hasFocus); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void onClick(View v) { |
| 155 | if (v == mStopButton) { |
| 156 | mUiController.stopLoading(); |
| 157 | } else if (v == mVoiceButton) { |
| 158 | mUiController.startVoiceSearch(); |
| 159 | } else { |
| 160 | super.onClick(v); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | } |