• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.car;
18 
19 import android.util.Log;
20 
21 import com.android.internal.annotations.GuardedBy;
22 
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.Map;
26 
27 public class VmsPublishersInfo {
28     private static final String TAG = "VmsPublishersInfo";
29     private static final boolean DBG = true;
30     private static final byte[] EMPTY_RESPONSE = new byte[0];
31 
32     private final Object mLock = new Object();
33     @GuardedBy("mLock")
34     private final Map<InfoWrapper, Integer> mPublishersIds = new HashMap<>();
35     @GuardedBy("mLock")
36     private final Map<Integer, byte[]> mPublishersInfo = new HashMap<>();
37 
38     private static class InfoWrapper {
39         private final byte[] mInfo;
40 
InfoWrapper(byte[] info)41         InfoWrapper(byte[] info) {
42             mInfo = info;
43         }
44 
getInfo()45         public byte[] getInfo() {
46             return mInfo;
47         }
48 
49         @Override
equals(Object o)50         public boolean equals(Object o) {
51             if (!(o instanceof InfoWrapper)) {
52                 return false;
53             }
54             InfoWrapper p = (InfoWrapper) o;
55             return Arrays.equals(this.mInfo, p.mInfo);
56         }
57 
58         @Override
hashCode()59         public int hashCode() {
60             return Arrays.hashCode(mInfo);
61         }
62     }
63 
64     /**
65      * Returns the ID associated with the publisher info. When called for the first time for a
66      * publisher info will store the info and assign an ID
67      */
getIdForInfo(byte[] publisherInfo)68     public int getIdForInfo(byte[] publisherInfo) {
69         Integer publisherId;
70         InfoWrapper wrappedPublisherInfo = new InfoWrapper(publisherInfo);
71         synchronized (mLock) {
72             maybeAddPublisherInfoLocked(wrappedPublisherInfo);
73             publisherId = mPublishersIds.get(wrappedPublisherInfo);
74         }
75         if (DBG) {
76             Log.i(TAG, "Publisher ID is: " + publisherId);
77         }
78         return publisherId;
79     }
80 
getPublisherInfo(int publisherId)81     public byte[] getPublisherInfo(int publisherId) {
82         synchronized (mLock) {
83             return mPublishersInfo.containsKey(publisherId)
84                     ? mPublishersInfo.get(publisherId).clone()
85                     : EMPTY_RESPONSE;
86         }
87     }
88 
89     @GuardedBy("mLock")
maybeAddPublisherInfoLocked(InfoWrapper wrappedPublisherInfo)90     private void maybeAddPublisherInfoLocked(InfoWrapper wrappedPublisherInfo) {
91         if (!mPublishersIds.containsKey(wrappedPublisherInfo)) {
92             // Assign ID to the info
93             Integer publisherId = mPublishersIds.size();
94 
95             mPublishersIds.put(wrappedPublisherInfo, publisherId);
96             mPublishersInfo.put(publisherId, wrappedPublisherInfo.getInfo());
97         }
98     }
99 }
100 
101