• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2022 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
16import geolocation from '@ohos.geoLocationManager';
17import Log from '../../../../../../../../common/src/main/ets/default/Log';
18import createOrGet from '../../../../../../../../common/src/main/ets/default/SingleInstanceHelper';
19
20const TAG = 'LocationModel';
21
22export interface LocationStatrListener {
23  updateServiceState(status: boolean): void;
24}
25
26export class LocationService {
27  mIsStart = false;
28  mListener: LocationStatrListener;
29
30  startService(): void {
31    if (this.mIsStart) {
32      return;
33    }
34    Log.showInfo(TAG, 'startService');
35    this.mIsStart = true;
36    this.getServiceState();
37    try {
38      geolocation.on('locationEnabledChange', (state: boolean) => {
39        Log.showInfo(TAG, `startService locationChange, state: ${JSON.stringify(state)}`);
40        this.getServiceState();
41      });
42    } catch {
43      Log.showError(TAG, 'geolocation.on try-catch error');
44    }
45  }
46
47  stopService(): void {
48    if (!this.mIsStart) {
49      return;
50    };
51    Log.showInfo(TAG, 'stopService');
52    this.mIsStart = false;
53    try {
54      geolocation.off('locationEnabledChange', (state: boolean) => {
55        Log.showInfo(TAG, `stopService locationChange, state: ${JSON.stringify(state)}`)
56      });
57    } catch {
58      Log.showError(TAG, 'geolocation.off try-catch error');
59    }
60  }
61
62  registerListener(listener: LocationStatrListener): void {
63    Log.showInfo(TAG, `registerListener, listener: ${listener}`);
64    this.mListener = listener;
65  }
66
67  getServiceState(): void {
68    Log.showDebug(TAG, 'getServiceState');
69    try {
70      let data = geolocation.isLocationEnabled()
71      Log.showInfo(TAG, `getServiceState isLocationEnabled, data: ${JSON.stringify(data)}`);
72      this.mListener?.updateServiceState(data);
73    } catch(err) {
74      Log.showDebug(TAG, `getServiceState Error:${JSON.stringify(err || {})}`)
75    }
76  }
77
78  enableLocation(): void {
79    Log.showInfo(TAG, 'enableLocation');
80    try {
81      geolocation.enableLocation()
82        .then((res) => Log.showInfo(TAG, `enableLocation, result: ${JSON.stringify(res)}`))
83        .then(() => {
84        }).catch((err) => {
85      });
86    } catch {
87      Log.showError(TAG, 'geolocation.enableLocation try-catch error');
88    }
89  }
90
91  disableLocation(): void {
92    Log.showInfo(TAG, 'disableLocation');
93    try {
94      geolocation.disableLocation();
95    } catch {
96      Log.showError(TAG, 'geolocation.disableLocation try-catch error');
97    }
98  }
99}
100
101let sLocationService = createOrGet(LocationService, TAG);
102
103export default sLocationService;