GpsMeasurementsEvent.java revision a62050d42c7d76d57ae555ffcb6d8efc5cf79de1
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
17package android.location;
18
19import android.annotation.NonNull;
20import android.annotation.SystemApi;
21import android.os.Parcel;
22import android.os.Parcelable;
23
24import java.security.InvalidParameterException;
25import java.util.Arrays;
26import java.util.Collection;
27import java.util.Collections;
28
29/**
30 * A class implementing a container for data associated with a measurement event.
31 * Events are delivered to registered instances of {@link Listener}.
32 *
33 * @hide
34 */
35@SystemApi
36public class GpsMeasurementsEvent implements Parcelable {
37    private final GpsClock mClock;
38    private final Collection<GpsMeasurement> mReadOnlyMeasurements;
39
40    /**
41     * Used for receiving GPS satellite measurements from the GPS engine.
42     * Each measurement contains raw and computed data identifying a satellite.
43     * You can implement this interface and call {@link LocationManager#addGpsMeasurementListener}.
44     *
45     * @hide
46     */
47    @SystemApi
48    public interface Listener {
49        void onGpsMeasurementsReceived(GpsMeasurementsEvent eventArgs);
50    }
51
52    public GpsMeasurementsEvent(GpsClock clock, GpsMeasurement[] measurements) {
53        if (clock == null) {
54            throw new InvalidParameterException("Parameter 'clock' must not be null.");
55        }
56        if (measurements == null || measurements.length == 0) {
57            throw new InvalidParameterException(
58                    "Parameter 'measurements' must not be null or empty.");
59        }
60
61        mClock = clock;
62        Collection<GpsMeasurement> measurementCollection = Arrays.asList(measurements);
63        mReadOnlyMeasurements = Collections.unmodifiableCollection(measurementCollection);
64    }
65
66    @NonNull
67    public GpsClock getClock() {
68        return mClock;
69    }
70
71    /**
72     * Gets a read-only collection of measurements associated with the current event.
73     */
74    @NonNull
75    public Collection<GpsMeasurement> getMeasurements() {
76        return mReadOnlyMeasurements;
77    }
78
79    public static final Creator<GpsMeasurementsEvent> CREATOR =
80            new Creator<GpsMeasurementsEvent>() {
81        @Override
82        public GpsMeasurementsEvent createFromParcel(Parcel in) {
83            ClassLoader classLoader = getClass().getClassLoader();
84
85            GpsClock clock = in.readParcelable(classLoader);
86
87            int measurementsLength = in.readInt();
88            GpsMeasurement[] measurementsArray = new GpsMeasurement[measurementsLength];
89            in.readTypedArray(measurementsArray, GpsMeasurement.CREATOR);
90
91            return new GpsMeasurementsEvent(clock, measurementsArray);
92        }
93
94        @Override
95        public GpsMeasurementsEvent[] newArray(int size) {
96            return new GpsMeasurementsEvent[size];
97        }
98    };
99
100    @Override
101    public int describeContents() {
102        return 0;
103    }
104
105    @Override
106    public void writeToParcel(Parcel parcel, int flags) {
107        parcel.writeParcelable(mClock, flags);
108
109        GpsMeasurement[] measurementsArray = mReadOnlyMeasurements.toArray(new GpsMeasurement[0]);
110        parcel.writeInt(measurementsArray.length);
111        parcel.writeTypedArray(measurementsArray, flags);
112    }
113
114    @Override
115    public String toString() {
116        StringBuilder builder = new StringBuilder("[ GpsMeasurementsEvent:\n\n");
117
118        builder.append(mClock.toString());
119        builder.append("\n");
120
121        for (GpsMeasurement measurement : mReadOnlyMeasurements) {
122            builder.append(measurement.toString());
123            builder.append("\n");
124        }
125
126        builder.append("]");
127
128        return builder.toString();
129    }
130}
131