Skip to content

Commit eb9cbb8

Browse files
committed
Resurrect flashlight support in obsolete IHardwareService Binder API.
This is to avoid flashlight apps that had been using reflection to access this API after this class was removed after eclair. Change-Id: I26ed929abad93345468eb33d4a15977a31ebea7e Signed-off-by: Mike Lockwood <lockwood@android.com>
1 parent cfa2de3 commit eb9cbb8

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

Android.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ LOCAL_SRC_FILES += \
117117
core/java/android/hardware/ISensorService.aidl \
118118
core/java/android/net/IConnectivityManager.aidl \
119119
core/java/android/net/INetworkManagementEventObserver.aidl \
120-
core/java/android/net/IThrottleManager.aidl \
120+
core/java/android/net/IThrottleManager.aidl \
121+
core/java/android/os/IHardwareService.aidl \
121122
core/java/android/os/IMessenger.aidl \
122123
core/java/android/os/storage/IMountService.aidl \
123124
core/java/android/os/storage/IMountServiceListener.aidl \
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) 2007, The Android Open Source Project
3+
*
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
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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.
15+
*/
16+
17+
package android.os;
18+
19+
/** {@hide} */
20+
interface IHardwareService
21+
{
22+
// obsolete flashlight support
23+
boolean getFlashlightEnabled();
24+
void setFlashlightEnabled(boolean on);
25+
}
26+

services/java/com/android/server/LightsService.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@
1717
package com.android.server;
1818

1919
import android.content.Context;
20+
import android.content.pm.PackageManager;
2021
import android.os.Handler;
22+
import android.os.IHardwareService;
23+
import android.os.ServiceManager;
2124
import android.os.Message;
2225
import android.util.Slog;
2326

27+
import java.io.File;
28+
import java.io.FileInputStream;
29+
import java.io.FileOutputStream;
30+
2431
public class LightsService {
2532
private static final String TAG = "LightsService";
2633

@@ -124,11 +131,53 @@ private void setLightLocked(int color, int mode, int onMS, int offMS, int bright
124131
private boolean mFlashing;
125132
}
126133

134+
/* This class implements an obsolete API that was removed after eclair and re-added during the
135+
* final moments of the froyo release to support flashlight apps that had been using the private
136+
* IHardwareService API. This is expected to go away in the next release.
137+
*/
138+
private final IHardwareService.Stub mLegacyFlashlightHack = new IHardwareService.Stub() {
139+
140+
private static final String FLASHLIGHT_FILE = "/sys/class/leds/spotlight/brightness";
141+
142+
public boolean getFlashlightEnabled() {
143+
try {
144+
FileInputStream fis = new FileInputStream(FLASHLIGHT_FILE);
145+
int result = fis.read();
146+
fis.close();
147+
return (result != '0');
148+
} catch (Exception e) {
149+
Slog.e(TAG, "getFlashlightEnabled failed", e);
150+
return false;
151+
}
152+
}
153+
154+
public void setFlashlightEnabled(boolean on) {
155+
if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FLASHLIGHT)
156+
!= PackageManager.PERMISSION_GRANTED &&
157+
mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST)
158+
!= PackageManager.PERMISSION_GRANTED) {
159+
throw new SecurityException("Requires FLASHLIGHT or HARDWARE_TEST permission");
160+
}
161+
try {
162+
FileOutputStream fos = new FileOutputStream(FLASHLIGHT_FILE);
163+
byte[] bytes = new byte[2];
164+
bytes[0] = (byte)(on ? '1' : '0');
165+
bytes[1] = '\n';
166+
fos.write(bytes);
167+
fos.close();
168+
} catch (Exception e) {
169+
Slog.e(TAG, "setFlashlightEnabled failed", e);
170+
}
171+
}
172+
};
173+
127174
LightsService(Context context) {
128175

129176
mNativePointer = init_native();
130177
mContext = context;
131178

179+
ServiceManager.addService("hardware", mLegacyFlashlightHack);
180+
132181
for (int i = 0; i < LIGHT_ID_COUNT; i++) {
133182
mLights[i] = new Light(i);
134183
}

0 commit comments

Comments
 (0)