blob: 5f14343756a4ed30134cde120c80937a019d39c9 [file] [log] [blame]
mariagpuyolea078ed2016-03-07 20:02:10 -08001/*
2 * Copyright (C) 2016 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 */
16package com.android.emergency;
17
mariagpuyol91095782016-03-24 17:52:28 -070018import android.app.Activity;
mariagpuyolea078ed2016-03-07 20:02:10 -080019import android.app.Fragment;
20import android.app.FragmentManager;
21import android.content.res.Configuration;
mariagpuyolea078ed2016-03-07 20:02:10 -080022import android.support.annotation.LayoutRes;
mariagpuyolea078ed2016-03-07 20:02:10 -080023import android.support.design.widget.TabLayout;
24import android.support.design.widget.TabLayout.TabLayoutOnPageChangeListener;
25import android.support.design.widget.TabLayout.ViewPagerOnTabSelectedListener;
26import android.support.v13.app.FragmentStatePagerAdapter;
27import android.support.v4.view.ViewPager;
mariagpuyolc3f3a802016-03-17 13:29:33 -070028import android.util.Pair;
mariagpuyolea078ed2016-03-07 20:02:10 -080029import android.view.MenuItem;
mariagpuyol91095782016-03-24 17:52:28 -070030import android.widget.Toolbar;
mariagpuyol58748352016-03-09 15:36:28 -080031
mariagpuyol0ad39002016-04-05 12:53:41 -070032import com.android.internal.annotations.VisibleForTesting;
33
mariagpuyolc3f3a802016-03-17 13:29:33 -070034import java.util.ArrayList;
mariagpuyolea078ed2016-03-07 20:02:10 -080035/**
mariagpuyol9be32652016-03-16 12:34:53 -070036 * An activity uses a tab layout to separate personal and medical information
mariagpuyolea078ed2016-03-07 20:02:10 -080037 * from emergency contacts.
38 */
mariagpuyol91095782016-03-24 17:52:28 -070039public abstract class EmergencyTabActivity extends Activity {
mariagpuyolea078ed2016-03-07 20:02:10 -080040 private ViewPagerAdapter mTabsAdapter;
41 private TabLayout mTabLayout;
42
mariagpuyolc3f3a802016-03-17 13:29:33 -070043 private ArrayList<Pair<String, Fragment>> mFragments;
mariagpuyolea078ed2016-03-07 20:02:10 -080044
mariagpuyol9be32652016-03-16 12:34:53 -070045 @Override
46 protected void onResume() {
47 super.onResume();
48 int display_mode = getResources().getConfiguration().orientation;
49
50 if (display_mode == Configuration.ORIENTATION_PORTRAIT) {
51 mTabLayout.setTabMode(TabLayout.MODE_FIXED);
52 mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
53 }
54 }
55
mariagpuyolde7a4052016-03-18 12:32:43 -070056 @Override
57 public boolean onOptionsItemSelected(MenuItem item) {
58 switch (item.getItemId()) {
59 // Respond to the action bar's Up/Home button.
60 case android.R.id.home:
61 onBackPressed();
62 return true;
63 }
64 return super.onOptionsItemSelected(item);
65 }
66
mariagpuyolea078ed2016-03-07 20:02:10 -080067 /** Returns the index of the currently selected tab. */
mariagpuyol0ad39002016-04-05 12:53:41 -070068 @VisibleForTesting
mariagpuyolea078ed2016-03-07 20:02:10 -080069 public int getSelectedTabPosition() {
70 return mTabLayout.getSelectedTabPosition();
71 }
72
73 @Override
mariagpuyolea078ed2016-03-07 20:02:10 -080074 public void setContentView(@LayoutRes int layoutResID) {
mariagpuyol9be32652016-03-16 12:34:53 -070075 super.setContentView(layoutResID);
mariagpuyol232daaf2016-03-09 11:26:00 -080076 setupTabs();
mariagpuyol9be32652016-03-16 12:34:53 -070077 Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
mariagpuyol91095782016-03-24 17:52:28 -070078 setActionBar(toolbar);
79 getActionBar().setDisplayHomeAsUpEnabled(true);
mariagpuyolea078ed2016-03-07 20:02:10 -080080 }
81
mariagpuyol7ee3df32016-03-30 12:14:39 -070082 /** Selects the tab at index {@code selectedTabIndex}. */
83 public void selectTab(int selectedTabIndex) {
84 if (mTabLayout != null && selectedTabIndex >= 0 &&
85 selectedTabIndex < mTabLayout.getTabCount()) {
86 mTabLayout.getTabAt(selectedTabIndex).select();
87 }
88 }
89
mariagpuyolc3f3a802016-03-17 13:29:33 -070090 protected void setupTabs() {
91 mFragments = setUpFragments();
92 mTabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
93 if (mTabsAdapter == null) {
94 // The viewpager that will host the section contents.
95 ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
96 mTabsAdapter = new ViewPagerAdapter(getFragmentManager());
97 viewPager.setAdapter(mTabsAdapter);
98 mTabLayout.setTabsFromPagerAdapter(mTabsAdapter);
99
100 // Set a listener via setOnTabSelectedListener(OnTabSelectedListener) to be notified
101 // when any tab's selection state has been changed.
102 mTabLayout.setOnTabSelectedListener(
103 new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
104
105 // Use a TabLayout.TabLayoutOnPageChangeListener to forward the scroll and selection
106 // changes to this layout
107 viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(mTabLayout));
108 } else {
109 mTabsAdapter.notifyDataSetChanged();
110 mTabLayout.setTabsFromPagerAdapter(mTabsAdapter);
111 }
112 }
113
mariagpuyol50c09e02016-03-29 20:31:57 -0700114 public TabLayout getTabLayout() {
mariagpuyolc3f3a802016-03-17 13:29:33 -0700115 return mTabLayout;
116 }
117
mariagpuyol88757782016-03-29 15:15:08 -0700118 /** Return the fragments. */
Maurice Lam109dfaf2017-01-12 21:35:10 -0800119 @VisibleForTesting
mariagpuyol88757782016-03-29 15:15:08 -0700120 public ArrayList<Pair<String, Fragment>> getFragments() {
121 return mFragments;
122 }
123
mariagpuyolc3f3a802016-03-17 13:29:33 -0700124 /** Return number of fragments to show in the tabs. */
125 public int getNumberFragments() {
126 return mFragments.size();
127 }
128
Naoki Amano9d8f6222016-09-15 21:08:57 +0900129 /** Return the adapter. */
130 protected ViewPagerAdapter getTabsAdapter() {
131 return mTabsAdapter;
132 }
133
mariagpuyolc3f3a802016-03-17 13:29:33 -0700134 /** Returns the fragments to show in the tabs. */
135 protected abstract ArrayList<Pair<String, Fragment>> setUpFragments();
136
mariagpuyolea078ed2016-03-07 20:02:10 -0800137 /** The adapter used to handle the two fragments. */
Naoki Amano9d8f6222016-09-15 21:08:57 +0900138 protected class ViewPagerAdapter extends FragmentStatePagerAdapter {
mariagpuyolea078ed2016-03-07 20:02:10 -0800139 public ViewPagerAdapter(FragmentManager fm) {
140 super(fm);
141 }
142
143 @Override
mariagpuyolea078ed2016-03-07 20:02:10 -0800144 public Fragment getItem(int position) {
mariagpuyolc3f3a802016-03-17 13:29:33 -0700145 return mFragments.get(position).second;
mariagpuyolea078ed2016-03-07 20:02:10 -0800146 }
147
148 @Override
149 public int getCount() {
mariagpuyolc3f3a802016-03-17 13:29:33 -0700150 return mFragments.size();
mariagpuyolea078ed2016-03-07 20:02:10 -0800151 }
152
153 @Override
154 public CharSequence getPageTitle(int position) {
mariagpuyolc3f3a802016-03-17 13:29:33 -0700155 return mFragments.get(position).first;
156 }
157
158 @Override
159 public int getItemPosition(Object object) {
160 // The default implementation assumes that items will never change position and always
161 // returns POSITION_UNCHANGED. This is how you can specify that the positions can change
162 return FragmentStatePagerAdapter.POSITION_NONE;
mariagpuyolea078ed2016-03-07 20:02:10 -0800163 }
164 }
165}