blob: 733de12cc1dee8491f4401e1e09963c5e736dae4 [file] [log] [blame]
Tarun Nainani8dc931d2015-04-30 06:46:28 -07001/*
Tarun Nainani87a86682015-02-05 11:47:35 -08002 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
Tarun Nainani8dc931d2015-04-30 06:46:28 -07003 * 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 Nainani87a86682015-02-05 11:47:35 -080017
Tarun Nainani8dc931d2015-04-30 06:46:28 -070018package com.android.browser;
19
20import android.app.ActionBar;
21import android.app.Activity;
Tarun Nainani8dc931d2015-04-30 06:46:28 -070022import android.content.Context;
23import android.content.Intent;
24import android.net.Uri;
25import android.os.Bundle;
Tarun Nainani8dc931d2015-04-30 06:46:28 -070026import android.support.v4.view.ViewPager;
Tarun Nainani87a86682015-02-05 11:47:35 -080027import android.util.AttributeSet;
28import android.view.View;
29import android.view.ViewGroup;
30import android.view.animation.AnimationSet;
31import android.view.animation.AnimationUtils;
32import android.widget.HorizontalScrollView;
33import android.widget.LinearLayout;
Tarun Nainani8dc931d2015-04-30 06:46:28 -070034
35import com.android.browser.UI.ComboViews;
36
Tarun Nainani87a86682015-02-05 11:47:35 -080037import java.util.Iterator;
38import java.util.Set;
Tarun Nainani8dc931d2015-04-30 06:46:28 -070039
Pankaj Garg68faf1c2015-06-26 17:07:37 -070040public class ComboView extends LinearLayout
41 implements CombinedBookmarksCallbacks, View.OnLayoutChangeListener {
Tarun Nainani8dc931d2015-04-30 06:46:28 -070042
Tarun Nainani87a86682015-02-05 11:47:35 -080043 private Activity mActivity;
Tarun Nainani8dc931d2015-04-30 06:46:28 -070044 private ViewPager mViewPager;
Tarun Nainani87a86682015-02-05 11:47:35 -080045 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 Nainani8dc931d2015-04-30 06:46:28 -070097
98 @Override
Tarun Nainani87a86682015-02-05 11:47:35 -080099 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 Nainani8dc931d2015-04-30 06:46:28 -0700108
Tarun Nainani87a86682015-02-05 11:47:35 -0800109 }
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 Garg68faf1c2015-06-26 17:07:37 -0700144 mTabsAdapter.removeAllTabs();
Tarun Nainani87a86682015-02-05 11:47:35 -0800145 mTabsAdapter = null;
146 }
147
148 final ActionBar bar = activity.getActionBar();
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700149 bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tarun Nainani87a86682015-02-05 11:47:35 -0800150 if (BrowserActivity.isTablet(activity)) {
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700151 bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME
152 | ActionBar.DISPLAY_USE_LOGO);
Tarun Nainani87a86682015-02-05 11:47:35 -0800153 bar.setDisplayHomeAsUpEnabled(true);
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700154 } else {
155 bar.setDisplayOptions(0);
156 }
Tarun Nainani87a86682015-02-05 11:47:35 -0800157 if (mTabsAdapter == null) {
158 mExtraArgs = args;
159 mTabsAdapter = new ComboTabsAdapter(activity, mViewPager);
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700160 mTabsAdapter.addTab(bar.newTab().setText(R.string.bookmarks),
Tarun Nainani87a86682015-02-05 11:47:35 -0800161 BrowserBookmarksPage.class, args);
Sagar Dhawan53b2ba72015-08-05 15:58:20 -0700162 mTabsAdapter.addTab(bar.newTab().setText(R.string.history),
Tarun Nainani87a86682015-02-05 11:47:35 -0800163 BrowserHistoryPage.class, args);
164 mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_snapshots),
165 BrowserSnapshotPage.class, args);
166 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700167
Tarun Nainani87a86682015-02-05 11:47:35 -0800168 /*if (savedInstanceState != null) {
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700169 bar.setSelectedNavigationItem(
170 savedInstanceState.getInt(STATE_SELECTED_TAB, 0));
Tarun Nainani87a86682015-02-05 11:47:35 -0800171 } else*/ {
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700172 switch (startingView) {
Tarun Nainani87a86682015-02-05 11:47:35 -0800173 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 Nainani8dc931d2015-04-30 06:46:28 -0700198 }
199 }
200 }
201
Tarun Nainani87a86682015-02-05 11:47:35 -0800202 public boolean isShowing() {
203 return (this.getVisibility() == View.VISIBLE);
204 }
205
206 public void hideViews() {
Vivek Sekhar55db4572015-12-02 10:20:03 -0800207 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 Nainani87a86682015-02-05 11:47:35 -0800218 }
Tarun Nainani87a86682015-02-05 11:47:35 -0800219 }
220
221 //TODO: Save the selected tab on BrowserActivity's onSaveInstanceState
222 /*public void onSaveInstanceState(Bundle outState) {
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700223 super.onSaveInstanceState(outState);
224 outState.putInt(STATE_SELECTED_TAB,
225 getActionBar().getSelectedNavigationIndex());
Tarun Nainani87a86682015-02-05 11:47:35 -0800226 }*/
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700227
228 @Override
229 public void openUrl(String url) {
230 Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
Tarun Nainani87a86682015-02-05 11:47:35 -0800231 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 Nainani8dc931d2015-04-30 06:46:28 -0700236 }
237
238 @Override
239 public void openInNewTab(String... urls) {
240 Intent i = new Intent();
Tarun Nainani87a86682015-02-05 11:47:35 -0800241 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 Nainani8dc931d2015-04-30 06:46:28 -0700247 }
248
249 @Override
250 public void close() {
Tarun Nainani87a86682015-02-05 11:47:35 -0800251 hideViews();
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700252 }
253
254 @Override
255 public void openSnapshot(long id) {
256 Intent i = new Intent();
Tarun Nainani87a86682015-02-05 11:47:35 -0800257 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 Nainani8dc931d2015-04-30 06:46:28 -0700263 }
Tarun Nainani8dc931d2015-04-30 06:46:28 -0700264}