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.AlertDialog;
21 import android.app.Dialog;
22 import android.app.DialogFragment;
23 import android.app.Fragment;
24 import android.content.ComponentName;
25 import android.content.DialogInterface;
26 import android.content.SharedPreferences;
27 import android.content.pm.PackageManager;
28 import android.os.Bundle;
29 import android.preference.PreferenceManager;
30 import android.util.Pair;
31 import android.view.Menu;
32 import android.view.MenuInflater;
33 import android.view.MenuItem;
34 
35 import com.android.emergency.EmergencyTabActivity;
36 import com.android.emergency.PreferenceKeys;
37 import com.android.emergency.R;
38 import com.android.emergency.view.ViewInfoActivity;
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 editing emergency information.
46  */
47 public class EditInfoActivity extends EmergencyTabActivity {
48     static final String TAG_WARNING_DIALOG = "warning_dialog";
49     static final String TAG_CLEAR_ALL_DIALOG = "clear_all_dialog";
50     static final String KEY_LAST_CONSENT_TIME_MS = "last_consent_time_ms";
51     static final long ONE_DAY_MS = 24 * 60 * 60 * 1000;
52     private static final String ACTION_EDIT_EMERGENCY_CONTACTS =
53             "android.emergency.EDIT_EMERGENCY_CONTACTS";
54 
55     @Override
onCreate(Bundle savedInstanceState)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         setContentView(R.layout.edit_activity_layout);
66         if (ACTION_EDIT_EMERGENCY_CONTACTS.equals(getIntent().getAction())) {
67             // Select emergency contacts tab
68             selectTab(1);
69         }
70 
71         getWindow().addFlags(FLAG_DISMISS_KEYGUARD);
72         MetricsLogger.visible(this, MetricsEvent.ACTION_EDIT_EMERGENCY_INFO);
73     }
74 
75     @Override
onResume()76     public void onResume() {
77         super.onResume();
78         long lastConsentTimeMs = PreferenceManager.getDefaultSharedPreferences(this)
79                 .getLong(KEY_LAST_CONSENT_TIME_MS, Long.MAX_VALUE);
80         long nowMs = System.currentTimeMillis();
81         // Check if at least one day has gone by since the user last gave his constant or if
82         // the last consent was in the future (e.g. if the user changed the date).
83         if (nowMs - lastConsentTimeMs > ONE_DAY_MS || lastConsentTimeMs > nowMs) {
84             showWarningDialog();
85         }
86     }
87 
88     @Override
onCreateOptionsMenu(Menu menu)89     public boolean onCreateOptionsMenu(Menu menu) {
90         MenuInflater inflater = getMenuInflater();
91         inflater.inflate(R.menu.edit_info_menu, menu);
92         return super.onCreateOptionsMenu(menu);
93     }
94 
95     @Override
onOptionsItemSelected(MenuItem item)96     public boolean onOptionsItemSelected(MenuItem item) {
97         switch (item.getItemId()) {
98             case R.id.action_clear_all:
99                 showClearAllDialog();
100                 return true;
101         }
102         return super.onOptionsItemSelected(item);
103     }
104 
105     @Override
setUpFragments()106     protected ArrayList<Pair<String, Fragment>> setUpFragments() {
107         // Always return the two fragments in edit mode.
108         ArrayList<Pair<String, Fragment>> fragments = new ArrayList<>(2);
109         fragments.add(Pair.create(getResources().getString(R.string.tab_title_info),
110                 EditEmergencyInfoFragment.newInstance()));
111         fragments.add(Pair.create(getResources().getString(R.string.tab_title_contacts),
112                 EditEmergencyContactsFragment.newInstance()));
113         return fragments;
114     }
115 
showWarningDialog()116     private void showWarningDialog() {
117         final WarningDialogFragment previousFragment =
118                 (WarningDialogFragment) getFragmentManager()
119                         .findFragmentByTag(EditInfoActivity.TAG_WARNING_DIALOG);
120 
121         if (previousFragment == null) {
122             DialogFragment newFragment = WarningDialogFragment.newInstance();
123             newFragment.setCancelable(false);
124             newFragment.show(getFragmentManager(), TAG_WARNING_DIALOG);
125         }
126     }
127 
showClearAllDialog()128     private void showClearAllDialog() {
129         final ClearAllDialogFragment previousFragment =
130                 (ClearAllDialogFragment) getFragmentManager()
131                         .findFragmentByTag(EditInfoActivity.TAG_CLEAR_ALL_DIALOG);
132         if (previousFragment == null) {
133             DialogFragment newFragment = ClearAllDialogFragment.newInstance();
134             newFragment.show(getFragmentManager(), TAG_CLEAR_ALL_DIALOG);
135         }
136     }
137 
onClearAllPreferences()138     private void onClearAllPreferences() {
139         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
140         for (String key : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) {
141             sharedPreferences.edit().remove(key).commit();
142         }
143         sharedPreferences.edit().remove(PreferenceKeys.KEY_EMERGENCY_CONTACTS).commit();
144 
145         // Refresh the UI.
146         ViewPagerAdapter adapter = getTabsAdapter();
147         if (adapter != null) {
148             adapter.notifyDataSetChanged();
149         }
150     }
151 
152     /**
153      * Warning dialog shown to the user each time they go in to the edit info view. Using a {@link
154      * DialogFragment} takes care of screen rotation issues.
155      */
156     public static class WarningDialogFragment extends DialogFragment {
157         @Override
onCreateDialog(Bundle savedInstanceState)158         public Dialog onCreateDialog(Bundle savedInstanceState) {
159             Dialog dialog = new AlertDialog.Builder(getActivity())
160                     .setTitle(R.string.user_emergency_info_title)
161                     .setMessage(R.string.user_emergency_info_consent)
162                     .setPositiveButton(R.string.emergency_info_continue,
163                             new DialogInterface.OnClickListener() {
164                                 @Override
165                                 public void onClick(DialogInterface dialog, int which) {
166                                     PreferenceManager.getDefaultSharedPreferences(
167                                             getActivity()).edit()
168                                             .putLong(KEY_LAST_CONSENT_TIME_MS,
169                                                     System.currentTimeMillis()).apply();
170                                 }
171                             })
172                     .setNegativeButton(android.R.string.cancel,
173                             new DialogInterface.OnClickListener() {
174                                 @Override
175                                 public void onClick(DialogInterface dialog, int which) {
176                                     getActivity().finish();
177                                 }
178                             })
179                     .create();
180             dialog.setCanceledOnTouchOutside(false);
181             return dialog;
182         }
183 
newInstance()184         public static DialogFragment newInstance() {
185             return new WarningDialogFragment();
186         }
187     }
188 
189     /**
190      * Dialog shown to the user when they tap on the CLEAR ALL menu item. Using a {@link
191      * DialogFragment} takes care of screen rotation issues.
192      */
193     public static class ClearAllDialogFragment extends DialogFragment {
194 
195         @Override
onCreateDialog(Bundle savedInstanceState)196         public Dialog onCreateDialog(Bundle savedInstanceState) {
197             Dialog dialog = new AlertDialog.Builder(getActivity())
198                     .setMessage(R.string.clear_all_message)
199                     .setPositiveButton(R.string.clear, new DialogInterface.OnClickListener() {
200                         @Override
201                         public void onClick(DialogInterface dialog, int which) {
202                             ((EditInfoActivity) getActivity()).onClearAllPreferences();
203                         }
204                     })
205                     .setNegativeButton(android.R.string.cancel, null)
206                     .create();
207             return dialog;
208         }
209 
newInstance()210         public static DialogFragment newInstance() {
211             return new ClearAllDialogFragment();
212         }
213     }
214 }
215