blob: 4026bddecad67ed91312369059670afa6aee4727 [file] [log] [blame]
John Reckd3e4d5b2011-07-13 15:48:43 -07001/*
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 Amirzada41242f22014-03-21 12:12:18 -070016package com.android.browser;
John Reckd3e4d5b2011-07-13 15:48:43 -070017
John Reck2d963a22011-08-10 15:53:07 -070018import android.app.ActionBar;
John Reckd3e4d5b2011-07-13 15:48:43 -070019import android.app.Activity;
John Reck2d963a22011-08-10 15:53:07 -070020import android.app.Fragment;
21import android.app.FragmentTransaction;
22import android.content.Context;
John Reckd3e4d5b2011-07-13 15:48:43 -070023import android.content.Intent;
24import android.net.Uri;
25import android.os.Bundle;
John Reck2d963a22011-08-10 15:53:07 -070026import android.support.v13.app.FragmentPagerAdapter;
27import android.support.v4.view.ViewPager;
John Reckd3e4d5b2011-07-13 15:48:43 -070028import android.view.Menu;
29import android.view.MenuItem;
30
Bijan Amirzada41242f22014-03-21 12:12:18 -070031import com.android.browser.R;
32import com.android.browser.UI.ComboViews;
John Reckd3e4d5b2011-07-13 15:48:43 -070033
John Reck2d963a22011-08-10 15:53:07 -070034import java.util.ArrayList;
35
John Reckd3e4d5b2011-07-13 15:48:43 -070036public class ComboViewActivity extends Activity implements CombinedBookmarksCallbacks {
37
John Reck2d963a22011-08-10 15:53:07 -070038 private static final String STATE_SELECTED_TAB = "tab";
John Reckd3e4d5b2011-07-13 15:48:43 -070039 public static final String EXTRA_COMBO_ARGS = "combo_args";
40 public static final String EXTRA_INITIAL_VIEW = "initial_view";
41
42 public static final String EXTRA_OPEN_SNAPSHOT = "snapshot_id";
43 public static final String EXTRA_OPEN_ALL = "open_all";
44 public static final String EXTRA_CURRENT_URL = "url";
John Reck2d963a22011-08-10 15:53:07 -070045 private ViewPager mViewPager;
46 private TabsAdapter mTabsAdapter;
John Reckd3e4d5b2011-07-13 15:48:43 -070047
48 @Override
49 protected void onCreate(Bundle savedInstanceState) {
50 super.onCreate(savedInstanceState);
51 setResult(RESULT_CANCELED);
52 Bundle extras = getIntent().getExtras();
53 Bundle args = extras.getBundle(EXTRA_COMBO_ARGS);
54 String svStr = extras.getString(EXTRA_INITIAL_VIEW, null);
55 ComboViews startingView = svStr != null
56 ? ComboViews.valueOf(svStr)
57 : ComboViews.Bookmarks;
John Reck2d963a22011-08-10 15:53:07 -070058 mViewPager = new ViewPager(this);
59 mViewPager.setId(R.id.tab_view);
60 setContentView(mViewPager);
61
62 final ActionBar bar = getActionBar();
63 bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
64 if (BrowserActivity.isTablet(this)) {
65 bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME
66 | ActionBar.DISPLAY_USE_LOGO);
John Reckc0b64c82011-11-10 17:12:24 -080067 bar.setHomeButtonEnabled(true);
John Reck2d963a22011-08-10 15:53:07 -070068 } else {
69 bar.setDisplayOptions(0);
70 }
71
72 mTabsAdapter = new TabsAdapter(this, mViewPager);
73 mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_bookmarks),
74 BrowserBookmarksPage.class, args);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080075 mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_history),
76 BrowserHistoryPage.class, args);
77 mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_snapshots),
78 BrowserSnapshotPage.class, args);
John Reck2d963a22011-08-10 15:53:07 -070079
80 if (savedInstanceState != null) {
81 bar.setSelectedNavigationItem(
82 savedInstanceState.getInt(STATE_SELECTED_TAB, 0));
83 } else {
84 switch (startingView) {
85 case Bookmarks:
86 mViewPager.setCurrentItem(0);
87 break;
88 case History:
89 mViewPager.setCurrentItem(1);
90 break;
91 case Snapshots:
92 mViewPager.setCurrentItem(2);
93 break;
94 }
95 }
96 }
97
98 @Override
99 protected void onSaveInstanceState(Bundle outState) {
100 super.onSaveInstanceState(outState);
101 outState.putInt(STATE_SELECTED_TAB,
102 getActionBar().getSelectedNavigationIndex());
John Reckd3e4d5b2011-07-13 15:48:43 -0700103 }
104
105 @Override
106 public void openUrl(String url) {
107 Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
108 setResult(RESULT_OK, i);
109 finish();
110 }
111
112 @Override
113 public void openInNewTab(String... urls) {
114 Intent i = new Intent();
115 i.putExtra(EXTRA_OPEN_ALL, urls);
116 setResult(RESULT_OK, i);
117 finish();
118 }
119
120 @Override
121 public void close() {
122 finish();
123 }
124
125 @Override
126 public void openSnapshot(long id) {
127 Intent i = new Intent();
128 i.putExtra(EXTRA_OPEN_SNAPSHOT, id);
129 setResult(RESULT_OK, i);
130 finish();
131 }
132
133 @Override
John Reckd3e4d5b2011-07-13 15:48:43 -0700134 public boolean onCreateOptionsMenu(Menu menu) {
135 getMenuInflater().inflate(R.menu.combined, menu);
136 return super.onCreateOptionsMenu(menu);
137 }
138
139 @Override
140 public boolean onOptionsItemSelected(MenuItem item) {
141 if (item.getItemId() == android.R.id.home) {
142 finish();
143 return true;
144 } else if (item.getItemId() == R.id.preferences_menu_id) {
145 String url = getIntent().getStringExtra(EXTRA_CURRENT_URL);
146 Intent intent = new Intent(this, BrowserPreferencesPage.class);
147 intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE, url);
148 startActivityForResult(intent, Controller.PREFERENCES_PAGE);
149 return true;
150 }
151 return super.onOptionsItemSelected(item);
152 }
John Reck2d963a22011-08-10 15:53:07 -0700153
154 /**
155 * This is a helper class that implements the management of tabs and all
156 * details of connecting a ViewPager with associated TabHost. It relies on a
157 * trick. Normally a tab host has a simple API for supplying a View or
158 * Intent that each tab will show. This is not sufficient for switching
159 * between pages. So instead we make the content part of the tab host
160 * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
161 * view to show as the tab content. It listens to changes in tabs, and takes
162 * care of switch to the correct page in the ViewPager whenever the selected
163 * tab changes.
164 */
165 public static class TabsAdapter extends FragmentPagerAdapter
166 implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
167 private final Context mContext;
168 private final ActionBar mActionBar;
169 private final ViewPager mViewPager;
170 private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
171
172 static final class TabInfo {
173 private final Class<?> clss;
174 private final Bundle args;
175
176 TabInfo(Class<?> _class, Bundle _args) {
177 clss = _class;
178 args = _args;
179 }
180 }
181
182 public TabsAdapter(Activity activity, ViewPager pager) {
183 super(activity.getFragmentManager());
184 mContext = activity;
185 mActionBar = activity.getActionBar();
186 mViewPager = pager;
187 mViewPager.setAdapter(this);
188 mViewPager.setOnPageChangeListener(this);
189 }
190
191 public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
192 TabInfo info = new TabInfo(clss, args);
193 tab.setTag(info);
194 tab.setTabListener(this);
195 mTabs.add(info);
196 mActionBar.addTab(tab);
197 notifyDataSetChanged();
198 }
199
200 @Override
201 public int getCount() {
202 return mTabs.size();
203 }
204
205 @Override
206 public Fragment getItem(int position) {
207 TabInfo info = mTabs.get(position);
208 return Fragment.instantiate(mContext, info.clss.getName(), info.args);
209 }
210
211 @Override
212 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
213 }
214
215 @Override
216 public void onPageSelected(int position) {
217 mActionBar.setSelectedNavigationItem(position);
218 }
219
220 @Override
221 public void onPageScrollStateChanged(int state) {
222 }
223
224 @Override
225 public void onTabSelected(android.app.ActionBar.Tab tab,
226 FragmentTransaction ft) {
227 Object tag = tab.getTag();
228 for (int i=0; i<mTabs.size(); i++) {
229 if (mTabs.get(i) == tag) {
230 mViewPager.setCurrentItem(i);
231 }
232 }
233 }
234
235 @Override
236 public void onTabUnselected(android.app.ActionBar.Tab tab,
237 FragmentTransaction ft) {
238 }
239
240 @Override
241 public void onTabReselected(android.app.ActionBar.Tab tab,
242 FragmentTransaction ft) {
243 }
244 }
245
246 private static String makeFragmentName(int viewId, int index) {
247 return "android:switcher:" + viewId + ":" + index;
248 }
249
John Reckd3e4d5b2011-07-13 15:48:43 -0700250}