Home | History | Annotate | Download | only in edit
      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.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
     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
     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
     89     public boolean onCreateOptionsMenu(Menu menu) {
     90         MenuInflater inflater = getMenuInflater();
     91         inflater.inflate(R.menu.edit_info_menu, menu);
     92         return true;
     93     }
     94 
     95     @Override
     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
    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 
    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 
    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 
    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         ArrayList<Pair<String, Fragment>> fragments = getFragments();
    147         EditEmergencyInfoFragment editEmergencyInfoFragment =
    148                 (EditEmergencyInfoFragment) fragments.get(0).second;
    149         editEmergencyInfoFragment.reloadFromPreference();
    150         EditEmergencyContactsFragment editEmergencyContactsFragment =
    151                 (EditEmergencyContactsFragment) fragments.get(1).second;
    152         editEmergencyContactsFragment.reloadFromPreference();
    153     }
    154 
    155     /**
    156      * Warning dialog shown to the user each time they go in to the edit info view. Using a {@link
    157      * DialogFragment} takes care of screen rotation issues.
    158      */
    159     public static class WarningDialogFragment extends DialogFragment {
    160         @Override
    161         public Dialog onCreateDialog(Bundle savedInstanceState) {
    162             Dialog dialog = new AlertDialog.Builder(getActivity())
    163                     .setTitle(R.string.user_emergency_info_title)
    164                     .setMessage(R.string.user_emergency_info_consent)
    165                     .setPositiveButton(R.string.emergency_info_continue,
    166                             new DialogInterface.OnClickListener() {
    167                                 @Override
    168                                 public void onClick(DialogInterface dialog, int which) {
    169                                     PreferenceManager.getDefaultSharedPreferences(
    170                                             getActivity()).edit()
    171                                             .putLong(KEY_LAST_CONSENT_TIME_MS,
    172                                                     System.currentTimeMillis()).apply();
    173                                 }
    174                             })
    175                     .setNegativeButton(android.R.string.cancel,
    176                             new DialogInterface.OnClickListener() {
    177                                 @Override
    178                                 public void onClick(DialogInterface dialog, int which) {
    179                                     getActivity().finish();
    180                                 }
    181                             })
    182                     .create();
    183             dialog.setCanceledOnTouchOutside(false);
    184             return dialog;
    185         }
    186 
    187         public static DialogFragment newInstance() {
    188             return new WarningDialogFragment();
    189         }
    190     }
    191 
    192     /**
    193      * Dialog shown to the user when they tap on the CLEAR ALL menu item. Using a {@link
    194      * DialogFragment} takes care of screen rotation issues.
    195      */
    196     public static class ClearAllDialogFragment extends DialogFragment {
    197 
    198         @Override
    199         public Dialog onCreateDialog(Bundle savedInstanceState) {
    200             Dialog dialog = new AlertDialog.Builder(getActivity())
    201                     .setMessage(R.string.clear_all_message)
    202                     .setPositiveButton(R.string.clear, new DialogInterface.OnClickListener() {
    203                         @Override
    204                         public void onClick(DialogInterface dialog, int which) {
    205                             ((EditInfoActivity) getActivity()).onClearAllPreferences();
    206                         }
    207                     })
    208                     .setNegativeButton(android.R.string.cancel, null)
    209                     .create();
    210             return dialog;
    211         }
    212 
    213         public static DialogFragment newInstance() {
    214             return new ClearAllDialogFragment();
    215         }
    216     }
    217 }
    218