altosdroid: Move pause before reopening bluetooth into connec thread
[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.FragmentTransaction;
25 import android.support.v4.app.FragmentActivity;
26 import android.support.v4.app.FragmentPagerAdapter;
27 import android.support.v4.view.ViewPager;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.TabHost;
31 import android.widget.TabWidget;
32 import android.util.Log;
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         private int position;
52
53         static class TabInfo {
54                 private final String tag;
55                 private final Class<?> clss;
56                 private final Bundle args;
57                 private Fragment fragment;
58
59                 TabInfo(String _tag, Class<?> _class, Bundle _args) {
60                         tag = _tag;
61                         clss = _class;
62                         args = _args;
63                 }
64         }
65
66         static class DummyTabFactory implements TabHost.TabContentFactory {
67                 private final Context mContext;
68
69                 public DummyTabFactory(Context context) {
70                         mContext = context;
71                 }
72
73                 public View createTabContent(String tag) {
74                         View v = new View(mContext);
75                         v.setMinimumWidth(0);
76                         v.setMinimumHeight(0);
77                         return v;
78                 }
79         }
80
81         public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
82                 super(activity.getSupportFragmentManager());
83                 mContext = activity;
84                 mTabHost = tabHost;
85                 mViewPager = pager;
86                 mTabHost.setOnTabChangedListener(this);
87                 mViewPager.setAdapter(this);
88                 mViewPager.setOnPageChangeListener(this);
89         }
90
91         public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
92                 tabSpec.setContent(new DummyTabFactory(mContext));
93                 String tag = tabSpec.getTag();
94
95                 TabInfo info = new TabInfo(tag, clss, args);
96                 mTabs.add(info);
97                 mTabHost.addTab(tabSpec);
98                 notifyDataSetChanged();
99         }
100
101         @Override
102         public int getCount() {
103                 return mTabs.size();
104         }
105
106         @Override
107         public Fragment getItem(int position) {
108                 TabInfo info = mTabs.get(position);
109                 Log.d(AltosDroid.TAG, String.format("TabsAdapter.getItem(%d)", position));
110                 info.fragment = Fragment.instantiate(mContext, info.clss.getName(), info.args);
111                 return info.fragment;
112         }
113
114         public Fragment currentItem() {
115                 TabInfo info = mTabs.get(position);
116                 return info.fragment;
117         }
118
119         public void onTabChanged(String tabId) {
120                 AltosDroidTab   prev_frag = (AltosDroidTab) mTabs.get(position).fragment;
121
122                 position = mTabHost.getCurrentTab();
123
124                 AltosDroidTab   cur_frag = (AltosDroidTab) mTabs.get(position).fragment;
125
126                 if (prev_frag != cur_frag) {
127                         if (prev_frag != null) {
128                                 prev_frag.set_visible(false);
129                         }
130                 }
131                 if (cur_frag != null) {
132                         cur_frag.set_visible(true);
133                 }
134                 Log.d(AltosDroid.TAG, String.format("TabsAdapter.onTabChanged(%s) = %d", tabId, position));
135                 mViewPager.setCurrentItem(position);
136         }
137
138         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
139         }
140
141         public void onPageSelected(int position) {
142                 // Unfortunately when TabHost changes the current tab, it kindly
143                 // also takes care of putting focus on it when not in touch mode.
144                 // The jerk.
145                 // This hack tries to prevent this from pulling focus out of our
146                 // ViewPager.
147                 TabWidget widget = mTabHost.getTabWidget();
148                 int oldFocusability = widget.getDescendantFocusability();
149                 widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
150                 mTabHost.setCurrentTab(position);
151                 widget.setDescendantFocusability(oldFocusability);
152         }
153
154         public void onPageScrollStateChanged(int state) {
155         }
156 }