Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 33314933 authored by Yifan Hong's avatar Yifan Hong Committed by Gerrit Code Review
Browse files

Merge changes from topic "charger_soong"

* changes:
  charger: Add tests for loading animation resources
  charger: Move built-in resources to /system
  charger: Load default resources from /system
parents 7298ba09 c2bed97e
Loading
Loading
Loading
Loading
+33 −6
Original line number Diff line number Diff line
@@ -243,17 +243,44 @@ cc_test {

cc_test {
    name: "libhealthd_charger_test",
    srcs: ["AnimationParser_test.cpp"],
    shared_libs: [
        "liblog",
        "libbase",
        "libcutils",
    defaults: ["charger_defaults"],
    srcs: [
        "AnimationParser_test.cpp",
        "healthd_mode_charger_test.cpp"
    ],
    static_libs: [
        "libhealthd_charger",
        "libgmock",
    ],
    test_suites: [
        "general-tests",
        "device-tests",
    ],
    data: [
        ":libhealthd_charger_test_data",
    ],
    require_root: true,
}

// /system/etc/res/images/charger/battery_fail.png
prebuilt_etc {
    name: "system_core_charger_res_images_battery_fail.png",
    src: "images/battery_fail.png",
    relative_install_path: "res/images/charger",
    filename: "battery_fail.png",
}

// /system/etc/res/images/charger/battery_scale.png
prebuilt_etc {
    name: "system_core_charger_res_images_battery_scale.png",
    src: "images/battery_scale.png",
    relative_install_path: "res/images/charger",
    filename: "battery_scale.png",
}

phony {
    name: "charger_res_images",
    required: [
        "system_core_charger_res_images_battery_fail.png",
        "system_core_charger_res_images_battery_scale.png",
    ],
}

healthd/Android.mk

deleted100644 → 0
+0 −36
Original line number Diff line number Diff line
# Copyright 2013 The Android Open Source Project

LOCAL_PATH := $(call my-dir)

ifeq ($(strip $(BOARD_CHARGER_NO_UI)),true)
LOCAL_CHARGER_NO_UI := true
endif

### charger_res_images ###
ifneq ($(strip $(LOCAL_CHARGER_NO_UI)),true)
define _add-charger-image
include $$(CLEAR_VARS)
LOCAL_MODULE := system_core_charger_res_images_$(notdir $(1))
LOCAL_MODULE_STEM := $(notdir $(1))
_img_modules += $$(LOCAL_MODULE)
LOCAL_SRC_FILES := $1
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := ETC
LOCAL_MODULE_PATH := $$(TARGET_ROOT_OUT)/res/images/charger
include $$(BUILD_PREBUILT)
endef

_img_modules :=
_images :=
$(foreach _img, $(call find-subdir-subdir-files, "images", "*.png"), \
  $(eval $(call _add-charger-image,$(_img))))

include $(CLEAR_VARS)
LOCAL_MODULE := charger_res_images
LOCAL_MODULE_TAGS := optional
LOCAL_REQUIRED_MODULES := $(_img_modules)
include $(BUILD_PHONY_PACKAGE)

_add-charger-image :=
_img_modules :=
endif # LOCAL_CHARGER_NO_UI
+6 −14
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
#define HEALTHD_ANIMATION_H

#include <inttypes.h>

#include <string>

class GRSurface;
@@ -52,20 +53,11 @@ struct animation {
    // - When treating paths as relative paths, it adds ".png" suffix.
    // - When treating paths as absolute paths, it doesn't add the suffix. Hence, the suffix
    //   is added here.
    void set_resource_root(const std::string& root) {
        if (!animation_file.empty()) {
            animation_file = root + animation_file + ".png";
        }
        if (!fail_file.empty()) {
            fail_file = root + fail_file + ".png";
        }
        if (!text_clock.font_file.empty()) {
            text_clock.font_file = root + text_clock.font_file + ".png";
        }
        if (!text_percent.font_file.empty()) {
            text_percent.font_file = root + text_percent.font_file + ".png";
        }
    }
    // If |backup_root| is provided, additionally check if file under |root| is accessbile or not.
    // If not accessbile, use |backup_root| instead.
    // Require that |root| starts and ends with "/". If |backup_root| is provided, require that
    // |backup_root| starts and ends with "/".
    void set_resource_root(const std::string& root, const std::string& backup_root = "");

    std::string animation_file;
    std::string fail_file;
+60 −7
Original line number Diff line number Diff line
@@ -33,7 +33,9 @@
#include <optional>

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/macros.h>
#include <android-base/strings.h>

#include <linux/netlink.h>
#include <sys/socket.h>
@@ -58,6 +60,7 @@
#include <health2impl/Health.h>
#include <healthd/healthd.h>

using std::string_literals::operator""s;
using namespace android;
using android::hardware::Return;
using android::hardware::health::GetHealthServiceOrDefault;
@@ -103,7 +106,13 @@ char* locale;

namespace android {

// Resources in /product/etc/res overrides resources in /res.
// Legacy animation resources are loaded from this directory.
static constexpr const char* legacy_animation_root = "/res/images/";

// Built-in animation resources are loaded from this directory.
static constexpr const char* system_animation_root = "/system/etc/res/images/";

// Resources in /product/etc/res overrides resources in /res and /system/etc/res.
// If the device is using the Generic System Image (GSI), resources may exist in
// both paths.
static constexpr const char* product_animation_desc_path =
@@ -625,6 +634,12 @@ void Charger::InitAnimation() {
        batt_anim_.set_resource_root(product_animation_root);
    } else if (base::ReadFileToString(animation_desc_path, &content)) {
        parse_success = parse_animation_desc(content, &batt_anim_);
        // Fallback resources always exist in system_animation_root. On legacy devices with an old
        // ramdisk image, resources may be overridden under root. For example,
        // /res/images/charger/battery_fail.png may not be the same as
        // system/core/healthd/images/battery_fail.png in the source tree, but is a device-specific
        // image. Hence, load from /res, and fall back to /system/etc/res.
        batt_anim_.set_resource_root(legacy_animation_root, system_animation_root);
    } else {
        LOGW("Could not open animation description at %s\n", animation_desc_path);
        parse_success = false;
@@ -633,13 +648,13 @@ void Charger::InitAnimation() {
    if (!parse_success) {
        LOGW("Could not parse animation description. Using default animation.\n");
        batt_anim_ = BASE_ANIMATION;
        batt_anim_.animation_file.assign("charger/battery_scale");
        batt_anim_.animation_file.assign(system_animation_root + "charger/battery_scale.png"s);
        InitDefaultAnimationFrames();
        batt_anim_.frames = owned_frames_.data();
        batt_anim_.num_frames = owned_frames_.size();
    }
    if (batt_anim_.fail_file.empty()) {
        batt_anim_.fail_file.assign("charger/battery_fail");
        batt_anim_.fail_file.assign(system_animation_root + "charger/battery_fail.png"s);
    }

    LOGV("Animation Description:\n");
@@ -678,10 +693,11 @@ void Charger::Init(struct healthd_config* config) {

    InitAnimation();

    ret = res_create_display_surface(batt_anim_.fail_file.c_str(), &surf_unknown_);
    ret = CreateDisplaySurface(batt_anim_.fail_file.c_str(), &surf_unknown_);
    if (ret < 0) {
        LOGE("Cannot load custom battery_fail image. Reverting to built in: %d\n", ret);
        ret = res_create_display_surface("charger/battery_fail", &surf_unknown_);
        ret = CreateDisplaySurface((system_animation_root + "charger/battery_fail.png"s).c_str(),
                                   &surf_unknown_);
        if (ret < 0) {
            LOGE("Cannot load built in battery_fail image\n");
            surf_unknown_ = NULL;
@@ -692,8 +708,8 @@ void Charger::Init(struct healthd_config* config) {
    int scale_count;
    int scale_fps;  // Not in use (charger/battery_scale doesn't have FPS text
                    // chunk). We are using hard-coded frame.disp_time instead.
    ret = res_create_multi_display_surface(batt_anim_.animation_file.c_str(), &scale_count,
                                           &scale_fps, &scale_frames);
    ret = CreateMultiDisplaySurface(batt_anim_.animation_file.c_str(), &scale_count, &scale_fps,
                                    &scale_frames);
    if (ret < 0) {
        LOGE("Cannot load battery_scale image\n");
        batt_anim_.num_frames = 0;
@@ -722,6 +738,43 @@ void Charger::Init(struct healthd_config* config) {
    boot_min_cap_ = config->boot_min_cap;
}

int Charger::CreateDisplaySurface(const std::string& name, GRSurface** surface) {
    return res_create_display_surface(name.c_str(), surface);
}

int Charger::CreateMultiDisplaySurface(const std::string& name, int* frames, int* fps,
                                       GRSurface*** surface) {
    return res_create_multi_display_surface(name.c_str(), frames, fps, surface);
}

void set_resource_root_for(const std::string& root, const std::string& backup_root,
                           std::string* value) {
    if (value->empty()) {
        return;
    }

    std::string new_value = root + *value + ".png";
    // If |backup_root| is provided, additionally check whether the file under |root| is
    // accessible or not. If not accessible, fallback to file under |backup_root|.
    if (!backup_root.empty() && access(new_value.data(), F_OK) == -1) {
        new_value = backup_root + *value + ".png";
    }

    *value = new_value;
}

void animation::set_resource_root(const std::string& root, const std::string& backup_root) {
    CHECK(android::base::StartsWith(root, "/") && android::base::EndsWith(root, "/"))
            << "animation root " << root << " must start and end with /";
    CHECK(backup_root.empty() || (android::base::StartsWith(backup_root, "/") &&
                                  android::base::EndsWith(backup_root, "/")))
            << "animation backup root " << backup_root << " must start and end with /";
    set_resource_root_for(root, backup_root, &animation_file);
    set_resource_root_for(root, backup_root, &fail_file);
    set_resource_root_for(root, backup_root, &text_clock.font_file);
    set_resource_root_for(root, backup_root, &text_percent.font_file);
}

}  // namespace android

int healthd_charger_main(int argc, char** argv) {
+5 −0
Original line number Diff line number Diff line
@@ -53,6 +53,11 @@ class Charger : public ::android::hardware::health::V2_1::implementation::HalHea
    // HalHealthLoop overrides
    void OnHealthInfoChanged(const HealthInfo_2_1& health_info) override;

    // Allowed to be mocked for testing.
    virtual int CreateDisplaySurface(const std::string& name, GRSurface** surface);
    virtual int CreateMultiDisplaySurface(const std::string& name, int* frames, int* fps,
                                          GRSurface*** surface);

  private:
    void InitDefaultAnimationFrames();
    void UpdateScreenState(int64_t now);
Loading