1 /*
2 * Copyright (c) 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
16 #include "utils/window_options_utils.h"
17
18 #include "ability_record.h"
19 #include "app_utils.h"
20
21 namespace OHOS {
22 namespace AAFwk {
SetWindowPositionAndSize(Want& want, const sptr<IRemoteObject>& callerToken, const StartOptions& startOptions)23 void WindowOptionsUtils::SetWindowPositionAndSize(Want& want,
24 const sptr<IRemoteObject>& callerToken, const StartOptions& startOptions)
25 {
26 if (!AppUtils::GetInstance().IsStartOptionsWithAnimation()) {
27 return;
28 }
29 if (startOptions.windowLeftUsed_) {
30 want.SetParam(Want::PARAM_RESV_WINDOW_LEFT, startOptions.GetWindowLeft());
31 }
32 if (startOptions.windowTopUsed_) {
33 want.SetParam(Want::PARAM_RESV_WINDOW_TOP, startOptions.GetWindowTop());
34 }
35 if (startOptions.windowWidthUsed_) {
36 want.SetParam(Want::PARAM_RESV_WINDOW_WIDTH, startOptions.GetWindowWidth());
37 }
38 if (startOptions.windowHeightUsed_) {
39 want.SetParam(Want::PARAM_RESV_WINDOW_HEIGHT, startOptions.GetWindowHeight());
40 }
41 bool withAnimation = startOptions.GetWithAnimation();
42 auto abilityRecord = Token::GetAbilityRecordByToken(callerToken);
43 if (!withAnimation && abilityRecord != nullptr &&
44 abilityRecord->GetAbilityInfo().bundleName == want.GetBundle()) {
45 want.SetParam(Want::PARAM_RESV_WITH_ANIMATION, withAnimation);
46 }
47 }
48
WindowModeMap(int32_t windowMode)49 std::pair<bool, AppExecFwk::SupportWindowMode> WindowOptionsUtils::WindowModeMap(int32_t windowMode)
50 {
51 std::pair<bool, AppExecFwk::SupportWindowMode> result(false, AppExecFwk::SupportWindowMode::FULLSCREEN);
52
53 if (windowMode == MULTI_WINDOW_DISPLAY_FULLSCREEN) {
54 result.first = true;
55 result.second = AppExecFwk::SupportWindowMode::FULLSCREEN;
56 } else if (windowMode == MULTI_WINDOW_DISPLAY_PRIMARY) {
57 result.first = true;
58 result.second = AppExecFwk::SupportWindowMode::SPLIT;
59 } else if (windowMode == MULTI_WINDOW_DISPLAY_SECONDARY) {
60 result.first = true;
61 result.second = AppExecFwk::SupportWindowMode::SPLIT;
62 } else if (windowMode == MULTI_WINDOW_DISPLAY_FLOATING) {
63 result.first = true;
64 result.second = AppExecFwk::SupportWindowMode::FLOATING;
65 }
66 return result;
67 }
68
is_number(const std::string& str)69 bool is_number(const std::string& str)
70 {
71 return std::all_of(str.begin(), str.end(), [](char c) { return std::isdigit(c); });
72 }
73
UpdateStartOptionsToSetDisplayID(StartOptions &startOptions, const sptr<IRemoteObject> &callerToken)74 void WindowOptionsUtils::UpdateStartOptionsToSetDisplayID(StartOptions &startOptions,
75 const sptr<IRemoteObject> &callerToken)
76 {
77 if (!AppUtils::GetInstance().IsStartOptionsWithAnimation()) {
78 return;
79 }
80 sptr<IRemoteObject> caller;
81 if (startOptions.GetDisplayID() == 0) {
82 if (callerToken != nullptr) {
83 caller = callerToken;
84 }
85 std::shared_ptr<AbilityRecord> abilityRecord = Token::GetAbilityRecordByToken(caller);
86 if (abilityRecord != nullptr) {
87 std::string displayId = abilityRecord->GetWant().GetParams().GetStringParam(Want::PARAM_RESV_DISPLAY_ID);
88 if (displayId.empty() || !is_number(displayId)) {
89 displayId = "0";
90 }
91 startOptions.SetDisplayID(std::stoi(displayId));
92 }
93 }
94 TAG_LOGD(AAFwkTag::ABILITYMGR, "start ability displayID is %{public}d", startOptions.GetDisplayID());
95 }
96
UpdateWantToSetDisplayID(Want &want, const sptr<IRemoteObject> &callerToken)97 void WindowOptionsUtils::UpdateWantToSetDisplayID(Want &want,
98 const sptr<IRemoteObject> &callerToken)
99 {
100 if (!AppUtils::GetInstance().IsStartOptionsWithAnimation()) {
101 return;
102 }
103 sptr<IRemoteObject> caller;
104 OHOS::AAFwk::WantParams params = want.GetParams();
105 if (callerToken != nullptr) {
106 caller = callerToken;
107 } else {
108 params.SetParam(Want::PARAM_RESV_DISPLAY_ID, AAFwk::String::Box("0"));
109 want.SetParams(params);
110 TAG_LOGD(AAFwkTag::ABILITYMGR, "start ability displayID is 0");
111 return;
112 }
113 std::shared_ptr<AbilityRecord> abilityRecord = Token::GetAbilityRecordByToken(caller);
114 if (abilityRecord != nullptr) {
115 std::string displayId = abilityRecord->GetWant().GetParams().GetStringParam(Want::PARAM_RESV_DISPLAY_ID);
116 params.SetParam(Want::PARAM_RESV_DISPLAY_ID, AAFwk::String::Box(displayId));
117 want.SetParams(params);
118 } else {
119 params.SetParam(Want::PARAM_RESV_DISPLAY_ID, AAFwk::String::Box("0"));
120 want.SetParams(params);
121 }
122 TAG_LOGD(AAFwkTag::ABILITYMGR, "start ability displayID is %{public}s",
123 want.GetParams().GetStringParam(Want::PARAM_RESV_DISPLAY_ID).c_str());
124 }
125 } // namespace AAFwk
126 } // namespace OHOS
127