简体   繁体   中英

Change blink speed for Cordova flashlight plugin

So, I am trying to make the torch/flashlight on my phone flicker or blink relatively quickly. I have added the Cordova flashlight plugin to my app and it seems to be working alright but when I try to toggle the torch faster than 500ms nothing changes. It seems to be limited to a 500ms delay or something of the sort. I am currently testing on a Galaxy S4 running android 4.4.4 I have installed other flashlight apps and have tried turning it on and off rapidly and it works fine so I know the hardware is capable of blinking faster.

I have looked through the source code for the plugin and I can't seem to find anything that I could override or change to fix the issue. Maybe I'm just missing it.

Anyone have any ideas of how I might override whatever delay might be in place? Or if anyone has any other methods to accomplish this I would be interested in those as well.

Thanks

js:

$(document).ready(function(){
  document.addEventListener("deviceready", function() {
    setInterval(function () {
      window.plugins.flashlight.toggle();
    }, 200);
  });
});

flashlight.js

function Flashlight() {
  // track flashlight state
  this._isSwitchedOn = false;
}

Flashlight.prototype = {

  available: function (callback) {
    cordova.exec(function (avail) {
      callback(avail ? true : false);
    }, null, "Flashlight", "available", []);
  },

  switchOn: function (successCallback, errorCallback) {
    this._isSwitchedOn = true;
    cordova.exec(successCallback, errorCallback, "Flashlight", "switchOn", []);
  },

  switchOff: function (successCallback, errorCallback) {
    this._isSwitchedOn = false;
    cordova.exec(successCallback, errorCallback, "Flashlight", "switchOff", []);
  },

  toggle: function (successCallback, errorCallback) {
    if (this._isSwitchedOn) {
      this.switchOff(successCallback, errorCallback);
    } else {
      this.switchOn(successCallback, errorCallback);
    }
  }
};

Flashlight.install = function () {
  if (!window.plugins) {
    window.plugins = {};
  }

  window.plugins.flashlight = new Flashlight();
  return window.plugins.flashlight;
};

cordova.addConstructor(Flashlight.install);

flashlight.java

package nl.xservices.plugins;

import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Build;
import android.util.Log;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

public class Flashlight extends CordovaPlugin {

  private static final String ACTION_AVAILABLE = "available";
  private static final String ACTION_SWITCH_ON = "switchOn";
  private static final String ACTION_SWITCH_OFF = "switchOff";

  private static Boolean capable;
  private boolean releasing;
  private Camera mCamera;

  @Override
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    Log.d("Flashlight", "Plugin Called: " + action);
    try {
      if (action.equals(ACTION_SWITCH_ON)) {
        // When switching on immediately after checking for isAvailable,
        // the release method may still be running, so wait a bit.
        while (releasing) {
          Thread.sleep(10);
        }
        mCamera = Camera.open();
        if (Build.VERSION.SDK_INT >= 11) { // honeycomb
          // required for (at least) the Nexus 5
          mCamera.setPreviewTexture(new SurfaceTexture(0));
        }
        toggleTorch(true, callbackContext);
        return true;
      } else if (action.equals(ACTION_SWITCH_OFF)) {
        toggleTorch(false, callbackContext);
        releaseCamera();
        return true;
      } else if (action.equals(ACTION_AVAILABLE)) {
        if (capable == null) {
          mCamera = Camera.open();
          capable = isCapable();
          releaseCamera();
        }
        callbackContext.success(capable ? 1 : 0);
        return true;
      } else {
        callbackContext.error("flashlight." + action + " is not a supported function.");
        return false;
      }
    } catch (Exception e) {
      callbackContext.error(e.getMessage());
      return false;
    }
  }

  private boolean isCapable() {
    final PackageManager packageManager = this.cordova.getActivity().getPackageManager();
    for (final FeatureInfo feature : packageManager.getSystemAvailableFeatures()) {
      if (PackageManager.FEATURE_CAMERA_FLASH.equalsIgnoreCase(feature.name))     {
        return true;
      }
    }
    return false;
  }

  private void toggleTorch(boolean switchOn, CallbackContext callbackContext) {
    final Camera.Parameters mParameters = mCamera.getParameters();
    if (isCapable()) {
      mParameters.setFlashMode(switchOn ? Camera.Parameters.FLASH_MODE_TORCH : Camera.Parameters.FLASH_MODE_OFF);
      mCamera.setParameters(mParameters);
      mCamera.startPreview();
      callbackContext.success();
    } else {
      callbackContext.error("Device is not capable of using the flashlight. Please test with flashlight.available()");
    }
  }

  private void releaseCamera() {
    releasing = true;
    // we need to release the camera, so other apps can use it
    new Thread(new Runnable() {
      public void run() {
        mCamera.setPreviewCallback(null);
        mCamera.stopPreview();
        mCamera.release();
        releasing = false;
      }
    }).start();
  }
}

What you propose is not going to work well, as the flashlight is one of the more fragmented aspects of Android, and many phones simply won't be able to keep up with your requirements, even if you weren't using Cordova and were full native.

I have seen phones that, for instance, require almost 2 seconds to turn the light on and off, which will make your effect impossible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM