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 "location_data_rdb_observer.h"
17
18 #include <singleton.h>
19 #include "common_utils.h"
20 #include "location_data_rdb_helper.h"
21 #include "location_log.h"
22 #include "uri.h"
23 #include "common_utils.h"
24 #include "location_data_manager.h"
25
26 namespace OHOS {
27 namespace Location {
28 using namespace AppExecFwk;
29
LocationDataRdbObserver()30 LocationDataRdbObserver::LocationDataRdbObserver()
31 {
32 }
33
34 LocationDataRdbObserver::~LocationDataRdbObserver() = default;
35
OnChange()36 void LocationDataRdbObserver::OnChange()
37 {
38 LBSLOGD(LOCATOR, "LocationDataRdbObserver::%{public}s enter", __func__);
39 HandleSwitchStateChanged();
40 }
41
HandleSwitchStateChanged()42 void LocationDataRdbObserver::HandleSwitchStateChanged()
43 {
44 auto rdbHelper = DelayedSingleton<LocationDataRdbHelper>::GetInstance();
45 auto locationDataManager = DelayedSingleton<LocationDataManager>::GetInstance();
46 if (rdbHelper == nullptr || locationDataManager == nullptr ||
47 !locationDataManager->IsSwitchStateReg()) {
48 LBSLOGE(LOCATOR, "%{public}s: param is nullptr", __func__);
49 return;
50 }
51
52 Uri locationDataEnableUri(LOCATION_DATA_URI);
53 int32_t state = DISABLED;
54 LocationErrCode errCode = rdbHelper->GetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, state);
55 if (errCode != ERRCODE_SUCCESS) {
56 LBSLOGE(LOCATOR, "%{public}s: query state failed, errcode = %{public}d", __func__, errCode);
57 return;
58 }
59 locationDataManager->SetCachedSwitchState(state);
60 locationDataManager->ReportSwitchState(state);
61 }
62 } // namespace Location
63 } // namespace OHOS