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