Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/onehanded/OneHandedTimeoutHandler.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.wm.shell.onehanded;
18
19 import static com.android.wm.shell.onehanded.OneHandedSettingsUtil.ONE_HANDED_TIMEOUT_MEDIUM_IN_SECONDS;
20
21 import androidx.annotation.NonNull;
22 import androidx.annotation.VisibleForTesting;
23
24 import com.android.wm.shell.common.ShellExecutor;
25
26 import java.io.PrintWriter;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.concurrent.TimeUnit;
30
31 /**
32 * Timeout handler for stop one handed mode operations.
33 */
34 public class OneHandedTimeoutHandler {
35 private static final String TAG = "OneHandedTimeoutHandler";
36
37 private final ShellExecutor mMainExecutor;
38
39 // Default timeout is ONE_HANDED_TIMEOUT_MEDIUM
40 private @OneHandedSettingsUtil.OneHandedTimeout int mTimeout =
41 ONE_HANDED_TIMEOUT_MEDIUM_IN_SECONDS;
42 private long mTimeoutMs = TimeUnit.SECONDS.toMillis(mTimeout);
43 private final Runnable mTimeoutRunnable = this::onStop;
44 private List<TimeoutListener> mListeners = new ArrayList<>();
45
46 /**
47 * Get the current config of timeout
48 *
49 * @return timeout of current config
50 */
51 public @OneHandedSettingsUtil.OneHandedTimeout int getTimeout() {
52 return mTimeout;
53 }
54
55 /**
56 * Listens for notify timeout events
57 */
58 public interface TimeoutListener {
59 /**
60 * Called whenever the config time out
61 *
62 * @param timeoutTime The time in seconds to trigger timeout
63 */
64 void onTimeout(int timeoutTime);
65 }
66
67 public OneHandedTimeoutHandler(ShellExecutor mainExecutor) {
68 mMainExecutor = mainExecutor;
69 }
70
71 /**
72 * Set the specific timeout of {@link OneHandedSettingsUtil.OneHandedTimeout}
73 */
74 public void setTimeout(@OneHandedSettingsUtil.OneHandedTimeout int timeout) {
75 mTimeout = timeout;
76 mTimeoutMs = TimeUnit.SECONDS.toMillis(mTimeout);
77 resetTimer();
78 }
79
80 /**
81 * Reset the timer when one handed trigger or user is operating in some conditions
82 */
83 public void removeTimer() {
84 mMainExecutor.removeCallbacks(mTimeoutRunnable);
85 }
86
87 /**
88 * Reset the timer when one handed trigger or user is operating in some conditions
89 */
90 public void resetTimer() {
91 removeTimer();
92 if (mTimeout == OneHandedSettingsUtil.ONE_HANDED_TIMEOUT_NEVER) {
93 return;
94 }
95 if (mTimeout != OneHandedSettingsUtil.ONE_HANDED_TIMEOUT_NEVER) {
96 mMainExecutor.executeDelayed(mTimeoutRunnable, mTimeoutMs);
97 }
98 }
99
100 /**
101 * Register timeout listener to receive time out events
102 *
103 * @param listener the listener be sent events when times up
104 */
105 public void registerTimeoutListener(TimeoutListener listener) {
106 mListeners.add(listener);
107 }
108
109 @VisibleForTesting
110 boolean hasScheduledTimeout() {
111 return mMainExecutor.hasCallback(mTimeoutRunnable);
112 }
113
114 private void onStop() {
115 for (int i = mListeners.size() - 1; i >= 0; i--) {
116 final TimeoutListener listener = mListeners.get(i);
117 listener.onTimeout(mTimeout);
118 }
119 }
120
121 void dump(@NonNull PrintWriter pw) {
122 final String innerPrefix = " ";
123 pw.println(TAG);
124 pw.print(innerPrefix + "sTimeout=");
125 pw.println(mTimeout);
126 pw.print(innerPrefix + "sListeners=");
127 pw.println(mListeners);
128 }
129
130 }