Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 19 | import com.android.browser.UrlInputView.UrlInputListener; |
| 20 | |
| 21 | import android.app.SearchManager; |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 22 | import android.content.Context; |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 23 | import android.content.Intent; |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.Color; |
| 26 | import android.graphics.drawable.BitmapDrawable; |
| 27 | import android.graphics.drawable.Drawable; |
| 28 | import android.graphics.drawable.LayerDrawable; |
| 29 | import android.graphics.drawable.PaintDrawable; |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 30 | import android.os.Bundle; |
| 31 | import android.speech.RecognizerResultsIntent; |
Michael Kolb | 7cdc490 | 2011-02-03 17:54:40 -0800 | [diff] [blame] | 32 | import android.view.Gravity; |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 33 | import android.view.View; |
Michael Kolb | 7cdc490 | 2011-02-03 17:54:40 -0800 | [diff] [blame] | 34 | import android.widget.AbsoluteLayout; |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 35 | import android.widget.ImageView; |
| 36 | import android.widget.LinearLayout; |
| 37 | |
| 38 | /** |
| 39 | * Base class for a title bar used by the browser. |
| 40 | */ |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 41 | public class TitleBarBase extends LinearLayout implements UrlInputListener { |
John Reck | 94b7e04 | 2011-02-15 15:02:33 -0800 | [diff] [blame] | 42 | |
| 43 | protected static final int PROGRESS_MAX = 100; |
| 44 | |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 45 | // These need to be set by the subclass. |
| 46 | protected ImageView mFavicon; |
| 47 | protected ImageView mLockIcon; |
| 48 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 49 | protected Drawable mGenericFavicon; |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 50 | protected UiController mUiController; |
| 51 | protected BaseUi mBaseUi; |
| 52 | protected UrlInputView mUrlInput; |
| 53 | protected boolean mInVoiceMode; |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 54 | |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 55 | public TitleBarBase(Context context, UiController controller, BaseUi ui) { |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 56 | super(context, null); |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 57 | mUiController = controller; |
| 58 | mBaseUi = ui; |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 59 | mGenericFavicon = context.getResources().getDrawable( |
| 60 | R.drawable.app_web_browser_sm); |
| 61 | } |
| 62 | |
| 63 | /* package */ void setProgress(int newProgress) {} |
| 64 | /* package */ void setDisplayTitle(String title) {} |
| 65 | |
| 66 | /* package */ void setLock(Drawable d) { |
| 67 | assert mLockIcon != null; |
| 68 | if (null == d) { |
| 69 | mLockIcon.setVisibility(View.GONE); |
| 70 | } else { |
| 71 | mLockIcon.setImageDrawable(d); |
| 72 | mLockIcon.setVisibility(View.VISIBLE); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /* package */ void setFavicon(Bitmap icon) { |
| 77 | assert mFavicon != null; |
| 78 | Drawable[] array = new Drawable[3]; |
| 79 | array[0] = new PaintDrawable(Color.BLACK); |
| 80 | PaintDrawable p = new PaintDrawable(Color.WHITE); |
| 81 | array[1] = p; |
| 82 | if (icon == null) { |
| 83 | array[2] = mGenericFavicon; |
| 84 | } else { |
| 85 | array[2] = new BitmapDrawable(icon); |
| 86 | } |
| 87 | LayerDrawable d = new LayerDrawable(array); |
| 88 | d.setLayerInset(1, 1, 1, 1, 1); |
| 89 | d.setLayerInset(2, 2, 2, 2, 2); |
| 90 | mFavicon.setImageDrawable(d); |
| 91 | } |
| 92 | |
| 93 | /* package */ void setInVoiceMode(boolean inVoiceMode) {} |
| 94 | |
John Reck | 117f07d | 2011-01-24 09:39:03 -0800 | [diff] [blame] | 95 | /* package */ void setIncognitoMode(boolean incognito) {} |
Michael Kolb | 7cdc490 | 2011-02-03 17:54:40 -0800 | [diff] [blame] | 96 | |
| 97 | void setTitleGravity(int gravity) { |
| 98 | int newTop = 0; |
| 99 | if (gravity != Gravity.NO_GRAVITY) { |
| 100 | View parent = (View) getParent(); |
| 101 | if (parent != null) { |
| 102 | if (gravity == Gravity.TOP) { |
| 103 | newTop = parent.getScrollY(); |
| 104 | } else if (gravity == Gravity.BOTTOM) { |
| 105 | newTop = parent.getScrollY() + parent.getHeight() - getHeight(); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) getLayoutParams(); |
| 110 | if (lp != null) { |
| 111 | lp.y = newTop; |
| 112 | setLayoutParams(lp); |
| 113 | } |
| 114 | } |
| 115 | |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 116 | public int getEmbeddedHeight() { |
| 117 | return getHeight(); |
| 118 | } |
| 119 | |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 120 | // UrlInputListener implementation |
| 121 | |
| 122 | /** |
| 123 | * callback from suggestion dropdown |
| 124 | * user selected a suggestion |
| 125 | */ |
| 126 | @Override |
| 127 | public void onAction(String text, String extra, String source) { |
| 128 | mUiController.getCurrentTopWebView().requestFocus(); |
| 129 | mBaseUi.hideTitleBar(); |
| 130 | Intent i = new Intent(); |
| 131 | String action = null; |
| 132 | if (UrlInputView.VOICE.equals(source)) { |
| 133 | action = RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS; |
| 134 | source = null; |
| 135 | } else { |
| 136 | action = Intent.ACTION_SEARCH; |
| 137 | } |
| 138 | i.setAction(action); |
| 139 | i.putExtra(SearchManager.QUERY, text); |
| 140 | if (extra != null) { |
| 141 | i.putExtra(SearchManager.EXTRA_DATA_KEY, extra); |
| 142 | } |
| 143 | if (source != null) { |
| 144 | Bundle appData = new Bundle(); |
| 145 | appData.putString(com.android.common.Search.SOURCE, source); |
| 146 | i.putExtra(SearchManager.APP_DATA, appData); |
| 147 | } |
| 148 | mUiController.handleNewIntent(i); |
| 149 | setDisplayTitle(text); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void onDismiss() { |
| 154 | final Tab currentTab = mBaseUi.getActiveTab(); |
| 155 | mBaseUi.hideTitleBar(); |
| 156 | post(new Runnable() { |
| 157 | public void run() { |
| 158 | clearFocus(); |
| 159 | if ((currentTab != null) && !mInVoiceMode) { |
| 160 | setDisplayTitle(currentTab.getUrl()); |
| 161 | } |
| 162 | } |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * callback from the suggestion dropdown |
| 168 | * copy text to input field and stay in edit mode |
| 169 | */ |
| 170 | @Override |
| 171 | public void onCopySuggestion(String text) { |
| 172 | mUrlInput.setText(text, true); |
| 173 | if (text != null) { |
| 174 | mUrlInput.setSelection(text.length()); |
| 175 | } |
| 176 | } |
| 177 | |
John Reck | 94b7e04 | 2011-02-15 15:02:33 -0800 | [diff] [blame] | 178 | public void setCurrentUrlIsBookmark(boolean isBookmark) { |
| 179 | } |
| 180 | |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 181 | } |