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.edit;
17 
18 import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
19 
20 import android.app.Activity;
21 import android.app.AlertDialog;
22 import android.app.Dialog;
23 import android.app.DialogFragment;
24 import android.app.Fragment;
25 import android.content.ComponentName;
26 import android.content.DialogInterface;
27 import android.content.SharedPreferences;
28 import android.content.pm.PackageManager;
29 import android.os.Bundle;
30 import androidx.preference.PreferenceFragment;
31 import androidx.preference.PreferenceManager;
32 import android.util.Pair;
33 import android.view.Menu;
34 import android.view.MenuInflater;
35 import android.view.MenuItem;
36 
37 import com.android.emergency.PreferenceKeys;
38 import com.android.emergency.R;
39 import com.android.emergency.overlay.FeatureFactory;
40 import com.android.emergency.util.PreferenceUtils;
41 import com.android.emergency.view.ViewInfoActivity;
42 import com.android.internal.annotations.VisibleForTesting;
43 import com.android.internal.logging.MetricsLogger;
44 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
45 
46 import java.util.ArrayList;
47 
48 /**
49  * Activity for editing emergency information.
50  */
51 public class EditInfoActivity extends Activity {
52     static final String TAG_CLEAR_ALL_DIALOG = "clear_all_dialog";
53 
54     private EditInfoFragment mEditInfoFragment;
55 
56     @Override
onCreate(Bundle savedInstanceState)57     protected void onCreate(Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59         // Protect against b/28401242 by enabling ViewInfoActivity.
60         // We used to have code that disabled/enabled it and it could have been left in disabled
61         // state.
62         PackageManager pm = getPackageManager();
63         pm.setComponentEnabledSetting(new ComponentName(this, ViewInfoActivity.class),
64                 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
65 
66         getActionBar().setDisplayHomeAsUpEnabled(true);
67 
68         // We only add a new EditInfoFragment if no fragment is restored.
69         Fragment fragment = getFragmentManager().findFragmentById(android.R.id.content);
70         if (fragment == null) {
71             mEditInfoFragment = new EditInfoFragment();
72             getFragmentManager().beginTransaction()
73                 .add(android.R.id.content, mEditInfoFragment)
74                 .commit();
75         } else {
76             mEditInfoFragment = (EditInfoFragment) fragment;
77         }
78 
79         // Show or hide the settings suggestion, depending on whether any emergency settings exist.
80         PreferenceUtils.updateSettingsSuggestionState(this);
81 
82         getWindow().addFlags(FLAG_DISMISS_KEYGUARD);
83         MetricsLogger.visible(this, MetricsEvent.ACTION_EDIT_EMERGENCY_INFO);
84     }
85 
86     @Override
onCreateOptionsMenu(Menu menu)87     public boolean onCreateOptionsMenu(Menu menu) {
88         MenuInflater inflater = getMenuInflater();
89         inflater.inflate(R.menu.edit_info_menu, menu);
90         return true;
91     }
92 
93     @Override
onOptionsItemSelected(MenuItem item)94     public boolean onOptionsItemSelected(MenuItem item) {
95         int itemId = item.getItemId();
96         if (itemId == android.R.id.home) {
97             // The user asked to navigate up, which, in this case, can easily be accomplished
98             // by finishing the activity.
99             finish();
100             return true;
101         } else if (itemId == R.id.action_clear_all) {
102             showClearAllDialog();
103             return true;
104         }
105         return super.onOptionsItemSelected(item);
106     }
107 
108     /** @return The single fragment managed by this activity. */
109     @VisibleForTesting
getFragment()110     public PreferenceFragment getFragment() {
111         return mEditInfoFragment;
112     }
113 
showClearAllDialog()114     private void showClearAllDialog() {
115         final ClearAllDialogFragment previousFragment =
116                 (ClearAllDialogFragment) getFragmentManager()
117                         .findFragmentByTag(EditInfoActivity.TAG_CLEAR_ALL_DIALOG);
118         if (previousFragment == null) {
119             DialogFragment newFragment = ClearAllDialogFragment.newInstance();
120             newFragment.show(getFragmentManager(), TAG_CLEAR_ALL_DIALOG);
121         }
122     }
123 
onClearAllPreferences()124     private void onClearAllPreferences() {
125         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
126         for (String key : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) {
127             sharedPreferences.edit().remove(key).commit();
128         }
129         sharedPreferences.edit().remove(PreferenceKeys.KEY_EMERGENCY_CONTACTS).commit();
130         // Show the settings suggestion again, since no emergency info is set.
131         PreferenceUtils.enableSettingsSuggestion(this);
132 
133         // Refresh the UI.
134         mEditInfoFragment.reloadFromPreference();
135     }
136 
137     /**
138      * Dialog shown to the user when they tap on the CLEAR ALL menu item. Using a {@link
139      * DialogFragment} takes care of screen rotation issues.
140      */
141     public static class ClearAllDialogFragment extends DialogFragment {
142 
143         @Override
onCreateDialog(Bundle savedInstanceState)144         public Dialog onCreateDialog(Bundle savedInstanceState) {
145             Dialog dialog = new AlertDialog.Builder(getActivity())
146                     .setMessage(R.string.clear_all_message)
147                     .setPositiveButton(R.string.clear, new DialogInterface.OnClickListener() {
148                         @Override
149                         public void onClick(DialogInterface dialog, int which) {
150                             ((EditInfoActivity) getActivity()).onClearAllPreferences();
151                         }
152                     })
153                     .setNegativeButton(android.R.string.cancel, null)
154                     .create();
155             return dialog;
156         }
157 
newInstance()158         public static DialogFragment newInstance() {
159             return new ClearAllDialogFragment();
160         }
161     }
162 }
163