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