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 { PhoneNumber } from '../../../../../../feature/phonenumber';
17import { CallType } from '../../../../../../feature/call/src/main/ets/entity/CallLog';
18import { HiLog } from '../../../../../../common/src/main/ets/util/HiLog';
19import EnvironmentProp from '../../feature/EnvironmentProp';
20import promptAction from '@ohos.promptAction'
21
22const TAG = 'ContactDetail-calllog';
23
24/**
25 * Call log
26 */
27@Component
28export default struct CallLogListItem {
29  @State message: { [key: string]: any } = {};
30  @State isEmergencyNum: boolean = false;
31  private imgRes: Resource;
32  @StorageLink('haveMultiSimCard') haveMultiSimCard: boolean = false;
33  @StorageLink('haveSimCard') haveSimCard: boolean = false;
34  @Link private mPresenter: { [key: string]: any };
35  private simImgRes: Resource = $r('app.media.stat_sys_sim1');
36
37  aboutToAppear() {
38    switch (this.message.callType) {
39      case 1:
40        this.imgRes = $r('app.media.ic_contacts_call_in_mini');
41        break;
42      case 2:
43        this.imgRes = $r('app.media.ic_contacts_callout_mini');
44        break;
45      case 3:
46        this.imgRes = $r('app.media.ic_contacts_call_missed_mini');
47        break;
48      case 5:
49        this.imgRes = $r('app.media.ic_contacts_call_rejected_mini');
50        break;
51    }
52    if (this.haveMultiSimCard) {
53      if (this.message.simId == 0) {
54        this.simImgRes = $r('app.media.stat_sys_sim1')
55      } else {
56        this.simImgRes = $r('app.media.stat_sys_sim2')
57      }
58    }
59  }
60
61  build() {
62    Row() {
63      Flex({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Start, }) {
64        Row() {
65          Text(this.message.dateDetail)
66            .fontWeight(FontWeight.Medium)
67            .fontSize($r('sys.float.ohos_id_text_size_body1'))
68            .fontColor($r('sys.color.ohos_id_color_text_primary'))
69          Text(this.message.timeDetail)
70            .fontWeight(FontWeight.Medium)
71            .fontSize($r('sys.float.ohos_id_text_size_body1'))
72            .fontColor($r('sys.color.ohos_id_color_text_primary'))
73        }
74        .height('22vp')
75
76        Row() {
77          Image(this.imgRes)
78            .objectFit(ImageFit.Fill)
79            .width($r('app.float.id_card_image_xs'))
80            .height($r('app.float.id_card_image_xs'))
81            .opacity(0.4)
82          Image(this.simImgRes)
83            .objectFit(ImageFit.Fill)
84            .width($r('app.float.id_card_image_xs'))
85            .height($r('app.float.id_card_image_xs'))
86            .opacity(0.4)
87            .visibility(this.haveMultiSimCard ? Visibility.Visible : Visibility.None)
88          Text(this.message.formatNumber)
89            .fontSize($r('sys.float.ohos_id_text_size_body2'))
90            .fontWeight(FontWeight.Regular)
91            .fontColor(this.message.callType == CallType.MISSED || this.message.callType
92            == CallType.REJECTED ? $r('sys.color.ohos_id_color_handup') : $r('sys.color.ohos_id_color_text_tertiary'))
93            .margin({ left: $r('app.float.id_card_margin_mid') })
94        }
95        .height('19vp')
96        .margin({ top: $r('app.float.id_card_margin_sm') })
97      }
98      .margin({ left: $r('app.float.id_card_margin_max') })
99
100      Blank();
101
102      Row() {
103        Text($r('app.string.Ringing'))
104          .fontWeight(FontWeight.Regular)
105          .fontSize($r('sys.float.ohos_id_text_size_body2'))
106          .fontColor($r('sys.color.ohos_id_color_text_tertiary'))
107          .visibility(this.message.callType == CallType.MISSED ? Visibility.Visible : Visibility.None)
108
109        Text(this.message.talkTime)
110          .fontWeight(FontWeight.Regular)
111          .fontSize($r('sys.float.ohos_id_text_size_body2'))
112          .fontColor($r('sys.color.ohos_id_color_text_tertiary'))
113      }
114      .margin({ right: $r('app.float.id_card_margin_max') })
115      .height('19vp')
116    }
117    .width('100%')
118    .height($r('app.float.id_item_height_max'))
119    .padding({ top: 5, bottom: 5 })
120    .onClick(() => {
121      HiLog.i(TAG, 'CallLogListItem onClick:' + JSON.stringify(this.message))
122      if (this.haveMultiSimCard) {
123        PhoneNumber.fromString(this.message.formatNumber).dial({
124          accountId: this.message.simId,
125        });
126      } else {
127        if (!this.haveSimCard) {
128          HiLog.i(TAG, 'No SIM card!');
129          //TODO Pop-up window for dialing without a SIM card
130          PhoneNumber.fromString(this.message.formatNumber).isDialEmergencyNum().then((res) => {
131            this.isEmergencyNum = res;
132            if (!this.isEmergencyNum) {
133              HiLog.i(TAG, 'Is not Emergency Phone Number!');
134              promptAction.showToast({
135                message: $r('app.string.no_simCardDailog'),
136                duration: 2000,
137                bottom:'60%'
138              });
139              return;
140            } else {
141              HiLog.i(TAG, 'No SIM card, but is Emergency Phone Number');
142              PhoneNumber.fromString(this.message.formatNumber).dial();
143            }
144          })
145        } else {
146          PhoneNumber.fromString(this.message.formatNumber).dial();
147        }
148      }
149    })
150    .gesture(LongPressGesture({ fingers: 1, repeat: false, duration: 500 })
151      .onAction((event: GestureEvent) => {
152      }))
153  }
154}
155