1import UIAbility from '@ohos.app.ability.UIAbility' 2import display from '@ohos.display' 3import { Callback } from 'basic' 4import { logInfo, logError, logWarn } from '../../../../../../common/src/main/ets/components/Utils/LogUtils' 5 6let displayWidth: number = 0 7let displayHeight: number = 0 8 9const TAG = "MainAbility" 10 11export default class MainAbility extends UIAbility { 12 onCreate(want, launchParam) { 13 logInfo(TAG, "onCreate") 14 globalThis.abilityWant = want; 15 16 globalThis.startMode = want.parameters.startMode 17 globalThis.saveFile = want.parameters.saveFile 18 globalThis.debugMode = want.parameters.debugMode 19 logInfo(TAG, `parameters ${JSON.stringify(want.parameters)}`) 20 } 21 22 onDestroy() { 23 logInfo(TAG, "onDestroy") 24 } 25 26 onWindowStageCreate(windowStage) { 27 // Main window is created, set main page for this ability 28 logInfo(TAG, "onWindowStageCreate") 29 30 globalThis.context = this.context 31 32 this.requestPermissions(() => this.displayWindow(windowStage)) 33 } 34 35 onWindowStageDestroy() { 36 // Main window is destroyed, release UI related resources 37 logInfo(TAG, "onWindowStageDestroy") 38 } 39 40 onForeground() { 41 // Ability has brought to foreground 42 logInfo(TAG, "onForeground") 43 } 44 45 onBackground() { 46 // Ability has back to background 47 logInfo(TAG, "onBackground") 48 } 49 50 private requestPermissions(callback: Callback<void>) { 51 let permissionList: Array<string> = [ 52 "ohos.permission.MEDIA_LOCATION", 53 "ohos.permission.READ_MEDIA", 54 "ohos.permission.WRITE_MEDIA" 55 ] 56 globalThis.context.requestPermissionsFromUser(permissionList).then(function (data) { 57 logInfo(TAG, 'request permission data result = ' + data.authResults) 58 callback() 59 }, (error) => { 60 logError(TAG, 'fail to request permission error code = ' + error.code) 61 }) 62 } 63 64 private displayWindow(windowStage) { 65 display.getDefaultDisplay().then(dis => { 66 displayWidth = dis.width 67 displayHeight = dis.height 68 69 globalThis.width = dis.width 70 globalThis.height = dis.height 71 globalThis.mainDialogWidth = dis.width 72 globalThis.mainDialogHeight = (((displayHeight) - 180) * 0.7) / 1.3 73 74 logInfo(TAG, "displayWidth = " + displayWidth + " displayHeight = " + displayHeight) 75 76 windowStage.getMainWindow().then(win => { 77 logInfo(TAG, "windowStage.getMainWindow()") 78 79 win.resetSize(displayWidth, displayHeight - 120) 80 81 win.moveTo(0, 0) 82 83 win.setBackgroundColor("#00FFFFFF", (err, data) => { 84 if (err.code) { 85 logWarn(TAG, "Fail to set the background color" + JSON.stringify(err)) 86 } else { 87 logInfo(TAG, "Success to set the background color" + JSON.stringify(data)) 88 } 89 }) 90 91 win.disableWindowDecor((err, data) => { 92 if (err.code) { 93 logWarn(TAG, "Fail to disable Window Decor" + JSON.stringify(err)) 94 } else { 95 logInfo(TAG, "Success to disable Window Decor" + JSON.stringify(data)) 96 } 97 }) 98 99 win.setWindowMode(102, (err, data) => { 100 if (err.code) { 101 logWarn(TAG, 'Failed to set the setWindowMode. Data: ' + JSON.stringify(data)) 102 } else { 103 logInfo(TAG, 'Succeeded in setting the setWindowMode. Data: ' + JSON.stringify(data)) 104 } 105 }) 106 107 windowStage.setUIContent(this.context, "pages/index", null) 108 }) 109 }) 110 } 111}; 112