Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 1 | /* |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 2 | * Copyright (c) 2015, The Linux Foundation. All rights reserved. |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 3 | * Copyright (C) 2011 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 17 | |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 18 | package com.android.browser; |
| 19 | |
| 20 | import android.app.ActionBar; |
| 21 | import android.app.Activity; |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.net.Uri; |
| 25 | import android.os.Bundle; |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 26 | import android.support.v4.view.ViewPager; |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 27 | import android.util.AttributeSet; |
| 28 | import android.view.View; |
| 29 | import android.view.ViewGroup; |
| 30 | import android.view.animation.AnimationSet; |
| 31 | import android.view.animation.AnimationUtils; |
| 32 | import android.widget.HorizontalScrollView; |
| 33 | import android.widget.LinearLayout; |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 34 | |
| 35 | import com.android.browser.UI.ComboViews; |
| 36 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 37 | import java.util.Iterator; |
| 38 | import java.util.Set; |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 39 | |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame] | 40 | public class ComboView extends LinearLayout |
| 41 | implements CombinedBookmarksCallbacks, View.OnLayoutChangeListener { |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 42 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 43 | private Activity mActivity; |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 44 | private ViewPager mViewPager; |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 45 | private AnimationSet mInAnimation; |
| 46 | private AnimationSet mOutAnimation; |
| 47 | private int mActionBarContainerId; |
| 48 | |
| 49 | private ComboTabsAdapter mTabsAdapter; |
| 50 | private Bundle mExtraArgs; |
| 51 | |
| 52 | public ComboView(Context context) { |
| 53 | super(context); |
| 54 | } |
| 55 | |
| 56 | public ComboView(Context context, AttributeSet attrs) { |
| 57 | super(context, attrs); |
| 58 | } |
| 59 | |
| 60 | public ComboView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 61 | super(context, attrs, defStyleAttr); |
| 62 | } |
| 63 | |
| 64 | public void setupViews(Activity activity) { |
| 65 | mActivity = activity; |
| 66 | |
| 67 | this.setId(R.id.combo_view_container); |
| 68 | |
| 69 | mViewPager = (ViewPager)this.findViewById(R.id.combo_view_pager); |
| 70 | mViewPager.setId(R.id.tab_view); // ??? |
| 71 | |
| 72 | mInAnimation = (AnimationSet) AnimationUtils.loadAnimation(mActivity, R.anim.combo_view_enter); |
| 73 | mOutAnimation = (AnimationSet) AnimationUtils.loadAnimation(mActivity, R.anim.combo_view_exit); |
| 74 | |
| 75 | final ActionBar bar = activity.getActionBar(); |
| 76 | bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
| 77 | bar.setDisplayOptions(0); |
| 78 | |
| 79 | mActionBarContainerId = getResources().getIdentifier("action_bar_container", "id", "android"); |
| 80 | ViewGroup actionBarContainer = (ViewGroup) mActivity.getWindow().getDecorView().findViewById(mActionBarContainerId); |
| 81 | if (actionBarContainer != null) { |
| 82 | actionBarContainer.addOnLayoutChangeListener(this); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private View getScrollingTabContainerView() { |
| 87 | ViewGroup actionBarContainer = (ViewGroup) mActivity.getWindow().getDecorView().findViewById(mActionBarContainerId); |
| 88 | int count = actionBarContainer.getChildCount(); |
| 89 | for (int i = 0; i < count; i++) { |
| 90 | View child = actionBarContainer.getChildAt(i); |
| 91 | if (child instanceof HorizontalScrollView) { |
| 92 | return child; |
| 93 | } |
| 94 | } |
| 95 | return null; |
| 96 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 97 | |
| 98 | @Override |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 99 | public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 100 | if (mActivity != null) { |
| 101 | if (!isShowing()) { |
| 102 | View container = getScrollingTabContainerView(); |
| 103 | if (container != null) { |
| 104 | container.setVisibility(View.INVISIBLE); |
| 105 | } |
| 106 | } |
| 107 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 108 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | private boolean compareArgs(Bundle b1, Bundle b2) { |
| 112 | |
| 113 | if(b1.size() != b2.size()) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | Set<String> keys = b1.keySet(); |
| 118 | Iterator<String> it = keys.iterator(); |
| 119 | while (it.hasNext()) { |
| 120 | String key = it.next(); |
| 121 | final Object v1 = b1.get(key); |
| 122 | final Object v2 = b2.get(key); |
| 123 | if (!b2.containsKey(key)) { |
| 124 | return false; |
| 125 | } else if (v1 == null) { |
| 126 | if (v2 != null) { |
| 127 | return false; |
| 128 | } |
| 129 | } else if (!v1.equals(v2)) { |
| 130 | return false; |
| 131 | } |
| 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | public void showViews(Activity activity, Bundle extras /*Bundle savedInstanceState*/) { |
| 137 | Bundle args = extras.getBundle(ComboViewActivity.EXTRA_COMBO_ARGS); |
| 138 | String svStr = extras.getString(ComboViewActivity.EXTRA_INITIAL_VIEW, null); |
| 139 | ComboViews startingView = svStr != null ? ComboViews.valueOf(svStr) : ComboViews.Bookmarks; |
| 140 | |
| 141 | |
| 142 | // Compare the items in args with old args and recreate the fragments if they don't match. |
| 143 | if (mExtraArgs != null && !compareArgs(mExtraArgs, args)) { |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame] | 144 | mTabsAdapter.removeAllTabs(); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 145 | mTabsAdapter = null; |
| 146 | } |
| 147 | |
| 148 | final ActionBar bar = activity.getActionBar(); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 149 | bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 150 | if (BrowserActivity.isTablet(activity)) { |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 151 | bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME |
| 152 | | ActionBar.DISPLAY_USE_LOGO); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 153 | bar.setDisplayHomeAsUpEnabled(true); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 154 | } else { |
| 155 | bar.setDisplayOptions(0); |
| 156 | } |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 157 | if (mTabsAdapter == null) { |
| 158 | mExtraArgs = args; |
| 159 | mTabsAdapter = new ComboTabsAdapter(activity, mViewPager); |
Sagar Dhawan | 53b2ba7 | 2015-08-05 15:58:20 -0700 | [diff] [blame] | 160 | mTabsAdapter.addTab(bar.newTab().setText(R.string.bookmarks), |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 161 | BrowserBookmarksPage.class, args); |
Sagar Dhawan | 53b2ba7 | 2015-08-05 15:58:20 -0700 | [diff] [blame] | 162 | mTabsAdapter.addTab(bar.newTab().setText(R.string.history), |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 163 | BrowserHistoryPage.class, args); |
| 164 | mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_snapshots), |
| 165 | BrowserSnapshotPage.class, args); |
| 166 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 167 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 168 | /*if (savedInstanceState != null) { |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 169 | bar.setSelectedNavigationItem( |
| 170 | savedInstanceState.getInt(STATE_SELECTED_TAB, 0)); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 171 | } else*/ { |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 172 | switch (startingView) { |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 173 | case Bookmarks: |
| 174 | mViewPager.setCurrentItem(0); |
| 175 | break; |
| 176 | case History: |
| 177 | mViewPager.setCurrentItem(1); |
| 178 | break; |
| 179 | case Snapshots: |
| 180 | mViewPager.setCurrentItem(2); |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (!bar.isShowing()) { |
| 186 | View v = getScrollingTabContainerView(); |
| 187 | if (v != null) { |
| 188 | v.setVisibility(View.VISIBLE); |
| 189 | } |
| 190 | bar.show(); |
| 191 | } |
| 192 | |
| 193 | if (!this.isShowing()) { |
| 194 | this.setVisibility(View.VISIBLE); |
| 195 | this.requestFocus(); |
| 196 | if (!(BrowserActivity.isTablet(activity))) { |
| 197 | this.startAnimation(mInAnimation); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 202 | public boolean isShowing() { |
| 203 | return (this.getVisibility() == View.VISIBLE); |
| 204 | } |
| 205 | |
| 206 | public void hideViews() { |
Vivek Sekhar | 55db457 | 2015-12-02 10:20:03 -0800 | [diff] [blame] | 207 | if (isShowing()) { |
| 208 | if (!(BrowserActivity.isTablet(mActivity))) |
| 209 | this.startAnimation(mOutAnimation); |
| 210 | this.setVisibility(View.INVISIBLE); |
| 211 | mActionBarContainerId = getResources().getIdentifier("action_bar_container", "id", "android"); |
| 212 | ViewGroup actionBarContainer = (ViewGroup) mActivity.getWindow().getDecorView().findViewById(mActionBarContainerId); |
| 213 | if (actionBarContainer != null) { |
| 214 | actionBarContainer.removeOnLayoutChangeListener(this); |
| 215 | } |
| 216 | ActionBar actionBar = mActivity.getActionBar(); |
| 217 | actionBar.hide(); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 218 | } |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | //TODO: Save the selected tab on BrowserActivity's onSaveInstanceState |
| 222 | /*public void onSaveInstanceState(Bundle outState) { |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 223 | super.onSaveInstanceState(outState); |
| 224 | outState.putInt(STATE_SELECTED_TAB, |
| 225 | getActionBar().getSelectedNavigationIndex()); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 226 | }*/ |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 227 | |
| 228 | @Override |
| 229 | public void openUrl(String url) { |
| 230 | Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 231 | i.setClassName(getContext().getPackageName(), BrowserActivity.class.getName()); |
| 232 | i.putExtra(Controller.EXTRA_REQUEST_CODE, Controller.COMBO_VIEW); |
| 233 | i.putExtra(Controller.EXTRA_RESULT_CODE, Activity.RESULT_OK); |
| 234 | this.getContext().startActivity(i); |
| 235 | hideViews(); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void openInNewTab(String... urls) { |
| 240 | Intent i = new Intent(); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 241 | i.putExtra(ComboViewActivity.EXTRA_OPEN_ALL, urls); |
| 242 | i.putExtra(Controller.EXTRA_REQUEST_CODE, Controller.COMBO_VIEW); |
| 243 | i.putExtra(Controller.EXTRA_RESULT_CODE, Activity.RESULT_OK); |
| 244 | i.setClassName(getContext().getPackageName(), BrowserActivity.class.getName()); |
| 245 | this.getContext().startActivity(i); |
| 246 | hideViews(); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public void close() { |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 251 | hideViews(); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void openSnapshot(long id) { |
| 256 | Intent i = new Intent(); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 257 | i.putExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT, id); |
| 258 | i.putExtra(Controller.EXTRA_REQUEST_CODE, Controller.COMBO_VIEW); |
| 259 | i.putExtra(Controller.EXTRA_RESULT_CODE, Activity.RESULT_OK); |
| 260 | i.setClassName(getContext().getPackageName(), BrowserActivity.class.getName()); |
| 261 | this.getContext().startActivity(i); |
| 262 | hideViews(); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 263 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 264 | } |