• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_subscriber/account_subscriber.h"
17 
18 #include <cinttypes>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22 
23 #include "appexecfwk_errors.h"
24 #include "bundle_info.h"
25 #include "common_event_manager.h"
26 #include "common_event_support.h"
27 #include "iservice_registry.h"
28 #include "storage_service_log.h"
29 #include "system_ability_definition.h"
30 #include "want.h"
31 
32 using namespace OHOS::AAFwk;
33 namespace OHOS {
34 namespace StorageManager {
35 std::shared_ptr<DataShare::DataShareHelper> AccountSubscriber::mediaShare_ = nullptr;
36 
AccountSubscriber(const EventFwk::CommonEventSubscribeInfo & subscriberInfo)37 AccountSubscriber::AccountSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo)
38     : EventFwk::CommonEventSubscriber(subscriberInfo)
39 {}
40 
41 std::shared_ptr<AccountSubscriber> AccountSubscriber_ = nullptr;
Subscriber(void)42 bool AccountSubscriber::Subscriber(void)
43 {
44     if (AccountSubscriber_ == nullptr) {
45         EventFwk::MatchingSkills matchingSkills;
46         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
47         matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
48         EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
49         AccountSubscriber_ = std::make_shared<AccountSubscriber>(subscribeInfo);
50         EventFwk::CommonEventManager::SubscribeCommonEvent(AccountSubscriber_);
51     }
52     return true;
53 }
54 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)55 void AccountSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
56 {
57     const AAFwk::Want& want = eventData.GetWant();
58     std::string action = want.GetAction();
59     int32_t userId = eventData.GetCode();
60     LOGI("StorageManager: OnReceiveEvent action:%{public}s.", action.c_str());
61 
62     std::unique_lock<std::mutex> lock(mutex_);
63     /* get user status */
64     uint32_t status = 0;
65     auto entry = userRecord_.find(userId);
66     if (entry != userRecord_.end()) {
67         status = entry->second;
68     }
69 
70     /* update status */
71     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
72         status |= 1 << USER_UNLOCK_BIT;
73     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
74         status |= 1 << USER_SWITCH_BIT;
75         /* clear previous user status */
76         auto oldEntry = userRecord_.find(userId_);
77         if (oldEntry != userRecord_.end()) {
78             userRecord_[userId_] = oldEntry->second & (~USER_SWITCH_BIT);
79         }
80     }
81     userId_ = userId;
82     userRecord_[userId] = status;
83 
84     LOGI("userId %{public}d, status %{public}d", userId, status);
85     if (status != (1 << USER_UNLOCK_BIT | 1 << USER_SWITCH_BIT)) {
86         return;
87     }
88     lock.unlock();
89 
90     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
91     if (sam == nullptr) {
92         LOGE("GetSystemAbilityManager sam == nullptr");
93         return;
94     }
95     auto remoteObj = sam->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
96     if (remoteObj == nullptr) {
97         LOGE("GetSystemAbility remoteObj == nullptr");
98         return;
99     }
100 
101     LOGI("connect %{public}d media library", userId);
102     mediaShare_ = DataShare::DataShareHelper::Creator(remoteObj, "datashare:///media");
103 }
104 
OnRemoteDied(const wptr<IRemoteObject> & object)105 void MediaShareDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
106 {
107     auto sptr = DataShare::DataShareHelper::Creator(object.promote(), "datashare:///media");
108     AccountSubscriber::SetMediaShare(sptr);
109 }
110 }  // namespace StorageManager
111 }  // namespace OHOS
112