• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.support.v14.preference.PreferenceFragment;
31 import android.support.v7.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         switch (item.getItemId()) {
96             case 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 
102             case R.id.action_clear_all:
103                 showClearAllDialog();
104                 return true;
105         }
106         return super.onOptionsItemSelected(item);
107     }
108 
109     /** @return The single fragment managed by this activity. */
110     @VisibleForTesting
getFragment()111     public PreferenceFragment getFragment() {
112         return mEditInfoFragment;
113     }
114 
showClearAllDialog()115     private void showClearAllDialog() {
116         final ClearAllDialogFragment previousFragment =
117                 (ClearAllDialogFragment) getFragmentManager()
118                         .findFragmentByTag(EditInfoActivity.TAG_CLEAR_ALL_DIALOG);
119         if (previousFragment == null) {
120             DialogFragment newFragment = ClearAllDialogFragment.newInstance();
121             newFragment.show(getFragmentManager(), TAG_CLEAR_ALL_DIALOG);
122         }
123     }
124 
onClearAllPreferences()125     private void onClearAllPreferences() {
126         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
127         for (String key : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) {
128             sharedPreferences.edit().remove(key).commit();
129         }
130         sharedPreferences.edit().remove(PreferenceKeys.KEY_EMERGENCY_CONTACTS).commit();
131         // Show the settings suggestion again, since no emergency info is set.
132         PreferenceUtils.enableSettingsSuggestion(this);
133 
134         // Refresh the UI.
135         mEditInfoFragment.reloadFromPreference();
136     }
137 
138     /**
139      * Dialog shown to the user when they tap on the CLEAR ALL menu item. Using a {@link
140      * DialogFragment} takes care of screen rotation issues.
141      */
142     public static class ClearAllDialogFragment extends DialogFragment {
143 
144         @Override
onCreateDialog(Bundle savedInstanceState)145         public Dialog onCreateDialog(Bundle savedInstanceState) {
146             Dialog dialog = new AlertDialog.Builder(getActivity())
147                     .setMessage(R.string.clear_all_message)
148                     .setPositiveButton(R.string.clear, new DialogInterface.OnClickListener() {
149                         @Override
150                         public void onClick(DialogInterface dialog, int which) {
151                             ((EditInfoActivity) getActivity()).onClearAllPreferences();
152                         }
153                     })
154                     .setNegativeButton(android.R.string.cancel, null)
155                     .create();
156             return dialog;
157         }
158 
newInstance()159         public static DialogFragment newInstance() {
160             return new ClearAllDialogFragment();
161         }
162     }
163 }
164