FileDocCategorySizeDatePackage
HardwareService.javaAPI DocAndroid 1.5 API14313Wed May 06 22:42:00 BST 2009com.android.server

HardwareService

public class HardwareService extends IHardwareService.Stub

Fields Summary
private static final String
TAG
static final int
LIGHT_ID_BACKLIGHT
static final int
LIGHT_ID_KEYBOARD
static final int
LIGHT_ID_BUTTONS
static final int
LIGHT_ID_BATTERY
static final int
LIGHT_ID_NOTIFICATIONS
static final int
LIGHT_ID_ATTENTION
static final int
LIGHT_FLASH_NONE
static final int
LIGHT_FLASH_TIMED
private boolean
mAttentionLightOn
private boolean
mPulsing
private android.os.Handler
mH
android.content.BroadcastReceiver
mIntentReceiver
private final android.content.Context
mContext
private final PowerManager.WakeLock
mWakeLock
private final com.android.internal.app.IBatteryStats
mBatteryStats
volatile VibrateThread
mThread
volatile Death
mDeath
volatile android.os.IBinder
mToken
private int
mNativePointer
Constructors Summary
HardwareService(android.content.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);

        mBatteryStats = BatteryStatsService.getService();
        
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        context.registerReceiver(mIntentReceiver, filter);
    
Methods Summary
public voidcancelVibrate()

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

        // so wakelock calls will succeed
        long identity = Binder.clearCallingIdentity();
        try {
            doCancelVibrate();
        }
        finally {
            Binder.restoreCallingIdentity(identity);
        }
    
private voiddoCancelVibrate()


       
        synchronized (this) {
            if (mThread != null) {
                synchronized (mThread) {
                    mThread.mDone = true;
                    mThread.notify();
                }
                mThread = null;
            }
            vibratorOff();
        }
    
public voidenableCameraFlash(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);
    
protected voidfinalize()

        finalize_native(mNativePointer);
        super.finalize();
    
private static native voidfinalize_native(int ptr)

public booleangetFlashlightEnabled()

        return Hardware.getFlashlightEnabled();
    
private static native intinit_native()

private booleanisAll0(long[] pattern)

        int N = pattern.length;
        for (int i = 0; i < N; i++) {
            if (pattern[i] != 0) {
                return false;
            }
        }
        return true;
    
public voidpulseBreathingLight()

        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);
            }
        }
    
public voidsetAttentionLight(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 voidsetBacklights(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);
        long identity = Binder.clearCallingIdentity();
        try {
            mBatteryStats.noteScreenBrightness(brightness);
        } catch (RemoteException e) {
            Log.w(TAG, "RemoteException calling noteScreenBrightness on BatteryStatsService", e);
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
    
public voidsetFlashlightEnabled(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);
    
voidsetLightBrightness_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);
    
voidsetLightColor_UNCHECKED(int light, int color)

        setLight_native(mNativePointer, light, color, LIGHT_FLASH_NONE, 0, 0);
    
voidsetLightFlashing_UNCHECKED(int light, int color, int mode, int onMS, int offMS)

        setLight_native(mNativePointer, light, color, mode, onMS, offMS);
    
voidsetLightOff_UNCHECKED(int light)

        setLight_native(mNativePointer, light, 0, LIGHT_FLASH_NONE, 0, 0);
    
private static native voidsetLight_native(int ptr, int light, int color, int mode, int onMS, int offMS)

public voidvibrate(long milliseconds)

        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.VIBRATE)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires VIBRATE permission");
        }
        doCancelVibrate();
        vibratorOn(milliseconds);
    
public voidvibratePattern(long[] pattern, int repeat, android.os.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);
        }
    
static native voidvibratorOff()

static native voidvibratorOn(long milliseconds)