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