• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.server.location.injector;
18 
19 import android.app.AlarmManager.OnAlarmListener;
20 import android.os.WorkSource;
21 
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 
25 public class FakeAlarmHelper extends AlarmHelper {
26 
27     private static class Alarm {
28         public long delayMs;
29         public final OnAlarmListener listener;
30 
Alarm(long delayMs, OnAlarmListener listener)31         Alarm(long delayMs, OnAlarmListener listener) {
32             this.delayMs = delayMs;
33             this.listener = listener;
34         }
35     }
36 
37     private final ArrayList<Alarm> mAlarms = new ArrayList<>();
38 
39     @Override
setDelayedAlarmInternal(long delayMs, OnAlarmListener listener, WorkSource workSource)40     public void setDelayedAlarmInternal(long delayMs, OnAlarmListener listener,
41             WorkSource workSource) {
42         mAlarms.add(new Alarm(delayMs, listener));
43     }
44 
45     @Override
cancel(OnAlarmListener listener)46     public void cancel(OnAlarmListener listener) {
47         mAlarms.removeIf(alarm -> alarm.listener == listener);
48     }
49 
incrementAlarmTime(long incrementMs)50     public void incrementAlarmTime(long incrementMs) {
51         Iterator<Alarm> it = mAlarms.iterator();
52         while (it.hasNext()) {
53             Alarm alarm = it.next();
54             alarm.delayMs -= incrementMs;
55             if (alarm.delayMs <= 0) {
56                 it.remove();
57                 alarm.listener.onAlarm();
58             }
59         }
60     }
61 }
62