• 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 */
15import LocationServicesModel from '../../model/locationServicesImpl/LocationServicesModel'
16import ISettingsController from '../../../../../../common/component/src/main/ets/default/controller/ISettingsController'
17import {LogAll} from '../../../../../../common/utils/src/main/ets/default/baseUtil/LogDecorator';
18import SwitchController from '../../../../../../common/component/src/main/ets/default/controller/SwitchController';
19import LogUtil from '../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil';
20import ConfigData from '../../../../../../common/utils/src/main/ets/default/baseUtil/ConfigData';
21
22@LogAll
23export default class LocationServicesController extends SwitchController {
24
25  /**
26   * Initialize data.
27   */
28  initData(): ISettingsController {
29    super.initData();
30    this.isLocationEnabled((err, value) => {
31      if (err) {
32        LogUtil.info(ConfigData.TAG + 'initData -> isLocationEnabled err:' + JSON.stringify(err));
33        this.isOn = false;
34      } else {
35        this.isOn = value;
36      }
37    })
38    return this;
39  }
40
41  /**
42   * obtain current location switch status
43   */
44  isLocationEnabled(callback) {
45    LocationServicesModel.isLocationEnabled(callback);
46  }
47
48  /**
49   * subscribe location switch changed
50   */
51  subscribe(): ISettingsController {
52    LocationServicesModel.onLocationServiceState((value) => {
53      this.isOn = value;
54    })
55    return this;
56  }
57
58  /**
59   * unsubscribe location switch changed
60   */
61  unsubscribe(): ISettingsController {
62    LocationServicesModel.offLocationServiceState((value) => {
63      this.isOn = value;
64    })
65    return this;
66  }
67
68  afterCurrentValueChanged() {
69    if (this.isOn) {
70      LocationServicesModel.enableLocation((err, data) => {
71        LogUtil.info(ConfigData.TAG + 'afterCurrentValueChanged -> enableLocation err:' + JSON.stringify(err) + ',data:' + JSON.stringify(data));
72        this.location(err, true);
73      })
74    } else {
75      LocationServicesModel.disableLocation((err, data) => {
76        LogUtil.info(ConfigData.TAG + 'afterCurrentValueChanged -> disableLocation err:' + JSON.stringify(err) + ',data:' + JSON.stringify(data));
77        this.location(err, false);
78      })
79    }
80  }
81
82  /**
83   * location switch
84   */
85  location(err, data) {
86    if (err) {
87      this.isLocationEnabled((err, value) => {
88        if (err) {
89          LogUtil.info(ConfigData.TAG + 'afterCurrentValueChanged -> isLocationEnabled err:' + JSON.stringify(err));
90          if (this.isOn) {
91            this.isOn = false;
92          }
93        } else {
94          this.isOn = value;
95        }
96      })
97    } else {
98      this.isOn = data;
99    }
100  }
101}