Home | History | Annotate | Download | only in emergency
      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
     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
     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
     69     public int getSelectedTabPosition() {
     70         return mTabLayout.getSelectedTabPosition();
     71     }
     72 
     73     @Override
     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}. */
     83     public void selectTab(int selectedTabIndex) {
     84         if (mTabLayout != null && selectedTabIndex >= 0 &&
     85                 selectedTabIndex < mTabLayout.getTabCount()) {
     86             mTabLayout.getTabAt(selectedTabIndex).select();
     87         }
     88     }
     89 
     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 
    114     public TabLayout getTabLayout() {
    115         return mTabLayout;
    116     }
    117 
    118     /** Return the fragments. */
    119     public ArrayList<Pair<String, Fragment>> getFragments() {
    120         return mFragments;
    121     }
    122 
    123     /** Return number of fragments to show in the tabs. */
    124     public int getNumberFragments() {
    125         return mFragments.size();
    126     }
    127 
    128     /** Return the adapter. */
    129     protected ViewPagerAdapter getTabsAdapter() {
    130         return mTabsAdapter;
    131     }
    132 
    133     /** Returns the fragments to show in the tabs. */
    134     protected abstract ArrayList<Pair<String, Fragment>> setUpFragments();
    135 
    136     /** The adapter used to handle the two fragments. */
    137     protected class ViewPagerAdapter extends FragmentStatePagerAdapter {
    138         public ViewPagerAdapter(FragmentManager fm) {
    139             super(fm);
    140         }
    141 
    142         @Override
    143         public Fragment getItem(int position) {
    144             return mFragments.get(position).second;
    145         }
    146 
    147         @Override
    148         public int getCount() {
    149             return mFragments.size();
    150         }
    151 
    152         @Override
    153         public CharSequence getPageTitle(int position) {
    154             return mFragments.get(position).first;
    155         }
    156 
    157         @Override
    158         public int getItemPosition(Object object) {
    159             // The default implementation assumes that items will never change position and always
    160             // returns POSITION_UNCHANGED. This is how you can specify that the positions can change
    161             return FragmentStatePagerAdapter.POSITION_NONE;
    162         }
    163     }
    164 }
    165