该功能在系统设置-显示-拿起设备时唤醒
java
alps\vendor\mediatek\proprietary\packages\apps\MtkSettings\src\com\android\settings\display\LiftToWakePreferenceController.java
public boolean isAvailable() {
SensorManager sensors = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
return sensors != null && sensors.getDefaultSensor(Sensor.TYPE_WAKE_GESTURE) != null;
}
通过以上代码可以控制该功能是不是在设置中显示,也可以看出来用到的sensor是Sensor.TYPE_WAKE_GESTURE,当然这个sensor是需要驱动配置好之后,上层才可以正常的控制该功能的开启和关闭。
java
alps\frameworks\base\core\java\android\hardware\SensorManager.java
public Sensor getDefaultSensor(int type) {
// TODO: need to be smarter, for now, just return the 1st sensor
List<Sensor> l = getSensorList(type);
boolean wakeUpSensor = false;
// For the following sensor types, return a wake-up sensor. These types are by default
// defined as wake-up sensors. For the rest of the SDK defined sensor types return a
// non_wake-up version.
if (type == Sensor.TYPE_PROXIMITY || type == Sensor.TYPE_SIGNIFICANT_MOTION
|| type == Sensor.TYPE_TILT_DETECTOR || type == Sensor.TYPE_WAKE_GESTURE
|| type == Sensor.TYPE_GLANCE_GESTURE || type == Sensor.TYPE_PICK_UP_GESTURE
|| type == Sensor.TYPE_LOW_LATENCY_OFFBODY_DETECT
|| type == Sensor.TYPE_WRIST_TILT_GESTURE
|| type == Sensor.TYPE_DYNAMIC_SENSOR_META || type == Sensor.TYPE_HINGE_ANGLE) {
wakeUpSensor = true;
}
for (Sensor sensor : l) {
if (sensor.isWakeUpSensor() == wakeUpSensor) return sensor;
}
return null;
}
alps\frameworks\base\services\core\java\com\android\server\policy\WakeGestureListener.java
public abstract void onWakeUp();
public boolean isSupported() {
synchronized (mLock) {
return mSensor != null;
}
}
public void requestWakeUpTrigger() {
synchronized (mLock) {
if (mSensor != null && !mTriggerRequested) {
mTriggerRequested = true;
mSensorManager.requestTriggerSensor(mListener, mSensor);
}
}
}
public void cancelWakeUpTrigger() {
synchronized (mLock) {
if (mSensor != null && mTriggerRequested) {
mTriggerRequested = false;
mSensorManager.cancelTriggerSensor(mListener, mSensor);
}
}
}
alps\frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.java
class MyWakeGestureListener extends WakeGestureListener {
MyWakeGestureListener(Context context, Handler handler) {
super(context, handler);
}
@Override
public void onWakeUp() {
synchronized (mLock) {
if (shouldEnableWakeGestureLp()) {
performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, false,
"Wake Up");
wakeUp(SystemClock.uptimeMillis(), mAllowTheaterModeWakeFromWakeGesture,
PowerManager.WAKE_REASON_GESTURE, "android.policy:GESTURE");
}
}
}
}
关键的方法是onWakeUp(),这个方法就是抬手的动作触发的操作,如果我们要在抬手的时候处理一些事情,那么可以在这个方法里面操作。
java
@Override
public void onWakeUp() {
synchronized (mLock) {
if (shouldEnableWakeGestureLp()) {
performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, false,
"Wake Up");
wakeUp(SystemClock.uptimeMillis(), mAllowTheaterModeWakeFromWakeGesture,
PowerManager.WAKE_REASON_GESTURE, "android.policy:GESTURE");
}
// Add on 2023/4/10 for start
if ((SystemProperties.getInt("ro.xxx_support", 0) == 1
|| SystemProperties.getInt("ro.xxx_support", 0) == 1)
&& Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.LIFT_ON, 1) == 1) {
Log.d("HZH", "PhoneWindowManager MyWakeGestureListener onWakeUp 890: open vicescreen");
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.xxx.xxx", "com.xxx.xxx.XService"));
intent.putExtra("broadcast_type", 109);
mContext.startForegroundService(intent);
}
// Add on 2023/4/10 for end
}
}
l流程比较简单,主要是给大家提供一个思路,如果有抬手相关的需求的时候,相信是会有帮助的