1 /*
2  * Copyright (c) 2022-2024 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 "ability_record.h"
18 #include "ability_util.h"
19 #include "app_gallery_enable_util.h"
20 #include "app_utils.h"
21 #include "display_info.h"
22 #include "display_manager.h"
23 #include "hitrace_meter.h"
24 #include "scene_board_judgement.h"
25 #include "ui_extension_utils.h"
26 
27 namespace OHOS {
28 namespace AAFwk {
29 const int32_t UI_SELECTOR_DIALOG_WIDTH = 328 * 2;
30 const int32_t UI_SELECTOR_DIALOG_HEIGHT = 350 * 2;
31 const int32_t UI_SELECTOR_DIALOG_HEIGHT_NARROW = 350 * 2;
32 const int32_t UI_SELECTOR_DIALOG_WIDTH_NARROW = 328 * 2;
33 const int32_t UI_SELECTOR_DIALOG_PHONE_H1 = 240 * 2;
34 const int32_t UI_SELECTOR_DIALOG_PHONE_H2 = 340 * 2;
35 const int32_t UI_SELECTOR_DIALOG_PHONE_H3 = 350 * 2;
36 const int32_t UI_SELECTOR_DIALOG_PC_H0 = 1;
37 const int32_t UI_SELECTOR_DIALOG_PC_H2 = (64 * 2 + 56 + 48 + 54 + 64 + 48 + 2) * 2;
38 const int32_t UI_SELECTOR_DIALOG_PC_H3 = (64 * 3 + 56 + 48 + 54 + 64 + 48 + 2) * 2;
39 const int32_t UI_SELECTOR_DIALOG_PC_H4 = (64 * 4 + 56 + 48 + 54 + 64 + 48 + 2) * 2;
40 const int32_t UI_SELECTOR_DIALOG_PC_H5 = (64 * 4 + 56 + 48 + 54 + 64 + 48 + 58 + 2) * 2;
41 
42 const int32_t UI_SELECTOR_PORTRAIT_PHONE_H1 = 280;
43 const int32_t UI_SELECTOR_PORTRAIT_PHONE_H2 = 400;
44 const int32_t UI_SELECTOR_PORTRAIT_PHONE_H3 = 410;
45 const int32_t UI_SELECTOR_LANDSCAPE_SIGNAL_BAR = 24;
46 const int32_t UI_SELECTOR_LANDSCAPE_HEIGHT = 350;
47 const int32_t UI_SELECTOR_LANDSCAPE_HEIGHT_NARROW = 350;
48 const int32_t UI_SELECTOR_LANDSCAPE_PHONE_H1 = 280;
49 const int32_t UI_SELECTOR_LANDSCAPE_PHONE_H2 = 400;
50 const int32_t UI_SELECTOR_LANDSCAPE_PHONE_H3 = 410;
51 const int32_t UI_SELECTOR_LANDSCAPE_COUNT_THREE = 3;
52 const int32_t UI_SELECTOR_LANDSCAPE_COUNT_FOUR = 4;
53 const float UI_SELECTOR_LANDSCAPE_GRILLE_LARGE = 0.107692;
54 const float UI_SELECTOR_LANDSCAPE_GRILLE_SAMLL = 0.015385;
55 const float UI_SELECTOR_LANDSCAPE_MAX_RATIO = 0.9;
56 const float UI_SELECTOR_PORTRAIT_WIDTH_RATIO = 0.8;
57 const float UI_SELECTOR_PORTRAIT_WIDTH_EDGE_RATIO = 0.1;
58 const float UI_SELECTOR_PORTRAIT_HEIGHT_RATIO = 0.98;
59 
60 const int32_t UI_TIPS_DIALOG_WIDTH = 328 * 2;
61 const int32_t UI_TIPS_DIALOG_HEIGHT = 135 * 2;
62 const int32_t UI_TIPS_DIALOG_HEIGHT_NARROW = 135 * 2;
63 const int32_t UI_TIPS_DIALOG_WIDTH_NARROW = 328 * 2;
64 
65 const int32_t UI_JUMP_INTERCEPTOR_DIALOG_WIDTH = 328 * 2;
66 const int32_t UI_JUMP_INTERCEPTOR_DIALOG_HEIGHT = 135 * 2;
67 const int32_t UI_JUMP_INTERCEPTOR_DIALOG_HEIGHT_NARROW = 135 * 2;
68 const int32_t UI_JUMP_INTERCEPTOR_DIALOG_WIDTH_NARROW = 328 * 2;
69 
70 const int32_t UI_ANR_DIALOG_WIDTH = 328 * 2;
71 const int32_t UI_ANR_DIALOG_HEIGHT = 192 * 2;
72 const std::string APP_NAME = "appName";
73 const std::string IS_DEFAULT_SELECTOR = "isDefaultSelector";
74 const std::string OFF_SET_X = "offsetX";
75 const std::string OFF_SET_Y = "offsetY";
76 const std::string WIDTH = "width";
77 const std::string HEIGHT = "height";
78 const std::string MODEL_FLAG = "modelFlag";
79 const std::string ACTION = "action";
80 const std::string OVERSIZE_HEIGHT = "oversizeHeight";
81 
82 const int32_t UI_HALF = 2;
83 const int32_t UI_DEFAULT_BUTTOM_CLIP = 100;
84 const int32_t UI_WIDTH_780DP = 1560;
85 const int32_t UI_DEFAULT_WIDTH = 2560;
86 const int32_t UI_DEFAULT_HEIGHT = 1600;
87 
88 const std::string STR_PHONE = "phone";
89 const std::string STR_DEFAULT = "default";
90 const std::string DIALOG_NAME_ANR = "dialog_anr_service";
91 const std::string DIALOG_NAME_TIPS = "dialog_tips_service";
92 const std::string DIALOG_SELECTOR_NAME = "dialog_selector_service";
93 const std::string DIALOG_JUMP_INTERCEPTOR_NAME = "dialog_jump_interceptor_service";
94 
95 const std::string BUNDLE_NAME = "bundleName";
96 const std::string BUNDLE_NAME_DIALOG = "com.ohos.amsdialog";
97 const std::string DIALOG_PARAMS = "params";
98 const std::string DIALOG_POSITION = "position";
99 const std::string VERTICAL_SCREEN_DIALOG_POSITION = "landscapeScreen";
100 const std::string ABILITY_NAME_FREEZE_DIALOG = "SwitchUserDialog";
101 const std::string ABILITY_NAME_ASSERT_FAULT_DIALOG = "AssertFaultDialog";
102 const std::string ABILITY_NAME_TIPS_DIALOG = "TipsDialog";
103 const std::string ABILITY_NAME_SELECTOR_DIALOG = "SelectorDialog";
104 const std::string ABILITY_NAME_APPGALLERY_SELECTOR_DIALOG = "AppSelectorExtensionAbility";
105 const std::string UIEXTENSION_TYPE_KEY = "ability.want.params.uiExtensionType";
106 const std::string UIEXTENSION_SYS_COMMON_UI = "sys/commonUI";
107 const std::string CALLER_TOKEN = "callerToken";
108 const std::string ABILITY_NAME_JUMP_INTERCEPTOR_DIALOG = "JumpInterceptorDialog";
109 const std::string TYPE_ONLY_MATCH_WILDCARD = "reserved/wildcard";
110 const std::string ORIENTATION = "orientation";
111 
112 const int32_t LINE_NUMS_ZERO = 0;
113 const int32_t LINE_NUMS_TWO = 2;
114 const int32_t LINE_NUMS_THREE = 3;
115 const int32_t LINE_NUMS_FOUR = 4;
116 const int32_t LINE_NUMS_EIGHT = 8;
117 
118 const float WIDTH_MULTIPLE = 0.8;
119 const float HEIGHT_MULTIPLE = 0.3;
120 const float SETX_WIDTH_MULTIPLE = 0.1;
121 
GetTipsDialogWant(const sptr<IRemoteObject> & callerToken)122 Want SystemDialogScheduler::GetTipsDialogWant(const sptr<IRemoteObject> &callerToken)
123 {
124     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
125     TAG_LOGD(AAFwkTag::DIALOG, "GetTipsDialogWant start");
126 
127     DialogPosition position;
128     GetDialogPositionAndSize(DialogType::DIALOG_TIPS, position);
129 
130     nlohmann::json jsonObj;
131     jsonObj[IS_DEFAULT_SELECTOR] = AppUtils::GetInstance().IsSelectorDialogDefaultPossion();
132     const std::string params = jsonObj.dump();
133 
134     AAFwk::Want want;
135     want.SetElementName(BUNDLE_NAME_DIALOG, ABILITY_NAME_TIPS_DIALOG);
136     want.SetParam(DIALOG_POSITION, GetDialogPositionParams(position));
137     want.SetParam(DIALOG_PARAMS, params);
138     auto abilityRecord = Token::GetAbilityRecordByToken(callerToken);
139     if (abilityRecord && !UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType)) {
140         want.SetParam(CALLER_TOKEN, callerToken);
141     }
142     return want;
143 }
144 
GetJumpInterceptorDialogWant(Want & targetWant)145 Want SystemDialogScheduler::GetJumpInterceptorDialogWant(Want &targetWant)
146 {
147     TAG_LOGD(AAFwkTag::DIALOG, "start");
148 
149     DialogPosition position;
150     GetDialogPositionAndSize(DialogType::DIALOG_JUMP_INTERCEPTOR, position);
151 
152     nlohmann::json jsonObj;
153     jsonObj[IS_DEFAULT_SELECTOR] = AppUtils::GetInstance().IsSelectorDialogDefaultPossion();
154     jsonObj["bundleName"] = targetWant.GetElement().GetBundleName();
155     jsonObj["abilityName"] = targetWant.GetElement().GetAbilityName();
156     jsonObj["moduleName"] = targetWant.GetElement().GetModuleName();
157     const std::string params = jsonObj.dump();
158 
159     targetWant.SetElementName(BUNDLE_NAME_DIALOG, ABILITY_NAME_JUMP_INTERCEPTOR_DIALOG);
160     targetWant.SetParam(DIALOG_POSITION, GetDialogPositionParams(position));
161     targetWant.SetParam(DIALOG_PARAMS, params);
162     targetWant.GetStringParam(DIALOG_PARAMS);
163     return targetWant;
164 }
165 
DialogPortraitPositionAdaptive(DialogPosition & position,float densityPixels,int lineNums) const166 void SystemDialogScheduler::DialogPortraitPositionAdaptive(
167     DialogPosition &position, float densityPixels, int lineNums) const
168 {
169     if (lineNums > LINE_NUMS_EIGHT) {
170         position.height = static_cast<int32_t>(UI_SELECTOR_PORTRAIT_PHONE_H3 * densityPixels);
171         return;
172     } else if (lineNums > LINE_NUMS_FOUR) {
173         position.height = static_cast<int32_t>(UI_SELECTOR_PORTRAIT_PHONE_H2 * densityPixels);
174         return;
175     } else if (lineNums > LINE_NUMS_ZERO) {
176         position.height = static_cast<int32_t>(UI_SELECTOR_PORTRAIT_PHONE_H1 * densityPixels);
177         return;
178     }
179 
180     TAG_LOGD(AAFwkTag::DIALOG, "dialog portrait lineNums is zero");
181 }
182 
GetSelectorDialogPortraitPosition(DialogPosition & position,int32_t height,int32_t width,int lineNums,float densityPixels) const183 void SystemDialogScheduler::GetSelectorDialogPortraitPosition(
184     DialogPosition &position, int32_t height, int32_t width, int lineNums, float densityPixels) const
185 {
186     TAG_LOGD(AAFwkTag::DIALOG, "PortraitPosition height %{public}d width %{public}d density %{public}f",
187         height, width, densityPixels);
188     position.width = static_cast<int32_t>(width * UI_SELECTOR_PORTRAIT_WIDTH_RATIO);
189     position.height = static_cast<int32_t>(UI_SELECTOR_DIALOG_HEIGHT * densityPixels);
190     position.width_narrow = static_cast<int32_t>(width * UI_SELECTOR_PORTRAIT_WIDTH_RATIO);
191     position.height_narrow = static_cast<int32_t>(UI_SELECTOR_DIALOG_HEIGHT_NARROW * densityPixels);
192 
193     if (width < UI_WIDTH_780DP) {
194         TAG_LOGI(AAFwkTag::DIALOG, "show dialog narrow");
195         position.width = position.width_narrow;
196         position.height = position.height_narrow;
197     }
198 
199     DialogPortraitPositionAdaptive(position, densityPixels, lineNums);
200 
201     int32_t portraitMax = static_cast<int32_t>(height * UI_SELECTOR_PORTRAIT_HEIGHT_RATIO);
202     if (portraitMax < position.height) {
203         position.oversizeHeight = true;
204         position.height = static_cast<int32_t>(UI_SELECTOR_PORTRAIT_PHONE_H1 * densityPixels);
205         TAG_LOGI(AAFwkTag::DIALOG, "portrait ratio 0.9 height is %{public}d", portraitMax);
206     }
207 
208     position.offsetX = static_cast<int32_t>(width * UI_SELECTOR_PORTRAIT_WIDTH_EDGE_RATIO);
209     position.offsetY = static_cast<int32_t>((height * UI_SELECTOR_PORTRAIT_HEIGHT_RATIO - position.height));
210     TAG_LOGD(AAFwkTag::DIALOG, "dialog offset x:%{public}d y:%{public}d h:%{public}d w:%{public}d",
211         position.offsetX, position.offsetY, position.height, position.width);
212 }
213 
DialogLandscapePositionAdaptive(DialogPosition & position,float densityPixels,int lineNums) const214 void SystemDialogScheduler::DialogLandscapePositionAdaptive(
215     DialogPosition &position, float densityPixels, int lineNums) const
216 {
217     if (lineNums > LINE_NUMS_EIGHT) {
218         position.height = static_cast<int32_t>(UI_SELECTOR_LANDSCAPE_PHONE_H3 * densityPixels);
219         return;
220     } else if (lineNums > LINE_NUMS_FOUR) {
221         position.height = static_cast<int32_t>(UI_SELECTOR_LANDSCAPE_PHONE_H2 * densityPixels);
222         return;
223     } else if (lineNums > LINE_NUMS_ZERO) {
224         position.height = static_cast<int32_t>(UI_SELECTOR_LANDSCAPE_PHONE_H1 * densityPixels);
225         return;
226     }
227 
228     TAG_LOGD(AAFwkTag::DIALOG, "dialog landscape lineNums is zero");
229 }
230 
GetSelectorDialogLandscapePosition(DialogPosition & position,int32_t height,int32_t width,int lineNums,float densityPixels) const231 void SystemDialogScheduler::GetSelectorDialogLandscapePosition(
232     DialogPosition &position, int32_t height, int32_t width, int lineNums, float densityPixels) const
233 {
234     TAG_LOGD(AAFwkTag::DIALOG, "LandscapePosition height %{public}d width %{public}d density %{public}f",
235         height, width, densityPixels);
236     position.width = static_cast<int32_t>(width *
237         (UI_SELECTOR_LANDSCAPE_GRILLE_LARGE * UI_SELECTOR_LANDSCAPE_COUNT_FOUR +
238         UI_SELECTOR_LANDSCAPE_GRILLE_SAMLL * UI_SELECTOR_LANDSCAPE_COUNT_THREE));
239     position.height = static_cast<int32_t>((UI_SELECTOR_LANDSCAPE_HEIGHT) * densityPixels);
240     position.width_narrow = static_cast<int32_t>(width *
241         (UI_SELECTOR_LANDSCAPE_GRILLE_LARGE * UI_SELECTOR_LANDSCAPE_COUNT_FOUR +
242         UI_SELECTOR_LANDSCAPE_GRILLE_SAMLL * UI_SELECTOR_LANDSCAPE_COUNT_THREE));
243     position.height_narrow = static_cast<int32_t>((UI_SELECTOR_LANDSCAPE_HEIGHT_NARROW) * densityPixels);
244     DialogLandscapePositionAdaptive(position, densityPixels, lineNums);
245 
246     int32_t landscapeMax = static_cast<int32_t>(
247         (height - UI_SELECTOR_LANDSCAPE_SIGNAL_BAR * densityPixels) * UI_SELECTOR_LANDSCAPE_MAX_RATIO);
248     if (position.height > landscapeMax) {
249         position.oversizeHeight = true;
250         position.height = static_cast<int32_t>(UI_SELECTOR_LANDSCAPE_PHONE_H1 * densityPixels);
251         TAG_LOGI(AAFwkTag::DIALOG, "landscape ratio 0.9 height is %{public}d", landscapeMax);
252     }
253 
254     TAG_LOGD(AAFwkTag::DIALOG, "dialog height is %{public}d", position.height);
255     position.offsetX = static_cast<int32_t>((width - position.width) / UI_HALF);
256     position.offsetY = static_cast<int32_t>((height * UI_SELECTOR_PORTRAIT_HEIGHT_RATIO - position.height));
257     TAG_LOGD(AAFwkTag::DIALOG, "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 
GetSelectorDialogPositionAndSize(DialogPosition & portraitPosition,DialogPosition & landscapePosition,int lineNums) const261 void SystemDialogScheduler::GetSelectorDialogPositionAndSize(
262     DialogPosition &portraitPosition, DialogPosition &landscapePosition, int lineNums) const
263 {
264     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
265     portraitPosition.wideScreen = !AppUtils::GetInstance().IsSelectorDialogDefaultPossion();
266     portraitPosition.align = AppUtils::GetInstance().IsSelectorDialogDefaultPossion() ?
267         DialogAlign::BOTTOM : DialogAlign::CENTER;
268     landscapePosition.wideScreen = portraitPosition.wideScreen;
269     landscapePosition.align = portraitPosition.align;
270 
271     auto display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
272     if (display == nullptr) {
273         TAG_LOGE(AAFwkTag::DIALOG, "GetDefaultDisplay fail, try again");
274         display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
275     }
276     if (display == nullptr) {
277         TAG_LOGE(AAFwkTag::DIALOG, "GetDefaultDisplay fail");
278         return;
279     }
280 
281     auto displayInfo = display->GetDisplayInfo();
282     if (displayInfo == nullptr) {
283         TAG_LOGE(AAFwkTag::DIALOG, "GetDisplayInfo fail");
284         return;
285     }
286 
287     TAG_LOGD(AAFwkTag::DIALOG, "GetOrientation, %{public}d %{public}f",
288         displayInfo->GetDisplayOrientation(), display->GetVirtualPixelRatio());
289     if (displayInfo->GetDisplayOrientation() == Rosen::DisplayOrientation::PORTRAIT ||
290         displayInfo->GetDisplayOrientation() == Rosen::DisplayOrientation::PORTRAIT_INVERTED) {
291         TAG_LOGI(AAFwkTag::DIALOG, "GetOrientation, PORTRAIT or PORTRAIT_INVERTED");
292         GetSelectorDialogPortraitPosition(portraitPosition, display->GetHeight(), display->GetWidth(),
293             lineNums, display->GetVirtualPixelRatio());
294         GetSelectorDialogLandscapePosition(landscapePosition, display->GetWidth(), display->GetHeight(),
295             lineNums, display->GetVirtualPixelRatio());
296         return;
297     }
298 
299     TAG_LOGI(AAFwkTag::DIALOG, "GetOrientation, LANDSCAPE or LANDSCAPE_INVERTED");
300     GetSelectorDialogPortraitPosition(portraitPosition, display->GetWidth(), display->GetHeight(),
301         lineNums, display->GetVirtualPixelRatio());
302     GetSelectorDialogLandscapePosition(landscapePosition, display->GetHeight(), display->GetWidth(),
303         lineNums, display->GetVirtualPixelRatio());
304 }
305 
GetSelectorDialogWant(const std::vector<DialogAppInfo> & dialogAppInfos,Want & requestWant,Want & targetWant,const sptr<IRemoteObject> & callerToken)306 int SystemDialogScheduler::GetSelectorDialogWant(const std::vector<DialogAppInfo> &dialogAppInfos, Want &requestWant,
307     Want &targetWant, const sptr<IRemoteObject> &callerToken)
308 {
309     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
310     TAG_LOGD(AAFwkTag::DIALOG, "start");
311     DialogPosition portraitPosition;
312     DialogPosition landscapePosition;
313     GetSelectorDialogPositionAndSize(portraitPosition, landscapePosition, static_cast<int>(dialogAppInfos.size()));
314     std::string params = GetSelectorParams(dialogAppInfos);
315 
316     requestWant.SetElementName(BUNDLE_NAME_DIALOG, ABILITY_NAME_SELECTOR_DIALOG);
317     requestWant.SetParam(DIALOG_POSITION, GetDialogPositionParams(portraitPosition));
318     requestWant.SetParam(VERTICAL_SCREEN_DIALOG_POSITION, GetDialogPositionParams(landscapePosition));
319     requestWant.SetParam(DIALOG_PARAMS, params);
320     return GetSelectorDialogWantCommon(dialogAppInfos, requestWant, targetWant, callerToken);
321 }
322 
GetSelectorParams(const std::vector<DialogAppInfo> & infos) const323 const std::string SystemDialogScheduler::GetSelectorParams(const std::vector<DialogAppInfo> &infos) const
324 {
325     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
326     if (infos.empty()) {
327         TAG_LOGW(AAFwkTag::DIALOG, "Invalid abilityInfos");
328         return {};
329     }
330 
331     nlohmann::json jsonObject;
332     jsonObject[IS_DEFAULT_SELECTOR] = AppUtils::GetInstance().IsSelectorDialogDefaultPossion();
333 
334     nlohmann::json hapListObj = nlohmann::json::array();
335     for (const auto &aInfo : infos) {
336         nlohmann::json aObj;
337         aObj["label"] = std::to_string(aInfo.abilityLabelId);
338         aObj["icon"] = std::to_string(aInfo.abilityIconId);
339         aObj["bundle"] = aInfo.bundleName;
340         aObj["ability"] = aInfo.abilityName;
341         aObj["module"] = aInfo.moduleName;
342         aObj["appIndex"] = std::to_string(aInfo.appIndex);
343         aObj["bundleLabel"] = std::to_string(aInfo.bundleLabelId);
344         aObj["bundleIcon"] = std::to_string(aInfo.bundleIconId);
345         hapListObj.emplace_back(aObj);
346     }
347     jsonObject["hapList"] = hapListObj;
348 
349     return jsonObject.dump();
350 }
351 
GetPcSelectorDialogWant(const std::vector<DialogAppInfo> & dialogAppInfos,Want & requestWant,Want & targetWant,const std::string & type,int32_t userId,const sptr<IRemoteObject> & callerToken)352 int SystemDialogScheduler::GetPcSelectorDialogWant(const std::vector<DialogAppInfo> &dialogAppInfos, Want &requestWant,
353     Want &targetWant, const std::string &type, int32_t userId, const sptr<IRemoteObject> &callerToken)
354 {
355     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
356     TAG_LOGD(AAFwkTag::DIALOG, "start");
357     DialogPosition position;
358     GetDialogPositionAndSize(DialogType::DIALOG_SELECTOR, position, static_cast<int>(dialogAppInfos.size()));
359 
360     std::string params = GetPcSelectorParams(dialogAppInfos, type, userId, requestWant.GetAction());
361     requestWant.SetElementName(BUNDLE_NAME_DIALOG, ABILITY_NAME_SELECTOR_DIALOG);
362     requestWant.SetParam(DIALOG_POSITION, GetDialogPositionParams(position));
363     requestWant.SetParam(DIALOG_PARAMS, params);
364     return GetSelectorDialogWantCommon(dialogAppInfos, requestWant, targetWant, callerToken);
365 }
366 
GetPcSelectorParams(const std::vector<DialogAppInfo> & infos,const std::string & type,int32_t userId,const std::string & action) const367 const std::string SystemDialogScheduler::GetPcSelectorParams(const std::vector<DialogAppInfo> &infos,
368     const std::string &type, int32_t userId, const std::string &action) const
369 {
370     TAG_LOGD(AAFwkTag::DIALOG, "start");
371     if (infos.empty()) {
372         TAG_LOGW(AAFwkTag::DIALOG, "Invalid abilityInfos");
373         return {};
374     }
375 
376     nlohmann::json jsonObject;
377     jsonObject[IS_DEFAULT_SELECTOR] = AppUtils::GetInstance().IsSelectorDialogDefaultPossion();
378     jsonObject[ACTION] = action;
379     if (type == TYPE_ONLY_MATCH_WILDCARD) {
380         jsonObject[MODEL_FLAG] = true;
381     } else {
382         jsonObject[MODEL_FLAG] = false;
383     }
384 
385     nlohmann::json hapListObj = nlohmann::json::array();
386     for (const auto &info : infos) {
387         nlohmann::json aObj;
388         aObj["label"] = std::to_string(info.abilityLabelId);
389         aObj["icon"] = std::to_string(info.abilityIconId);
390         aObj["bundle"] = info.bundleName;
391         aObj["ability"] = info.abilityName;
392         aObj["module"] = info.moduleName;
393         aObj["type"] = type;
394         aObj["userId"] = std::to_string(userId);
395         aObj["appIndex"] = std::to_string(info.appIndex);
396         aObj["bundleLabel"] = std::to_string(info.bundleLabelId);
397         aObj["bundleIcon"] = std::to_string(info.bundleIconId);
398         hapListObj.emplace_back(aObj);
399     }
400     jsonObject["hapList"] = hapListObj;
401 
402     return jsonObject.dump();
403 }
404 
GetSelectorDialogWantCommon(const std::vector<DialogAppInfo> & dialogAppInfos,Want & requestWant,Want & targetWant,const sptr<IRemoteObject> & callerToken)405 int SystemDialogScheduler::GetSelectorDialogWantCommon(const std::vector<DialogAppInfo> &dialogAppInfos,
406     Want &requestWant, Want &targetWant, const sptr<IRemoteObject> &callerToken)
407 {
408     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
409     TAG_LOGD(AAFwkTag::DIALOG, "start");
410     bool isCallerStageBasedModel = true;
411     if (callerToken != nullptr) {
412         TAG_LOGD(AAFwkTag::DIALOG, "set callertoken to targetWant");
413         auto abilityRecord = Token::GetAbilityRecordByToken(callerToken);
414         if (abilityRecord && !abilityRecord->GetAbilityInfo().isStageBasedModel) {
415             isCallerStageBasedModel = false;
416         }
417         if (abilityRecord && UIExtensionUtils::IsUIExtension(abilityRecord->GetAbilityInfo().extensionAbilityType)) {
418             // SelectorDialog can't bind to the window of UIExtension, so set CALLER_TOKEN to null.
419             requestWant.RemoveParam(CALLER_TOKEN);
420         } else {
421             requestWant.SetParam(CALLER_TOKEN, callerToken);
422         }
423     }
424     if (AppGalleryEnableUtil::IsEnableAppGallerySelector() && Rosen::SceneBoardJudgement::IsSceneBoardEnabled()
425         && isCallerStageBasedModel) {
426         auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
427         if (bundleMgrHelper == nullptr) {
428             TAG_LOGE(AAFwkTag::DIALOG, "bundleMgrHelper null");
429             return INNER_ERR;
430         }
431         std::string bundleName;
432         if (!IN_PROCESS_CALL(bundleMgrHelper->QueryAppGalleryBundleName(bundleName))) {
433             TAG_LOGE(AAFwkTag::DIALOG, "QueryAppGalleryBundleName failed");
434             return INNER_ERR;
435         }
436         targetWant.SetElementName(bundleName, ABILITY_NAME_APPGALLERY_SELECTOR_DIALOG);
437         targetWant.SetParam(UIEXTENSION_TYPE_KEY, UIEXTENSION_SYS_COMMON_UI);
438         targetWant.SetParam("isCreateAppGallerySelector", true);
439     }
440     return ERR_OK;
441 }
442 
GetDialogPositionParams(const DialogPosition position) const443 const std::string SystemDialogScheduler::GetDialogPositionParams(const DialogPosition position) const
444 {
445     nlohmann::json dialogPositionData;
446     dialogPositionData[OFF_SET_X] = position.offsetX;
447     dialogPositionData[OFF_SET_Y] = position.offsetY;
448     dialogPositionData[WIDTH] = position.width;
449     dialogPositionData[HEIGHT] = position.height;
450     dialogPositionData[OVERSIZE_HEIGHT] = position.oversizeHeight;
451     return dialogPositionData.dump();
452 }
453 
InitDialogPosition(DialogType type,DialogPosition & position) const454 void SystemDialogScheduler::InitDialogPosition(DialogType type, DialogPosition &position) const
455 {
456     position.wideScreen = !AppUtils::GetInstance().IsSelectorDialogDefaultPossion();
457     position.align = AppUtils::GetInstance().IsSelectorDialogDefaultPossion() ?
458         DialogAlign::BOTTOM : DialogAlign::CENTER;
459     auto display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
460 
461     switch (type) {
462         case DialogType::DIALOG_ANR:
463             if (position.wideScreen) {
464                 position.width = UI_ANR_DIALOG_WIDTH;
465                 position.height = UI_ANR_DIALOG_HEIGHT;
466                 position.width_narrow = UI_ANR_DIALOG_WIDTH;
467                 position.height_narrow = UI_ANR_DIALOG_HEIGHT;
468                 position.align = DialogAlign::CENTER;
469             } else {
470                 position.width =  display->GetWidth();
471                 position.height = display->GetHeight();
472                 position.width_narrow =  display->GetWidth();
473                 position.height_narrow = display->GetHeight();
474                 position.window_width = UI_ANR_DIALOG_WIDTH;
475                 position.window_height = UI_ANR_DIALOG_HEIGHT;
476                 position.align = DialogAlign::CENTER;
477             }
478             break;
479         case DialogType::DIALOG_SELECTOR:
480             position.width = UI_SELECTOR_DIALOG_WIDTH;
481             position.height = UI_SELECTOR_DIALOG_HEIGHT;
482             position.width_narrow = UI_SELECTOR_DIALOG_WIDTH_NARROW;
483             position.height_narrow = UI_SELECTOR_DIALOG_HEIGHT_NARROW;
484             break;
485         case DialogType::DIALOG_TIPS:
486             position.width = UI_TIPS_DIALOG_WIDTH;
487             position.height = UI_TIPS_DIALOG_HEIGHT;
488             position.width_narrow = UI_TIPS_DIALOG_WIDTH_NARROW;
489             position.height_narrow = UI_TIPS_DIALOG_HEIGHT_NARROW;
490             break;
491         case DialogType::DIALOG_JUMP_INTERCEPTOR:
492             position.width = UI_JUMP_INTERCEPTOR_DIALOG_WIDTH;
493             position.height = UI_JUMP_INTERCEPTOR_DIALOG_HEIGHT;
494             position.width_narrow = UI_JUMP_INTERCEPTOR_DIALOG_WIDTH_NARROW;
495             position.height_narrow = UI_JUMP_INTERCEPTOR_DIALOG_HEIGHT_NARROW;
496             break;
497         default:
498             position.width = UI_DEFAULT_WIDTH;
499             position.height = UI_DEFAULT_HEIGHT;
500             position.width_narrow = UI_DEFAULT_WIDTH;
501             position.height_narrow = UI_DEFAULT_HEIGHT;
502             break;
503     }
504 }
505 
DialogPositionAdaptive(DialogPosition & position,int lineNums) const506 void SystemDialogScheduler::DialogPositionAdaptive(DialogPosition &position, int lineNums) const
507 {
508     if (position.wideScreen) {
509         if (lineNums <= LINE_NUMS_TWO) {
510             position.height = UI_SELECTOR_DIALOG_PC_H2;
511         } else if (lineNums == LINE_NUMS_THREE) {
512             position.height = UI_SELECTOR_DIALOG_PC_H3;
513         } else if (lineNums == LINE_NUMS_FOUR) {
514             position.height = UI_SELECTOR_DIALOG_PC_H4;
515         } else if (lineNums > LINE_NUMS_FOUR) {
516             position.height = UI_SELECTOR_DIALOG_PC_H5;
517         } else {
518             position.height = UI_SELECTOR_DIALOG_PC_H0;
519         }
520     } else {
521         position.height = (lineNums > LINE_NUMS_EIGHT) ? UI_SELECTOR_DIALOG_PHONE_H3 :
522             (lineNums > LINE_NUMS_THREE ? UI_SELECTOR_DIALOG_PHONE_H2 :
523             (lineNums > LINE_NUMS_ZERO ? UI_SELECTOR_DIALOG_PHONE_H1 : position.height));
524     }
525 }
526 
GetDialogPositionAndSize(DialogType type,DialogPosition & position,int lineNums) const527 void SystemDialogScheduler::GetDialogPositionAndSize(DialogType type, DialogPosition &position, int lineNums) const
528 {
529     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
530     InitDialogPosition(type, position);
531 
532     auto display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
533     if (display == nullptr) {
534         TAG_LOGW(AAFwkTag::DIALOG, "GetDefaultDisplay fail, try again");
535         display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
536     }
537     if (display != nullptr) {
538         TAG_LOGI(AAFwkTag::DIALOG, "display width:%{public}d, height:%{public}d", display->GetWidth(),
539             display->GetHeight());
540         if (display->GetWidth() < UI_WIDTH_780DP) {
541             TAG_LOGI(AAFwkTag::DIALOG, "show dialog narrow");
542             position.width = position.width_narrow;
543             position.height = position.height_narrow;
544         }
545 
546         if (type == DialogType::DIALOG_SELECTOR) {
547             DialogPositionAdaptive(position, lineNums);
548         }
549         switch (position.align) {
550             case DialogAlign::CENTER:
551                 if (position.wideScreen) {
552                     position.offsetX = (display->GetWidth() - position.width) / UI_HALF;
553                     position.offsetY = (display->GetHeight() - position.height) / UI_HALF;
554                 } else {
555                     position.window_width = position.window_width / UI_HALF;
556                     position.window_height = position.window_height / UI_HALF;
557                     position.offsetX = LINE_NUMS_ZERO;
558                     position.offsetY = LINE_NUMS_ZERO;
559                 }
560                 break;
561             case DialogAlign::BOTTOM:
562                 position.width = display->GetWidth() * WIDTH_MULTIPLE;
563                 position.height = display->GetHeight() * HEIGHT_MULTIPLE;
564                 position.offsetX = display->GetWidth() * SETX_WIDTH_MULTIPLE;
565                 position.offsetY = display->GetHeight() - position.height - UI_DEFAULT_BUTTOM_CLIP;
566                 break;
567             default:
568                 position.offsetX = (display->GetWidth() - position.width) / UI_HALF;
569                 position.offsetY = (display->GetHeight() - position.height - UI_DEFAULT_BUTTOM_CLIP) / UI_HALF;
570                 break;
571         }
572     } else {
573         TAG_LOGW(AAFwkTag::DIALOG, "get display fail, use default wide");
574         if (type == DialogType::DIALOG_SELECTOR) {
575             DialogPositionAdaptive(position, lineNums);
576         }
577         position.offsetX = (UI_DEFAULT_WIDTH - position.width) / UI_HALF;
578         position.offsetY = UI_DEFAULT_HEIGHT - position.height - UI_DEFAULT_BUTTOM_CLIP;
579     }
580 }
581 
GetAssertFaultDialogWant(Want & want)582 bool SystemDialogScheduler::GetAssertFaultDialogWant(Want &want)
583 {
584     auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
585     if (bundleMgrHelper == nullptr) {
586         TAG_LOGE(AAFwkTag::DIALOG, "Failed get bms");
587         return false;
588     }
589 
590     std::string bundleName;
591     auto callingUid = IPCSkeleton::GetCallingUid();
592     if (IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callingUid, bundleName)) != ERR_OK) {
593         TAG_LOGE(AAFwkTag::DIALOG, "VerifyPermission failed to get bundle name by uid");
594         return false;
595     }
596 
597     want.SetElementName(BUNDLE_NAME_DIALOG, ABILITY_NAME_ASSERT_FAULT_DIALOG);
598     want.SetParam(BUNDLE_NAME, bundleName);
599     want.SetParam(UIEXTENSION_TYPE_KEY, UIEXTENSION_SYS_COMMON_UI);
600     return true;
601 }
602 
GetSwitchUserDialogWant()603 Want SystemDialogScheduler::GetSwitchUserDialogWant()
604 {
605     TAG_LOGD(AAFwkTag::DIALOG, "start");
606     AAFwk::Want dialogWant;
607     dialogWant.SetElementName(BUNDLE_NAME_DIALOG, ABILITY_NAME_FREEZE_DIALOG);
608 
609     return dialogWant;
610 }
611 }  // namespace AAFwk
612 }  // namespace OHOS
613