ANDROID8.0 双击POWER键启动流程及音量面板调取需求实现
标签: Android 双击power键启动流程 双击power键 Android启动音量面板 Android power键
之前参考双击调用Camera,添加了一个双击启动音量面板的功能,为啥?因为这手机只有power键!
实现功能前,先摸清流程对我们的开发会更方便。
POWER键大概流程
这里写图片描述PhoneWindowManager.java 类是 处理各种 power 键流程的地方,
case KeyEvent.KEYCODE_POWER:
if (down) {
interceptPowerKeyDown(event, interactive);
} else {
interceptPowerKeyUp(event, interactive, canceled);
}
power键按下在interceptPowerKeyDown()执行,松开的操作在interceptPowerKeyUp()中执行
interceptPowerKeyDown()方法中会调用 GestureLauncherService.java 的 interceptPowerKeyDown() 方法
GestureLauncherService gestureService = LocalServices.getService(
GestureLauncherService.class);
boolean gesturedServiceIntercepted = false;
if (gestureService != null) {
gesturedServiceIntercepted = gestureService.interceptPowerKeyDown(event, interactive,
mTmpBoolean);
if (mTmpBoolean.value && mRequestedOrGoingToSleep) {
mCameraGestureTriggeredDuringGoingToSleep = true;
}
}
跟踪看看GestureLauncherService.java 中 执行 interceptPowerKeyDown()方法如下
public boolean interceptPowerKeyDown(KeyEvent event, boolean interactive,
MutableBoolean outLaunched) {
boolean launched = false;
boolean intercept = false;
long powerTapInterval;
synchronized (this) {
powerTapInterval = event.getEventTime() - mLastPowerDown;
if (mCameraDoubleTapPowerEnabled
&& powerTapInterval < CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS) {//双击事件
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DOUBLE_TAP_TAG,1);
launched = true;
intercept = interactive;
mPowerButtonConsecutiveTaps++;
} else if (powerTapInterval < POWER_SHORT_TAP_SEQUENCE_MAX_INTERVAL_MS) {//延迟双击事件
mPowerButtonConsecutiveTaps++;
} else {//单击事件
mPowerButtonConsecutiveTaps = 1;
}
mLastPowerDown = event.getEventTime();
}
if (DBG && mPowerButtonConsecutiveTaps > 1) {
Slog.i(TAG, Long.valueOf(mPowerButtonConsecutiveTaps) +
" consecutive power button taps detected");
}
if (launched) {
Slog.i(TAG, "Power button double tap gesture detected, launching camera. Interval="
+ powerTapInterval + "ms");
//调用开启相机
launched = handleCameraGesture(false/* useWakelock */,
StatusBarManager.CAMERA_LAUNCH_SOURCE_POWER_DOUBLE_TAP);
if (launched) {
mMetricsLogger.action(MetricsEvent.ACTION_DOUBLE_TAP_POWER_CAMERA_GESTURE,
(int) powerTapInterval);
}
}
mMetricsLogger.histogram("power_consecutive_short_tap_count", mPowerButtonConsecutiveTaps);
mMetricsLogger.histogram("power_double_tap_interval", (int) powerTapInterval);
outLaunched.value = launched;
return intercept && launched;
}
系统会对mCameraDoubleTapPowerEnabled 取值,核心是通过resources.getBoolean(com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled) 来取,
其实这个值是配置在/frameworks/base/core/res/res/values/config.xml中,这里是为true的
<!-- Allow the gesture to double tap the power button twice to start the camera while the deviceis non-interactive. -->
<bool name="config_cameraDoubleTapPowerGestureEnabled">true</bool>12
接下来会分别对power连续按2或者1次进行判断,如果mCameraDoubleTapPowerEnabled = true 会通过比较按键的时间powerTapInterval小于系统默认时间(CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS=300毫秒),
mPowerButtonConsecutiveTaps计数加1,说明连续按power键,或者延迟最大500毫秒内连续按键,系统预计用户接下来可能会执行一些操作,计数也会加1.
static final long POWER_SHORT_TAP_SEQUENCE_MAX_INTERVAL_MS = 500;
static final long CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS = 300;
powerTapInterval = event.getEventTime() - mLastPowerDown;
mLastPowerDown = event.getEventTime();
if (mCameraDoubleTapPowerEnabled && powerTapInterval < CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS)
if (powerTapInterval < POWER_SHORT_TAP_SEQUENCE_MAX_INTERVAL_MS)
执行完成之后会调用 handleCameraGesture()方法调用开启摄像机。
@VisibleForTesting
boolean handleCameraGesture(boolean useWakelock, int source) {
boolean userSetupComplete = Settings.Secure.getIntForUser(mContext.getContentResolver(),
Settings.Secure.USER_SETUP_COMPLETE, 0, UserHandle.USER_CURRENT) != 0;
if (!userSetupComplete) {
if (DBG) Slog.d(TAG, String.format(
"userSetupComplete = %s, ignoring camera gesture.",
userSetupComplete));
return false;
}
if (DBG) Slog.d(TAG, String.format(
"userSetupComplete = %s, performing camera gesture.",
userSetupComplete));
if (useWakelock) {
// Make sure we don't sleep too early
mWakeLock.acquire(500L);
}
StatusBarManagerInternal service = LocalServices.getService(
StatusBarManagerInternal.class);
//启动相机
service.onCameraLaunchGestureDetected(source);
return true;
}
原生双击启动相机流程差不多是这样,后面service不详细说,但是我们这里是需要启动音量面板啊~
需求实现
流程缕清了实现起来就很方便了,现在双击事件那边记录下一个标记值
if (mCameraDoubleTapPowerEnabled&& powerTapInterval < CAMERA_POWER_DOUBLE_TAP_MAX_TIME_MS) {
//双击事件,添加标记为1
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DOUBLE_TAP_TAG,1);
然后在外层 PhoneWindowManager.java 添加如果在setting那边选了调用音量面板模式和双击标记值符合则调用音量面板显示即可完成需求。
case KeyEvent.KEYCODE_POWER: {
final int powerKeyModeTag = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0);
int doubleTapTag = Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.DOUBLE_TAP_TAG, 0);
mCameraDoubleTapPowerEnabled = powerKeyModeTag == 2 ? true : false;
mButtonDoubleTapPowerEnabled = doubleTapTag == 1 ? true : false;
if (mCameraDoubleTapPowerEnabled
&& mButtonDoubleTapPowerEnabled) {
AudioManager am = (AudioManager)mContext. getSystemService(mContext.AUDIO_SERVICE);
am.adjustVolume(AudioManager.ADJUST_SAME, AudioManager.FLAG_SHOW_UI) ;
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.DOUBLE_TAP_TAG,0);
return ACTION_PASS_TO_USER;
}
...
break;
}
如果大家有更好的办法欢迎指导交流~~
版权声明:本文为ouzhuangzhuang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ouzhuangzhuang/article/details/82150180
网友评论