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 #define MLOG_TAG "RingtoneAsset"
16 
17 #include "ringtone_asset.h"
18 
19 #include "ringtone_db_const.h"
20 #include "ringtone_errno.h"
21 #include "ringtone_log.h"
22 #include "ringtone_type.h"
23 
24 namespace OHOS {
25 namespace Media {
26 using namespace std;
27 
RingtoneAsset()28 RingtoneAsset::RingtoneAsset()
29 {
30 }
31 
GetId() const32 int32_t RingtoneAsset::GetId() const
33 {
34     return GetInt32Member(RINGTONE_COLUMN_TONE_ID);
35 }
36 
SetId(int32_t id)37 void RingtoneAsset::SetId(int32_t id)
38 {
39     member_[RINGTONE_COLUMN_TONE_ID] = id;
40 }
41 
GetSize() const42 int64_t RingtoneAsset::GetSize() const
43 {
44     return GetInt64Member(RINGTONE_COLUMN_SIZE);
45 }
46 
SetSize(int64_t size)47 void RingtoneAsset::SetSize(int64_t size)
48 {
49     member_[RINGTONE_COLUMN_SIZE] = size;
50 }
51 
GetPath() const52 const string &RingtoneAsset::GetPath() const
53 {
54     return GetStrMember(RINGTONE_COLUMN_DATA);
55 }
56 
SetPath(const string & path)57 void RingtoneAsset::SetPath(const string &path)
58 {
59     member_[RINGTONE_COLUMN_DATA] = path;
60 }
61 
GetDisplayName() const62 const string &RingtoneAsset::GetDisplayName() const
63 {
64     return GetStrMember(RINGTONE_COLUMN_DISPLAY_NAME);
65 }
66 
SetDisplayName(const string & displayName)67 void RingtoneAsset::SetDisplayName(const string &displayName)
68 {
69     member_[RINGTONE_COLUMN_DISPLAY_NAME] = displayName;
70 }
71 
GetTitle() const72 const string &RingtoneAsset::GetTitle() const
73 {
74     return GetStrMember(RINGTONE_COLUMN_TITLE);
75 }
76 
SetTitle(const string & title)77 void RingtoneAsset::SetTitle(const string &title)
78 {
79     member_[RINGTONE_COLUMN_TITLE] = title;
80 }
81 
GetMimeType() const82 const string &RingtoneAsset::GetMimeType() const
83 {
84     return GetStrMember(RINGTONE_COLUMN_MIME_TYPE);
85 }
86 
SetMimeType(const string & mimeType)87 void RingtoneAsset::SetMimeType(const string &mimeType)
88 {
89     member_[RINGTONE_COLUMN_MIME_TYPE] = mimeType;
90 }
91 
GetToneType() const92 int32_t RingtoneAsset::GetToneType() const
93 {
94     return GetInt32Member(RINGTONE_COLUMN_TONE_TYPE);
95 }
96 
SetToneType(int32_t toneType)97 void RingtoneAsset::SetToneType(int32_t toneType)
98 {
99     member_[RINGTONE_COLUMN_TONE_TYPE] = toneType;
100 }
101 
GetSourceType() const102 int32_t RingtoneAsset::GetSourceType() const
103 {
104     return GetInt32Member(RINGTONE_COLUMN_SOURCE_TYPE);
105 }
106 
SetSourceType(int32_t type)107 void RingtoneAsset::SetSourceType(int32_t type)
108 {
109     member_[RINGTONE_COLUMN_SOURCE_TYPE] = type;
110 }
111 
GetDateAdded() const112 int64_t RingtoneAsset::GetDateAdded() const
113 {
114     return GetInt64Member(RINGTONE_COLUMN_DATE_ADDED);
115 }
116 
SetDateAdded(int64_t dateAdded)117 void RingtoneAsset::SetDateAdded(int64_t dateAdded)
118 {
119     member_[RINGTONE_COLUMN_DATE_ADDED] = dateAdded;
120 }
121 
GetDateModified() const122 int64_t RingtoneAsset::GetDateModified() const
123 {
124     return GetInt64Member(RINGTONE_COLUMN_DATE_MODIFIED);
125 }
126 
SetDateModified(int64_t dateModified)127 void RingtoneAsset::SetDateModified(int64_t dateModified)
128 {
129     member_[RINGTONE_COLUMN_DATE_MODIFIED] = dateModified;
130 }
131 
132 
GetDateTaken() const133 int64_t RingtoneAsset::GetDateTaken() const
134 {
135     return GetInt64Member(RINGTONE_COLUMN_DATE_TAKEN);
136 }
137 
SetDateTaken(int64_t dataTaken)138 void RingtoneAsset::SetDateTaken(int64_t dataTaken)
139 {
140     member_[RINGTONE_COLUMN_DATE_TAKEN] = dataTaken;
141 }
142 
GetDuration() const143 int32_t RingtoneAsset::GetDuration() const
144 {
145     return GetInt32Member(RINGTONE_COLUMN_DURATION);
146 }
147 
SetDuration(int32_t duration)148 void RingtoneAsset::SetDuration(int32_t duration)
149 {
150     member_[RINGTONE_COLUMN_DURATION] = duration;
151 }
152 
GetShottoneType() const153 int32_t RingtoneAsset::GetShottoneType() const
154 {
155     return GetInt32Member(RINGTONE_COLUMN_SHOT_TONE_TYPE);
156 }
157 
SetShottoneType(int32_t type)158 void RingtoneAsset::SetShottoneType(int32_t type)
159 {
160     member_[RINGTONE_COLUMN_SHOT_TONE_TYPE] = type;
161 }
162 
GetShottoneSourceType() const163 int32_t RingtoneAsset::GetShottoneSourceType() const
164 {
165     return GetInt32Member(RINGTONE_COLUMN_SHOT_TONE_SOURCE_TYPE);
166 }
167 
SetShottoneSourceType(int32_t type)168 void RingtoneAsset::SetShottoneSourceType(int32_t type)
169 {
170     member_[RINGTONE_COLUMN_SHOT_TONE_SOURCE_TYPE] = type;
171 }
172 
GetNotificationtoneType() const173 int32_t RingtoneAsset::GetNotificationtoneType() const
174 {
175     return GetInt32Member(RINGTONE_COLUMN_NOTIFICATION_TONE_TYPE);
176 }
177 
SetNotificationtoneType(int32_t type)178 void RingtoneAsset::SetNotificationtoneType(int32_t type)
179 {
180     member_[RINGTONE_COLUMN_NOTIFICATION_TONE_TYPE] = type;
181 }
182 
GetNotificationtoneSourceType() const183 int32_t RingtoneAsset::GetNotificationtoneSourceType() const
184 {
185     return GetInt32Member(RINGTONE_COLUMN_NOTIFICATION_TONE_SOURCE_TYPE);
186 }
187 
SetNotificationtoneSourceType(int32_t type)188 void RingtoneAsset::SetNotificationtoneSourceType(int32_t type)
189 {
190     member_[RINGTONE_COLUMN_NOTIFICATION_TONE_SOURCE_TYPE] = type;
191 }
192 
GetRingtoneType() const193 int32_t RingtoneAsset::GetRingtoneType() const
194 {
195     return GetInt32Member(RINGTONE_COLUMN_RING_TONE_TYPE);
196 }
197 
SetRingtoneType(int32_t type)198 void RingtoneAsset::SetRingtoneType(int32_t type)
199 {
200     member_[RINGTONE_COLUMN_RING_TONE_TYPE] = type;
201 }
202 
GetRingtoneSourceType() const203 int32_t RingtoneAsset::GetRingtoneSourceType() const
204 {
205     return GetInt32Member(RINGTONE_COLUMN_RING_TONE_SOURCE_TYPE);
206 }
207 
SetRingtoneSourceType(int32_t type)208 void RingtoneAsset::SetRingtoneSourceType(int32_t type)
209 {
210     member_[RINGTONE_COLUMN_RING_TONE_SOURCE_TYPE] = type;
211 }
212 
GetAlarmtoneType() const213 int32_t RingtoneAsset::GetAlarmtoneType() const
214 {
215     return GetInt32Member(RINGTONE_COLUMN_ALARM_TONE_TYPE);
216 }
217 
SetAlarmtoneType(int32_t type)218 void RingtoneAsset::SetAlarmtoneType(int32_t type)
219 {
220     member_[RINGTONE_COLUMN_ALARM_TONE_TYPE] = type;
221 }
222 
GetAlarmtoneSourceType() const223 int32_t RingtoneAsset::GetAlarmtoneSourceType() const
224 {
225     return GetInt32Member(RINGTONE_COLUMN_ALARM_TONE_SOURCE_TYPE);
226 }
227 
SetAlarmtoneSourceType(int32_t type)228 void RingtoneAsset::SetAlarmtoneSourceType(int32_t type)
229 {
230     member_[RINGTONE_COLUMN_ALARM_TONE_SOURCE_TYPE] = type;
231 }
232 
SetOpenStatus(int32_t fd,int32_t openStatus)233 void RingtoneAsset::SetOpenStatus(int32_t fd, int32_t openStatus)
234 {
235     lock_guard<mutex> lock(openStatusMapMutex_);
236     if (openStatusMap_ == nullptr) {
237         openStatusMap_ = make_shared<unordered_map<int32_t, int32_t>>();
238     }
239     openStatusMap_->insert({fd, openStatus});
240 }
241 
RemoveOpenStatus(int32_t fd)242 void RingtoneAsset::RemoveOpenStatus(int32_t fd)
243 {
244     lock_guard<mutex> lock(openStatusMapMutex_);
245     if (openStatusMap_ == nullptr) {
246         return;
247     }
248     openStatusMap_->erase(fd);
249 }
250 
GetOpenStatus(int32_t fd)251 int32_t RingtoneAsset::GetOpenStatus(int32_t fd)
252 {
253     lock_guard<mutex> lock(openStatusMapMutex_);
254     if (openStatusMap_ == nullptr) {
255         return E_INVALID_VALUES;
256     }
257     if (openStatusMap_->find(fd) != openStatusMap_->end()) {
258         return openStatusMap_->at(fd);
259     } else {
260         RINGTONE_ERR_LOG("can not find this fd: [%{public}d]", fd);
261         return E_INVALID_VALUES;
262     }
263 }
264 
GetMemberMap()265 unordered_map<string, variant<int32_t, int64_t, string, double>> &RingtoneAsset::GetMemberMap()
266 {
267     return member_;
268 }
269 
GetMemberValue(const string & name)270 variant<int32_t, int64_t, string, double> &RingtoneAsset::GetMemberValue(const string &name)
271 {
272     return member_[name];
273 }
274 
GetStrMember(const string & name) const275 const string &RingtoneAsset::GetStrMember(const string &name) const
276 {
277     return (member_.count(name) > 0) ? get<string>(member_.at(name)) : RINGTONE_DEFAULT_STR;
278 }
279 
GetInt32Member(const string & name) const280 int32_t RingtoneAsset::GetInt32Member(const string &name) const
281 {
282     return (member_.count(name) > 0) ? get<int32_t>(member_.at(name)) : RINGTONE_DEFAULT_INT32;
283 }
284 
GetInt64Member(const string & name) const285 int64_t RingtoneAsset::GetInt64Member(const string &name) const
286 {
287     return (member_.count(name) > 0) ? get<int64_t>(member_.at(name)) : RINGTONE_DEFAULT_INT64;
288 }
289 } // namespace Media
290 } // namespace OHOS
291