Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 17 | package com.android.server.lights; |
| 18 | |
| 19 | import com.android.server.SystemService; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 20 | |
| 21 | import android.content.Context; |
Mike Lockwood | eb9cbb8 | 2010-05-17 17:27:30 -0400 | [diff] [blame] | 22 | import android.content.pm.PackageManager; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 23 | import android.os.Handler; |
Mike Lockwood | eb9cbb8 | 2010-05-17 17:27:30 -0400 | [diff] [blame] | 24 | import android.os.IHardwareService; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 25 | import android.os.Message; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 26 | import android.util.Slog; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 27 | |
Mike Lockwood | eb9cbb8 | 2010-05-17 17:27:30 -0400 | [diff] [blame] | 28 | import java.io.FileInputStream; |
| 29 | import java.io.FileOutputStream; |
| 30 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 31 | public class LightsService extends SystemService { |
| 32 | static final String TAG = "LightsService"; |
| 33 | static final boolean DEBUG = false; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 34 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 35 | final LightImpl mLights[] = new LightImpl[LightsManager.LIGHT_ID_COUNT]; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 36 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 37 | private final class LightImpl extends Light { |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 38 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 39 | private LightImpl(int id) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 40 | mId = id; |
| 41 | } |
| 42 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 43 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 44 | public void setBrightness(int brightness) { |
| 45 | setBrightness(brightness, BRIGHTNESS_MODE_USER); |
| 46 | } |
| 47 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 48 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 49 | public void setBrightness(int brightness, int brightnessMode) { |
| 50 | synchronized (this) { |
| 51 | int color = brightness & 0x000000ff; |
| 52 | color = 0xff000000 | (color << 16) | (color << 8) | color; |
| 53 | setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, brightnessMode); |
| 54 | } |
| 55 | } |
| 56 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 57 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 58 | public void setColor(int color) { |
| 59 | synchronized (this) { |
| 60 | setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, 0); |
| 61 | } |
| 62 | } |
| 63 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 64 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 65 | public void setFlashing(int color, int mode, int onMS, int offMS) { |
| 66 | synchronized (this) { |
| 67 | setLightLocked(color, mode, onMS, offMS, BRIGHTNESS_MODE_USER); |
| 68 | } |
| 69 | } |
| 70 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 71 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 72 | public void pulse() { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 73 | pulse(0x00ffffff, 7); |
| 74 | } |
| 75 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 76 | @Override |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 77 | public void pulse(int color, int onMS) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 78 | synchronized (this) { |
| 79 | if (mColor == 0 && !mFlashing) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 80 | setLightLocked(color, LIGHT_FLASH_HARDWARE, onMS, 1000, BRIGHTNESS_MODE_USER); |
| 81 | mH.sendMessageDelayed(Message.obtain(mH, 1, this), onMS); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 86 | @Override |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 87 | public void turnOff() { |
| 88 | synchronized (this) { |
| 89 | setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, 0); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private void stopFlashing() { |
| 94 | synchronized (this) { |
| 95 | setLightLocked(mColor, LIGHT_FLASH_NONE, 0, 0, BRIGHTNESS_MODE_USER); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) { |
| 100 | if (color != mColor || mode != mMode || onMS != mOnMS || offMS != mOffMS) { |
Dianne Hackborn | ff801ec | 2011-01-22 18:05:38 -0800 | [diff] [blame] | 101 | if (DEBUG) Slog.v(TAG, "setLight #" + mId + ": color=#" |
| 102 | + Integer.toHexString(color)); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 103 | mColor = color; |
| 104 | mMode = mode; |
| 105 | mOnMS = onMS; |
| 106 | mOffMS = offMS; |
| 107 | setLight_native(mNativePointer, mId, color, mode, onMS, offMS, brightnessMode); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private int mId; |
| 112 | private int mColor; |
| 113 | private int mMode; |
| 114 | private int mOnMS; |
| 115 | private int mOffMS; |
| 116 | private boolean mFlashing; |
| 117 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 118 | |
Mike Lockwood | eb9cbb8 | 2010-05-17 17:27:30 -0400 | [diff] [blame] | 119 | /* This class implements an obsolete API that was removed after eclair and re-added during the |
| 120 | * final moments of the froyo release to support flashlight apps that had been using the private |
| 121 | * IHardwareService API. This is expected to go away in the next release. |
| 122 | */ |
| 123 | private final IHardwareService.Stub mLegacyFlashlightHack = new IHardwareService.Stub() { |
| 124 | |
| 125 | private static final String FLASHLIGHT_FILE = "/sys/class/leds/spotlight/brightness"; |
| 126 | |
| 127 | public boolean getFlashlightEnabled() { |
| 128 | try { |
| 129 | FileInputStream fis = new FileInputStream(FLASHLIGHT_FILE); |
| 130 | int result = fis.read(); |
| 131 | fis.close(); |
| 132 | return (result != '0'); |
| 133 | } catch (Exception e) { |
Mike Lockwood | eb9cbb8 | 2010-05-17 17:27:30 -0400 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | public void setFlashlightEnabled(boolean on) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 139 | final Context context = getContext(); |
| 140 | if (context.checkCallingOrSelfPermission(android.Manifest.permission.FLASHLIGHT) |
Mike Lockwood | eb9cbb8 | 2010-05-17 17:27:30 -0400 | [diff] [blame] | 141 | != PackageManager.PERMISSION_GRANTED && |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 142 | context.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST) |
Mike Lockwood | eb9cbb8 | 2010-05-17 17:27:30 -0400 | [diff] [blame] | 143 | != PackageManager.PERMISSION_GRANTED) { |
| 144 | throw new SecurityException("Requires FLASHLIGHT or HARDWARE_TEST permission"); |
| 145 | } |
| 146 | try { |
| 147 | FileOutputStream fos = new FileOutputStream(FLASHLIGHT_FILE); |
| 148 | byte[] bytes = new byte[2]; |
| 149 | bytes[0] = (byte)(on ? '1' : '0'); |
| 150 | bytes[1] = '\n'; |
| 151 | fos.write(bytes); |
| 152 | fos.close(); |
| 153 | } catch (Exception e) { |
Mike Lockwood | 35167d9 | 2011-03-28 20:29:14 -0700 | [diff] [blame] | 154 | // fail silently |
Mike Lockwood | eb9cbb8 | 2010-05-17 17:27:30 -0400 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | }; |
| 158 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame^] | 159 | public LightsService(Context context) { |
| 160 | super(context); |
| 161 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 162 | mNativePointer = init_native(); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 163 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 164 | for (int i = 0; i < LightsManager.LIGHT_ID_COUNT; i++) { |
| 165 | mLights[i] = new LightImpl(i); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 166 | } |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 167 | } |
| 168 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 169 | @Override |
| 170 | public void onStart() { |
| 171 | publishBinderService("hardware", mLegacyFlashlightHack); |
| 172 | publishLocalService(LightsManager.class, mService); |
| 173 | } |
| 174 | |
| 175 | private final LightsManager mService = new LightsManager() { |
| 176 | @Override |
| 177 | public com.android.server.lights.Light getLight(int id) { |
| 178 | if (id < LIGHT_ID_COUNT) { |
| 179 | return mLights[id]; |
| 180 | } else { |
| 181 | return null; |
| 182 | } |
| 183 | } |
| 184 | }; |
| 185 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame^] | 186 | @Override |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 187 | protected void finalize() throws Throwable { |
| 188 | finalize_native(mNativePointer); |
| 189 | super.finalize(); |
| 190 | } |
| 191 | |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 192 | private Handler mH = new Handler() { |
| 193 | @Override |
| 194 | public void handleMessage(Message msg) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 195 | LightImpl light = (LightImpl)msg.obj; |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 196 | light.stopFlashing(); |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 197 | } |
| 198 | }; |
| 199 | |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 200 | private static native long init_native(); |
| 201 | private static native void finalize_native(long ptr); |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 202 | |
Narayan Kamath | 3f7b8d0 | 2014-01-08 12:19:40 +0000 | [diff] [blame] | 203 | static native void setLight_native(long ptr, int light, int color, int mode, |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 204 | int onMS, int offMS, int brightnessMode); |
| 205 | |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 206 | private long mNativePointer; |
Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 207 | } |