• 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 router from '@ohos.router'
17import sms from '@ohos.telephony.sms'
18import Contact from '../model/Contact'
19import Message from '../model/Message'
20import Logger from '../model/Logger'
21import SmsModel from '../model/SmsModel'
22import DateTimeUtil from '../model/DateTimeUtil'
23import {MessageSource} from "../model/DataSource"
24
25const TAG: string = '[SendMessage]'
26const NO_SIM_STATE: number = -1
27
28@Entry
29@Component
30struct SendMessage {
31  @State clickable: boolean = false
32  @State sendResult: string = ''
33  @State serviceCenter: string = ''
34  @State deliveryResult: string = ''
35  @State content: string = ''
36  @State messages: MessageSource = new MessageSource([])
37  private slotId: number = 0
38  private destinationPort: number= 1000
39  private contact: Contact
40  private smsModel: SmsModel = new SmsModel()
41  private dateTimeUtil: DateTimeUtil = new DateTimeUtil()
42
43  aboutToAppear() {
44    this.contact = <Contact> router.getParams()['contact']
45    Logger.info(`${TAG}, aboutToAppear name = ${this.contact.name} destinationHost = ${this.contact.destinationHost}`)
46    this.getData()
47  }
48
49  async getData() {
50    Logger.info(`${TAG}, getData start`)
51    sms.getDefaultSmsSlotId((err, data) => {
52      Logger.info(`${TAG}, getData getDefaultSmsSlotId: err = ${JSON.stringify(err)} data = ${JSON.stringify(data)}`)
53      if (err !== undefined) {
54        this.clickable = false
55        if (err.code === NO_SIM_STATE) {
56          this.showDialog()
57        }
58      } else {
59        this.clickable = true
60        this.slotId = data
61        sms.getSmscAddr(this.slotId, (err, data) => {
62          this.serviceCenter = data
63          Logger.info(`${TAG}, getData getSmscAddr: err = ${JSON.stringify(err)} data = ${JSON.stringify(data)}`)
64        })
65      }
66      Logger.info(`${TAG}, getData getDefaultSmsSlotId: clickable = ${this.clickable}`)
67    })
68
69    this.smsModel.createMessage()
70    Logger.info(`${TAG}, getData end`)
71  }
72
73  showDialog() {
74    AlertDialog.show(
75      {
76        title: $r('app.string.no_sim'),
77        message: $r('app.string.insert_sim'),
78        confirm: {
79          value: $r('app.string.determine'),
80          action: () => {
81            Logger.info(`${TAG}, insert sim AlertDialog confirm`)
82          }
83        }
84      }
85    )
86  }
87
88  sendMessage() {
89    this.smsModel.sendMessage(this.slotId, this.content, this.contact.destinationHost, this.serviceCenter, this.destinationPort,
90      (err, data) => {
91        Logger.info(`${TAG}, sendCallback--- data = ${JSON.stringify(data)} err = ${JSON.stringify(err)}`)
92        if (err !== undefined) {
93          this.sendResult = 'error'
94          Logger.info(`${TAG}, sendCallback--- error`)
95        } else {
96          if (data.result === sms.SendSmsResult.SEND_SMS_SUCCESS) {
97            this.sendResult = ''
98            Logger.info(`${TAG}, sendCallback--- success`)
99          } else {
100            this.sendResult = 'fail'
101            Logger.info(`${TAG}, sendCallback--- fail`)
102          }
103        }
104        this.messages.pushData(new Message(this.content, this.sendResult))
105        this.content = ''
106        Logger.info(`${TAG}, sendCallback--- push message`)
107      },
108      (err, data) => {
109        Logger.info(`${TAG}, deliveryCallback--- data = ${JSON.stringify(data)} err = ${JSON.stringify(err)}`)
110        if (err !== undefined) {
111          this.sendResult = 'error'
112          Logger.info(`${TAG}, deliveryCallback--- error`)
113        } else {
114          this.sendResult = ''
115          Logger.info(`${TAG}, deliveryCallback--- success`)
116        }
117      })
118    Logger.info(`${TAG}, onClick sendMessage done ${JSON.stringify(this.messages['MessageData'])}`)
119  }
120
121  build() {
122    Column() {
123      Row() {
124        Image($r('app.media.back'))
125          .width(150)
126          .height(50)
127          .margin({ left: 20 })
128          .objectFit(ImageFit.Contain)
129          .onClick(() => {
130            router.back()
131          })
132        Column() {
133          Text(`${this.contact.name}`)
134            .height(40)
135            .fontSize(22)
136            .fontSize(28)
137            .fontWeight(FontWeight.Bold)
138            .textAlign(TextAlign.Center)
139            .margin({ right: 50 })
140          Text(`${this.contact.destinationHost}`)
141            .height(20)
142            .fontSize(20)
143            .fontColor(Color.Gray)
144            .textAlign(TextAlign.Center)
145            .margin({ right: 50 })
146        }
147        .layoutWeight(1)
148
149      }
150      .height(70)
151      .margin({ bottom: 10 })
152
153      Scroll() {
154        Column() {
155          LazyForEach(this.messages, (item) => {
156            Row() {
157              Text(`${item.sendResult}`)
158                .fontSize(20)
159                .fontColor(Color.Red)
160                .margin({ top: 20, right: 10 })
161                .textAlign(TextAlign.Center)
162                .textOverflow({ overflow: TextOverflow.None })
163              Column() {
164                Text(`${this.dateTimeUtil.getDate()}  ${this.dateTimeUtil.getTime()}`)
165                  .width('70%')
166                  .fontSize(20)
167                  .textAlign(TextAlign.End)
168                  .margin({ top: 20 })
169                Text(`${item.content}`)
170                  .width('70%')
171                  .fontSize(25)
172                  .padding({ top: 20, bottom: 20, left: 20 })
173                  .borderRadius(20)
174                  .backgroundColor('#6bcfcdce')
175                  .textOverflow({ overflow: TextOverflow.None })
176              }
177              .alignSelf(ItemAlign.End)
178            }
179          }, item => item.content)
180        }
181      }
182      .height('70%')
183      .width('100%')
184      .layoutWeight(1)
185
186
187      Row() {
188        TextArea({ placeholder: $r('app.string.message'), text: this.content })
189          .placeholderColor('#92958C')
190          .placeholderFont({ size: 20, weight: 100, family: 'cursive', style: FontStyle.Normal })
191          .textAlign(TextAlign.Start)
192          .width('80%')
193          .height(50)
194          .fontSize(20)
195          .margin({ left: 10 })
196          .fontWeight(FontWeight.Bold)
197          .fontFamily('sans-serif')
198          .onChange((value: string) => {
199            this.content = value
200          })
201        Image($r('app.media.arrow'))
202          .width(50)
203          .height(50)
204          .margin({ right: 10 })
205          .objectFit(ImageFit.Contain)
206          .onClick(() => {
207            Logger.info(`${TAG}, onClick sendMessage`)
208            if (this.content === '') {
209              Logger.info(`${TAG}, onClick this.content===''`)
210              AlertDialog.show(
211                {
212                  title: $r('app.string.no_message'),
213                  message: $r('app.string.input_message'),
214                  confirm: {
215                    value: $r('app.string.determine'),
216                    action: () => {
217                      Logger.info(`${TAG}, input message AlertDialog confirm`)
218                    }
219                  }
220                }
221              )
222            } else {
223              if (this.clickable) {
224                this.sendMessage()
225              } else {
226                this.showDialog()
227              }
228            }
229          })
230      }
231      .height(60)
232    }
233    .width('100%')
234    .height('100%')
235  }
236}