1 /*
2 * Copyright (c) 2021-2022 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 "account_sync.h"
17
18 #include <mutex>
19
20 #include "common.h"
21 #include "contacts.h"
22 #include "contacts_account.h"
23 #include "contacts_columns.h"
24 #include "contacts_database.h"
25 #include "contacts_string_utils.h"
26 #include "hilog_wrapper.h"
27 #include "raw_contacts.h"
28
29 namespace OHOS {
30 namespace Contacts {
31 namespace {
32 std::mutex g_mtx;
33 }
34 std::shared_ptr<ContactsDataBase> AccountSync::contactsDataBase_ = nullptr;
35 std::shared_ptr<ProfileDatabase> AccountSync::profileDataBase_ = nullptr;
36 std::shared_ptr<AccountSync> AccountSync::instance_ = nullptr;
37 std::shared_ptr<ContactsAccount> g_contactsAccount = nullptr;
38
GetInstance()39 std::shared_ptr<AccountSync> AccountSync::GetInstance()
40 {
41 if (instance_ == nullptr) {
42 instance_.reset(new AccountSync());
43 }
44 return instance_;
45 }
46
AccountSync()47 AccountSync::AccountSync()
48 {
49 contactsDataBase_ = ContactsDataBase::GetInstance();
50 g_contactsAccount = ContactsAccount::GetInstance();
51 profileDataBase_ = ProfileDatabase::GetInstance();
52 }
53
~AccountSync()54 AccountSync::~AccountSync()
55 {
56 }
57
getShouldUpdateAndAddAccounts(std::vector<OHOS::AccountSA::OhosAccountInfo> systemAccounts,std::vector<AccountDataCollection> localAccounts,std::vector<OHOS::AccountSA::OhosAccountInfo> & shouldUpdateAccounts,std::vector<OHOS::AccountSA::OhosAccountInfo> & shouldAddAccounts)58 void AccountSync::getShouldUpdateAndAddAccounts(std::vector<OHOS::AccountSA::OhosAccountInfo> systemAccounts,
59 std::vector<AccountDataCollection> localAccounts,
60 std::vector<OHOS::AccountSA::OhosAccountInfo> &shouldUpdateAccounts,
61 std::vector<OHOS::AccountSA::OhosAccountInfo> &shouldAddAccounts)
62 {
63 for (size_t i = 0; i < systemAccounts.size(); i++) {
64 for (size_t j = 0; j < localAccounts.size(); j++) {
65 if (systemAccounts[i].uid_ == localAccounts[j].GetcAccountType()) {
66 break;
67 }
68 if (j == localAccounts.size() - 1) {
69 shouldAddAccounts.push_back(systemAccounts[i]);
70 }
71 }
72 }
73 }
74
SyncUpdateAccount(std::vector<OHOS::AccountSA::OhosAccountInfo> sysAccounts,bool isProfile)75 void AccountSync::SyncUpdateAccount(std::vector<OHOS::AccountSA::OhosAccountInfo> sysAccounts, bool isProfile)
76 {
77 g_mtx.lock();
78 if (sysAccounts.empty()) {
79 HILOG_ERROR("AccountSync::SyncUpdateAccount sysAccounts is null");
80 g_mtx.unlock();
81 return;
82 }
83 std::shared_ptr<OHOS::NativeRdb::RdbStore> store = nullptr;
84 if (isProfile) {
85 store = profileDataBase_->store_;
86 HILOG_INFO("AccountSync::SyncUpdateAccount this is profileDataBase_ operation");
87 } else {
88 store = contactsDataBase_->contactStore_;
89 HILOG_INFO("AccountSync::SyncUpdateAccount this is contactsDataBase_ operation");
90 }
91 if (store == nullptr) {
92 HILOG_ERROR("SyncUpdateAccount isProfile values is %{public}d this database store ponit is null", isProfile);
93 g_mtx.unlock();
94 return;
95 }
96 std::vector<AccountDataCollection> accounts = g_contactsAccount->GetAccountFromLocal(store);
97 std::vector<AccountDataCollection> notInSysAccounts;
98 for (size_t i = 0; i < accounts.size(); i++) {
99 auto iter = accounts[i];
100 // if account is not default and not in sysAccounts then this should remove
101 if (!iter.IsDefaultAccount() && !iter.ContainSysAccounts(sysAccounts)) {
102 notInSysAccounts.push_back(iter);
103 }
104 }
105 HILOG_INFO("SyncUpdateAccount getNotExistAccount notInSysAccounts size is :%{public}zu", notInSysAccounts.size());
106 std::vector<OHOS::AccountSA::OhosAccountInfo> shouldUpdateAccounts;
107 std::vector<OHOS::AccountSA::OhosAccountInfo> shouldAddAccounts;
108 getShouldUpdateAndAddAccounts(sysAccounts, accounts, shouldUpdateAccounts, shouldAddAccounts);
109 HILOG_INFO("SyncUpdateAccount:%{public}zu, :%{public}zu", shouldUpdateAccounts.size(), shouldAddAccounts.size());
110 store->BeginTransaction();
111 if (!notInSysAccounts.empty()) {
112 g_contactsAccount->StopForegin(store);
113 store->Commit();
114 for (size_t i = 0; i < notInSysAccounts.size(); i++) {
115 int accountId = g_contactsAccount->GetNotExistAccount(store, notInSysAccounts[i]);
116 HILOG_INFO("SyncUpdateAccount getNotExistAccount value is :%{public}d", accountId);
117 ClearData(store, accountId);
118 }
119 }
120 for (size_t i = 0; i < shouldAddAccounts.size(); i++) {
121 g_contactsAccount->Insert(store, shouldAddAccounts[i].name_, shouldAddAccounts[i].uid_);
122 store->Commit();
123 }
124 g_contactsAccount->OpenForegin(store);
125 store->Commit();
126 g_mtx.unlock();
127 }
128
ClearData(std::shared_ptr<OHOS::NativeRdb::RdbStore> store,int accountId)129 int AccountSync::ClearData(std::shared_ptr<OHOS::NativeRdb::RdbStore> store, int accountId)
130 {
131 RawContacts contactsRawContact;
132 Contacts contactsContact;
133 if (accountId > ID_EMPTY) {
134 int needDeleteRawContactId = contactsRawContact.GetDeleteRawContactIdByAccountId(store, accountId);
135 HILOG_INFO("GetDeleteRawContactIdByAccountId successfully, needDeleteRawContactId is %{public}d",
136 needDeleteRawContactId);
137 int needDeleteContactId = contactsRawContact.GetDeleteContactIdByAccountId(store, accountId);
138 HILOG_INFO("GetDeleteContactIdByAccountId successfully needDeleteContactId is %{public}d", needDeleteContactId);
139 g_contactsAccount->DeleteDataByRawId(store, needDeleteRawContactId);
140 int ret = store->Commit();
141 HILOG_INFO("DeleteDataByRawId successfully commit ret %{public}d", ret);
142 contactsRawContact.DeleteRawcontactByRawId(store, needDeleteRawContactId);
143 ret = store->Commit();
144 HILOG_INFO("DeleteRawcontactByRawId successfully commit ret %{public}d", ret);
145 contactsContact.DeleteContactById(store, needDeleteContactId);
146 ret = store->Commit();
147 HILOG_INFO("DeletecontactsById successfully commit ret %{public}d", ret);
148 g_contactsAccount->DeleteGroupsByAccountId(store, accountId);
149 ret = store->Commit();
150 HILOG_INFO("DeleteGroupsByAccountId successfully commit ret %{public}d", ret);
151 g_contactsAccount->DeleteAccountByAccountId(store, accountId);
152 ret = store->Commit();
153 HILOG_INFO("DeleteAccountByAccountId successfully commit ret %{public}d", ret);
154 }
155 return RDB_EXECUTE_OK;
156 }
157 } // namespace Contacts
158 } // namespace OHOS
159