Remove duplicated code and store last consent time

Bug:28076797
Change-Id: I4559f1ee202cc56a30f26f80774017a5eb06b6cf
diff --git a/src/com/android/emergency/BootReceiver.java b/src/com/android/emergency/BootReceiver.java
index 2a2232c..d2dc114 100644
--- a/src/com/android/emergency/BootReceiver.java
+++ b/src/com/android/emergency/BootReceiver.java
@@ -16,13 +16,9 @@
 package com.android.emergency;
 
 import android.content.BroadcastReceiver;
-import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
-import android.content.pm.PackageManager;
 
-import com.android.emergency.edit.EditEmergencyInfoFragment;
-import com.android.emergency.view.ViewEmergencyContactsFragment;
 import com.android.emergency.view.ViewInfoActivity;
 
 /**
@@ -32,15 +28,8 @@
 public class BootReceiver extends BroadcastReceiver {
     @Override
     public void onReceive(Context context, Intent intent) {
-        // Enable ViewInfoActivity if the user input some info. Otherwise, disable it.
-        PackageManager pm = context.getPackageManager();
-        if (ViewEmergencyContactsFragment.hasAtLeastOneEmergencyContact(context)
-                || EditEmergencyInfoFragment.hasAtLeastOnePreferenceSet(context)) {
-            pm.setComponentEnabledSetting(new ComponentName(context, ViewInfoActivity.class),
-                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
-        } else {
-            pm.setComponentEnabledSetting(new ComponentName(context, ViewInfoActivity.class),
-                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
+        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
+            PackageManagerUtils.disableViewInfoActivityIfNoInfoAvailable(context);
         }
     }
 }
diff --git a/src/com/android/emergency/PackageManagerUtils.java b/src/com/android/emergency/PackageManagerUtils.java
new file mode 100644
index 0000000..f392061
--- /dev/null
+++ b/src/com/android/emergency/PackageManagerUtils.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.emergency;
+
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.pm.PackageManager;
+
+import com.android.emergency.edit.EditEmergencyInfoFragment;
+import com.android.emergency.view.ViewEmergencyContactsFragment;
+import com.android.emergency.view.ViewInfoActivity;
+
+/**
+ * Utils related to {@link PackageManager}.
+ */
+public class PackageManagerUtils {
+
+    /**
+     * Disables {@link ViewInfoActivity} if no emergency information or emergency contacts were
+     * input by the user. Otherwise, it enables it.
+     */
+    public static void disableViewInfoActivityIfNoInfoAvailable(Context context) {
+        // Enable ViewInfoActivity if the user input some info. Otherwise, disable it.
+        PackageManager pm = context.getPackageManager();
+        if (ViewEmergencyContactsFragment.hasAtLeastOneEmergencyContact(context)
+                || EditEmergencyInfoFragment.hasAtLeastOnePreferenceSet(context)) {
+            pm.setComponentEnabledSetting(new ComponentName(context, ViewInfoActivity.class),
+                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
+        } else {
+            pm.setComponentEnabledSetting(new ComponentName(context, ViewInfoActivity.class),
+                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
+        }
+    }
+
+    private PackageManagerUtils() {
+        // Prevent instantiation
+        throw new UnsupportedOperationException();
+    }
+}
diff --git a/src/com/android/emergency/edit/EditInfoActivity.java b/src/com/android/emergency/edit/EditInfoActivity.java
index fc0aa57..0931fcc 100644
--- a/src/com/android/emergency/edit/EditInfoActivity.java
+++ b/src/com/android/emergency/edit/EditInfoActivity.java
@@ -21,10 +21,9 @@
 import android.app.Dialog;
 import android.app.DialogFragment;
 import android.app.Fragment;
-import android.content.ComponentName;
 import android.content.DialogInterface;
 import android.content.Intent;
-import android.content.pm.PackageManager;
+import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
 import android.util.Pair;
@@ -33,9 +32,9 @@
 import android.view.MenuItem;
 
 import com.android.emergency.EmergencyTabActivity;
+import com.android.emergency.PackageManagerUtils;
+import com.android.emergency.PreferenceKeys;
 import com.android.emergency.R;
-import com.android.emergency.view.ViewEmergencyContactsFragment;
-import com.android.emergency.view.ViewInfoActivity;
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.MetricsProto.MetricsEvent;
 
@@ -83,16 +82,7 @@
     @Override
     public void onPause() {
         super.onPause();
-        // Enable ViewInfoActivity if the user input some info. Otherwise, disable it.
-        PackageManager pm = getPackageManager();
-        if (ViewEmergencyContactsFragment.hasAtLeastOneEmergencyContact(this)
-                || EditEmergencyInfoFragment.hasAtLeastOnePreferenceSet(this)) {
-            pm.setComponentEnabledSetting(new ComponentName(this, ViewInfoActivity.class),
-                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
-        } else {
-            pm.setComponentEnabledSetting(new ComponentName(this, ViewInfoActivity.class),
-                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
-        }
+        PackageManagerUtils.disableViewInfoActivityIfNoInfoAvailable(this);
     }
 
     @Override
@@ -151,8 +141,13 @@
     }
 
     private void onClearAllPreferences() {
-        PreferenceManager.getDefaultSharedPreferences(this).edit().clear().commit();
+        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
+        for (String key : PreferenceKeys.KEYS_EDIT_EMERGENCY_INFO) {
+            sharedPreferences.edit().remove(key).commit();
+        }
+        sharedPreferences.edit().remove(PreferenceKeys.KEY_EMERGENCY_CONTACTS).commit();
 
+        // Refresh the UI.
         ArrayList<Pair<String, Fragment>> fragments = getFragments();
         EditEmergencyInfoFragment editEmergencyInfoFragment =
                 (EditEmergencyInfoFragment) fragments.get(0).second;