1 /* 2 * Copyright (C) 2023 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.cobalt.system; 18 19 import android.os.Build; 20 21 import com.google.cobalt.ReportDefinition; 22 import com.google.cobalt.SystemProfile; 23 import com.google.cobalt.SystemProfileField; 24 import com.google.common.base.Ascii; 25 import com.google.common.base.Strings; 26 import com.google.common.collect.ImmutableMap; 27 28 /** Manages SystemProfile data for the current system. */ 29 public final class SystemData { 30 private static final ImmutableMap<String, SystemProfile.ARCH> ABI_MAP = 31 new ImmutableMap.Builder<String, SystemProfile.ARCH>() 32 .put("arm64-v8a", SystemProfile.ARCH.ARM_64) 33 .put("armeabi-v7a", SystemProfile.ARCH.ARM_32) 34 .put("x86_64", SystemProfile.ARCH.X86_64) 35 .put("x86", SystemProfile.ARCH.X86_32) 36 .buildOrThrow(); 37 38 private final SystemProfile mCurrentSystemProfile; 39 40 /** Construct a system data object without a system profile. */ SystemData()41 public SystemData() { 42 this(/* cobaltAppVersion= */ null); 43 } 44 SystemData(String cobaltAppVersion)45 public SystemData(String cobaltAppVersion) { 46 SystemProfile.Builder systemProfile = 47 SystemProfile.newBuilder() 48 .setBoardName(Build.BOARD) 49 .setSystemVersion(Build.VERSION.RELEASE) 50 .setOs(SystemProfile.OS.ANDROID); 51 if (Build.SUPPORTED_ABIS.length > 0) { 52 if (ABI_MAP.containsKey(Ascii.toLowerCase(Build.SUPPORTED_ABIS[0]))) { 53 systemProfile.setArch( 54 ABI_MAP.getOrDefault( 55 Ascii.toLowerCase(Build.SUPPORTED_ABIS[0]), 56 SystemProfile.ARCH.UNKNOWN_ARCH)); 57 } 58 } 59 if (!Strings.isNullOrEmpty(cobaltAppVersion)) { 60 systemProfile.setAppVersion(cobaltAppVersion); 61 } 62 mCurrentSystemProfile = systemProfile.build(); 63 } 64 65 /** Return the current system profile, filtered to the fields specified for this report. */ filteredSystemProfile(ReportDefinition report)66 public SystemProfile filteredSystemProfile(ReportDefinition report) { 67 SystemProfile.Builder systemProfileBuilder = SystemProfile.newBuilder(); 68 for (SystemProfileField field : report.getSystemProfileFieldList()) { 69 switch (field) { 70 case APP_VERSION: 71 systemProfileBuilder.setAppVersion(mCurrentSystemProfile.getAppVersion()); 72 break; 73 case ARCH: 74 systemProfileBuilder.setArch(mCurrentSystemProfile.getArch()); 75 break; 76 case BOARD_NAME: 77 systemProfileBuilder.setBoardName(mCurrentSystemProfile.getBoardName()); 78 break; 79 case BUILD_TYPE: 80 systemProfileBuilder.setBuildType(mCurrentSystemProfile.getBuildType()); 81 break; 82 case CHANNEL: 83 systemProfileBuilder.setChannel(mCurrentSystemProfile.getChannel()); 84 break; 85 case EXPERIMENT_IDS: 86 // Experiment IDs are not yet supported 87 break; 88 case OS: 89 systemProfileBuilder.setOs(mCurrentSystemProfile.getOs()); 90 break; 91 case PRODUCT_NAME: 92 systemProfileBuilder.setProductName(mCurrentSystemProfile.getProductName()); 93 break; 94 case SYSTEM_VERSION: 95 systemProfileBuilder.setSystemVersion(mCurrentSystemProfile.getSystemVersion()); 96 break; 97 case UNRECOGNIZED: 98 throw new AssertionError("Unrecognized SystemProfileField"); 99 } 100 } 101 return systemProfileBuilder.build(); 102 } 103 } 104