1 /* 2 * Copyright (C) 2024 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 package com.android.cts.backportedfixes; 17 18 import com.android.build.backportedfixes.BackportedFix; 19 import com.android.build.backportedfixes.BackportedFixes; 20 21 import com.google.common.collect.ImmutableBiMap; 22 import com.google.common.collect.ImmutableSet; 23 import com.google.common.io.Resources; 24 25 import java.net.URL; 26 import java.util.Set; 27 28 /** 29 * List of all known issues approved by Android Partner Engineering as a backported fix reportable 30 * by {@link android.os.Build#getBackportedFixStatus(long)}. 31 * 32 * The known issues are listed in cts/backported_fixes/approved/backported_fixes.txtpb 33 */ 34 public final class ApprovedBackportedFixes { 35 private final ImmutableSet<Long> mAllIssues; 36 private final ImmutableBiMap<Long, Integer> mId2Alias; 37 ApprovedBackportedFixes()38 private ApprovedBackportedFixes() { 39 BackportedFixes fixes = readBackportedFixes(); 40 mAllIssues = fixes.getFixesList().stream().map(BackportedFix::getKnownIssue).collect( 41 ImmutableSet.toImmutableSet()); 42 mId2Alias = 43 fixes.getFixesList().stream() 44 .filter(fix -> fix.getAlias() > 0) 45 .collect( 46 ImmutableBiMap.toImmutableBiMap( 47 BackportedFix::getKnownIssue, BackportedFix::getAlias)); 48 } 49 50 51 /** 52 * Returns the alias of a known issue. 53 * 54 * @param issueId The id of the known issue. 55 * @return the alias or 0 if the issue does not have an alias or is not found. 56 */ getAlias(Long issueId)57 public int getAlias(Long issueId) { 58 Integer alias = mId2Alias.get(issueId); 59 return alias == null ? 0 : alias; 60 } 61 62 /** 63 * Returns the id of a known issue. 64 * 65 * @param alias The alias of the known issue. 66 * @return the id or 0 if the alias is not found. 67 */ getId(int alias)68 public long getId(int alias) { 69 Long id = mId2Alias.inverse().get(alias); 70 return id == null ? 0 : id; 71 } 72 getAllAliases()73 public Set<Integer> getAllAliases() { 74 return mId2Alias.values(); 75 } 76 77 /** Returns the ids of all known issues approved as a backported fix. */ getAllIssues()78 public ImmutableSet<Long> getAllIssues() { 79 return mAllIssues; 80 } 81 readBackportedFixes()82 private static BackportedFixes readBackportedFixes() { 83 try { 84 URL configResource = ApprovedBackportedFixes.class.getResource( 85 "backported_fixes.binpb"); 86 var byteStream = Resources.toByteArray(configResource); 87 return BackportedFixes.parseFrom(byteStream); 88 } catch (Exception e) { 89 throw new IllegalStateException("Unable to load list of approved backported fixes.", e); 90 } 91 } 92 93 private static volatile ApprovedBackportedFixes sInstance; 94 95 /** Get the singleton instance of {@code ApprovedBackportedFixes}. */ getInstance()96 public static ApprovedBackportedFixes getInstance() { 97 if (sInstance == null) { 98 synchronized (ApprovedBackportedFixes.class) { 99 if (sInstance == null) { 100 sInstance = new ApprovedBackportedFixes(); 101 } 102 } 103 } 104 return sInstance; 105 } 106 } 107