blob: f62e0e420ac542a872a60ca5fa99a5f572c18794 [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 {
Leon Scroggins571b3762010-05-26 10:25:01 -040042 // These need to be set by the subclass.
43 protected ImageView mFavicon;
44 protected ImageView mLockIcon;
45
Michael Kolbfe251992010-07-08 15:41:55 -070046 protected Drawable mGenericFavicon;
John Reck92026732011-02-15 10:12:30 -080047 protected UiController mUiController;
48 protected BaseUi mBaseUi;
49 protected UrlInputView mUrlInput;
50 protected boolean mInVoiceMode;
Leon Scroggins571b3762010-05-26 10:25:01 -040051
John Reck92026732011-02-15 10:12:30 -080052 public TitleBarBase(Context context, UiController controller, BaseUi ui) {
Leon Scroggins571b3762010-05-26 10:25:01 -040053 super(context, null);
John Reck92026732011-02-15 10:12:30 -080054 mUiController = controller;
55 mBaseUi = ui;
Leon Scroggins571b3762010-05-26 10:25:01 -040056 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 Reck117f07d2011-01-24 09:39:03 -080092 /* package */ void setIncognitoMode(boolean incognito) {}
Michael Kolb7cdc4902011-02-03 17:54:40 -080093
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 Reck92026732011-02-15 10:12:30 -0800113 // 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 Scroggins571b3762010-05-26 10:25:01 -0400171}