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; |
Pankaj Garg | 73d0e14 | 2014-12-02 13:38:38 -0800 | [diff] [blame] | 24 | import android.util.TypedValue; |
John Reck | f26ff63 | 2011-07-29 10:56:07 -0700 | [diff] [blame] | 25 | import android.view.MenuItem; |
John Reck | 419f6b4 | 2011-08-16 16:10:51 -0700 | [diff] [blame] | 26 | import android.view.View; |
| 27 | import android.view.View.OnClickListener; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 28 | import android.view.ViewConfiguration; |
| 29 | import android.view.ViewPropertyAnimator; |
| 30 | import android.widget.ImageView; |
| 31 | import android.widget.LinearLayout; |
John Reck | f26ff63 | 2011-07-29 10:56:07 -0700 | [diff] [blame] | 32 | import android.widget.PopupMenu.OnMenuItemClickListener; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 33 | import android.widget.TextView; |
| 34 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 35 | import com.android.browser.R; |
| 36 | import com.android.browser.UI.ComboViews; |
Michael Kolb | 315d502 | 2011-10-13 12:47:11 -0700 | [diff] [blame] | 37 | |
Pankaj Garg | 73d0e14 | 2014-12-02 13:38:38 -0800 | [diff] [blame] | 38 | import org.codeaurora.swe.util.Activator; |
| 39 | import org.codeaurora.swe.util.Observable; |
| 40 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 41 | import java.text.DateFormat; |
| 42 | import java.util.Date; |
| 43 | |
John Reck | e1a03a3 | 2011-09-14 17:04:16 -0700 | [diff] [blame] | 44 | public class SnapshotBar extends LinearLayout implements OnClickListener { |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 45 | |
| 46 | private static final int MSG_SHOW_TITLE = 1; |
Vivek Sekhar | d4de616 | 2015-07-21 15:01:45 -0700 | [diff] [blame] | 47 | private static final long DURATION_SHOW_DATE = 1500; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 48 | |
| 49 | private ImageView mFavicon; |
Ze G Riande | 2a675c2 | 2015-06-03 11:15:24 -0700 | [diff] [blame] | 50 | private ImageView mSnapshoticon; |
| 51 | private ImageView mReadericon; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 52 | private TextView mDate; |
| 53 | private TextView mTitle; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 54 | private TitleBar mTitleBar; |
| 55 | private View mTabSwitcher; |
Pankaj Garg | 73d0e14 | 2014-12-02 13:38:38 -0800 | [diff] [blame] | 56 | private TextView mTabText; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 57 | private View mOverflowMenu; |
| 58 | private View mToggleContainer; |
| 59 | private boolean mIsAnimating; |
| 60 | private ViewPropertyAnimator mTitleAnimator, mDateAnimator; |
| 61 | private float mAnimRadius = 20f; |
Pankaj Garg | 73d0e14 | 2014-12-02 13:38:38 -0800 | [diff] [blame] | 62 | private float mTabSwitcherInitialTextSize = 0; |
| 63 | private float mTabSwitcherCompressedTextSize = 0; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 64 | |
| 65 | public SnapshotBar(Context context) { |
| 66 | super(context); |
| 67 | } |
| 68 | |
| 69 | public SnapshotBar(Context context, AttributeSet attrs) { |
| 70 | super(context, attrs); |
| 71 | } |
| 72 | |
| 73 | public SnapshotBar(Context context, AttributeSet attrs, int defStyle) { |
| 74 | super(context, attrs, defStyle); |
| 75 | } |
| 76 | |
| 77 | public void setTitleBar(TitleBar titleBar) { |
| 78 | mTitleBar = titleBar; |
| 79 | setFavicon(null); |
Pankaj Garg | 73d0e14 | 2014-12-02 13:38:38 -0800 | [diff] [blame] | 80 | Activator.activate( |
| 81 | new Observable.Observer() { |
| 82 | @Override |
| 83 | public void onChange(Object... params) { |
Sagar Dhawan | 3776553 | 2015-07-31 18:46:35 -0700 | [diff] [blame] | 84 | if (mTabText == null) |
| 85 | return; |
Pankaj Garg | 73d0e14 | 2014-12-02 13:38:38 -0800 | [diff] [blame] | 86 | if ((Integer) params[0] > 9) { |
| 87 | mTabText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTabSwitcherCompressedTextSize); |
| 88 | } else { |
| 89 | mTabText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTabSwitcherInitialTextSize); |
| 90 | } |
| 91 | |
| 92 | mTabText.setText(Integer.toString((Integer) params[0])); |
| 93 | } |
| 94 | }, |
| 95 | mTitleBar.getUiController().getTabControl().getTabCountObservable()); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | private Handler mHandler = new Handler() { |
| 99 | @Override |
| 100 | public void handleMessage(Message msg) { |
| 101 | if (msg.what == MSG_SHOW_TITLE) { |
| 102 | mIsAnimating = false; |
| 103 | showTitle(); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | @Override |
| 109 | protected void onFinishInflate() { |
| 110 | super.onFinishInflate(); |
John Reck | e1a03a3 | 2011-09-14 17:04:16 -0700 | [diff] [blame] | 111 | mFavicon = (ImageView) findViewById(R.id.favicon); |
Ze G Riande | 2a675c2 | 2015-06-03 11:15:24 -0700 | [diff] [blame] | 112 | mSnapshoticon = (ImageView) findViewById(R.id.snapshot_icon); |
| 113 | mReadericon = (ImageView) findViewById(R.id.reader_icon); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 114 | mDate = (TextView) findViewById(R.id.date); |
| 115 | mTitle = (TextView) findViewById(R.id.title); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 116 | mTabSwitcher = findViewById(R.id.tab_switcher); |
Pankaj Garg | 73d0e14 | 2014-12-02 13:38:38 -0800 | [diff] [blame] | 117 | mTabText = (TextView) findViewById(R.id.tab_switcher_text); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 118 | mOverflowMenu = findViewById(R.id.more); |
| 119 | mToggleContainer = findViewById(R.id.toggle_container); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 120 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 121 | if (mTabSwitcher != null) { |
| 122 | mTabSwitcher.setOnClickListener(this); |
| 123 | } |
| 124 | if (mOverflowMenu != null) { |
| 125 | mOverflowMenu.setOnClickListener(this); |
Sagar Dhawan | 9023614 | 2015-07-24 14:16:38 -0700 | [diff] [blame] | 126 | mOverflowMenu.setVisibility(VISIBLE); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 127 | } |
| 128 | if (mToggleContainer != null) { |
| 129 | mToggleContainer.setOnClickListener(this); |
| 130 | resetAnimation(); |
| 131 | } |
Pankaj Garg | 73d0e14 | 2014-12-02 13:38:38 -0800 | [diff] [blame] | 132 | |
Sagar Dhawan | 3776553 | 2015-07-31 18:46:35 -0700 | [diff] [blame] | 133 | if (mTabSwitcherInitialTextSize == 0 && mTabText != null) { |
Pankaj Garg | 73d0e14 | 2014-12-02 13:38:38 -0800 | [diff] [blame] | 134 | mTabSwitcherInitialTextSize = mTabText.getTextSize(); |
| 135 | mTabSwitcherCompressedTextSize = (float) (mTabSwitcherInitialTextSize / 1.2); |
| 136 | } |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
| 140 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 141 | super.onLayout(changed, l, t, r, b); |
| 142 | if (mToggleContainer != null) { |
| 143 | mAnimRadius = mToggleContainer.getHeight() / 2f; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void resetAnimation() { |
| 148 | if (mToggleContainer == null) { |
| 149 | // No animation needed/used |
| 150 | return; |
| 151 | } |
| 152 | if (mTitleAnimator != null) { |
| 153 | mTitleAnimator.cancel(); |
| 154 | mTitleAnimator = null; |
| 155 | } |
| 156 | if (mDateAnimator != null) { |
| 157 | mDateAnimator.cancel(); |
| 158 | mDateAnimator = null; |
| 159 | } |
| 160 | mIsAnimating = false; |
| 161 | mHandler.removeMessages(MSG_SHOW_TITLE); |
| 162 | mTitle.setAlpha(1f); |
| 163 | mTitle.setTranslationY(0f); |
| 164 | mTitle.setRotationX(0f); |
John Reck | 419f6b4 | 2011-08-16 16:10:51 -0700 | [diff] [blame] | 165 | mDate.setAlpha(0f); |
| 166 | mDate.setTranslationY(-mAnimRadius); |
| 167 | mDate.setRotationX(90f); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | private void showDate() { |
| 171 | mTitleAnimator = mTitle.animate() |
| 172 | .alpha(0f) |
| 173 | .translationY(mAnimRadius) |
| 174 | .rotationX(-90f); |
John Reck | 419f6b4 | 2011-08-16 16:10:51 -0700 | [diff] [blame] | 175 | mDateAnimator = mDate.animate() |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 176 | .alpha(1f) |
| 177 | .translationY(0f) |
| 178 | .rotationX(0f); |
| 179 | } |
| 180 | |
| 181 | private void showTitle() { |
| 182 | mTitleAnimator = mTitle.animate() |
| 183 | .alpha(1f) |
| 184 | .translationY(0f) |
| 185 | .rotationX(0f); |
John Reck | 419f6b4 | 2011-08-16 16:10:51 -0700 | [diff] [blame] | 186 | mDateAnimator = mDate.animate() |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 187 | .alpha(0f) |
| 188 | .translationY(-mAnimRadius) |
| 189 | .rotationX(90f); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void onClick(View v) { |
jrizzoli | 3f57be0 | 2016-01-24 21:06:30 +0100 | [diff] [blame] | 194 | if (mTabSwitcher == v) { |
John Reck | a3bc250 | 2011-07-20 15:09:47 -0700 | [diff] [blame] | 195 | ((PhoneUi) mTitleBar.getUi()).toggleNavScreen(); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 196 | } else if (mOverflowMenu == v) { |
| 197 | NavigationBarBase navBar = mTitleBar.getNavigationBar(); |
| 198 | if (navBar instanceof NavigationBarPhone) { |
| 199 | ((NavigationBarPhone)navBar).showMenu(mOverflowMenu); |
| 200 | } |
Sagar Dhawan | 3776553 | 2015-07-31 18:46:35 -0700 | [diff] [blame] | 201 | else if (navBar instanceof NavigationBarTablet) { |
| 202 | ((NavigationBarTablet)navBar).showMenu(mOverflowMenu); |
| 203 | } |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 204 | } else if (mToggleContainer == v && !mIsAnimating) { |
| 205 | mIsAnimating = true; |
| 206 | showDate(); |
Vivek Sekhar | d4de616 | 2015-07-21 15:01:45 -0700 | [diff] [blame] | 207 | mTitleBar.showTopControls(false); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 208 | Message m = mHandler.obtainMessage(MSG_SHOW_TITLE); |
| 209 | mHandler.sendMessageDelayed(m, DURATION_SHOW_DATE); |
| 210 | } |
| 211 | } |
| 212 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 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) { |
Enrico Ros | d6efa97 | 2014-12-02 19:49:59 -0800 | [diff] [blame] | 228 | if (mFavicon != null) |
| 229 | mFavicon.setImageDrawable(mTitleBar.getUi().getFaviconDrawable(icon)); |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | public boolean isAnimating() { |
| 233 | return mIsAnimating; |
| 234 | } |
| 235 | |
Ze G Riande | 2a675c2 | 2015-06-03 11:15:24 -0700 | [diff] [blame] | 236 | public void setTitle(String title) { |
| 237 | mTitle.setText(title); |
| 238 | } |
| 239 | |
| 240 | public void setDate(String date) { |
| 241 | mDate.setText(date); |
| 242 | } |
| 243 | |
| 244 | public void setSnapshoticonVisibility(int visibility) { |
| 245 | if (mSnapshoticon.getVisibility() != visibility) { |
| 246 | mSnapshoticon.setVisibility(visibility); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | public void setReadericonVisibility(int visibility) { |
Sagar Dhawan | 3776553 | 2015-07-31 18:46:35 -0700 | [diff] [blame] | 251 | if (mReadericon != null && mReadericon.getVisibility() != visibility) { |
Ze G Riande | 2a675c2 | 2015-06-03 11:15:24 -0700 | [diff] [blame] | 252 | mReadericon.setVisibility(visibility); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | public void setFaviconVisibility(int visibility) { |
Sagar Dhawan | 3776553 | 2015-07-31 18:46:35 -0700 | [diff] [blame] | 257 | if (mFavicon != null && mFavicon.getVisibility() != visibility) { |
Ze G Riande | 2a675c2 | 2015-06-03 11:15:24 -0700 | [diff] [blame] | 258 | mFavicon.setVisibility(visibility); |
| 259 | } |
| 260 | } |
| 261 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 262 | } |