• 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 data management
18 */
19import CallStateManager from './CallStateManager';
20import CallStateConst from '../common/constant/CallStateConst';
21import ContactManager from './ContactManager';
22import NotificationManager from '../model/NotificationManager';
23import LogUtils from '../common/utils/LogUtils';
24import GlobalThisHelper from '../common/utils/GlobalThisHelper';
25import Constants from '../common/utils/Constants';
26
27const TAG = "CallDataManager";
28/**
29 * class CallDataManager
30 */
31export default class CallDataManager {
32  callData: any = {};
33  callList: any = [];
34  callTimeList: any = [];
35  private callStateChange: any;
36  private mCallStateManager: CallStateManager ;
37  private NotificationManager;
38  private contactManager;
39  private static sCallDataManager: CallDataManager;
40
41  public static getInstance(): CallDataManager {
42    if (!CallDataManager.sCallDataManager) {
43      CallDataManager.sCallDataManager = new CallDataManager();
44    }
45    return CallDataManager.sCallDataManager;
46  }
47
48  /**
49   * Init data.
50   */
51  public init(callData, callList, callTimeList) {
52    this.NotificationManager = new NotificationManager();
53    this.contactManager = new ContactManager();
54    this.mCallStateManager = CallStateManager.getInstance()
55    this.callData = callData;
56    this.callList = callList;
57    this.callTimeList = callTimeList;
58    this.callStateChange = (arg) => arg;
59    this.mCallStateManager = new CallStateManager(this.callData);
60  }
61
62  /**
63   * update callList and callData callTimeList
64   *`
65   * @param { object } callData
66   */
67  public update(callData) {
68    const { callState, callId } = callData;
69    const targetObj = this.callList.find((v) => v.callId === callId);
70    LogUtils.i(TAG, "update :")
71    if (targetObj) {
72      const { oldCallState } = targetObj;
73      Object.assign(targetObj, {
74        ...callData
75      });
76      if (oldCallState != callState) {
77        AppStorage.Get<NotificationManager>('notificationManager').sendCapsuleNotification(callData, GlobalThisHelper.get<boolean>(Constants.GLOBALTHIS_APPINACTIVE_STATE));
78      }
79    } else {
80      this.addCallList({
81        ...callData
82      });
83
84      // use setTimeout to avoid block ui show
85      setTimeout(() => {
86        if (this.contactManager != undefined) {
87          this.contactManager.getContactInfo(callData)
88        }
89      }, 0);
90    }
91
92    if (callData.callState === CallStateConst.CALL_STATUS_ACTIVE) {
93      this.updateCallTimeList(callData);
94    }
95    const singleCallState = callState === CallStateConst.CALL_STATUS_ACTIVE ||
96    callState === CallStateConst.CALL_STATUS_WAITING || this.callList.length === 1;
97    const multiCallState = (callState === CallStateConst.CALL_STATUS_DIALING ||
98    callState === CallStateConst.CALL_STATUS_ALERTING) && this.callList.length > 1;
99    if (singleCallState || multiCallState) {
100      this.mCallStateManager.update(callData);
101      this.callStateChange(callState);
102    }
103
104    if (callState === CallStateConst.CALL_STATUS_DISCONNECTED) {
105      if (this.callList.length === 1) {
106        this.NotificationManager.cancelNotification();
107        AppStorage.Get<NotificationManager>('notificationManager').sendCapsuleNotification(callData, true);
108        AppStorage.Delete("CallTimeList");
109        GlobalThisHelper.get<any>(Constants.GLOBALTHIS_CONTEXT)?.terminateSelf().then((data) => {
110          LogUtils.i(TAG, "calluiAbility terminateSelf");
111        });
112      } else {
113        this.removeCallById(callId);
114        const activeCallData = this.callList.find((v) => v.callState === CallStateConst.CALL_STATUS_ACTIVE);
115        if (activeCallData) {
116          this.mCallStateManager.update(activeCallData);
117          this.callStateChange(activeCallData);
118        } else if (this.callList[0]) {
119          this.mCallStateManager.update(this.callList[0]);
120          this.callStateChange(this.callList[0].callState);
121        }
122      }
123    }
124  }
125
126  /**
127   * Judge whether the call exists.
128   */
129  public hasAliveCall() {
130    const callData = this.callList.find((call) => call.callState !== CallStateConst.CALL_STATUS_DISCONNECTED
131      && call.callState !== CallStateConst.CALL_STATUS_DISCONNECTING);
132    LogUtils.i(TAG, "hasAliveCall:" + JSON.stringify(callData !== undefined));
133    return callData !== undefined;
134  }
135
136  /**
137   * Judge whether the call exists.
138   */
139  public hasActiveCall(): boolean {
140    return this.callList.find((call) => call.callState === CallStateConst.CALL_STATUS_ACTIVE
141    || call.callState === CallStateConst.CALL_STATUS_HOLDING);
142  }
143
144  /**
145   * Judge whether the call is active or holding.
146   */
147  public isActiveCall(callId): boolean {
148    return this.callList.find((call) => call.callId === callId && (call.callState === CallStateConst.CALL_STATUS_ACTIVE
149    || call.callState === CallStateConst.CALL_STATUS_HOLDING));
150  }
151
152  /**
153   * addCallList
154   *
155   * @param { object } callData
156   */
157  private addCallList(callData) {
158    this.callList.push(callData);
159  }
160
161  /**
162   * remove call by call id
163   *
164   * @param { object } callId - call id
165   */
166  private removeCallById(callId) {
167    const index = this.callList.findIndex((v) => v.callId === callId);
168    this.callList.splice(index, 1);
169    if (this.callTimeList.find((v) => v.callId === callId)) {
170      this.callTimeList.splice(index, 1);
171    }
172  }
173
174  /**
175   * update callTimeList
176   *
177   * @param { object } callData
178   */
179  updateCallTimeList(callData) {
180    const CallTimeObj = this.callTimeList.find((v) => v.callId === callData.callId);
181    LogUtils.i(TAG, "updateCallTimeList : " + JSON.stringify(CallTimeObj))
182    if (!CallTimeObj && callData.callState === CallStateConst.CALL_STATUS_ACTIVE) {
183      const obj = {
184        callId: callData.callId,
185        callTime: '00:00',
186        startTimestamp: (callData.startTime * 1000).valueOf() || new Date().valueOf(),
187        endTimestamp: 0,
188      };
189      this.callTimeList.push(obj);
190      AppStorage.SetOrCreate("CallTimeList", this.callTimeList)
191      LogUtils.i(TAG, "updateCallTimeList : " + JSON.stringify(this.callTimeList))
192    }
193  }
194}