altoslib: Support binary reading/writing in AltosLink
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TabsAdapter.java
1 /*
2  * Copyright (C) 2009 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
17 package org.altusmetrum.AltosDroid;
18
19 import java.util.ArrayList;
20
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.support.v4.app.Fragment;
24 import android.support.v4.app.FragmentActivity;
25 import android.support.v4.app.FragmentPagerAdapter;
26 import android.support.v4.view.ViewPager;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.TabHost;
30 import android.widget.TabWidget;
31
32
33
34 /**
35  * This is a helper class that implements the management of tabs and all
36  * details of connecting a ViewPager with associated TabHost.  It relies on a
37  * trick.  Normally a tab host has a simple API for supplying a View or
38  * Intent that each tab will show.  This is not sufficient for switching
39  * between pages.  So instead we make the content part of the tab host
40  * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
41  * view to show as the tab content.  It listens to changes in tabs, and takes
42  * care of switch to the correct paged in the ViewPager whenever the selected
43  * tab changes.
44  */
45 public class TabsAdapter extends FragmentPagerAdapter
46                 implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
47         private final Context mContext;
48         private final TabHost mTabHost;
49         private final ViewPager mViewPager;
50         private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
51
52         static final class TabInfo {
53                 private final String tag;
54                 private final Class<?> clss;
55                 private final Bundle args;
56
57                 TabInfo(String _tag, Class<?> _class, Bundle _args) {
58                         tag = _tag;
59                         clss = _class;
60                         args = _args;
61                 }
62         }
63
64         static class DummyTabFactory implements TabHost.TabContentFactory {
65                 private final Context mContext;
66
67                 public DummyTabFactory(Context context) {
68                         mContext = context;
69                 }
70
71                 public View createTabContent(String tag) {
72                         View v = new View(mContext);
73                         v.setMinimumWidth(0);
74                         v.setMinimumHeight(0);
75                         return v;
76                 }
77         }
78
79         public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
80                 super(activity.getSupportFragmentManager());
81                 mContext = activity;
82                 mTabHost = tabHost;
83                 mViewPager = pager;
84                 mTabHost.setOnTabChangedListener(this);
85                 mViewPager.setAdapter(this);
86                 mViewPager.setOnPageChangeListener(this);
87         }
88
89         public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
90                 tabSpec.setContent(new DummyTabFactory(mContext));
91                 String tag = tabSpec.getTag();
92
93                 TabInfo info = new TabInfo(tag, clss, args);
94                 mTabs.add(info);
95                 mTabHost.addTab(tabSpec);
96                 notifyDataSetChanged();
97         }
98
99         @Override
100         public int getCount() {
101                 return mTabs.size();
102         }
103
104         @Override
105         public Fragment getItem(int position) {
106                 TabInfo info = mTabs.get(position);
107                 return Fragment.instantiate(mContext, info.clss.getName(), info.args);
108         }
109
110         public void onTabChanged(String tabId) {
111                 int position = mTabHost.getCurrentTab();
112                 mViewPager.setCurrentItem(position);
113         }
114
115         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
116         }
117
118         public void onPageSelected(int position) {
119                 // Unfortunately when TabHost changes the current tab, it kindly
120                 // also takes care of putting focus on it when not in touch mode.
121                 // The jerk.
122                 // This hack tries to prevent this from pulling focus out of our
123                 // ViewPager.
124                 TabWidget widget = mTabHost.getTabWidget();
125                 int oldFocusability = widget.getDescendantFocusability();
126                 widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
127                 mTabHost.setCurrentTab(position);
128                 widget.setDescendantFocusability(oldFocusability);
129         }
130
131         public void onPageScrollStateChanged(int state) {
132         }
133 }