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 {
74 restoreService_ = std::make_unique<CloneRestore>();
75 }
76 }
77
StartRestore(const std::shared_ptr<AbilityRuntime::Context> & context,const RestoreInfo & info)78 void BackupRestoreService::StartRestore(const std::shared_ptr<AbilityRuntime::Context> &context,
79 const RestoreInfo &info)
80 {
81 MEDIA_INFO_LOG("Start restore service: %{public}d", info.sceneCode);
82 Init(info);
83 if (restoreService_ == nullptr) {
84 MEDIA_ERR_LOG("Create media restore service failed.");
85 return;
86 }
87 if (context != nullptr) {
88 BackupFileUtils::CreateDataShareHelper(context->GetToken());
89 }
90 restoreService_->StartRestore(serviceBackupDir_, UPGRADE_FILE_DIR);
91 }
92
StartRestoreEx(const std::shared_ptr<AbilityRuntime::Context> & context,const RestoreInfo & info,std::string & restoreExInfo)93 void BackupRestoreService::StartRestoreEx(const std::shared_ptr<AbilityRuntime::Context> &context,
94 const RestoreInfo &info, std::string &restoreExInfo)
95 {
96 MEDIA_INFO_LOG("Start restoreEx service: %{public}d", info.sceneCode);
97 Init(info);
98 if (restoreService_ == nullptr) {
99 MEDIA_ERR_LOG("Create media restore service failed.");
100 restoreExInfo = "";
101 return;
102 }
103 if (context != nullptr) {
104 BackupFileUtils::CreateDataShareHelper(context->GetToken());
105 }
106 restoreService_->StartRestoreEx(serviceBackupDir_, UPGRADE_FILE_DIR, restoreExInfo);
107 }
108
GetBackupInfo(int32_t sceneCode,std::string & backupInfo)109 void BackupRestoreService::GetBackupInfo(int32_t sceneCode, std::string &backupInfo)
110 {
111 MEDIA_INFO_LOG("Start restore service: %{public}d", sceneCode);
112 if (sceneCode != CLONE_RESTORE_ID) {
113 MEDIA_ERR_LOG("StartRestoreEx current scene is not supported");
114 backupInfo = "";
115 return;
116 }
117 Init({CLONE_RESTORE_ID, "", "", "", ""});
118 if (restoreService_ == nullptr) {
119 MEDIA_ERR_LOG("Create media restore service failed.");
120 backupInfo = "";
121 return;
122 }
123 backupInfo = restoreService_->GetBackupInfo();
124 }
125
GetProgressInfo(std::string & progressInfo)126 void BackupRestoreService::GetProgressInfo(std::string &progressInfo)
127 {
128 MEDIA_INFO_LOG("Start get progressInfo");
129 if (restoreService_ == nullptr) {
130 MEDIA_WARN_LOG("Media restore service not created.");
131 progressInfo = "";
132 return;
133 }
134 progressInfo = restoreService_->GetProgressInfo();
135 }
136
StartBackup(int32_t sceneCode,const std::string & galleryAppName,const std::string & mediaAppName)137 void BackupRestoreService::StartBackup(int32_t sceneCode, const std::string &galleryAppName,
138 const std::string &mediaAppName)
139 {
140 MEDIA_INFO_LOG("Start backup service: %{public}d", sceneCode);
141 if (sceneCode != CLONE_RESTORE_ID) {
142 MEDIA_ERR_LOG("StartBackup current scene is not supported");
143 return;
144 }
145 Init({CLONE_RESTORE_ID, galleryAppName, mediaAppName, "", ""});
146 if (restoreService_ == nullptr) {
147 MEDIA_ERR_LOG("Create media backup service failed.");
148 return;
149 }
150 restoreService_->StartBackup();
151 }
152 } // namespace Media
153 } // namespace OHOS
154