Disable ViewInfoActivity on boot

Bug:28054211
Change-Id: Ia1df07fece2b7f41b44710d4abc5af9768083502
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 2dd6059..f14c4b1 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -20,6 +20,7 @@
     <uses-permission android:name="android.permission.CALL_PHONE" />
     <uses-permission android:name="android.permission.READ_CONTACTS" />
     <uses-permission android:name="android.permission.MANAGE_USERS" />
+    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 
     <application
         android:defaultToDeviceProtectedStorage="true"
@@ -55,6 +56,14 @@
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
         </activity>
+
+        <receiver
+            android:name=".BootReceiver">
+            <intent-filter>
+                <action android:name="android.intent.action.BOOT_COMPLETED" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </receiver>
     </application>
 
 </manifest>
diff --git a/src/com/android/emergency/BootReceiver.java b/src/com/android/emergency/BootReceiver.java
new file mode 100644
index 0000000..2a2232c
--- /dev/null
+++ b/src/com/android/emergency/BootReceiver.java
@@ -0,0 +1,46 @@
+/*
+ * 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.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;
+
+/**
+ * Broadcast receiver which handles the BOOT_COMPLETED intent and enables or disables
+ * {@link ViewInfoActivity} depending on information being available.
+ */
+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);
+        }
+    }
+}