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