blob: 0d799a8b431fa7262263a7d98e9e8a9fc30b1418 [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
19import android.content.Context;
20import android.content.res.Resources;
21import android.graphics.drawable.Animatable;
22import android.graphics.drawable.Drawable;
23import android.util.DisplayMetrics;
24import android.util.TypedValue;
25import android.view.ContextMenu;
26import android.view.LayoutInflater;
27import android.view.MenuInflater;
28import android.view.View;
29import android.widget.ImageView;
30import android.widget.ProgressBar;
31import android.widget.TextView;
32
33import com.android.common.speech.LoggingEvents;
34
35/**
36 * This class represents a title bar for a particular "tab" or "window" in the
37 * browser.
38 */
39public class TitleBarXLarge extends TitleBarBase {
40 private Drawable mCircularProgress;
41 private ProgressBar mHorizontalProgress;
42 private Drawable mStopDrawable;
43 private Drawable mReloadDrawable;
44 private boolean mInLoad;
45 private BrowserActivity mBrowserActivity;
46
47 private final View mBackButton;
48 private final View mForwardButton;
49 private final View mStar;
50 private final View mMenu;
51 private final ImageView mStopButton;
52 private final TextView mTitle;
53 private final View mAllButton;
54
55 public TitleBarXLarge(BrowserActivity context) {
56 super(context);
57 Resources resources = context.getResources();
58 LayoutInflater factory = LayoutInflater.from(context);
59 factory.inflate(R.layout.title_bar_xlarge, this);
60 mBrowserActivity = context;
61
62 mTitle = (TextView) findViewById(R.id.title);
63 mTitle.setCompoundDrawablePadding(5);
64 mTitle.setLongClickable(true);
65
66 mLockIcon = (ImageView) findViewById(R.id.lock);
67 mFavicon = (ImageView) findViewById(R.id.favicon);
68 mStopButton = (ImageView) findViewById(R.id.stop);
69 mStopDrawable = mStopButton.getDrawable();
70 mReloadDrawable = resources.getDrawable(R.drawable.ic_reload);
71
72 mAllButton = (ImageView) findViewById(R.id.all_btn);
73 mCircularProgress = (Drawable) resources.getDrawable(
74 com.android.internal.R.drawable.search_spinner);
75 DisplayMetrics metrics = resources.getDisplayMetrics();
76 int iconDimension = (int) TypedValue.applyDimension(
77 TypedValue.COMPLEX_UNIT_DIP, 20f, metrics);
78 mCircularProgress.setBounds(0, 0, iconDimension, iconDimension);
79 mHorizontalProgress = (ProgressBar) findViewById(
80 R.id.progress_horizontal);
Leon Scroggins897ba062010-06-16 14:35:33 -040081 mHorizontalProgress.setProgressDrawable(
82 resources.getDrawable(R.drawable.progress));
Leon Scroggins571b3762010-05-26 10:25:01 -040083
84 // FIXME: Change enabled states based on whether you can go
85 // back/forward. Probably should be done inside onPageStarted.
86 mBackButton = findViewById(R.id.back);
87 mForwardButton = findViewById(R.id.forward);
88 mStar = findViewById(R.id.star);
89 mMenu = findViewById(R.id.menu);
90 View.OnClickListener listener = new View.OnClickListener() {
91 public void onClick(View v) {
92 if (mBackButton == v) {
93 mBrowserActivity.getTopWindow().goBack();
94 } else if (mForwardButton == v) {
95 mBrowserActivity.getTopWindow().goForward();
96 } else if (mStar == v) {
Ben Murdocheecb4e62010-07-06 16:30:38 +010097 mBrowserActivity.promptAddOrInstallBookmark();
Leon Scroggins571b3762010-05-26 10:25:01 -040098 } else if (mMenu == v) {
99 mBrowserActivity.openOptionsMenu();
100 } else if (mStopButton == v) {
101 if (mInLoad) {
102 mBrowserActivity.stopLoading();
103 } else {
104 mBrowserActivity.getTopWindow().reload();
105 }
106 } else if (mTitle == v) {
107 mBrowserActivity.editUrl();
108 } else if (mAllButton == v) {
109 // FIXME: Show the new bookmarks/windows view.
110 mBrowserActivity.bookmarksOrHistoryPicker(false);
111 }
112 }
113 };
114 mBackButton.setOnClickListener(listener);
115 mForwardButton.setOnClickListener(listener);
116 mStar.setOnClickListener(listener);
117 mStopButton.setOnClickListener(listener);
118 mTitle.setOnClickListener(listener);
119 mAllButton.setOnClickListener(listener);
120 mMenu.setOnClickListener(listener);
121 }
122
123 @Override
124 public void createContextMenu(ContextMenu menu) {
125 MenuInflater inflater = mBrowserActivity.getMenuInflater();
126 inflater.inflate(R.menu.title_context, menu);
127 mBrowserActivity.onCreateContextMenu(menu, this, null);
128 }
129
130 /**
131 * Update the progress, from 0 to 100.
132 */
133 /* package */ void setProgress(int newProgress) {
134 if (newProgress >= mHorizontalProgress.getMax()) {
135 mTitle.setCompoundDrawables(null, null, null, null);
136 ((Animatable) mCircularProgress).stop();
137 mHorizontalProgress.setVisibility(View.GONE);
138 mInLoad = false;
139 mStopButton.setImageDrawable(mReloadDrawable);
140 } else {
141 mHorizontalProgress.setProgress(newProgress);
142 if (!mInLoad && getWindowToken() != null) {
143 // checking the window token lets us be sure that we
144 // are attached to a window before starting the animation,
145 // preventing a potential race condition
146 // (fix for bug http://b/2115736)
147 mTitle.setCompoundDrawables(null, null, mCircularProgress,
148 null);
149 ((Animatable) mCircularProgress).start();
150 mHorizontalProgress.setVisibility(View.VISIBLE);
151 mInLoad = true;
152 mStopButton.setImageDrawable(mStopDrawable);
153 }
154 }
155 }
156
157 /**
158 * Update the text displayed in the title bar.
159 * @param title String to display. If null, the loading string will be
160 * shown.
161 */
162 /* package */ void setDisplayTitle(String title) {
163 if (title == null) {
164 mTitle.setText(R.string.title_bar_loading);
165 } else {
166 mTitle.setText(title);
167 }
168 }
169
170}