Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.car.vms; |
| 18 | |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.SystemApi; |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
Asaf Rosenfeld | f9be20b | 2017-06-30 18:20:08 -0700 | [diff] [blame] | 23 | |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 24 | import java.util.ArrayList; |
| 25 | import java.util.Collections; |
Asaf Rosenfeld | f9be20b | 2017-06-30 18:20:08 -0700 | [diff] [blame] | 26 | import java.util.HashSet; |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 27 | import java.util.List; |
Asaf Rosenfeld | 9e1c315 | 2018-02-02 08:47:10 -0800 | [diff] [blame] | 28 | import java.util.Objects; |
Asaf Rosenfeld | f9be20b | 2017-06-30 18:20:08 -0700 | [diff] [blame] | 29 | import java.util.Set; |
| 30 | |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 31 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 32 | * Subscription state of Vehicle Map Service layers. |
| 33 | * |
| 34 | * The subscription state is used by publishers to determine which layers to publish data for, as |
| 35 | * any data published to a layer without subscribers will be dropped by the Vehicle Map Service. |
| 36 | * |
| 37 | * Sequence numbers are used to indicate the succession of subscription states, and increase |
| 38 | * monotonically with each change in subscriptions. They must be used by clients to ignore states |
| 39 | * that are received out-of-order. |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 40 | * |
| 41 | * @hide |
| 42 | */ |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 43 | @SystemApi |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 44 | public final class VmsSubscriptionState implements Parcelable { |
| 45 | private final int mSequenceNumber; |
Asaf Rosenfeld | f9be20b | 2017-06-30 18:20:08 -0700 | [diff] [blame] | 46 | private final Set<VmsLayer> mLayers; |
| 47 | private final Set<VmsAssociatedLayer> mSubscribedLayersFromPublishers; |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 50 | * Constructs a summary of the state of the current subscriptions for publishers to consume |
Asaf Rosenfeld | f9be20b | 2017-06-30 18:20:08 -0700 | [diff] [blame] | 51 | * and adjust which layers that the are publishing. |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 52 | */ |
Asaf Rosenfeld | f9be20b | 2017-06-30 18:20:08 -0700 | [diff] [blame] | 53 | public VmsSubscriptionState(int sequenceNumber, |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 54 | @NonNull Set<VmsLayer> subscribedLayers, |
| 55 | @NonNull Set<VmsAssociatedLayer> layersFromPublishers) { |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 56 | mSequenceNumber = sequenceNumber; |
Asaf Rosenfeld | f9be20b | 2017-06-30 18:20:08 -0700 | [diff] [blame] | 57 | mLayers = Collections.unmodifiableSet(subscribedLayers); |
| 58 | mSubscribedLayersFromPublishers = Collections.unmodifiableSet(layersFromPublishers); |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 61 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 62 | * @return sequence number of the subscription state |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 63 | */ |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 64 | public int getSequenceNumber() { |
| 65 | return mSequenceNumber; |
| 66 | } |
| 67 | |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 68 | /** |
| 69 | * @return set of layers with subscriptions to all publishers |
| 70 | */ |
| 71 | @NonNull |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 72 | public Set<VmsLayer> getLayers() { |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 73 | return mLayers; |
| 74 | } |
| 75 | |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 76 | /** |
| 77 | * @return set of layers with subscriptions to a subset of publishers |
| 78 | */ |
| 79 | @NonNull |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 80 | public Set<VmsAssociatedLayer> getAssociatedLayers() { |
Asaf Rosenfeld | f9be20b | 2017-06-30 18:20:08 -0700 | [diff] [blame] | 81 | return mSubscribedLayersFromPublishers; |
| 82 | } |
| 83 | |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 84 | @Override |
| 85 | public String toString() { |
| 86 | StringBuilder sb = new StringBuilder(); |
| 87 | sb.append("sequence number=").append(mSequenceNumber); |
| 88 | sb.append("; layers={"); |
Asaf Rosenfeld | f9be20b | 2017-06-30 18:20:08 -0700 | [diff] [blame] | 89 | for (VmsLayer layer : mLayers) { |
| 90 | sb.append(layer).append(","); |
| 91 | } |
| 92 | sb.append("}"); |
| 93 | sb.append("; associatedLayers={"); |
| 94 | for (VmsAssociatedLayer layer : mSubscribedLayersFromPublishers) { |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 95 | sb.append(layer).append(","); |
| 96 | } |
| 97 | sb.append("}"); |
| 98 | return sb.toString(); |
| 99 | } |
| 100 | |
| 101 | public static final Parcelable.Creator<VmsSubscriptionState> CREATOR = new |
Asaf Rosenfeld | f9be20b | 2017-06-30 18:20:08 -0700 | [diff] [blame] | 102 | Parcelable.Creator<VmsSubscriptionState>() { |
| 103 | public VmsSubscriptionState createFromParcel(Parcel in) { |
| 104 | return new VmsSubscriptionState(in); |
| 105 | } |
| 106 | |
| 107 | public VmsSubscriptionState[] newArray(int size) { |
| 108 | return new VmsSubscriptionState[size]; |
| 109 | } |
| 110 | }; |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 111 | |
| 112 | @Override |
| 113 | public void writeToParcel(Parcel out, int flags) { |
| 114 | out.writeInt(mSequenceNumber); |
Asaf Rosenfeld | f9be20b | 2017-06-30 18:20:08 -0700 | [diff] [blame] | 115 | out.writeParcelableList(new ArrayList(mLayers), flags); |
| 116 | out.writeParcelableList(new ArrayList(mSubscribedLayersFromPublishers), flags); |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
Asaf Rosenfeld | 9e1c315 | 2018-02-02 08:47:10 -0800 | [diff] [blame] | 120 | public boolean equals(Object o) { |
| 121 | if (!(o instanceof VmsSubscriptionState)) { |
| 122 | return false; |
| 123 | } |
| 124 | VmsSubscriptionState p = (VmsSubscriptionState) o; |
| 125 | return Objects.equals(p.mSequenceNumber, mSequenceNumber) && |
| 126 | p.mLayers.equals(mLayers) && |
| 127 | p.mSubscribedLayersFromPublishers.equals(mSubscribedLayersFromPublishers); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public int hashCode() { |
| 132 | return Objects.hash(mSequenceNumber, mLayers, mSubscribedLayersFromPublishers); |
| 133 | } |
| 134 | |
| 135 | @Override |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 136 | public int describeContents() { |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | private VmsSubscriptionState(Parcel in) { |
| 141 | mSequenceNumber = in.readInt(); |
Asaf Rosenfeld | f9be20b | 2017-06-30 18:20:08 -0700 | [diff] [blame] | 142 | |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 143 | List<VmsLayer> layers = new ArrayList<>(); |
| 144 | in.readParcelableList(layers, VmsLayer.class.getClassLoader()); |
Asaf Rosenfeld | f9be20b | 2017-06-30 18:20:08 -0700 | [diff] [blame] | 145 | mLayers = Collections.unmodifiableSet(new HashSet(layers)); |
| 146 | |
| 147 | List<VmsAssociatedLayer> associatedLayers = new ArrayList<>(); |
| 148 | in.readParcelableList(associatedLayers, VmsAssociatedLayer.class.getClassLoader()); |
Asaf Rosenfeld | 9e1c315 | 2018-02-02 08:47:10 -0800 | [diff] [blame] | 149 | mSubscribedLayersFromPublishers = |
| 150 | Collections.unmodifiableSet(new HashSet(associatedLayers)); |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 151 | } |
| 152 | } |