Merge "QS: Fix missing flashlight tile" into nyc-mr1-dev
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightController.java
index 9a21a1e..91b21ed 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightController.java
@@ -17,6 +17,7 @@
 package com.android.systemui.statusbar.policy;
 
 import android.content.Context;
+import android.content.pm.PackageManager;
 import android.hardware.camera2.CameraAccessException;
 import android.hardware.camera2.CameraCharacteristics;
 import android.hardware.camera2.CameraManager;
@@ -42,6 +43,7 @@
     private static final int DISPATCH_AVAILABILITY_CHANGED = 2;
 
     private final CameraManager mCameraManager;
+    private final Context mContext;
     /** Call {@link #ensureHandler()} before using */
     private Handler mHandler;
 
@@ -51,20 +53,22 @@
     /** Lock on {@code this} when accessing */
     private boolean mFlashlightEnabled;
 
-    private final String mCameraId;
+    private String mCameraId;
     private boolean mTorchAvailable;
 
-    public FlashlightController(Context mContext) {
+    public FlashlightController(Context context) {
+        mContext = context;
         mCameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
 
-        String cameraId = null;
+        tryInitCamera();
+    }
+
+    private void tryInitCamera() {
         try {
-            cameraId = getCameraId();
+            mCameraId = getCameraId();
         } catch (Throwable e) {
             Log.e(TAG, "Couldn't initialize.", e);
             return;
-        } finally {
-            mCameraId = cameraId;
         }
 
         if (mCameraId != null) {
@@ -94,7 +98,7 @@
     }
 
     public boolean hasFlashlight() {
-        return mCameraId != null;
+        return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
     }
 
     public synchronized boolean isEnabled() {
@@ -107,6 +111,9 @@
 
     public void addListener(FlashlightListener l) {
         synchronized (mListeners) {
+            if (mCameraId == null) {
+                tryInitCamera();
+            }
             cleanUpListenersLocked(l);
             mListeners.add(new WeakReference<>(l));
         }