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 | */ |
| 16 | package com.android.browser; |
| 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; |
| 24 | import android.view.View; |
| 25 | import android.view.View.OnClickListener; |
| 26 | import android.view.ViewConfiguration; |
| 27 | import android.view.ViewPropertyAnimator; |
| 28 | import android.widget.ImageView; |
| 29 | import android.widget.LinearLayout; |
| 30 | import android.widget.TextView; |
| 31 | |
| 32 | import java.text.DateFormat; |
| 33 | import java.util.Date; |
| 34 | |
| 35 | public class SnapshotBar extends LinearLayout implements OnClickListener { |
| 36 | |
| 37 | private static final int MSG_SHOW_TITLE = 1; |
| 38 | private static final long DURATION_SHOW_DATE = BaseUi.HIDE_TITLEBAR_DELAY; |
| 39 | |
| 40 | private ImageView mFavicon; |
| 41 | private View mGoLive; |
| 42 | private TextView mDate; |
| 43 | private TextView mTitle; |
| 44 | private View mBookmarks; |
| 45 | private TitleBar mTitleBar; |
| 46 | private View mTabSwitcher; |
| 47 | private View mOverflowMenu; |
| 48 | private View mToggleContainer; |
| 49 | private boolean mIsAnimating; |
| 50 | private ViewPropertyAnimator mTitleAnimator, mDateAnimator; |
| 51 | private float mAnimRadius = 20f; |
| 52 | private View mDateContainer; |
| 53 | |
| 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(); |
| 85 | mGoLive = mFavicon = (ImageView) findViewById(R.id.favicon); |
| 86 | if (mGoLive == null) { |
| 87 | mGoLive = findViewById(R.id.date_icon); |
| 88 | } |
| 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); |
| 95 | mDateContainer = findViewById(R.id.date_container); |
| 96 | |
| 97 | if (mBookmarks != null) { |
| 98 | mBookmarks.setOnClickListener(this); |
| 99 | } |
| 100 | if (mTabSwitcher != null) { |
| 101 | mTabSwitcher.setOnClickListener(this); |
| 102 | } |
| 103 | if (mOverflowMenu != null) { |
| 104 | mOverflowMenu.setOnClickListener(this); |
| 105 | boolean showMenu = !ViewConfiguration.get(getContext()) |
| 106 | .hasPermanentMenuKey(); |
| 107 | mOverflowMenu.setVisibility(showMenu ? VISIBLE : GONE); |
| 108 | } |
| 109 | if (mToggleContainer != null) { |
| 110 | mToggleContainer.setOnClickListener(this); |
| 111 | resetAnimation(); |
| 112 | } |
| 113 | mGoLive.setOnClickListener(this); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 118 | super.onLayout(changed, l, t, r, b); |
| 119 | if (mToggleContainer != null) { |
| 120 | mAnimRadius = mToggleContainer.getHeight() / 2f; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void resetAnimation() { |
| 125 | if (mToggleContainer == null) { |
| 126 | // No animation needed/used |
| 127 | return; |
| 128 | } |
| 129 | if (mTitleAnimator != null) { |
| 130 | mTitleAnimator.cancel(); |
| 131 | mTitleAnimator = null; |
| 132 | } |
| 133 | if (mDateAnimator != null) { |
| 134 | mDateAnimator.cancel(); |
| 135 | mDateAnimator = null; |
| 136 | } |
| 137 | mIsAnimating = false; |
| 138 | mHandler.removeMessages(MSG_SHOW_TITLE); |
| 139 | mTitle.setAlpha(1f); |
| 140 | mTitle.setTranslationY(0f); |
| 141 | mTitle.setRotationX(0f); |
| 142 | mDateContainer.setAlpha(0f); |
| 143 | mDateContainer.setTranslationY(-mAnimRadius); |
| 144 | mDateContainer.setRotationX(90f); |
| 145 | } |
| 146 | |
| 147 | private void showDate() { |
| 148 | mTitleAnimator = mTitle.animate() |
| 149 | .alpha(0f) |
| 150 | .translationY(mAnimRadius) |
| 151 | .rotationX(-90f); |
| 152 | mDateAnimator = mDateContainer.animate() |
| 153 | .alpha(1f) |
| 154 | .translationY(0f) |
| 155 | .rotationX(0f); |
| 156 | } |
| 157 | |
| 158 | private void showTitle() { |
| 159 | mTitleAnimator = mTitle.animate() |
| 160 | .alpha(1f) |
| 161 | .translationY(0f) |
| 162 | .rotationX(0f); |
| 163 | mDateAnimator = mDateContainer.animate() |
| 164 | .alpha(0f) |
| 165 | .translationY(-mAnimRadius) |
| 166 | .rotationX(90f); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void onClick(View v) { |
| 171 | if (mBookmarks == v) { |
| 172 | mTitleBar.getUiController().bookmarksOrHistoryPicker(false); |
| 173 | } else if (mGoLive == v) { |
| 174 | goLive(); |
| 175 | } else if (mTabSwitcher == v) { |
| 176 | mTitleBar.getUi().onMenuKey(); |
| 177 | } else if (mOverflowMenu == v) { |
| 178 | NavigationBarBase navBar = mTitleBar.getNavigationBar(); |
| 179 | if (navBar instanceof NavigationBarPhone) { |
| 180 | ((NavigationBarPhone)navBar).showMenu(mOverflowMenu); |
| 181 | } |
| 182 | } else if (mToggleContainer == v && !mIsAnimating) { |
| 183 | mIsAnimating = true; |
| 184 | showDate(); |
| 185 | mTitleBar.getUi().showTitleBar(); |
| 186 | Message m = mHandler.obtainMessage(MSG_SHOW_TITLE); |
| 187 | mHandler.sendMessageDelayed(m, DURATION_SHOW_DATE); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | private void goLive() { |
| 192 | Tab t = mTitleBar.getUi().getActiveTab(); |
| 193 | t.loadUrl(t.getUrl(), null); |
| 194 | } |
| 195 | |
| 196 | public void onTabDataChanged(Tab tab) { |
| 197 | if (!tab.isSnapshot()) return; |
| 198 | SnapshotTab snapshot = (SnapshotTab) tab; |
| 199 | DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG); |
| 200 | mDate.setText(dateFormat.format(new Date(snapshot.getDateCreated()))); |
| 201 | String title = snapshot.getTitle(); |
| 202 | if (TextUtils.isEmpty(title)) { |
| 203 | title = UrlUtils.stripUrl(snapshot.getUrl()); |
| 204 | } |
| 205 | mTitle.setText(title); |
| 206 | setFavicon(tab.getFavicon()); |
| 207 | resetAnimation(); |
| 208 | } |
| 209 | |
| 210 | public void setFavicon(Bitmap icon) { |
| 211 | if (mFavicon == null) return; |
| 212 | mFavicon.setImageDrawable(mTitleBar.getUi().getFaviconDrawable(icon)); |
| 213 | } |
| 214 | |
| 215 | public boolean isAnimating() { |
| 216 | return mIsAnimating; |
| 217 | } |
| 218 | |
| 219 | } |