blob: 601d35f1661d0df9a88e54d0da3a1dee09f324ce [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;
Michael Kolb7c6cbc22011-04-07 16:40:34 -070099 int newLeft = 0;
100 View parent = (View) getParent();
101 if (parent != null) {
102 newLeft = parent.getScrollX();
103 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800104 if (gravity != Gravity.NO_GRAVITY) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800105 if (parent != null) {
106 if (gravity == Gravity.TOP) {
107 newTop = parent.getScrollY();
108 } else if (gravity == Gravity.BOTTOM) {
109 newTop = parent.getScrollY() + parent.getHeight() - getHeight();
110 }
111 }
112 }
113 AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) getLayoutParams();
114 if (lp != null) {
Michael Kolb7c6cbc22011-04-07 16:40:34 -0700115 lp.x = newLeft;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800116 lp.y = newTop;
117 setLayoutParams(lp);
118 }
119 }
120
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800121 public int getEmbeddedHeight() {
122 return getHeight();
123 }
124
John Reck92026732011-02-15 10:12:30 -0800125 // UrlInputListener implementation
126
127 /**
128 * callback from suggestion dropdown
129 * user selected a suggestion
130 */
131 @Override
132 public void onAction(String text, String extra, String source) {
133 mUiController.getCurrentTopWebView().requestFocus();
134 mBaseUi.hideTitleBar();
135 Intent i = new Intent();
136 String action = null;
137 if (UrlInputView.VOICE.equals(source)) {
138 action = RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS;
139 source = null;
140 } else {
141 action = Intent.ACTION_SEARCH;
142 }
143 i.setAction(action);
144 i.putExtra(SearchManager.QUERY, text);
145 if (extra != null) {
146 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
147 }
148 if (source != null) {
149 Bundle appData = new Bundle();
150 appData.putString(com.android.common.Search.SOURCE, source);
151 i.putExtra(SearchManager.APP_DATA, appData);
152 }
153 mUiController.handleNewIntent(i);
154 setDisplayTitle(text);
155 }
156
157 @Override
158 public void onDismiss() {
159 final Tab currentTab = mBaseUi.getActiveTab();
160 mBaseUi.hideTitleBar();
161 post(new Runnable() {
162 public void run() {
163 clearFocus();
164 if ((currentTab != null) && !mInVoiceMode) {
165 setDisplayTitle(currentTab.getUrl());
166 }
167 }
168 });
169 }
170
171 /**
172 * callback from the suggestion dropdown
173 * copy text to input field and stay in edit mode
174 */
175 @Override
176 public void onCopySuggestion(String text) {
177 mUrlInput.setText(text, true);
178 if (text != null) {
179 mUrlInput.setSelection(text.length());
180 }
181 }
182
John Reck94b7e042011-02-15 15:02:33 -0800183 public void setCurrentUrlIsBookmark(boolean isBookmark) {
184 }
185
Leon Scroggins571b3762010-05-26 10:25:01 -0400186}