1 /* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef CHARGER_ANIMATION_H 16 #define CHARGER_ANIMATION_H 17 18 #include "animation_config.h" 19 #include "charger_log.h" 20 #include "components/ui_image_view.h" 21 #include "components/ui_label.h" 22 23 #include "animator/animator_manager.h" 24 #include "animator/easing_equation.h" 25 #include "charger_graphic_engine.h" 26 #include "components/root_view.h" 27 28 #include <sstream> 29 #include <string> 30 #include <thread> 31 #include <unistd.h> 32 #include <vector> 33 34 namespace OHOS { 35 namespace PowerMgr { 36 constexpr int CHAR_WIDTH = 5; 37 constexpr int MICROSECONDS_PER_MILLISECOND = 1000; 38 class ChargerAnimation { 39 enum AnimationState { 40 ANIMATION_STOP = 0, 41 ANIMATION_START, 42 LACKPOWER_CHARGING_PROMPT_STOP, 43 LACKPOWER_CHARGING_PROMPT_START, 44 LACKPOWER_NOTCHARGING_PROMPT_STOP, 45 LACKPOWER_NOTCHARGING_PROMPT_START, 46 }; 47 48 public: 49 ChargerAnimation() = default; 50 ~ChargerAnimation() = default; 51 bool InitConfig(); 52 void AnimationStart(const int32_t& capacity); 53 void AnimationStop(); 54 void CapacityDisplay(const int32_t& capacity); 55 void CapacityDestroy(); 56 void LackPowerChargingPromptStart(); 57 void LackPowerChargingPromptStop(); 58 void LackPowerNotChargingPromptStart(); 59 void LackPowerNotChargingPromptStop(); 60 61 class ChargerAnimatorCallback : public OHOS::AnimatorCallback { 62 public: ChargerAnimatorCallback(std::shared_ptr<UIImageView> imageView,ImageComponentInfo & info)63 ChargerAnimatorCallback(std::shared_ptr<UIImageView> imageView, ImageComponentInfo& info) 64 : view_ {imageView}, info_ {info} 65 { 66 } Init()67 void Init() 68 { 69 if (view_ == nullptr) { 70 BATTERY_HILOGE(FEATURE_CHARGING, "view_ is nullptr"); 71 return; 72 } 73 animator_ = std::make_unique<OHOS::Animator>(this, view_.get(), 0, true); 74 } 75 Callback(OHOS::UIView * view)76 virtual void Callback(OHOS::UIView* view) 77 { 78 if (stop_) { 79 return; 80 } 81 82 ShowNextImage(); 83 } 84 Start()85 void Start() 86 { 87 view_->SetVisible(true); 88 GetAnimator()->Start(); 89 stop_ = false; 90 } 91 Stop()92 void Stop() 93 { 94 view_->SetVisible(false); 95 stop_ = true; 96 } 97 GetAnimator()98 OHOS::Animator* GetAnimator() const 99 { 100 if (animator_ == nullptr) { 101 BATTERY_HILOGE(FEATURE_CHARGING, "animator_ is nullptr"); 102 return nullptr; 103 } 104 return animator_.get(); 105 } 106 ShowNextImage()107 void ShowNextImage() 108 { 109 std::stringstream ss; 110 ss << info_.resPath << info_.filePrefix << std::setw(CHAR_WIDTH) << std::setfill('0') << currId_ << ".png"; 111 currPath_ = ss.str(); 112 if (access(currPath_.c_str(), F_OK) == -1) { 113 BATTERY_HILOGE(FEATURE_CHARGING, "path not exist"); 114 } 115 116 view_->SetSrc(currPath_.c_str()); 117 view_->SetResizeMode(OHOS::UIImageView::ImageResizeMode::FILL); 118 currId_ = (currId_ + 1) % info_.imgCnt; 119 ChargerGraphicEngine::UsSleep(info_.updInterval * MICROSECONDS_PER_MILLISECOND); 120 } 121 122 private: 123 std::string currPath_ {}; 124 uint32_t currId_ {0}; 125 std::shared_ptr<UIImageView> view_; 126 std::unique_ptr<OHOS::Animator> animator_; 127 bool stop_; 128 ImageComponentInfo info_; 129 }; 130 131 private: 132 void InitRootView(); 133 bool InitAllComponents(); 134 std::unique_ptr<UILabel> lackPower_; 135 std::unique_ptr<UILabel> lackPowerNotCharge_; 136 std::unique_ptr<UILabel> percentLabel_; 137 std::shared_ptr<UIImageView> animatorImage_; 138 std::shared_ptr<ChargerAnimatorCallback> animatorCallback_ = nullptr; 139 enum AnimationState animationState_ = ANIMATION_STOP; 140 enum AnimationState chargingPromptState_ = LACKPOWER_CHARGING_PROMPT_STOP; 141 enum AnimationState notChargingPromptState_ = LACKPOWER_NOTCHARGING_PROMPT_STOP; 142 }; 143 } // namespace PowerMgr 144 } // namespace OHOS 145 #endif 146