1/* 2 * Copyright (c) 2025 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 { BusinessError } from '@ohos.base' 17import { AsyncCallback } from '@ohos.base'; 18import Want from '@ohos.app.ability.Want'; 19import image from '@ohos.multimedia.image'; 20 21class Cleaner { 22 static callback(cleaner: Cleaner): void { 23 console.println("enter Cleaner.callback. "); 24 cleaner.clean() 25 } 26 27 constructor(targetPtr: long) { 28 this.targetPtr = targetPtr 29 } 30 31 native clean(): void 32 33 private targetPtr: long = 0 34} 35 36class FinalizationAgent<T extends Object> { 37 constructor(obj: T, ptr: long) { 38 this.register(obj, ptr); 39 } 40 41 register(obj: T, ptr: long): void { 42 this.unregisterToken = {}; 43 this.cleaner = new Cleaner(ptr); 44 finalizer.register(obj, this.cleaner!, this.unregisterToken); 45 } 46 47 unregister(): void { 48 finalizer.unregister(this.unregisterToken); 49 } 50 51 private cleaner: Cleaner | null = null; 52 private unregisterToken: object = {}; 53} 54 55let finalizer = new FinalizationRegistry<Cleaner>(Cleaner.callback) 56 57export namespace pasteboard { 58 59 loadLibrary("pasteboard_ani"); 60 61 export const MIMETYPE_TEXT_URI: string = 'text/uri'; 62 export const MIMETYPE_TEXT_HTML: string = 'text/html'; 63 export const MIMETYPE_TEXT_PLAIN: string = 'text/plain'; 64 export const MIMETYPE_PIXELMAP: string = 'pixelMap'; 65 export const MIMETYPE_TEXT_WANT: string = 'text/want'; 66 export type ValueType = string | image.PixelMap | Want | ArrayBuffer; 67 68 export native function createDataTypeValue(mimeType: string, value: ValueType): PasteData; 69 export native function createDataRecord(data: Record<string, ValueType>): PasteData; 70 export native function getSystemPasteboard(): SystemPasteboard; 71 72 export function createData(mimeType: string, value: ValueType): PasteData { 73 return createDataTypeValue(mimeType, value); 74 } 75 76 export function createData(data: Record<string, ValueType>): PasteData { 77 return createDataRecord(data); 78 } 79 80 export enum ShareOption { 81 INAPP, 82 LOCALDEVICE, 83 CROSSDEVICE 84 } 85 86 export interface PasteDataProperty { 87 tag: string; 88 readonly timestamp: number; 89 shareOption: ShareOption; 90 } 91 92 export interface PasteDataRecord { 93 mimeType: string; 94 plainText: string; 95 uri: string; 96 } 97 98 export interface PasteData { 99 addRecord(record: PasteDataRecord): void; 100 addRecord(mimeType: string, value: ValueType): void; 101 getRecordCount(): number; 102 getRecord(index: number): PasteDataRecord; 103 setProperty(property: PasteDataProperty): void; 104 getProperty(): PasteDataProperty; 105 } 106 107 export interface SystemPasteboard { 108 hasDataType(mimeType: string): boolean; 109 setData(data: PasteData, callback: AsyncCallback<void>): void; 110 setData(data: PasteData): Promise<void>; 111 clearData(callback: AsyncCallback<void>): void; 112 clearData(): Promise<void>; 113 getDataSync(): PasteData; 114 getDataSource(): string; 115 } 116} 117 118export class PasteDataRecordImpl implements pasteboard.PasteDataRecord { 119 mimeType: string = ""; 120 plainText: string = ""; 121 uri: string = ""; 122} 123 124export class PasteDataPropertyImpl implements pasteboard.PasteDataProperty { 125 tag: string = ""; 126 timestamp: number = 0; 127 shareOption: pasteboard.ShareOption = pasteboard.ShareOption.CROSSDEVICE; 128} 129 130export class PasteDataImpl implements pasteboard.PasteData { 131 private nativePtr: long = 0; 132 private fzAgent: FinalizationAgent<PasteDataImpl>; 133 constructor(nativePtr: long) { 134 this.nativePtr = nativePtr; 135 this.fzAgent = new FinalizationAgent<PasteDataImpl>(this, this.nativePtr); 136 } 137 138 unregisterCleaner(): void { 139 this.fzAgent.unregister(); 140 } 141 142 getNativePtr() : long { 143 return this.nativePtr; 144 } 145 146 native addRecordByPasteDataRecord(record: pasteboard.PasteDataRecord): void; 147 native addRecordByTypeValue(mimeType: string, value: pasteboard.ValueType): void; 148 native getRecordCount(): number; 149 native getRecord(index: number): PasteDataRecordImpl; 150 native setProperty(property: pasteboard.PasteDataProperty): void; 151 native getProperty(): pasteboard.PasteDataProperty; 152 153 addRecord(record: pasteboard.PasteDataRecord): void { 154 return this.addRecordByPasteDataRecord(record); 155 } 156 157 addRecord(mimeType: string, value: pasteboard.ValueType): void { 158 return this.addRecordByTypeValue(mimeType, value); 159 } 160} 161 162export class SystemPasteboardImpl implements pasteboard.SystemPasteboard { 163 native hasDataType(mimeType: string): boolean; 164 native nativeSetData(data: pasteboard.PasteData): number; 165 native nativeClearData(): void; 166 native getDataSync(): pasteboard.PasteData; 167 native getDataSource(): string; 168 169 setData(data: pasteboard.PasteData, callback: AsyncCallback<void>): void { 170 console.println("setData enter") 171 let p = taskpool.execute(this.nativeSetData, data) 172 p.then((e: NullishType) => { 173 let r = e as number 174 console.println("setData return :" + r) 175 let err: BusinessError<void> 176 callback(err, undefined) 177 }).catch((e: NullishType) => { 178 let err = e as BusinessError<void>; 179 callback(err, undefined); 180 }); 181 } 182 setData(data: pasteboard.PasteData): Promise<void> { 183 console.println("promise => setData enter") 184 return new Promise<void>((resolve: (v: undefined) => void, reject: (error: Object) => void): void => { 185 let p = taskpool.execute(this.nativeSetData, data) 186 p.then((e: NullishType): void => { 187 let r = e as number 188 console.println("promise => setData return:" + r) 189 resolve(undefined) 190 }).catch((e: Error): void => { 191 console.log("promise => set failed:", e) 192 reject(e) 193 }) 194 }) 195 } 196 clearData(callback: AsyncCallback<void>): void { 197 console.println("clearData enter") 198 let p = taskpool.execute((): number => { 199 this.nativeClearData() 200 return 0 201 }) 202 p.then((e: NullishType) => { 203 let r = e as number 204 let err: BusinessError<void> 205 callback(err, undefined) 206 }).catch((e: NullishType) => { 207 let err = e as BusinessError<void>; 208 callback(err, undefined); 209 }); 210 } 211 clearData(): Promise<void> { 212 console.println("promise => clearData enter") 213 return new Promise<void>((resolve: (v: undefined) => void, reject: (error: Object) => void): void => { 214 let p = taskpool.execute((): number => { 215 this.nativeClearData() 216 return 0 217 }) 218 p.then((e: NullishType): void => { 219 resolve(undefined) 220 }).catch((e: Error): void => { 221 console.log("promise => clear failed:", e) 222 reject(e) 223 }) 224 }) 225 } 226}