1 /*
2  * Copyright (C) 2018 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.game.qualification;
17 
18 import java.util.List;
19 import java.util.stream.Collectors;
20 
21 public class GameCoreConfiguration {
22 
23     public static String FEATURE_STRING = "android.software.gamecore.preview.certified";
24 
25     List<CertificationRequirements> mCertificationRequirements;
26     List<ApkInfo> mApkInfo;
27 
GameCoreConfiguration( List<CertificationRequirements> certificationRequirements, List<ApkInfo> apkInfo)28     public GameCoreConfiguration(
29             List<CertificationRequirements> certificationRequirements,
30             List<ApkInfo> apkInfo) {
31         mCertificationRequirements = certificationRequirements;
32         mApkInfo = apkInfo;
33         validateConfiguration();
34     }
35 
validateConfiguration()36     private void validateConfiguration() {
37         if (mCertificationRequirements == null) {
38             throw new IllegalArgumentException("Missing certification requirements");
39         }
40         if (mApkInfo == null) {
41             throw new IllegalArgumentException("Missing apk-info");
42         }
43 
44         List<String> requirementNames =
45                 getCertificationRequirements().stream()
46                         .map(CertificationRequirements::getName)
47                         .collect(Collectors.toList());
48 
49         List<String> apkNames =
50                 getApkInfo().stream()
51                         .map(ApkInfo::getName)
52                         .collect(Collectors.toList());
53 
54         List<String> missingApks =
55                 requirementNames.stream()
56                         .filter((it) -> !apkNames.contains(it))
57                         .collect(Collectors.toList());
58 
59         if (!missingApks.isEmpty()) {
60             throw new IllegalArgumentException(
61                     "<certification> contains the following apk that does not exists in "
62                             + "<apk-info>:\n"
63                             + String.join("\n", missingApks));
64         }
65     }
66 
getCertificationRequirements()67     public List<CertificationRequirements> getCertificationRequirements() {
68         return mCertificationRequirements;
69     }
70 
getApkInfo()71     public List<ApkInfo> getApkInfo() {
72         return mApkInfo;
73     }
74 
findCertificationRequirements(String name)75     public CertificationRequirements findCertificationRequirements(String name) {
76         return mCertificationRequirements.stream()
77                 .filter((it) -> it.getName().equals(name))
78                 .findFirst()
79                 .orElse(null);
80     }
81 }
82