/*
 * Copyright 2016 Eric Peterson
 *
 * 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 co.ericp.flashlight;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.widget.Toast;

/**
 * A {@link Flashlight} for the current API.
 */
@TargetApi(Build.VERSION_CODES.M)
public class FlashlightImpl implements Flashlight {

    private final Application app;
    private final CameraManager cameraManager;
    private String cameraId;
    private boolean isOn;

    public FlashlightImpl(Context c) {
        app = (Application) c.getApplicationContext();
        cameraManager = (CameraManager) app.getSystemService(Context.CAMERA_SERVICE);

        try {
            for (String id : cameraManager.getCameraIdList()) {
                CameraCharacteristics characteristics =
                        cameraManager.getCameraCharacteristics(id);

                Boolean hasCamera =
                        characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);

                if (hasCamera != null && hasCamera) {
                    cameraId = id;
                    break;
                }
            }
        } catch (CameraAccessException e) {
            Toast.makeText(c, R.string.not_available, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void toggle() {
        try {
            cameraManager.setTorchMode(cameraId, !isOn);
            isOn = !isOn;
            if (!isOn) FlashlightProvider.clear();
        } catch (CameraAccessException e) {
            Toast.makeText(app, R.string.not_available, Toast.LENGTH_LONG).show();
        }
    }
}

@@ -899,16 +899,130 @@
 oid.
-os.Build
+hardware.camera2.CameraManager.TorchCallback;%0Aimport android.os.Build;%0Aimport android.os.Handler;%0Aimport android.os.Looper
 ;%0Aim
@@ -1327,16 +1327,308 @@
  isOn;%0A%0A
+    private final TorchCallback torchCallback = new TorchCallback() %7B%0A        @Override%0A        public void onTorchModeChanged(String cameraId, boolean enabled) %7B%0A            if (cameraId.equals(FlashlightImpl.this.cameraId)) %7B%0A                isOn = enabled;%0A            %7D%0A        %7D%0A    %7D;%0A%0A
     publ
@@ -2269,32 +2269,191 @@
 %7D%0A            %7D%0A
+%0A            Handler callbackHandler = new Handler(Looper.getMainLooper());%0A%0A            cameraManager.registerTorchCallback(torchCallback, callbackHandler);%0A%0A
         %7D catch 
@@ -2738,16 +2738,34 @@
  (!isOn)
+ %7B%0A               
  Flashli
@@ -2781,24 +2781,108 @@
 er.clear();%0A
+                cameraManager.unregisterTorchCallback(torchCallback);%0A            %7D%0A
         %7D ca

Fix bug where you have to tap twice after another application disabled the torch