John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 1 | /* |
| 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 Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame^] | 16 | package com.android.browser; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 17 | |
| 18 | import android.content.Context; |
| 19 | import android.graphics.Bitmap; |
| 20 | import android.os.Handler; |
| 21 | import android.os.Message; |
| 22 | import android.text.TextUtils; |
| 23 | import android.util.AttributeSet; |
John Reck | f26ff63 | 2011-07-29 10:56:07 -0700 | [diff] [blame] | 24 | import android.view.MenuItem; |
John Reck | 419f6b4 | 2011-08-16 16:10:51 -0700 | [diff] [blame] | 25 | import android.view.View; |
| 26 | import android.view.View.OnClickListener; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 27 | import android.view.ViewConfiguration; |
| 28 | import android.view.ViewPropertyAnimator; |
| 29 | import android.widget.ImageView; |
| 30 | import android.widget.LinearLayout; |
John Reck | f26ff63 | 2011-07-29 10:56:07 -0700 | [diff] [blame] | 31 | import android.widget.PopupMenu.OnMenuItemClickListener; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 32 | import android.widget.TextView; |
| 33 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame^] | 34 | import com.android.browser.R; |
| 35 | import com.android.browser.UI.ComboViews; |
Michael Kolb | 315d502 | 2011-10-13 12:47:11 -0700 | [diff] [blame] | 36 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 37 | import java.text.DateFormat; |
| 38 | import java.util.Date; |
| 39 | |
John Reck | e1a03a3 | 2011-09-14 17:04:16 -0700 | [diff] [blame] | 40 | public class SnapshotBar extends LinearLayout implements OnClickListener { |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 41 | |
| 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 Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 46 | 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 Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 56 | |
| 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 Reck | e1a03a3 | 2011-09-14 17:04:16 -0700 | [diff] [blame] | 88 | mFavicon = (ImageView) findViewById(R.id.favicon); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 89 | 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 Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 95 | |
| 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 Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 112 | } |
| 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 Reck | 419f6b4 | 2011-08-16 16:10:51 -0700 | [diff] [blame] | 140 | mDate.setAlpha(0f); |
| 141 | mDate.setTranslationY(-mAnimRadius); |
| 142 | mDate.setRotationX(90f); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private void showDate() { |
| 146 | mTitleAnimator = mTitle.animate() |
| 147 | .alpha(0f) |
| 148 | .translationY(mAnimRadius) |
| 149 | .rotationX(-90f); |
John Reck | 419f6b4 | 2011-08-16 16:10:51 -0700 | [diff] [blame] | 150 | mDateAnimator = mDate.animate() |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 151 | .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 Reck | 419f6b4 | 2011-08-16 16:10:51 -0700 | [diff] [blame] | 161 | mDateAnimator = mDate.animate() |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 162 | .alpha(0f) |
| 163 | .translationY(-mAnimRadius) |
| 164 | .rotationX(90f); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void onClick(View v) { |
| 169 | if (mBookmarks == v) { |
Michael Kolb | 315d502 | 2011-10-13 12:47:11 -0700 | [diff] [blame] | 170 | mTitleBar.getUiController().bookmarksOrHistoryPicker(ComboViews.Bookmarks); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 171 | } else if (mTabSwitcher == v) { |
John Reck | a3bc250 | 2011-07-20 15:09:47 -0700 | [diff] [blame] | 172 | ((PhoneUi) mTitleBar.getUi()).toggleNavScreen(); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 173 | } 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 Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 187 | 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 | } |