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