summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/util/wakelock/WakeLock.java
blob: f320d071b54f8c3bc0e12301c969c5073e413f3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * 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.util.wakelock;

import android.content.Context;
import android.os.PowerManager;
import android.util.Log;

import androidx.annotation.VisibleForTesting;

import java.util.HashMap;

import javax.inject.Inject;

/** WakeLock wrapper for testability */
public interface WakeLock {

    static final String TAG = "WakeLock";
    static final String REASON_WRAP = "wrap";

    /**
     * Default wake-lock timeout in milliseconds, to avoid battery regressions.
     */
    long DEFAULT_MAX_TIMEOUT = 20000;

    /**
     * Default wake-lock levels and flags.
     */
    int DEFAULT_LEVELS_AND_FLAGS = PowerManager.PARTIAL_WAKE_LOCK;

    /**
     * @param why A tag that will be saved for sysui dumps.
     * @see android.os.PowerManager.WakeLock#acquire()
     **/
    void acquire(String why);

    /**
     * @param why Same tag used in {@link #acquire(String)}
     * @see android.os.PowerManager.WakeLock#release()
     **/
    void release(String why);

    /** @see android.os.PowerManager.WakeLock#wrap(Runnable) */
    Runnable wrap(Runnable r);

    static WakeLock createPartial(Context context, String tag) {
        return createPartial(context, tag, DEFAULT_MAX_TIMEOUT);
    }

    /**
     * Creates a {@link WakeLock} that has a default release timeout.
     * @see android.os.PowerManager.WakeLock#acquire(long) */
    static WakeLock createPartial(Context context, String tag, long maxTimeout) {
        return wrap(createWakeLockInner(context, tag, DEFAULT_LEVELS_AND_FLAGS), maxTimeout);
    }

    /**
     * Creates a {@link WakeLock} that has a default release timeout and flags.
     */
    static WakeLock createWakeLock(Context context, String tag, int flags, long maxTimeout) {
        return wrap(createWakeLockInner(context, tag, flags), maxTimeout);
    }

    @VisibleForTesting
    static PowerManager.WakeLock createWakeLockInner(
            Context context, String tag, int levelsAndFlags) {
        return context.getSystemService(PowerManager.class)
                    .newWakeLock(levelsAndFlags, tag);
    }

    static Runnable wrapImpl(WakeLock w, Runnable r) {
        w.acquire(REASON_WRAP);
        return () -> {
            try {
                r.run();
            } finally {
                w.release(REASON_WRAP);
            }
        };
    }

    /**
     * Create a {@link WakeLock} containing a {@link PowerManager.WakeLock}.
     * @param inner To be wrapped.
     * @param maxTimeout When to expire.
     * @return The new wake lock.
     */
    @VisibleForTesting
    static WakeLock wrap(final PowerManager.WakeLock inner, long maxTimeout) {
        return new WakeLock() {
            private final HashMap<String, Integer> mActiveClients = new HashMap<>();

            /** @see PowerManager.WakeLock#acquire() */
            public void acquire(String why) {
                mActiveClients.putIfAbsent(why, 0);
                mActiveClients.put(why, mActiveClients.get(why) + 1);
                inner.acquire(maxTimeout);
            }

            /** @see PowerManager.WakeLock#release() */
            public void release(String why) {
                Integer count = mActiveClients.get(why);
                if (count == null) {
                    Log.wtf(TAG, "Releasing WakeLock with invalid reason: " + why,
                            new Throwable());
                    return;
                } else if (count == 1) {
                    mActiveClients.remove(why);
                } else {
                    mActiveClients.put(why, count - 1);
                }
                inner.release();
            }

            /** @see PowerManager.WakeLock#wrap(Runnable) */
            public Runnable wrap(Runnable runnable) {
                return wrapImpl(this, runnable);
            }

            @Override
            public String toString() {
                return "active clients= " + mActiveClients.toString();
            }
        };
    }

    /**
     * An injectable Builder that wraps {@link #createPartial(Context, String, long)}.
     */
    class Builder {
        private final Context mContext;
        private String mTag;
        private int mLevelsAndFlags = DEFAULT_LEVELS_AND_FLAGS;
        private long mMaxTimeout = DEFAULT_MAX_TIMEOUT;

        @Inject
        public Builder(Context context) {
            mContext = context;
        }

        public Builder setTag(String tag) {
            this.mTag = tag;
            return this;
        }

        public Builder setLevelsAndFlags(int levelsAndFlags) {
            this.mLevelsAndFlags = levelsAndFlags;
            return this;
        }

        public Builder setMaxTimeout(long maxTimeout) {
            this.mMaxTimeout = maxTimeout;
            return this;
        }

        public WakeLock build() {
            return WakeLock.createWakeLock(mContext, mTag, mLevelsAndFlags, mMaxTimeout);
        }
    }
}