2 * Copyright (C) 2010 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.client.android.camera;
19 import android.os.IBinder;
20 import android.util.Log;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
26 * This class is used to activate the weak light on some camera phones (not flash)
27 * in order to illuminate surfaces for scanning. There is no official way to do this,
28 * but, classes which allow access to this function still exist on some devices.
29 * This therefore proceeds through a great deal of reflection.
31 * See <a href="http://almondmendoza.com/2009/01/05/changing-the-screen-brightness-programatically/">
32 * http://almondmendoza.com/2009/01/05/changing-the-screen-brightness-programatically/</a> and
33 * <a href="http://code.google.com/p/droidled/source/browse/trunk/src/com/droidled/demo/DroidLED.java">
34 * http://code.google.com/p/droidled/source/browse/trunk/src/com/droidled/demo/DroidLED.java</a>.
35 * Thanks to Ryan Alford for pointing out the availability of this class.
37 final class FlashlightManager {
39 private static final String TAG = FlashlightManager.class.getSimpleName();
41 private static final Object iHardwareService;
42 private static final Method setFlashEnabledMethod;
44 iHardwareService = getHardwareService();
45 setFlashEnabledMethod = getSetFlashEnabledMethod(iHardwareService);
46 if (iHardwareService == null) {
47 Log.v(TAG, "This device does supports control of a flashlight");
49 Log.v(TAG, "This device does not support control of a flashlight");
53 private FlashlightManager() {
56 private static Object getHardwareService() {
57 Class<?> serviceManagerClass = maybeForName("android.os.ServiceManager");
58 if (serviceManagerClass == null) {
62 Method getServiceMethod = maybeGetMethod(serviceManagerClass, "getService", String.class);
63 if (getServiceMethod == null) {
67 Object hardwareService = invoke(getServiceMethod, null, "hardware");
68 if (hardwareService == null) {
72 Class<?> iHardwareServiceStubClass = maybeForName("android.os.IHardwareService$Stub");
73 if (iHardwareServiceStubClass == null) {
77 Method asInterfaceMethod = maybeGetMethod(iHardwareServiceStubClass, "asInterface", IBinder.class);
78 if (asInterfaceMethod == null) {
82 return invoke(asInterfaceMethod, null, hardwareService);
85 private static Method getSetFlashEnabledMethod(Object iHardwareService) {
86 if (iHardwareService == null) {
89 Class<?> proxyClass = iHardwareService.getClass();
90 return maybeGetMethod(proxyClass, "setFlashlightEnabled", boolean.class);
93 private static Class<?> maybeForName(String name) {
95 return Class.forName(name);
96 } catch (ClassNotFoundException cnfe) {
99 } catch (RuntimeException re) {
100 Log.w(TAG, "Unexpected error while finding class " + name, re);
105 private static Method maybeGetMethod(Class<?> clazz, String name, Class<?>... argClasses) {
107 return clazz.getMethod(name, argClasses);
108 } catch (NoSuchMethodException nsme) {
111 } catch (RuntimeException re) {
112 Log.w(TAG, "Unexpected error while finding method " + name, re);
117 private static Object invoke(Method method, Object instance, Object... args) {
119 return method.invoke(instance, args);
120 } catch (IllegalAccessException e) {
121 Log.w(TAG, "Unexpected error while invoking " + method, e);
123 } catch (InvocationTargetException e) {
124 Log.w(TAG, "Unexpected error while invoking " + method, e.getCause());
126 } catch (RuntimeException re) {
127 Log.w(TAG, "Unexpected error while invoking " + method, re);
132 static void enableFlashlight() {
136 static void disableFlashlight() {
137 setFlashlight(false);
140 private static void setFlashlight(boolean active) {
141 if (iHardwareService != null) {
142 invoke(setFlashEnabledMethod, iHardwareService, active);