Home | History | Annotate | Download | only in pm
      1 /*
      2  * Copyright (C) 2013 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.server.pm;
     18 
     19 import java.util.Arrays;
     20 import java.util.HashMap;
     21 import java.util.HashSet;
     22 import java.util.Map;
     23 import java.util.Set;
     24 
     25 public class PackageKeySetData {
     26 
     27     private long[] mSigningKeySets;
     28 
     29     private long[] mDefinedKeySets;
     30 
     31     private final Map<String, Long> mKeySetAliases;
     32 
     33     PackageKeySetData() {
     34         mSigningKeySets = new long[0];
     35         mDefinedKeySets = new long[0];
     36         mKeySetAliases =  new HashMap<String, Long>();
     37     }
     38 
     39     PackageKeySetData(PackageKeySetData original) {
     40         mSigningKeySets = original.getSigningKeySets().clone();
     41         mDefinedKeySets = original.getDefinedKeySets().clone();
     42         mKeySetAliases = new HashMap<String, Long>();
     43         mKeySetAliases.putAll(original.getAliases());
     44     }
     45 
     46     public void addSigningKeySet(long ks) {
     47         // deduplicate
     48         for (long knownKeySet : mSigningKeySets) {
     49             if (ks == knownKeySet) {
     50                 return;
     51             }
     52         }
     53         int end = mSigningKeySets.length;
     54         mSigningKeySets = Arrays.copyOf(mSigningKeySets, end + 1);
     55         mSigningKeySets[end] = ks;
     56     }
     57 
     58     public void removeSigningKeySet(long ks) {
     59         if (packageIsSignedBy(ks)) {
     60             long[] keysets = new long[mSigningKeySets.length - 1];
     61             int index = 0;
     62             for (long signingKeySet : mSigningKeySets) {
     63                 if (signingKeySet != ks) {
     64                     keysets[index] = signingKeySet;
     65                     index += 1;
     66                 }
     67             }
     68             mSigningKeySets = keysets;
     69         }
     70     }
     71 
     72     public void addDefinedKeySet(long ks, String alias) {
     73         // deduplicate
     74         for (long knownKeySet : mDefinedKeySets) {
     75             if (ks == knownKeySet) {
     76                 return;
     77             }
     78         }
     79         int end = mDefinedKeySets.length;
     80         mDefinedKeySets = Arrays.copyOf(mDefinedKeySets, end + 1);
     81         mDefinedKeySets[end] = ks;
     82         mKeySetAliases.put(alias, ks);
     83     }
     84 
     85     public void removeDefinedKeySet(long ks) {
     86         if (mKeySetAliases.containsValue(ks)) {
     87             long[] keysets = new long[mDefinedKeySets.length - 1];
     88             int index = 0;
     89             for (long definedKeySet : mDefinedKeySets) {
     90                 if (definedKeySet != ks) {
     91                     keysets[index] = definedKeySet;
     92                     index += 1;
     93                 }
     94             }
     95             mDefinedKeySets = keysets;
     96             for (String alias : mKeySetAliases.keySet()) {
     97                 if (mKeySetAliases.get(alias) == ks) {
     98                     mKeySetAliases.remove(alias);
     99                     break;
    100                 }
    101             }
    102         }
    103     }
    104 
    105     public boolean packageIsSignedBy(long ks) {
    106         for (long signingKeySet : mSigningKeySets) {
    107             if (ks == signingKeySet) {
    108                 return true;
    109             }
    110         }
    111         return false;
    112     }
    113 
    114     public long[] getSigningKeySets() {
    115         return mSigningKeySets;
    116     }
    117 
    118     public long[] getDefinedKeySets() {
    119         return mDefinedKeySets;
    120     }
    121 
    122     public Map<String, Long> getAliases() {
    123         return mKeySetAliases;
    124     }
    125 }