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
InitDisplayDriverExt(void)42 __attribute__((weak)) void InitDisplayDriverExt(void)
43 {
44 LOG(INFO) << "InitDisplayDriver extension function";
45 }
46
Init()47 void UpdaterUiEnv::Init()
48 {
49 [[maybe_unused]] static bool initOnce = [] () {
50 InitDisplayDriver(); // init display driver
51 InitEngine(); // Graphic UI init
52 InitConfig(); // page manager init
53 InitEvts(); // init input driver and input events callback
54 InitInputDriver(); // init input driver and input events callback
55 return true;
56 } ();
57 }
58
InitEngine()59 void UpdaterUiEnv::InitEngine()
60 {
61 OHOS::GraphicStartUp::Init();
62 GraphicEngine::GetInstance().Init(WHITE_BGCOLOR, OHOS::ColorMode::ARGB8888, VECTOR_FONT_DIR);
63 InitRootView();
64 LOG(INFO) << "UxInitEngine done";
65 }
66
InitConfig()67 void UpdaterUiEnv::InitConfig()
68 {
69 // load pages, language resource, ui strategy
70 if (!UpdaterUiConfig::Init()) {
71 LOG(ERROR) << "config init failed";
72 }
73 }
74
InitEvts()75 void UpdaterUiEnv::InitEvts()
76 {
77 CallbackManager::Init(UpdaterUiConfig::GetFocusCfg());
78 }
79
InitInputDriver()80 void UpdaterUiEnv::InitInputDriver()
81 {
82 InputEvent::GetInstance().HdfInit();
83 }
84
InitDisplayDriver()85 void UpdaterUiEnv::InitDisplayDriver()
86 {
87 InitDisplayDriverExt();
88 static_cast<void>(std::find_if(std::begin(BRIGHTNESS_FILES), std::end(BRIGHTNESS_FILES), [] (auto filePair) {
89 return InitBrightness(filePair.first, filePair.second);
90 }));
91 }
92
InitRootView()93 void UpdaterUiEnv::InitRootView()
94 {
95 using namespace OHOS;
96 RootView::GetInstance()->SetPosition(0, 0);
97 RootView::GetInstance()->SetStyle(STYLE_BACKGROUND_COLOR, Color::Black().full);
98 RootView::GetInstance()->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
99 RootView::GetInstance()->Invalidate();
100 }
101
InitBrightness(const char * brightnessFile,const char * maxBrightnessFile)102 bool UpdaterUiEnv::InitBrightness(const char *brightnessFile, const char *maxBrightnessFile)
103 {
104 if (access(brightnessFile, R_OK | W_OK) != 0 || access(maxBrightnessFile, R_OK) != 0) {
105 LOG(ERROR) << "can't access brigntness file";
106 return false;
107 }
108
109 std::ifstream ifs { maxBrightnessFile };
110 if (!ifs.is_open()) {
111 LOG(ERROR) << "open " << maxBrightnessFile << " failed";
112 return false;
113 }
114 int maxValue = 0;
115 ifs >> maxValue;
116 if (ifs.fail() || ifs.bad()) {
117 LOG(ERROR) << "read int from " << maxBrightnessFile << " failed sd maxValue = " << maxValue;
118 return false;
119 }
120
121 std::ofstream ofs { brightnessFile };
122 if (!ofs.is_open()) {
123 LOG(ERROR) << "open " << brightnessFile << " failed";
124 return false;
125 }
126
127 constexpr std::size_t SHIFT_WIDTH = 3;
128 // set to one eighth of max brigtness
129 ofs << (static_cast<std::size_t>(maxValue) >> SHIFT_WIDTH);
130 if (ofs.fail() || ofs.bad()) {
131 LOG(ERROR) << "write int to " << brightnessFile << " failed";
132 return false;
133 }
134 return true;
135 }
136 } // namespace Updater
137