• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "updater_ui_env.h"
16 #include <chrono>
17 #include <fstream>
18 #include <map>
19 #include <thread>
20 #include <unistd.h>
21 #include "callback_manager.h"
22 #include "common/graphic_startup.h"
23 #include "common/screen.h"
24 #include "components/root_view.h"
25 #include "graphic_engine.h"
26 #include "input_event.h"
27 #include "language/language_ui.h"
28 #include "log/log.h"
29 #include "page/page_manager.h"
30 #include "updater_ui_config.h"
31 
32 namespace Updater {
33 namespace {
34 constexpr std::array BRIGHTNESS_FILES {
35     std::pair { "/sys/class/leds/lcd_backlight0/brightness", "/sys/class/leds/lcd_backlight0/max_brightness" },
36     std::pair { "/sys/class/leds/lcd-backlight/brightness", "/sys/class/leds/lcd-backlight/max_brightness" }
37 };
38 
39 constexpr uint32_t WHITE_BGCOLOR = 0x000000ff;
40 }
41 
Init()42 void UpdaterUiEnv::Init()
43 {
44     [[maybe_unused]] static bool initOnce = [] () {
45         InitDisplayDriver(); // init display driver
46         InitEngine(); // Graphic UI init
47         InitConfig(); // page manager init
48         InitEvts(); // init input driver and input events callback
49         InitInputDriver(); // init input driver and input events callback
50         return true;
51     } ();
52 }
53 
InitEngine()54 void UpdaterUiEnv::InitEngine()
55 {
56     OHOS::GraphicStartUp::Init();
57     GraphicEngine::GetInstance().Init(WHITE_BGCOLOR, OHOS::ColorMode::ARGB8888, VECTOR_FONT_DIR);
58     InitRootView();
59     LOG(INFO) << "UxInitEngine done";
60 }
61 
InitConfig()62 void UpdaterUiEnv::InitConfig()
63 {
64     // load pages, language resource, ui strategy
65     if (!UpdaterUiConfig::Init()) {
66         LOG(ERROR) << "config init failed";
67     }
68 }
69 
InitEvts()70 void UpdaterUiEnv::InitEvts()
71 {
72     CallbackManager::Init(UpdaterUiConfig::GetFocusCfg());
73 }
74 
InitInputDriver()75 void UpdaterUiEnv::InitInputDriver()
76 {
77     HdfInit();
78 }
79 
InitDisplayDriver()80 void UpdaterUiEnv::InitDisplayDriver()
81 {
82     static_cast<void>(std::find_if(std::begin(BRIGHTNESS_FILES), std::end(BRIGHTNESS_FILES), [] (auto filePair) {
83         return InitBrightness(filePair.first, filePair.second);
84     }));
85 }
86 
InitRootView()87 void UpdaterUiEnv::InitRootView()
88 {
89     using namespace OHOS;
90     RootView::GetInstance()->SetPosition(0, 0);
91     RootView::GetInstance()->SetStyle(STYLE_BACKGROUND_COLOR, Color::Black().full);
92     RootView::GetInstance()->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
93     RootView::GetInstance()->Invalidate();
94 }
95 
InitBrightness(const char * brightnessFile,const char * maxBrightnessFile)96 bool UpdaterUiEnv::InitBrightness(const char *brightnessFile, const char *maxBrightnessFile)
97 {
98     if (access(brightnessFile, R_OK | W_OK) != 0 || access(maxBrightnessFile, R_OK) != 0) {
99         LOG(ERROR) << "can't access brigntness file";
100         return false;
101     }
102 
103     std::ifstream ifs { maxBrightnessFile };
104     if (!ifs.is_open()) {
105         LOG(ERROR) << "open " << maxBrightnessFile << " failed";
106         return false;
107     }
108     int maxValue = 0;
109     ifs >> maxValue;
110     if (ifs.fail() || ifs.bad()) {
111         LOG(ERROR) << "read int from " << maxBrightnessFile << " failed sd maxValue = " << maxValue;
112         return false;
113     }
114 
115     std::ofstream ofs { brightnessFile };
116     if (!ofs.is_open()) {
117         LOG(ERROR) << "open " << brightnessFile << " failed";
118         return false;
119     }
120 
121     constexpr std::size_t SHIFT_WIDTH = 3;
122     // set to one eighth of max brigtness
123     ofs << (static_cast<std::size_t>(maxValue) >> SHIFT_WIDTH);
124     if (ofs.fail() || ofs.bad()) {
125         LOG(ERROR) << "write int to " << brightnessFile << " failed";
126         return false;
127     }
128     return true;
129 }
130 } // namespace Updater
131