Merge "BootParameters: Support new API."
diff --git a/cmds/bootanimation/iot/BootParameters.cpp b/cmds/bootanimation/iot/BootParameters.cpp
index 995a3aa..2cf1c19 100644
--- a/cmds/bootanimation/iot/BootParameters.cpp
+++ b/cmds/bootanimation/iot/BootParameters.cpp
@@ -24,8 +24,8 @@
#include <android-base/file.h>
#include <utils/Log.h>
-using android::base::RemoveFileIfExists;
using android::base::ReadFileToString;
+using android::base::RemoveFileIfExists;
using Json::Reader;
using Json::Value;
@@ -33,9 +33,8 @@
namespace {
-// Keys for volume, brightness, and user-defined parameters.
-constexpr const char* kKeyVolume = "volume";
-constexpr const char* kKeyBrightness = "brightness";
+// Keys for supporting a silent boot and user-defined BootAction parameters.
+constexpr const char *kKeySilentBoot = "silent_boot";
constexpr const char* kKeyParams = "params";
constexpr const char* kNextBootFile = "/data/misc/bootanimation/next_boot.json";
@@ -82,35 +81,37 @@
loadParameters(contents);
}
+// If the boot parameters -
+// - File is missing, we assume a normal, non-silent boot.
+// - Are well-formed, initially assume a normal, non-silent boot and parse.
void BootParameters::loadParameters(const std::string& raw_json) {
if (!Reader().parse(raw_json, mJson)) {
return;
}
- // A missing key returns a safe, missing value.
- // Ignore invalid or missing JSON parameters.
- Value& jsonValue = mJson[kKeyVolume];
- if (jsonValue.isDouble()) {
- mVolume = jsonValue.asFloat();
- }
+ parseBootParameters();
+}
- jsonValue = mJson[kKeyBrightness];
- if (jsonValue.isDouble()) {
- mBrightness = jsonValue.asFloat();
- }
-
- jsonValue = mJson[kKeyParams];
- if (jsonValue.isObject()) {
- for (auto &key : jsonValue.getMemberNames()) {
- Value& value = jsonValue[key];
- if (value.isString()) {
- mParameters.push_back({
- .key = key.c_str(),
- .value = value.asCString()
- });
- }
+void BootParameters::parseBootParameters() {
+ // A missing key returns a safe, missing value.
+ // Ignore invalid or missing JSON parameters.
+ Value &jsonValue = mJson[kKeySilentBoot];
+ if (jsonValue.isBool()) {
+ mIsSilentBoot = jsonValue.asBool();
}
- }
+
+ jsonValue = mJson[kKeyParams];
+ if (jsonValue.isObject()) {
+ // getMemberNames returns a copy of the keys which must be stored.
+ mKeys = jsonValue.getMemberNames();
+ for (auto &key : mKeys) {
+ Value &value = jsonValue[key];
+ if (value.isString()) {
+ mParameters.push_back(
+ {.key = key.c_str(), .value = value.asCString()});
+ }
+ }
+ }
}
} // namespace android
diff --git a/cmds/bootanimation/iot/BootParameters.h b/cmds/bootanimation/iot/BootParameters.h
index f421790..49c34dd 100644
--- a/cmds/bootanimation/iot/BootParameters.h
+++ b/cmds/bootanimation/iot/BootParameters.h
@@ -33,13 +33,8 @@
// to clear the parameters for next boot.
BootParameters();
- // Returns true if volume/brightness were explicitly set on reboot.
- bool hasVolume() const { return mVolume >= 0; }
- bool hasBrightness() const { return mBrightness >= 0; }
-
- // Returns volume/brightness in [0,1], or -1 if unset.
- float getVolume() const { return mVolume; }
- float getBrightness() const { return mBrightness; }
+ // Returns whether or not this is a silent boot.
+ bool isSilentBoot() const { return mIsSilentBoot; }
// Returns the additional boot parameters that were set on reboot.
const std::vector<ABootActionParameter>& getParameters() const { return mParameters; }
@@ -49,12 +44,17 @@
private:
void loadParameters();
- float mVolume = -1.f;
- float mBrightness = -1.f;
+ void parseBootParameters();
+
+ bool mIsSilentBoot = false;
+
std::vector<ABootActionParameter> mParameters;
// Store parsed JSON because mParameters makes a shallow copy.
Json::Value mJson;
+
+ // Store parameter keys because mParameters makes a shallow copy.
+ Json::Value::Members mKeys;
};
} // namespace android
diff --git a/cmds/bootanimation/iot/BootParameters_test.cpp b/cmds/bootanimation/iot/BootParameters_test.cpp
index 9a03113..27d7d6f 100644
--- a/cmds/bootanimation/iot/BootParameters_test.cpp
+++ b/cmds/bootanimation/iot/BootParameters_test.cpp
@@ -22,71 +22,85 @@
namespace {
-TEST(BootParametersTest, TestParseValidParameters) {
- BootParameters boot_parameters = BootParameters();
- boot_parameters.loadParameters(R"(
+TEST(BootParametersTest, TestNoBootParametersIsNotSilent) {
+ BootParameters boot_parameters = BootParameters();
+ boot_parameters.loadParameters("");
+
+ ASSERT_FALSE(boot_parameters.isSilentBoot());
+ ASSERT_EQ(0u, boot_parameters.getParameters().size());
+}
+
+TEST(BootParametersTest, TestParseIsSilent) {
+ BootParameters boot_parameters = BootParameters();
+ boot_parameters.loadParameters(R"(
{
- "brightness":0.2,
- "volume":0.1,
+ "silent_boot":true,
+ "params":{}
+ }
+ )");
+
+ ASSERT_TRUE(boot_parameters.isSilentBoot());
+}
+
+TEST(BootParametersTest, TestParseIsNotSilent) {
+ BootParameters boot_parameters = BootParameters();
+ boot_parameters.loadParameters(R"(
+ {
+ "silent_boot":false,
+ "params":{}
+ }
+ )");
+
+ ASSERT_FALSE(boot_parameters.isSilentBoot());
+}
+
+TEST(BootParametersTest, TestParseBootParameters) {
+ BootParameters boot_parameters = BootParameters();
+ boot_parameters.loadParameters(R"(
+ {
+ "silent_boot":false,
"params":{
"key1":"value1",
"key2":"value2"
}
}
- )");
+ )");
- EXPECT_TRUE(boot_parameters.hasBrightness());
- EXPECT_TRUE(boot_parameters.hasVolume());
- EXPECT_FLOAT_EQ(0.2f, boot_parameters.getBrightness());
- EXPECT_FLOAT_EQ(0.1f, boot_parameters.getVolume());
-
- auto parameters = boot_parameters.getParameters();
- ASSERT_EQ(2u, parameters.size());
- ASSERT_STREQ(parameters[0].key, "key1");
- ASSERT_STREQ(parameters[0].value, "value1");
- ASSERT_STREQ(parameters[1].key, "key2");
- ASSERT_STREQ(parameters[1].value, "value2");
+ auto ¶meters = boot_parameters.getParameters();
+ ASSERT_EQ(2u, parameters.size());
+ ASSERT_STREQ(parameters[0].key, "key1");
+ ASSERT_STREQ(parameters[0].value, "value1");
+ ASSERT_STREQ(parameters[1].key, "key2");
+ ASSERT_STREQ(parameters[1].value, "value2");
}
-TEST(BootParametersTest, TestMalformedParametersAreSkipped) {
- BootParameters boot_parameters = BootParameters();
- boot_parameters.loadParameters(R"(
+TEST(BootParametersTest, TestParseMissingParametersIsNotSilent) {
+ BootParameters boot_parameters = BootParameters();
+ boot_parameters.loadParameters(R"(
{
- "brightness":0.5,
- "volume":0.5,
- "params": {
- "key1":1,
+ "params":{}
+ }
+ )");
+
+ ASSERT_FALSE(boot_parameters.isSilentBoot());
+}
+
+TEST(BootParametersTest, TestParseMalformedParametersAreSkipped) {
+ BootParameters boot_parameters = BootParameters();
+ boot_parameters.loadParameters(R"(
+ {
+ "silent_boot":false,
+ "params":{
+ "key1":123,
"key2":"value2"
}
}
- )");
+ )");
- EXPECT_TRUE(boot_parameters.hasBrightness());
- EXPECT_TRUE(boot_parameters.hasVolume());
- EXPECT_FLOAT_EQ(0.5f, boot_parameters.getBrightness());
- EXPECT_FLOAT_EQ(0.5f, boot_parameters.getVolume());
-
- auto parameters = boot_parameters.getParameters();
- ASSERT_EQ(1u, parameters.size());
- ASSERT_STREQ(parameters[0].key, "key2");
- ASSERT_STREQ(parameters[0].value, "value2");
-}
-
-TEST(BootParametersTest, TestMissingParametersHaveDefaults) {
- BootParameters boot_parameters = BootParameters();
- boot_parameters.loadParameters(R"(
- {
- "brightness":0.5
- }
- )");
-
- EXPECT_TRUE(boot_parameters.hasBrightness());
- EXPECT_FALSE(boot_parameters.hasVolume());
- EXPECT_FLOAT_EQ(0.5f, boot_parameters.getBrightness());
- EXPECT_FLOAT_EQ(-1.0f, boot_parameters.getVolume());
-
- auto parameters = boot_parameters.getParameters();
- ASSERT_EQ(0u, parameters.size());
+ auto ¶meters = boot_parameters.getParameters();
+ ASSERT_EQ(1u, parameters.size());
+ ASSERT_STREQ(parameters[0].key, "key2");
+ ASSERT_STREQ(parameters[0].value, "value2");
}
} // namespace