1 /* 2 * Copyright (c) 2021 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 "ability_start_setting.h" 17 #include <cstring> 18 #include "string_ex.h" 19 20 using namespace OHOS; 21 22 namespace OHOS { 23 namespace AAFwk { 24 25 const std::string AbilityStartSetting::BOUNDS_KEY = "bounds"; 26 const std::string AbilityStartSetting::WINDOW_DISPLAY_ID_KEY = "windowId"; 27 const std::string AbilityStartSetting::WINDOW_MODE_KEY = "windowMode"; 28 29 /** 30 * @brief Construct copy function. 31 * @param other indicates instance of abilitystartsetting object 32 * @return none. 33 */ AbilityStartSetting(const AbilityStartSetting & other)34AbilityStartSetting::AbilityStartSetting(const AbilityStartSetting &other) 35 { 36 abilityStarKey_.clear(); 37 abilityStarKey_ = other.abilityStarKey_; 38 } 39 /** 40 * @brief Overload assignment operation. 41 * @param other indicates instance of abilitystartsetting object. 42 * @return Returns current instance of abilitystartsetting object. 43 */ operator =(const AbilityStartSetting & other)44AbilityStartSetting &AbilityStartSetting::operator=(const AbilityStartSetting &other) 45 { 46 if (this != &other) { 47 abilityStarKey_.clear(); 48 abilityStarKey_ = other.abilityStarKey_; 49 } 50 return *this; 51 } 52 /** 53 * @brief Inner function to create AbilityStartSetting 54 * 55 * @return Returns the shared_ptr of AbilityStartSetting object. 56 */ AbilityStartSettingCreator()57std::shared_ptr<AbilityStartSetting> AbilityStartSettingCreator() 58 { 59 std::shared_ptr<AbilityStartSetting> abilityStartSetting {new (std::nothrow) AbilityStartSetting()}; 60 return abilityStartSetting; 61 } 62 63 /** 64 * @brief Obtains an empty AbilityStartSetting object. 65 * 66 * @return Returns the btains an empty AbilityStartSetting object. 67 */ GetEmptySetting()68std::shared_ptr<AbilityStartSetting> AbilityStartSetting::GetEmptySetting() 69 { 70 return AbilityStartSettingCreator(); 71 } 72 73 /** 74 * @brief Obtains the names of all the attributes that have been added to this AbilityStartSetting object. 75 * 76 * @return Returns the set of attribute names included in this AbilityStartSetting object. 77 */ GetPropertiesKey()78std::set<std::string> AbilityStartSetting::GetPropertiesKey() 79 { 80 std::set<std::string> abilityStartSet; 81 abilityStartSet.clear(); 82 83 for (auto it : abilityStarKey_) { 84 abilityStartSet.emplace(it.first); 85 } 86 return abilityStartSet; 87 } 88 89 /** 90 * @brief Checks whether this AbilityStartSetting object is empty. 91 * 92 * @return Returns true if this AbilityStartSetting object is empty and animatorOption is null; returns false otherwise. 93 */ IsEmpty()94bool AbilityStartSetting::IsEmpty() 95 { 96 return (abilityStarKey_.size() == 0); 97 } 98 99 /** 100 * @brief Sets the names of all the attributes of the AbilityStartSetting object. 101 * 102 * @param key Indicates the name of the key. 103 * @param value The window display mode of the values. 104 */ AddProperty(const std::string & key,const std::string & value)105void AbilityStartSetting::AddProperty(const std::string &key, const std::string &value) 106 { 107 abilityStarKey_[key] = value; 108 } 109 110 /** 111 * @brief Gets the name of the attributes of the AbilityStartSetting object. 112 * 113 * @param key Indicates the name of the key. 114 * @return Returns value Indicates the value of the attributes of the AbilityStartSetting object 115 */ GetProperty(const std::string & key)116std::string AbilityStartSetting::GetProperty(const std::string &key) 117 { 118 auto it = abilityStarKey_.find(key); 119 if (it == abilityStarKey_.end()) { 120 return std::string(); 121 } 122 return abilityStarKey_[key]; 123 } 124 125 /** 126 * @brief Write the data of AbilityStartSetting to the file stream 127 * @param parcel indicates write the data of AbilityStartSetting to the file stream through parcel 128 * @return bool 129 */ Marshalling(Parcel & parcel) const130bool AbilityStartSetting::Marshalling(Parcel &parcel) const 131 { 132 size_t size = abilityStarKey_.size(); 133 134 // 1. Number of key value pairs written 135 parcel.WriteUint32((uint32_t)size); 136 137 std::map<std::string, std::string>::const_iterator it; 138 139 // 2. Write the key and value strings 140 for (auto pair : abilityStarKey_) { 141 // 1.key 142 parcel.WriteString16(Str8ToStr16(pair.first)); 143 // 2.data content 144 parcel.WriteString16(Str8ToStr16(pair.second)); 145 } 146 147 return true; 148 } 149 150 /** 151 * @brief Reading file stream through parcel to generate AbilityStartSetting instance 152 * @param parcel indicates reading file stream through parcel to generate AbilityStartSetting instance 153 * @return AbilityStartSetting shared_ptr 154 */ Unmarshalling(Parcel & parcel)155AbilityStartSetting *AbilityStartSetting::Unmarshalling(Parcel &parcel) 156 { 157 AbilityStartSetting *abilityStartSetting = new (std::nothrow) AbilityStartSetting(); 158 if (abilityStartSetting == nullptr) { 159 return nullptr; 160 } 161 // 1. Number of key value pairs read 162 uint32_t size = 0; 163 parcel.ReadUint32(size); 164 std::u16string keyReadString16; 165 std::u16string dataReadString16; 166 for (size_t i = 0; (i < size) && abilityStartSetting; i++) { 167 // 1.key 168 keyReadString16 = parcel.ReadString16(); 169 // 2.data content 170 dataReadString16 = parcel.ReadString16(); 171 abilityStartSetting->abilityStarKey_[Str16ToStr8(keyReadString16)] = Str16ToStr8(dataReadString16); 172 keyReadString16.clear(); 173 dataReadString16.clear(); 174 } 175 176 return abilityStartSetting; 177 } 178 } // namespace AAFwk 179 } // namespace OHOS