summaryrefslogtreecommitdiff
path: root/libs/graphicsenv/FeatureOverrides.cpp
blob: 51afe285fb9c968b86de7beea7b47eafe5aeb0dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cinttypes>

#include <android-base/stringprintf.h>
#include <binder/Parcel.h>
#include <graphicsenv/FeatureOverrides.h>

namespace android {

using base::StringAppendF;

status_t FeatureConfig::writeToParcel(Parcel* parcel) const {
    status_t status;

    status = parcel->writeUtf8AsUtf16(mFeatureName);
    if (status != OK) {
        return status;
    }
    status = parcel->writeBool(mEnabled);
    if (status != OK) {
        return status;
    }

    return OK;
}

status_t FeatureConfig::readFromParcel(const Parcel* parcel) {
    status_t status;

    status = parcel->readUtf8FromUtf16(&mFeatureName);
    if (status != OK) {
        return status;
    }
    status = parcel->readBool(&mEnabled);
    if (status != OK) {
        return status;
    }

    return OK;
}

std::string FeatureConfig::toString() const {
    std::string result;
    StringAppendF(&result, "Feature: %s\n", mFeatureName.c_str());
    StringAppendF(&result, "      Status: %s\n", mEnabled ? "enabled" : "disabled");

    return result;
}

status_t FeatureOverrides::writeToParcel(Parcel* parcel) const {
    status_t status;
    // Number of global feature configs.
    status = parcel->writeVectorSize(mGlobalFeatures);
    if (status != OK) {
        return status;
    }
    // Global feature configs.
    for (const auto& cfg : mGlobalFeatures) {
        status = cfg.writeToParcel(parcel);
        if (status != OK) {
            return status;
        }
    }
    // Number of package feature overrides.
    status = parcel->writeInt32(static_cast<int32_t>(mPackageFeatures.size()));
    if (status != OK) {
        return status;
    }
    for (const auto& feature : mPackageFeatures) {
        // Package name.
        status = parcel->writeUtf8AsUtf16(feature.first);
        if (status != OK) {
            return status;
        }
        // Number of package feature configs.
        status = parcel->writeVectorSize(feature.second);
        if (status != OK) {
            return status;
        }
        // Package feature configs.
        for (const auto& cfg : feature.second) {
            status = cfg.writeToParcel(parcel);
            if (status != OK) {
                return status;
            }
        }
    }

    return OK;
}

status_t FeatureOverrides::readFromParcel(const Parcel* parcel) {
    status_t status;

    // Number of global feature configs.
    status = parcel->resizeOutVector(&mGlobalFeatures);
    if (status != OK) {
        return status;
    }
    // Global feature configs.
    for (FeatureConfig& cfg : mGlobalFeatures) {
        status = cfg.readFromParcel(parcel);
        if (status != OK) {
            return status;
        }
    }

    // Number of package feature overrides.
    int numPkgOverrides = parcel->readInt32();
    for (int i = 0; i < numPkgOverrides; i++) {
        // Package name.
        std::string name;
        status = parcel->readUtf8FromUtf16(&name);
        if (status != OK) {
            return status;
        }
        std::vector<FeatureConfig> cfgs;
        // Number of package feature configs.
        int numCfgs = parcel->readInt32();
        // Package feature configs.
        for (int j = 0; j < numCfgs; j++) {
            FeatureConfig cfg;
            status = cfg.readFromParcel(parcel);
            if (status != OK) {
                return status;
            }
            cfgs.emplace_back(cfg);
        }
        mPackageFeatures[name] = cfgs;
    }

    return OK;
}

std::string FeatureOverrides::toString() const {
    std::string result;
    result.append("Global Features:\n");
    for (auto& cfg : mGlobalFeatures) {
        result.append("  " + cfg.toString());
    }
    result.append("\n");
    result.append("Package Features:\n");
    for (const auto& packageFeature : mPackageFeatures) {
        result.append("  Package:");
        StringAppendF(&result, " %s\n", packageFeature.first.c_str());
        for (auto& cfg : packageFeature.second) {
            result.append("    " + cfg.toString());
        }
    }

    return result;
}

} // namespace android