Tim Van Patten | 0b6b81d | 2025-01-13 10:19:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2025 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 | |
Tim Van Patten | 4a5fd7e | 2025-01-13 11:32:23 -0700 | [diff] [blame^] | 17 | #include <cinttypes> |
Tim Van Patten | 0b6b81d | 2025-01-13 10:19:19 -0700 | [diff] [blame] | 18 | |
| 19 | #include <android-base/stringprintf.h> |
Tim Van Patten | 4a5fd7e | 2025-01-13 11:32:23 -0700 | [diff] [blame^] | 20 | #include <binder/Parcel.h> |
| 21 | #include <graphicsenv/FeatureOverrides.h> |
Tim Van Patten | 0b6b81d | 2025-01-13 10:19:19 -0700 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | using base::StringAppendF; |
| 26 | |
Tim Van Patten | 4a5fd7e | 2025-01-13 11:32:23 -0700 | [diff] [blame^] | 27 | status_t FeatureConfig::writeToParcel(Parcel* parcel) const { |
| 28 | status_t status; |
| 29 | |
| 30 | status = parcel->writeUtf8AsUtf16(mFeatureName); |
| 31 | if (status != OK) { |
| 32 | return status; |
| 33 | } |
| 34 | status = parcel->writeBool(mEnabled); |
| 35 | if (status != OK) { |
| 36 | return status; |
| 37 | } |
| 38 | |
| 39 | return OK; |
| 40 | } |
| 41 | |
| 42 | status_t FeatureConfig::readFromParcel(const Parcel* parcel) { |
| 43 | status_t status; |
| 44 | |
| 45 | status = parcel->readUtf8FromUtf16(&mFeatureName); |
| 46 | if (status != OK) { |
| 47 | return status; |
| 48 | } |
| 49 | status = parcel->readBool(&mEnabled); |
| 50 | if (status != OK) { |
| 51 | return status; |
| 52 | } |
| 53 | |
| 54 | return OK; |
| 55 | } |
| 56 | |
Tim Van Patten | 0b6b81d | 2025-01-13 10:19:19 -0700 | [diff] [blame] | 57 | std::string FeatureConfig::toString() const { |
| 58 | std::string result; |
| 59 | StringAppendF(&result, "Feature: %s\n", mFeatureName.c_str()); |
| 60 | StringAppendF(&result, " Status: %s\n", mEnabled ? "enabled" : "disabled"); |
| 61 | |
| 62 | return result; |
| 63 | } |
| 64 | |
Tim Van Patten | 4a5fd7e | 2025-01-13 11:32:23 -0700 | [diff] [blame^] | 65 | status_t FeatureOverrides::writeToParcel(Parcel* parcel) const { |
| 66 | status_t status; |
| 67 | // Number of global feature configs. |
| 68 | status = parcel->writeVectorSize(mGlobalFeatures); |
| 69 | if (status != OK) { |
| 70 | return status; |
| 71 | } |
| 72 | // Global feature configs. |
| 73 | for (const auto& cfg : mGlobalFeatures) { |
| 74 | status = cfg.writeToParcel(parcel); |
| 75 | if (status != OK) { |
| 76 | return status; |
| 77 | } |
| 78 | } |
| 79 | // Number of package feature overrides. |
| 80 | status = parcel->writeInt32(static_cast<int32_t>(mPackageFeatures.size())); |
| 81 | if (status != OK) { |
| 82 | return status; |
| 83 | } |
| 84 | for (const auto& feature : mPackageFeatures) { |
| 85 | // Package name. |
| 86 | status = parcel->writeUtf8AsUtf16(feature.first); |
| 87 | if (status != OK) { |
| 88 | return status; |
| 89 | } |
| 90 | // Number of package feature configs. |
| 91 | status = parcel->writeVectorSize(feature.second); |
| 92 | if (status != OK) { |
| 93 | return status; |
| 94 | } |
| 95 | // Package feature configs. |
| 96 | for (const auto& cfg : feature.second) { |
| 97 | status = cfg.writeToParcel(parcel); |
| 98 | if (status != OK) { |
| 99 | return status; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return OK; |
| 105 | } |
| 106 | |
| 107 | status_t FeatureOverrides::readFromParcel(const Parcel* parcel) { |
| 108 | status_t status; |
| 109 | |
| 110 | // Number of global feature configs. |
| 111 | status = parcel->resizeOutVector(&mGlobalFeatures); |
| 112 | if (status != OK) { |
| 113 | return status; |
| 114 | } |
| 115 | // Global feature configs. |
| 116 | for (FeatureConfig& cfg : mGlobalFeatures) { |
| 117 | status = cfg.readFromParcel(parcel); |
| 118 | if (status != OK) { |
| 119 | return status; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Number of package feature overrides. |
| 124 | int numPkgOverrides = parcel->readInt32(); |
| 125 | for (int i = 0; i < numPkgOverrides; i++) { |
| 126 | // Package name. |
| 127 | std::string name; |
| 128 | status = parcel->readUtf8FromUtf16(&name); |
| 129 | if (status != OK) { |
| 130 | return status; |
| 131 | } |
| 132 | std::vector<FeatureConfig> cfgs; |
| 133 | // Number of package feature configs. |
| 134 | int numCfgs = parcel->readInt32(); |
| 135 | // Package feature configs. |
| 136 | for (int j = 0; j < numCfgs; j++) { |
| 137 | FeatureConfig cfg; |
| 138 | status = cfg.readFromParcel(parcel); |
| 139 | if (status != OK) { |
| 140 | return status; |
| 141 | } |
| 142 | cfgs.emplace_back(cfg); |
| 143 | } |
| 144 | mPackageFeatures[name] = cfgs; |
| 145 | } |
| 146 | |
| 147 | return OK; |
| 148 | } |
| 149 | |
Tim Van Patten | 0b6b81d | 2025-01-13 10:19:19 -0700 | [diff] [blame] | 150 | std::string FeatureOverrides::toString() const { |
| 151 | std::string result; |
| 152 | result.append("Global Features:\n"); |
| 153 | for (auto& cfg : mGlobalFeatures) { |
| 154 | result.append(" " + cfg.toString()); |
| 155 | } |
| 156 | result.append("\n"); |
| 157 | result.append("Package Features:\n"); |
| 158 | for (const auto& packageFeature : mPackageFeatures) { |
| 159 | result.append(" Package:"); |
| 160 | StringAppendF(&result, " %s\n", packageFeature.first.c_str()); |
| 161 | for (auto& cfg : packageFeature.second) { |
| 162 | result.append(" " + cfg.toString()); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return result; |
| 167 | } |
| 168 | |
| 169 | } // namespace android |