android11.0上通过广播屏蔽电源键功能

2023-05-16

/framework/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

 import java.util.HashSet;
 import java.util.List;

+//xyx:
+import android.os.SystemProperties;
+//xyx
+
 /**
  * WindowManagerPolicy implementation for the Android phone UI.  This
  * introduces a new method suffix, Lp, for an internal lock of the
@@ -1129,6 +1133,13 @@ public class PhoneWindowManager implements WindowManagerPolicy {
     }

     private void powerPress(long eventTime, boolean interactive, int count) {
+               //add by xyx to control powerPress
+               boolean enable = SystemProperties.getBoolean("persist.sys.enablepower", true);
+               Log.d("xyx", "powerPress enable = " + enable);
+        if (!enable) {
+            return;
+        }
+               //end xyx
         if (mDefaultDisplayPolicy.isScreenOnEarly() && !mDefaultDisplayPolicy.isScreenOnFully()) {
             Slog.i(TAG, "Suppressed redundant power key press while "
                     + "already in the process of turning the screen on.");
@@ -1277,6 +1288,14 @@ public class PhoneWindowManager implements WindowManagerPolicy {
     }

     private void powerLongPress() {
+               //add by xyx to control powerLongPress
+        boolean enable = SystemProperties.getBoolean("persist.sys.enablepower", true);
+        Log.d("xyx", "powerLongPress enable = " + enable);
+        if (!enable) {
+            mPowerKeyHandled = true;   // in order to power key up no sleep
+            return;
+        }
+        //end xyx
         final int behavior = getResolvedLongPressOnPowerBehavior();
         switch (behavior) {
             case LONG_PRESS_POWER_NOTHING:
@@ -4645,6 +4664,13 @@ public class PhoneWindowManager implements WindowManagerPolicy {
     }

     private void wakeUpFromPowerKey(long eventTime) {
+               //add by xyx to control wakeUpFromPowerKey
+        boolean enable = SystemProperties.getBoolean("persist.sys.enablepower", true);
+        Log.d("xyx", "wakeUpFromPowerKey enable = " + enable);
+        if (!enable) {
+            return;
+        }
+        //end xyx
         wakeUp(eventTime, mAllowTheaterModeWakeFromPowerKey,
                 PowerManager.WAKE_REASON_POWER_BUTTON, "android.policy:POWER");
     }

powerPress、powerLongPress、wakeUpFromPowerKey分别对应:休眠,长按,唤醒三个场景

在这里直接return就可以达到屏蔽电源键功能的目的。

如果想要将短按和长按的控制逻辑分开,这里将powerLongPress的属性修改一下就可以了,后续广播的控制逻辑做一下改动即可。

按照上面改完PhoneWindowManager.java后,可以编译出来,然后通过adb来测试是否控制电源键成功:

由于SystemUI是系统级应用,且一开机便运行,因此在SystemUI里面添加静态广播。 

添加静态广播:

/framework/base/packages/SystemUI/AndroidManifast.xml

diff --git a/packages/SystemUI/AndroidManifest.xml b/packages/SystemUI/AndroidManifest.xml
index c8a902e..d597aed 100755
--- a/packages/SystemUI/AndroidManifest.xml
+++ b/packages/SystemUI/AndroidManifest.xml
@@ -346,6 +346,16 @@
                 <data android:scheme="package" />
             </intent-filter>
         </receiver>
+
+               <!-- add by xyx to control powerkey -->
+               <receiver android:name=".WiwoodPowerKeyReceiver"
+            android:enabled="true"
+                       android:exported="true">
+            <intent-filter>
+                <action android:name="action.wiwood.enable_powerkey" />
+            </intent-filter>
+        </receiver>
+               <!-- end xyx -->

         <service android:name=".ImageWallpaper"
                 android:permission="android.permission.BIND_WALLPAPER"

/framework/base/packages/SystemUI/src/com/android/systemui/WiwoodPowerKeyReceiver.java

diff --git a/packages/SystemUI/src/com/android/systemui/WiwoodPowerKeyReceiver.java b/packages/SystemUI/src/com/android/systemui/WiwoodPowerKeyReceiver.java
new file mode 100755
index 0000000..8e64b0b
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/WiwoodPowerKeyReceiver.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+package com.android.systemui;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+import android.os.SystemProperties;
+
+public class WiwoodPowerKeyReceiver extends BroadcastReceiver {
+       
+       private final String TAG = "WiwoodPowerKeyReceiver";
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+               
+               String isEnable = intent.getStringExtra("isEnable");
+               
+               Log.d(TAG, "Receive BroadCast:-----------------> action.wiwood.enable_powerkey : " + isEnable);
+               if (isEnable.equals("true")) {
+                       SystemProperties.set("persist.sys.enablepower", "1");
+               } else if (isEnable.equals("false")) {
+                       SystemProperties.set("persist.sys.enablepower", "0");
+               }
+    }
+}

静态广播添加完成之后,可以make SystemUI

将编译出来的SystemUI.apk push到对应路径下,然后reboot;

再通过adb的方式发送广播验证是否添加成功:

powerkey可用:adb shell am broadcast -n com.android.systemui/.WiwoodPowerKeyReceiver -a action.wiwood.enable_powerkey --es isEnable "true"

powerkey不可用:adb shell am broadcast -n com.android.systemui/.WiwoodPowerKeyReceiver -a action.wiwood.enable_powerkey --es isEnable "false"

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android11.0上通过广播屏蔽电源键功能 的相关文章

随机推荐

  • 《Python爬虫技术:深入理解原理、技术与开发》读书笔记(一)

    目录 前言第1章 基础知识第2章 爬虫基础HTTP基础URL与URI超文本HTTP与HTTPSHTTP的请求过程Network面版 前言 这是本系列的第一篇文章 xff0c 文如其题 xff0c 这个系列旨在学习Python爬虫技术 本系列
  • 给软件工程师的自学建议

    给软件工程师的自学建议 与现在大学生的情况类似 xff0c 学校学的专业知识总是与实际工作中需要的知识相差甚远 或许进入我们这个行业就注定要一辈子不离书本 不离学习了 由于软硬件技术的推陈出新 xff0c 学校教的C Basic Pasca
  • Python基础入门—for循环

    Python基础入门 for循环 for 循环 xff1a range的使用 xff1a 循环控制语句 xff1a for else的使用 xff1a for循环嵌套 xff1a for 循环 xff1a for循环格式 xff1a for
  • 软件测试之项目总结全攻略

    在我们测试工作过程中 xff0c 由于公司业务发展 xff0c 快速迭代等原因 xff0c 我们遇到的项目以小项目居多 更新界面元素 xff0c 上个活动页 xff0c 优化一下原有的功能等等 xff0c 加上事情繁琐 xff0c 任务多
  • 教你用Python写一个京东自动下单抢购脚本(Python实现京东自动抢购)

    很多朋友都有网购抢购限量商品的经历 有时候蹲点抢怎么也抢不到 今天小编带你们学习怎么用Python写一个京东自动下单抢购脚本 以后再也不用拼手速拼网速啦 快来一起看看吧 1 问题背景 经过无数次抢购失败后 xff0c 发现商家会不定时的放出
  • JAVA基础题练习

    顺序插入 xff1a 插入有序 Scanner input 61 new Scanner System in char arr2 61 39 b 39 39 d 39 39 f 39 39 i 39 39 k 39 39 m 39 39 x
  • ubuntu20.04安装qq音乐并解决闪退问题

    在qq音乐官网下载linux版coco音乐 xff1a https y qq com download download html 下载deb包并且通过下面命令行安装 xff1a span class token function sudo
  • 架构师装逼核武器

    架构师这个职位是很多程序猿的梦想 xff0c 我有很多朋友私下和我聊天的时候 xff0c 曾多次问我要如何才能成为一个架构师 xff0c 对于这个问题 xff0c 我只能粗略的谈谈我个人的观点 xff0c 如有不同观点 xff0c 欢迎交流
  • 一文讲透java日志框架

    在项目开发过程中 xff0c 有一个必不可少的环节就是记录日志 xff0c 相信只要是个程序员都用过 xff0c 可是咱们自问下 xff0c 用了这么多年的日志框架 xff0c 你确定自己真弄懂了日志框架的来龙去脉嘛 xff1f 下面笔者就
  • HashMap底层原理

    在我们实际的项目中 xff0c HashMap这个集合类经常被用到 xff0c 可是就是这么一个常用的集合类 xff0c 却往往成了咱们面试中的绊脚石 即便你是个初级程序员 xff0c 也常会让你谈谈HashMap的底层原理 xff0c 今
  • 聊聊ThreadPoolExecutor线程池

    ThreadPoolExecutor是线程的池化技术 xff0c 也就是首先创建几个线程 xff0c 然后把线程放到池子里 xff0c 有任务来的时候直接从线程池中拉线程来执行任务 为什么要用池化技术 xff1f java中的线程是系统级别
  • Spring框架系列之bean的生命周期底层原理06

    bean的生命周期 xff0c 咱们必须从 AnnotationConfigApplicationContext的getBean方法开始 xff0c getBean顾名思义就是从Spring容器中得到一个Bean的实例对象 xff0c Sp
  • 电信运营商移动互联网发展分析

    电信运营商移动互联网发展分析 移动互联网是通信业发展的大趋势 xff0c 随着3G 和WiMAX 等高速无线接入技术的飞速发展 xff0c 移动互联网不仅继承固定互联网的很多技术 xff0c 并且在商务 娱乐以及移动性等方面拓展用户需求 自
  • Spring框架系列之bean的生命周期底层原理07

    上一篇我们预留了两个大的内容 xff0c 一个是Object sharedInstance 61 getSingleton beanName 从单例池中获取数据 xff0c 另外一个是getSingleton方法创建单例Bean xff0c
  • Spring框架系列之bean的生命周期底层原理08

    接着上一篇 xff0c 咱们继续doCreateBean方法的分析 xff0c doCreateBean内容比较多 xff0c 我们这次主要是把它的整体流程说下 xff0c 后续会逐个来分析每一个关键点 代码如下 xff1a protect
  • 2020-09-25 Python基础学习第三天笔记

    文章目录 一 可变字符串二 运算符三 列表1 列表的创建2 列表常用命令3 多维列表 四 元组 2020 9 24 Day3 一 可变字符串 需要原地修改字符串 xff0c 可以使用 io StringIO 对象或 array 模块 spa
  • Python Cookbook学习总结

    第一章 xff1a 数据结构和算法 任何序列 xff08 可迭代的对象 xff09 都可以通过一个简单的赋值操作来分解为单独的变量 xff0c 唯一的要求是变量的总数和结构要与序列相吻合 xff08 比如对于存储二维坐标等的二维数组 xff
  • SpringBoot解析yml/yaml/properties配置文件的四种方式汇总

    目录 一 配置文件注入方式一 64 Value 二 配置文件注入方式二 64 ConfigurationProperties 三 自定义解析类 xff0c 直接暴力读取yml配置文件 四 Spring配置文件的解析类Environment获
  • Linux下配置Apache为多端口 (centos7)

    apache设置多个不同的端口 xff0c 映射不同的文件 一 xff1a vim etc httpd conf httpd conf 查看http配置文件 滑倒最底部 xff0c 箭头标注的位置 我们需要进入该目录编辑 二 xff1a c
  • android11.0上通过广播屏蔽电源键功能

    framework base services core java com android server policy PhoneWindowManager java import java util HashSet import java