此次修改主要有俩部分:
一、android 9 把亮度调节粒度是前半段大(0-127),后半部(127-255) 小。基于这个愿意,客户希望能修改成原来 android os的版本一样的亮度调节粒度,也就是我理解的等差的亮度调节;
二、startActivityAsUser(new Intent(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG), UserHandle.CURRENT_OR_SELF);的方式来启动的,这样子启动activity的方式来弹出亮度调节框会导致正在处于前台的应用activity进入pause状态,会导致部分应用播放器暂停播放,基于这个愿意,我们来把亮度调节弹出框的弹出方式来做下修改,修改为弹出window的方式来进行提示。下面开始分享此次定制修改:
1、android系统按键处理是在PhoneWindowManager.java中,此次定制亮度调节弹窗,机器是带物理亮度调节按键的,所以从此处出发frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.java 。找到KEYCODE_BRIGHTNESS_UP 、KEYCODE_BRIGHTNESS_DOWN 事件处理的位置,然后修改成如下
if (keyCode == KeyEvent.KEYCODE_BRIGHTNESS_UP
|| keyCode == KeyEvent.KEYCODE_BRIGHTNESS_DOWN) {
if (down) {
int direction = (keyCode == KeyEvent.KEYCODE_BRIGHTNESS_UP)? 1 : -1;
// Disable autobrightness if it's on
int auto = Settings.System.getIntForUser(
mContext.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL,
UserHandle.USER_CURRENT_OR_SELF);
if (auto != 0) {
Settings.System.putIntForUser(mContext.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL,
UserHandle.USER_CURRENT_OR_SELF);
}
int min = mPowerManager.getMinimumScreenBrightnessSetting();
int max = mPowerManager.getMaximumScreenBrightnessSetting();
//int step = (max - min + BRIGHTNESS_STEPS - 1) / BRIGHTNESS_STEPS * direction;
int step = 20 * direction;
int brightness = Settings.System.getIntForUser(mContext.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS,
mPowerManager.getDefaultScreenBrightnessSetting(),
UserHandle.USER_CURRENT_OR_SELF);
brightness += step;
// Make sure we don't go beyond the limits.
brightness = Math.min(max, brightness);
brightness = Math.max(min, brightness);
Settings.System.putIntForUser(mContext.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, brightness,
UserHandle.USER_CURRENT_OR_SELF);
Intent intent1 = new Intent( );
intent1.setAction("com.starmini.action.brightness");
intent1.setPackage("com.android.systemui");
mContext.startService(intent1);
brightNessFlag = true;
mHandler.removeCallbacks(timeOut);
mHandler.postDelayed(timeOut,brightnessTimeout);
}
return -1;
}else {
if(brightNessFlag){
brightNessFlag = false;
mHandler.removeCallbacks(timeOut);
hideWindow();
}
}
此外还需要添加
private static boolean brightNessFlag = false;
private static int brightnessTimeout = 2500;//超时时间
Runnable timeOut = new Runnable() {
@Override
public void run() {
hideWindow();
}
};
private void hideWindow(){
Intent intent1 = new Intent( );
intent1.setAction("com.starmini.action.brightness");
intent1.setPackage("com.android.systemui");
mContext.stopService(intent1);
}
至此亮度调节跳转到了systemUI的BrightnessService.java中
package com.android.systemui.settings;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import com.android.systemui.R;
import android.view.ContextThemeWrapper;
import android.widget.ImageView;
public class BrightnessService extends Service {
public BrightnessService() {
}
private BrightnessController mBrightnessController;
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
showBrightnessWindow();
mBrightnessController.registerCallbacks();
}
@Override
public void onDestroy() {
super.onDestroy();
if(windowManager!=null && view!=null) {
windowManager.removeView(view);
}
mBrightnessController.unregisterCallbacks();
}
private WindowManager windowManager;
private WindowManager.LayoutParams params;
private View slider;
private View view;
private void showBrightnessWindow(){
windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_DRAG;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
|WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
params.format = PixelFormat.TRANSLUCENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.CENTER | Gravity.TOP;
ContextThemeWrapper themedContext = new ContextThemeWrapper(getApplicationContext(),
com.android.internal.R.style.Theme_DeviceDefault_QuickSettings);
view = LayoutInflater.from(themedContext).inflate(R.layout.quick_settings_brightness_dialog,null);
final ImageView icon = view.findViewById(R.id.brightness_icon);
final ToggleSliderView slider = view.findViewById(R.id.brightness_slider);
mBrightnessController = new BrightnessController(getApplicationContext(), icon, slider);
windowManager.addView(view,params);
}
}
service启动后弹出的亮度调节条是可以拖动进行亮度调节的,至此就进入到了BrightnessController中
在他初始化方法中修改mControl.setMax(GAMMA_SPACE_MAX);为 mControl.setMax(245);因为andorid默认最低亮度是10,所以我把进度条最大值设成245
在updateSlider方法中修改如下:
// final int sliderVal = convertLinearToGamma(val, min, max);
//animateSliderTo(sliderVal);
mControl.setValue(val-10);
而这个val的值是从BrightnessUtils中的convertGammaToLinear获取来的,所以去BrightnessUtils.java中把convertGammaToLinear方法做了修改如下:
//return Math.round(MathUtils.lerp(min, max, ret / 12));
return val;
自此本次修改完成,分享完毕
谢谢
1900 20191104 starmini
网友评论