• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 "mms_persist_helper.h"
17 
18 #include "gsm_pdu_hex_value.h"
19 #include "telephony_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace Telephony {
23 const std::string SMS_PROFILE_URI = "datashare:///com.ohos.smsmmsability";
24 const std::string SMS_PROFILE_MMS_PDU_URI = "datashare:///com.ohos.smsmmsability/sms_mms/mms_pdu";
25 static constexpr const char *PDU_CONTENT = "pdu_content";
26 static constexpr const char *ID = "id";
27 static constexpr uint8_t SLIDE_STEP = 2;
28 
CreateSmsHelper()29 std::shared_ptr<DataShare::DataShareHelper> MmsPersistHelper::CreateSmsHelper()
30 {
31     if (mmsPduDataAbilityHelper_ == nullptr) {
32         mmsPduDataAbilityHelper_ = CreateDataAHelper(TELEPHONY_SMS_MMS_SYS_ABILITY_ID, SMS_PROFILE_URI);
33     }
34     return mmsPduDataAbilityHelper_;
35 }
36 
CreateDataAHelper(int32_t systemAbilityId,const std::string & dataAbilityUri) const37 std::shared_ptr<DataShare::DataShareHelper> MmsPersistHelper::CreateDataAHelper(
38     int32_t systemAbilityId, const std::string &dataAbilityUri) const
39 {
40     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41     if (saManager == nullptr) {
42         TELEPHONY_LOGE("Get system ability mgr failed");
43         return nullptr;
44     }
45     auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
46     if (remoteObj == nullptr) {
47         TELEPHONY_LOGE("GetSystemAbility Service Failed");
48         return nullptr;
49     }
50     return DataShare::DataShareHelper::Creator(remoteObj, dataAbilityUri);
51 }
52 
DeleteMmsPdu(const std::string & dbUrl)53 void MmsPersistHelper::DeleteMmsPdu(const std::string &dbUrl)
54 {
55     std::shared_ptr<DataShare::DataShareHelper> helper = CreateSmsHelper();
56     if (helper == nullptr) {
57         TELEPHONY_LOGE("helper is nullptr");
58         return;
59     }
60 
61     Uri uri(SMS_PROFILE_MMS_PDU_URI);
62 
63     DataShare::DataSharePredicates predicates;
64     predicates.EqualTo("id", dbUrl);
65     int32_t result = helper->Delete(uri, predicates);
66     helper->Release();
67     mmsPdu_ = "";
68     TELEPHONY_LOGI("result:%{public}d", result);
69 }
70 
UpdateMmsPdu(const std::string & mmsPdu,const std::string & dbUrl)71 bool MmsPersistHelper::UpdateMmsPdu(const std::string &mmsPdu, const std::string &dbUrl)
72 {
73     std::shared_ptr<DataShare::DataShareHelper> helper = CreateSmsHelper();
74     if (helper == nullptr) {
75         TELEPHONY_LOGE("helper is nullptr");
76         return false;
77     }
78 
79     Uri uri(SMS_PROFILE_MMS_PDU_URI);
80     DataShare::DataShareValuesBucket bucket;
81     std::string targetMmsPdu;
82     for (size_t i = 0; i < mmsPdu.size(); i++) {
83         targetMmsPdu += static_cast<char>((mmsPdu[i] & 0x0F) | 0xF0);
84         targetMmsPdu += static_cast<char>((mmsPdu[i] & 0xF0) | 0x0F);
85     }
86     bucket.Put(PDU_CONTENT, targetMmsPdu);
87     DataShare::DataSharePredicates predicates;
88     predicates.EqualTo(ID, dbUrl);
89     int32_t result = helper->Update(uri, predicates, bucket);
90     helper->Release();
91     TELEPHONY_LOGI("result:%{public}d", result);
92     return result >= 0 ? true : false;
93 }
94 
GetMmsPdu(const std::string & dbUrl)95 std::string MmsPersistHelper::GetMmsPdu(const std::string &dbUrl)
96 {
97     if (!QueryMmsPdu(dbUrl)) {
98         return "";
99     }
100     return mmsPdu_;
101 }
102 
SetMmsPdu(const std::string & mmsPdu)103 void MmsPersistHelper::SetMmsPdu(const std::string &mmsPdu)
104 {
105     mmsPdu_ = mmsPdu;
106 }
107 
QueryMmsPdu(const std::string & dbUrl)108 bool MmsPersistHelper::QueryMmsPdu(const std::string &dbUrl)
109 {
110     std::shared_ptr<DataShare::DataShareHelper> helper = CreateSmsHelper();
111     if (helper == nullptr) {
112         TELEPHONY_LOGE("helper is nullptr");
113         return false;
114     }
115     Uri uri(SMS_PROFILE_MMS_PDU_URI);
116     std::vector<std::string> colume;
117     DataShare::DataSharePredicates predicates;
118     predicates.EqualTo(ID, dbUrl);
119     auto resultSet = helper->Query(uri, predicates, colume);
120     if (resultSet == nullptr) {
121         TELEPHONY_LOGE("Query Result Set nullptr Failed.");
122         helper->Release();
123         return false;
124     }
125     int count = 0;
126     resultSet->GetRowCount(count);
127     if (count <= 0) {
128         TELEPHONY_LOGE("MmsPdu count: %{public}d error", count);
129         resultSet->Close();
130         helper->Release();
131         return false;
132     }
133     int columnIndex;
134     std::vector<uint8_t> blobValue;
135     resultSet->GoToFirstRow();
136     resultSet->GetColumnIndex(PDU_CONTENT, columnIndex);
137     resultSet->GetBlob(columnIndex, blobValue);
138     resultSet->Close();
139     helper->Release();
140     std::string mmsPdu;
141     char pduChar = 0x00;
142     for (size_t i = 0; i + 1 < blobValue.size(); i = i + SLIDE_STEP) {
143         pduChar = (blobValue[i] & HEX_VALUE_0F) | (blobValue[i + 1] & HEX_VALUE_F0);
144         mmsPdu += static_cast<char>(pduChar);
145     }
146     TELEPHONY_LOGI("blob len:%{public}d, mmsPdu len:%{public}d", static_cast<uint32_t>(blobValue.size()),
147         static_cast<uint32_t>(mmsPdu.size()));
148     SetMmsPdu(mmsPdu);
149     return true;
150 }
151 } // namespace Telephony
152 } // namespace OHOS