1 /*
2  * Copyright (C) 2014 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 android.hardware.cts.helpers.sensorverification;
18 
19 import android.hardware.cts.helpers.TestSensorEvent;
20 
21 import java.util.Collection;
22 import java.util.List;
23 
24 /**
25  * Abstract class that deals with the synchronization of the sensor verifications.
26  */
27 public abstract class AbstractSensorVerification implements ISensorVerification {
28 
29     /**
30      * {@inheritDoc}
31      */
32     @Override
addSensorEvents(Collection<TestSensorEvent> events)33     public synchronized void addSensorEvents(Collection<TestSensorEvent> events) {
34         for (TestSensorEvent event : events) {
35             addSensorEventInternal(event);
36         }
37     }
38 
39     /**
40      * {@inheritDoc}
41      */
42     @Override
clone()43     public abstract ISensorVerification clone();
44 
45     /**
46      * Used by implementing classes to add a sensor event.
47      */
addSensorEventInternal(TestSensorEvent event)48     protected abstract void addSensorEventInternal(TestSensorEvent event);
49 
getIndexArray(List<TEvent> indexedEvents)50     protected <TEvent extends IndexedEvent> int[] getIndexArray(List<TEvent> indexedEvents) {
51         int eventsCount = indexedEvents.size();
52         int[] indices = new int[eventsCount];
53         for (int i = 0; i < eventsCount; i++) {
54             indices[i] = indexedEvents.get(i).index;
55         }
56         return indices;
57     }
58 
59     /**
60      * Helper class to store the index and current event.
61      * Events are added to the verification in the order they are generated, the index represents
62      * the position of the given event, in the list of added events.
63      */
64     protected class IndexedEvent {
65         public final int index;
66         public final TestSensorEvent event;
67 
IndexedEvent(int index, TestSensorEvent event)68         public IndexedEvent(int index, TestSensorEvent event) {
69             this.index = index;
70             this.event = event;
71         }
72     }
73 
74     /**
75      * Helper class to store the index, previous event, and current event.
76      */
77     protected class IndexedEventPair extends IndexedEvent {
78         public final TestSensorEvent previousEvent;
79 
IndexedEventPair(int index, TestSensorEvent event, TestSensorEvent previousEvent)80         public IndexedEventPair(int index, TestSensorEvent event, TestSensorEvent previousEvent) {
81             super(index, event);
82             this.previousEvent = previousEvent;
83         }
84     }
85 
nanosToMillis(long nanos)86     protected double nanosToMillis(long nanos) {
87         return nanos/(1000.0 * 1000.0);
88     }
89 }
90