Android三色呼吸灯实现
三色(红绿蓝)呼吸灯,主要控制来电,短息,未接三种情况下不同颜色呼吸灯显示。
具体实现在应用层(Phone,mms)获取呼吸灯服务,调用预留的接口,传递频率和色值;
1 private void RGBBreathLedsSwitchOn() { 2 IBreathLedsService ledSvc = IBreathLedsService.Stub.asInterface(ServiceManager.getService("breath_leds")); // 获取呼吸灯服务 3 int time = 2; 4 int reqType = 0x80 | time; // 频率 5 int ledType = 1; // 色值 6 try { 7 if(FeatureOption.HEXING_CUSTOM_BREATH_LEDS) { // 宏控制代码 8 ledSvc.setLedsBrightness(reqType, ledType); // 调用setLedsBrightness()接口 9 Log.i(this, "Breathleds SwitchOn, reqType = " + reqType + ", ledType = " + ledType); 10 } else { 11 Log.i(this, "Not Support BreathLeds."); 12 } 13 } catch (Exception e) { 14 e.printStackTrace(); 15 } 16 }
在Service端,继承IBreathLedsService.aidl服务通讯,实现setLedsBrightness()接口,调用本地方法,访问JNI;
1 public class BreathLedsService extends IBreathLedsService.Stub { 2 3 // 亮度等级(32级) 4 private static final int[] BRIGHTNESS_LEVEL = { 5 0, 1, 2, 4, 6, 10, 13, 18, 6 22, 28, 33, 39, 46, 53, 61, 69, 7 78, 86, 96, 106, 116, 126, 138, 149, 8 161, 173, 186, 199, 212, 226, 240, 255, 9 }; 10 11 public BreathLedsService(Context context) { 12 init_native(); 13 } 14 15 public void turnOnLeds() { 16 set_brightness_native(0x80); 17 } 18 19 public void turnOffLeds() { 20 set_brightness_native(0x00); 21 } 22 23 public void setLedsBrightness(int which, int level) { 24 //if ((which > 12) || (which < 1)) return; 25 26 //level = level % 32; 27 int data = (which << 8) | level; 28 set_brightness_native(data); 29 } 30 31 private native void init_native(); 32 private native void set_brightness_native(int data); 33 }
注意:需在SystemServer.java中add服务
1 try { 2 Slog.i(TAG, "BreathLeds Service"); 3 ServiceManager.addService("breath_leds", new BreathLedsService(context)); // add service 4 } catch (Throwable e) { 5 Slog.e(TAG, "Failure starting BreathLeds Service", e); 6 }
在JNI中实现模块的初始化,函数映射和注册本地方法,完成呼吸灯模块的整个流程;
1 #define LOG_TAG "BreathLedsService" 2 3 #include "jni.h" 4 #include "JNIHelp.h" 5 #include "android_runtime/AndroidRuntime.h" 6 7 #include <hardware/hw_breath_leds.h> 8 #include <hardware/hardware.h> 9 10 namespace android { 11 12 struct breath_leds_device_t* leds_dev = NULL; // hw_breath_leds.h中定义的HAL设备结构体 13 14 static void leds_ctl_open(const struct hw_module_t* module, struct breath_leds_device_t** dev) 15 { 16 // 调用open函数进行一系列初始化工作 17 module->methods->open(module, BREATH_LEDS_HW_MODULE_ID, (struct hw_device_t**) dev); 18 } 19 20 static void init_leds(JNIEnv* env, jobject thiz) 21 { 22 breath_leds_module_t* leds_module = NULL; 23 24 // 通过hw_get_module函数查找HAL模块 25 if (hw_get_module(BREATH_LEDS_HW_MODULE_ID, (const hw_module_t**) &leds_module) == 0) 26 { 27 leds_ctl_open(&(leds_module->breath_module), &leds_dev); // 装载leds_dev 28 } 29 } 30 31 static void set_brightness_leds(JNIEnv* env, jobject thiz, jint level) 32 { 33 leds_dev->set_breath_value(leds_dev, level); 34 } 35 36 // 定义jni函数映射 37 static const JNINativeMethod method_tab[] = { 38 {"init_native", "()V", (void*) init_leds}, 39 {"set_brightness_native", "(I)V", (void*) set_brightness_leds}, 40 }; 41 42 int register_android_server_BreathLedsService(JNIEnv *env) 43 { 44 return AndroidRuntime::registerNativeMethods(env, 45 "com/android/server/BreathLedsService", method_tab, NELEM(method_tab)); 46 } 47 48 } /* namespace android */
感言:励志向Android系统框架层奋斗,不断学习,不断努力;从现在起,往垂直方向深度学习。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。