• 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 Method from '../utils/Method';
17import FuncBtn from './FuncBtn';
18import CallStateConst from '../constant/CallStateConst';
19import CallServiceProxy from '../../model/CallServiceProxy';
20import Clone from '../utils/Clone';
21import BtnGroupConfig from '../configs/BtnGroupConfig';
22import LogUtils from '../utils/LogUtils';
23import screenLock from '@ohos.screenLock';
24import GlobalThisHelper from '../utils/GlobalThisHelper';
25import Constants from '../utils/Constants';
26
27const TAG = 'FuncBtnGroup';
28const textMap =
29  {
30    'record': $r('app.string.recording'),
31    'keep': $r('app.string.keep'),
32    'add': $r('app.string.addCall'),
33    'video': $r('app.string.videoCall'),
34    'mute': $r('app.string.mute'),
35    'contact': $r('app.string.contactPerson'),
36    'exchange': $r('app.string.exchange'),
37    'merge': $r('app.string.mergeCall'),
38  };
39
40@Component
41export default struct FuncBtnGroup {
42  @Link @Watch("updateBtnList") callData: any;
43  @Link @Watch("updateBtnList") callList: Array<any>;
44  @State count: number = 0;
45  @State btnList: Array<any> = [];
46  @State m: number = 0;
47  @State n: number = 0;
48  @State oldCallState: any = CallStateConst.CALL_STATUS_IDLE;
49  private mCallServiceProxy: CallServiceProxy;
50  private mBtnGroupConfig = BtnGroupConfig;
51  private timer: any = null;
52  private mClone: Clone;
53  private btnListCall;
54  private btnListDialing;
55  private btnListMulti;
56
57  aboutToAppear() {
58    this.mClone = Clone.getInstance()
59    this.mCallServiceProxy = CallServiceProxy.getInstance();
60    this.mBtnGroupConfig.btnGroupList.forEach((v) => {
61      v.iconText = textMap[v.type];
62    });
63    this.mBtnGroupConfig.threePartyList.forEach((v) => {
64      v.iconText = textMap[v.type];
65    });
66    this.btnListCall = this.mClone.clone(this.mBtnGroupConfig.btnGroupList);
67    this.getBtnListCall();
68    this.btnList = this.btnListCall;
69    this.getBtnListMulti();
70    this.updateBtnList();
71    LogUtils.i(TAG, "aboutToAppear :");
72  }
73
74  /**
75   * Switching BtnList Based on CallStatus
76   */
77  updateBtnList() {
78    if (this.callData.callState === CallStateConst.CALL_STATUS_DIALING || this.callData.callState === CallStateConst.CALL_STATUS_ALERTING) {
79      this.btnList = this.btnListDialing;
80    } else if (this.callList.length > 1){
81      this.btnList = this.btnListMulti;
82    } else {
83      this.btnList = this.btnListCall;
84    }
85    this.onCallStateChange(this.callData.callState);
86  }
87
88  /**
89   * Get BtnList Based on CallStatus
90   */
91  getBtnListCall() {
92    this.btnListDialing = this.mClone.clone(this.mBtnGroupConfig.btnGroupList);
93    this.btnListDialing[1].isDisable = true
94    this.btnListDialing[2].isDisable = true
95  }
96
97  /**
98   * Get BtnList for Multi Calls
99   */
100  getBtnListMulti() {
101    this.btnListMulti = this.mClone.clone(this.mBtnGroupConfig.btnGroupList);
102    this.btnListMulti[1] = this.mClone.clone(this.mBtnGroupConfig.threePartyList[0]);
103    this.btnListMulti[2] = this.mClone.clone(this.mBtnGroupConfig.threePartyList[1]);
104  }
105
106  /**
107   * update state of group buttons
108   *
109   * @param {Object} callData - call data
110   */
111  onCallStateChange(newVal) {
112    LogUtils.i(TAG, "Calling status changed newState : " + JSON.stringify(newVal));
113    const BtnName = ['video', 'record', 'add', 'contact']
114    if (newVal === CallStateConst.CALL_STATUS_ACTIVE || newVal === CallStateConst.CALL_STATUS_HOLDING) {
115      this.btnList.forEach((item) => {
116        if (!Method.includes(BtnName, (item.type))) {
117          item.isDisable = false;
118          item.isActive = false;
119        }
120      });
121      if (newVal === CallStateConst.CALL_STATUS_HOLDING) {
122        this.btnList[1].isActive = true;
123        this.btnList[4].isDisable = true;
124      }
125    } else {
126      this.btnList.forEach((item) => {
127        if (item.type === 'contact') {
128          item.isDisable = false;
129        }
130      });
131    }
132  }
133
134  /**
135   * Display the buttons of the button group
136   *
137   * @param {Object} obj - object
138   */
139  btnClick(obj) {
140    LogUtils.i(TAG, "btnClick get icon type : " + JSON.stringify(obj));
141    const BtnName = ['record', 'video', 'mute']
142    const type = obj.type;
143    const { callId } = this.callData;
144    if (Method.includes(BtnName, type)) {
145      this.btnList.forEach((item) => {
146        if (item.type === type) {
147          item.isActive = !item.isActive;
148        }
149      });
150      if (type === 'record') {
151        if (this.btnList[0].isActive) {
152        } else {
153          this.count = 0;
154          clearInterval(this.timer);
155          this.btnList[0].iconText = $r('app.string.recording');
156        }
157      }
158    }
159
160    switch (type) {
161      case 'record':
162        break;
163      case 'keep':
164        this.keepHandle('keep');
165        break;
166      case 'exchange':
167        LogUtils.i(TAG, "exchange button clicked, callid: " + callId);
168        this.callList.forEach((item) => {
169          if (item.callState === CallStateConst.CALL_STATUS_HOLDING) {
170            this.mCallServiceProxy.unHoldCall(item.callId);
171            return;
172          }
173        });
174        break;
175      case 'add':
176        this.startContact('page_flag_dialer')
177        break;
178      case 'video':
179        break;
180      case 'mute':
181        this.muteHandle('mute');
182        break;
183      case 'contact':
184        this.startContact("page_flag_choose_contacts")
185        break;
186      case 'merge':
187        this.mCallServiceProxy.combineConference(callId);
188        break;
189      default:
190        break;
191    }
192  }
193
194  startContact(pageFlag) {
195    if (screenLock.isLocked()) {
196      screenLock.unlock((err, isUnlock) => {
197        if (isUnlock) {
198          this.startContactAbility(pageFlag)
199        } else {
200          LogUtils.i(TAG, "startContact screen isLocked")
201        }
202      });
203    } else {
204        this.startContactAbility(pageFlag)
205    }
206  }
207
208  startContactAbility(pageFlag) {
209    GlobalThisHelper.get<any>(Constants.GLOBALTHIS_CONTEXT)?.startAbility({
210      bundleName: 'com.ohos.contacts',
211      abilityName: 'com.ohos.contacts.MainAbility',
212      parameters: {
213        pageFlag: pageFlag
214      }
215    });
216  }
217
218  /**
219   * Call hold interface
220   *
221   * @param {string} type - Click the hold button
222   */
223  keepHandle(type) {
224    const awaitIsActive = this.btnList.find((v) => v.type === type).isActive;
225    LogUtils.i(TAG, "keep handle awaitIsActive : " + !awaitIsActive);
226    !awaitIsActive ? this.mCallServiceProxy.holdCall(this.callData.callId) : this.mCallServiceProxy.unHoldCall(this.callData.callId);
227  }
228
229  /**
230   * Call hold mute
231   *
232   * @param {string} type - Click the hold button
233   */
234  muteHandle(type) {
235    const awaitIsActive = this.btnList.find((v) => v.type === type).isActive;
236    LogUtils.i(TAG, "mute Handle awaitIsActive : " + awaitIsActive);
237    awaitIsActive ? this.mCallServiceProxy.setMuted() : this.mCallServiceProxy.cancelMuted();
238  }
239
240  /**
241   * Clear timer
242   */
243  onDestroy() {
244    LogUtils.i(TAG, "onDestroy");
245    this.timer && clearInterval(this.timer);
246  }
247
248  build() {
249    GridRow({ columns: { sm: 4, md: 8, lg: 12 }, gutter: 0 }) {
250      GridCol({ span: { sm: 4, md: 6, lg: 6 }, offset: { md: 1, lg: 3 } }) {
251        Grid() {
252          ForEach(this.btnList, (item) => {
253            GridItem() {
254              FuncBtn({
255                btnType: item.type,
256                isDisable: item.isDisable,
257                isActive: item.isActive,
258                iconText: item.iconText,
259                iconDisableUrl: item.iconDisableUrl,
260                iconDefaultUrl: item.iconDefaultUrl,
261                iconActiveUrl: item.iconActiveUrl,
262                btnClick: () => {
263                  this.btnClick(item)
264                }
265              })
266            }
267            .height(51.5)
268          })
269        }
270        .columnsGap(24)
271        .rowsGap(29.5)
272        .height(132.5)
273        .columnsTemplate('1fr 1fr 1fr')
274        .rowsTemplate('1fr 1fr')
275      }
276    }
277    .margin(24)
278  }
279}