Home | History | Annotate | Download | only in browser
      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         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             Intent intent = new Intent(this, BrowserPreferencesPage.class);
    146             intent.putExtra(BrowserPreferencesPage.CURRENT_PAGE, url);
    147             startActivityForResult(intent, Controller.PREFERENCES_PAGE);
    148             return true;
    149         }
    150         return super.onOptionsItemSelected(item);
    151     }
    152 
    153     /**
    154      * This is a helper class that implements the management of tabs and all
    155      * details of connecting a ViewPager with associated TabHost.  It relies on a
    156      * trick.  Normally a tab host has a simple API for supplying a View or
    157      * Intent that each tab will show.  This is not sufficient for switching
    158      * between pages.  So instead we make the content part of the tab host
    159      * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
    160      * view to show as the tab content.  It listens to changes in tabs, and takes
    161      * care of switch to the correct page in the ViewPager whenever the selected
    162      * tab changes.
    163      */
    164     public static class TabsAdapter extends FragmentPagerAdapter
    165             implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
    166         private final Context mContext;
    167         private final ActionBar mActionBar;
    168         private final ViewPager mViewPager;
    169         private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
    170 
    171         static final class TabInfo {
    172             private final Class<?> clss;
    173             private final Bundle args;
    174 
    175             TabInfo(Class<?> _class, Bundle _args) {
    176                 clss = _class;
    177                 args = _args;
    178             }
    179         }
    180 
    181         public TabsAdapter(Activity activity, ViewPager pager) {
    182             super(activity.getFragmentManager());
    183             mContext = activity;
    184             mActionBar = activity.getActionBar();
    185             mViewPager = pager;
    186             mViewPager.setAdapter(this);
    187             mViewPager.setOnPageChangeListener(this);
    188         }
    189 
    190         public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
    191             TabInfo info = new TabInfo(clss, args);
    192             tab.setTag(info);
    193             tab.setTabListener(this);
    194             mTabs.add(info);
    195             mActionBar.addTab(tab);
    196             notifyDataSetChanged();
    197         }
    198 
    199         @Override
    200         public int getCount() {
    201             return mTabs.size();
    202         }
    203 
    204         @Override
    205         public Fragment getItem(int position) {
    206             TabInfo info = mTabs.get(position);
    207             return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    208         }
    209 
    210         @Override
    211         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    212         }
    213 
    214         @Override
    215         public void onPageSelected(int position) {
    216             mActionBar.setSelectedNavigationItem(position);
    217         }
    218 
    219         @Override
    220         public void onPageScrollStateChanged(int state) {
    221         }
    222 
    223         @Override
    224         public void onTabSelected(android.app.ActionBar.Tab tab,
    225                 FragmentTransaction ft) {
    226             Object tag = tab.getTag();
    227             for (int i=0; i<mTabs.size(); i++) {
    228                 if (mTabs.get(i) == tag) {
    229                     mViewPager.setCurrentItem(i);
    230                 }
    231             }
    232         }
    233 
    234         @Override
    235         public void onTabUnselected(android.app.ActionBar.Tab tab,
    236                 FragmentTransaction ft) {
    237         }
    238 
    239         @Override
    240         public void onTabReselected(android.app.ActionBar.Tab tab,
    241                 FragmentTransaction ft) {
    242         }
    243     }
    244 
    245     private static String makeFragmentName(int viewId, int index) {
    246         return "android:switcher:" + viewId + ":" + index;
    247     }
    248 
    249 }
    250