需求:短时间来回触控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 {
文章来源地址https://uudwc.com/A/nJD02
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事件。
文章来源:https://uudwc.com/A/nJD02
Mark···