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 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 40 | public class ComboView extends LinearLayout implements CombinedBookmarksCallbacks, View.OnLayoutChangeListener { |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 41 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 42 | private Activity mActivity; |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 43 | private ViewPager mViewPager; |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 44 | private AnimationSet mInAnimation; |
| 45 | private AnimationSet mOutAnimation; |
| 46 | private int mActionBarContainerId; |
| 47 | |
| 48 | private ComboTabsAdapter mTabsAdapter; |
| 49 | private Bundle mExtraArgs; |
| 50 | |
| 51 | public ComboView(Context context) { |
| 52 | super(context); |
| 53 | } |
| 54 | |
| 55 | public ComboView(Context context, AttributeSet attrs) { |
| 56 | super(context, attrs); |
| 57 | } |
| 58 | |
| 59 | public ComboView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 60 | super(context, attrs, defStyleAttr); |
| 61 | } |
| 62 | |
| 63 | public void setupViews(Activity activity) { |
| 64 | mActivity = activity; |
| 65 | |
| 66 | this.setId(R.id.combo_view_container); |
| 67 | |
| 68 | mViewPager = (ViewPager)this.findViewById(R.id.combo_view_pager); |
| 69 | mViewPager.setId(R.id.tab_view); // ??? |
| 70 | |
| 71 | mInAnimation = (AnimationSet) AnimationUtils.loadAnimation(mActivity, R.anim.combo_view_enter); |
| 72 | mOutAnimation = (AnimationSet) AnimationUtils.loadAnimation(mActivity, R.anim.combo_view_exit); |
| 73 | |
| 74 | final ActionBar bar = activity.getActionBar(); |
| 75 | bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
| 76 | bar.setDisplayOptions(0); |
| 77 | |
| 78 | mActionBarContainerId = getResources().getIdentifier("action_bar_container", "id", "android"); |
| 79 | ViewGroup actionBarContainer = (ViewGroup) mActivity.getWindow().getDecorView().findViewById(mActionBarContainerId); |
| 80 | if (actionBarContainer != null) { |
| 81 | actionBarContainer.addOnLayoutChangeListener(this); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private View getScrollingTabContainerView() { |
| 86 | ViewGroup actionBarContainer = (ViewGroup) mActivity.getWindow().getDecorView().findViewById(mActionBarContainerId); |
| 87 | int count = actionBarContainer.getChildCount(); |
| 88 | for (int i = 0; i < count; i++) { |
| 89 | View child = actionBarContainer.getChildAt(i); |
| 90 | if (child instanceof HorizontalScrollView) { |
| 91 | return child; |
| 92 | } |
| 93 | } |
| 94 | return null; |
| 95 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 96 | |
| 97 | @Override |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 98 | public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 99 | if (mActivity != null) { |
| 100 | if (!isShowing()) { |
| 101 | View container = getScrollingTabContainerView(); |
| 102 | if (container != null) { |
| 103 | container.setVisibility(View.INVISIBLE); |
| 104 | } |
| 105 | } |
| 106 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 107 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private boolean compareArgs(Bundle b1, Bundle b2) { |
| 111 | |
| 112 | if(b1.size() != b2.size()) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | Set<String> keys = b1.keySet(); |
| 117 | Iterator<String> it = keys.iterator(); |
| 118 | while (it.hasNext()) { |
| 119 | String key = it.next(); |
| 120 | final Object v1 = b1.get(key); |
| 121 | final Object v2 = b2.get(key); |
| 122 | if (!b2.containsKey(key)) { |
| 123 | return false; |
| 124 | } else if (v1 == null) { |
| 125 | if (v2 != null) { |
| 126 | return false; |
| 127 | } |
| 128 | } else if (!v1.equals(v2)) { |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | public void showViews(Activity activity, Bundle extras /*Bundle savedInstanceState*/) { |
| 136 | Bundle args = extras.getBundle(ComboViewActivity.EXTRA_COMBO_ARGS); |
| 137 | String svStr = extras.getString(ComboViewActivity.EXTRA_INITIAL_VIEW, null); |
| 138 | ComboViews startingView = svStr != null ? ComboViews.valueOf(svStr) : ComboViews.Bookmarks; |
| 139 | |
| 140 | |
| 141 | // Compare the items in args with old args and recreate the fragments if they don't match. |
| 142 | if (mExtraArgs != null && !compareArgs(mExtraArgs, args)) { |
| 143 | mTabsAdapter = null; |
| 144 | } |
| 145 | |
| 146 | final ActionBar bar = activity.getActionBar(); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 147 | bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 148 | if (BrowserActivity.isTablet(activity)) { |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 149 | bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME |
| 150 | | ActionBar.DISPLAY_USE_LOGO); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 151 | bar.setDisplayHomeAsUpEnabled(true); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 152 | } else { |
| 153 | bar.setDisplayOptions(0); |
| 154 | } |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 155 | if (mTabsAdapter == null) { |
| 156 | mExtraArgs = args; |
| 157 | mTabsAdapter = new ComboTabsAdapter(activity, mViewPager); |
| 158 | mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_bookmarks), |
| 159 | BrowserBookmarksPage.class, args); |
| 160 | mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_history), |
| 161 | BrowserHistoryPage.class, args); |
| 162 | mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_snapshots), |
| 163 | BrowserSnapshotPage.class, args); |
| 164 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 165 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 166 | /*if (savedInstanceState != null) { |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 167 | bar.setSelectedNavigationItem( |
| 168 | savedInstanceState.getInt(STATE_SELECTED_TAB, 0)); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 169 | } else*/ { |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 170 | switch (startingView) { |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 171 | case Bookmarks: |
| 172 | mViewPager.setCurrentItem(0); |
| 173 | break; |
| 174 | case History: |
| 175 | mViewPager.setCurrentItem(1); |
| 176 | break; |
| 177 | case Snapshots: |
| 178 | mViewPager.setCurrentItem(2); |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (!bar.isShowing()) { |
| 184 | View v = getScrollingTabContainerView(); |
| 185 | if (v != null) { |
| 186 | v.setVisibility(View.VISIBLE); |
| 187 | } |
| 188 | bar.show(); |
| 189 | } |
| 190 | |
| 191 | if (!this.isShowing()) { |
| 192 | this.setVisibility(View.VISIBLE); |
| 193 | this.requestFocus(); |
| 194 | if (!(BrowserActivity.isTablet(activity))) { |
| 195 | this.startAnimation(mInAnimation); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 200 | public boolean isShowing() { |
| 201 | return (this.getVisibility() == View.VISIBLE); |
| 202 | } |
| 203 | |
| 204 | public void hideViews() { |
| 205 | if(!(BrowserActivity.isTablet(mActivity))) |
| 206 | this.startAnimation(mOutAnimation); |
| 207 | this.setVisibility(View.INVISIBLE); |
| 208 | mActionBarContainerId = getResources().getIdentifier("action_bar_container", "id", "android"); |
| 209 | ViewGroup actionBarContainer = (ViewGroup) mActivity.getWindow().getDecorView().findViewById(mActionBarContainerId); |
| 210 | if (actionBarContainer != null) { |
| 211 | actionBarContainer.removeOnLayoutChangeListener(this); |
| 212 | } |
| 213 | ActionBar actionBar = mActivity.getActionBar(); |
| 214 | actionBar.hide(); |
| 215 | } |
| 216 | |
| 217 | //TODO: Save the selected tab on BrowserActivity's onSaveInstanceState |
| 218 | /*public void onSaveInstanceState(Bundle outState) { |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 219 | super.onSaveInstanceState(outState); |
| 220 | outState.putInt(STATE_SELECTED_TAB, |
| 221 | getActionBar().getSelectedNavigationIndex()); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 222 | }*/ |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 223 | |
| 224 | @Override |
| 225 | public void openUrl(String url) { |
| 226 | Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 227 | i.setClassName(getContext().getPackageName(), BrowserActivity.class.getName()); |
| 228 | i.putExtra(Controller.EXTRA_REQUEST_CODE, Controller.COMBO_VIEW); |
| 229 | i.putExtra(Controller.EXTRA_RESULT_CODE, Activity.RESULT_OK); |
| 230 | this.getContext().startActivity(i); |
| 231 | hideViews(); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public void openInNewTab(String... urls) { |
| 236 | Intent i = new Intent(); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 237 | i.putExtra(ComboViewActivity.EXTRA_OPEN_ALL, urls); |
| 238 | i.putExtra(Controller.EXTRA_REQUEST_CODE, Controller.COMBO_VIEW); |
| 239 | i.putExtra(Controller.EXTRA_RESULT_CODE, Activity.RESULT_OK); |
| 240 | i.setClassName(getContext().getPackageName(), BrowserActivity.class.getName()); |
| 241 | this.getContext().startActivity(i); |
| 242 | hideViews(); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public void close() { |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 247 | hideViews(); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public void openSnapshot(long id) { |
| 252 | Intent i = new Intent(); |
Tarun Nainani | 87a8668 | 2015-02-05 11:47:35 -0800 | [diff] [blame] | 253 | i.putExtra(ComboViewActivity.EXTRA_OPEN_SNAPSHOT, id); |
| 254 | i.putExtra(Controller.EXTRA_REQUEST_CODE, Controller.COMBO_VIEW); |
| 255 | i.putExtra(Controller.EXTRA_RESULT_CODE, Activity.RESULT_OK); |
| 256 | i.setClassName(getContext().getPackageName(), BrowserActivity.class.getName()); |
| 257 | this.getContext().startActivity(i); |
| 258 | hideViews(); |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 259 | } |
Tarun Nainani | 8dc931d | 2015-04-30 06:46:28 -0700 | [diff] [blame] | 260 | } |