FlashlightManager.java
3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.ectrip.cyt.zxing.camera;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.os.IBinder;
import com.ectrip.cyt.utils.LogUtil;
final class FlashlightManager {
private final String TAG = FlashlightManager.class.getSimpleName();
private Object iHardwareService;
private Method setFlashEnabledMethod;
public FlashlightManager() {
iHardwareService = getHardwareService();
setFlashEnabledMethod = getSetFlashEnabledMethod(iHardwareService);
if (iHardwareService == null) {
LogUtil.v(TAG, "This device does supports control of a flashlight");
} else {
LogUtil.v(TAG, "This device does not support control of a flashlight");
}
}
/**
* 控制相机闪光灯开关
*/
// FIXME
public void enableFlashlight() {
setFlashlight(false);
}
public void disableFlashlight() {
setFlashlight(false);
}
private Object 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 Method getSetFlashEnabledMethod(Object iHardwareService) {
if (iHardwareService == null) {
return null;
}
Class<?> proxyClass = iHardwareService.getClass();
return maybeGetMethod(proxyClass, "setFlashlightEnabled", boolean.class);
}
private Class<?> maybeForName(String name) {
try {
return Class.forName(name);
} catch (ClassNotFoundException cnfe) {
// OK
return null;
} catch (RuntimeException re) {
LogUtil.w(TAG, "Unexpected error while finding class " + name);
return null;
}
}
private Method maybeGetMethod(Class<?> clazz, String name,
Class<?>... argClasses) {
try {
return clazz.getMethod(name, argClasses);
} catch (NoSuchMethodException nsme) {
// OK
return null;
} catch (RuntimeException re) {
LogUtil.w(TAG, "Unexpected error while finding method " + name);
return null;
}
}
private Object invoke(Method method, Object instance, Object... args) {
try {
return method.invoke(instance, args);
} catch (IllegalAccessException e) {
LogUtil.w(TAG, "Unexpected error while invoking " + method);
return null;
} catch (InvocationTargetException e) {
LogUtil.w(TAG, "Unexpected error while invoking " + method);
return null;
} catch (RuntimeException re) {
LogUtil.w(TAG, "Unexpected error while invoking " + method);
return null;
}
}
private void setFlashlight(boolean active) {
if (iHardwareService != null) {
invoke(setFlashEnabledMethod, iHardwareService, active);
}
}
}