mariagpuyol | ea078ed | 2016-03-07 20:02:10 -0800 | [diff] [blame^] | 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.Fragment; |
| 19 | import android.app.FragmentManager; |
| 20 | import android.content.res.Configuration; |
| 21 | import android.os.Bundle; |
| 22 | import android.preference.PreferenceActivity; |
| 23 | import android.support.annotation.LayoutRes; |
| 24 | import android.support.annotation.Nullable; |
| 25 | import android.support.design.widget.TabLayout; |
| 26 | import android.support.design.widget.TabLayout.TabLayoutOnPageChangeListener; |
| 27 | import android.support.design.widget.TabLayout.ViewPagerOnTabSelectedListener; |
| 28 | import android.support.v13.app.FragmentStatePagerAdapter; |
| 29 | import android.support.v4.view.ViewPager; |
| 30 | import android.support.v7.app.ActionBar; |
| 31 | import android.support.v7.app.AppCompatDelegate; |
| 32 | import android.support.v7.widget.Toolbar; |
| 33 | import android.view.MenuInflater; |
| 34 | import android.view.MenuItem; |
| 35 | import android.view.View; |
| 36 | import android.view.ViewGroup; |
| 37 | import android.widget.TableLayout; |
| 38 | |
| 39 | /** |
| 40 | * A {@link PreferenceActivity} which implements and proxies the necessary calls |
| 41 | * to be used with AppCompat. It uses a tab layout to separate personal and medical information |
| 42 | * from emergency contacts. |
| 43 | */ |
| 44 | public abstract class EmergencyTabPreferenceActivity extends PreferenceActivity { |
| 45 | |
| 46 | private AppCompatDelegate mDelegate; |
| 47 | |
| 48 | protected static final String EXTRA_SELECTED_TAB = "selected_tab"; |
| 49 | |
| 50 | public static final int INDEX_INFO_TAB = 0; |
| 51 | public static final int INDEX_CONTACTS_TAB = 1; |
| 52 | private static final int NUMBER_TABS = 2; |
| 53 | |
| 54 | private ViewPagerAdapter mTabsAdapter; |
| 55 | private TabLayout mTabLayout; |
| 56 | |
| 57 | private Fragment[] mFragments = new Fragment[NUMBER_TABS]; |
| 58 | |
| 59 | @Override |
| 60 | protected void onCreate(Bundle savedInstanceState) { |
| 61 | getDelegate().installViewFactory(); |
| 62 | getDelegate().onCreate(savedInstanceState); |
| 63 | super.onCreate(savedInstanceState); |
| 64 | |
| 65 | setContentView(R.layout.tab_layout); |
| 66 | mTabLayout = (TabLayout) findViewById(R.id.sliding_tabs); |
| 67 | |
| 68 | if (mTabsAdapter == null) { |
| 69 | // The viewpager that will host the section contents. |
| 70 | ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); |
| 71 | mTabsAdapter = new ViewPagerAdapter(getFragmentManager()); |
| 72 | viewPager.setAdapter(mTabsAdapter); |
| 73 | mTabLayout.setTabsFromPagerAdapter(mTabsAdapter); |
| 74 | |
| 75 | // Set a listener via setOnTabSelectedListener(OnTabSelectedListener) to be notified |
| 76 | // when any tab's selection state has been changed. |
| 77 | mTabLayout.setOnTabSelectedListener( |
| 78 | new TabLayout.ViewPagerOnTabSelectedListener(viewPager)); |
| 79 | |
| 80 | // Use a TabLayout.TabLayoutOnPageChangeListener to forward the scroll and selection |
| 81 | // changes to this layout |
| 82 | viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(mTabLayout)); |
| 83 | } |
| 84 | |
| 85 | // Select the same tab that was selected in the Edit/View activity. |
| 86 | final int selectedTabIndex = getIntent().getIntExtra(EXTRA_SELECTED_TAB, INDEX_INFO_TAB); |
| 87 | selectTab(selectedTabIndex); |
| 88 | |
| 89 | setupActionBar(); |
| 90 | } |
| 91 | |
| 92 | /** Selects the tab at index {@code selectedTabIndex}. */ |
| 93 | public void selectTab(int selectedTabIndex) { |
| 94 | TabLayout.Tab tab = mTabLayout.getTabAt(selectedTabIndex); |
| 95 | tab.select(); |
| 96 | } |
| 97 | |
| 98 | /** Returns the index of the currently selected tab. */ |
| 99 | public int getSelectedTabPosition() { |
| 100 | return mTabLayout.getSelectedTabPosition(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | protected void onSaveInstanceState(Bundle outState) { |
| 105 | super.onSaveInstanceState(outState); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected void onPostCreate(Bundle savedInstanceState) { |
| 110 | super.onPostCreate(savedInstanceState); |
| 111 | getDelegate().onPostCreate(savedInstanceState); |
| 112 | } |
| 113 | |
| 114 | public ActionBar getSupportActionBar() { |
| 115 | return getDelegate().getSupportActionBar(); |
| 116 | } |
| 117 | |
| 118 | public void setSupportActionBar(@Nullable Toolbar toolbar) { |
| 119 | getDelegate().setSupportActionBar(toolbar); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public MenuInflater getMenuInflater() { |
| 124 | return getDelegate().getMenuInflater(); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void setContentView(@LayoutRes int layoutResID) { |
| 129 | getDelegate().setContentView(layoutResID); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void setContentView(View view) { |
| 134 | getDelegate().setContentView(view); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 139 | getDelegate().setContentView(view, params); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void addContentView(View view, ViewGroup.LayoutParams params) { |
| 144 | getDelegate().addContentView(view, params); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | protected void onPostResume() { |
| 149 | super.onPostResume(); |
| 150 | getDelegate().onPostResume(); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | protected void onTitleChanged(CharSequence title, int color) { |
| 155 | super.onTitleChanged(title, color); |
| 156 | getDelegate().setTitle(title); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onConfigurationChanged(Configuration newConfig) { |
| 161 | super.onConfigurationChanged(newConfig); |
| 162 | getDelegate().onConfigurationChanged(newConfig); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | protected void onStop() { |
| 167 | super.onStop(); |
| 168 | getDelegate().onStop(); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | protected void onDestroy() { |
| 173 | super.onDestroy(); |
| 174 | getDelegate().onDestroy(); |
| 175 | } |
| 176 | |
| 177 | private void setupActionBar() { |
| 178 | ActionBar actionBar = getSupportActionBar(); |
| 179 | if (actionBar != null) { |
| 180 | // Show the Up button in the action bar. |
| 181 | actionBar.setDisplayHomeAsUpEnabled(true); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public boolean onOptionsItemSelected(MenuItem item) { |
| 187 | switch (item.getItemId()) { |
| 188 | // Respond to the action bar's Up/Home button. |
| 189 | case android.R.id.home: |
| 190 | onBackPressed(); |
| 191 | return true; |
| 192 | } |
| 193 | return super.onOptionsItemSelected(item); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void invalidateOptionsMenu() { |
| 198 | getDelegate().invalidateOptionsMenu(); |
| 199 | } |
| 200 | |
| 201 | /** Returns whether the activity is in view mode (true) or in edit mode (false). */ |
| 202 | public abstract boolean isInViewMode(); |
| 203 | |
| 204 | private AppCompatDelegate getDelegate() { |
| 205 | if (mDelegate == null) { |
| 206 | mDelegate = AppCompatDelegate.create(this, null); |
| 207 | } |
| 208 | return mDelegate; |
| 209 | } |
| 210 | |
| 211 | /** The adapter used to handle the two fragments. */ |
| 212 | private class ViewPagerAdapter extends FragmentStatePagerAdapter { |
| 213 | public ViewPagerAdapter(FragmentManager fm) { |
| 214 | super(fm); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public Object instantiateItem(ViewGroup container, int position) { |
| 219 | Fragment fragment = (Fragment) super.instantiateItem(container, position); |
| 220 | mFragments[position] = fragment; |
| 221 | return fragment; |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void destroyItem(ViewGroup container, int position, Object object) { |
| 226 | mFragments[position] = null; |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public Fragment getItem(int position) { |
| 231 | Fragment fragment = mFragments[position]; |
| 232 | if (fragment != null) { |
| 233 | return fragment; |
| 234 | } |
| 235 | switch (position) { |
| 236 | case INDEX_INFO_TAB: |
| 237 | return EmergencyInfoFragment.newInstance(isInViewMode()); |
| 238 | case INDEX_CONTACTS_TAB: |
| 239 | return EmergencyContactsFragment.newInstance(isInViewMode()); |
| 240 | } |
| 241 | return null; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public int getCount() { |
| 246 | return NUMBER_TABS; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public CharSequence getPageTitle(int position) { |
| 251 | switch (position) { |
| 252 | case INDEX_INFO_TAB: |
| 253 | return getResources().getString(R.string.tab_title_info); |
| 254 | case INDEX_CONTACTS_TAB: |
| 255 | return getResources().getString(R.string.tab_title_contacts); |
| 256 | } |
| 257 | return null; |
| 258 | } |
| 259 | } |
| 260 | } |