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