1 /*
2  * Copyright (c) 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 #include "core/pipeline/pipeline_context.h"
16 #if !defined(PREVIEW)
17 #include <dlfcn.h>
18 #endif
19 
20 #include "data_ability_helper.h"
21 #include "datashare_helper.h"
22 
23 #include "adapter/ohos/entrance/data_ability_helper_standard.h"
24 #include "base/log/ace_trace.h"
25 #include "base/utils/string_utils.h"
26 #include "base/utils/utils.h"
27 
28 namespace OHOS::Ace {
29 const std::string MEDIA_SERVER_HEAD = "datashare:///media";
30 
DataAbilityHelperStandard(const std::shared_ptr<OHOS::AppExecFwk::Context> & context,const std::shared_ptr<OHOS::AbilityRuntime::Context> & runtimeContext,bool useStageModel)31 DataAbilityHelperStandard::DataAbilityHelperStandard(const std::shared_ptr<OHOS::AppExecFwk::Context>& context,
32     const std::shared_ptr<OHOS::AbilityRuntime::Context>& runtimeContext, bool useStageModel)
33     : useStageModel_(useStageModel)
34 {
35     if (useStageModel) {
36         runtimeContext_ = runtimeContext;
37 #ifdef MEDIA_LIBRARY_EXISTS
38         mgr_.InitMediaLibraryManager(runtimeContext->GetToken());
39 #endif
40     } else {
41         context_ = context;
42 #ifdef MEDIA_LIBRARY_EXISTS
43         mgr_.InitMediaLibraryManager(context->GetToken());
44 #endif
45     }
46 }
47 
QueryThumbnailResFromDataAbility(const std::string & uri)48 void* DataAbilityHelperStandard::QueryThumbnailResFromDataAbility(const std::string& uri)
49 {
50 #ifdef PREVIEW
51     return nullptr;
52 #else
53 #ifdef MEDIA_LIBRARY_EXISTS
54     ACE_FUNCTION_TRACE();
55     Uri fileUri(uri);
56     return mgr_.GetThumbnail(fileUri).release();
57 #else
58     return nullptr;
59 #endif
60 #endif
61 }
62 
GetMovingPhotoImageUri(const std::string & uri)63 std::string DataAbilityHelperStandard::GetMovingPhotoImageUri(const std::string& uri)
64 {
65 #ifdef MEDIA_LIBRARY_EXISTS
66     return mgr_.GetMovingPhotoImageUri(uri);
67 #else
68     return "";
69 #endif
70 }
71 
GetMovingPhotoDateModified(const std::string & uri)72 int64_t DataAbilityHelperStandard::GetMovingPhotoDateModified(const std::string& uri)
73 {
74 #ifdef MEDIA_LIBRARY_EXISTS
75     return mgr_.GetMovingPhotoDateModified(uri);
76 #else
77     return -1;
78 #endif
79 }
80 
OpenFile(const std::string & uriStr,const std::string & mode)81 int32_t DataAbilityHelperStandard::OpenFile(const std::string& uriStr, const std::string& mode)
82 {
83     // FA model always uses DataAbility
84     if (!useStageModel_ || StringUtils::StartWith(uriStr, "dataability://")) {
85         return OpenFileWithDataAbility(uriStr, mode);
86     }
87     if (StringUtils::StartWith(uriStr, "datashare://") || StringUtils::StartWith(uriStr, "file://")) {
88         return OpenFileWithDataShare(uriStr, mode);
89     }
90     LOGE("DataAbilityHelperStandard OpenFile uri is not support.");
91     return -1;
92 }
93 
OpenFileWithDataAbility(const std::string & uriStr,const std::string & mode)94 int32_t DataAbilityHelperStandard::OpenFileWithDataAbility(const std::string& uriStr, const std::string& mode)
95 {
96     std::shared_ptr<OHOS::Uri> uri = std::make_shared<Uri>(uriStr);
97     if (!dataAbilityHelper_) {
98         if (useStageModel_) {
99             dataAbilityHelper_ = AppExecFwk::DataAbilityHelper::Creator(runtimeContext_.lock(), uri, false);
100         } else {
101             dataAbilityHelper_ = AppExecFwk::DataAbilityHelper::Creator(context_.lock(), uri);
102         }
103     }
104 
105     CHECK_NULL_RETURN(dataAbilityHelper_, -1);
106     return dataAbilityHelper_->OpenFile(*uri, mode);
107 }
108 
OpenFileWithDataShare(const std::string & uriStr,const std::string & mode)109 int32_t DataAbilityHelperStandard::OpenFileWithDataShare(const std::string& uriStr, const std::string& mode)
110 {
111     auto context = runtimeContext_.lock();
112     if (useStageModel_ && !dataShareHelper_ && context) {
113         dataShareHelper_ = DataShare::DataShareHelper::Creator(context->GetToken(), MEDIA_SERVER_HEAD);
114     }
115 
116     CHECK_NULL_RETURN(dataShareHelper_, -1);
117     Uri uri = Uri(uriStr);
118     return dataShareHelper_->OpenFile(uri, mode);
119 }
120 
ReadMovingPhotoVideo(const std::string & uri)121 int32_t DataAbilityHelperStandard::ReadMovingPhotoVideo(const std::string &uri)
122 {
123 #ifdef MEDIA_LIBRARY_EXISTS
124     return mgr_.ReadMovingPhotoVideo(uri);
125 #else
126     return -1;
127 #endif
128 }
129 
130 } // namespace OHOS::Ace
131