1 /*
2 * Copyright (c) 2021 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 #include "action_popup.h"
17
18 #include "display_manager.h"
19 #include "ui_service_mgr_client.h"
20 #include "wm_common.h"
21 #include "constants.h"
22 #include "thermal_common.h"
23
24 namespace OHOS {
25 namespace PowerMgr {
26 namespace {
27 constexpr int UI_DIALOG_POWER_WIDTH_NARROW = 400;
28 constexpr int UI_DIALOG_POWER_HEIGHT_NARROW = 240;
29 constexpr int UI_DEFAULT_WIDTH = 2560;
30 constexpr int UI_DEFAULT_HEIGHT = 1600;
31 constexpr int UI_DEFAULT_BUTTOM_CLIP = 50 * 2;
32 constexpr int UI_HALF = 2;
33 }
InitParams(const std::string & params)34 bool ActionPopup::InitParams(const std::string ¶ms)
35 {
36 params_ = params;
37 return true;
38 }
39
SetStrict(bool flag)40 void ActionPopup::SetStrict(bool flag)
41 {
42 flag_ = flag;
43 }
44
AddActionValue(std::string value)45 void ActionPopup::AddActionValue(std::string value)
46 {
47 if (value.empty()) return;
48 valueList_.push_back(atoi(value.c_str()));
49 }
50
Execute()51 void ActionPopup::Execute()
52 {
53 uint32_t value = lastValue_;
54 if (valueList_.empty()) {
55 value = 0;
56 } else {
57 if (flag_) {
58 value = *max_element(valueList_.begin(), valueList_.end());
59 } else {
60 value = *min_element(valueList_.begin(), valueList_.end());
61 }
62 valueList_.clear();
63 }
64
65 if (value != lastValue_) {
66 HandlePopupEvent(value);
67 lastValue_ = value;
68 }
69 }
70
HandlePopupEvent(const int32_t value)71 void ActionPopup::HandlePopupEvent(const int32_t value)
72 {
73 bool ret = false;
74 switch (value) {
75 case LOWER_TEMP:
76 ret = ShowDialog(THERMAL_LOWER_TEMP_PARAMS);
77 if (!ret) {
78 THERMAL_HILOGE(COMP_SVC, "failed to popup");
79 return;
80 }
81 break;
82 case HIGHER_TEMP:
83 ret = ShowDialog(THERMAL_HIGH_TEMP_PARAMS);
84 if (!ret) {
85 THERMAL_HILOGE(COMP_SVC, "failed to popup");
86 return;
87 }
88 break;
89 default:
90 break;
91 }
92 }
93
ShowDialog(const std::string & params)94 bool ActionPopup::ShowDialog(const std::string ¶ms)
95 {
96 // show dialog
97 int pos_x;
98 int pos_y;
99 int width;
100 int height;
101 bool wideScreen;
102
103 GetDisplayPosition(pos_x, pos_y, width, height, wideScreen);
104
105 if (params.empty()) {
106 return false;
107 }
108
109 Ace::UIServiceMgrClient::GetInstance()->ShowDialog(
110 "thermal_dialog",
111 params,
112 OHOS::Rosen::WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW,
113 pos_x,
114 pos_y,
115 width,
116 height,
117 [this](int32_t id, const std::string& event, const std::string& params) {
118 THERMAL_HILOGD(COMP_SVC, "Dialog callback: %{public}s, %{public}s",
119 event.c_str(), params.c_str());
120 if (event == "EVENT_CANCEL") {
121 Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id);
122 }
123 });
124 return true;
125 }
126
GetDisplayPosition(int32_t & offsetX,int32_t & offsetY,int32_t & width,int32_t & height,bool & wideScreen)127 void ActionPopup::GetDisplayPosition(
128 int32_t& offsetX, int32_t& offsetY, int32_t& width, int32_t& height, bool& wideScreen)
129 {
130 wideScreen = true;
131 auto display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
132 if (display == nullptr) {
133 THERMAL_HILOGE(COMP_SVC, "dialog GetDefaultDisplay fail, try again.");
134 display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
135 }
136
137 if (display != nullptr) {
138 THERMAL_HILOGD(COMP_SVC, "display size: %{public}d x %{public}d",
139 display->GetWidth(), display->GetHeight());
140 if (display->GetWidth() < display->GetHeight()) {
141 THERMAL_HILOGD(COMP_SVC, "share dialog narrow.");
142 const int NARROW_WIDTH_N = 3;
143 const int NARROW_WIDTH_D = 4;
144 const int NARROW_HEIGHT_RATE = 8;
145 wideScreen = false;
146 width = display->GetWidth() * NARROW_WIDTH_N / NARROW_WIDTH_D;
147 height = display->GetHeight() / NARROW_HEIGHT_RATE;
148 } else {
149 THERMAL_HILOGD(COMP_SVC, "share dialog wide.");
150 const int NARROW_WIDTH_N = 1;
151 const int NARROW_WIDTH_D = 3;
152 const int NARROW_HEIGHT_RATE = 6;
153 wideScreen = true;
154 width = display->GetWidth() * NARROW_WIDTH_N / NARROW_WIDTH_D;
155 height = display->GetHeight() / NARROW_HEIGHT_RATE;
156 }
157 offsetX = (display->GetWidth() - width) / UI_HALF;
158 offsetY = display->GetHeight() - height - UI_DEFAULT_BUTTOM_CLIP;
159 } else {
160 THERMAL_HILOGD(COMP_SVC, "dialog get display fail, use default wide.");
161 wideScreen = false;
162 width = UI_DIALOG_POWER_WIDTH_NARROW;
163 height = UI_DIALOG_POWER_HEIGHT_NARROW;
164 offsetX = (UI_DEFAULT_WIDTH - width) / UI_HALF;
165 offsetY = UI_DEFAULT_HEIGHT - height - UI_DEFAULT_BUTTOM_CLIP;
166 }
167 THERMAL_HILOGD(COMP_SVC, "GetDisplayPosition: %{public}d, %{public}d (%{public}d x %{public}d)",
168 offsetX, offsetY, width, height);
169 }
170 } // namespace PowerMgr
171 } // namespace OHOS
172