1 /* 2 * Copyright (c) 2021-2022 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 16 #ifndef DISPLAYMGR_GRADUAL_ANIMATOR_H 17 #define DISPLAYMGR_GRADUAL_ANIMATOR_H 18 19 #include <memory> 20 #include <string> 21 #include <cstdint> 22 #include <iosfwd> 23 #include <event_handler.h> 24 #include <event_runner.h> 25 #include "inner_event.h" 26 27 namespace OHOS { 28 namespace DisplayPowerMgr { 29 class AnimateCallback { 30 public: 31 virtual ~AnimateCallback() = default; 32 virtual void OnStart() = 0; 33 virtual void OnChanged(uint32_t currentValue) = 0; 34 virtual void OnEnd() = 0; 35 virtual void DiscountBrightness(double discount) = 0; 36 }; 37 38 class GradualAnimator : public std::enable_shared_from_this<GradualAnimator> { 39 public: 40 GradualAnimator(const std::string& name, std::shared_ptr<AnimateCallback>& callback); 41 virtual ~GradualAnimator() = default; 42 void StartAnimation(uint32_t from, uint32_t to, uint32_t duration); 43 void StopAnimation(); 44 bool IsAnimating() const; 45 uint32_t GetAnimationUpdateTime() const; 46 private: 47 static const uint32_t DEFAULT_UPDATE_TIME = 30; 48 static const uint32_t EVENT_STEP = 1; 49 static const int32_t STRIDE_ABSOLUTE_MIN = 1; 50 class AnimatorHandler : public AppExecFwk::EventHandler { 51 public: 52 AnimatorHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner, 53 const std::shared_ptr<GradualAnimator>& owner); 54 ~AnimatorHandler() = default; 55 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override; 56 private: 57 std::weak_ptr<GradualAnimator> owner_; 58 }; 59 void NextStep(); 60 std::string name_; 61 std::shared_ptr<AppExecFwk::EventRunner> eventRunner_; 62 std::shared_ptr<AnimatorHandler> handler_; 63 std::shared_ptr<AnimateCallback> callback_; 64 bool animating_ = false; 65 uint32_t fromBrightness_; 66 uint32_t toBrightness_; 67 uint32_t duration_; 68 uint32_t updateTime_; 69 uint32_t totalSteps_; 70 int32_t stride_; 71 uint32_t currentBrightness_; 72 uint32_t currentStep_; 73 }; 74 } // namespace DisplayPowerMgr 75 } // namespace OHOS 76 #endif // DISPLAYMGR_GRADUAL_ANIMATOR_H