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
|
/*
* Copyright (C) 2017 Google Inc.
*
* 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.google.android.mobly.snippet.event;
import com.google.android.mobly.snippet.util.Log;
import java.util.Deque;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;
/**
* Manage the event queue.
*
* <p>EventCache APIs interact with the SnippetEvent cache - a data structure that holds {@link
* SnippetEvent} objects posted from snippet classes. The SnippetEvent cache provides a useful means
* of recording background events (such as sensor data) when the phone is busy with foreground
* activities.
*/
public class EventCache {
private static final String EVENT_DEQUE_ID_TEMPLATE = "%s|%s";
private static final int EVENT_DEQUE_MAX_SIZE = 1024;
// A Map with each value being the queue for a particular type of event, and the key being the
// unique ID of the queue. The ID is composed of a callback ID and an event's name.
private final Map<String, LinkedBlockingDeque<SnippetEvent>> mEventDeques = new HashMap<>();
private static volatile EventCache mEventCache;
private EventCache() {}
public static EventCache getInstance() {
if (mEventCache == null) {
synchronized (EventCache.class) {
if (mEventCache == null) {
mEventCache = new EventCache();
}
}
}
return mEventCache;
}
public static String getQueueId(String callbackId, String name) {
return String.format(Locale.US, EVENT_DEQUE_ID_TEMPLATE, callbackId, name);
}
public LinkedBlockingDeque<SnippetEvent> getEventDeque(String qId) {
synchronized (mEventDeques) {
LinkedBlockingDeque<SnippetEvent> eventDeque = mEventDeques.get(qId);
if (eventDeque == null) {
eventDeque = new LinkedBlockingDeque<>(EVENT_DEQUE_MAX_SIZE);
mEventDeques.put(qId, eventDeque);
}
return eventDeque;
}
}
/**
* Post an {@link SnippetEvent} object to the Event cache.
*
* <p>Snippet classes should use this method to post events. If EVENT_DEQUE_MAX_SIZE is reached,
* the oldest elements will be retired until the new event could be posted.
*
* @param snippetEvent The snippetEvent to post to {@link EventCache}.
*/
public void postEvent(SnippetEvent snippetEvent) {
String qId = getQueueId(snippetEvent.getCallbackId(), snippetEvent.getName());
Deque<SnippetEvent> q = getEventDeque(qId);
synchronized (q) {
while (!q.offer(snippetEvent)) {
SnippetEvent retiredEvent = q.removeFirst();
Log.v(
String.format(
Locale.US,
"Retired event %s due to deque reaching the size limit (%s).",
retiredEvent,
EVENT_DEQUE_MAX_SIZE));
}
}
Log.v(String.format(Locale.US, "Posted event(%s)", qId));
}
/** Clears all cached events. */
public void clearAll() {
synchronized (mEventDeques) {
mEventDeques.clear();
}
}
}
|