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