blob: 42f9fba7f31368c40712512d29aaa26bf1fbba0a [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;
John Reckf26ff632011-07-29 10:56:07 -070024import android.view.MenuItem;
John Reck419f6b42011-08-16 16:10:51 -070025import android.view.View;
26import android.view.View.OnClickListener;
John Reckef654f12011-07-12 16:42:08 -070027import android.view.ViewConfiguration;
28import android.view.ViewPropertyAnimator;
29import android.widget.ImageView;
30import android.widget.LinearLayout;
John Reckf26ff632011-07-29 10:56:07 -070031import android.widget.PopupMenu.OnMenuItemClickListener;
John Reckef654f12011-07-12 16:42:08 -070032import android.widget.TextView;
33
Bijan Amirzada41242f22014-03-21 12:12:18 -070034import com.android.browser.R;
35import com.android.browser.UI.ComboViews;
Michael Kolb315d5022011-10-13 12:47:11 -070036
John Reckef654f12011-07-12 16:42:08 -070037import java.text.DateFormat;
38import java.util.Date;
39
John Recke1a03a32011-09-14 17:04:16 -070040public class SnapshotBar extends LinearLayout implements OnClickListener {
John Reckef654f12011-07-12 16:42:08 -070041
42 private static final int MSG_SHOW_TITLE = 1;
43 private static final long DURATION_SHOW_DATE = BaseUi.HIDE_TITLEBAR_DELAY;
44
45 private ImageView mFavicon;
John Reckef654f12011-07-12 16:42:08 -070046 private TextView mDate;
47 private TextView mTitle;
48 private View mBookmarks;
49 private TitleBar mTitleBar;
50 private View mTabSwitcher;
51 private View mOverflowMenu;
52 private View mToggleContainer;
53 private boolean mIsAnimating;
54 private ViewPropertyAnimator mTitleAnimator, mDateAnimator;
55 private float mAnimRadius = 20f;
John Reckef654f12011-07-12 16:42:08 -070056
57 public SnapshotBar(Context context) {
58 super(context);
59 }
60
61 public SnapshotBar(Context context, AttributeSet attrs) {
62 super(context, attrs);
63 }
64
65 public SnapshotBar(Context context, AttributeSet attrs, int defStyle) {
66 super(context, attrs, defStyle);
67 }
68
69 public void setTitleBar(TitleBar titleBar) {
70 mTitleBar = titleBar;
71 setFavicon(null);
72 }
73
74 private Handler mHandler = new Handler() {
75 @Override
76 public void handleMessage(Message msg) {
77 if (msg.what == MSG_SHOW_TITLE) {
78 mIsAnimating = false;
79 showTitle();
80 mTitleBar.getUi().showTitleBarForDuration();
81 }
82 }
83 };
84
85 @Override
86 protected void onFinishInflate() {
87 super.onFinishInflate();
John Recke1a03a32011-09-14 17:04:16 -070088 mFavicon = (ImageView) findViewById(R.id.favicon);
John Reckef654f12011-07-12 16:42:08 -070089 mDate = (TextView) findViewById(R.id.date);
90 mTitle = (TextView) findViewById(R.id.title);
91 mBookmarks = findViewById(R.id.all_btn);
92 mTabSwitcher = findViewById(R.id.tab_switcher);
93 mOverflowMenu = findViewById(R.id.more);
94 mToggleContainer = findViewById(R.id.toggle_container);
John Reckef654f12011-07-12 16:42:08 -070095
96 if (mBookmarks != null) {
97 mBookmarks.setOnClickListener(this);
98 }
99 if (mTabSwitcher != null) {
100 mTabSwitcher.setOnClickListener(this);
101 }
102 if (mOverflowMenu != null) {
103 mOverflowMenu.setOnClickListener(this);
104 boolean showMenu = !ViewConfiguration.get(getContext())
105 .hasPermanentMenuKey();
106 mOverflowMenu.setVisibility(showMenu ? VISIBLE : GONE);
107 }
108 if (mToggleContainer != null) {
109 mToggleContainer.setOnClickListener(this);
110 resetAnimation();
111 }
John Reckef654f12011-07-12 16:42:08 -0700112 }
113
114 @Override
115 protected void onLayout(boolean changed, int l, int t, int r, int b) {
116 super.onLayout(changed, l, t, r, b);
117 if (mToggleContainer != null) {
118 mAnimRadius = mToggleContainer.getHeight() / 2f;
119 }
120 }
121
122 void resetAnimation() {
123 if (mToggleContainer == null) {
124 // No animation needed/used
125 return;
126 }
127 if (mTitleAnimator != null) {
128 mTitleAnimator.cancel();
129 mTitleAnimator = null;
130 }
131 if (mDateAnimator != null) {
132 mDateAnimator.cancel();
133 mDateAnimator = null;
134 }
135 mIsAnimating = false;
136 mHandler.removeMessages(MSG_SHOW_TITLE);
137 mTitle.setAlpha(1f);
138 mTitle.setTranslationY(0f);
139 mTitle.setRotationX(0f);
John Reck419f6b42011-08-16 16:10:51 -0700140 mDate.setAlpha(0f);
141 mDate.setTranslationY(-mAnimRadius);
142 mDate.setRotationX(90f);
John Reckef654f12011-07-12 16:42:08 -0700143 }
144
145 private void showDate() {
146 mTitleAnimator = mTitle.animate()
147 .alpha(0f)
148 .translationY(mAnimRadius)
149 .rotationX(-90f);
John Reck419f6b42011-08-16 16:10:51 -0700150 mDateAnimator = mDate.animate()
John Reckef654f12011-07-12 16:42:08 -0700151 .alpha(1f)
152 .translationY(0f)
153 .rotationX(0f);
154 }
155
156 private void showTitle() {
157 mTitleAnimator = mTitle.animate()
158 .alpha(1f)
159 .translationY(0f)
160 .rotationX(0f);
John Reck419f6b42011-08-16 16:10:51 -0700161 mDateAnimator = mDate.animate()
John Reckef654f12011-07-12 16:42:08 -0700162 .alpha(0f)
163 .translationY(-mAnimRadius)
164 .rotationX(90f);
165 }
166
167 @Override
168 public void onClick(View v) {
169 if (mBookmarks == v) {
Michael Kolb315d5022011-10-13 12:47:11 -0700170 mTitleBar.getUiController().bookmarksOrHistoryPicker(ComboViews.Bookmarks);
John Reckef654f12011-07-12 16:42:08 -0700171 } else if (mTabSwitcher == v) {
John Recka3bc2502011-07-20 15:09:47 -0700172 ((PhoneUi) mTitleBar.getUi()).toggleNavScreen();
John Reckef654f12011-07-12 16:42:08 -0700173 } else if (mOverflowMenu == v) {
174 NavigationBarBase navBar = mTitleBar.getNavigationBar();
175 if (navBar instanceof NavigationBarPhone) {
176 ((NavigationBarPhone)navBar).showMenu(mOverflowMenu);
177 }
178 } else if (mToggleContainer == v && !mIsAnimating) {
179 mIsAnimating = true;
180 showDate();
181 mTitleBar.getUi().showTitleBar();
182 Message m = mHandler.obtainMessage(MSG_SHOW_TITLE);
183 mHandler.sendMessageDelayed(m, DURATION_SHOW_DATE);
184 }
185 }
186
John Reckef654f12011-07-12 16:42:08 -0700187 public void onTabDataChanged(Tab tab) {
188 if (!tab.isSnapshot()) return;
189 SnapshotTab snapshot = (SnapshotTab) tab;
190 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
191 mDate.setText(dateFormat.format(new Date(snapshot.getDateCreated())));
192 String title = snapshot.getTitle();
193 if (TextUtils.isEmpty(title)) {
194 title = UrlUtils.stripUrl(snapshot.getUrl());
195 }
196 mTitle.setText(title);
197 setFavicon(tab.getFavicon());
198 resetAnimation();
199 }
200
201 public void setFavicon(Bitmap icon) {
202 if (mFavicon == null) return;
203 mFavicon.setImageDrawable(mTitleBar.getUi().getFaviconDrawable(icon));
204 }
205
206 public boolean isAnimating() {
207 return mIsAnimating;
208 }
209
210}