EditInfoActivity.java revision df83424c06ae06bbf3360b9a9f0c3bc6ab2dce4d
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 */ 16package com.android.emergency.edit; 17 18import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD; 19 20import android.app.AlertDialog; 21import android.app.Dialog; 22import android.app.DialogFragment; 23import android.app.Fragment; 24import android.content.ComponentName; 25import android.content.DialogInterface; 26import android.content.SharedPreferences; 27import android.content.pm.PackageManager; 28import android.os.Bundle; 29import android.preference.PreferenceManager; 30import android.util.Pair; 31import android.view.Menu; 32import android.view.MenuInflater; 33import android.view.MenuItem; 34 35import com.android.emergency.EmergencyTabActivity; 36import com.android.emergency.PreferenceKeys; 37import com.android.emergency.R; 38import com.android.emergency.overlay.FeatureFactory; 39import com.android.emergency.view.ViewInfoActivity; 40import com.android.internal.logging.MetricsLogger; 41import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 42 43import java.util.ArrayList; 44 45/** 46 * Activity for editing emergency information. 47 */ 48public class EditInfoActivity extends EmergencyTabActivity { 49 static final String TAG_CLEAR_ALL_DIALOG = "clear_all_dialog"; 50 51 @Override 52 protected void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 // Protect against b/28401242 by enabling ViewInfoActivity. 55 // We used to have code that disabled/enabled it and it could have been left in disabled 56 // state. 57 PackageManager pm = getPackageManager(); 58 pm.setComponentEnabledSetting(new ComponentName(this, ViewInfoActivity.class), 59 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP); 60 61 setContentView(R.layout.edit_activity_layout); 62 63 getWindow().addFlags(FLAG_DISMISS_KEYGUARD); 64 MetricsLogger.visible(this, MetricsEvent.ACTION_EDIT_EMERGENCY_INFO); 65 } 66 67 @Override 68 public boolean onCreateOptionsMenu(Menu menu) { 69 MenuInflater inflater = getMenuInflater(); 70 inflater.inflate(R.menu.edit_info_menu, menu); 71 return true; 72 } 73 74 @Override 75 public boolean onOptionsItemSelected(MenuItem item) { 76 switch (item.getItemId()) { 77 case R.id.action_clear_all: 78 showClearAllDialog(); 79 return true; 80 } 81 return super.onOptionsItemSelected(item); 82 } 83 84 @Override 85 protected ArrayList<Pair<String, Fragment>> setUpFragments() { 86 FeatureFactory featureFactory = FeatureFactory.getFactory(this); 87 88 // Always return the two fragments in edit mode. 89 ArrayList<Pair<String, Fragment>> fragments = new ArrayList<>(2); 90 fragments.add(Pair.create(getResources().getString(R.string.tab_title_info), 91 EditEmergencyInfoFragment.newInstance())); 92 fragments.add(Pair.create(getResources().getString(R.string.tab_title_contacts), 93 featureFactory.getEmergencyContactsFeatureProvider().createEditContactsFragment())); 94 return fragments; 95 } 96 97 private void showClearAllDialog() { 98 final ClearAllDialogFragment previousFragment = 99 (ClearAllDialogFragment) getFragmentManager() 100 .findFragmentByTag(EditInfoActivity.TAG_CLEAR_ALL_DIALOG); 101 if (previousFragment == null) { 102 DialogFragment newFragment = ClearAllDialogFragment.newInstance(); 103 newFragment.show(getFragmentManager(), TAG_CLEAR_ALL_DIALOG); 104 } 105 } 106 107 private void onClearAllPreferences() { 108 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 109 for (String key : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) { 110 sharedPreferences.edit().remove(key).commit(); 111 } 112 sharedPreferences.edit().remove(PreferenceKeys.KEY_EMERGENCY_CONTACTS).commit(); 113 114 // Refresh the UI. 115 ViewPagerAdapter adapter = getTabsAdapter(); 116 if (adapter != null) { 117 adapter.notifyDataSetChanged(); 118 } 119 } 120 121 /** 122 * Dialog shown to the user when they tap on the CLEAR ALL menu item. Using a {@link 123 * DialogFragment} takes care of screen rotation issues. 124 */ 125 public static class ClearAllDialogFragment extends DialogFragment { 126 127 @Override 128 public Dialog onCreateDialog(Bundle savedInstanceState) { 129 Dialog dialog = new AlertDialog.Builder(getActivity()) 130 .setMessage(R.string.clear_all_message) 131 .setPositiveButton(R.string.clear, new DialogInterface.OnClickListener() { 132 @Override 133 public void onClick(DialogInterface dialog, int which) { 134 ((EditInfoActivity) getActivity()).onClearAllPreferences(); 135 } 136 }) 137 .setNegativeButton(android.R.string.cancel, null) 138 .create(); 139 return dialog; 140 } 141 142 public static DialogFragment newInstance() { 143 return new ClearAllDialogFragment(); 144 } 145 } 146} 147