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