AOSP/Camera2: there is no camera icon shown if have no camera in first boot up

image below scene:
there is only one external usb camera connect to board, and the usb camera is not connected
in first boot up. the camera will not show again even you connect the usb camera to board again in later boot up.

the old logic is:
system will call DisableCameraReceiver when boot complete.
If there is no valid camera reported from camera hal in first boot up, the camera icon will not show again.
It will disable component DisableCameraReceiver in first boot up.

new logic after apply this patch:
system will decide whether show camera icon according that system have valid camera in every boot up rather
than first boot up.

Bug: 160797712
Signed-off-by: zhang sanshan <pete.zhang@nxp.com>
Change-Id: I49ac01feeb7367aa97bed087e7cfba6af6cc05c7
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 751d08a..e4b175a 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -156,7 +156,7 @@
             android:configChanges="keyboardHidden|orientation|screenSize">
         </activity>
 
-        <receiver android:name="com.android.camera.DisableCameraReceiver"
+        <receiver android:name="com.android.camera.SetActivitiesCameraReceiver"
             android:exported="true">
             <intent-filter>
                 <action android:name="android.intent.action.BOOT_COMPLETED" />
diff --git a/src/com/android/camera/DisableCameraReceiver.java b/src/com/android/camera/SetActivitiesCameraReceiver.java
similarity index 72%
rename from src/com/android/camera/DisableCameraReceiver.java
rename to src/com/android/camera/SetActivitiesCameraReceiver.java
index ed58783..7a79c52 100644
--- a/src/com/android/camera/DisableCameraReceiver.java
+++ b/src/com/android/camera/SetActivitiesCameraReceiver.java
@@ -25,11 +25,10 @@
 
 import com.android.camera.debug.Log;
 
-// We want to disable camera-related activities if there is no camera. This
-// receiver runs when BOOT_COMPLETED intent is received. After running once
-// this receiver will be disabled, so it will not run again.
-public class DisableCameraReceiver extends BroadcastReceiver {
-    private static final Log.Tag TAG = new Log.Tag("DisableCamRcver");
+// Enable or disable camera-related activities based on "camera hal" in every boot up.
+// This receiver runs when BOOT_COMPLETED intent is received.
+public class SetActivitiesCameraReceiver extends BroadcastReceiver {
+    private static final Log.Tag TAG = new Log.Tag("SetActivitiesCameraReceiver");
     private static final boolean CHECK_BACK_CAMERA_ONLY = false;
     private static final String ACTIVITIES[] = {
         "com.android.camera.CameraLauncher",
@@ -38,19 +37,17 @@
     @Override
     public void onReceive(Context context, Intent intent) {
         // Disable camera-related activities if there is no camera.
-        boolean needCameraActivity = CHECK_BACK_CAMERA_ONLY
+        int component_state = (CHECK_BACK_CAMERA_ONLY
             ? hasBackCamera()
-            : hasCamera();
+            : (hasCamera() || supportExternalCamera(context)))
+            ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
+            : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
 
-        if (!needCameraActivity && !supportExternalCamera(context)) {
-            Log.i(TAG, "disable all camera activities");
-            for (int i = 0; i < ACTIVITIES.length; i++) {
-                disableComponent(context, ACTIVITIES[i]);
-            }
+        Log.i(TAG, "component state is " + component_state);
+        for (int i = 0; i < ACTIVITIES.length; i++) {
+            setComponent(context, ACTIVITIES[i],
+                component_state);
         }
-
-        // Disable this receiver so it won't run again.
-        disableComponent(context, "com.android.camera.DisableCameraReceiver");
     }
 
     private boolean supportExternalCamera(Context context) {
@@ -78,14 +75,14 @@
         return false;
     }
 
-    private void disableComponent(Context context, String klass) {
+    private void setComponent(Context context, String klass, final int enabledState) {
         ComponentName name = new ComponentName(context, klass);
         PackageManager pm = context.getPackageManager();
 
         // We need the DONT_KILL_APP flag, otherwise we will be killed
         // immediately because we are in the same app.
         pm.setComponentEnabledSetting(name,
-            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
+            enabledState,
             PackageManager.DONT_KILL_APP);
     }
 }