• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 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 { HiLog } from '../../../../../../common';
17import commonEvent from '@ohos.commonEventManager';
18import { PhoneNumber } from '../../../../../../feature/phonenumber';
19import telephonySim from '@ohos.telephony.sim';
20
21const TAG = 'MissedCallManager'
22
23class MissedCallManager {
24  requestMissedCallAction(action, data: any) {
25    if ('notification.event.message' == action) {
26      if (data && data.phoneNumber) {
27        PhoneNumber.fromString(data.phoneNumber).sendMessage();
28      }
29    } else if ('notification.event.dialBack' == action) {
30      if (data && data.phoneNumber) {
31        this.autoCall(data.phoneNumber)
32      }
33    }
34    HiLog.i(TAG, `requestMissedCallAction action: ${action}`);
35    this.cancelNotification(data);
36  }
37
38  private async autoCall(phoneNumber: string) {
39    HiLog.i(TAG, 'autoCall:')
40    let readySimCount: number = 0;
41    let readySim = -1;
42    for (let i = 0; i < telephonySim.getMaxSimCount(); i++) {
43      try {
44        const state = await telephonySim.getSimState(i);
45        if (state) {
46          if (this.isSimReady(state)) {
47            readySimCount++;
48            readySim = i;
49          }
50        }
51      } catch (err) {
52        HiLog.e(TAG, `autoCall, ${i} error: ${JSON.stringify(err.message)}`);
53      }
54    }
55    if (readySimCount == 1 && readySim != -1) {
56      PhoneNumber.fromString(phoneNumber).dial({
57        accountId: readySim,
58      })
59    }
60  }
61
62  private isSimReady(state) {
63    return state == telephonySim.SimState.SIM_STATE_READY || state == telephonySim.SimState.SIM_STATE_LOADED;
64  }
65
66  cancelNotification(data?: any) {
67    commonEvent.publish("contact.event.CANCEL_MISSED", {
68      bundleName: 'com.ohos.contacts',
69      parameters: {
70        missedCallData: data
71      }
72    }, (err) => {
73      HiLog.w(TAG, JSON.stringify(err))
74    })
75  }
76}
77
78export const missedCallManager: MissedCallManager = new MissedCallManager();