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.annotation.VisibleForTesting; 24 import android.support.design.widget.TabLayout; 25 import android.support.design.widget.TabLayout.TabLayoutOnPageChangeListener; 26 import android.support.design.widget.TabLayout.ViewPagerOnTabSelectedListener; 27 import android.support.v13.app.FragmentStatePagerAdapter; 28 import android.support.v4.view.ViewPager; 29 import android.util.Pair; 30 import android.view.Menu; 31 import android.view.MenuItem; 32 import android.widget.Toolbar; 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 private Menu mMenu; 43 44 private ArrayList<Pair<String, Fragment>> mFragments; 45 46 @Override onResume()47 protected void onResume() { 48 super.onResume(); 49 int display_mode = getResources().getConfiguration().orientation; 50 51 if (display_mode == Configuration.ORIENTATION_PORTRAIT) { 52 mTabLayout.setTabMode(TabLayout.MODE_FIXED); 53 mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 54 } 55 } 56 57 @VisibleForTesting getMenu()58 public Menu getMenu() { 59 return mMenu; 60 } 61 62 @Override onCreateOptionsMenu(Menu menu)63 public boolean onCreateOptionsMenu(Menu menu) { 64 mMenu = menu; 65 return super.onCreateOptionsMenu(menu); 66 } 67 68 @Override onOptionsItemSelected(MenuItem item)69 public boolean onOptionsItemSelected(MenuItem item) { 70 switch (item.getItemId()) { 71 // Respond to the action bar's Up/Home button. 72 case android.R.id.home: 73 onBackPressed(); 74 return true; 75 } 76 return super.onOptionsItemSelected(item); 77 } 78 79 /** Returns the index of the currently selected tab. */ 80 @VisibleForTesting getSelectedTabPosition()81 public int getSelectedTabPosition() { 82 return mTabLayout.getSelectedTabPosition(); 83 } 84 85 @Override setContentView(@ayoutRes int layoutResID)86 public void setContentView(@LayoutRes int layoutResID) { 87 super.setContentView(layoutResID); 88 setupTabs(); 89 Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar); 90 setActionBar(toolbar); 91 getActionBar().setDisplayHomeAsUpEnabled(true); 92 } 93 94 /** Selects the tab at index {@code selectedTabIndex}. */ selectTab(int selectedTabIndex)95 public void selectTab(int selectedTabIndex) { 96 if (mTabLayout != null && selectedTabIndex >= 0 && 97 selectedTabIndex < mTabLayout.getTabCount()) { 98 mTabLayout.getTabAt(selectedTabIndex).select(); 99 } 100 } 101 setupTabs()102 protected void setupTabs() { 103 mFragments = setUpFragments(); 104 mTabLayout = (TabLayout) findViewById(R.id.sliding_tabs); 105 if (mTabsAdapter == null) { 106 // The viewpager that will host the section contents. 107 ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); 108 mTabsAdapter = new ViewPagerAdapter(getFragmentManager()); 109 viewPager.setAdapter(mTabsAdapter); 110 mTabLayout.setTabsFromPagerAdapter(mTabsAdapter); 111 112 // Set a listener via setOnTabSelectedListener(OnTabSelectedListener) to be notified 113 // when any tab's selection state has been changed. 114 mTabLayout.setOnTabSelectedListener( 115 new TabLayout.ViewPagerOnTabSelectedListener(viewPager)); 116 117 // Use a TabLayout.TabLayoutOnPageChangeListener to forward the scroll and selection 118 // changes to this layout 119 viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(mTabLayout)); 120 } else { 121 mTabsAdapter.notifyDataSetChanged(); 122 mTabLayout.setTabsFromPagerAdapter(mTabsAdapter); 123 } 124 } 125 getTabLayout()126 public TabLayout getTabLayout() { 127 return mTabLayout; 128 } 129 130 /** Return the fragments. */ 131 @VisibleForTesting getFragments()132 public ArrayList<Pair<String, Fragment>> getFragments() { 133 return mFragments; 134 } 135 136 /** Return number of fragments to show in the tabs. */ getNumberFragments()137 public int getNumberFragments() { 138 return mFragments.size(); 139 } 140 141 /** Return the adapter. */ getTabsAdapter()142 protected ViewPagerAdapter getTabsAdapter() { 143 return mTabsAdapter; 144 } 145 146 /** Returns the fragments to show in the tabs. */ setUpFragments()147 protected abstract ArrayList<Pair<String, Fragment>> setUpFragments(); 148 149 /** The adapter used to handle the two fragments. */ 150 protected class ViewPagerAdapter extends FragmentStatePagerAdapter { ViewPagerAdapter(FragmentManager fm)151 public ViewPagerAdapter(FragmentManager fm) { 152 super(fm); 153 } 154 155 @Override getItem(int position)156 public Fragment getItem(int position) { 157 return mFragments.get(position).second; 158 } 159 160 @Override getCount()161 public int getCount() { 162 return mFragments.size(); 163 } 164 165 @Override getPageTitle(int position)166 public CharSequence getPageTitle(int position) { 167 return mFragments.get(position).first; 168 } 169 170 @Override getItemPosition(Object object)171 public int getItemPosition(Object object) { 172 // The default implementation assumes that items will never change position and always 173 // returns POSITION_UNCHANGED. This is how you can specify that the positions can change 174 return FragmentStatePagerAdapter.POSITION_NONE; 175 } 176 } 177 } 178