blob: 9446b29f18cc4adff55fca53781152edb4f21b30 [file] [log] [blame]
John Reckef654f12011-07-12 16:42:08 -07001/*
2 * Copyright (C) 2011 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 */
Bijan Amirzada41242f22014-03-21 12:12:18 -070016package com.android.browser;
John Reckef654f12011-07-12 16:42:08 -070017
18import android.content.Context;
19import android.graphics.Bitmap;
20import android.os.Handler;
21import android.os.Message;
22import android.text.TextUtils;
23import android.util.AttributeSet;
Pankaj Garg73d0e142014-12-02 13:38:38 -080024import android.util.TypedValue;
John Reckf26ff632011-07-29 10:56:07 -070025import android.view.MenuItem;
John Reck419f6b42011-08-16 16:10:51 -070026import android.view.View;
27import android.view.View.OnClickListener;
John Reckef654f12011-07-12 16:42:08 -070028import android.view.ViewConfiguration;
29import android.view.ViewPropertyAnimator;
30import android.widget.ImageView;
31import android.widget.LinearLayout;
John Reckf26ff632011-07-29 10:56:07 -070032import android.widget.PopupMenu.OnMenuItemClickListener;
John Reckef654f12011-07-12 16:42:08 -070033import android.widget.TextView;
34
Bijan Amirzada41242f22014-03-21 12:12:18 -070035import com.android.browser.R;
36import com.android.browser.UI.ComboViews;
Michael Kolb315d5022011-10-13 12:47:11 -070037
Pankaj Garg73d0e142014-12-02 13:38:38 -080038import org.codeaurora.swe.util.Activator;
39import org.codeaurora.swe.util.Observable;
40
John Reckef654f12011-07-12 16:42:08 -070041import java.text.DateFormat;
42import java.util.Date;
43
John Recke1a03a32011-09-14 17:04:16 -070044public class SnapshotBar extends LinearLayout implements OnClickListener {
John Reckef654f12011-07-12 16:42:08 -070045
46 private static final int MSG_SHOW_TITLE = 1;
47 private static final long DURATION_SHOW_DATE = BaseUi.HIDE_TITLEBAR_DELAY;
48
49 private ImageView mFavicon;
John Reckef654f12011-07-12 16:42:08 -070050 private TextView mDate;
51 private TextView mTitle;
52 private View mBookmarks;
53 private TitleBar mTitleBar;
54 private View mTabSwitcher;
Pankaj Garg73d0e142014-12-02 13:38:38 -080055 private TextView mTabText;
John Reckef654f12011-07-12 16:42:08 -070056 private View mOverflowMenu;
57 private View mToggleContainer;
58 private boolean mIsAnimating;
59 private ViewPropertyAnimator mTitleAnimator, mDateAnimator;
60 private float mAnimRadius = 20f;
Pankaj Garg73d0e142014-12-02 13:38:38 -080061 private float mTabSwitcherInitialTextSize = 0;
62 private float mTabSwitcherCompressedTextSize = 0;
John Reckef654f12011-07-12 16:42:08 -070063
64 public SnapshotBar(Context context) {
65 super(context);
66 }
67
68 public SnapshotBar(Context context, AttributeSet attrs) {
69 super(context, attrs);
70 }
71
72 public SnapshotBar(Context context, AttributeSet attrs, int defStyle) {
73 super(context, attrs, defStyle);
74 }
75
76 public void setTitleBar(TitleBar titleBar) {
77 mTitleBar = titleBar;
78 setFavicon(null);
Pankaj Garg73d0e142014-12-02 13:38:38 -080079 Activator.activate(
80 new Observable.Observer() {
81 @Override
82 public void onChange(Object... params) {
83 if ((Integer) params[0] > 9) {
84 mTabText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTabSwitcherCompressedTextSize);
85 } else {
86 mTabText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTabSwitcherInitialTextSize);
87 }
88
89 mTabText.setText(Integer.toString((Integer) params[0]));
90 }
91 },
92 mTitleBar.getUiController().getTabControl().getTabCountObservable());
John Reckef654f12011-07-12 16:42:08 -070093 }
94
95 private Handler mHandler = new Handler() {
96 @Override
97 public void handleMessage(Message msg) {
98 if (msg.what == MSG_SHOW_TITLE) {
99 mIsAnimating = false;
100 showTitle();
101 mTitleBar.getUi().showTitleBarForDuration();
102 }
103 }
104 };
105
106 @Override
107 protected void onFinishInflate() {
108 super.onFinishInflate();
John Recke1a03a32011-09-14 17:04:16 -0700109 mFavicon = (ImageView) findViewById(R.id.favicon);
John Reckef654f12011-07-12 16:42:08 -0700110 mDate = (TextView) findViewById(R.id.date);
111 mTitle = (TextView) findViewById(R.id.title);
112 mBookmarks = findViewById(R.id.all_btn);
113 mTabSwitcher = findViewById(R.id.tab_switcher);
Pankaj Garg73d0e142014-12-02 13:38:38 -0800114 mTabText = (TextView) findViewById(R.id.tab_switcher_text);
John Reckef654f12011-07-12 16:42:08 -0700115 mOverflowMenu = findViewById(R.id.more);
116 mToggleContainer = findViewById(R.id.toggle_container);
John Reckef654f12011-07-12 16:42:08 -0700117
118 if (mBookmarks != null) {
119 mBookmarks.setOnClickListener(this);
120 }
121 if (mTabSwitcher != null) {
122 mTabSwitcher.setOnClickListener(this);
123 }
124 if (mOverflowMenu != null) {
125 mOverflowMenu.setOnClickListener(this);
126 boolean showMenu = !ViewConfiguration.get(getContext())
127 .hasPermanentMenuKey();
128 mOverflowMenu.setVisibility(showMenu ? VISIBLE : GONE);
129 }
130 if (mToggleContainer != null) {
131 mToggleContainer.setOnClickListener(this);
132 resetAnimation();
133 }
Pankaj Garg73d0e142014-12-02 13:38:38 -0800134
135 if (mTabSwitcherInitialTextSize == 0) {
136 mTabSwitcherInitialTextSize = mTabText.getTextSize();
137 mTabSwitcherCompressedTextSize = (float) (mTabSwitcherInitialTextSize / 1.2);
138 }
John Reckef654f12011-07-12 16:42:08 -0700139 }
140
141 @Override
142 protected void onLayout(boolean changed, int l, int t, int r, int b) {
143 super.onLayout(changed, l, t, r, b);
144 if (mToggleContainer != null) {
145 mAnimRadius = mToggleContainer.getHeight() / 2f;
146 }
147 }
148
149 void resetAnimation() {
150 if (mToggleContainer == null) {
151 // No animation needed/used
152 return;
153 }
154 if (mTitleAnimator != null) {
155 mTitleAnimator.cancel();
156 mTitleAnimator = null;
157 }
158 if (mDateAnimator != null) {
159 mDateAnimator.cancel();
160 mDateAnimator = null;
161 }
162 mIsAnimating = false;
163 mHandler.removeMessages(MSG_SHOW_TITLE);
164 mTitle.setAlpha(1f);
165 mTitle.setTranslationY(0f);
166 mTitle.setRotationX(0f);
John Reck419f6b42011-08-16 16:10:51 -0700167 mDate.setAlpha(0f);
168 mDate.setTranslationY(-mAnimRadius);
169 mDate.setRotationX(90f);
John Reckef654f12011-07-12 16:42:08 -0700170 }
171
172 private void showDate() {
173 mTitleAnimator = mTitle.animate()
174 .alpha(0f)
175 .translationY(mAnimRadius)
176 .rotationX(-90f);
John Reck419f6b42011-08-16 16:10:51 -0700177 mDateAnimator = mDate.animate()
John Reckef654f12011-07-12 16:42:08 -0700178 .alpha(1f)
179 .translationY(0f)
180 .rotationX(0f);
181 }
182
183 private void showTitle() {
184 mTitleAnimator = mTitle.animate()
185 .alpha(1f)
186 .translationY(0f)
187 .rotationX(0f);
John Reck419f6b42011-08-16 16:10:51 -0700188 mDateAnimator = mDate.animate()
John Reckef654f12011-07-12 16:42:08 -0700189 .alpha(0f)
190 .translationY(-mAnimRadius)
191 .rotationX(90f);
192 }
193
194 @Override
195 public void onClick(View v) {
196 if (mBookmarks == v) {
Michael Kolb315d5022011-10-13 12:47:11 -0700197 mTitleBar.getUiController().bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reckef654f12011-07-12 16:42:08 -0700198 } else if (mTabSwitcher == v) {
John Recka3bc2502011-07-20 15:09:47 -0700199 ((PhoneUi) mTitleBar.getUi()).toggleNavScreen();
John Reckef654f12011-07-12 16:42:08 -0700200 } else if (mOverflowMenu == v) {
201 NavigationBarBase navBar = mTitleBar.getNavigationBar();
202 if (navBar instanceof NavigationBarPhone) {
203 ((NavigationBarPhone)navBar).showMenu(mOverflowMenu);
204 }
205 } else if (mToggleContainer == v && !mIsAnimating) {
206 mIsAnimating = true;
207 showDate();
208 mTitleBar.getUi().showTitleBar();
209 Message m = mHandler.obtainMessage(MSG_SHOW_TITLE);
210 mHandler.sendMessageDelayed(m, DURATION_SHOW_DATE);
211 }
212 }
213
John Reckef654f12011-07-12 16:42:08 -0700214 public void onTabDataChanged(Tab tab) {
215 if (!tab.isSnapshot()) return;
216 SnapshotTab snapshot = (SnapshotTab) tab;
217 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
218 mDate.setText(dateFormat.format(new Date(snapshot.getDateCreated())));
219 String title = snapshot.getTitle();
220 if (TextUtils.isEmpty(title)) {
221 title = UrlUtils.stripUrl(snapshot.getUrl());
222 }
223 mTitle.setText(title);
224 setFavicon(tab.getFavicon());
225 resetAnimation();
226 }
227
228 public void setFavicon(Bitmap icon) {
229 if (mFavicon == null) return;
230 mFavicon.setImageDrawable(mTitleBar.getUi().getFaviconDrawable(icon));
231 }
232
233 public boolean isAnimating() {
234 return mIsAnimating;
235 }
236
237}