EditInfoActivity.java revision 432ad365542a71f22c81c341592a16cde9e6be6f
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.Activity; 21import android.app.AlertDialog; 22import android.app.Dialog; 23import android.app.DialogFragment; 24import android.app.Fragment; 25import android.content.ComponentName; 26import android.content.DialogInterface; 27import android.content.SharedPreferences; 28import android.content.pm.PackageManager; 29import android.os.Bundle; 30import android.preference.PreferenceFragment; 31import android.preference.PreferenceManager; 32import android.util.Pair; 33import android.view.Menu; 34import android.view.MenuInflater; 35import android.view.MenuItem; 36 37import com.android.emergency.PreferenceKeys; 38import com.android.emergency.R; 39import com.android.emergency.overlay.FeatureFactory; 40import com.android.emergency.view.ViewInfoActivity; 41import com.android.internal.annotations.VisibleForTesting; 42import com.android.internal.logging.MetricsLogger; 43import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 44 45import java.util.ArrayList; 46 47/** 48 * Activity for editing emergency information. 49 */ 50public class EditInfoActivity extends Activity { 51 static final String TAG_CLEAR_ALL_DIALOG = "clear_all_dialog"; 52 53 private EditInfoFragment mEditInfoFragment; 54 55 @Override 56 protected void onCreate(Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 // Protect against b/28401242 by enabling ViewInfoActivity. 59 // We used to have code that disabled/enabled it and it could have been left in disabled 60 // state. 61 PackageManager pm = getPackageManager(); 62 pm.setComponentEnabledSetting(new ComponentName(this, ViewInfoActivity.class), 63 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP); 64 65 mEditInfoFragment = new EditInfoFragment(); 66 getFragmentManager().beginTransaction() 67 .replace(android.R.id.content, mEditInfoFragment) 68 .commit(); 69 70 getWindow().addFlags(FLAG_DISMISS_KEYGUARD); 71 MetricsLogger.visible(this, MetricsEvent.ACTION_EDIT_EMERGENCY_INFO); 72 } 73 74 @Override 75 public boolean onCreateOptionsMenu(Menu menu) { 76 MenuInflater inflater = getMenuInflater(); 77 inflater.inflate(R.menu.edit_info_menu, menu); 78 return true; 79 } 80 81 @Override 82 public boolean onOptionsItemSelected(MenuItem item) { 83 switch (item.getItemId()) { 84 case R.id.action_clear_all: 85 showClearAllDialog(); 86 return true; 87 } 88 return super.onOptionsItemSelected(item); 89 } 90 91 /** @return The single fragment managed by this activity. */ 92 @VisibleForTesting 93 public PreferenceFragment getFragment() { 94 return mEditInfoFragment; 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 mEditInfoFragment.reloadFromPreference(); 116 } 117 118 /** 119 * Dialog shown to the user when they tap on the CLEAR ALL menu item. Using a {@link 120 * DialogFragment} takes care of screen rotation issues. 121 */ 122 public static class ClearAllDialogFragment extends DialogFragment { 123 124 @Override 125 public Dialog onCreateDialog(Bundle savedInstanceState) { 126 Dialog dialog = new AlertDialog.Builder(getActivity()) 127 .setMessage(R.string.clear_all_message) 128 .setPositiveButton(R.string.clear, new DialogInterface.OnClickListener() { 129 @Override 130 public void onClick(DialogInterface dialog, int which) { 131 ((EditInfoActivity) getActivity()).onClearAllPreferences(); 132 } 133 }) 134 .setNegativeButton(android.R.string.cancel, null) 135 .create(); 136 return dialog; 137 } 138 139 public static DialogFragment newInstance() { 140 return new ClearAllDialogFragment(); 141 } 142 } 143} 144