• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-2023 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// @ts-nocheck
17import { PrintJobState, PrintJobSubState } from '@ohos/common';
18import { Log } from '@ohos/common';
19import { StringUtil } from '@ohos/common';
20import {Constants, AppCommonEvent } from '@ohos/common';
21import { GlobalThisHelper, GlobalThisStorageKey} from '@ohos/common';
22import CheckEmptyUtils from '@ohos/common';
23import emitter from '@ohos.events.emitter';
24import PrintAdapter from '../Common/Adapter/PrintAdapter';
25import common from '@ohos.app.ability.common';
26import { PrintJob }  from '../Model/PrintJob';
27
28const TAG = '[JobManagerPage]'
29
30let jobArray = new Array<PrintJob>()
31
32@Extend(Text) function statusStyle(jobState:number, jobSubState:number){
33  .fontSize($r('app.float.font_size_body2'))
34  .fontColor(adjustFontColor(jobState, jobSubState))
35  .fontWeight(FontWeight.Medium)
36  .textOverflow({ overflow: TextOverflow.Ellipsis })
37}
38function adjustFontColor(jobState:number, jobSubState:number){
39  Log.info(TAG,'adjustFontColor,jobState: '+jobState + ',jobSubState:' + jobSubState)
40  let color = $r('app.color.text_jobState_running')
41  if(jobState === PrintJobState.PRINT_JOB_BLOCKED){
42    if (jobSubState === PrintJobSubState.PRINT_JOB_BLOCK_FILE_CONVERT_SLOWLY) {
43      color = $r('app.color.text_jobState_completed')
44    } else {
45      color = $r('app.color.text_jobState_blocked')
46    }
47  }else if(jobState === PrintJobState.PRINT_JOB_COMPLETED){
48    color = $r('app.color.text_jobState_completed')
49  }
50  return color
51}
52
53@Entry
54@Component
55struct JobManagerPage {
56  @State message: string = '当前没有任务';
57  @Provide('IsBlocked') isBlocked: boolean = false
58  @Provide('BlockedReason') blockedReason: string = Constants.STRING_NONE;
59  @Provide('Context') abilityContext : common.UIAbilityContext = undefined;
60  @Provide('PrintAdapter') adapter:PrintAdapter = undefined;
61  @StorageLink('JobQueue') jobQueue: Array<PrintJob> = new Array();
62  private jobId:string = Constants.STRING_NONE;
63  build() {
64    Column(){
65      Column(){
66        Text($r('app.string.print_job')).key('JobManagerPage_Text_printJob')
67          .fontSize($r('app.float.font_size_headline8'))
68          .fontWeight(FontWeight.Medium)
69          .maxLines(Constants.NUMBER_1)
70          .textOverflow({overflow:TextOverflow.Ellipsis})
71      }
72      .justifyContent(FlexAlign.Center)
73      .alignItems(HorizontalAlign.Start)
74      .width($r('app.float.print_job_comp_width'))
75      .height($r('app.float.print_job_title_height'))
76      .margin({left:$r('app.float.print_job_margin_left'),right:$r('app.float.print_job_margin_right')})
77      Row(){
78        Image($r('app.media.ic_printer_tips')).key('JobManagerPage_Image_printerTips')
79          .width($r('app.float.image_comp_width'))
80          .height($r('app.float.image_comp_height'))
81          .margin({right:$r('app.float.print_job_tips_image_margin_right')})
82        Text(this.blockedReason).key('JobManagerPage_Text_blockedReason')
83          .fontSize($r('app.float.font_size_body2'))
84          .fontColor($r('app.color.text_block_reason'))
85          .fontWeight(FontWeight.Regular)
86      }
87      .width($r('app.float.print_job_comp_width'))
88      .margin({left:$r('app.float.print_job_margin_left'),right:$r('app.float.print_job_margin_right')})
89      .visibility(this.isBlocked?Visibility.Visible:Visibility.None)
90
91      Row() {
92        List() {
93          ForEach(this.jobQueue, (printJob:PrintJob)=>{
94            ListItem(){
95              printJobComponent({ mPrintJob: printJob});
96            }.key(`JobManagerPage_ListItem_${printJob.jobId}`)
97          }, printJob=>printJob.jobId)
98        }
99        .key('JobManagerPage_List_jobQueue')
100        .edgeEffect(EdgeEffect.Spring)
101        .divider({ strokeWidth: $r('app.float.print_job_item_divider_strokeWidth')
102        , color: $r('app.color.divider_color'), startMargin: $r('app.float.print_job_item_divider_startMargin')})
103      }
104      .width($r('app.float.print_job_comp_width'))
105      .height($r('app.float.print_job_height'))
106      .margin({left:$r('app.float.print_job_margin_left'),right:$r('app.float.print_job_margin_right')})
107      .alignItems(VerticalAlign.Top)
108      Button($r('app.string.JobManagerPage_ok')).key('JobManagerPage_Button_confirmed')
109        .fontColor($r('app.color.button_cancel_text'))
110        .backgroundColor($r('app.color.button_color_cancel'))
111        .width($r('app.float.print_job_button_width'))
112        .height($r('app.float.print_job_button_height'))
113        .margin({top:$r('app.float.print_job_button_margin_top'),bottom:$r('app.float.print_job_button_margin_bottom')})
114        .onClick(()=>{
115          this.abilityContext.terminateSelf().then((data) => {
116            console.info('===>terminateSelfCallBack===>: '+data);
117          });
118        })
119    }
120    .width('100%')
121    .height('100%')
122    .backgroundColor(Color.White)
123    .alignItems(HorizontalAlign.Center)
124
125
126  }
127
128  aboutToAppear() {
129
130    this.abilityContext = GlobalThisHelper.getValue<common.UIAbilityContext>(GlobalThisStorageKey.KEY_JOB_MANAGER_ABILITY_CONTEXT)
131    let data = {
132      wantJobId : Constants.STRING_NONE
133    }
134    this.abilityContext.eventHub.emit(Constants.EVENT_GET_ABILITY_DATA, data);
135    Log.info(TAG, 'aboutToAppear jobId: ' + JSON.stringify(data.wantJobId) );
136    this.jobId = data.wantJobId;
137    this.adapter = PrintAdapter.getInstance();
138    this.adapter.getPrintJobCtl().createPrintJob(this.jobId)
139    this.subscribe();
140  }
141
142  aboutToDisappear() {
143    this.unsubscribe();
144  }
145
146  private subscribe() {
147    var innerEventState = { eventId: AppCommonEvent.TERMINATE_JOB_MANAGER_ABILITY_EVENT }
148    emitter.on(innerEventState, (eventData) => {
149      Log.info(TAG, 'emitter TERMINATE_JOB_MANAGER_ABILITY_EVENT');
150      this.abilityContext.terminateSelf().then((data) => {
151        console.info('===>terminateSelfCallBack===>: '+data);
152      });
153    })
154  }
155
156  private unsubscribe() {
157    emitter.off(AppCommonEvent.TERMINATE_JOB_MANAGER_ABILITY_EVENT)
158  }
159
160}
161
162@Component
163struct printJobComponent {
164  @ObjectLink @Watch('checkBlocked') mPrintJob: PrintJob;
165  @State errorFlag: boolean = false;
166  @State deleteBackgroundColor:Resource = $r('app.color.effect_color_none');
167  @Consume('IsBlocked') isBlocked: boolean;
168  @Consume('BlockedReason') blockedReason: string;
169  @Consume('Context') context : common.UIAbilityContext
170
171  @Consume('PrintAdapter') adapter:PrintAdapter;
172  private opt : any = undefined
173  private jobNum : number = Constants.NUMBER_1
174
175  build(){
176    Row() {
177      Stack(){
178        Image(this.jobNum>Constants.NUMBER_1?$r('app.media.ic_two'):$r('app.media.ic_one'))
179          .width($r('app.float.print_job_item_icon_width'))
180          .height($r('app.float.print_job_item_icon_height'))
181          .margin({left: $r('app.float.print_job_item_icon_margin_left'), right: $r('app.float.print_job_item_icon_margin_right')})
182        Image($r('app.media.ic_job_tips'))
183          .width($r('app.float.print_job_item_tips_width'))
184          .height($r('app.float.print_job_item_tips_height'))
185          .offset({x:$r('app.float.print_job_item_tips_offset_x'),y:$r('app.float.print_job_item_tips_offset_y')})
186          .visibility(this.mPrintJob.jobState === PrintJobState.PRINT_JOB_BLOCKED?Visibility.Visible:Visibility.None)
187      }
188      Column() {
189        Text(this.opt===null ? Constants.STRING_NONE : this.opt.jobName)
190          .fontSize($r('app.float.font_size_body1'))
191          .fontWeight(FontWeight.Medium)
192          .textOverflow({ overflow: TextOverflow.Ellipsis })
193        Text(this.opt===null ? Constants.STRING_NONE : this.opt.jobDescription)
194          .fontSize($r('app.float.font_size_body2'))
195          .fontColor($r('app.color.text_jobDes'))
196          .fontWeight(FontWeight.Lighter)
197          .textOverflow({ overflow: TextOverflow.Ellipsis })
198          .margin({top:$r('app.float.print_job_item_des_margin_top'),bottom:$r('app.float.print_job_item_des_margin_bottom')});
199        Text(this.getStateStr()).statusStyle(this.mPrintJob.jobState, this.mPrintJob.jobSubState)
200      }
201      .width($r('app.float.print_job_item_des_width'))
202      .alignItems(HorizontalAlign.Start)
203      Button({ type: ButtonType.Normal, stateEffect: true }) {
204        Image($r('app.media.ic_delete')).width($r('app.float.image_comp_width')).height($r('app.float.image_comp_height'))
205      }
206      .width($r('app.float.print_job_delete_button_width'))
207      .height($r('app.float.print_job_delete_button_height'))
208      .backgroundColor(this.deleteBackgroundColor)
209      .borderRadius($r('app.float.radius_s'))
210      .margin({left:$r('app.float.print_job_delete_button_margin_left')})
211      .visibility(this.isHideDeleteBtn(this.mPrintJob)? Visibility.None:Visibility.Visible)
212      .onHover((isHover: boolean) => {
213        if (isHover) {
214          this.deleteBackgroundColor =  $r('app.color.effect_color_hover')
215        } else {
216          this.deleteBackgroundColor =  $r('app.color.effect_color_none')
217        }
218      })
219      .onTouch((event: TouchEvent) => {
220        if (event.type === TouchType.Down) {
221          this.deleteBackgroundColor =  $r('app.color.effect_color_press')
222        }
223        if (event.type === TouchType.Up) {
224          this.deleteBackgroundColor =  $r('app.color.effect_color_none')
225        }
226
227      })
228      .onClick(()=>{
229        this.adapter.getPrintJobCtl().cancelPrintJob(this.mPrintJob.jobId)
230      })
231    }
232    .width($r('app.float.print_job_comp_width'))
233    .height($r('app.float.print_job_item_height'))
234
235  }
236
237  private isHideDeleteBtn(printJob: PrintJob): boolean {
238    if (printJob.jobState === PrintJobState.PRINT_JOB_COMPLETED) {
239      return true;
240    }
241    return false;
242  }
243
244  aboutToAppear() {
245    Log.info(TAG, 'aboutToAppear')
246    this.opt = JSON.parse(this.mPrintJob.option)
247    if (!CheckEmptyUtils.isEmpty(this.opt)) {
248      this.jobNum =this.opt.jobNum
249    }
250    this.checkBlocked()
251  }
252
253  onPageShow(){
254    Log.info(TAG, 'onPageShow')
255  }
256
257  getJobName(){
258    let jobName = Constants.STRING_NONE
259    if (!CheckEmptyUtils.isEmpty(this.opt)) {
260      jobName = this.opt.jobName
261    }
262    return jobName
263  }
264
265  getStateStr(){
266    Log.info(TAG, 'getStateStr , jobState =  ', this.mPrintJob.jobState );
267    let name = Constants.STRING_NONE
268    if(this.mPrintJob.jobState === PrintJobState.PRINT_JOB_PREPARED) {
269      name = 'print_job_created'
270    } else if(this.mPrintJob.jobState === PrintJobState.PRINT_JOB_QUEUED) {
271      name = 'print_job_queued'
272    } else if(this.mPrintJob.jobState === PrintJobState.PRINT_JOB_RUNNING) {
273      name = 'print_job_printing'
274    }else if(this.mPrintJob.jobState === PrintJobState.PRINT_JOB_COMPLETED) {
275      if (this.mPrintJob.jobSubState === PrintJobSubState.PRINT_JOB_COMPLETED_SUCCESS) {
276        name = 'print_job_completed'
277      }else if(this.mPrintJob.jobSubState ===PrintJobSubState.PRINT_JOB_COMPLETED_FAILED){
278        name = 'print_job_failed'
279      }else if(this.mPrintJob.jobSubState ===PrintJobSubState.PRINT_JOB_COMPLETED_CANCELLED){
280        name = 'print_job_cancelled'
281      }
282    } else if(this.mPrintJob.jobState === PrintJobState.PRINT_JOB_CANCELLING) {
283      name = 'print_job_cancelling'
284    } else {
285      name = 'print_job_blocked'
286    }
287    return StringUtil.getString(name)
288  }
289
290  checkBlocked(){
291    if (this.mPrintJob.jobState === undefined) {
292      return
293    }
294    if (this.mPrintJob.jobState === PrintJobState.PRINT_JOB_BLOCKED) {
295      this.isBlocked = true
296      let subState = this.mPrintJob.jobSubState
297      if (subState === PrintJobSubState.PRINT_JOB_BLOCK_DOOR_OPEN) {
298        this.blockedReason = StringUtil.getString('printer_door_open')
299      } else if (subState === PrintJobSubState.PRINT_JOB_BLOCK_BUSY) {
300        this.blockedReason = StringUtil.getString('printer_busy')
301      } else if (subState === PrintJobSubState.PRINT_JOB_BLOCK_JAMMED) {
302        this.blockedReason = StringUtil.getString('printer_jammed')
303      } else if (subState === PrintJobSubState.PRINT_JOB_BLOCK_LOW_ON_INK) {
304        this.blockedReason = StringUtil.getString('printer_low_on_ink')
305      } else if (subState === PrintJobSubState.PRINT_JOB_BLOCK_OFFLINE) {
306        this.blockedReason = StringUtil.getString('printer_offline')
307      } else if (subState === PrintJobSubState.PRINT_JOB_BLOCK_LOW_ON_TONER) {
308        this.blockedReason = StringUtil.getString('printer_low_on_toner')
309      } else if (subState === PrintJobSubState.PRINT_JOB_BLOCK_OUT_OF_INK) {
310        this.blockedReason = StringUtil.getString('printer_out_of_ink')
311      } else if (subState === PrintJobSubState.PRINT_JOB_BLOCK_OUT_OF_PAPER) {
312        this.blockedReason = StringUtil.getString('printer_out_of_paper')
313      } else if (subState === PrintJobSubState.PRINT_JOB_BLOCK_OUT_OF_TONER) {
314        this.blockedReason = StringUtil.getString('printer_out_of_toner')
315      } else if (subState === PrintJobSubState.PRINT_JOB_BLOCK_REALLY_LOW_ON_INK) {
316        this.blockedReason = StringUtil.getString('printer_out_of_ink')
317      } else if (subState === PrintJobSubState.PRINT_JOB_BLOCK_SERVICE_REQUEST) {
318        this.blockedReason = StringUtil.getString('printer_check')
319      } else if (subState === PrintJobSubState.PRINT_JOB_BLOCK_BAD_CERTIFICATE) {
320        this.blockedReason = StringUtil.getString('printer_bad_certificate')
321      }
322      Log.debug(TAG,'checkBlocked: blockedReason = '+this.blockedReason)
323    }else{
324      this.isBlocked = false
325      Log.info(TAG,'checkBlocked: not blocked jobState = '+this.mPrintJob.jobState)
326    }
327  }
328
329}
330
331/**
332 * Block Tips
333 */
334export interface BlockTips {
335  isJobBlocked:boolean;
336  blockedReason:string;
337  textColor:Resource;
338  imageRes:Resource;
339}