1 /*
2  * Copyright (C) 2024 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 "media_analysis_proxy.h"
17 
18 #include "if_system_ability_manager.h"
19 #include "system_ability_definition.h"
20 #include "iservice_registry.h"
21 
22 namespace OHOS {
23 namespace Media {
MediaAnalysisProxy(const sptr<IRemoteObject> & object)24 MediaAnalysisProxy::MediaAnalysisProxy(const sptr<IRemoteObject> &object)
25     : IRemoteProxy<IMediaAnalysisService>(object)
26 {
27     MEDIA_INFO_LOG("creat MediaAnalysisProxy instance");
28 }
29 
~MediaAnalysisProxy()30 MediaAnalysisProxy::~MediaAnalysisProxy()
31 {
32     MEDIA_INFO_LOG("destroy MediaAnalysisProxy instance");
33 }
SendTransactCmd(int32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 bool MediaAnalysisProxy::SendTransactCmd(int32_t code, MessageParcel &data, MessageParcel &reply,
35     MessageOption &option)
36 {
37     int32_t minTimeout = 4;
38     auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
39     if (saMgr == nullptr) {
40         MEDIA_ERR_LOG("Get samgr fail, samgr is nullptr");
41         return false;
42     }
43     sptr<IRemoteObject> remoteObject = saMgr->CheckSystemAbility(SAID);
44     if (remoteObject != nullptr) {
45         MEDIA_INFO_LOG("Active task: sa already running");
46         return false;
47     }
48 
49     int ret = SetParameter("persist.multimedia.media_analysis_service.startactively", "1");
50     if (ret != 0) {
51         MEDIA_ERR_LOG("Failed to set parameter startactively, result:%{public}d", ret);
52     }
53     sptr<IRemoteObject> remote = saMgr->LoadSystemAbility(SAID, minTimeout);
54     if (remote == nullptr) {
55         MEDIA_ERR_LOG("fail to send transact %{public}d due to remote object", code);
56         return false;
57     }
58 
59     int32_t result = remote->SendRequest(code, data, reply, option);
60     if (result != NO_ERROR) {
61         MEDIA_ERR_LOG("receive error transact code %{public}d in transact cmd %{public}d", result, code);
62         return false;
63     }
64     MEDIA_INFO_LOG("send request success");
65     return true;
66 }
67 } // namespace Media
68 } // namespace OHOS