blob: 46136dbc95b86f9180faca68961633bd1e08b97e [file] [log] [blame]
Leon Scroggins571b3762010-05-26 10:25:01 -04001/*
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
17package com.android.browser;
18
John Reck92026732011-02-15 10:12:30 -080019import com.android.browser.UrlInputView.UrlInputListener;
20
21import android.app.SearchManager;
Leon Scroggins571b3762010-05-26 10:25:01 -040022import android.content.Context;
John Reck92026732011-02-15 10:12:30 -080023import android.content.Intent;
Leon Scroggins571b3762010-05-26 10:25:01 -040024import android.graphics.Bitmap;
25import android.graphics.Color;
26import android.graphics.drawable.BitmapDrawable;
27import android.graphics.drawable.Drawable;
28import android.graphics.drawable.LayerDrawable;
29import android.graphics.drawable.PaintDrawable;
John Reck92026732011-02-15 10:12:30 -080030import android.os.Bundle;
31import android.speech.RecognizerResultsIntent;
Michael Kolb7cdc4902011-02-03 17:54:40 -080032import android.view.Gravity;
Leon Scroggins571b3762010-05-26 10:25:01 -040033import android.view.View;
Michael Kolb7cdc4902011-02-03 17:54:40 -080034import android.widget.AbsoluteLayout;
Leon Scroggins571b3762010-05-26 10:25:01 -040035import android.widget.ImageView;
36import android.widget.LinearLayout;
37
38/**
39 * Base class for a title bar used by the browser.
40 */
John Reck92026732011-02-15 10:12:30 -080041public class TitleBarBase extends LinearLayout implements UrlInputListener {
John Reck94b7e042011-02-15 15:02:33 -080042
43 protected static final int PROGRESS_MAX = 100;
44
Leon Scroggins571b3762010-05-26 10:25:01 -040045 // These need to be set by the subclass.
46 protected ImageView mFavicon;
47 protected ImageView mLockIcon;
48
Michael Kolbfe251992010-07-08 15:41:55 -070049 protected Drawable mGenericFavicon;
John Reck92026732011-02-15 10:12:30 -080050 protected UiController mUiController;
51 protected BaseUi mBaseUi;
52 protected UrlInputView mUrlInput;
53 protected boolean mInVoiceMode;
Leon Scroggins571b3762010-05-26 10:25:01 -040054
John Reck92026732011-02-15 10:12:30 -080055 public TitleBarBase(Context context, UiController controller, BaseUi ui) {
Leon Scroggins571b3762010-05-26 10:25:01 -040056 super(context, null);
John Reck92026732011-02-15 10:12:30 -080057 mUiController = controller;
58 mBaseUi = ui;
Leon Scroggins571b3762010-05-26 10:25:01 -040059 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 Reck117f07d2011-01-24 09:39:03 -080095 /* package */ void setIncognitoMode(boolean incognito) {}
Michael Kolb7cdc4902011-02-03 17:54:40 -080096
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 Kolb29ccf8a2011-02-23 16:13:24 -0800116 public int getEmbeddedHeight() {
117 return getHeight();
118 }
119
John Reck92026732011-02-15 10:12:30 -0800120 // 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 Reck94b7e042011-02-15 15:02:33 -0800178 public void setCurrentUrlIsBookmark(boolean isBookmark) {
179 }
180
Leon Scroggins571b3762010-05-26 10:25:01 -0400181}