Home | History | Annotate | Download | only in lights
      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 
     17 package com.android.server.lights;
     18 
     19 import com.android.server.SystemService;
     20 
     21 import android.content.Context;
     22 import android.os.Handler;
     23 import android.os.Message;
     24 import android.os.Trace;
     25 import android.util.Slog;
     26 
     27 public class LightsService extends SystemService {
     28     static final String TAG = "LightsService";
     29     static final boolean DEBUG = false;
     30 
     31     final LightImpl mLights[] = new LightImpl[LightsManager.LIGHT_ID_COUNT];
     32 
     33     private final class LightImpl extends Light {
     34 
     35         private LightImpl(int id) {
     36             mId = id;
     37         }
     38 
     39         @Override
     40         public void setBrightness(int brightness) {
     41             setBrightness(brightness, BRIGHTNESS_MODE_USER);
     42         }
     43 
     44         @Override
     45         public void setBrightness(int brightness, int brightnessMode) {
     46             synchronized (this) {
     47                 int color = brightness & 0x000000ff;
     48                 color = 0xff000000 | (color << 16) | (color << 8) | color;
     49                 setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, brightnessMode);
     50             }
     51         }
     52 
     53         @Override
     54         public void setColor(int color) {
     55             synchronized (this) {
     56                 setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, 0);
     57             }
     58         }
     59 
     60         @Override
     61         public void setFlashing(int color, int mode, int onMS, int offMS) {
     62             synchronized (this) {
     63                 setLightLocked(color, mode, onMS, offMS, BRIGHTNESS_MODE_USER);
     64             }
     65         }
     66 
     67         @Override
     68         public void pulse() {
     69             pulse(0x00ffffff, 7);
     70         }
     71 
     72         @Override
     73         public void pulse(int color, int onMS) {
     74             synchronized (this) {
     75                 if (mColor == 0 && !mFlashing) {
     76                     setLightLocked(color, LIGHT_FLASH_HARDWARE, onMS, 1000, BRIGHTNESS_MODE_USER);
     77                     mColor = 0;
     78                     mH.sendMessageDelayed(Message.obtain(mH, 1, this), onMS);
     79                 }
     80             }
     81         }
     82 
     83         @Override
     84         public void turnOff() {
     85             synchronized (this) {
     86                 setLightLocked(0, LIGHT_FLASH_NONE, 0, 0, 0);
     87             }
     88         }
     89 
     90         private void stopFlashing() {
     91             synchronized (this) {
     92                 setLightLocked(mColor, LIGHT_FLASH_NONE, 0, 0, BRIGHTNESS_MODE_USER);
     93             }
     94         }
     95 
     96         private void setLightLocked(int color, int mode, int onMS, int offMS, int brightnessMode) {
     97             if (color != mColor || mode != mMode || onMS != mOnMS || offMS != mOffMS) {
     98                 if (DEBUG) Slog.v(TAG, "setLight #" + mId + ": color=#"
     99                         + Integer.toHexString(color));
    100                 mColor = color;
    101                 mMode = mode;
    102                 mOnMS = onMS;
    103                 mOffMS = offMS;
    104                 Trace.traceBegin(Trace.TRACE_TAG_POWER, "setLight(" + mId + ", 0x"
    105                         + Integer.toHexString(color) + ")");
    106                 try {
    107                     setLight_native(mNativePointer, mId, color, mode, onMS, offMS, brightnessMode);
    108                 } finally {
    109                     Trace.traceEnd(Trace.TRACE_TAG_POWER);
    110                 }
    111             }
    112         }
    113 
    114         private int mId;
    115         private int mColor;
    116         private int mMode;
    117         private int mOnMS;
    118         private int mOffMS;
    119         private boolean mFlashing;
    120     }
    121 
    122     public LightsService(Context context) {
    123         super(context);
    124 
    125         mNativePointer = init_native();
    126 
    127         for (int i = 0; i < LightsManager.LIGHT_ID_COUNT; i++) {
    128             mLights[i] = new LightImpl(i);
    129         }
    130     }
    131 
    132     @Override
    133     public void onStart() {
    134         publishLocalService(LightsManager.class, mService);
    135     }
    136 
    137     private final LightsManager mService = new LightsManager() {
    138         @Override
    139         public Light getLight(int id) {
    140             if (id < LIGHT_ID_COUNT) {
    141                 return mLights[id];
    142             } else {
    143                 return null;
    144             }
    145         }
    146     };
    147 
    148     @Override
    149     protected void finalize() throws Throwable {
    150         finalize_native(mNativePointer);
    151         super.finalize();
    152     }
    153 
    154     private Handler mH = new Handler() {
    155         @Override
    156         public void handleMessage(Message msg) {
    157             LightImpl light = (LightImpl)msg.obj;
    158             light.stopFlashing();
    159         }
    160     };
    161 
    162     private static native long init_native();
    163     private static native void finalize_native(long ptr);
    164 
    165     static native void setLight_native(long ptr, int light, int color, int mode,
    166             int onMS, int offMS, int brightnessMode);
    167 
    168     private long mNativePointer;
    169 }
    170