summaryrefslogtreecommitdiffstats
path: root/services/java/com/android/server/HardwareService.java
blob: 42367ef86a8ef2b486032c76dca9d325741b075c (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Hardware;
import android.os.IHardwareService;
import android.os.Message;
import android.os.Power;
import android.os.PowerManager;
import android.os.Process;
import android.os.RemoteException;
import android.os.IBinder;
import android.os.Binder;
import android.os.SystemClock;
import android.util.Log;

public class HardwareService extends IHardwareService.Stub {
    private static final String TAG = "HardwareService";

    static final int LIGHT_ID_BACKLIGHT = 0;
    static final int LIGHT_ID_KEYBOARD = 1;
    static final int LIGHT_ID_BUTTONS = 2;
    static final int LIGHT_ID_BATTERY = 3;
    static final int LIGHT_ID_NOTIFICATIONS = 4;
    static final int LIGHT_ID_ATTENTION = 5;

    static final int LIGHT_FLASH_NONE = 0;
    static final int LIGHT_FLASH_TIMED = 1;

    private boolean mAttentionLightOn;
    private boolean mPulsing;

    HardwareService(Context context) {
        // Reset the hardware to a default state, in case this is a runtime
        // restart instead of a fresh boot.
        vibratorOff();

        mNativePointer = init_native();

        mContext = context;
        PowerManager pm = (PowerManager)context.getSystemService(
                Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
        mWakeLock.setReferenceCounted(true);

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        context.registerReceiver(mIntentReceiver, filter);
    }

    protected void finalize() throws Throwable {
        finalize_native(mNativePointer);
        super.finalize();
    }

    public void vibrate(long milliseconds) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires VIBRATE permission");
        }
        doCancelVibrate();
        vibratorOn(milliseconds);
    }

    private boolean isAll0(long[] pattern) {
        int N = pattern.length;
        for (int i = 0; i < N; i++) {
            if (pattern[i] != 0) {
                return false;
            }
        }
        return true;
    }

    public void vibratePattern(long[] pattern, int repeat, IBinder token) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires VIBRATE permission");
        }
        // so wakelock calls will succeed
        long identity = Binder.clearCallingIdentity();
        try {
            if (false) {
                String s = "";
                int N = pattern.length;
                for (int i=0; i<N; i++) {
                    s += " " + pattern[i];
                }
                Log.i(TAG, "vibrating with pattern: " + s);
            }

            // we're running in the server so we can't fail
            if (pattern == null || pattern.length == 0
                    || isAll0(pattern)
                    || repeat >= pattern.length || token == null) {
                return;
            }

            synchronized (this) {
                Death death = new Death(token);
                try {
                    token.linkToDeath(death, 0);
                } catch (RemoteException e) {
                    return;
                }

                Thread oldThread = mThread;

                if (oldThread != null) {
                    // stop the old one
                    synchronized (mThread) {
                        mThread.mDone = true;
                        mThread.notify();
                    }
                }

                if (mDeath != null) {
                    mToken.unlinkToDeath(mDeath, 0);
                }

                mDeath = death;
                mToken = token;

                // start the new thread
                mThread = new VibrateThread(pattern, repeat);
                mThread.start();
            }
        }
        finally {
            Binder.restoreCallingIdentity(identity);
        }
    }

    public void cancelVibrate() {
        mContext.enforceCallingOrSelfPermission(
                android.Manifest.permission.VIBRATE,
                "cancelVibrate");

        // so wakelock calls will succeed
        long identity = Binder.clearCallingIdentity();
        try {
            doCancelVibrate();
        }
        finally {
            Binder.restoreCallingIdentity(identity);
        }
    }
    
    public boolean getFlashlightEnabled() {
        return Hardware.getFlashlightEnabled();
    }
    
    public void setFlashlightEnabled(boolean on) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FLASHLIGHT) 
                != PackageManager.PERMISSION_GRANTED &&
                mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST) 
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FLASHLIGHT or HARDWARE_TEST permission");
        }
        Hardware.setFlashlightEnabled(on);
    }

    public void enableCameraFlash(int milliseconds) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.CAMERA) 
                != PackageManager.PERMISSION_GRANTED &&
                mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST) 
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires CAMERA or HARDWARE_TEST permission");
        }
        Hardware.enableCameraFlash(milliseconds);
    }

    public void setBacklights(int brightness) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.HARDWARE_TEST) 
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires HARDWARE_TEST permission");
        }
        // Don't let applications turn the screen all the way off
        brightness = Math.max(brightness, Power.BRIGHTNESS_DIM);
        setLightBrightness_UNCHECKED(LIGHT_ID_BACKLIGHT, brightness);
        setLightBrightness_UNCHECKED(LIGHT_ID_KEYBOARD, brightness);
        setLightBrightness_UNCHECKED(LIGHT_ID_BUTTONS, brightness);
    }

    void setLightOff_UNCHECKED(int light) {
        setLight_native(mNativePointer, light, 0, LIGHT_FLASH_NONE, 0, 0);
    }

    void setLightBrightness_UNCHECKED(int light, int brightness) {
        int b = brightness & 0x000000ff;
        b = 0xff000000 | (b << 16) | (b << 8) | b;
        setLight_native(mNativePointer, light, b, LIGHT_FLASH_NONE, 0, 0);
    }

    void setLightColor_UNCHECKED(int light, int color) {
        setLight_native(mNativePointer, light, color, LIGHT_FLASH_NONE, 0, 0);
    }

    void setLightFlashing_UNCHECKED(int light, int color, int mode, int onMS, int offMS) {
        setLight_native(mNativePointer, light, color, mode, onMS, offMS);
    }

    public void setAttentionLight(boolean on) {
        // Not worthy of a permission.  We shouldn't have a flashlight permission.
        synchronized (this) {
            mAttentionLightOn = on;
            mPulsing = false;
            setLight_native(mNativePointer, LIGHT_ID_ATTENTION, on ? 0xffffffff : 0,
                    LIGHT_FLASH_NONE, 0, 0);
        }
    }

    public void pulseBreathingLight() {
        synchronized (this) {
            // HACK: Added at the last minute of cupcake -- design this better;
            // Don't reuse the attention light -- make another one.
            if (false) {
                Log.d(TAG, "pulseBreathingLight mAttentionLightOn=" + mAttentionLightOn
                        + " mPulsing=" + mPulsing);
            }
            if (!mAttentionLightOn && !mPulsing) {
                mPulsing = true;
                setLight_native(mNativePointer, LIGHT_ID_ATTENTION, 0xff101010,
                        LIGHT_FLASH_NONE, 0, 0);
                mH.sendMessageDelayed(Message.obtain(mH, 1), 3000);
            }
        }
    }

    private Handler mH = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            synchronized (this) {
                if (false) {
                    Log.d(TAG, "pulse cleanup handler firing mPulsing=" + mPulsing);
                }
                if (mPulsing) {
                    mPulsing = false;
                    setLight_native(mNativePointer, LIGHT_ID_ATTENTION,
                            mAttentionLightOn ? 0xffffffff : 0,
                            LIGHT_FLASH_NONE, 0, 0);
                }
            }
        }
    };

    private void doCancelVibrate() {
        synchronized (this) {
            if (mThread != null) {
                synchronized (mThread) {
                    mThread.mDone = true;
                    mThread.notify();
                }
                mThread = null;
            }
            vibratorOff();
        }
    }

    private class VibrateThread extends Thread {
        long[] mPattern;
        int mRepeat;
        boolean mDone;
    
        VibrateThread(long[] pattern, int repeat) {
            mPattern = pattern;
            mRepeat = repeat;
            mWakeLock.acquire();
        }

        private void delay(long duration) {
            if (duration > 0) {
                long bedtime = SystemClock.uptimeMillis();
                do {
                    try {
                        this.wait(duration);
                    }
                    catch (InterruptedException e) {
                    }
                    if (mDone) {
                        break;
                    }
                    duration = duration
                            - SystemClock.uptimeMillis() - bedtime;
                } while (duration > 0);
            }
        }

        public void run() {
            Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY);
            synchronized (this) {
                int index = 0;
                long[] pattern = mPattern;
                int len = pattern.length;
                long duration = 0;

                while (!mDone) {
                    // add off-time duration to any accumulated on-time duration 
                    if (index < len) {
                        duration += pattern[index++];
                    }

                    // sleep until it is time to start the vibrator
                    delay(duration);
                    if (mDone) {
                        break;
                    }

                    if (index < len) {
                        // read on-time duration and start the vibrator
                        // duration is saved for delay() at top of loop
                        duration = pattern[index++];
                        if (duration > 0) {
                            HardwareService.this.vibratorOn(duration);
                        }
                    } else {
                        if (mRepeat < 0) {
                            break;
                        } else {
                            index = mRepeat;
                            duration = 0;
                        }
                    }
                }
                if (mDone) {
                    // make sure vibrator is off if we were cancelled.
                    // otherwise, it will turn off automatically 
                    // when the last timeout expires.
                    HardwareService.this.vibratorOff();
                }
                mWakeLock.release();
            }
            synchronized (HardwareService.this) {
                if (mThread == this) {
                    mThread = null;
                }
            }
        }
    };

    private class Death implements IBinder.DeathRecipient {
        IBinder mMe;

        Death(IBinder me) {
            mMe = me;
        }

        public void binderDied() {
            synchronized (HardwareService.this) {
                if (mMe == mToken) {
                    doCancelVibrate();
                }
            }
        }
    }

    BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                doCancelVibrate();
            }
        }
    };
    
    private static native int init_native();
    private static native void finalize_native(int ptr);

    private static native void setLight_native(int ptr, int light, int color, int mode,
            int onMS, int offMS);

    private Context mContext;
    private PowerManager.WakeLock mWakeLock;

    volatile VibrateThread mThread;
    volatile Death mDeath;
    volatile IBinder mToken;

    private int mNativePointer;

    native static void vibratorOn(long milliseconds);
    native static void vibratorOff();
}