1 
2 /*
3  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "extra_params.h"
17 #include "string_ex.h"
18 
19 namespace OHOS {
20 namespace AppExecFwk {
21 /**
22  * @brief A default constructor used to create an empty {@code ExtraParams} instance.
23  * @param none
24  */
ExtraParams()25 ExtraParams::ExtraParams()
26 {}
27 /**
28  * @brief A copy constructor used to create an empty {@code ExtraParams} instance.
29  * @param other indicates object instance.
30  */
ExtraParams(const ExtraParams & other)31 ExtraParams::ExtraParams(const ExtraParams &other)
32 {
33     devType_ = other.devType_;
34     targetBundleName_ = other.targetBundleName_;
35     description_ = other.description_;
36     jsonParams_ = other.jsonParams_;
37 }
38 /**
39  * @brief Overloading assignment operators to create the same object instance.
40  * @param other Indicates object instance.
41  */
operator =(const ExtraParams & other)42 ExtraParams &ExtraParams::operator=(const ExtraParams &other)
43 {
44     if (this != &other) {
45         devType_ = other.devType_;
46         targetBundleName_ = other.targetBundleName_;
47         description_ = other.description_;
48         jsonParams_ = other.jsonParams_;
49     }
50     return *this;
51 }
52 /**
53  * @brief Judge whether the parameter of extraparam instance is equal to that of other instance parameter
54  * @param other Indicates object instance.
55  * @return returns true the parameter of extraparam instance is equal to that of other instance parameter
56  * otherwise false
57  */
operator ==(const ExtraParams & other) const58 bool ExtraParams::operator==(const ExtraParams &other) const
59 {
60     if (targetBundleName_ == other.targetBundleName_ && description_ == other.description_ &&
61         jsonParams_ == other.jsonParams_) {
62         if (devType_.size() != other.devType_.size()) {
63             return false;
64         } else if (devType_.size() > 0 && other.devType_.size() > 0 && devType_.size() == other.devType_.size()) {
65             for (std::vector<string>::const_iterator it1 = devType_.cbegin(); it1 != devType_.cend(); it1++) {
66                 std::vector<string>::const_iterator it2 =
67                     std::find(other.devType_.cbegin(), other.devType_.cend(), *it1);
68                 if (it2 == other.devType_.cend()) {
69                     return false;
70                 }
71             }
72             return true;
73         }
74         return true;
75     }
76     return false;
77 }
78 /**
79  * @brief A constructor used to create an {@code ExtraParams} instance based on the input parameters{@code devType},
80  * {@code targetBundleName}, and {@code description}.
81  *
82  * @param devType Indicates the type of devices to be matched. This parameter can be any combination of
83  * {@link #DEVICETYPE_SMART_PHONE}, {@link #DEVICETYPE_SMART_PAD}, {@link #DEVICETYPE_SMART_WATCH}, and
84  * {@link #DEVICETYPE_SMART_TV}.
85  *
86  * @param targetBundleName Indicates the bundle name of the target application where the ability will be migrated.
87  *
88  * @param description Indicates the description used for device filtering.
89  *
90  * @param jsonParams Indicates the custom JSON parameters to be used as filter conditions.
91  *
92  * @return none
93  */
ExtraParams(const std::vector<string> & devType,const string & targetBundleName,const string & description,const string & jsonParams)94 ExtraParams::ExtraParams(const std::vector<string> &devType, const string &targetBundleName, const string &description,
95     const string &jsonParams)
96 {
97     devType_ = devType;
98     targetBundleName_ = targetBundleName;
99     description_ = description;
100     jsonParams_ = jsonParams;
101 }
102 
103 /**
104  * @brief A destructor release an empty {@code ExtraParams} instance.
105  * @param none
106  */
~ExtraParams()107 ExtraParams::~ExtraParams()
108 {}
109 
110 /**
111  * @brief Sets the list of device types.
112  *
113  * @param devType Indicates the type of devices to be matched. This parameter can be any combination of
114  * {@link #DEVICETYPE_SMART_PHONE}, {@link #DEVICETYPE_SMART_PAD}, {@link #DEVICETYPE_SMART_WATCH}, and
115  * {@link #DEVICETYPE_SMART_TV}.
116  *
117  * @return none
118  */
SetDevType(const std::vector<string> & devType)119 void ExtraParams::SetDevType(const std::vector<string> &devType)
120 {
121     devType_ = devType;
122 }
123 
124 /**
125  * @brief Obtains the list of device types.
126  *
127  * @param none
128  *
129  * @return Returns the list of device types.
130  */
GetDevType() const131 std::vector<string> ExtraParams::GetDevType() const
132 {
133     return devType_;
134 }
135 
136 /**
137  * @brief Sets the bundle name of the target application where ability will be migrated.
138  *
139  * @param targetBundleName Indicates the bundle name of the target application to set.
140  *
141  * @return none
142  */
SetTargetBundleName(const string & targetBundleName)143 void ExtraParams::SetTargetBundleName(const string &targetBundleName)
144 {
145     targetBundleName_ = targetBundleName;
146 }
147 
148 /**
149  * @brief Obtains the bundle name of the target application where the ability will be migrated.
150  *
151  * @return Returns the bundle name of the target application.
152  */
GetTargetBundleName() const153 string ExtraParams::GetTargetBundleName() const
154 {
155     return targetBundleName_;
156 }
157 
158 /**
159  * @brief Sets the description used for device filtering.
160  *
161  * @param jsonParams Indicates the device description to set.
162  *
163  * @return none
164  */
SetJsonParams(const string & jsonParams)165 void ExtraParams::SetJsonParams(const string &jsonParams)
166 {
167     jsonParams_ = jsonParams;
168 }
169 
170 /**
171  * @brief Obtains the custom JSON parameters used as filter conditions.
172  *
173  * @param none
174  *
175  * @return Returns the custom JSON parameters.
176  */
GetJsonParams() const177 string ExtraParams::GetJsonParams() const
178 {
179     return jsonParams_;
180 }
181 
182 /**
183  * @brief Sets the custom JSON parameters to be used as filter conditions.
184  *
185  * @param description Indicates the custom JSON parameters to set.
186  *
187  */
SetDescription(const string & description)188 void ExtraParams::SetDescription(const string &description)
189 {
190     description_ = description;
191 }
192 
193 /**
194  * @brief Obtains the description used for device filtering.
195  *
196  * @param none
197  *
198  * @return Returns the description used for device filtering.
199  */
GetDescription() const200 string ExtraParams::GetDescription() const
201 {
202     return description_;
203 }
204 
205 /**
206  * @brief Marshals this {@code ExtraParams} object into a {@link ohos.utils.Parcel} object.
207  *
208  * @param parcel Indicates the {@code Parcel} object for marshalling.
209  *
210  * @return Returns {@code true} if the marshalling is successful; returns {@code false} otherwise.
211  */
Marshalling(Parcel & parcel) const212 bool ExtraParams::Marshalling(Parcel &parcel) const
213 {
214     bool ret = true;
215     // devType
216     bool ret1 = parcel.WriteStringVector(devType_);
217 
218     // targetBundleName
219     bool ret2 = parcel.WriteString16(Str8ToStr16(targetBundleName_));
220 
221     // description
222     bool ret3 = parcel.WriteString16(Str8ToStr16(description_));
223 
224     // jsonParams
225     bool ret4 = parcel.WriteString16(Str8ToStr16(jsonParams_));
226 
227     ret = (ret1 && ret2 && ret3 && ret4) ? true : false;
228     return ret;
229 }
230 
231 /**
232  * @brief Unmarshals this {@code ExtraParams} object from a {@link ohos.utils.Parcel} object.
233  *
234  * @param parcel Indicates the {@code Parcel} object for unmarshalling.
235  *
236  * @return Returns {@code true} if the unmarshalling is successful; returns {@code false} otherwise.
237  */
Unmarshalling(Parcel & parcel)238 ExtraParams *ExtraParams::Unmarshalling(Parcel &parcel)
239 {
240     // devType.
241     std::vector<string> devtype;
242     parcel.ReadStringVector(&devtype);
243 
244     // targetBundleName
245     string targetBundleName = Str16ToStr8(parcel.ReadString16());
246 
247     // description
248     string description = Str16ToStr8(parcel.ReadString16());
249 
250     // jsonParams
251     string jsonParams = Str16ToStr8(parcel.ReadString16());
252 
253     ExtraParams *extraParams = new (std::nothrow) ExtraParams(devtype, targetBundleName, description, jsonParams);
254 
255     return extraParams;
256 }
257 }  // namespace AppExecFwk
258 }  // namespace OHOS