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
16import type Want from '@ohos.application.Want';
17// @ts-ignore
18import PrintExtensionAbility from '@ohos.app.ability.PrintExtensionAbility';
19import {LocalDiscoverySession} from '@ohos/ippprint';
20import { P2PDiscovery } from '@ohos/ippprint';
21import { P2PMonitor } from '@ohos/ippprint';
22import { MdnsDiscovery } from '@ohos/ippprint';
23import {PrintServiceAdapter} from '@ohos/ippprint';
24import { Backend } from '@ohos/ippprint';
25import { CapabilitiesCache } from '@ohos/ippprint';
26import { Log, MediaSizeHelper } from '@ohos/common';
27import { WifiModel } from '@ohos/ippprint';
28import type { PrintJob } from '@ohos/common';
29import { SERVICE_IPP } from '@ohos/common';
30import { checkWifiEnable } from '@ohos/common';
31import { GlobalThisHelper, GlobalThisStorageKey} from '@ohos/common';
32import print from '@ohos.print';
33
34const TAG = 'PrintExtension';
35
36export default class PrintExtension extends PrintExtensionAbility {
37  private mPrintServiceAdapter: PrintServiceAdapter;
38  private mLocalDiscoverySession: LocalDiscoverySession;
39
40  onCreate(want: Want): void {
41    // @ts-ignore
42    GlobalThisHelper.createValue(this.context, GlobalThisStorageKey.KEY_ABILITY_CONTEXT);
43    // @ts-ignore
44    GlobalThisHelper.createValue<string>(<string> this.context.filesDir, GlobalThisStorageKey.KEY_FILES_DIR);
45    this.init();
46  }
47
48  /**
49   * init
50   */
51  init(): void {
52    this.mPrintServiceAdapter = <PrintServiceAdapter>PrintServiceAdapter.getInstance();
53    this.mPrintServiceAdapter.backend = <Backend> new Backend();
54    this.mPrintServiceAdapter.wifiModel = <WifiModel> new WifiModel();
55    this.mPrintServiceAdapter.capabilitiesCache = <CapabilitiesCache> new CapabilitiesCache(this.mPrintServiceAdapter);
56    this.mPrintServiceAdapter.p2pDiscovery = <P2PDiscovery> new P2PDiscovery(this.mPrintServiceAdapter);
57    this.mPrintServiceAdapter.p2pMonitor = <P2PMonitor> new P2PMonitor();
58    this.mPrintServiceAdapter.mdnsDiscovery = <MdnsDiscovery> new MdnsDiscovery(SERVICE_IPP);
59    this.mPrintServiceAdapter.localDiscoverySession = <LocalDiscoverySession> new LocalDiscoverySession(this.mPrintServiceAdapter);
60    // @ts-ignore
61    MediaSizeHelper.init(this.context);
62  }
63
64  /**
65   * start discovery printer
66   */
67  onStartDiscoverPrinter(): void {
68    Log.info(TAG, 'onStartDiscoverPrinter enter');
69    this.mLocalDiscoverySession = <LocalDiscoverySession> this.mPrintServiceAdapter.localDiscoverySession;
70    if (this.mLocalDiscoverySession === undefined) {
71      return;
72    }
73    if (!checkWifiEnable()) {
74      Log.error(TAG, 'wifi is inactive');
75      return;
76    }
77    this.mLocalDiscoverySession.startPrinterDiscovery();
78  }
79
80  /**
81   * stop discovery printer
82   */
83  onStopDiscoverPrinter(): void {
84    Log.info(TAG, 'onStopDiscoverPrinter enter');
85    if (this.mLocalDiscoverySession === undefined) {
86      Log.error(TAG, 'mLocalDiscoverySession is null');
87      return;
88    }
89    this.mLocalDiscoverySession.stopPrinterDiscovery();
90  }
91
92  /**
93   * connect to printer
94   */
95  onConnectPrinter(printerId: string | number): void {
96    Log.info(TAG, 'onConnectPrinter enter');
97    if (printerId === undefined || printerId === null) {
98      Log.error(TAG, 'printerId is undefined');
99      return;
100    }
101    if (typeof printerId === 'number') {
102      printerId = printerId.toString();
103    }
104    if (this.mLocalDiscoverySession === undefined) {
105      Log.error(TAG, 'mLocalDiscoverySession is undefined');
106      return;
107    }
108    this.mLocalDiscoverySession.startConnectPrinter(printerId);
109  }
110
111  /**
112   * disconnect to printer
113   */
114  onDisconnectPrinter(printerId: string | number): void {
115    Log.info(TAG, 'onDisconnectPrinter enter');
116    if (printerId === undefined || printerId === null) {
117      Log.error(TAG, 'printerId is undefined');
118      return;
119    }
120    if (typeof printerId === 'number') {
121      printerId = printerId.toString();
122    }
123    if (this.mLocalDiscoverySession === undefined) {
124      Log.error(TAG, 'mLocalDiscoverySession is undefined');
125      return;
126    }
127    this.mLocalDiscoverySession.stopConnectPrinter(printerId);
128  }
129
130  /**
131   * start job
132   *
133   * @param printJob
134   */
135  onStartPrintJob(printJob: PrintJob): void {
136    Log.debug(TAG, 'onStartPrintJob');
137  }
138
139  /**
140   * cancel job
141   *
142   * @param PrintJob
143   */
144  onCancelPrintJob(printJob: PrintJob): void {
145    Log.info(TAG, 'onCancelPrintJob');
146  }
147
148
149  /**
150   * request printer caps
151   *
152   * @param printerId
153   */
154  onRequestPrinterCapability(printerId: string | number): print.PrinterCapability {
155    Log.info(TAG, 'onRequestPrinterCapability enter');
156    if (printerId === undefined || printerId === null) {
157      Log.error(TAG, 'printerId is undefined');
158      return;
159    }
160    if (typeof printerId === 'number') {
161      printerId = printerId.toString();
162    }
163    if (this.mLocalDiscoverySession === undefined) {
164      Log.error(TAG, 'mLocalDiscoverySession is undefined');
165      return;
166    }
167    this.mLocalDiscoverySession.getCapabilities(printerId);
168  }
169
170  onDestroy(): void {
171    Log.info(TAG, 'onDestroy');
172  }
173}