• 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
16/**
17 * @file: Call management
18 */
19import CallDataManager from './CallDataManager';
20import CallUtils from '../common/utils/CallUtils';
21import Utils from '../common/utils/utils';
22import CallServiceProxy from './CallServiceProxy';
23import LogUtils from '../common/utils/LogUtils'
24import call from '@ohos.telephony.call';
25import CallStateConst from '../common/constant/CallStateConst';
26import GlobalThisHelper from '../common/utils/GlobalThisHelper'
27import Constants from '../common/utils/Constants'
28
29const TAG = "CallManager";
30const TIMING = 1000;
31
32/**
33 * class CallManager
34 */
35export default class CallManager {
36  private callData: any = {};
37  private timer = null ;
38  callTimeList = [];
39  private ctx = [];
40  private sendNotificationHandle: any;
41  private mCallDataManager: CallDataManager;
42  private mCallServiceProxy: CallServiceProxy ;
43  private mUtils: Utils;
44  private diffSeconds
45  private mTimeMeter
46
47  public static getInstance(): CallManager {
48    if (GlobalThisHelper.get<any>(Constants.GLOBALTHIS_CALLMANAGER) === undefined) {
49      GlobalThisHelper.set<any>(Constants.GLOBALTHIS_CALLMANAGER, new CallManager());
50    }
51    return GlobalThisHelper.get<any>(Constants.GLOBALTHIS_CALLMANAGER);
52  }
53
54  private constructor() {
55    this.mCallServiceProxy = CallServiceProxy.getInstance();
56    this.mUtils = Utils.getInstance();
57    this.timer = null;
58    this.mCallDataManager = CallDataManager.getInstance();
59  }
60  init(ctx) {
61    this.callData = ctx.callData;
62    this.timer = null;
63    this.ctx = ctx;
64    this.callTimeList = ctx.callTimeList;
65    this.mCallDataManager?.init(ctx.callData, ctx.callList, ctx.callTimeList);
66    this.openTimer();
67    this.sendNotificationHandle = (arg) => arg;
68    this.initCallData();
69  }
70
71  /**
72   * init CallData
73   */
74  private initCallData() {
75    if (!GlobalThisHelper.get<any>(Constants.GLOBALTHIS_ABILITY_WANT)
76      && GlobalThisHelper.get<any>(Constants.GLOBALTHIS_ABILITY_WANT)?.parameters
77      && ('callState' in GlobalThisHelper.get<any>(Constants.GLOBALTHIS_ABILITY_WANT)?.parameters)) {
78      let callData = this.getCallDataFromWant(GlobalThisHelper.get<any>(Constants.GLOBALTHIS_ABILITY_WANT).parameters);
79      this.update(callData);
80      LogUtils.i(TAG, "initCallData featureAbility.getWant :")
81    } else {
82      this.mCallServiceProxy.publish({
83        key: 'getInitCallData',
84        params: []
85      });
86    }
87  }
88
89  /**
90   * get callData from want parameters
91   */
92  private getCallDataFromWant(parameters) {
93    return Object.assign({}, {
94      accountId: parameters.accountId,
95      accountNumber: parameters.accountNumber,
96      callId: parameters.callId,
97      callState: parameters.callState,
98      callType: parameters.callType,
99      conferenceState: parameters.conferenceState,
100      isEcc: parameters.isEcc,
101      startTime: parameters.startTime,
102      videoState: parameters.videoState});
103  }
104
105  /**
106   * update callData callBack
107   *
108   * @param { Object } callData -Object
109   */
110  async update(callData) {
111    LogUtils.i(TAG, "update calldata:")
112    if (this.callData != undefined && this.callData.callId === callData.callId) {
113      const { callState } = this.callData;
114      if (callState === 6) {
115        GlobalThisHelper.get<any>(Constants.GLOBALTHIS_CONTEXT)?.terminateSelf().then((data) => {
116          LogUtils.i(TAG, "calluiAbility terminateSelf because service disconnected");
117        });
118        return;
119      }
120    }
121    this.callData = callData;
122    this.mCallDataManager.update(callData);
123    call.formatPhoneNumber(callData.accountNumber, (err, data) => {
124      if (data === undefined) {
125        AppStorage.SetOrCreate("AccountNumber", callData.accountNumber)
126      } else {
127        AppStorage.SetOrCreate("AccountNumber", data)
128      }
129    });
130    CallUtils.isEmergencyPhoneNumber(callData.accountNumber)
131    LogUtils.i(TAG, "update :")
132  }
133
134  /**
135   * update call time list
136   */
137  updateCallTimeList() {
138    if (!this.mCallDataManager.hasActiveCall()) {
139      LogUtils.i(TAG, "no active calls to update");
140      return;
141    }
142
143    this.callTimeList = AppStorage.Get("CallTimeList")
144    this.callTimeList.forEach((item, i) => {
145      if (this.mCallDataManager.isActiveCall(item.callId)) {
146        item.endTimestamp = new Date().valueOf();
147        const diffSeconds = item.endTimestamp - item.startTimestamp;
148        this.diffSeconds = diffSeconds
149        item.callTime = this.mUtils.formatTime(diffSeconds);
150        this.callTimeList.splice(i, 1, {
151          ...item,
152        });
153        AppStorage.SetOrCreate("CallTimeList", this.callTimeList);
154      }
155    });
156    this.mTimeMeter = setTimeout(() => {
157      this.updateCallTimeList();
158    }, 1000 - this.diffSeconds % 1000);
159
160  }
161
162  /**
163   * open timer
164   *
165   * @param { Function } callBack - add updateCallTimeList callBack
166   */
167  openTimer() {
168    this.timer = setInterval(() => {
169      this.updateCallTimeList();
170      if (this.callData.callState === CallStateConst.CALL_STATUS_ACTIVE) {
171        clearInterval(this.timer);
172      }
173    }, TIMING);
174  }
175
176  /**
177   * clear timer
178   */
179  clearTimer() {
180    clearInterval(this.timer);
181  }
182}
183