目前做的一个项目,仿的是三星S8曲面双摄的手机,有个功能就是后盖灯呼吸的效果,呼吸效果是驱动完成,上层我主要做的就是两个功能,一个是来电时呼吸,还有一个是充电时呼吸。先说一下来电呼吸功能的实现,其实就是发一个广播,还是很简单的。
1.在alps/packages/apps/Dialer/InCallUI/src/com/android/incallui/InCallPresenter.java
的onUiShowing(boolean showing)中添加发送广播的内容。
if (showing) {
+ Intent intent = new Intent("com.vanzo.logoLightReceiver.callingState");
+ mContext.sendBroadcast(intent);
mIsActivityPreviouslyStarted = true;
} else {
+ Intent intent = new Intent("com.vanzo.logoLightReceiver.notCallingState");
+ mContext.sendBroadcast(intent);
updateIsChangingConfigurations();
}
2.我将这个广播的实现放在了apls/frameworks/base/services/core/java/com/android/server/BatteryService.java
添加
import android.telephony.TelephonyManager;
import java.io.FileWriter;
private static final String LOGO_LIGHT_CALLING = "com.vanzo.logoLightReceiver.callingState";//拨打电话收到的广播
private static final String LOGO_LIGHT_NOTCALLING = "com.vanzo.logoLightReceiver.notCallingState";//停止拨打电话的广播
private static String LOGOLIGHT = "/sys/devices/platform/1100f000.i2c/i2c-5/5-0034/mt6370_pmu_rgbled/leds/mt6370_pmu_led4/brightness";//后盖灯的节点,由驱动提供
private static String BRETHMODE = "/sys/devices/platform/1100f000.i2c/i2c-5/5-0034/mt6370_pmu_rgbled/leds/mt6370_pmu_led4/trigger";//呼吸模式的节点,由驱动提供
自己写一下动态注册广播,接收
+ private BroadcastReceiver logoLightReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+ if ("com.vanzo.logoLightReceiver.callingState".equals(action)) {
+ setLedStatus("0", LOGOLIGHT);
+ setLedStatus("1" , LOGOLIGHT);
+ setLedStatus("breath_mode" , BRETHMODE);
+ }else if("com.vanzo.logoLightReceiver.notCallingState".equals(action)){
+ setLedStatus("0", LOGOLIGHT);
+ }
+ }
+ };
在构造方法中注册广播
+ IntentFilter mIntentFilter = new IntentFilter();
+ mIntentFilter.addAction(LOGO_LIGHT_CALLING);
+ mIntentFilter.addAction(LOGO_LIGHT_NOTCALLING);
+ context.registerReceiver(logoLightReceiver, mIntentFilter);
实现一下setLedStatus()这个方法
+ public void setLedStatus(String value, String ledPath) {
+ android.util.Log.d("libing", "setLedStatus enable =====" + value + " path " + ledPath);
+ FileWriter f1 = null;
+ File file = new File(ledPath);
+ try {
+ f1 = new FileWriter(file);
+ f1.write(value);
+ f1.flush();
+ f1.close();
+ } catch (Exception e) {
+ android.util.Log.d("libing", "setLedStatus e " + e.toString());
+ }
+ }
3.顺便写一下充电时实现呼吸的方法,都在BatteryService.java中,充电主要读取的是充电的状态,在什么状态就让它实现开启还是关闭。
private int getIconLocked(int level) {
if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
+ setLedStatus("0", LOGOLIGHT);
+ setLedStatus("1" , LOGOLIGHT);
+ setLedStatus("breath_mode" , BRETHMODE);
return com.android.internal.R.drawable.stat_sys_battery_charge;
} else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_DISCHARGING) {
+ setLedStatus("0", LOGOLIGHT);
return com.android.internal.R.drawable.stat_sys_battery;
} else if (mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_NOT_CHARGING
|| mBatteryProps.batteryStatus == BatteryManager.BATTERY_STATUS_FULL) {
+ setLedStatus("0", LOGOLIGHT);
if (isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY)
&& mBatteryProps.batteryLevel >= 100) {
return com.android.internal.R.drawable.stat_sys_battery_charge;
@@ -727,6 +766,7 @@ public final class BatteryService extends SystemService {
return com.android.internal.R.drawable.stat_sys_battery;
}
} else {
+ setLedStatus("0", LOGOLIGHT);
return com.android.internal.R.drawable.stat_sys_battery_unknown;
}
}