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 commonEvent from '@ohos.commonEvent'; 17import Log from '../../../../../../../common/src/main/ets/default/Log'; 18 19const TAG = 'CapsuleModel'; 20 21let commonEventSubscribeInfo = { 22 events: ['CAPSULE_EVENT_CALL_UI'], 23 publisherPermission: "ohos.permission.GET_TELEPHONY_STATE" 24}; 25let commonEventSubscriber = null; 26 27export class CapsuleModel { 28 mCallback: any; 29 30 registerCallback(callback) { 31 Log.showInfo(TAG, 'registerCallback'); 32 this.mCallback = callback; 33 if (commonEventSubscriber == null) { 34 commonEvent.createSubscriber( 35 commonEventSubscribeInfo, 36 this.createCapsuleSubscriberCallBack.bind(this) 37 ); 38 } 39 } 40 41 createCapsuleSubscriberCallBack(err, data) { 42 commonEventSubscriber = data; 43 commonEvent.subscribe(commonEventSubscriber, (err, data) => { 44 Log.showInfo(TAG, `createCapsuleSubscriberCallBack err: ${err.code}`); 45 if (err.code == 0) { 46 const processingData = JSON.parse(data.data); 47 if (processingData) { 48 Log.showInfo(TAG, `createCapsuleSubscriberCallBack processingData: ${JSON.stringify(processingData)} `); 49 this.mCallback.onStateChange(processingData); 50 } 51 } else { 52 Log.showError(TAG, 'date is err'); 53 } 54 }); 55 } 56 57 unregisterCallback() { 58 if (commonEventSubscriber != null) { 59 commonEvent.unsubscribe(commonEventSubscriber, () => { 60 Log.showInfo(TAG, 'Subscriberregister unregister Capsule Status Listener ==============='); 61 }); 62 } 63 } 64} 65 66let mCapsuleModel = new CapsuleModel(); 67 68export default mCapsuleModel as CapsuleModel;