public classFlashlightManager {private static final String TAG = FlashlightManager.class.getSimpleName();private static finalObject iHardwareService;private static finalMethod setFlashEnabledMethod;static{
iHardwareService=getHardwareService();
setFlashEnabledMethod=getSetFlashEnabledMethod(iHardwareService);if (iHardwareService == null) {
Log.v(TAG,"This device does supports control of a flashlight");
}else{
Log.v(TAG,"This device does not support control of a flashlight");
}
}privateFlashlightManager() {
}private staticObject getHardwareService() {
Class> serviceManagerClass = maybeForName("android.os.ServiceManager");if (serviceManagerClass == null) {return null;
}
Method getServiceMethod=maybeGetMethod(serviceManagerClass,"getService", String.class);if (getServiceMethod == null) {return null;
}
Object hardwareService= invoke(getServiceMethod, null, "hardware");if (hardwareService == null) {return null;
}
Class> iHardwareServiceStubClass = maybeForName("android.os.IHardwareService$Stub");if (iHardwareServiceStubClass == null) {return null;
}
Method asInterfaceMethod=maybeGetMethod(iHardwareServiceStubClass,"asInterface", IBinder.class);if (asInterfaceMethod == null) {return null;
}return invoke(asInterfaceMethod, null, hardwareService);
}private staticMethod getSetFlashEnabledMethod(Object iHardwareService) {if (iHardwareService == null) {return null;
}
Class> proxyClass =iHardwareService.getClass();return maybeGetMethod(proxyClass, "setFlashlightEnabled", boolean.class);
}private static Class>maybeForName(String name) {try{returnClass.forName(name);
}catch(ClassNotFoundException cnfe) {//OK
return null;
}catch(Exception re) {
re.printStackTrace();
Log.w(TAG,"Unexpected error while finding class " +name, re);return null;
}
}/*** 通过设置Camera打开闪光灯
*@parammCamera*/
public static voidturnLightOn(Camera mCamera) {if (mCamera == null) {return;
}
Parameters parameters=mCamera.getParameters();if (parameters == null) {return;
}
List flashModes =parameters.getSupportedFlashModes();//Check if camera flash exists
if (flashModes == null) {//Use the screen as a flashlight (next best thing)
return;
}
String flashMode=parameters.getFlashMode();
Log.i(TAG,"Flash mode: " +flashMode);
Log.i(TAG,"Flash modes: " +flashModes);if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {//Turn on the flash
if(flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(parameters);
}else{
}
}
}/*** 通过设置Camera关闭闪光灯
*@parammCamera*/
public static voidturnLightOff(Camera mCamera) {if (mCamera == null) {return;
}
Parameters parameters=mCamera.getParameters();if (parameters == null) {return;
}
List flashModes =parameters.getSupportedFlashModes();
String flashMode=parameters.getFlashMode();//Check if camera flash exists
if (flashModes == null) {return;
}
Log.i(TAG,"Flash mode: " +flashMode);
Log.i(TAG,"Flash modes: " +flashModes);if (!Parameters.FLASH_MODE_OFF.equals(flashMode)) {//Turn off the flash
if(flashModes.contains(Parameters.FLASH_MODE_OFF)) {
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(parameters);
}else{
Log.e(TAG,"FLASH_MODE_OFF not supported");
}
}
}private static Method maybeGetMethod(Class>clazz, String name,
Class>... argClasses) {try{returnclazz.getMethod(name, argClasses);
}catch(Exception nsme) {
nsme.printStackTrace();//OK
return null;
}
}private staticObject invoke(Method method, Object instance, Object... args) {try{returnmethod.invoke(instance, args);
}catch(Exception e) {
Log.w(TAG,"Unexpected error while invoking " +method, e);return null;
}
}/*** 通过反射来操作闪光灯
*@paramactive*/
public static void switchFlashlight(booleanactive) {
setFlashlight(active);
}static voiddisableFlashlight() {
setFlashlight(false);
}private static void setFlashlight(booleanactive) {if (iHardwareService != null) {
invoke(setFlashEnabledMethod, iHardwareService, active);
}
}
}