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