face_auth和fingerprint_auth的弱依赖解耦合设计

背景:face以及finger依赖其他功能实现更多功能,但有时候是非必须的,裁掉会更加轻便,称为弱依赖,所以需要设计控制方式可以选择性进行编译该不必要的功能

我们的实现思路是通过宏定义的方式控制部分依赖项代码,在不需要的时候则不编译代码减少代码量

通过添加face_auth.gni定义

declare_args() {
  face_use_display_manager_component = true           //用于控制face_auth对display_mangaer的依赖
  if (defined(global_parts_info) &&          //当global_parts_info被定义且没有定义display_mangaer(被裁剪)的时候设为不依赖
      !defined(global_parts_info.powermgr_display_manager)) {
    face_use_display_manager_component = false
  }

  face_use_power_manager_component = true
  if (defined(global_parts_info) &&
      !defined(global_parts_info.powermgr_power_manager)) {
    face_use_power_manager_component = false
  }

  face_use_sensor_component = true
  if (defined(global_parts_info) &&
      !defined(global_parts_info.sensors_sensor)) {
    face_use_sensor_component = false
  }
}

global_parts_info是编译的时候生成的信息,从vendor中的config.json或者其关联的文件读取是否编译某个部件

这里我们通过删除display_mannger找到face_auth会产生报错的地方即和display_mannger相关

在service_ex/BUILD.gn

import("//base/useriam/face_auth/face_auth.gni")
 defines = []

 external_deps = [
    "c_utils:utils",
    "display_manager:displaymgr",    //删除
    "drivers_interface_camera:libbuffer_producer_sequenceable_1.0",
    "drivers_interface_face_auth:libface_auth_proxy_2.0",
    "hilog:libhilog",
    "init:libbegetutil",
    "power_manager:powermgr_client",   //删除
    "sensor:sensor_interface_native",  //删除
    "user_auth_framework:userauth_executors",
  ]
  
  if (face_use_display_manager_component) {    //如果定义了依赖该部件则添加依赖以及相关的宏定义
    external_deps += [ "display_manager:displaymgr" ]
    defines += [ "FACE_USE_DISPLAY_MANAGER_COMPONENT" ]
  }

  if (face_use_power_manager_component) {
    external_deps += [ "power_manager:powermgr_client" ]
  }

  if (defined(face_use_sensor_component)) {
    external_deps += [ "sensor:sensor_interface_native" ]
    defines += [ "FACE_USE_SENSOR_COMPONENT" ]
  }

services_ex/src/screen_brightness_task.cpp

#ifdef FACE_USE_DISPLAY_MANAGER_COMPONENT              //通过宏去控制编译
#include "display_power_mgr_client.h"
#endif
#include "parameter.h"
#ifdef FACE_USE_SENSOR_COMPONENT
#include "sensor_agent.h"
#endif


namespace UserIam {
namespace FaceAuth {
using ResultCode = UserAuth::ResultCode;
#ifdef FACE_USE_DISPLAY_MANAGER_COMPONENT
using namespace DisplayPowerMgr;
#endif
namespace {
#ifdef FACE_USE_SENSOR_COMPONENT
constexpr SensorUser SENSOR_USER = {
    "FaceAuthService",
    [](SensorEvent *event) {
@@ -53,6 +60,7 @@ constexpr SensorUser SENSOR_USER = {
        task->SetAmbientLight(data->intensity);
    },
};
#endif


ResultCode SubscribeSensor()
{
    IAM_LOGI("start");
#ifdef FACE_USE_SENSOR_COMPONENT
    int32_t subscribeSensorRet = SubscribeSensor(SENSOR_TYPE_ID_AMBIENT_LIGHT, &SENSOR_USER);
    IF_FALSE_LOGE_AND_RETURN_VAL(subscribeSensorRet == 0, ResultCode::GENERAL_ERROR);
    int32_t setBatchRet = SetBatch(SENSOR_TYPE_ID_AMBIENT_LIGHT, &SENSOR_USER, SENSOR_SAMPLE_AND_REPORT_INTERVAL,
@@ -126,16 +135,24 @@ ResultCode SubscribeSensor()
    IF_FALSE_LOGE_AND_RETURN_VAL(activateSensorRet == 0, ResultCode::GENERAL_ERROR);
    int32_t setModeRet = SetMode(SENSOR_TYPE_ID_AMBIENT_LIGHT, &SENSOR_USER, SENSOR_ON_CHANGE);
    IF_FALSE_LOGE_AND_RETURN_VAL(setModeRet == 0, ResultCode::GENERAL_ERROR);

    return ResultCode::SUCCESS;
#else
    IAM_LOGI("sensor component is not used");
    return ResultCode::GENERAL_ERROR
#endif

void UnsubscribeSensor()
{
    IAM_LOGI("start");
#ifdef FACE_USE_SENSOR_COMPONENT
    DeactivateSensor(SENSOR_TYPE_ID_AMBIENT_LIGHT, &SENSOR_USER);
    UnsubscribeSensor(SENSOR_TYPE_ID_AMBIENT_LIGHT, &SENSOR_USER);
    return;
#else
    IAM_LOGI("sensor component is not used");
    return;
#endif
}

void OverrideScreenBrightness(uint32_t brightness)
{
#ifdef FACE_USE_DISPLAY_MANAGER_COMPONENT
    int32_t displayId = DisplayPowerMgrClient::GetInstance().GetMainDisplayId();
    if (!DisplayPowerMgrClient::GetInstance().OverrideBrightness(brightness, displayId)) {
        IAM_LOGE("override brightness fail");
        return;
    }
#else
    IAM_LOGI("display_manager component is not used.");
    return;
#endif
}

void RestoreScreenBrightness()
{
#ifdef FACE_USE_DISPLAY_MANAGER_COMPONENT
    int32_t displayId = DisplayPowerMgrClient::GetInstance().GetMainDisplayId();
    if (!DisplayPowerMgrClient::GetInstance().RestoreBrightness(displayId)) {
        IAM_LOGE("restore brightness fail");
        return;
    }
#else
    IAM_LOGI("display_manager component is not used.");
    return;
#endif
}

uint32_t GetCurrentScreenBrightness()
{
#ifdef FACE_USE_DISPLAY_MANAGER_COMPONENT
    return DisplayPowerMgrClient::GetInstance().GetDeviceBrightness();
#else
    IAM_LOGI("display_manager component is not used.");
    return INVALID_BRIGHTNESS;                            //对于需要返回值的需要设置一个无效的亮度
#endif
}  



同理fingerprint的设计也是一样的

添加fingerprint_auth.gni文件

import("//base/useriam/fingerprint_auth/fingerprint_auth.gni")
去掉external_dep相关依赖添加到下方

defines = []

  if (use_display_manager_component) {
    external_deps += [ "display_manager:displaymgr" ]
    defines += [ "CONFIG_USE_DISPLAY_MANAGER_COMPONENT" ]
  }

  if (use_power_manager_component) {
    external_deps += [ "power_manager:powermgr_client" ]
    defines += [ "CONFIG_USE_POWER_MANAGER_COMPONENT" ]
  }
  
   if (defined(use_rosen_drawing) && use_rosen_drawing) {
    external_deps += [ "graphic_2d:2d_graphics" ]
  修改  defines = [ "USE_ROSEN_DRAWING" ] ->defines += [ "USE_ROSEN_DRAWING" ]
  }
#include "common_event_subscriber.h"
#include "common_event_support.h"
#include "iservice_registry.h"
#ifdef CONFIG_USE_POWER_MANAGER_COMPONENT
#include "power_mgr_client.h"
#endif
#include "system_ability_definition.h"
#include "system_ability_status_change_stub.h"
#include "want.h"
@@ -37,7 +39,9 @@ namespace UserIam {
namespace FingerprintAuth {
using namespace EventFwk;
using namespace AAFwk;
#ifdef CONFIG_USE_POWER_MANAGER_COMPONENT
using namespace PowerMgr;
#endif
using ResultCode = UserAuth::ResultCode;

namespace {
@@ -132,8 +136,10 @@ void ScreenStateMonitor::Subscribe()
    ResultCode result = EventSubscriber::Subscribe();
    IF_FALSE_LOGE_AND_RETURN(result == ResultCode::SUCCESS);
    isSubscribing_ = true;
#ifdef CONFIG_USE_POWER_MANAGER_COMPONENT
    isOn_ = PowerMgrClient::GetInstance().IsScreenOn();
    IAM_LOGI("screen on %{public}d", isOn_);
#endif
}

void ScreenStateMonitor::Unsubscribe()
 +6 services_ex/src/sensor_illumination_task.cpp  100644 -> 100644
@@ -18,7 +18,9 @@
#include "common_event_manager.h"
#include "common_event_subscriber.h"
#include "common_event_support.h"
#ifdef CONFIG_USE_DISPLAY_MANAGER_COMPONENT
#include "display_power_mgr_client.h"
#endif
#include "iservice_registry.h"
#include "pipeline/rs_render_thread.h"
#include "system_ability_definition.h"
@@ -231,7 +233,11 @@ ResultCode SensorIlluminationTask::DrawSurfaceNode()

    IAM_LOGI("start");

#ifdef CONFIG_USE_DISPLAY_MANAGER_COMPONENT
    uint32_t brightness = DisplayPowerMgr::DisplayPowerMgrClient::GetInstance().GetDeviceBrightness();
#else
    uint32_t brightness = INVALID_BRIGHTNESS;
#endif
    IF_FALSE_LOGE_AND_RETURN_VAL(brightness != INVALID_BRIGHTNESS, ResultCode::GENERAL_ERROR);
    IAM_LOGI("get device brightness %{public}u", brightness);
Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐