blob: 2fb90d29cab26c6c86216e7167412a4df2340ac7 [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 */
16package com.android.browser;
17
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
34import java.text.DateFormat;
35import java.util.Date;
36
John Recke1a03a32011-09-14 17:04:16 -070037public class SnapshotBar extends LinearLayout implements OnClickListener {
John Reckef654f12011-07-12 16:42:08 -070038
39 private static final int MSG_SHOW_TITLE = 1;
40 private static final long DURATION_SHOW_DATE = BaseUi.HIDE_TITLEBAR_DELAY;
41
42 private ImageView mFavicon;
John Reckef654f12011-07-12 16:42:08 -070043 private TextView mDate;
44 private TextView mTitle;
45 private View mBookmarks;
46 private TitleBar mTitleBar;
47 private View mTabSwitcher;
48 private View mOverflowMenu;
49 private View mToggleContainer;
50 private boolean mIsAnimating;
51 private ViewPropertyAnimator mTitleAnimator, mDateAnimator;
52 private float mAnimRadius = 20f;
John Reckef654f12011-07-12 16:42:08 -070053
54 public SnapshotBar(Context context) {
55 super(context);
56 }
57
58 public SnapshotBar(Context context, AttributeSet attrs) {
59 super(context, attrs);
60 }
61
62 public SnapshotBar(Context context, AttributeSet attrs, int defStyle) {
63 super(context, attrs, defStyle);
64 }
65
66 public void setTitleBar(TitleBar titleBar) {
67 mTitleBar = titleBar;
68 setFavicon(null);
69 }
70
71 private Handler mHandler = new Handler() {
72 @Override
73 public void handleMessage(Message msg) {
74 if (msg.what == MSG_SHOW_TITLE) {
75 mIsAnimating = false;
76 showTitle();
77 mTitleBar.getUi().showTitleBarForDuration();
78 }
79 }
80 };
81
82 @Override
83 protected void onFinishInflate() {
84 super.onFinishInflate();
John Recke1a03a32011-09-14 17:04:16 -070085 mFavicon = (ImageView) findViewById(R.id.favicon);
John Reckef654f12011-07-12 16:42:08 -070086 mDate = (TextView) findViewById(R.id.date);
87 mTitle = (TextView) findViewById(R.id.title);
88 mBookmarks = findViewById(R.id.all_btn);
89 mTabSwitcher = findViewById(R.id.tab_switcher);
90 mOverflowMenu = findViewById(R.id.more);
91 mToggleContainer = findViewById(R.id.toggle_container);
John Reckef654f12011-07-12 16:42:08 -070092
93 if (mBookmarks != null) {
94 mBookmarks.setOnClickListener(this);
95 }
96 if (mTabSwitcher != null) {
97 mTabSwitcher.setOnClickListener(this);
98 }
99 if (mOverflowMenu != null) {
100 mOverflowMenu.setOnClickListener(this);
101 boolean showMenu = !ViewConfiguration.get(getContext())
102 .hasPermanentMenuKey();
103 mOverflowMenu.setVisibility(showMenu ? VISIBLE : GONE);
104 }
105 if (mToggleContainer != null) {
106 mToggleContainer.setOnClickListener(this);
107 resetAnimation();
108 }
John Reckef654f12011-07-12 16:42:08 -0700109 }
110
111 @Override
112 protected void onLayout(boolean changed, int l, int t, int r, int b) {
113 super.onLayout(changed, l, t, r, b);
114 if (mToggleContainer != null) {
115 mAnimRadius = mToggleContainer.getHeight() / 2f;
116 }
117 }
118
119 void resetAnimation() {
120 if (mToggleContainer == null) {
121 // No animation needed/used
122 return;
123 }
124 if (mTitleAnimator != null) {
125 mTitleAnimator.cancel();
126 mTitleAnimator = null;
127 }
128 if (mDateAnimator != null) {
129 mDateAnimator.cancel();
130 mDateAnimator = null;
131 }
132 mIsAnimating = false;
133 mHandler.removeMessages(MSG_SHOW_TITLE);
134 mTitle.setAlpha(1f);
135 mTitle.setTranslationY(0f);
136 mTitle.setRotationX(0f);
John Reck419f6b42011-08-16 16:10:51 -0700137 mDate.setAlpha(0f);
138 mDate.setTranslationY(-mAnimRadius);
139 mDate.setRotationX(90f);
John Reckef654f12011-07-12 16:42:08 -0700140 }
141
142 private void showDate() {
143 mTitleAnimator = mTitle.animate()
144 .alpha(0f)
145 .translationY(mAnimRadius)
146 .rotationX(-90f);
John Reck419f6b42011-08-16 16:10:51 -0700147 mDateAnimator = mDate.animate()
John Reckef654f12011-07-12 16:42:08 -0700148 .alpha(1f)
149 .translationY(0f)
150 .rotationX(0f);
151 }
152
153 private void showTitle() {
154 mTitleAnimator = mTitle.animate()
155 .alpha(1f)
156 .translationY(0f)
157 .rotationX(0f);
John Reck419f6b42011-08-16 16:10:51 -0700158 mDateAnimator = mDate.animate()
John Reckef654f12011-07-12 16:42:08 -0700159 .alpha(0f)
160 .translationY(-mAnimRadius)
161 .rotationX(90f);
162 }
163
164 @Override
165 public void onClick(View v) {
166 if (mBookmarks == v) {
167 mTitleBar.getUiController().bookmarksOrHistoryPicker(false);
John Reckef654f12011-07-12 16:42:08 -0700168 } else if (mTabSwitcher == v) {
John Recka3bc2502011-07-20 15:09:47 -0700169 ((PhoneUi) mTitleBar.getUi()).toggleNavScreen();
John Reckef654f12011-07-12 16:42:08 -0700170 } else if (mOverflowMenu == v) {
171 NavigationBarBase navBar = mTitleBar.getNavigationBar();
172 if (navBar instanceof NavigationBarPhone) {
173 ((NavigationBarPhone)navBar).showMenu(mOverflowMenu);
174 }
175 } else if (mToggleContainer == v && !mIsAnimating) {
176 mIsAnimating = true;
177 showDate();
178 mTitleBar.getUi().showTitleBar();
179 Message m = mHandler.obtainMessage(MSG_SHOW_TITLE);
180 mHandler.sendMessageDelayed(m, DURATION_SHOW_DATE);
181 }
182 }
183
John Reckef654f12011-07-12 16:42:08 -0700184 public void onTabDataChanged(Tab tab) {
185 if (!tab.isSnapshot()) return;
186 SnapshotTab snapshot = (SnapshotTab) tab;
187 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
188 mDate.setText(dateFormat.format(new Date(snapshot.getDateCreated())));
189 String title = snapshot.getTitle();
190 if (TextUtils.isEmpty(title)) {
191 title = UrlUtils.stripUrl(snapshot.getUrl());
192 }
193 mTitle.setText(title);
194 setFavicon(tab.getFavicon());
195 resetAnimation();
196 }
197
198 public void setFavicon(Bitmap icon) {
199 if (mFavicon == null) return;
200 mFavicon.setImageDrawable(mTitleBar.getUi().getFaviconDrawable(icon));
201 }
202
203 public boolean isAnimating() {
204 return mIsAnimating;
205 }
206
207}