From 044dcadf1d694e41f6653c02b29540289f2c2f89 Mon Sep 17 00:00:00 2001 From: maojingling Date: Mon, 1 Apr 2024 17:16:00 +0800 Subject: [PATCH] maojingling@huawei.com Signed-off-by: maojingling --- build-profile.json5 | 2 +- .../src/main/ets/default/{Log.ets => Log.ts} | 7 ++-- .../common/{constants.ets => constants.ts} | 0 .../ets/com/ohos/model/screenShotModel.ets | 42 ++++++++++--------- ...iceExtAbility.ts => ServiceExtAbility.ets} | 8 ++-- product/phone/src/main/ets/pages/index.ets | 7 ++-- product/phone/src/main/ets/vm/ViewModel.ets | 19 +++++---- product/phone/src/main/module.json5 | 2 +- 8 files changed, 46 insertions(+), 41 deletions(-) rename common/src/main/ets/default/{Log.ets => Log.ts} (90%) rename features/screenshot/src/main/ets/com/ohos/common/{constants.ets => constants.ts} (100%) rename product/phone/src/main/ets/ServiceExtAbility/{ServiceExtAbility.ts => ServiceExtAbility.ets} (90%) diff --git a/build-profile.json5 b/build-profile.json5 index 7e32763..7b38050 100644 --- a/build-profile.json5 +++ b/build-profile.json5 @@ -4,7 +4,7 @@ { "name": "default", "signingConfig": "release", - "compileSdkVersion": 10, + "compileSdkVersion": 12, "compatibleSdkVersion": 9 } ], diff --git a/common/src/main/ets/default/Log.ets b/common/src/main/ets/default/Log.ts similarity index 90% rename from common/src/main/ets/default/Log.ets rename to common/src/main/ets/default/Log.ts index eec6889..8ee37c2 100644 --- a/common/src/main/ets/default/Log.ets +++ b/common/src/main/ets/default/Log.ts @@ -31,7 +31,7 @@ export default class Log { * @param {string} tag - Page or class tag * @param {string} log - Log needs to be printed */ - static showInfo(tag, ...args: any[]) { + static showInfo(tag: string, ...args: string[]) { hiLog.info(DOMAIN, PREFIX, FORMAT, `tag: ${tag} --> ${args.join(SEPARATOR)}`); } @@ -41,7 +41,7 @@ export default class Log { * @param {string} tag - Page or class tag * @param {string} log - Log needs to be printed */ - static showDebug(tag, ...args: any[]) { + static showDebug(tag: string, ...args: string[]) { hiLog.debug(DOMAIN, PREFIX, FORMAT, `tag: ${tag} --> ${args.join(SEPARATOR)}`); } @@ -51,8 +51,7 @@ export default class Log { * @param {string} tag - Page or class tag * @param {string} log - Log needs to be printed */ - static showError(tag, ...args: any[]) { + static showError(tag: string, ...args: string[]) { hiLog.error(DOMAIN, PREFIX, FORMAT, `tag: ${tag} --> ${args.join(SEPARATOR)}`); } - } diff --git a/features/screenshot/src/main/ets/com/ohos/common/constants.ets b/features/screenshot/src/main/ets/com/ohos/common/constants.ts similarity index 100% rename from features/screenshot/src/main/ets/com/ohos/common/constants.ets rename to features/screenshot/src/main/ets/com/ohos/common/constants.ts diff --git a/features/screenshot/src/main/ets/com/ohos/model/screenShotModel.ets b/features/screenshot/src/main/ets/com/ohos/model/screenShotModel.ets index 14cc232..c429b9b 100644 --- a/features/screenshot/src/main/ets/com/ohos/model/screenShotModel.ets +++ b/features/screenshot/src/main/ets/com/ohos/model/screenShotModel.ets @@ -13,6 +13,7 @@ * limitations under the License. */ +import { BusinessError } from '@ohos.base'; import ScreenshotManager from '@ohos.screenshot'; import WindowMar from '@ohos.window'; import ImageMar from '@ohos.multimedia.image'; @@ -21,6 +22,7 @@ import FileIo from '@ohos.fileio'; import display from '@ohos.display'; import Constants from '../common/constants'; import Log from '../../../../../../../../common/src/main/ets/default/Log'; +import Want from '@ohos.app.ability.Want'; const TAG = 'ScreenShot-ScreenShotModel'; @@ -32,35 +34,35 @@ const OPTIONS_QUALITY = 100; const CREATE_WINDOW_DELAY = 300; export class ScreenShotModel { - private captureImage: ImageMar.PixelMap; + private captureImage: ImageMar.PixelMap | undefined = undefined; private imageFileName: string = ''; async shotScreen() { Log.showInfo(TAG, 'shotScreen'); - await new Promise((resolve) => setTimeout(resolve, CREATE_WINDOW_DELAY)); + await new Promise((resolve) => setTimeout(resolve, CREATE_WINDOW_DELAY)); ScreenshotManager.save().then(async (data) => { - Log.showInfo(TAG, `ScreenshotManager.save data:${JSON.stringify(data)}`) + Log.showInfo(TAG, `ScreenshotManager.save data:${JSON.stringify(data)}`); if (!!data) { this.captureImage = data; AppStorage.Set('captureImage', data); this.saveImage(this.captureImage, { - format: ['image/jpeg'], + format: 'image/jpeg', quality: OPTIONS_QUALITY, }); } - }).catch((err) => { + }).catch((err: BusinessError) => { Log.showInfo(TAG, `ScreenshotManager.save err:${JSON.stringify(err)}`); - }) + }); } - async saveImage(pixelMap, options) { + async saveImage(pixelMap: ImageMar.PixelMap, options: ImageMar.PackingOption) { const media = mediaLibrary.getMediaLibrary(globalThis.shotScreenContext); Log.showInfo(TAG, `saveImage options:${JSON.stringify(options)}`); this.imageFileName = SCREENSHOT_PREFIX + '_' + (new Date()).getTime() + PICTURE_TYPE; - var packer = ImageMar.createImagePacker(); + const packer = ImageMar.createImagePacker(); Log.showInfo(TAG, `saveImage packer:${JSON.stringify(packer)}`); this.showWindow(); - await new Promise((resolve) => setTimeout(resolve, SAVE_IMAGE_DELAY)); + await new Promise((resolve) => setTimeout(resolve, SAVE_IMAGE_DELAY)); packer.packing(pixelMap, options).then((jpegData) => { Log.showInfo(TAG, 'packing jpegData type:' + jpegData.constructor.name + ' value : ' + jpegData); media.getPublicDirectory(mediaLibrary.DirectoryType.DIR_IMAGE, (err, rp) => { @@ -80,17 +82,17 @@ export class ScreenShotModel { Log.showInfo(TAG, `FileIo write, num = ${JSON.stringify(num)}`); fileAsset.close(fd).then(() => { Log.showInfo(TAG, 'FileIo close,successful'); - }) + }); }); }); } else { Log.showInfo(TAG, 'createAsset fail'); } - }) + }); } else { Log.showInfo(TAG, 'getPublicDirectory fail'); } - }) + }); }); } @@ -99,24 +101,24 @@ export class ScreenShotModel { WindowMar.find(Constants.WIN_NAME).then((win) => { win.show(() => { Log.showInfo(TAG, 'window show'); - }) - }) + }); + }); } dismiss(): void { Log.showInfo(TAG, 'dismiss'); //close ability - globalThis.shotScreenContext.terminateSelf((err, data) => { - Log.showInfo(TAG, `terminateSelf finish err:${err} data:${JSON.stringify(data)}`); + globalThis.shotScreenContext.terminateSelf((err: BusinessError) => { + Log.showInfo(TAG, `terminateSelf finish err:${err}`); WindowMar.find(Constants.WIN_NAME).then((win) => { win.destroy(() => { Log.showInfo(TAG, 'destroy the window finish'); - }) - }) - }) + }); + }); + }); } - openAbility(wantData) { + openAbility(wantData: Want) { Log.showInfo(TAG, `openAbility want:${JSON.stringify(wantData)}`); globalThis.shotScreenContext.startAbility(wantData); } diff --git a/product/phone/src/main/ets/ServiceExtAbility/ServiceExtAbility.ts b/product/phone/src/main/ets/ServiceExtAbility/ServiceExtAbility.ets similarity index 90% rename from product/phone/src/main/ets/ServiceExtAbility/ServiceExtAbility.ts rename to product/phone/src/main/ets/ServiceExtAbility/ServiceExtAbility.ets index c5268b6..1c74de6 100644 --- a/product/phone/src/main/ets/ServiceExtAbility/ServiceExtAbility.ts +++ b/product/phone/src/main/ets/ServiceExtAbility/ServiceExtAbility.ets @@ -19,6 +19,8 @@ import display from '@ohos.display'; import Log from '../../../../../../common/src/main/ets/default/Log'; import Constants from '../../../../../../features/screenshot/src/main/ets/com/ohos/common/constants'; import ScreenShotModel from '../../../../../../features/screenshot/src/main/ets/com/ohos/model/screenShotModel'; +import Want from '@ohos.app.ability.Want'; +import { BusinessError } from '@ohos.base'; const TAG = 'ScreenShot-ScreenShotServiceAbility'; const INDEX_PAGE = 'pages/index'; @@ -26,10 +28,10 @@ const ZOOM_RATIO = 0.4; const WINDOW_Y = 300; class ServiceExtAbility extends ServiceExtensionAbility { - onCreate(want): void { + onCreate(want: Want): void { Log.showInfo(TAG, 'api8New onCreate, want:' + want.abilityName); globalThis.shotScreenContext = this.context; - const windowConfig = { + const windowConfig: windowManager.Configuration = { name: Constants.WIN_NAME, windowType: windowManager.WindowType.TYPE_SCREENSHOT, ctx: this.context, @@ -48,7 +50,7 @@ class ServiceExtAbility extends ServiceExtensionAbility { }); }); }); - }, (error) => { + }, (error: BusinessError) => { Log.showInfo(TAG, 'window createFailed, error.code = ' + error.code); }); Log.showInfo(TAG, 'after window create'); diff --git a/product/phone/src/main/ets/pages/index.ets b/product/phone/src/main/ets/pages/index.ets index d50cdab..aaf8197 100644 --- a/product/phone/src/main/ets/pages/index.ets +++ b/product/phone/src/main/ets/pages/index.ets @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + import Log from '../../../../../../common/src/main/ets/default/Log'; import Constants from '../common/constants'; import ViewModel from '../vm/ViewModel'; @@ -28,16 +29,16 @@ struct Index { Stack({ alignContent: Alignment.Bottom }) { Column() { Image(this.captureImage) - .border({ color: Color.White, radius: 8, width: 8 }) + .border({ color: Color.White, radius: 8, width: 8 }); } .width(Constants.FULL_CONTAINER_WIDTH) .height(Constants.FULL_CONTAINER_HEIGHT) .onClick(() => { ViewModel.StartPhotosAbility(this.imageFilename); - }) + }); } .width(Constants.FULL_CONTAINER_WIDTH) - .height(Constants.FULL_CONTAINER_HEIGHT) + .height(Constants.FULL_CONTAINER_HEIGHT); } aboutToAppear() { diff --git a/product/phone/src/main/ets/vm/ViewModel.ets b/product/phone/src/main/ets/vm/ViewModel.ets index 9a24a89..39faa68 100644 --- a/product/phone/src/main/ets/vm/ViewModel.ets +++ b/product/phone/src/main/ets/vm/ViewModel.ets @@ -15,29 +15,30 @@ import Log from '../../../../../../common/src/main/ets/default/Log'; import ShotScreenModel from '../../../../../../features/screenshot/src/main/ets/com/ohos/model/screenShotModel'; +import Want from '@ohos.app.ability.Want'; -const TAG = 'ScreenShot-ViewModel' -const GALLERY_BUNDLE = 'com.ohos.photos' -const GALLERY_ABILITY = 'com.ohos.photos.MainAbility' +const TAG = 'ScreenShot-ViewModel'; +const GALLERY_BUNDLE = 'com.ohos.photos'; +const GALLERY_ABILITY = 'com.ohos.photos.MainAbility'; export class ViewModel { constructor() { - this.ViewModelInit() + this.ViewModelInit(); } ViewModelInit(): void { Log.showInfo(TAG, 'ViewModelInit'); } - StartPhotosAbility(imageFilename): void { - Log.showInfo(TAG, `StartPhotosAbility imageFilename:${imageFilename}`); - let wantData = { + StartPhotosAbility(imageFileName: string): void { + Log.showInfo(TAG, `StartPhotosAbility imageFileName:${imageFileName}`); + const wantData: Want = { bundleName: GALLERY_BUNDLE, abilityName: GALLERY_ABILITY, parameters: { - uri: imageFilename + uri: imageFileName } - } + }; ShotScreenModel.openAbility(wantData); //close window ShotScreenModel.dismiss(); diff --git a/product/phone/src/main/module.json5 b/product/phone/src/main/module.json5 index c723e13..f129035 100644 --- a/product/phone/src/main/module.json5 +++ b/product/phone/src/main/module.json5 @@ -26,7 +26,7 @@ "permissions": [ "ohos.permission.CAPTURE_SCREEN" ], - "srcEntrance": "./ets/ServiceExtAbility/ServiceExtAbility.ts", + "srcEntrance": "./ets/ServiceExtAbility/ServiceExtAbility.ets", "name": "com.ohos.screenshot.ServiceExtAbility", "icon": "$media:icon", "description": "$string:mainability_description",