Home | History | Annotate | Download | only in helpers
      1 /*
      2  * Copyright (C) 2013 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;
     18 
     19 import android.hardware.Sensor;
     20 import android.hardware.SensorEvent;
     21 import android.hardware.SensorEventListener2;
     22 import android.os.SystemClock;
     23 
     24 import java.util.Arrays;
     25 
     26 /**
     27  * Class for holding information about individual {@link SensorEvent}s.
     28  */
     29 public class TestSensorEvent {
     30     public final Sensor sensor;
     31     public final long timestamp;
     32     public final long receivedTimestamp;
     33     public final int accuracy;
     34     public final float values[];
     35 
     36     /**
     37      * Constructor that sets {@link #receivedTimestamp} to
     38      * {@link SystemClock#elapsedRealtimeNanos()}
     39      *
     40      * @param event the received sensor event
     41      */
     42     public TestSensorEvent(SensorEvent event) {
     43         this(event, SystemClock.elapsedRealtimeNanos());
     44     }
     45 
     46     /**
     47      * Construct a TestSensorEvent from {@link SensorEvent} data and a received timestamp.
     48      *
     49      * @param event the {@link SensorEvent} to be cloned
     50      * @param receivedTimestamp the timestamp when
     51      * {@link SensorEventListener2#onSensorChanged(SensorEvent)} was called, in nanoseconds.
     52      */
     53     public TestSensorEvent(SensorEvent event, long receivedTimestamp) {
     54         values = event.values.clone();
     55         sensor = event.sensor;
     56         timestamp = event.timestamp;
     57         accuracy = event.accuracy;
     58 
     59         this.receivedTimestamp = receivedTimestamp;
     60     }
     61 
     62     /**
     63      * Constructor for TestSensorEvent. Exposed for unit testing.
     64      */
     65     public TestSensorEvent(Sensor sensor, long timestamp, int accuracy, float[] values) {
     66         this(sensor, timestamp, timestamp, accuracy, values);
     67     }
     68 
     69     /**
     70      * Constructor for TestSensorEvent. Exposed for unit testing.
     71      */
     72     public TestSensorEvent(Sensor sensor, long timestamp, long receivedTimestamp, int accuracy,
     73             float[] values) {
     74         this.sensor = sensor;
     75         this.timestamp = timestamp;
     76         this.receivedTimestamp = receivedTimestamp;
     77         this.accuracy = accuracy;
     78         this.values = values;
     79     }
     80 
     81     @Override
     82     public String toString() {
     83         return String.format(
     84                 "Timestamp=%sns, ReceivedTimestamp=%sns, Accuracy=%s, Values=%s",
     85                 this.timestamp,
     86                 this.receivedTimestamp,
     87                 this.accuracy,
     88                 Arrays.toString(this.values));
     89     }
     90 }
     91