Android Open Source - androidtool Torch Helper






From Project

Back to project page androidtool.

License

The source code is released under:

MIT License

If you think the Android project androidtool listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.chris.android.tool;
/*from   ww  w  .j  av a 2s  . co m*/
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

public class TorchHelper {

    private static final Logger LOG = LoggerFactory.getLogger(TorchHelper.class);
    private Camera camera;
    private final Context context;

    private TorchHelper(Context context, Camera camera, final SurfaceHolder holder) {
        this.context = context;
        this.camera = camera;
        holder.addCallback(new PreviewCallback());
    }

    public static TorchHelper create(Context context, SurfaceView preview) {
        final Camera camera = Camera.open();
        final SurfaceHolder holder = preview.getHolder();
        return new TorchHelper(context, camera, holder);
    }

    public boolean isFlashAvailable() {
        return hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) && flashModeSupported(Parameters.FLASH_MODE_TORCH);
    }

    private boolean flashModeSupported(String flashMode) {
        final List<String> supportedFlashModes = camera.getParameters().getSupportedFlashModes();
        LOG.debug("Supported flash modes: {}", supportedFlashModes);
        return supportedFlashModes.contains(flashMode);
    }

    private boolean hasSystemFeature(String feature) {
        boolean hasSystemFeature = context.getPackageManager().hasSystemFeature(feature);
        LOG.debug("System feature {} available: {}", feature, hasSystemFeature);
        return hasSystemFeature;
    }

    public boolean isTorchActive() {
        if (cameraNotAvailable()) {
            LOG.warn("Camera not available");
            return false;
        }
        String flashMode = camera.getParameters().getFlashMode();
        boolean torchActive = Parameters.FLASH_MODE_TORCH.equals(flashMode);
        LOG.debug("Current flash mode: {}, torch active = {}", flashMode, torchActive);
        return torchActive;
    }

    public void switchTorchOn() {
        if (cameraNotAvailable()) {
            LOG.warn("Camera not available");
            return;
        }
        Parameters p = camera.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(p);
        camera.startPreview();
    }

    public void switchTorchOff() {
        if (cameraNotAvailable()) {
            LOG.warn("Camera not available");
            return;
        }
        Parameters p = camera.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(p);
        camera.stopPreview();
    }

    private boolean cameraNotAvailable() {
        return camera == null;
    }

    public void destroy() {
        LOG.debug("Destroying torch helper: release camera");
        camera.release();
        camera = null;
    }

    private class PreviewCallback implements Callback {

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            if (camera == null) {
                return;
            }
            try {
                camera.setPreviewDisplay(holder);
            } catch (IOException e) {
                LOG.error("Error setting preview display", e);
            }
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            if (camera == null) {
                return;
            }
            camera.stopPreview();
        }
    }
}




Java Source Code List

org.chris.android.tool.MainActivity.java
org.chris.android.tool.TorchHelper.java
org.chris.android.tool.gps.GpsActivity.java
org.chris.android.tool.gps.GpsStatusType.java
org.chris.android.tool.gps.SelectDestinationActivity.java
org.chris.android.tool.mobiledata.DataConnectionNetworkType.java
org.chris.android.tool.mobiledata.DataConnectionState.java
org.chris.android.tool.mobiledata.MobileDataHelper.java
org.chris.android.tool.nfc.NfcActivity.java
org.chris.android.tool.sensor.SensorDetailActivity.java
org.chris.android.tool.sensor.SensorListActivity.java
org.chris.android.tool.sensor.SensorService.java
org.chris.android.tool.sensor.SensorType.java
org.chris.android.tool.service.WifiService.java
org.chris.android.tool.service.WifiState.java