EditInfoActivity.java revision 7ee3df3c714b614ab28cef094a28543fb5e01465
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.AlertDialog; 21import android.app.Dialog; 22import android.app.DialogFragment; 23import android.app.Fragment; 24import android.app.FragmentTransaction; 25import android.content.ComponentName; 26import android.content.DialogInterface; 27import android.content.pm.PackageManager; 28import android.os.Bundle; 29import android.preference.PreferenceManager; 30import android.util.Pair; 31 32import com.android.emergency.EmergencyTabActivity; 33import com.android.emergency.R; 34import com.android.emergency.view.ViewEmergencyContactsFragment; 35import com.android.emergency.view.ViewInfoActivity; 36import com.android.internal.logging.MetricsLogger; 37import com.android.internal.logging.MetricsProto.MetricsEvent; 38 39import java.util.ArrayList; 40 41/** 42 * Activity for editing emergency information. 43 */ 44public class EditInfoActivity extends EmergencyTabActivity { 45 private static final String TAG_WARNING_DIALOG = "warning_dialog"; 46 private static final String KEY_LAST_CONSENT_TIME_MS = "last_consent_time_ms"; 47 private static final long ONE_DAY_MS = 24 * 60 * 60 * 1000; 48 private static final String ACTION_EDIT_EMERGENCY_CONTACTS = 49 "android.emergency.EDIT_EMERGENCY_CONTACTS"; 50 51 @Override 52 protected void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 setContentView(R.layout.edit_activity_layout); 55 56 // savedInstanceState is null on first start and non-null on restart. 57 // We want to show the dialog on first start, even if there is a screen rotation but avoid 58 // reshowing it if a rotation occurs (which causes onCreate to be called again, but this 59 // time with savedInstanceState!=null). 60 if (savedInstanceState == null) { 61 long lastConsentTimeMs = PreferenceManager.getDefaultSharedPreferences(this) 62 .getLong(KEY_LAST_CONSENT_TIME_MS, Long.MAX_VALUE); 63 long nowMs = System.currentTimeMillis(); 64 // Check if at least one day has gone by since the user last gave his constant or if 65 // the last consent was in the future (e.g. if the user changed the date). 66 if (nowMs - lastConsentTimeMs > ONE_DAY_MS || lastConsentTimeMs > nowMs) { 67 showWarningDialog(); 68 } 69 } 70 71 if (ACTION_EDIT_EMERGENCY_CONTACTS.equals(getIntent().getAction())) { 72 // Select emergency contacts tab 73 selectTab(1); 74 } 75 76 getWindow().addFlags(FLAG_DISMISS_KEYGUARD); 77 MetricsLogger.visible(this, MetricsEvent.ACTION_EDIT_EMERGENCY_INFO); 78 } 79 80 @Override 81 public void onPause() { 82 super.onPause(); 83 84 // Enable ViewInfoActivity if the user input some info. Otherwise, disable it. 85 PackageManager pm = getPackageManager(); 86 if (ViewEmergencyContactsFragment.hasAtLeastOneEmergencyContact(this) 87 || EditEmergencyInfoFragment.hasAtLeastOnePreferenceSet(this)) { 88 pm.setComponentEnabledSetting(new ComponentName(this, ViewInfoActivity.class), 89 PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); 90 } else { 91 pm.setComponentEnabledSetting(new ComponentName(this, ViewInfoActivity.class), 92 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 93 } 94 } 95 96 @Override 97 public boolean isInViewMode() { 98 return false; 99 } 100 101 @Override 102 public String getActivityTitle() { 103 return getString(R.string.edit_emergency_info_label); 104 } 105 106 @Override 107 protected ArrayList<Pair<String, Fragment>> setUpFragments() { 108 // Always return the two fragments in edit mode. 109 ArrayList<Pair<String, Fragment>> fragments = new ArrayList<>(2); 110 fragments.add(Pair.create(getResources().getString(R.string.tab_title_info), 111 EditEmergencyInfoFragment.newInstance())); 112 fragments.add(Pair.create(getResources().getString(R.string.tab_title_contacts), 113 EditEmergencyContactsFragment.newInstance())); 114 return fragments; 115 } 116 117 private void showWarningDialog() { 118 // DialogFragment.show() will take care of adding the fragment 119 // in a transaction. We also want to remove any currently showing 120 // dialog, so make our own transaction and take care of that here. 121 FragmentTransaction ft = getFragmentManager().beginTransaction(); 122 Fragment previousDialog = getFragmentManager().findFragmentByTag(TAG_WARNING_DIALOG); 123 if (previousDialog != null) { 124 ft.remove(previousDialog); 125 } 126 ft.addToBackStack(null); 127 128 DialogFragment newFragment = WarningDialogFragment.newInstance(); 129 newFragment.setCancelable(false); 130 newFragment.show(ft, TAG_WARNING_DIALOG); 131 } 132 133 /** 134 * Warning dialog shown to the user each time they go in to the edit info view. Using a {@link 135 * DialogFragment} takes care of screen rotation issues. 136 */ 137 public static class WarningDialogFragment extends DialogFragment { 138 @Override 139 public Dialog onCreateDialog(Bundle savedInstanceState) { 140 Dialog dialog = new AlertDialog.Builder(getActivity()) 141 .setTitle(R.string.user_emergency_info_title) 142 .setMessage(R.string.user_emergency_info_consent) 143 .setPositiveButton(R.string.emergency_info_continue, 144 new DialogInterface.OnClickListener() { 145 @Override 146 public void onClick(DialogInterface dialog, int which) { 147 PreferenceManager.getDefaultSharedPreferences( 148 getActivity()).edit() 149 .putLong(KEY_LAST_CONSENT_TIME_MS, 150 System.currentTimeMillis()).apply(); 151 } 152 }) 153 .setNegativeButton(android.R.string.cancel, 154 new DialogInterface.OnClickListener() { 155 @Override 156 public void onClick(DialogInterface dialog, int which) { 157 getActivity().finish(); 158 } 159 }) 160 .create(); 161 dialog.setCanceledOnTouchOutside(false); 162 return dialog; 163 } 164 165 public static DialogFragment newInstance() { 166 return new WarningDialogFragment(); 167 } 168 } 169} 170