altosdroid: Fix tab updates on Android 11 after rotate
[fw/altos] / altosdroid / app / src / main / java / 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 androidx.fragment.app.Fragment;
24 import androidx.fragment.app.FragmentActivity;
25 import androidx.fragment.app.FragmentPagerAdapter;
26 import androidx.viewpager.widget.ViewPager;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.TabHost;
30 import android.widget.TabWidget;
31
32 /**
33  * This is a helper class that implements the management of tabs and all
34  * details of connecting a ViewPager with associated TabHost.  It relies on a
35  * trick.  Normally a tab host has a simple API for supplying a View or
36  * Intent that each tab will show.  This is not sufficient for switching
37  * between pages.  So instead we make the content part of the tab host
38  * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy
39  * view to show as the tab content.  It listens to changes in tabs, and takes
40  * care of switch to the correct paged in the ViewPager whenever the selected
41  * tab changes.
42  */
43 public class TabsAdapter extends FragmentPagerAdapter
44                 implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
45         private final Context mContext;
46         private final TabHost mTabHost;
47         private final ViewPager mViewPager;
48         private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
49         private int position;
50
51         static class TabInfo {
52                 private final String tag;
53                 private final Class<?> clss;
54                 private final Bundle args;
55                 private Fragment fragment;
56
57                 TabInfo(String _tag, Class<?> _class, Bundle _args, Fragment _fragment) {
58                         tag = _tag;
59                         clss = _class;
60                         args = _args;
61                         fragment = _fragment;
62                 }
63         }
64
65         static class DummyTabFactory implements TabHost.TabContentFactory {
66                 private final Context mContext;
67
68                 public DummyTabFactory(Context context) {
69                         mContext = context;
70                 }
71
72                 public View createTabContent(String tag) {
73                         View v = new View(mContext);
74                         v.setMinimumWidth(0);
75                         v.setMinimumHeight(0);
76                         return v;
77                 }
78         }
79
80         public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
81                 super(activity.getSupportFragmentManager());
82                 mContext = activity;
83                 mTabHost = tabHost;
84                 mViewPager = pager;
85                 mTabHost.setOnTabChangedListener(this);
86                 mViewPager.setAdapter(this);
87                 mViewPager.addOnPageChangeListener(this);
88         }
89
90         public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args, Fragment fragment) {
91                 tabSpec.setContent(new DummyTabFactory(mContext));
92                 String tag = tabSpec.getTag();
93
94                 TabInfo info = new TabInfo(tag, clss, args, fragment);
95                 mTabs.add(info);
96                 mTabHost.addTab(tabSpec);
97                 notifyDataSetChanged();
98         }
99
100         @Override
101         public int getCount() {
102                 return mTabs.size();
103         }
104
105         @Override
106         public Fragment getItem(int position) {
107                 TabInfo info = mTabs.get(position);
108                 AltosDebug.debug("TabsAdapter.getItem(%d)", position);
109                 if (info.fragment == null)
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                 AltosDebug.debug("TabsAdapter.onTabChanged(%s) = %d cur %s prev %s", tabId, position, cur_frag, prev_frag);
127
128                 if (prev_frag != cur_frag) {
129                         if (prev_frag != null) {
130                                 prev_frag.set_visible(false);
131                         }
132                 }
133
134                 /* This happens when the tab is selected before any of them
135                  * have been created, like during rotation
136                  */
137                 if (cur_frag != null)
138                         cur_frag.set_visible(true);
139                 mViewPager.setCurrentItem(position);
140         }
141
142         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
143         }
144
145         public void onPageSelected(int position) {
146                 // Unfortunately when TabHost changes the current tab, it kindly
147                 // also takes care of putting focus on it when not in touch mode.
148                 // The jerk.
149                 // This hack tries to prevent this from pulling focus out of our
150                 // ViewPager.
151                 TabWidget widget = mTabHost.getTabWidget();
152                 int oldFocusability = widget.getDescendantFocusability();
153                 widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
154                 mTabHost.setCurrentTab(position);
155                 widget.setDescendantFocusability(oldFocusability);
156         }
157
158         public void onPageScrollStateChanged(int state) {
159         }
160 }