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.Fragment; 19 import android.content.Intent; 20 import android.content.SharedPreferences; 21 import android.os.Bundle; 22 import android.preference.PreferenceManager; 23 import android.support.design.widget.TabLayout; 24 import android.text.TextUtils; 25 import android.util.Pair; 26 import android.view.Menu; 27 import android.view.MenuInflater; 28 import android.view.MenuItem; 29 import android.view.View; 30 import android.view.WindowManager; 31 import android.widget.LinearLayout; 32 import android.widget.TextView; 33 import android.widget.ViewFlipper; 34 35 import com.android.emergency.EmergencyTabActivity; 36 import com.android.emergency.PreferenceKeys; 37 import com.android.emergency.R; 38 import com.android.emergency.edit.EditInfoActivity; 39 import com.android.internal.logging.MetricsLogger; 40 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 41 42 import java.util.ArrayList; 43 44 /** 45 * Activity for viewing emergency information. 46 */ 47 public class ViewInfoActivity extends EmergencyTabActivity { 48 private TextView mPersonalCardLargeItem; 49 private SharedPreferences mSharedPreferences; 50 private LinearLayout mPersonalCard; 51 private ViewFlipper mViewFlipper; 52 53 54 @Override onCreate(Bundle savedInstanceState)55 protected void onCreate(Bundle savedInstanceState) { 56 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 57 super.onCreate(savedInstanceState); 58 setContentView(R.layout.view_activity_layout); 59 mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 60 mPersonalCard = (LinearLayout) findViewById(R.id.name_and_dob_linear_layout); 61 mPersonalCardLargeItem = (TextView) findViewById(R.id.personal_card_large); 62 mViewFlipper = (ViewFlipper) findViewById(R.id.view_flipper); 63 64 MetricsLogger.visible(this, MetricsEvent.ACTION_VIEW_EMERGENCY_INFO); 65 } 66 67 @Override onResume()68 public void onResume() { 69 super.onResume(); 70 loadName(); 71 // Update the tabs: new info might have been added/deleted from the edit screen that 72 // could lead to adding/removing a fragment 73 setupTabs(); 74 maybeHideTabs(); 75 } 76 loadName()77 private void loadName() { 78 String name = mSharedPreferences.getString(PreferenceKeys.KEY_NAME, ""); 79 if (TextUtils.isEmpty(name)) { 80 mPersonalCard.setVisibility(View.GONE); 81 } else { 82 mPersonalCard.setVisibility(View.VISIBLE); 83 mPersonalCardLargeItem.setText(name); 84 } 85 } 86 maybeHideTabs()87 private void maybeHideTabs() { 88 // Show a TextView with "No information provided" if there are no fragments. 89 if (getNumberFragments() == 0) { 90 mViewFlipper.setDisplayedChild( 91 mViewFlipper.indexOfChild(findViewById(R.id.no_info))); 92 } else { 93 mViewFlipper.setDisplayedChild(mViewFlipper.indexOfChild(findViewById(R.id.tabs))); 94 } 95 96 TabLayout tabLayout = getTabLayout(); 97 if (getNumberFragments() <= 1) { 98 tabLayout.setVisibility(View.GONE); 99 } else { 100 tabLayout.setVisibility(View.VISIBLE); 101 } 102 } 103 104 @Override onCreateOptionsMenu(Menu menu)105 public boolean onCreateOptionsMenu(Menu menu) { 106 MenuInflater inflater = getMenuInflater(); 107 inflater.inflate(R.menu.view_info_menu, menu); 108 return super.onCreateOptionsMenu(menu); 109 } 110 111 @Override onOptionsItemSelected(MenuItem item)112 public boolean onOptionsItemSelected(MenuItem item) { 113 switch (item.getItemId()) { 114 case R.id.action_edit: 115 Intent intent = new Intent(this, EditInfoActivity.class); 116 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 117 startActivity(intent); 118 return true; 119 } 120 return super.onOptionsItemSelected(item); 121 } 122 123 @Override setUpFragments()124 protected ArrayList<Pair<String, Fragment>> setUpFragments() { 125 // Return only the fragments that have at least one piece of information set: 126 ArrayList<Pair<String, Fragment>> fragments = new ArrayList<>(2); 127 128 if (ViewEmergencyInfoFragment.hasAtLeastOnePreferenceSet(this)) { 129 fragments.add(Pair.create(getResources().getString(R.string.tab_title_info), 130 ViewEmergencyInfoFragment.newInstance())); 131 } 132 if (ViewEmergencyContactsFragment.hasAtLeastOneEmergencyContact(this)) { 133 fragments.add(Pair.create(getResources().getString(R.string.tab_title_contacts), 134 ViewEmergencyContactsFragment.newInstance())); 135 } 136 return fragments; 137 } 138 } 139