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系統框架層奮斗,不斷學習,不斷努力;從現在起,往垂直方向深度學習。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM