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