!52 升级SDK到12,同步适配ArkTS规范

Merge pull request !52 from jlm/codecheck1
This commit is contained in:
openharmony_ci 2024-04-01 12:17:14 +00:00 committed by Gitee
commit 38f3b016a9
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 46 additions and 41 deletions

View File

@ -4,7 +4,7 @@
{ {
"name": "default", "name": "default",
"signingConfig": "release", "signingConfig": "release",
"compileSdkVersion": 10, "compileSdkVersion": 12,
"compatibleSdkVersion": 9 "compatibleSdkVersion": 9
} }
], ],

View File

@ -31,7 +31,7 @@ export default class Log {
* @param {string} tag - Page or class tag * @param {string} tag - Page or class tag
* @param {string} log - Log needs to be printed * @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)}`); 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} tag - Page or class tag
* @param {string} log - Log needs to be printed * @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)}`); 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} tag - Page or class tag
* @param {string} log - Log needs to be printed * @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)}`); hiLog.error(DOMAIN, PREFIX, FORMAT, `tag: ${tag} --> ${args.join(SEPARATOR)}`);
} }
} }

View File

@ -13,6 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { BusinessError } from '@ohos.base';
import ScreenshotManager from '@ohos.screenshot'; import ScreenshotManager from '@ohos.screenshot';
import WindowMar from '@ohos.window'; import WindowMar from '@ohos.window';
import ImageMar from '@ohos.multimedia.image'; import ImageMar from '@ohos.multimedia.image';
@ -21,6 +22,7 @@ import FileIo from '@ohos.fileio';
import display from '@ohos.display'; import display from '@ohos.display';
import Constants from '../common/constants'; import Constants from '../common/constants';
import Log from '../../../../../../../../common/src/main/ets/default/Log'; import Log from '../../../../../../../../common/src/main/ets/default/Log';
import Want from '@ohos.app.ability.Want';
const TAG = 'ScreenShot-ScreenShotModel'; const TAG = 'ScreenShot-ScreenShotModel';
@ -32,35 +34,35 @@ const OPTIONS_QUALITY = 100;
const CREATE_WINDOW_DELAY = 300; const CREATE_WINDOW_DELAY = 300;
export class ScreenShotModel { export class ScreenShotModel {
private captureImage: ImageMar.PixelMap; private captureImage: ImageMar.PixelMap | undefined = undefined;
private imageFileName: string = ''; private imageFileName: string = '';
async shotScreen() { async shotScreen() {
Log.showInfo(TAG, 'shotScreen'); Log.showInfo(TAG, 'shotScreen');
await new Promise((resolve) => setTimeout(resolve, CREATE_WINDOW_DELAY)); await new Promise<number>((resolve) => setTimeout(resolve, CREATE_WINDOW_DELAY));
ScreenshotManager.save().then(async (data) => { 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) { if (!!data) {
this.captureImage = data; this.captureImage = data;
AppStorage.Set('captureImage', data); AppStorage.Set('captureImage', data);
this.saveImage(this.captureImage, { this.saveImage(this.captureImage, {
format: ['image/jpeg'], format: 'image/jpeg',
quality: OPTIONS_QUALITY, quality: OPTIONS_QUALITY,
}); });
} }
}).catch((err) => { }).catch((err: BusinessError) => {
Log.showInfo(TAG, `ScreenshotManager.save err:${JSON.stringify(err)}`); 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); const media = mediaLibrary.getMediaLibrary(globalThis.shotScreenContext);
Log.showInfo(TAG, `saveImage options:${JSON.stringify(options)}`); Log.showInfo(TAG, `saveImage options:${JSON.stringify(options)}`);
this.imageFileName = SCREENSHOT_PREFIX + '_' + (new Date()).getTime() + PICTURE_TYPE; 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)}`); Log.showInfo(TAG, `saveImage packer:${JSON.stringify(packer)}`);
this.showWindow(); this.showWindow();
await new Promise((resolve) => setTimeout(resolve, SAVE_IMAGE_DELAY)); await new Promise<number>((resolve) => setTimeout(resolve, SAVE_IMAGE_DELAY));
packer.packing(pixelMap, options).then((jpegData) => { packer.packing(pixelMap, options).then((jpegData) => {
Log.showInfo(TAG, 'packing jpegData type:' + jpegData.constructor.name + ' value : ' + jpegData); Log.showInfo(TAG, 'packing jpegData type:' + jpegData.constructor.name + ' value : ' + jpegData);
media.getPublicDirectory(mediaLibrary.DirectoryType.DIR_IMAGE, (err, rp) => { media.getPublicDirectory(mediaLibrary.DirectoryType.DIR_IMAGE, (err, rp) => {
@ -80,17 +82,17 @@ export class ScreenShotModel {
Log.showInfo(TAG, `FileIo write, num = ${JSON.stringify(num)}`); Log.showInfo(TAG, `FileIo write, num = ${JSON.stringify(num)}`);
fileAsset.close(fd).then(() => { fileAsset.close(fd).then(() => {
Log.showInfo(TAG, 'FileIo close,successful'); Log.showInfo(TAG, 'FileIo close,successful');
}) });
}); });
}); });
} else { } else {
Log.showInfo(TAG, 'createAsset fail'); Log.showInfo(TAG, 'createAsset fail');
} }
}) });
} else { } else {
Log.showInfo(TAG, 'getPublicDirectory fail'); Log.showInfo(TAG, 'getPublicDirectory fail');
} }
}) });
}); });
} }
@ -99,24 +101,24 @@ export class ScreenShotModel {
WindowMar.find(Constants.WIN_NAME).then((win) => { WindowMar.find(Constants.WIN_NAME).then((win) => {
win.show(() => { win.show(() => {
Log.showInfo(TAG, 'window show'); Log.showInfo(TAG, 'window show');
}) });
}) });
} }
dismiss(): void { dismiss(): void {
Log.showInfo(TAG, 'dismiss'); Log.showInfo(TAG, 'dismiss');
//close ability //close ability
globalThis.shotScreenContext.terminateSelf((err, data) => { globalThis.shotScreenContext.terminateSelf((err: BusinessError) => {
Log.showInfo(TAG, `terminateSelf finish err:${err} data:${JSON.stringify(data)}`); Log.showInfo(TAG, `terminateSelf finish err:${err}`);
WindowMar.find(Constants.WIN_NAME).then((win) => { WindowMar.find(Constants.WIN_NAME).then((win) => {
win.destroy(() => { win.destroy(() => {
Log.showInfo(TAG, 'destroy the window finish'); Log.showInfo(TAG, 'destroy the window finish');
}) });
}) });
}) });
} }
openAbility(wantData) { openAbility(wantData: Want) {
Log.showInfo(TAG, `openAbility want:${JSON.stringify(wantData)}`); Log.showInfo(TAG, `openAbility want:${JSON.stringify(wantData)}`);
globalThis.shotScreenContext.startAbility(wantData); globalThis.shotScreenContext.startAbility(wantData);
} }

View File

@ -19,6 +19,8 @@ import display from '@ohos.display';
import Log from '../../../../../../common/src/main/ets/default/Log'; import Log from '../../../../../../common/src/main/ets/default/Log';
import Constants from '../../../../../../features/screenshot/src/main/ets/com/ohos/common/constants'; 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 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 TAG = 'ScreenShot-ScreenShotServiceAbility';
const INDEX_PAGE = 'pages/index'; const INDEX_PAGE = 'pages/index';
@ -26,10 +28,10 @@ const ZOOM_RATIO = 0.4;
const WINDOW_Y = 300; const WINDOW_Y = 300;
class ServiceExtAbility extends ServiceExtensionAbility { class ServiceExtAbility extends ServiceExtensionAbility {
onCreate(want): void { onCreate(want: Want): void {
Log.showInfo(TAG, 'api8New onCreate, want:' + want.abilityName); Log.showInfo(TAG, 'api8New onCreate, want:' + want.abilityName);
globalThis.shotScreenContext = this.context; globalThis.shotScreenContext = this.context;
const windowConfig = { const windowConfig: windowManager.Configuration = {
name: Constants.WIN_NAME, name: Constants.WIN_NAME,
windowType: windowManager.WindowType.TYPE_SCREENSHOT, windowType: windowManager.WindowType.TYPE_SCREENSHOT,
ctx: this.context, 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, 'window createFailed, error.code = ' + error.code);
}); });
Log.showInfo(TAG, 'after window create'); Log.showInfo(TAG, 'after window create');

View File

@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import Log from '../../../../../../common/src/main/ets/default/Log'; import Log from '../../../../../../common/src/main/ets/default/Log';
import Constants from '../common/constants'; import Constants from '../common/constants';
import ViewModel from '../vm/ViewModel'; import ViewModel from '../vm/ViewModel';
@ -28,16 +29,16 @@ struct Index {
Stack({ alignContent: Alignment.Bottom }) { Stack({ alignContent: Alignment.Bottom }) {
Column() { Column() {
Image(this.captureImage) Image(this.captureImage)
.border({ color: Color.White, radius: 8, width: 8 }) .border({ color: Color.White, radius: 8, width: 8 });
} }
.width(Constants.FULL_CONTAINER_WIDTH) .width(Constants.FULL_CONTAINER_WIDTH)
.height(Constants.FULL_CONTAINER_HEIGHT) .height(Constants.FULL_CONTAINER_HEIGHT)
.onClick(() => { .onClick(() => {
ViewModel.StartPhotosAbility(this.imageFilename); ViewModel.StartPhotosAbility(this.imageFilename);
}) });
} }
.width(Constants.FULL_CONTAINER_WIDTH) .width(Constants.FULL_CONTAINER_WIDTH)
.height(Constants.FULL_CONTAINER_HEIGHT) .height(Constants.FULL_CONTAINER_HEIGHT);
} }
aboutToAppear() { aboutToAppear() {

View File

@ -15,29 +15,30 @@
import Log from '../../../../../../common/src/main/ets/default/Log'; import Log from '../../../../../../common/src/main/ets/default/Log';
import ShotScreenModel from '../../../../../../features/screenshot/src/main/ets/com/ohos/model/screenShotModel'; import ShotScreenModel from '../../../../../../features/screenshot/src/main/ets/com/ohos/model/screenShotModel';
import Want from '@ohos.app.ability.Want';
const TAG = 'ScreenShot-ViewModel' const TAG = 'ScreenShot-ViewModel';
const GALLERY_BUNDLE = 'com.ohos.photos' const GALLERY_BUNDLE = 'com.ohos.photos';
const GALLERY_ABILITY = 'com.ohos.photos.MainAbility' const GALLERY_ABILITY = 'com.ohos.photos.MainAbility';
export class ViewModel { export class ViewModel {
constructor() { constructor() {
this.ViewModelInit() this.ViewModelInit();
} }
ViewModelInit(): void { ViewModelInit(): void {
Log.showInfo(TAG, 'ViewModelInit'); Log.showInfo(TAG, 'ViewModelInit');
} }
StartPhotosAbility(imageFilename): void { StartPhotosAbility(imageFileName: string): void {
Log.showInfo(TAG, `StartPhotosAbility imageFilename:${imageFilename}`); Log.showInfo(TAG, `StartPhotosAbility imageFileName:${imageFileName}`);
let wantData = { const wantData: Want = {
bundleName: GALLERY_BUNDLE, bundleName: GALLERY_BUNDLE,
abilityName: GALLERY_ABILITY, abilityName: GALLERY_ABILITY,
parameters: { parameters: {
uri: imageFilename uri: imageFileName
} }
} };
ShotScreenModel.openAbility(wantData); ShotScreenModel.openAbility(wantData);
//close window //close window
ShotScreenModel.dismiss(); ShotScreenModel.dismiss();

View File

@ -26,7 +26,7 @@
"permissions": [ "permissions": [
"ohos.permission.CAPTURE_SCREEN" "ohos.permission.CAPTURE_SCREEN"
], ],
"srcEntrance": "./ets/ServiceExtAbility/ServiceExtAbility.ts", "srcEntrance": "./ets/ServiceExtAbility/ServiceExtAbility.ets",
"name": "com.ohos.screenshot.ServiceExtAbility", "name": "com.ohos.screenshot.ServiceExtAbility",
"icon": "$media:icon", "icon": "$media:icon",
"description": "$string:mainability_description", "description": "$string:mainability_description",