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 38 try { 39 geolocation.on('locationEnabledChange', (state: boolean) => { 40 Log.showInfo(TAG, `startService locationChange, state: ${JSON.stringify(state)}`); 41 this.getServiceState(); 42 }); 43 } catch(err) { 44 Log.showInfo(TAG, `on-locationEnabledChange Error:${JSON.stringify(err || {})}`); 45 } 46 } 47 48 stopService(): void { 49 if (!this.mIsStart) { 50 return; 51 }; 52 Log.showInfo(TAG, 'stopService'); 53 this.mIsStart = false; 54 55 try { 56 geolocation.off('locationEnabledChange', (state: boolean) => { 57 Log.showInfo(TAG, `stopService locationChange, state: ${JSON.stringify(state)}`) 58 }); 59 } catch(err) { 60 Log.showInfo(TAG, `off-locationEnabledChange Error:${JSON.stringify(err || {})}`); 61 } 62 } 63 64 registerListener(listener: LocationStatrListener): void { 65 Log.showInfo(TAG, `registerListener, listener: ${listener}`); 66 this.mListener = listener; 67 } 68 69 getServiceState(): void { 70 Log.showDebug(TAG, 'getServiceState'); 71 try { 72 let data = geolocation.isLocationEnabled(); 73 Log.showInfo(TAG, `getServiceState isLocationEnabled, data: ${JSON.stringify(data)}`); 74 this.mListener?.updateServiceState(data); 75 } catch(err) { 76 Log.showInfo(TAG, `getServiceState Error:${JSON.stringify(err || {})}`); 77 } 78 } 79 80 enableLocation(): void { 81 Log.showInfo(TAG, 'enableLocation'); 82 try { 83 geolocation.enableLocation() 84 .then((res) => Log.showInfo(TAG, `enableLocation, result: ${JSON.stringify(res)}`)) 85 .then(() => { 86 }).catch((err) => { 87 }); 88 } catch (error) { 89 Log.showInfo(TAG, `enableLocation, trycatch error: ${JSON.stringify(error || {})}`); 90 } 91 } 92 93 disableLocation(): void { 94 Log.showInfo(TAG, 'disableLocation'); 95 try { 96 geolocation.disableLocation(); 97 } catch (error) { 98 Log.showInfo(TAG, `disableLocation, trycatch error: ${JSON.stringify(error || {})}`); 99 } 100 } 101} 102 103let sLocationService = createOrGet(LocationService, TAG); 104 105export default sLocationService;