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