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.view; 17 18 import android.app.Activity; 19 import android.app.Fragment; 20 import android.app.FragmentManager; 21 import android.content.Intent; 22 import android.content.SharedPreferences; 23 import android.os.Bundle; 24 import android.support.annotation.LayoutRes; 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.preference.PreferenceManager; 31 import android.text.TextUtils; 32 import android.util.Pair; 33 import android.view.Menu; 34 import android.view.MenuInflater; 35 import android.view.MenuItem; 36 import android.view.View; 37 import android.view.WindowManager; 38 import android.widget.LinearLayout; 39 import android.widget.TextView; 40 import android.widget.Toolbar; 41 import android.widget.ViewFlipper; 42 43 import com.android.emergency.PreferenceKeys; 44 import com.android.emergency.R; 45 import com.android.emergency.edit.EditInfoActivity; 46 import com.android.emergency.util.PreferenceUtils; 47 import com.android.internal.annotations.VisibleForTesting; 48 import com.android.internal.logging.MetricsLogger; 49 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 50 51 import java.util.ArrayList; 52 53 /** 54 * Activity for viewing emergency information. 55 */ 56 public class ViewInfoActivity extends Activity { 57 private TextView mPersonalCardLargeItem; 58 private SharedPreferences mSharedPreferences; 59 private LinearLayout mPersonalCard; 60 private ViewFlipper mViewFlipper; 61 private ViewPagerAdapter mTabsAdapter; 62 private TabLayout mTabLayout; 63 private ArrayList<Pair<String, Fragment>> mFragments; 64 private Menu mMenu; 65 66 @Override setContentView(@ayoutRes int layoutResID)67 public void setContentView(@LayoutRes int layoutResID) { 68 super.setContentView(layoutResID); 69 setupTabs(); 70 Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar); 71 setActionBar(toolbar); 72 getActionBar().setDisplayHomeAsUpEnabled(true); 73 } 74 75 @Override onCreate(Bundle savedInstanceState)76 protected void onCreate(Bundle savedInstanceState) { 77 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 78 super.onCreate(savedInstanceState); 79 setContentView(R.layout.view_activity_layout); 80 mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 81 mPersonalCard = (LinearLayout) findViewById(R.id.name_and_dob_linear_layout); 82 mPersonalCardLargeItem = (TextView) findViewById(R.id.personal_card_large); 83 mViewFlipper = (ViewFlipper) findViewById(R.id.view_flipper); 84 85 MetricsLogger.visible(this, MetricsEvent.ACTION_VIEW_EMERGENCY_INFO); 86 } 87 88 @Override onResume()89 public void onResume() { 90 super.onResume(); 91 loadName(); 92 // Update the tabs: new info might have been added/deleted from the edit screen that 93 // could lead to adding/removing a fragment 94 setupTabs(); 95 maybeHideTabs(); 96 } 97 loadName()98 private void loadName() { 99 String name = mSharedPreferences.getString(PreferenceKeys.KEY_NAME, ""); 100 if (TextUtils.isEmpty(name)) { 101 mPersonalCard.setVisibility(View.GONE); 102 } else { 103 mPersonalCard.setVisibility(View.VISIBLE); 104 mPersonalCardLargeItem.setText(name); 105 } 106 } 107 maybeHideTabs()108 private void maybeHideTabs() { 109 // Show a TextView with "No information provided" if there are no fragments. 110 if (mFragments.size() == 0) { 111 mViewFlipper.setDisplayedChild( 112 mViewFlipper.indexOfChild(findViewById(R.id.no_info))); 113 } else { 114 mViewFlipper.setDisplayedChild(mViewFlipper.indexOfChild(findViewById(R.id.tabs))); 115 } 116 117 TabLayout tabLayout = mTabLayout; 118 if (mFragments.size() <= 1) { 119 tabLayout.setVisibility(View.GONE); 120 } else { 121 tabLayout.setVisibility(View.VISIBLE); 122 } 123 } 124 125 @Override onCreateOptionsMenu(Menu menu)126 public boolean onCreateOptionsMenu(Menu menu) { 127 MenuInflater inflater = getMenuInflater(); 128 inflater.inflate(R.menu.view_info_menu, menu); 129 mMenu = menu; 130 return super.onCreateOptionsMenu(menu); 131 } 132 133 @Override onOptionsItemSelected(MenuItem item)134 public boolean onOptionsItemSelected(MenuItem item) { 135 switch (item.getItemId()) { 136 case android.R.id.home: 137 onBackPressed(); 138 return true; 139 140 case R.id.action_edit: 141 Intent intent = new Intent(this, EditInfoActivity.class); 142 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 143 startActivity(intent); 144 return true; 145 } 146 return super.onOptionsItemSelected(item); 147 } 148 149 /** Return the tab layout. */ 150 @VisibleForTesting getTabLayout()151 public TabLayout getTabLayout() { 152 return mTabLayout; 153 } 154 155 @VisibleForTesting getMenu()156 public Menu getMenu() { 157 return mMenu; 158 } 159 160 /** Return the fragments. */ 161 @VisibleForTesting getFragments()162 public ArrayList<Pair<String, Fragment>> getFragments() { 163 return mFragments; 164 } 165 setUpFragments()166 private ArrayList<Pair<String, Fragment>> setUpFragments() { 167 // Return only the fragments that have at least one piece of information set: 168 ArrayList<Pair<String, Fragment>> fragments = new ArrayList<>(2); 169 170 if (PreferenceUtils.hasAtLeastOnePreferenceSet(this)) { 171 fragments.add(Pair.create(getResources().getString(R.string.tab_title_info), 172 ViewEmergencyInfoFragment.newInstance())); 173 } 174 if (PreferenceUtils.hasAtLeastOneEmergencyContact(this)) { 175 fragments.add(Pair.create(getResources().getString(R.string.tab_title_contacts), 176 ViewEmergencyContactsFragment.newInstance())); 177 } 178 return fragments; 179 } 180 setupTabs()181 private void setupTabs() { 182 mFragments = setUpFragments(); 183 mTabLayout = (TabLayout) findViewById(R.id.sliding_tabs); 184 if (mTabsAdapter == null) { 185 // The viewpager that will host the section contents. 186 ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); 187 mTabsAdapter = new ViewPagerAdapter(getFragmentManager()); 188 viewPager.setAdapter(mTabsAdapter); 189 mTabLayout.setTabsFromPagerAdapter(mTabsAdapter); 190 191 // Set a listener via setOnTabSelectedListener(OnTabSelectedListener) to be notified 192 // when any tab's selection state has been changed. 193 mTabLayout.setOnTabSelectedListener( 194 new TabLayout.ViewPagerOnTabSelectedListener(viewPager)); 195 196 // Use a TabLayout.TabLayoutOnPageChangeListener to forward the scroll and selection 197 // changes to this layout 198 viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(mTabLayout)); 199 } else { 200 mTabsAdapter.notifyDataSetChanged(); 201 mTabLayout.setTabsFromPagerAdapter(mTabsAdapter); 202 } 203 } 204 205 /** The adapter used to handle the two fragments. */ 206 protected class ViewPagerAdapter extends FragmentStatePagerAdapter { ViewPagerAdapter(FragmentManager fm)207 public ViewPagerAdapter(FragmentManager fm) { 208 super(fm); 209 } 210 211 @Override getItem(int position)212 public Fragment getItem(int position) { 213 return mFragments.get(position).second; 214 } 215 216 @Override getCount()217 public int getCount() { 218 return mFragments.size(); 219 } 220 221 @Override getPageTitle(int position)222 public CharSequence getPageTitle(int position) { 223 return mFragments.get(position).first; 224 } 225 226 @Override getItemPosition(Object object)227 public int getItemPosition(Object object) { 228 // The default implementation assumes that items will never change position and always 229 // returns POSITION_UNCHANGED. This is how you can specify that the positions can change 230 return FragmentStatePagerAdapter.POSITION_NONE; 231 } 232 } 233 } 234