• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "work_record_statistic.h"
17 #include "common_utils.h"
18 #include "location_data_rdb_manager.h"
19 
20 namespace OHOS {
21 namespace Location {
22 std::shared_ptr<WorkRecordStatistic> WorkRecordStatistic::instance_ = nullptr;
23 std::mutex WorkRecordStatistic::workRecordStatisticMutex_;
24 
25 const uint32_t WORKING_STATE = 1;
26 const uint32_t NO_WORKING_STATE = 0;
27 
GetInstance()28 std::shared_ptr<WorkRecordStatistic> WorkRecordStatistic::GetInstance()
29 {
30     if (instance_ == nullptr) {
31         std::unique_lock<std::mutex> lock(workRecordStatisticMutex_);
32         if (instance_ == nullptr) {
33             std::shared_ptr<WorkRecordStatistic> workRecordStatistic = std::make_shared<WorkRecordStatistic>();
34             instance_ = workRecordStatistic;
35         }
36     }
37     return instance_;
38 }
39 
DestroyInstance()40 void WorkRecordStatistic::DestroyInstance()
41 {
42     std::unique_lock<std::mutex> lock(workRecordStatisticMutex_);
43     if (instance_ != nullptr) {
44         instance_ = nullptr;
45     }
46 }
47 
Update(std::string name,int requestNum)48 bool WorkRecordStatistic::Update(std::string name, int requestNum)
49 {
50     std::unique_lock<std::mutex> lock(mutex_);
51     if (name == "CacheLocation") {
52         workRecordStatisticMap_[name] = workRecordStatisticMap_[name] += requestNum;
53     } else {
54         workRecordStatisticMap_[name] = requestNum;
55     }
56     if (!UpdateLocationWorkingState()) {
57         return false;
58     }
59     return true;
60 }
61 
UpdateLocationWorkingState()62 bool WorkRecordStatistic::UpdateLocationWorkingState()
63 {
64     int requestNum = 0;
65     for (const auto& pair : workRecordStatisticMap_) {
66         requestNum += pair.second;
67     }
68 
69     if (requestNum == 1 && location_working_state_ != WORKING_STATE) {
70         location_working_state_ = WORKING_STATE;
71     } else if (requestNum == 0 && location_working_state_ != NO_WORKING_STATE) {
72         location_working_state_ = NO_WORKING_STATE;
73     }
74     return true;
75 }
76 } // namespace Location
77 } // namespace OHOS