Mr.Alright---MTK安卓13 抬手亮屏功能的逻辑

该功能在系统设置-显示-拿起设备时唤醒

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流程比较简单,主要是给大家提供一个思路,如果有抬手相关的需求的时候,相信是会有帮助的

相关推荐
Crossoads1 小时前
【汇编语言】外中断(一)—— 外中断的魔法:PC机键盘如何触发计算机响应
android·开发语言·数据库·深度学习·机器学习·计算机外设·汇编语言
sunphp开发者2 小时前
黑客攻击网站,篡改首页问题排查修复
android·js
我又来搬代码了2 小时前
【Android Studio】创建新项目遇到的一些问题
android·ide·android studio
ggs_and_ddu6 小时前
Android--java实现手机亮度控制
android·java·智能手机
明明真系叻8 小时前
第二十六周机器学习笔记:PINN求正反解求PDE文献阅读——正问题
人工智能·笔记·深度学习·机器学习·1024程序员节
zhangphil12 小时前
Android绘图Path基于LinearGradient线性动画渐变,Kotlin(2)
android·kotlin
watl012 小时前
【Android】unzip aar删除冲突classes再zip
android·linux·运维
键盘上的蚂蚁-12 小时前
PHP爬虫类的并发与多线程处理技巧
android
喜欢猪猪13 小时前
Java技术专家视角解读:SQL优化与批处理在大数据处理中的应用及原理
android·python·adb
JasonYin~15 小时前
HarmonyOS NEXT 实战之元服务:静态案例效果---手机查看电量
android·华为·harmonyos