1-首次启动释放字体文件不再自重启
2-修改子ability启动模式为"launchType":"specified",后单一同功能子ability单例,并且支持不同功能的ability独立的实现
This commit is contained in:
parent
d1709c3dec
commit
1241061ce4
@ -20,7 +20,7 @@ export default class EntryAbility extends UIAbility {
|
||||
onWindowStageCreate(windowStage: window.WindowStage): void {
|
||||
if(!InitHub(windowStage.getMainWindowSync(),windowStage,this.context)){
|
||||
console.log("InitHub Failed!")
|
||||
this.context.getApplicationContext().killAllProcesses();
|
||||
///this.context.getApplicationContext().killAllProcesses();
|
||||
}else{
|
||||
//Get Main Window
|
||||
windowStage.getMainWindow((err, data) => {
|
||||
|
||||
13
entry/src/main/ets/exabilitystage/ExAbilityStage.ets
Normal file
13
entry/src/main/ets/exabilitystage/ExAbilityStage.ets
Normal file
@ -0,0 +1,13 @@
|
||||
import { AbilityStage, Want } from '@kit.AbilityKit';
|
||||
|
||||
export default class ExAbilityStage extends AbilityStage {
|
||||
onAcceptWant(want: Want): string {
|
||||
if (want.abilityName === 'SubAbility') {
|
||||
if (want.parameters) {
|
||||
console.info('SubAbility:',`${want.parameters.instanceKey}`);
|
||||
return `${want.parameters.instanceKey}`;
|
||||
}
|
||||
}
|
||||
return 'ExAbilityStage';
|
||||
}
|
||||
}
|
||||
@ -8,30 +8,27 @@ import {
|
||||
import UIAbility from "@ohos.app.ability.UIAbility";
|
||||
|
||||
export function InitFonts(uac:common.UIAbilityContext):boolean{
|
||||
let sbFontsDir=uac.filesDir + '/fonts/';
|
||||
let sbExampleDir=uac.filesDir + '/example/';
|
||||
if(!CheckExistDir(uac,sbFontsDir)||!CheckExistDir(uac,sbExampleDir)){
|
||||
console.info(`first run app check dir exist`);
|
||||
console.info(`extract dir file to sandbox dir`);
|
||||
const fontsCopied = ExtractDir(uac, 'fonts');
|
||||
const exampleCopied = ExtractDir(uac, 'example');
|
||||
console.info(`restart app`);
|
||||
if(fontsCopied&&exampleCopied){
|
||||
let wantInfo: Want = {
|
||||
deviceId: '',
|
||||
bundleName: 'com.example.opencax',
|
||||
abilityName: 'EntryAbility',
|
||||
};
|
||||
uac.startAbility(wantInfo).then(() => {
|
||||
uac.terminateSelf();
|
||||
})
|
||||
}else{
|
||||
return false;
|
||||
try {
|
||||
let sbFontsDir=uac.filesDir + '/fonts/';
|
||||
let sbExampleDir=uac.filesDir + '/example/';
|
||||
if(!CheckExistDir(uac,sbFontsDir)||!CheckExistDir(uac,sbExampleDir)) {
|
||||
console.info(`first run app check dir exist`);
|
||||
console.info(`extract dir file to sandbox dir`);
|
||||
const fontsCopied = ExtractDir(uac, 'fonts');
|
||||
const exampleCopied = ExtractDir(uac, 'example');
|
||||
console.info(`restart app`);
|
||||
}
|
||||
return true;
|
||||
}else{
|
||||
HilogSadboxFontDirFile(uac,'fonts');
|
||||
HilogSadboxFontDirFile(uac,'example');
|
||||
return true;
|
||||
} catch (err) {
|
||||
let msg = 'Unknown error';
|
||||
if (err instanceof Error) {
|
||||
msg = err.message;
|
||||
} else if (typeof err === 'string') {
|
||||
msg = err;
|
||||
}
|
||||
console.error(`Init Fonts Failed: ${msg}, error code: ${err.code}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -20,13 +20,17 @@ export class SubWinInfo{
|
||||
subOpt?:window.SubWindowOptions;
|
||||
subPointX?:number=200;
|
||||
subPointY?:number=200;
|
||||
subEventName?:string;
|
||||
width?:number=0;
|
||||
height?:number=0;
|
||||
constructor(name?: string, page?: string,width?: number,height?: number) {
|
||||
this.name = name;
|
||||
this.page = page;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
SetSubWinSize(_width:number, _height:number){
|
||||
this.width=_width;
|
||||
this.height=_height;
|
||||
}
|
||||
constructor(_name?: string, _subEventName?:string,_page?: string) {
|
||||
this.name = _name;
|
||||
this.subEventName=_subEventName;
|
||||
this.page = _page;
|
||||
}
|
||||
}
|
||||
|
||||
@ -141,20 +145,20 @@ export function EventPage(event:TitleButton){
|
||||
|
||||
//事件类型:子窗体
|
||||
export function EventWin(event:TitleButton){
|
||||
let winInfo: SubWinInfo=new SubWinInfo;
|
||||
let winInfo: SubWinInfo = new SubWinInfo(event.btnName,event.btnEvent.command, event.btnEvent.page);
|
||||
if(event?.btnEvent.command=='Page_NewFile') {
|
||||
winInfo = new SubWinInfo(event.btnName, event.btnEvent.page, mdwInfo.winWidth * 0.4, mdwInfo.winHeight * 0.4);
|
||||
winInfo.SetSubWinSize(mdwInfo.winWidth * 0.4,mdwInfo.winHeight * 0.4);
|
||||
}else if(event?.btnEvent.command=='Page_SaveAsFile'){
|
||||
winInfo=new SubWinInfo(event.btnName,event.btnEvent.page,mdwInfo.winWidth*0.4,mdwInfo.winHeight*0.4);
|
||||
winInfo.SetSubWinSize(mdwInfo.winWidth * 0.4,mdwInfo.winHeight * 0.4);
|
||||
} else if(event?.btnEvent.command=='Page_ImportFile'){
|
||||
winInfo=new SubWinInfo(event.btnName,event.btnEvent.page,mdwInfo.winWidth*0.4,mdwInfo.winHeight*0.4);
|
||||
winInfo.SetSubWinSize(mdwInfo.winWidth * 0.4,mdwInfo.winHeight * 0.4);
|
||||
}else if(event?.btnEvent.command=='Page_ExportFile'){
|
||||
winInfo=new SubWinInfo(event.btnName,event.btnEvent.page,mdwInfo.winWidth*0.4,mdwInfo.winHeight*0.4);
|
||||
winInfo.SetSubWinSize(mdwInfo.winWidth * 0.4,mdwInfo.winHeight * 0.4);
|
||||
}else if(event?.btnEvent.command=='Page_Options'){
|
||||
winInfo=new SubWinInfo(event.btnName,event.btnEvent.page,mdwInfo.winWidth*0.6,mdwInfo.winHeight*0.8);
|
||||
winInfo.SetSubWinSize(mdwInfo.winWidth * 0.6,mdwInfo.winHeight * 0.8);
|
||||
}
|
||||
let ctx = mdwInfo.win?.getUIContext().getHostContext() as common.UIAbilityContext;
|
||||
EventSubAbility(ctx).then((result) => {
|
||||
|
||||
EventSubAbility(winInfo).then((result) => {
|
||||
if(result){
|
||||
let context =mdwInfo.win?.getUIContext().getHostContext()?.getApplicationContext();
|
||||
context?.eventHub.emit('InitSubWindow',winInfo);
|
||||
|
||||
@ -1,19 +1,26 @@
|
||||
import { common, StartOptions,Want } from "@kit.AbilityKit";
|
||||
import { mdwInfo } from "../dispwininfo/DispWinInfo";
|
||||
import { util } from '@kit.ArkTS';
|
||||
import { SubWinInfo } from "./EventBase";
|
||||
|
||||
export async function EventSubAbility(uac:common.UIAbilityContext): Promise<boolean>{
|
||||
|
||||
export async function EventSubAbility(winInfo:SubWinInfo): Promise<boolean>{
|
||||
let ctx = mdwInfo.win?.getUIContext().getHostContext() as common.UIAbilityContext;
|
||||
let want: Want = {
|
||||
bundleName: 'com.example.opencax',
|
||||
abilityName: 'SubAbility',
|
||||
parameters: {
|
||||
instanceKey: winInfo.subEventName as string
|
||||
}
|
||||
};
|
||||
try {
|
||||
if (!uac) {
|
||||
if (!ctx) {
|
||||
console.error("Failed to get UIAbilityContext");
|
||||
return false;
|
||||
}
|
||||
// 监听来自 SubWinAbility 的初始化完成事件
|
||||
const initializationPromise = new Promise<boolean>((resolve) => {
|
||||
let appCtx =uac.getApplicationContext();
|
||||
let appCtx =ctx.getApplicationContext();
|
||||
appCtx?.eventHub.on('SubWinInitialized', (data: Object) => {
|
||||
console.info("Received SubWinInitialized event:", data);
|
||||
// 可以根据 data 中的信息判断是否成功
|
||||
@ -32,7 +39,7 @@ export async function EventSubAbility(uac:common.UIAbilityContext): Promise<bool
|
||||
};
|
||||
// 发起启动 Ability 的请求
|
||||
console.info("Attempting to start SubWinAbility...");
|
||||
await uac.startAbility(want,options);
|
||||
await ctx.startAbility(want,options);
|
||||
console.info("StartAbility request sent.");
|
||||
// --- 等待初始化完成事件 ---
|
||||
console.info("Waiting for SubWinInitialized event...");
|
||||
|
||||
@ -7,12 +7,15 @@ const DOMAIN = 0x0000;
|
||||
export default class SubAbility extends UIAbility {
|
||||
private subWin?:window.Window=undefined;
|
||||
private subWinStage?: window.WindowStage=undefined;
|
||||
private want?: Want = undefined;
|
||||
//注册订阅事件
|
||||
onWinEventHub(){
|
||||
let ctx =this.context.getApplicationContext()
|
||||
ctx.eventHub.on('InitSubWindow', (data: Object) => {
|
||||
let winInfo=data as SubWinInfo;
|
||||
this.InitWindowParam(winInfo);
|
||||
if(this.want&&this.want.parameters&&this.want.parameters['instanceKey']===winInfo.subEventName){
|
||||
this.InitWindowParam(winInfo);
|
||||
}
|
||||
});
|
||||
ctx.eventHub.on('MainWindowDestroy', (data: Object) => {
|
||||
this.context.terminateSelf();
|
||||
@ -47,12 +50,10 @@ export default class SubAbility extends UIAbility {
|
||||
});
|
||||
}
|
||||
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
|
||||
this.want=want;
|
||||
this.onWinEventHub();
|
||||
}
|
||||
onAcceptWant(want: Want): string {
|
||||
|
||||
return 'SubAbility';
|
||||
}
|
||||
onDestroy(): void {
|
||||
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
"type": "entry",
|
||||
"description": "$string:module_desc",
|
||||
"mainElement": "EntryAbility",
|
||||
"srcEntry": "./ets/exabilitystage/ExAbilityStage.ets",
|
||||
"deviceTypes": [
|
||||
"2in1",
|
||||
"tablet"
|
||||
@ -13,10 +14,10 @@
|
||||
"pages": "$profile:main_pages",
|
||||
"requestPermissions": [
|
||||
{
|
||||
"name": "ohos.permission.INPUT_MONITORING",
|
||||
"name": "ohos.permission.INPUT_MONITORING"
|
||||
},
|
||||
{
|
||||
"name": "ohos.permission.SET_WINDOW_TRANSPARENT",
|
||||
"name": "ohos.permission.SET_WINDOW_TRANSPARENT"
|
||||
}
|
||||
],
|
||||
"abilities": [
|
||||
@ -76,7 +77,7 @@
|
||||
"name": "ohos.extension.backup",
|
||||
"resource": "$profile:backup_config"
|
||||
}
|
||||
],
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user