• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <singleton.h>
17 #include "location_data_handler.h"
18 #include "uri.h"
19 #include "common_utils.h"
20 #include "location_data_rdb_helper.h"
21 #include "location_data_manager.h"
22 
23 namespace OHOS {
24 namespace Location {
25 const uint32_t SWITCH_STATE_CHANGED = 1;
26 
LocationDataHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)27 LocationDataHandler::LocationDataHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner)
28     : EventHandler(runner) {}
29 
~LocationDataHandler()30 LocationDataHandler::~LocationDataHandler() {}
31 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)32 void LocationDataHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
33 {
34     if (event == nullptr) {
35         return;
36     }
37     uint32_t eventId = event->GetInnerEventId();
38     switch (eventId) {
39         case SWITCH_STATE_CHANGED: {
40             HandleSwitchStateChanged(event);
41             break;
42         }
43         default: {
44             break;
45         }
46     }
47 }
48 
HandleSwitchStateChanged(const AppExecFwk::InnerEvent::Pointer & event)49 void LocationDataHandler::HandleSwitchStateChanged(const AppExecFwk::InnerEvent::Pointer &event)
50 {
51     auto rdbHelper = DelayedSingleton<LocationDataRdbHelper>::GetInstance();
52     auto locationDataManager = DelayedSingleton<LocationDataManager>::GetInstance();
53     if (event == nullptr || rdbHelper == nullptr || locationDataManager == nullptr) {
54         LBSLOGE(LOCATOR_STANDARD, "%{public}s: param is nullptr", __func__);
55         return;
56     }
57 
58     Uri locationDataEnableUri(LOCATION_DATA_URI);
59     int32_t state = DISABLED;
60     rdbHelper->GetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, state);
61     int64_t value = event->GetParam();
62     if (state != value) {
63         rdbHelper->SetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, state);
64     }
65     // cache the value
66     locationDataManager->SetCachedSwitchState(state);
67     locationDataManager->ReportSwitchState(state);
68 }
69 } // namespace Location
70 } // namespace OHOS
71