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 #define MLOG_TAG "MediaLibraryRestoreService"
17
18 #include <context.h>
19 #include <context_impl.h>
20
21 #include "backup_file_utils.h"
22 #include "backup_restore_service.h"
23 #include "media_log.h"
24 #include "clone_restore.h"
25 #include "upgrade_restore.h"
26 #include "others_clone_restore.h"
27
28 namespace OHOS {
29 namespace Media {
30 const int DUAL_FIRST_NUMBER = 65;
31 const int DUAL_SECOND_NUMBER = 110;
32 const int DUAL_THIRD_NUMBER = 100;
33 const int DUAL_FOURTH_NUMBER = 114;
34 const int DUAL_FIFTH_NUMBER = 111;
35 const int DUAL_SIXTH_NUMBER = 105;
36 const int DUAL_SEVENTH_NUMBER = 100;
GetInstance(void)37 BackupRestoreService &BackupRestoreService::GetInstance(void)
38 {
39 static BackupRestoreService inst;
40 return inst;
41 }
42
GetDualDirName()43 std::string GetDualDirName()
44 {
45 int arr[] = { DUAL_FIRST_NUMBER, DUAL_SECOND_NUMBER, DUAL_THIRD_NUMBER, DUAL_FOURTH_NUMBER, DUAL_FIFTH_NUMBER,
46 DUAL_SIXTH_NUMBER, DUAL_SEVENTH_NUMBER };
47 int len = sizeof(arr) / sizeof(arr[0]);
48 std::string dualDirName = "";
49 for (int i = 0; i < len; i++) {
50 dualDirName += char(arr[i]);
51 }
52 return dualDirName;
53 }
54
Init(const RestoreInfo & info)55 void BackupRestoreService::Init(const RestoreInfo &info)
56 {
57 if (restoreService_ != nullptr) {
58 return;
59 }
60 serviceBackupDir_ = info.backupDir;
61 if (info.sceneCode == UPGRADE_RESTORE_ID) {
62 restoreService_ = std::make_unique<UpgradeRestore>(info.galleryAppName, info.mediaAppName, UPGRADE_RESTORE_ID,
63 GetDualDirName());
64 serviceBackupDir_ = RESTORE_SANDBOX_DIR;
65 } else if (info.sceneCode == DUAL_FRAME_CLONE_RESTORE_ID) {
66 restoreService_ = std::make_unique<UpgradeRestore>(info.galleryAppName, info.mediaAppName,
67 DUAL_FRAME_CLONE_RESTORE_ID);
68 } else if (info.sceneCode == I_PHONE_CLONE_RESTORE) {
69 restoreService_ = std::make_unique<OthersCloneRestore>(I_PHONE_CLONE_RESTORE, info.mediaAppName,
70 info.bundleInfo);
71 } else if (info.sceneCode == OTHERS_PHONE_CLONE_RESTORE) {
72 restoreService_ = std::make_unique<OthersCloneRestore>(OTHERS_PHONE_CLONE_RESTORE, info.mediaAppName);
73 } else if (info.sceneCode == LITE_PHONE_CLONE_RESTORE) {
74 restoreService_ = std::make_unique<OthersCloneRestore>(LITE_PHONE_CLONE_RESTORE, info.mediaAppName);
75 } else {
76 restoreService_ = std::make_unique<CloneRestore>();
77 }
78 }
79
StartRestore(const std::shared_ptr<AbilityRuntime::Context> & context,const RestoreInfo & info)80 void BackupRestoreService::StartRestore(const std::shared_ptr<AbilityRuntime::Context> &context,
81 const RestoreInfo &info)
82 {
83 MEDIA_INFO_LOG("Start restore service: %{public}d", info.sceneCode);
84 Init(info);
85 if (restoreService_ == nullptr) {
86 MEDIA_ERR_LOG("Create media restore service failed.");
87 return;
88 }
89 if (context != nullptr) {
90 BackupFileUtils::CreateDataShareHelper(context->GetToken());
91 }
92 restoreService_->StartRestore(serviceBackupDir_, UPGRADE_FILE_DIR);
93 }
94
StartRestoreEx(const std::shared_ptr<AbilityRuntime::Context> & context,const RestoreInfo & info,std::string & restoreExInfo)95 void BackupRestoreService::StartRestoreEx(const std::shared_ptr<AbilityRuntime::Context> &context,
96 const RestoreInfo &info, std::string &restoreExInfo)
97 {
98 MEDIA_INFO_LOG("Start restoreEx service: %{public}d", info.sceneCode);
99 Init(info);
100 if (restoreService_ == nullptr) {
101 MEDIA_ERR_LOG("Create media restore service failed.");
102 restoreExInfo = "";
103 return;
104 }
105 if (context != nullptr) {
106 BackupFileUtils::CreateDataShareHelper(context->GetToken());
107 }
108 restoreService_->StartRestoreEx(serviceBackupDir_, UPGRADE_FILE_DIR, restoreExInfo);
109 }
110
GetBackupInfo(int32_t sceneCode,std::string & backupInfo)111 void BackupRestoreService::GetBackupInfo(int32_t sceneCode, std::string &backupInfo)
112 {
113 MEDIA_INFO_LOG("Start restore service: %{public}d", sceneCode);
114 if (sceneCode != CLONE_RESTORE_ID) {
115 MEDIA_ERR_LOG("StartRestoreEx current scene is not supported");
116 backupInfo = "";
117 return;
118 }
119 Init({CLONE_RESTORE_ID, "", "", "", ""});
120 if (restoreService_ == nullptr) {
121 MEDIA_ERR_LOG("Create media restore service failed.");
122 backupInfo = "";
123 return;
124 }
125 backupInfo = restoreService_->GetBackupInfo();
126 }
127
GetProgressInfo(std::string & progressInfo)128 void BackupRestoreService::GetProgressInfo(std::string &progressInfo)
129 {
130 MEDIA_INFO_LOG("Start get progressInfo");
131 if (restoreService_ == nullptr) {
132 MEDIA_WARN_LOG("Media restore service not created.");
133 progressInfo = "";
134 return;
135 }
136 progressInfo = restoreService_->GetProgressInfo();
137 }
138
StartBackup(int32_t sceneCode,const std::string & galleryAppName,const std::string & mediaAppName)139 void BackupRestoreService::StartBackup(int32_t sceneCode, const std::string &galleryAppName,
140 const std::string &mediaAppName)
141 {
142 MEDIA_INFO_LOG("Start backup service: %{public}d", sceneCode);
143 if (sceneCode != CLONE_RESTORE_ID) {
144 MEDIA_ERR_LOG("StartBackup current scene is not supported");
145 return;
146 }
147 Init({CLONE_RESTORE_ID, galleryAppName, mediaAppName, "", ""});
148 if (restoreService_ == nullptr) {
149 MEDIA_ERR_LOG("Create media backup service failed.");
150 return;
151 }
152 restoreService_->StartBackup();
153 }
154 } // namespace Media
155 } // namespace OHOS
156