blob: eeb300bb7944d837684c7b596a324a9e0b21e309 [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;
Vivek Sekhard4de6162015-07-21 15:01:45 -070047 private static final long DURATION_SHOW_DATE = 1500;
John Reckef654f12011-07-12 16:42:08 -070048
49 private ImageView mFavicon;
Ze G Riande2a675c22015-06-03 11:15:24 -070050 private ImageView mSnapshoticon;
51 private ImageView mReadericon;
John Reckef654f12011-07-12 16:42:08 -070052 private TextView mDate;
53 private TextView mTitle;
54 private View mBookmarks;
55 private TitleBar mTitleBar;
56 private View mTabSwitcher;
Pankaj Garg73d0e142014-12-02 13:38:38 -080057 private TextView mTabText;
John Reckef654f12011-07-12 16:42:08 -070058 private View mOverflowMenu;
59 private View mToggleContainer;
60 private boolean mIsAnimating;
61 private ViewPropertyAnimator mTitleAnimator, mDateAnimator;
62 private float mAnimRadius = 20f;
Pankaj Garg73d0e142014-12-02 13:38:38 -080063 private float mTabSwitcherInitialTextSize = 0;
64 private float mTabSwitcherCompressedTextSize = 0;
John Reckef654f12011-07-12 16:42:08 -070065
66 public SnapshotBar(Context context) {
67 super(context);
68 }
69
70 public SnapshotBar(Context context, AttributeSet attrs) {
71 super(context, attrs);
72 }
73
74 public SnapshotBar(Context context, AttributeSet attrs, int defStyle) {
75 super(context, attrs, defStyle);
76 }
77
78 public void setTitleBar(TitleBar titleBar) {
79 mTitleBar = titleBar;
80 setFavicon(null);
Pankaj Garg73d0e142014-12-02 13:38:38 -080081 Activator.activate(
82 new Observable.Observer() {
83 @Override
84 public void onChange(Object... params) {
Sagar Dhawan37765532015-07-31 18:46:35 -070085 if (mTabText == null)
86 return;
Pankaj Garg73d0e142014-12-02 13:38:38 -080087 if ((Integer) params[0] > 9) {
88 mTabText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTabSwitcherCompressedTextSize);
89 } else {
90 mTabText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTabSwitcherInitialTextSize);
91 }
92
93 mTabText.setText(Integer.toString((Integer) params[0]));
94 }
95 },
96 mTitleBar.getUiController().getTabControl().getTabCountObservable());
John Reckef654f12011-07-12 16:42:08 -070097 }
98
99 private Handler mHandler = new Handler() {
100 @Override
101 public void handleMessage(Message msg) {
102 if (msg.what == MSG_SHOW_TITLE) {
103 mIsAnimating = false;
104 showTitle();
John Reckef654f12011-07-12 16:42:08 -0700105 }
106 }
107 };
108
109 @Override
110 protected void onFinishInflate() {
111 super.onFinishInflate();
John Recke1a03a32011-09-14 17:04:16 -0700112 mFavicon = (ImageView) findViewById(R.id.favicon);
Ze G Riande2a675c22015-06-03 11:15:24 -0700113 mSnapshoticon = (ImageView) findViewById(R.id.snapshot_icon);
114 mReadericon = (ImageView) findViewById(R.id.reader_icon);
John Reckef654f12011-07-12 16:42:08 -0700115 mDate = (TextView) findViewById(R.id.date);
116 mTitle = (TextView) findViewById(R.id.title);
117 mBookmarks = findViewById(R.id.all_btn);
118 mTabSwitcher = findViewById(R.id.tab_switcher);
Pankaj Garg73d0e142014-12-02 13:38:38 -0800119 mTabText = (TextView) findViewById(R.id.tab_switcher_text);
John Reckef654f12011-07-12 16:42:08 -0700120 mOverflowMenu = findViewById(R.id.more);
121 mToggleContainer = findViewById(R.id.toggle_container);
John Reckef654f12011-07-12 16:42:08 -0700122
123 if (mBookmarks != null) {
124 mBookmarks.setOnClickListener(this);
125 }
126 if (mTabSwitcher != null) {
127 mTabSwitcher.setOnClickListener(this);
128 }
129 if (mOverflowMenu != null) {
130 mOverflowMenu.setOnClickListener(this);
Sagar Dhawan90236142015-07-24 14:16:38 -0700131 mOverflowMenu.setVisibility(VISIBLE);
John Reckef654f12011-07-12 16:42:08 -0700132 }
133 if (mToggleContainer != null) {
134 mToggleContainer.setOnClickListener(this);
135 resetAnimation();
136 }
Pankaj Garg73d0e142014-12-02 13:38:38 -0800137
Sagar Dhawan37765532015-07-31 18:46:35 -0700138 if (mTabSwitcherInitialTextSize == 0 && mTabText != null) {
Pankaj Garg73d0e142014-12-02 13:38:38 -0800139 mTabSwitcherInitialTextSize = mTabText.getTextSize();
140 mTabSwitcherCompressedTextSize = (float) (mTabSwitcherInitialTextSize / 1.2);
141 }
John Reckef654f12011-07-12 16:42:08 -0700142 }
143
144 @Override
145 protected void onLayout(boolean changed, int l, int t, int r, int b) {
146 super.onLayout(changed, l, t, r, b);
147 if (mToggleContainer != null) {
148 mAnimRadius = mToggleContainer.getHeight() / 2f;
149 }
150 }
151
152 void resetAnimation() {
153 if (mToggleContainer == null) {
154 // No animation needed/used
155 return;
156 }
157 if (mTitleAnimator != null) {
158 mTitleAnimator.cancel();
159 mTitleAnimator = null;
160 }
161 if (mDateAnimator != null) {
162 mDateAnimator.cancel();
163 mDateAnimator = null;
164 }
165 mIsAnimating = false;
166 mHandler.removeMessages(MSG_SHOW_TITLE);
167 mTitle.setAlpha(1f);
168 mTitle.setTranslationY(0f);
169 mTitle.setRotationX(0f);
John Reck419f6b42011-08-16 16:10:51 -0700170 mDate.setAlpha(0f);
171 mDate.setTranslationY(-mAnimRadius);
172 mDate.setRotationX(90f);
John Reckef654f12011-07-12 16:42:08 -0700173 }
174
175 private void showDate() {
176 mTitleAnimator = mTitle.animate()
177 .alpha(0f)
178 .translationY(mAnimRadius)
179 .rotationX(-90f);
John Reck419f6b42011-08-16 16:10:51 -0700180 mDateAnimator = mDate.animate()
John Reckef654f12011-07-12 16:42:08 -0700181 .alpha(1f)
182 .translationY(0f)
183 .rotationX(0f);
184 }
185
186 private void showTitle() {
187 mTitleAnimator = mTitle.animate()
188 .alpha(1f)
189 .translationY(0f)
190 .rotationX(0f);
John Reck419f6b42011-08-16 16:10:51 -0700191 mDateAnimator = mDate.animate()
John Reckef654f12011-07-12 16:42:08 -0700192 .alpha(0f)
193 .translationY(-mAnimRadius)
194 .rotationX(90f);
195 }
196
197 @Override
198 public void onClick(View v) {
199 if (mBookmarks == v) {
Michael Kolb315d5022011-10-13 12:47:11 -0700200 mTitleBar.getUiController().bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reckef654f12011-07-12 16:42:08 -0700201 } else if (mTabSwitcher == v) {
John Recka3bc2502011-07-20 15:09:47 -0700202 ((PhoneUi) mTitleBar.getUi()).toggleNavScreen();
John Reckef654f12011-07-12 16:42:08 -0700203 } else if (mOverflowMenu == v) {
204 NavigationBarBase navBar = mTitleBar.getNavigationBar();
205 if (navBar instanceof NavigationBarPhone) {
206 ((NavigationBarPhone)navBar).showMenu(mOverflowMenu);
207 }
Sagar Dhawan37765532015-07-31 18:46:35 -0700208 else if (navBar instanceof NavigationBarTablet) {
209 ((NavigationBarTablet)navBar).showMenu(mOverflowMenu);
210 }
John Reckef654f12011-07-12 16:42:08 -0700211 } else if (mToggleContainer == v && !mIsAnimating) {
212 mIsAnimating = true;
213 showDate();
Vivek Sekhard4de6162015-07-21 15:01:45 -0700214 mTitleBar.showTopControls(false);
John Reckef654f12011-07-12 16:42:08 -0700215 Message m = mHandler.obtainMessage(MSG_SHOW_TITLE);
216 mHandler.sendMessageDelayed(m, DURATION_SHOW_DATE);
217 }
218 }
219
John Reckef654f12011-07-12 16:42:08 -0700220 public void onTabDataChanged(Tab tab) {
221 if (!tab.isSnapshot()) return;
222 SnapshotTab snapshot = (SnapshotTab) tab;
223 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
224 mDate.setText(dateFormat.format(new Date(snapshot.getDateCreated())));
225 String title = snapshot.getTitle();
226 if (TextUtils.isEmpty(title)) {
227 title = UrlUtils.stripUrl(snapshot.getUrl());
228 }
229 mTitle.setText(title);
230 setFavicon(tab.getFavicon());
231 resetAnimation();
232 }
233
234 public void setFavicon(Bitmap icon) {
Enrico Rosd6efa972014-12-02 19:49:59 -0800235 if (mFavicon != null)
236 mFavicon.setImageDrawable(mTitleBar.getUi().getFaviconDrawable(icon));
John Reckef654f12011-07-12 16:42:08 -0700237 }
238
239 public boolean isAnimating() {
240 return mIsAnimating;
241 }
242
Ze G Riande2a675c22015-06-03 11:15:24 -0700243 public void setTitle(String title) {
244 mTitle.setText(title);
245 }
246
247 public void setDate(String date) {
248 mDate.setText(date);
249 }
250
251 public void setSnapshoticonVisibility(int visibility) {
252 if (mSnapshoticon.getVisibility() != visibility) {
253 mSnapshoticon.setVisibility(visibility);
254 }
255 }
256
257 public void setReadericonVisibility(int visibility) {
Sagar Dhawan37765532015-07-31 18:46:35 -0700258 if (mReadericon != null && mReadericon.getVisibility() != visibility) {
Ze G Riande2a675c22015-06-03 11:15:24 -0700259 mReadericon.setVisibility(visibility);
260 }
261 }
262
263 public void setFaviconVisibility(int visibility) {
Sagar Dhawan37765532015-07-31 18:46:35 -0700264 if (mFavicon != null && mFavicon.getVisibility() != visibility) {
Ze G Riande2a675c22015-06-03 11:15:24 -0700265 mFavicon.setVisibility(visibility);
266 }
267 }
268
John Reckef654f12011-07-12 16:42:08 -0700269}