blob: 322949a44d3ee83c639bf0d8100f253e2e0f06e3 [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;
30import android.view.View;
mariagpuyol91095782016-03-24 17:52:28 -070031import android.widget.Toolbar;
mariagpuyol58748352016-03-09 15:36:28 -080032
mariagpuyolc3f3a802016-03-17 13:29:33 -070033import java.util.ArrayList;
mariagpuyolea078ed2016-03-07 20:02:10 -080034/**
mariagpuyol9be32652016-03-16 12:34:53 -070035 * An activity uses a tab layout to separate personal and medical information
mariagpuyolea078ed2016-03-07 20:02:10 -080036 * from emergency contacts.
37 */
mariagpuyol91095782016-03-24 17:52:28 -070038public abstract class EmergencyTabActivity extends Activity {
mariagpuyolea078ed2016-03-07 20:02:10 -080039 private ViewPagerAdapter mTabsAdapter;
40 private TabLayout mTabLayout;
41
mariagpuyolc3f3a802016-03-17 13:29:33 -070042 private ArrayList<Pair<String, Fragment>> mFragments;
mariagpuyolea078ed2016-03-07 20:02:10 -080043
mariagpuyol9be32652016-03-16 12:34:53 -070044 @Override
45 protected void onResume() {
46 super.onResume();
47 int display_mode = getResources().getConfiguration().orientation;
48
49 if (display_mode == Configuration.ORIENTATION_PORTRAIT) {
50 mTabLayout.setTabMode(TabLayout.MODE_FIXED);
51 mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
52 }
53 }
54
mariagpuyolde7a4052016-03-18 12:32:43 -070055 @Override
56 public boolean onOptionsItemSelected(MenuItem item) {
57 switch (item.getItemId()) {
58 // Respond to the action bar's Up/Home button.
59 case android.R.id.home:
60 onBackPressed();
61 return true;
62 }
63 return super.onOptionsItemSelected(item);
64 }
65
mariagpuyolea078ed2016-03-07 20:02:10 -080066 /** Returns the index of the currently selected tab. */
67 public int getSelectedTabPosition() {
68 return mTabLayout.getSelectedTabPosition();
69 }
70
71 @Override
mariagpuyolea078ed2016-03-07 20:02:10 -080072 public void setContentView(@LayoutRes int layoutResID) {
mariagpuyol9be32652016-03-16 12:34:53 -070073 super.setContentView(layoutResID);
mariagpuyol232daaf2016-03-09 11:26:00 -080074 setupTabs();
mariagpuyol9be32652016-03-16 12:34:53 -070075 Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
76 toolbar.setTitle(getActivityTitle());
mariagpuyol91095782016-03-24 17:52:28 -070077 setActionBar(toolbar);
78 getActionBar().setDisplayHomeAsUpEnabled(true);
mariagpuyolea078ed2016-03-07 20:02:10 -080079 }
80
mariagpuyol7ee3df32016-03-30 12:14:39 -070081 /** Selects the tab at index {@code selectedTabIndex}. */
82 public void selectTab(int selectedTabIndex) {
83 if (mTabLayout != null && selectedTabIndex >= 0 &&
84 selectedTabIndex < mTabLayout.getTabCount()) {
85 mTabLayout.getTabAt(selectedTabIndex).select();
86 }
87 }
88
mariagpuyolc3f3a802016-03-17 13:29:33 -070089 protected void setupTabs() {
90 mFragments = setUpFragments();
91 mTabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
92 if (mTabsAdapter == null) {
93 // The viewpager that will host the section contents.
94 ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
95 mTabsAdapter = new ViewPagerAdapter(getFragmentManager());
96 viewPager.setAdapter(mTabsAdapter);
97 mTabLayout.setTabsFromPagerAdapter(mTabsAdapter);
98
99 // Set a listener via setOnTabSelectedListener(OnTabSelectedListener) to be notified
100 // when any tab's selection state has been changed.
101 mTabLayout.setOnTabSelectedListener(
102 new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
103
104 // Use a TabLayout.TabLayoutOnPageChangeListener to forward the scroll and selection
105 // changes to this layout
106 viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(mTabLayout));
107 } else {
108 mTabsAdapter.notifyDataSetChanged();
109 mTabLayout.setTabsFromPagerAdapter(mTabsAdapter);
110 }
111 }
112
113 protected TabLayout getTabLayout() {
114 return mTabLayout;
115 }
116
117 /** Return number of fragments to show in the tabs. */
118 public int getNumberFragments() {
119 return mFragments.size();
120 }
121
mariagpuyolea078ed2016-03-07 20:02:10 -0800122 /** Returns whether the activity is in view mode (true) or in edit mode (false). */
123 public abstract boolean isInViewMode();
124
mariagpuyol9be32652016-03-16 12:34:53 -0700125 /** Returns the activity title. */
126 public abstract String getActivityTitle();
mariagpuyolea078ed2016-03-07 20:02:10 -0800127
mariagpuyolc3f3a802016-03-17 13:29:33 -0700128 /** Returns the fragments to show in the tabs. */
129 protected abstract ArrayList<Pair<String, Fragment>> setUpFragments();
130
mariagpuyolea078ed2016-03-07 20:02:10 -0800131 /** The adapter used to handle the two fragments. */
132 private class ViewPagerAdapter extends FragmentStatePagerAdapter {
133 public ViewPagerAdapter(FragmentManager fm) {
134 super(fm);
135 }
136
137 @Override
mariagpuyolea078ed2016-03-07 20:02:10 -0800138 public Fragment getItem(int position) {
mariagpuyolc3f3a802016-03-17 13:29:33 -0700139 return mFragments.get(position).second;
mariagpuyolea078ed2016-03-07 20:02:10 -0800140 }
141
142 @Override
143 public int getCount() {
mariagpuyolc3f3a802016-03-17 13:29:33 -0700144 return mFragments.size();
mariagpuyolea078ed2016-03-07 20:02:10 -0800145 }
146
147 @Override
148 public CharSequence getPageTitle(int position) {
mariagpuyolc3f3a802016-03-17 13:29:33 -0700149 return mFragments.get(position).first;
150 }
151
152 @Override
153 public int getItemPosition(Object object) {
154 // The default implementation assumes that items will never change position and always
155 // returns POSITION_UNCHANGED. This is how you can specify that the positions can change
156 return FragmentStatePagerAdapter.POSITION_NONE;
mariagpuyolea078ed2016-03-07 20:02:10 -0800157 }
158 }
159}