1 /*
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  */
16 package com.android.emergency;
17 
18 import android.app.Activity;
19 import android.app.Fragment;
20 import android.app.FragmentManager;
21 import android.content.res.Configuration;
22 import android.support.annotation.LayoutRes;
23 import android.support.design.widget.TabLayout;
24 import android.support.design.widget.TabLayout.TabLayoutOnPageChangeListener;
25 import android.support.design.widget.TabLayout.ViewPagerOnTabSelectedListener;
26 import android.support.v13.app.FragmentStatePagerAdapter;
27 import android.support.v4.view.ViewPager;
28 import android.util.Pair;
29 import android.view.MenuItem;
30 import android.widget.Toolbar;
31 
32 import com.android.internal.annotations.VisibleForTesting;
33 
34 import java.util.ArrayList;
35 /**
36  * An activity uses a tab layout to separate personal and medical information
37  * from emergency contacts.
38  */
39 public abstract class EmergencyTabActivity extends Activity {
40     private ViewPagerAdapter mTabsAdapter;
41     private TabLayout mTabLayout;
42 
43     private ArrayList<Pair<String, Fragment>> mFragments;
44 
45     @Override
onResume()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 
56     @Override
onOptionsItemSelected(MenuItem item)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 
67     /** Returns the index of the currently selected tab. */
68     @VisibleForTesting
getSelectedTabPosition()69     public int getSelectedTabPosition() {
70         return mTabLayout.getSelectedTabPosition();
71     }
72 
73     @Override
setContentView(@ayoutRes int layoutResID)74     public void setContentView(@LayoutRes int layoutResID) {
75         super.setContentView(layoutResID);
76         setupTabs();
77         Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
78         setActionBar(toolbar);
79         getActionBar().setDisplayHomeAsUpEnabled(true);
80     }
81 
82     /** Selects the tab at index {@code selectedTabIndex}. */
selectTab(int 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 
setupTabs()90     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 
getTabLayout()114     public TabLayout getTabLayout() {
115         return mTabLayout;
116     }
117 
118     /** Return the fragments. */
getFragments()119     public ArrayList<Pair<String, Fragment>> getFragments() {
120         return mFragments;
121     }
122 
123     /** Return number of fragments to show in the tabs. */
getNumberFragments()124     public int getNumberFragments() {
125         return mFragments.size();
126     }
127 
128     /** Returns the fragments to show in the tabs. */
setUpFragments()129     protected abstract ArrayList<Pair<String, Fragment>> setUpFragments();
130 
131     /** The adapter used to handle the two fragments. */
132     private class ViewPagerAdapter extends FragmentStatePagerAdapter {
ViewPagerAdapter(FragmentManager fm)133         public ViewPagerAdapter(FragmentManager fm) {
134             super(fm);
135         }
136 
137         @Override
getItem(int position)138         public Fragment getItem(int position) {
139             return mFragments.get(position).second;
140         }
141 
142         @Override
getCount()143         public int getCount() {
144             return mFragments.size();
145         }
146 
147         @Override
getPageTitle(int position)148         public CharSequence getPageTitle(int position) {
149             return mFragments.get(position).first;
150         }
151 
152         @Override
getItemPosition(Object object)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;
157         }
158     }
159 }
160