• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 "system_dialog_scheduler.h"
16 
17 #include <cmath>
18 #include <regex>
19 
20 #include "display_info.h"
21 #include "constants.h"
22 #include "ability_util.h"
23 #include "app_scheduler.h"
24 #include "dm_common.h"
25 #include "display_manager.h"
26 #include "errors.h"
27 #include "hilog_wrapper.h"
28 #include "in_process_call_wrapper.h"
29 #include "locale_config.h"
30 #include "parameters.h"
31 #include "resource_manager.h"
32 
33 namespace OHOS {
34 namespace AAFwk {
35 const int32_t UI_SELECTOR_DIALOG_WIDTH = 328 * 2;
36 const int32_t UI_SELECTOR_DIALOG_HEIGHT = 350 * 2;
37 const int32_t UI_SELECTOR_DIALOG_HEIGHT_NARROW = 350 * 2;
38 const int32_t UI_SELECTOR_DIALOG_WIDTH_NARROW = 328 * 2;
39 const int32_t UI_SELECTOR_DIALOG_PHONE_H1 = 240 * 2;
40 const int32_t UI_SELECTOR_DIALOG_PHONE_H2 = 340 * 2;
41 const int32_t UI_SELECTOR_DIALOG_PHONE_H3 = 350 * 2;
42 const int32_t UI_SELECTOR_DIALOG_PC_H0 = 1;
43 const int32_t UI_SELECTOR_DIALOG_PC_H2 = (64 * 2 + 56 + 48 + 54 + 64 + 48 + 2) * 2;
44 const int32_t UI_SELECTOR_DIALOG_PC_H3 = (64 * 3 + 56 + 48 + 54 + 64 + 48 + 2) * 2;
45 const int32_t UI_SELECTOR_DIALOG_PC_H4 = (64 * 4 + 56 + 48 + 54 + 64 + 48 + 2) * 2;
46 const int32_t UI_SELECTOR_DIALOG_PC_H5 = (64 * 4 + 56 + 48 + 54 + 64 + 48 + 58 + 2) * 2;
47 
48 const int32_t UI_SELECTOR_PORTRAIT_PHONE_H1 = 280;
49 const int32_t UI_SELECTOR_PORTRAIT_PHONE_H2 = 400;
50 const int32_t UI_SELECTOR_PORTRAIT_PHONE_H3 = 410;
51 const int32_t UI_SELECTOR_LANDSCAPE_SIGNAL_BAR = 24;
52 const int32_t UI_SELECTOR_LANDSCAPE_HEIGHT = 350;
53 const int32_t UI_SELECTOR_LANDSCAPE_HEIGHT_NARROW = 350;
54 const int32_t UI_SELECTOR_LANDSCAPE_PHONE_H1 = 280;
55 const int32_t UI_SELECTOR_LANDSCAPE_PHONE_H2 = 400;
56 const int32_t UI_SELECTOR_LANDSCAPE_PHONE_H3 = 410;
57 const int32_t UI_SELECTOR_LANDSCAPE_COUNT_THREE = 3;
58 const int32_t UI_SELECTOR_LANDSCAPE_COUNT_FOUR = 4;
59 const float UI_SELECTOR_LANDSCAPE_GRILLE_LARGE = 0.107692;
60 const float UI_SELECTOR_LANDSCAPE_GRILLE_SAMLL = 0.015385;
61 const float UI_SELECTOR_LANDSCAPE_MAX_RATIO = 0.9;
62 const float UI_SELECTOR_PORTRAIT_WIDTH_RATIO = 0.8;
63 const float UI_SELECTOR_PORTRAIT_WIDTH_EDGE_RATIO = 0.1;
64 const float UI_SELECTOR_PORTRAIT_HEIGHT_RATIO = 0.98;
65 
66 const int32_t UI_TIPS_DIALOG_WIDTH = 328 * 2;
67 const int32_t UI_TIPS_DIALOG_HEIGHT = 135 * 2;
68 const int32_t UI_TIPS_DIALOG_HEIGHT_NARROW = 135 * 2;
69 const int32_t UI_TIPS_DIALOG_WIDTH_NARROW = 328 * 2;
70 
71 const int32_t UI_JUMP_INTERCEPTOR_DIALOG_WIDTH = 328 * 2;
72 const int32_t UI_JUMP_INTERCEPTOR_DIALOG_HEIGHT = 135 * 2;
73 const int32_t UI_JUMP_INTERCEPTOR_DIALOG_HEIGHT_NARROW = 135 * 2;
74 const int32_t UI_JUMP_INTERCEPTOR_DIALOG_WIDTH_NARROW = 328 * 2;
75 
76 const int32_t UI_ANR_DIALOG_WIDTH = 328 * 2;
77 const int32_t UI_ANR_DIALOG_HEIGHT = 192 * 2;
78 const std::string APP_NAME = "appName";
79 const std::string DEVICE_TYPE = "deviceType";
80 const std::string OFF_SET_X = "offsetX";
81 const std::string OFF_SET_Y = "offsetY";
82 const std::string WIDTH = "width";
83 const std::string HEIGHT = "height";
84 const std::string MODEL_FLAG = "modelFlag";
85 const std::string ACTION = "action";
86 const std::string OVERSIZE_HEIGHT = "oversizeHeight";
87 
88 const int32_t UI_HALF = 2;
89 const int32_t UI_DEFAULT_BUTTOM_CLIP = 100;
90 const int32_t UI_WIDTH_780DP = 1560;
91 const int32_t UI_DEFAULT_WIDTH = 2560;
92 const int32_t UI_DEFAULT_HEIGHT = 1600;
93 
94 const std::string STR_PHONE = "phone";
95 const std::string STR_DEFAULT = "default";
96 const std::string DIALOG_NAME_ANR = "dialog_anr_service";
97 const std::string DIALOG_NAME_TIPS = "dialog_tips_service";
98 const std::string DIALOG_SELECTOR_NAME = "dialog_selector_service";
99 const std::string DIALOG_JUMP_INTERCEPTOR_NAME = "dialog_jump_interceptor_service";
100 
101 const std::string BUNDLE_NAME = "bundleName";
102 const std::string BUNDLE_NAME_DIALOG = "com.ohos.amsdialog";
103 const std::string DIALOG_PARAMS = "params";
104 const std::string DIALOG_POSITION = "position";
105 const std::string VERTICAL_SCREEN_DIALOG_POSITION = "landscapeScreen";
106 const std::string ABILITY_NAME_ANR_DIALOG = "AnrDialog";
107 const std::string ABILITY_NAME_TIPS_DIALOG = "TipsDialog";
108 const std::string ABILITY_NAME_SELECTOR_DIALOG = "SelectorDialog";
109 const std::string CALLER_TOKEN = "callerToken";
110 const std::string ABILITY_NAME_JUMP_INTERCEPTOR_DIALOG = "JumpInterceptorDialog";
111 const std::string TYPE_ONLY_MATCH_WILDCARD = "reserved/wildcard";
112 const std::string ORIENTATION = "orientation";
113 
114 const int32_t LINE_NUMS_ZERO = 0;
115 const int32_t LINE_NUMS_TWO = 2;
116 const int32_t LINE_NUMS_THREE = 3;
117 const int32_t LINE_NUMS_FOUR = 4;
118 const int32_t LINE_NUMS_EIGHT = 8;
119 
120 const float WIDTH_MULTIPLE = 0.8;
121 const float HEIGHT_MULTIPLE = 0.3;
122 const float SETX_WIDTH_MULTIPLE = 0.1;
123 
GetANRDialogWant(int userId,int pid,AAFwk::Want & want)124 bool SystemDialogScheduler::GetANRDialogWant(int userId, int pid, AAFwk::Want &want)
125 {
126     HILOG_DEBUG("GetANRDialogWant start");
127     AppExecFwk::ApplicationInfo appInfo;
128     bool debug;
129     auto appScheduler = DelayedSingleton<AppScheduler>::GetInstance();
130     if (appScheduler->GetApplicationInfoByProcessID(pid, appInfo, debug) != ERR_OK) {
131         HILOG_ERROR("Get application info failed.");
132         return false;
133     }
134 
135     std::string appName {""};
136     GetAppNameFromResource(appInfo.labelId, appInfo.bundleName, userId, appName);
137     DialogPosition position;
138     GetDialogPositionAndSize(DialogType::DIALOG_ANR, position);
139     std::string params = GetAnrParams(position, appName);
140 
141     want.SetElementName(BUNDLE_NAME_DIALOG, ABILITY_NAME_ANR_DIALOG);
142     want.SetParam(BUNDLE_NAME, appInfo.bundleName);
143     want.SetParam(DIALOG_POSITION, GetDialogPositionParams(position));
144     want.SetParam(DIALOG_PARAMS, params);
145     return true;
146 }
147 
GetAnrParams(const DialogPosition position,const std::string & appName) const148 const std::string SystemDialogScheduler::GetAnrParams(const DialogPosition position, const std::string &appName) const
149 {
150     nlohmann::json anrData;
151     anrData[APP_NAME] = appName;
152     anrData[DEVICE_TYPE] = deviceType_;
153     if (!position.wideScreen) {
154         anrData[OFF_SET_X] = position.window_offsetX;
155         anrData[OFF_SET_Y] = position.window_offsetY;
156         anrData[WIDTH] = position.window_width;
157         anrData[HEIGHT] = position.window_height;
158     }
159     return anrData.dump();
160 }
161 
GetTipsDialogWant(const sptr<IRemoteObject> & callerToken)162 Want SystemDialogScheduler::GetTipsDialogWant(const sptr<IRemoteObject> &callerToken)
163 {
164     HILOG_DEBUG("GetTipsDialogWant start");
165 
166     DialogPosition position;
167     GetDialogPositionAndSize(DialogType::DIALOG_TIPS, position);
168 
169     nlohmann::json jsonObj;
170     jsonObj[DEVICE_TYPE] = deviceType_;
171     const std::string params = jsonObj.dump();
172 
173     AAFwk::Want want;
174     want.SetElementName(BUNDLE_NAME_DIALOG, ABILITY_NAME_TIPS_DIALOG);
175     want.SetParam(DIALOG_POSITION, GetDialogPositionParams(position));
176     want.SetParam(DIALOG_PARAMS, params);
177     want.SetParam(CALLER_TOKEN, callerToken);
178     return want;
179 }
180 
GetJumpInterceptorDialogWant(Want & targetWant)181 Want SystemDialogScheduler::GetJumpInterceptorDialogWant(Want &targetWant)
182 {
183     HILOG_DEBUG("GetJumpInterceptorDialogWant start");
184 
185     DialogPosition position;
186     GetDialogPositionAndSize(DialogType::DIALOG_JUMP_INTERCEPTOR, position);
187 
188     nlohmann::json jsonObj;
189     jsonObj[DEVICE_TYPE] = deviceType_;
190     jsonObj["bundleName"] = targetWant.GetElement().GetBundleName();
191     jsonObj["abilityName"] = targetWant.GetElement().GetAbilityName();
192     jsonObj["moduleName"] = targetWant.GetElement().GetModuleName();
193     const std::string params = jsonObj.dump();
194 
195     targetWant.SetElementName(BUNDLE_NAME_DIALOG, ABILITY_NAME_JUMP_INTERCEPTOR_DIALOG);
196     targetWant.SetParam(DIALOG_POSITION, GetDialogPositionParams(position));
197     targetWant.SetParam(DIALOG_PARAMS, params);
198     targetWant.GetStringParam(DIALOG_PARAMS);
199     return targetWant;
200 }
201 
DialogPortraitPositionAdaptive(DialogPosition & position,float densityPixels,int lineNums) const202 void SystemDialogScheduler::DialogPortraitPositionAdaptive(
203     DialogPosition &position, float densityPixels, int lineNums) const
204 {
205     if (lineNums > LINE_NUMS_EIGHT) {
206         position.height = static_cast<int32_t>(UI_SELECTOR_PORTRAIT_PHONE_H3 * densityPixels);
207         return;
208     } else if (lineNums > LINE_NUMS_FOUR) {
209         position.height = static_cast<int32_t>(UI_SELECTOR_PORTRAIT_PHONE_H2 * densityPixels);
210         return;
211     } else if (lineNums > LINE_NUMS_ZERO) {
212         position.height = static_cast<int32_t>(UI_SELECTOR_PORTRAIT_PHONE_H1 * densityPixels);
213         return;
214     }
215 
216     HILOG_DEBUG("dialog portrait lineNums is zero.");
217 }
218 
GetSelectorDialogPortraitPosition(DialogPosition & position,int32_t height,int32_t width,int lineNums,float densityPixels) const219 void SystemDialogScheduler::GetSelectorDialogPortraitPosition(
220     DialogPosition &position, int32_t height, int32_t width, int lineNums, float densityPixels) const
221 {
222     HILOG_DEBUG("PortraitPosition height %{public}d width %{public}d density %{public}f.",
223         height, width, densityPixels);
224     position.width = static_cast<int32_t>(width * UI_SELECTOR_PORTRAIT_WIDTH_RATIO);
225     position.height = static_cast<int32_t>(UI_SELECTOR_DIALOG_HEIGHT * densityPixels);
226     position.width_narrow = static_cast<int32_t>(width * UI_SELECTOR_PORTRAIT_WIDTH_RATIO);
227     position.height_narrow = static_cast<int32_t>(UI_SELECTOR_DIALOG_HEIGHT_NARROW * densityPixels);
228 
229     if (width < UI_WIDTH_780DP) {
230         HILOG_INFO("show dialog narrow.");
231         position.width = position.width_narrow;
232         position.height = position.height_narrow;
233     }
234 
235     DialogPortraitPositionAdaptive(position, densityPixels, lineNums);
236 
237     int32_t portraitMax = static_cast<int32_t>(height * UI_SELECTOR_PORTRAIT_HEIGHT_RATIO);
238     if (portraitMax < position.height) {
239         position.oversizeHeight = true;
240         position.height = static_cast<int32_t>(UI_SELECTOR_PORTRAIT_PHONE_H1 * densityPixels);
241         HILOG_INFO("portrait ratio 0.9 height is %{public}d.", portraitMax);
242     }
243 
244     position.offsetX = static_cast<int32_t>(width * UI_SELECTOR_PORTRAIT_WIDTH_EDGE_RATIO);
245     position.offsetY = static_cast<int32_t>((height * UI_SELECTOR_PORTRAIT_HEIGHT_RATIO - position.height));
246     HILOG_DEBUG("dialog offset x:%{public}d y:%{public}d h:%{public}d w:%{public}d",
247         position.offsetX, position.offsetY, position.height, position.width);
248 }
249 
DialogLandscapePositionAdaptive(DialogPosition & position,float densityPixels,int lineNums) const250 void SystemDialogScheduler::DialogLandscapePositionAdaptive(
251     DialogPosition &position, float densityPixels, int lineNums) const
252 {
253     if (lineNums > LINE_NUMS_EIGHT) {
254         position.height = static_cast<int32_t>(UI_SELECTOR_LANDSCAPE_PHONE_H3 * densityPixels);
255         return;
256     } else if (lineNums > LINE_NUMS_FOUR) {
257         position.height = static_cast<int32_t>(UI_SELECTOR_LANDSCAPE_PHONE_H2 * densityPixels);
258         return;
259     } else if (lineNums > LINE_NUMS_ZERO) {
260         position.height = static_cast<int32_t>(UI_SELECTOR_LANDSCAPE_PHONE_H1 * densityPixels);
261         return;
262     }
263 
264     HILOG_DEBUG("dialog landscape lineNums is zero.");
265 }
266 
GetSelectorDialogLandscapePosition(DialogPosition & position,int32_t height,int32_t width,int lineNums,float densityPixels) const267 void SystemDialogScheduler::GetSelectorDialogLandscapePosition(
268     DialogPosition &position, int32_t height, int32_t width, int lineNums, float densityPixels) const
269 {
270     HILOG_DEBUG("LandscapePosition height %{public}d width %{public}d density %{public}f.",
271         height, width, densityPixels);
272     position.width = static_cast<int32_t>(width *
273         (UI_SELECTOR_LANDSCAPE_GRILLE_LARGE * UI_SELECTOR_LANDSCAPE_COUNT_FOUR +
274         UI_SELECTOR_LANDSCAPE_GRILLE_SAMLL * UI_SELECTOR_LANDSCAPE_COUNT_THREE));
275     position.height = static_cast<int32_t>((UI_SELECTOR_LANDSCAPE_HEIGHT) * densityPixels);
276     position.width_narrow = static_cast<int32_t>(width *
277         (UI_SELECTOR_LANDSCAPE_GRILLE_LARGE * UI_SELECTOR_LANDSCAPE_COUNT_FOUR +
278         UI_SELECTOR_LANDSCAPE_GRILLE_SAMLL * UI_SELECTOR_LANDSCAPE_COUNT_THREE));
279     position.height_narrow = static_cast<int32_t>((UI_SELECTOR_LANDSCAPE_HEIGHT_NARROW) * densityPixels);
280     DialogLandscapePositionAdaptive(position, densityPixels, lineNums);
281 
282     int32_t landscapeMax = static_cast<int32_t>(
283         (height - UI_SELECTOR_LANDSCAPE_SIGNAL_BAR * densityPixels) * UI_SELECTOR_LANDSCAPE_MAX_RATIO);
284     if (position.height > landscapeMax) {
285         position.oversizeHeight = true;
286         position.height = static_cast<int32_t>(UI_SELECTOR_LANDSCAPE_PHONE_H1 * densityPixels);
287         HILOG_INFO("landscape ratio 0.9 height is %{public}d.", landscapeMax);
288     }
289 
290     HILOG_DEBUG("dialog height is %{public}d.", position.height);
291     position.offsetX = static_cast<int32_t>((width - position.width) / UI_HALF);
292     position.offsetY = static_cast<int32_t>((height * UI_SELECTOR_PORTRAIT_HEIGHT_RATIO - position.height));
293     HILOG_DEBUG("dialog offset x:%{public}d y:%{public}d h:%{public}d w:%{public}d",
294         position.offsetX, position.offsetY, position.height, position.width);
295 }
296 
GetSelectorDialogPositionAndSize(DialogPosition & portraitPosition,DialogPosition & landscapePosition,int lineNums) const297 void SystemDialogScheduler::GetSelectorDialogPositionAndSize(
298     DialogPosition &portraitPosition, DialogPosition &landscapePosition, int lineNums) const
299 {
300     portraitPosition.wideScreen = (deviceType_ != STR_PHONE) && (deviceType_ != STR_DEFAULT);
301     portraitPosition.align = ((deviceType_ == STR_PHONE) || (deviceType_ == STR_DEFAULT)) ?
302         DialogAlign::BOTTOM : DialogAlign::CENTER;
303     landscapePosition.wideScreen = portraitPosition.wideScreen;
304     landscapePosition.align = portraitPosition.align;
305 
306     auto display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
307     if (display == nullptr) {
308         HILOG_ERROR("share dialog GetDefaultDisplay fail, try again.");
309         display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
310     }
311     if (display == nullptr) {
312         HILOG_ERROR("share dialog GetDefaultDisplay fail.");
313         return;
314     }
315 
316     auto displayInfo = display->GetDisplayInfo();
317     if (displayInfo == nullptr) {
318         HILOG_ERROR("share dialog GetDisplayInfo fail.");
319         return;
320     }
321 
322     HILOG_DEBUG("GetDialogPositionAndSize GetOrientation, %{public}d %{public}f",
323         displayInfo->GetDisplayOrientation(), display->GetVirtualPixelRatio());
324     if (displayInfo->GetDisplayOrientation() == Rosen::DisplayOrientation::PORTRAIT ||
325         displayInfo->GetDisplayOrientation() == Rosen::DisplayOrientation::PORTRAIT_INVERTED) {
326         HILOG_INFO("GetDialogPositionAndSize GetOrientation, PORTRAIT or PORTRAIT_INVERTED");
327         GetSelectorDialogPortraitPosition(portraitPosition, display->GetHeight(), display->GetWidth(),
328             lineNums, display->GetVirtualPixelRatio());
329         GetSelectorDialogLandscapePosition(landscapePosition, display->GetWidth(), display->GetHeight(),
330             lineNums, display->GetVirtualPixelRatio());
331         return;
332     }
333 
334     HILOG_INFO("GetDialogPositionAndSize GetOrientation, LANDSCAPE or LANDSCAPE_INVERTED");
335     GetSelectorDialogPortraitPosition(portraitPosition, display->GetWidth(), display->GetHeight(),
336         lineNums, display->GetVirtualPixelRatio());
337     GetSelectorDialogLandscapePosition(landscapePosition, display->GetHeight(), display->GetWidth(),
338         lineNums, display->GetVirtualPixelRatio());
339 }
340 
GetSelectorDialogWant(const std::vector<DialogAppInfo> & dialogAppInfos,Want & targetWant,const sptr<IRemoteObject> & callerToken)341 Want SystemDialogScheduler::GetSelectorDialogWant(const std::vector<DialogAppInfo> &dialogAppInfos, Want &targetWant,
342     const sptr<IRemoteObject> &callerToken)
343 {
344     HILOG_DEBUG("GetSelectorDialogWant start");
345     DialogPosition portraitPosition;
346     DialogPosition landscapePosition;
347     GetSelectorDialogPositionAndSize(portraitPosition, landscapePosition, static_cast<int>(dialogAppInfos.size()));
348     std::string params = GetSelectorParams(dialogAppInfos);
349 
350     targetWant.SetElementName(BUNDLE_NAME_DIALOG, ABILITY_NAME_SELECTOR_DIALOG);
351     targetWant.SetParam(DIALOG_POSITION, GetDialogPositionParams(portraitPosition));
352     targetWant.SetParam(VERTICAL_SCREEN_DIALOG_POSITION, GetDialogPositionParams(landscapePosition));
353     targetWant.SetParam(DIALOG_PARAMS, params);
354     if (callerToken != nullptr) {
355         HILOG_DEBUG("set callertoken to targetWant");
356         targetWant.SetParam(CALLER_TOKEN, callerToken);
357     }
358 
359     return targetWant;
360 }
361 
GetSelectorParams(const std::vector<DialogAppInfo> & infos) const362 const std::string SystemDialogScheduler::GetSelectorParams(const std::vector<DialogAppInfo> &infos) const
363 {
364     if (infos.empty()) {
365         HILOG_WARN("Invalid abilityInfos.");
366         return {};
367     }
368 
369     nlohmann::json jsonObject;
370     jsonObject[DEVICE_TYPE] = deviceType_;
371 
372     nlohmann::json hapListObj = nlohmann::json::array();
373     for (const auto &aInfo : infos) {
374         nlohmann::json aObj;
375         aObj["label"] = std::to_string(aInfo.labelId);
376         aObj["icon"] = std::to_string(aInfo.iconId);
377         aObj["bundle"] = aInfo.bundleName;
378         aObj["ability"] = aInfo.abilityName;
379         aObj["module"] = aInfo.moduleName;
380         hapListObj.emplace_back(aObj);
381     }
382     jsonObject["hapList"] = hapListObj;
383 
384     return jsonObject.dump();
385 }
386 
GetPcSelectorDialogWant(const std::vector<DialogAppInfo> & dialogAppInfos,Want & targetWant,const std::string & type,int32_t userId,const sptr<IRemoteObject> & callerToken)387 Want SystemDialogScheduler::GetPcSelectorDialogWant(const std::vector<DialogAppInfo> &dialogAppInfos,
388     Want &targetWant, const std::string &type, int32_t userId, const sptr<IRemoteObject> &callerToken)
389 {
390     HILOG_DEBUG("GetPcSelectorDialogWant start");
391     DialogPosition position;
392     GetDialogPositionAndSize(DialogType::DIALOG_SELECTOR, position, static_cast<int>(dialogAppInfos.size()));
393 
394     std::string params = GetPcSelectorParams(dialogAppInfos, type, userId, targetWant.GetAction());
395     targetWant.SetElementName(BUNDLE_NAME_DIALOG, ABILITY_NAME_SELECTOR_DIALOG);
396     targetWant.SetParam(DIALOG_POSITION, GetDialogPositionParams(position));
397     targetWant.SetParam(DIALOG_PARAMS, params);
398     targetWant.SetParam(CALLER_TOKEN, callerToken);
399 
400     return targetWant;
401 }
402 
GetPcSelectorParams(const std::vector<DialogAppInfo> & infos,const std::string & type,int32_t userId,const std::string & action) const403 const std::string SystemDialogScheduler::GetPcSelectorParams(const std::vector<DialogAppInfo> &infos,
404     const std::string &type, int32_t userId, const std::string &action) const
405 {
406     HILOG_DEBUG("GetPcSelectorParams start");
407     if (infos.empty()) {
408         HILOG_WARN("Invalid abilityInfos.");
409         return {};
410     }
411 
412     nlohmann::json jsonObject;
413     jsonObject[DEVICE_TYPE] = deviceType_;
414     jsonObject[ACTION] = action;
415     if (type == TYPE_ONLY_MATCH_WILDCARD) {
416         jsonObject[MODEL_FLAG] = true;
417     } else {
418         jsonObject[MODEL_FLAG] = false;
419     }
420 
421     nlohmann::json hapListObj = nlohmann::json::array();
422     for (const auto &info : infos) {
423         nlohmann::json aObj;
424         aObj["label"] = std::to_string(info.labelId);
425         aObj["icon"] = std::to_string(info.iconId);
426         aObj["bundle"] = info.bundleName;
427         aObj["ability"] = info.abilityName;
428         aObj["module"] = info.moduleName;
429         aObj["type"] = type;
430         aObj["userId"] = std::to_string(userId);
431         hapListObj.emplace_back(aObj);
432     }
433     jsonObject["hapList"] = hapListObj;
434 
435     return jsonObject.dump();
436 }
437 
GetDialogPositionParams(const DialogPosition position) const438 const std::string SystemDialogScheduler::GetDialogPositionParams(const DialogPosition position) const
439 {
440     nlohmann::json dialogPositionData;
441     dialogPositionData[OFF_SET_X] = position.offsetX;
442     dialogPositionData[OFF_SET_Y] = position.offsetY;
443     dialogPositionData[WIDTH] = position.width;
444     dialogPositionData[HEIGHT] = position.height;
445     dialogPositionData[OVERSIZE_HEIGHT] = position.oversizeHeight;
446     return dialogPositionData.dump();
447 }
448 
InitDialogPosition(DialogType type,DialogPosition & position) const449 void SystemDialogScheduler::InitDialogPosition(DialogType type, DialogPosition &position) const
450 {
451     position.wideScreen = (deviceType_ == STR_PHONE) ? false : (deviceType_ != STR_DEFAULT);
452     position.align = ((deviceType_ == STR_PHONE) ? true : (deviceType_ == STR_DEFAULT)) ?
453         DialogAlign::BOTTOM : DialogAlign::CENTER;
454     auto display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
455 
456     switch (type) {
457         case DialogType::DIALOG_ANR:
458             if (position.wideScreen) {
459                 position.width = UI_ANR_DIALOG_WIDTH;
460                 position.height = UI_ANR_DIALOG_HEIGHT;
461                 position.width_narrow = UI_ANR_DIALOG_WIDTH;
462                 position.height_narrow = UI_ANR_DIALOG_HEIGHT;
463                 position.align = DialogAlign::CENTER;
464             } else {
465                 position.width =  display->GetWidth();
466                 position.height = display->GetHeight();
467                 position.width_narrow =  display->GetWidth();
468                 position.height_narrow = display->GetHeight();
469                 position.window_width = UI_ANR_DIALOG_WIDTH;
470                 position.window_height = UI_ANR_DIALOG_HEIGHT;
471                 position.align = DialogAlign::CENTER;
472             }
473             break;
474         case DialogType::DIALOG_SELECTOR:
475             position.width = UI_SELECTOR_DIALOG_WIDTH;
476             position.height = UI_SELECTOR_DIALOG_HEIGHT;
477             position.width_narrow = UI_SELECTOR_DIALOG_WIDTH_NARROW;
478             position.height_narrow = UI_SELECTOR_DIALOG_HEIGHT_NARROW;
479             break;
480         case DialogType::DIALOG_TIPS:
481             position.width = UI_TIPS_DIALOG_WIDTH;
482             position.height = UI_TIPS_DIALOG_HEIGHT;
483             position.width_narrow = UI_TIPS_DIALOG_WIDTH_NARROW;
484             position.height_narrow = UI_TIPS_DIALOG_HEIGHT_NARROW;
485             break;
486         case DialogType::DIALOG_JUMP_INTERCEPTOR:
487             position.width = UI_JUMP_INTERCEPTOR_DIALOG_WIDTH;
488             position.height = UI_JUMP_INTERCEPTOR_DIALOG_HEIGHT;
489             position.width_narrow = UI_JUMP_INTERCEPTOR_DIALOG_WIDTH_NARROW;
490             position.height_narrow = UI_JUMP_INTERCEPTOR_DIALOG_HEIGHT_NARROW;
491             break;
492         default:
493             position.width = UI_DEFAULT_WIDTH;
494             position.height = UI_DEFAULT_HEIGHT;
495             position.width_narrow = UI_DEFAULT_WIDTH;
496             position.height_narrow = UI_DEFAULT_HEIGHT;
497             break;
498     }
499 }
500 
DialogPositionAdaptive(DialogPosition & position,int lineNums) const501 void SystemDialogScheduler::DialogPositionAdaptive(DialogPosition &position, int lineNums) const
502 {
503     if (position.wideScreen) {
504         if (lineNums <= LINE_NUMS_TWO) {
505             position.height = UI_SELECTOR_DIALOG_PC_H2;
506         } else if (lineNums == LINE_NUMS_THREE) {
507             position.height = UI_SELECTOR_DIALOG_PC_H3;
508         } else if (lineNums == LINE_NUMS_FOUR) {
509             position.height = UI_SELECTOR_DIALOG_PC_H4;
510         } else if (lineNums > LINE_NUMS_FOUR) {
511             position.height = UI_SELECTOR_DIALOG_PC_H5;
512         } else {
513             position.height = UI_SELECTOR_DIALOG_PC_H0;
514         }
515     } else {
516         position.height = (lineNums > LINE_NUMS_EIGHT) ? UI_SELECTOR_DIALOG_PHONE_H3 :
517             (lineNums > LINE_NUMS_THREE ? UI_SELECTOR_DIALOG_PHONE_H2 :
518             (lineNums > LINE_NUMS_ZERO ? UI_SELECTOR_DIALOG_PHONE_H1 : position.height));
519     }
520 }
521 
GetDialogPositionAndSize(DialogType type,DialogPosition & position,int lineNums) const522 void SystemDialogScheduler::GetDialogPositionAndSize(DialogType type, DialogPosition &position, int lineNums) const
523 {
524     InitDialogPosition(type, position);
525 
526     auto display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
527     if (display == nullptr) {
528         HILOG_WARN("share dialog GetDefaultDisplay fail, try again.");
529         display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
530     }
531     if (display != nullptr) {
532         HILOG_INFO("display width: %{public}d, height: %{public}d", display->GetWidth(), display->GetHeight());
533         if (display->GetWidth() < UI_WIDTH_780DP) {
534             HILOG_INFO("show dialog narrow.");
535             position.width = position.width_narrow;
536             position.height = position.height_narrow;
537         }
538 
539         if (type == DialogType::DIALOG_SELECTOR) {
540             DialogPositionAdaptive(position, lineNums);
541         }
542         switch (position.align) {
543             case DialogAlign::CENTER:
544                 if (position.wideScreen) {
545                     position.offsetX = (display->GetWidth() - position.width) / UI_HALF;
546                     position.offsetY = (display->GetHeight() - position.height) / UI_HALF;
547                 } else {
548                     position.window_width = position.window_width / UI_HALF;
549                     position.window_height = position.window_height / UI_HALF;
550                     position.offsetX = LINE_NUMS_ZERO;
551                     position.offsetY = LINE_NUMS_ZERO;
552                 }
553                 break;
554             case DialogAlign::BOTTOM:
555                 position.width = display->GetWidth() * WIDTH_MULTIPLE;
556                 position.height = display->GetHeight() * HEIGHT_MULTIPLE;
557                 position.offsetX = display->GetWidth() * SETX_WIDTH_MULTIPLE;
558                 position.offsetY = display->GetHeight() - position.height - UI_DEFAULT_BUTTOM_CLIP;
559                 break;
560             default:
561                 position.offsetX = (display->GetWidth() - position.width) / UI_HALF;
562                 position.offsetY = (display->GetHeight() - position.height - UI_DEFAULT_BUTTOM_CLIP) / UI_HALF;
563                 break;
564         }
565     } else {
566         HILOG_WARN("share dialog get display fail, use default wide.");
567         if (type == DialogType::DIALOG_SELECTOR) {
568             DialogPositionAdaptive(position, lineNums);
569         }
570         position.offsetX = (UI_DEFAULT_WIDTH - position.width) / UI_HALF;
571         position.offsetY = UI_DEFAULT_HEIGHT - position.height - UI_DEFAULT_BUTTOM_CLIP;
572     }
573 }
574 
GetAppNameFromResource(int32_t labelId,const std::string & bundleName,int32_t userId,std::string & appName)575 void SystemDialogScheduler::GetAppNameFromResource(int32_t labelId,
576     const std::string &bundleName, int32_t userId, std::string &appName)
577 {
578     std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
579     if (resourceManager == nullptr) {
580         HILOG_ERROR("resourceManager init failed!");
581         return;
582     }
583 
584     AppExecFwk::BundleInfo bundleInfo;
585     auto bms = GetBundleManager();
586     CHECK_POINTER(bms);
587     if (!IN_PROCESS_CALL(
588         bms->GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, userId))) {
589         HILOG_ERROR("Failed to get bundle info.");
590         return;
591     }
592     std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
593     UErrorCode status = U_ZERO_ERROR;
594     icu::Locale locale = icu::Locale::forLanguageTag(Global::I18n::LocaleConfig::GetSystemLanguage(), status);
595     resConfig->SetLocaleInfo(locale);
596     resourceManager->UpdateResConfig(*resConfig);
597 
598     std::regex pattern(std::string(AbilityBase::Constants::ABS_CODE_PATH) +
599         std::string(AbilityBase::Constants::FILE_SEPARATOR) + bundleInfo.name);
600     for (auto hapModuleInfo : bundleInfo.hapModuleInfos) {
601         std::string loadPath;
602         if (!hapModuleInfo.hapPath.empty()) {
603             loadPath = hapModuleInfo.hapPath;
604         } else {
605             loadPath = hapModuleInfo.resourcePath;
606         }
607         if (loadPath.empty()) {
608             continue;
609         }
610         HILOG_DEBUG("GetAppNameFromResource loadPath: %{public}s", loadPath.c_str());
611         if (!resourceManager->AddResource(loadPath.c_str())) {
612             HILOG_ERROR("ResourceManager add %{public}s resource path failed!", bundleInfo.name.c_str());
613         }
614     }
615     resourceManager->GetStringById(static_cast<uint32_t>(labelId), appName);
616     HILOG_DEBUG("Get app display info, labelId: %{public}d, appname: %{public}s", labelId, appName.c_str());
617 }
618 
GetBundleManager()619 sptr<AppExecFwk::IBundleMgr> SystemDialogScheduler::GetBundleManager()
620 {
621     if (iBundleManager_ == nullptr) {
622         iBundleManager_ = AbilityUtil::GetBundleManager();
623     }
624     return iBundleManager_;
625 }
626 }  // namespace AAFwk
627 }  // namespace OHOS
628