blob: 039afcf17f0b7e722edd1fdb146ef5e5d6b35f90 [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.Menu;
25import 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;
33import android.widget.PopupMenu.OnMenuItemClickListener;
John Reckef654f12011-07-12 16:42:08 -070034import android.widget.TextView;
35
36import java.text.DateFormat;
37import java.util.Date;
38
John Reckf26ff632011-07-29 10:56:07 -070039public class SnapshotBar extends LinearLayout implements OnClickListener,
40 OnMenuItemClickListener {
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;
46 private View mGoLive;
47 private TextView mDate;
48 private TextView mTitle;
49 private View mBookmarks;
50 private TitleBar mTitleBar;
51 private View mTabSwitcher;
52 private View mOverflowMenu;
53 private View mToggleContainer;
54 private boolean mIsAnimating;
55 private ViewPropertyAnimator mTitleAnimator, mDateAnimator;
56 private float mAnimRadius = 20f;
John Reckef654f12011-07-12 16:42:08 -070057
58 public SnapshotBar(Context context) {
59 super(context);
60 }
61
62 public SnapshotBar(Context context, AttributeSet attrs) {
63 super(context, attrs);
64 }
65
66 public SnapshotBar(Context context, AttributeSet attrs, int defStyle) {
67 super(context, attrs, defStyle);
68 }
69
70 public void setTitleBar(TitleBar titleBar) {
71 mTitleBar = titleBar;
72 setFavicon(null);
73 }
74
75 private Handler mHandler = new Handler() {
76 @Override
77 public void handleMessage(Message msg) {
78 if (msg.what == MSG_SHOW_TITLE) {
79 mIsAnimating = false;
80 showTitle();
81 mTitleBar.getUi().showTitleBarForDuration();
82 }
83 }
84 };
85
86 @Override
87 protected void onFinishInflate() {
88 super.onFinishInflate();
89 mGoLive = mFavicon = (ImageView) findViewById(R.id.favicon);
90 if (mGoLive == null) {
91 mGoLive = findViewById(R.id.date_icon);
92 }
93 mDate = (TextView) findViewById(R.id.date);
94 mTitle = (TextView) findViewById(R.id.title);
95 mBookmarks = findViewById(R.id.all_btn);
96 mTabSwitcher = findViewById(R.id.tab_switcher);
97 mOverflowMenu = findViewById(R.id.more);
98 mToggleContainer = findViewById(R.id.toggle_container);
John Reckef654f12011-07-12 16:42:08 -070099
100 if (mBookmarks != null) {
101 mBookmarks.setOnClickListener(this);
102 }
103 if (mTabSwitcher != null) {
104 mTabSwitcher.setOnClickListener(this);
105 }
106 if (mOverflowMenu != null) {
107 mOverflowMenu.setOnClickListener(this);
108 boolean showMenu = !ViewConfiguration.get(getContext())
109 .hasPermanentMenuKey();
110 mOverflowMenu.setVisibility(showMenu ? VISIBLE : GONE);
111 }
112 if (mToggleContainer != null) {
113 mToggleContainer.setOnClickListener(this);
114 resetAnimation();
115 }
116 mGoLive.setOnClickListener(this);
117 }
118
119 @Override
120 protected void onLayout(boolean changed, int l, int t, int r, int b) {
121 super.onLayout(changed, l, t, r, b);
122 if (mToggleContainer != null) {
123 mAnimRadius = mToggleContainer.getHeight() / 2f;
124 }
125 }
126
127 void resetAnimation() {
128 if (mToggleContainer == null) {
129 // No animation needed/used
130 return;
131 }
132 if (mTitleAnimator != null) {
133 mTitleAnimator.cancel();
134 mTitleAnimator = null;
135 }
136 if (mDateAnimator != null) {
137 mDateAnimator.cancel();
138 mDateAnimator = null;
139 }
140 mIsAnimating = false;
141 mHandler.removeMessages(MSG_SHOW_TITLE);
142 mTitle.setAlpha(1f);
143 mTitle.setTranslationY(0f);
144 mTitle.setRotationX(0f);
John Reck419f6b42011-08-16 16:10:51 -0700145 mDate.setAlpha(0f);
146 mDate.setTranslationY(-mAnimRadius);
147 mDate.setRotationX(90f);
John Reckef654f12011-07-12 16:42:08 -0700148 }
149
150 private void showDate() {
151 mTitleAnimator = mTitle.animate()
152 .alpha(0f)
153 .translationY(mAnimRadius)
154 .rotationX(-90f);
John Reck419f6b42011-08-16 16:10:51 -0700155 mDateAnimator = mDate.animate()
John Reckef654f12011-07-12 16:42:08 -0700156 .alpha(1f)
157 .translationY(0f)
158 .rotationX(0f);
159 }
160
161 private void showTitle() {
162 mTitleAnimator = mTitle.animate()
163 .alpha(1f)
164 .translationY(0f)
165 .rotationX(0f);
John Reck419f6b42011-08-16 16:10:51 -0700166 mDateAnimator = mDate.animate()
John Reckef654f12011-07-12 16:42:08 -0700167 .alpha(0f)
168 .translationY(-mAnimRadius)
169 .rotationX(90f);
170 }
171
172 @Override
173 public void onClick(View v) {
174 if (mBookmarks == v) {
175 mTitleBar.getUiController().bookmarksOrHistoryPicker(false);
176 } else if (mGoLive == v) {
John Reckf26ff632011-07-29 10:56:07 -0700177 PopupMenu popup = new PopupMenu(mContext, mGoLive);
178 Menu menu = popup.getMenu();
179 popup.getMenuInflater().inflate(R.menu.snapshot_go_live, menu);
180 popup.setOnMenuItemClickListener(this);
181 popup.show();
John Reckef654f12011-07-12 16:42:08 -0700182 } else if (mTabSwitcher == v) {
John Recka3bc2502011-07-20 15:09:47 -0700183 ((PhoneUi) mTitleBar.getUi()).toggleNavScreen();
John Reckef654f12011-07-12 16:42:08 -0700184 } else if (mOverflowMenu == v) {
185 NavigationBarBase navBar = mTitleBar.getNavigationBar();
186 if (navBar instanceof NavigationBarPhone) {
187 ((NavigationBarPhone)navBar).showMenu(mOverflowMenu);
188 }
189 } else if (mToggleContainer == v && !mIsAnimating) {
190 mIsAnimating = true;
191 showDate();
192 mTitleBar.getUi().showTitleBar();
193 Message m = mHandler.obtainMessage(MSG_SHOW_TITLE);
194 mHandler.sendMessageDelayed(m, DURATION_SHOW_DATE);
195 }
196 }
197
John Reckf26ff632011-07-29 10:56:07 -0700198 @Override
199 public boolean onMenuItemClick(MenuItem item) {
200 switch (item.getItemId()) {
201 case R.id.snapshot_go_live:
202 goLive();
203 return true;
204 }
205 return false;
206 }
207
John Reckef654f12011-07-12 16:42:08 -0700208 private void goLive() {
209 Tab t = mTitleBar.getUi().getActiveTab();
210 t.loadUrl(t.getUrl(), null);
211 }
212
213 public void onTabDataChanged(Tab tab) {
214 if (!tab.isSnapshot()) return;
215 SnapshotTab snapshot = (SnapshotTab) tab;
216 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
217 mDate.setText(dateFormat.format(new Date(snapshot.getDateCreated())));
218 String title = snapshot.getTitle();
219 if (TextUtils.isEmpty(title)) {
220 title = UrlUtils.stripUrl(snapshot.getUrl());
221 }
222 mTitle.setText(title);
223 setFavicon(tab.getFavicon());
224 resetAnimation();
225 }
226
227 public void setFavicon(Bitmap icon) {
228 if (mFavicon == null) return;
229 mFavicon.setImageDrawable(mTitleBar.getUi().getFaviconDrawable(icon));
230 }
231
232 public boolean isAnimating() {
233 return mIsAnimating;
234 }
235
236}