!502 connect medialibrary when receving both user unlocked and user switch

Merge pull request !502 from jiahaoluo/lock
This commit is contained in:
openharmony_ci 2023-03-25 11:12:16 +00:00 committed by Gitee
commit 0e40dad913
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 43 additions and 1 deletions

View File

@ -44,6 +44,7 @@ bool AccountSubscriber::Subscriber(void)
if (AccountSubscriber_ == nullptr) {
EventFwk::MatchingSkills matchingSkills;
matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
AccountSubscriber_ = std::make_shared<AccountSubscriber>(subscribeInfo);
EventFwk::CommonEventManager::SubscribeCommonEvent(AccountSubscriber_);
@ -55,20 +56,49 @@ void AccountSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &eventDat
{
const AAFwk::Want& want = eventData.GetWant();
std::string action = want.GetAction();
int32_t userId = eventData.GetCode();
LOGI("StorageManager: OnReceiveEvent action:%{public}s.", action.c_str());
std::unique_lock<std::mutex> lock(mutex_);
/* get user status */
uint32_t status = 0;
auto entry = userRecord_.find(userId);
if (entry != userRecord_.end()) {
status = entry->second;
}
/* update status */
if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
status |= 1 << USER_UNLOCK_BIT;
} else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
status |= 1 << USER_SWITCH_BIT;
/* clear previous user status */
auto oldEntry = userRecord_.find(userId_);
if (oldEntry != userRecord_.end()) {
userRecord_[userId_] = oldEntry->second & (~USER_SWITCH_BIT);
}
}
userId_ = userId;
userRecord_[userId] = status;
LOGI("userId %{public}d, status %{public}d", userId, status);
if (status != (1 << USER_UNLOCK_BIT | 1 << USER_SWITCH_BIT)) {
return;
}
lock.unlock();
auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (sam == nullptr) {
LOGE("GetSystemAbilityManager sam == nullptr");
return;
}
auto remoteObj = sam->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID);
if (remoteObj == nullptr) {
LOGE("GetSystemAbility remoteObj == nullptr");
return;
}
LOGI("connect %{public}d media library", userId);
mediaShare_ = DataShare::DataShareHelper::Creator(remoteObj, "datashare:///media");
}
} // namespace StorageManager

View File

@ -15,6 +15,9 @@
#ifndef STORAGE_MANAGER_ACCOUNT_SUBSCRIBER_H
#define STORAGE_MANAGER_ACCOUNT_SUBSCRIBER_H
#include <mutex>
#include <unordered_map>
#include "common_event_manager.h"
#include "common_event_subscribe_info.h"
#include "common_event_subscriber.h"
@ -24,6 +27,11 @@
namespace OHOS {
namespace StorageManager {
enum {
USER_UNLOCK_BIT,
USER_SWITCH_BIT
};
class AccountSubscriber : public EventFwk::CommonEventSubscriber {
public:
AccountSubscriber() = default;
@ -38,6 +46,10 @@ public:
private:
static std::shared_ptr<DataShare::DataShareHelper> mediaShare_;
std::mutex mutex_;
int32_t userId_;
std::unordered_map<int32_t, uint32_t> userRecord_;
};
} // namespace StorageManager
} // namespace OHOS