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_utils.h"
20#include "application_util.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
27namespace OHOS {
28namespace AAFwk {
29const int32_t UI_SELECTOR_DIALOG_WIDTH = 328 * 2;
30const int32_t UI_SELECTOR_DIALOG_HEIGHT = 350 * 2;
31const int32_t UI_SELECTOR_DIALOG_HEIGHT_NARROW = 350 * 2;
32const int32_t UI_SELECTOR_DIALOG_WIDTH_NARROW = 328 * 2;
33const int32_t UI_SELECTOR_DIALOG_PHONE_H1 = 240 * 2;
34const int32_t UI_SELECTOR_DIALOG_PHONE_H2 = 340 * 2;
35const int32_t UI_SELECTOR_DIALOG_PHONE_H3 = 350 * 2;
36const int32_t UI_SELECTOR_DIALOG_PC_H0 = 1;
37const int32_t UI_SELECTOR_DIALOG_PC_H2 = (64 * 2 + 56 + 48 + 54 + 64 + 48 + 2) * 2;
38const int32_t UI_SELECTOR_DIALOG_PC_H3 = (64 * 3 + 56 + 48 + 54 + 64 + 48 + 2) * 2;
39const int32_t UI_SELECTOR_DIALOG_PC_H4 = (64 * 4 + 56 + 48 + 54 + 64 + 48 + 2) * 2;
40const int32_t UI_SELECTOR_DIALOG_PC_H5 = (64 * 4 + 56 + 48 + 54 + 64 + 48 + 58 + 2) * 2;
41
42const int32_t UI_SELECTOR_PORTRAIT_PHONE_H1 = 280;
43const int32_t UI_SELECTOR_PORTRAIT_PHONE_H2 = 400;
44const int32_t UI_SELECTOR_PORTRAIT_PHONE_H3 = 410;
45const int32_t UI_SELECTOR_LANDSCAPE_SIGNAL_BAR = 24;
46const int32_t UI_SELECTOR_LANDSCAPE_HEIGHT = 350;
47const int32_t UI_SELECTOR_LANDSCAPE_HEIGHT_NARROW = 350;
48const int32_t UI_SELECTOR_LANDSCAPE_PHONE_H1 = 280;
49const int32_t UI_SELECTOR_LANDSCAPE_PHONE_H2 = 400;
50const int32_t UI_SELECTOR_LANDSCAPE_PHONE_H3 = 410;
51const int32_t UI_SELECTOR_LANDSCAPE_COUNT_THREE = 3;
52const int32_t UI_SELECTOR_LANDSCAPE_COUNT_FOUR = 4;
53const float UI_SELECTOR_LANDSCAPE_GRILLE_LARGE = 0.107692;
54const float UI_SELECTOR_LANDSCAPE_GRILLE_SAMLL = 0.015385;
55const float UI_SELECTOR_LANDSCAPE_MAX_RATIO = 0.9;
56const float UI_SELECTOR_PORTRAIT_WIDTH_RATIO = 0.8;
57const float UI_SELECTOR_PORTRAIT_WIDTH_EDGE_RATIO = 0.1;
58const float UI_SELECTOR_PORTRAIT_HEIGHT_RATIO = 0.98;
59
60const int32_t UI_TIPS_DIALOG_WIDTH = 328 * 2;
61const int32_t UI_TIPS_DIALOG_HEIGHT = 135 * 2;
62const int32_t UI_TIPS_DIALOG_HEIGHT_NARROW = 135 * 2;
63const int32_t UI_TIPS_DIALOG_WIDTH_NARROW = 328 * 2;
64
65const int32_t UI_JUMP_INTERCEPTOR_DIALOG_WIDTH = 328 * 2;
66const int32_t UI_JUMP_INTERCEPTOR_DIALOG_HEIGHT = 135 * 2;
67const int32_t UI_JUMP_INTERCEPTOR_DIALOG_HEIGHT_NARROW = 135 * 2;
68const int32_t UI_JUMP_INTERCEPTOR_DIALOG_WIDTH_NARROW = 328 * 2;
69
70const int32_t UI_ANR_DIALOG_WIDTH = 328 * 2;
71const int32_t UI_ANR_DIALOG_HEIGHT = 192 * 2;
72const std::string APP_NAME = "appName";
73const std::string IS_DEFAULT_SELECTOR = "isDefaultSelector";
74const std::string OFF_SET_X = "offsetX";
75const std::string OFF_SET_Y = "offsetY";
76const std::string WIDTH = "width";
77const std::string HEIGHT = "height";
78const std::string MODEL_FLAG = "modelFlag";
79const std::string ACTION = "action";
80const std::string OVERSIZE_HEIGHT = "oversizeHeight";
81
82const int32_t UI_HALF = 2;
83const int32_t UI_DEFAULT_BUTTOM_CLIP = 100;
84const int32_t UI_WIDTH_780DP = 1560;
85const int32_t UI_DEFAULT_WIDTH = 2560;
86const int32_t UI_DEFAULT_HEIGHT = 1600;
87
88const std::string STR_PHONE = "phone";
89const std::string STR_DEFAULT = "default";
90const std::string DIALOG_NAME_ANR = "dialog_anr_service";
91const std::string DIALOG_NAME_TIPS = "dialog_tips_service";
92const std::string DIALOG_SELECTOR_NAME = "dialog_selector_service";
93const std::string DIALOG_JUMP_INTERCEPTOR_NAME = "dialog_jump_interceptor_service";
94
95const std::string BUNDLE_NAME = "bundleName";
96const std::string BUNDLE_NAME_DIALOG = "com.ohos.amsdialog";
97const std::string DIALOG_PARAMS = "params";
98const std::string DIALOG_POSITION = "position";
99const std::string VERTICAL_SCREEN_DIALOG_POSITION = "landscapeScreen";
100const std::string ABILITY_NAME_FREEZE_DIALOG = "SwitchUserDialog";
101const std::string ABILITY_NAME_ASSERT_FAULT_DIALOG = "AssertFaultDialog";
102const std::string ABILITY_NAME_TIPS_DIALOG = "TipsDialog";
103const std::string ABILITY_NAME_SELECTOR_DIALOG = "SelectorDialog";
104const std::string ABILITY_NAME_APPGALLERY_SELECTOR_DIALOG = "AppSelectorExtensionAbility";
105const std::string UIEXTENSION_TYPE_KEY = "ability.want.params.uiExtensionType";
106const std::string UIEXTENSION_SYS_COMMON_UI = "sys/commonUI";
107const std::string CALLER_TOKEN = "callerToken";
108const std::string ABILITY_NAME_JUMP_INTERCEPTOR_DIALOG = "JumpInterceptorDialog";
109const std::string TYPE_ONLY_MATCH_WILDCARD = "reserved/wildcard";
110const std::string ORIENTATION = "orientation";
111
112const int32_t LINE_NUMS_ZERO = 0;
113const int32_t LINE_NUMS_TWO = 2;
114const int32_t LINE_NUMS_THREE = 3;
115const int32_t LINE_NUMS_FOUR = 4;
116const int32_t LINE_NUMS_EIGHT = 8;
117
118const float WIDTH_MULTIPLE = 0.8;
119const float HEIGHT_MULTIPLE = 0.3;
120const float SETX_WIDTH_MULTIPLE = 0.1;
121
122Want 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
145Want 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
166void 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
183void 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: %{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
214void 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
231void 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:%{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
261void 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
306int 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
323const 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
352int 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
367const 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
405int 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 (Rosen::SceneBoardJudgement::IsSceneBoardEnabled() && isCallerStageBasedModel) {
425        auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
426        if (bundleMgrHelper == nullptr) {
427            TAG_LOGE(AAFwkTag::DIALOG, "bundleMgrHelper null");
428            return INNER_ERR;
429        }
430        std::string bundleName;
431        if (!IN_PROCESS_CALL(bundleMgrHelper->QueryAppGalleryBundleName(bundleName))) {
432            TAG_LOGE(AAFwkTag::DIALOG, "QueryAppGalleryBundleName failed");
433            return INNER_ERR;
434        }
435        targetWant.SetElementName(bundleName, ABILITY_NAME_APPGALLERY_SELECTOR_DIALOG);
436        targetWant.SetParam(UIEXTENSION_TYPE_KEY, UIEXTENSION_SYS_COMMON_UI);
437        targetWant.SetParam("isCreateAppGallerySelector", true);
438    }
439    return ERR_OK;
440}
441
442const std::string SystemDialogScheduler::GetDialogPositionParams(const DialogPosition position) const
443{
444    nlohmann::json dialogPositionData;
445    dialogPositionData[OFF_SET_X] = position.offsetX;
446    dialogPositionData[OFF_SET_Y] = position.offsetY;
447    dialogPositionData[WIDTH] = position.width;
448    dialogPositionData[HEIGHT] = position.height;
449    dialogPositionData[OVERSIZE_HEIGHT] = position.oversizeHeight;
450    return dialogPositionData.dump();
451}
452
453void SystemDialogScheduler::InitDialogPosition(DialogType type, DialogPosition &position) const
454{
455    position.wideScreen = !AppUtils::GetInstance().IsSelectorDialogDefaultPossion();
456    position.align = AppUtils::GetInstance().IsSelectorDialogDefaultPossion() ?
457        DialogAlign::BOTTOM : DialogAlign::CENTER;
458    auto display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
459
460    switch (type) {
461        case DialogType::DIALOG_ANR:
462            if (position.wideScreen) {
463                position.width = UI_ANR_DIALOG_WIDTH;
464                position.height = UI_ANR_DIALOG_HEIGHT;
465                position.width_narrow = UI_ANR_DIALOG_WIDTH;
466                position.height_narrow = UI_ANR_DIALOG_HEIGHT;
467                position.align = DialogAlign::CENTER;
468            } else {
469                position.width =  display->GetWidth();
470                position.height = display->GetHeight();
471                position.width_narrow =  display->GetWidth();
472                position.height_narrow = display->GetHeight();
473                position.window_width = UI_ANR_DIALOG_WIDTH;
474                position.window_height = UI_ANR_DIALOG_HEIGHT;
475                position.align = DialogAlign::CENTER;
476            }
477            break;
478        case DialogType::DIALOG_SELECTOR:
479            position.width = UI_SELECTOR_DIALOG_WIDTH;
480            position.height = UI_SELECTOR_DIALOG_HEIGHT;
481            position.width_narrow = UI_SELECTOR_DIALOG_WIDTH_NARROW;
482            position.height_narrow = UI_SELECTOR_DIALOG_HEIGHT_NARROW;
483            break;
484        case DialogType::DIALOG_TIPS:
485            position.width = UI_TIPS_DIALOG_WIDTH;
486            position.height = UI_TIPS_DIALOG_HEIGHT;
487            position.width_narrow = UI_TIPS_DIALOG_WIDTH_NARROW;
488            position.height_narrow = UI_TIPS_DIALOG_HEIGHT_NARROW;
489            break;
490        case DialogType::DIALOG_JUMP_INTERCEPTOR:
491            position.width = UI_JUMP_INTERCEPTOR_DIALOG_WIDTH;
492            position.height = UI_JUMP_INTERCEPTOR_DIALOG_HEIGHT;
493            position.width_narrow = UI_JUMP_INTERCEPTOR_DIALOG_WIDTH_NARROW;
494            position.height_narrow = UI_JUMP_INTERCEPTOR_DIALOG_HEIGHT_NARROW;
495            break;
496        default:
497            position.width = UI_DEFAULT_WIDTH;
498            position.height = UI_DEFAULT_HEIGHT;
499            position.width_narrow = UI_DEFAULT_WIDTH;
500            position.height_narrow = UI_DEFAULT_HEIGHT;
501            break;
502    }
503}
504
505void SystemDialogScheduler::DialogPositionAdaptive(DialogPosition &position, int lineNums) const
506{
507    if (position.wideScreen) {
508        if (lineNums <= LINE_NUMS_TWO) {
509            position.height = UI_SELECTOR_DIALOG_PC_H2;
510        } else if (lineNums == LINE_NUMS_THREE) {
511            position.height = UI_SELECTOR_DIALOG_PC_H3;
512        } else if (lineNums == LINE_NUMS_FOUR) {
513            position.height = UI_SELECTOR_DIALOG_PC_H4;
514        } else if (lineNums > LINE_NUMS_FOUR) {
515            position.height = UI_SELECTOR_DIALOG_PC_H5;
516        } else {
517            position.height = UI_SELECTOR_DIALOG_PC_H0;
518        }
519    } else {
520        position.height = (lineNums > LINE_NUMS_EIGHT) ? UI_SELECTOR_DIALOG_PHONE_H3 :
521            (lineNums > LINE_NUMS_THREE ? UI_SELECTOR_DIALOG_PHONE_H2 :
522            (lineNums > LINE_NUMS_ZERO ? UI_SELECTOR_DIALOG_PHONE_H1 : position.height));
523    }
524}
525
526void SystemDialogScheduler::GetDialogPositionAndSize(DialogType type, DialogPosition &position, int lineNums) const
527{
528    HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
529    InitDialogPosition(type, position);
530
531    auto display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
532    if (display == nullptr) {
533        TAG_LOGW(AAFwkTag::DIALOG, "GetDefaultDisplay fail, try again");
534        display = Rosen::DisplayManager::GetInstance().GetDefaultDisplay();
535    }
536    if (display != nullptr) {
537        TAG_LOGI(AAFwkTag::DIALOG, "display width:%{public}d, height:%{public}d", display->GetWidth(),
538            display->GetHeight());
539        if (display->GetWidth() < UI_WIDTH_780DP) {
540            TAG_LOGI(AAFwkTag::DIALOG, "show dialog narrow");
541            position.width = position.width_narrow;
542            position.height = position.height_narrow;
543        }
544
545        if (type == DialogType::DIALOG_SELECTOR) {
546            DialogPositionAdaptive(position, lineNums);
547        }
548        switch (position.align) {
549            case DialogAlign::CENTER:
550                if (position.wideScreen) {
551                    position.offsetX = (display->GetWidth() - position.width) / UI_HALF;
552                    position.offsetY = (display->GetHeight() - position.height) / UI_HALF;
553                } else {
554                    position.window_width = position.window_width / UI_HALF;
555                    position.window_height = position.window_height / UI_HALF;
556                    position.offsetX = LINE_NUMS_ZERO;
557                    position.offsetY = LINE_NUMS_ZERO;
558                }
559                break;
560            case DialogAlign::BOTTOM:
561                position.width = display->GetWidth() * WIDTH_MULTIPLE;
562                position.height = display->GetHeight() * HEIGHT_MULTIPLE;
563                position.offsetX = display->GetWidth() * SETX_WIDTH_MULTIPLE;
564                position.offsetY = display->GetHeight() - position.height - UI_DEFAULT_BUTTOM_CLIP;
565                break;
566            default:
567                position.offsetX = (display->GetWidth() - position.width) / UI_HALF;
568                position.offsetY = (display->GetHeight() - position.height - UI_DEFAULT_BUTTOM_CLIP) / UI_HALF;
569                break;
570        }
571    } else {
572        TAG_LOGW(AAFwkTag::DIALOG, "fail, use default wide");
573        if (type == DialogType::DIALOG_SELECTOR) {
574            DialogPositionAdaptive(position, lineNums);
575        }
576        position.offsetX = (UI_DEFAULT_WIDTH - position.width) / UI_HALF;
577        position.offsetY = UI_DEFAULT_HEIGHT - position.height - UI_DEFAULT_BUTTOM_CLIP;
578    }
579}
580
581bool SystemDialogScheduler::GetAssertFaultDialogWant(Want &want)
582{
583    auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
584    if (bundleMgrHelper == nullptr) {
585        TAG_LOGE(AAFwkTag::DIALOG, "Failed get bms");
586        return false;
587    }
588
589    std::string bundleName;
590    auto callingUid = IPCSkeleton::GetCallingUid();
591    if (IN_PROCESS_CALL(bundleMgrHelper->GetNameForUid(callingUid, bundleName)) != ERR_OK) {
592        TAG_LOGE(AAFwkTag::DIALOG, "VerifyPermission get bundle name failed");
593        return false;
594    }
595
596    want.SetElementName(BUNDLE_NAME_DIALOG, ABILITY_NAME_ASSERT_FAULT_DIALOG);
597    want.SetParam(BUNDLE_NAME, bundleName);
598    want.SetParam(UIEXTENSION_TYPE_KEY, UIEXTENSION_SYS_COMMON_UI);
599    return true;
600}
601
602Want SystemDialogScheduler::GetSwitchUserDialogWant()
603{
604    TAG_LOGD(AAFwkTag::DIALOG, "start");
605    AAFwk::Want dialogWant;
606    dialogWant.SetElementName(BUNDLE_NAME_DIALOG, ABILITY_NAME_FREEZE_DIALOG);
607
608    return dialogWant;
609}
610}  // namespace AAFwk
611}  // namespace OHOS
612