Android11 Framework添加Powerkey事件去抖

    需求:短时间来回触控powerkey造成屏幕来回亮灭切换,需要去抖动,即在每次响应执行完一次休眠或者唤醒事件后,再响应powerkey事件。

1、首先在framework中做了一个去抖动的工具类:

diff --git a/api/current.txt b/api/current.txt
index 689cf4833a0..e2eab020325 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -61605,6 +61605,17 @@ package android.widget.inline {
 
 }
 
+package android.yuanfudao.util {
+
+  public class Debouncer {
+    ctor public Debouncer();
+    ctor public Debouncer(long);
+    method public boolean ignore();
+    method public void setDebounceTime(long);
+  }
+
+}
+
 package dalvik.annotation {
 
   @Deprecated @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) @java.lang.annotation.Target({java.lang.annotation.ElementType.ANNOTATION_TYPE}) public @interface TestTarget {
diff --git a/core/java/android/yuanfudao/util/Debouncer.java b/core/java/android/yuanfudao/util/Debouncer.java
new file mode 100755
index 00000000000..83724e9e6d7
--- /dev/null
+++ b/core/java/android/yuanfudao/util/Debouncer.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2020 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 android.yuanfudao.util;
+
+import android.os.SystemClock;
+
+public class Debouncer {
+
+    private long debounceTime = 500L;
+
+    private long lastTriggerTimestamp;
+
+    public Debouncer() {}
+
+    public Debouncer(long paramLong) {
+        this.debounceTime = paramLong;
+    }
+
+    public boolean ignore() {
+        long l1 = SystemClock.elapsedRealtime();
+        long l2 = this.lastTriggerTimestamp;
+        if (l2 != 0L && l1 - l2 < this.debounceTime){
+            return true;
+        }else {
            this.lastTriggerTimestamp = l1;
+            return false;
+        }
+    }
+
+    public void setDebounceTime(long paramLong) {
+        this.debounceTime = paramLong;
+    }
+}
diff --git a/non-updatable-api/current.txt b/non-updatable-api/current.txt
index 07be752e01d..e9154685b05 100644
--- a/non-updatable-api/current.txt
+++ b/non-updatable-api/current.txt
@@ -59766,6 +59766,17 @@ package android.widget.inline {
 
 }
 
+package android.yuanfudao.util {
+
+  public class Debouncer {
+    ctor public Debouncer();
+    ctor public Debouncer(long);
+    method public boolean ignore();
+    method public void setDebounceTime(long);
+  }
+
+}
+
 package dalvik.annotation {
 
   @Deprecated @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) @java.lang.annotation.Target({java.lang.annotation.ElementType.ANNOTATION_TYPE}) public @interface TestTarget {

2、PowerManagerService添加相关逻辑:

--- a/services/core/java/com/android/server/power/PowerManagerService.java
+++ b/services/core/java/com/android/server/power/PowerManagerService.java
@@ -118,6 +118,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
+import android.yuanfudao.util.Debouncer;
 
 /**
  * The power manager service is responsible for coordinating power management
@@ -612,6 +613,8 @@ public final class PowerManagerService extends SystemService
     // Per-profile state to track when a profile should be locked.
     private final SparseArray<ProfilePowerState> mProfilePowerState = new SparseArray<>();
 
+       private Debouncer debouncer = new Debouncer(2500L);
+
     private static final class ProfilePowerState {
         // Profile user id.
         final @UserIdInt int mUserId;
@@ -1701,6 +1704,13 @@ public final class PowerManagerService extends SystemService
             return false;
         }
 
+        //Heguodong add - begin
+        if (this.debouncer.ignore()) {
+            Slog.d(TAG, "[YFD_PowerManagerService]wakeUpNoUpdateLocked,!!!rejected!!!" );
+            return false;
+        }
+        //Heguodong add - end
+
         //if (isAirplaneModeOn()) {
            // setAirplaneModeOn(false);
         //}
@@ -1763,6 +1773,13 @@ public final class PowerManagerService extends SystemService
             return false;
         }
 
+        //Heguodong add - begin
+        if (this.debouncer.ignore()) {
+            Slog.d(TAG, "[YFD_PowerManagerService]goToSleepNoUpdateLocked,!!!rejected!!!" );
+            return false;
+        }
+        //Heguodong add - end
+
         Trace.traceBegin(Trace.TRACE_TAG_POWER, "goToSleep");
         try {
             reason = Math.min(PowerManager.GO_TO_SLEEP_REASON_MAX,

增量编译验证:达到的效果是每次经历完一次完整的唤醒、休眠逻辑才会响应PowerKey事件。

Mark···

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值