!969 ets代码,使用4.0-SDK编译告警处理

Merge pull request !969 from guojin31/master
This commit is contained in:
openharmony_ci 2023-11-21 10:50:50 +00:00 committed by Gitee
commit 5615bf69bc
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 120 additions and 109 deletions

View File

@ -103,7 +103,7 @@ export struct InputMethodListDialog {
controller: CustomDialogController = new CustomDialogController({ builder: undefined });
patternOptions?: PatternOptions;
async getDefaultInputMethodSubType() {
async getDefaultInputMethodSubType(): Promise<void> {
console.info(`${TAG} getDefaultInputMethodSubType`);
this.inputMethodConfig = inputMethod.getSystemInputMethodConfigAbility();
if (this.inputMethodConfig) {
@ -365,7 +365,7 @@ export struct InputMethodListDialog {
})
}
switchPositionPattern(mode: number) {
switchPositionPattern(mode: number): void {
if (this.patternOptions) {
this.patternMode = mode;
AppStorage.set('patternMode', this.patternMode);
@ -375,7 +375,7 @@ export struct InputMethodListDialog {
}
}
async switchMethod(inputProperty: inputMethod.InputMethodProperty) {
async switchMethod(inputProperty: inputMethod.InputMethodProperty): Promise<void> {
if (this.currentInputMethod && this.currentInputMethod.name !== inputProperty.name) {
let subTypes = await inputMethod.getSetting().listInputMethodSubtype(inputProperty);
inputMethod.switchCurrentInputMethodAndSubtype(inputProperty, subTypes[0], (err: Error, result: boolean) => {
@ -387,7 +387,7 @@ export struct InputMethodListDialog {
}
}
switchMethodSub(inputSub: InputMethodSubtype) {
switchMethodSub(inputSub: InputMethodSubtype): void {
if (this.currentInputMethod && this.defaultInputMethod) {
if (this.currentInputMethod.name !== this.defaultInputMethod.name) {
inputMethod.switchCurrentInputMethodAndSubtype(this.defaultInputMethod, inputSub, () => {

View File

@ -13,74 +13,63 @@
* limitations under the License.
*/
import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
import common from '@ohos.app.ability.common';
import window from '@ohos.window';
import display from '@ohos.display';
import inputMethod from '@ohos.inputMethod';
import prompt from '@ohos.prompt';
import commonEvent from '@ohos.commonEvent';
import commonEvent from '@ohos.commonEventManager';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let TAG = '[InputMethodChooseDialog]';
let commonEvent1 = 'usual.event.PACKAGE_ADDED';
let commonEvent2 = 'usual.event.PACKAGE_REMOVED';
let subscribeInfo = {
events: [commonEvent1, commonEvent2]
let TAG: string = '[InputMethodChooseDialog]';
let PACKAGE_ADDED: string = 'usual.event.PACKAGE_ADDED';
let PACKAGE_REMOVED: string = 'usual.event.PACKAGE_REMOVED';
let subscribeInfo: commonEvent.CommonEventSubscribeInfo = {
events: [PACKAGE_ADDED, PACKAGE_REMOVED]
};
const EXIT_TIME = 1000;
interface DialogRect {
left: number;
top: number;
width: number;
height: number;
}
export default class ServiceExtAbility extends ServiceExtensionAbility {
onCreate(want): void {
private extensionWin: window.Window | undefined = undefined;
private mContext: common.ServiceExtensionContext | undefined = undefined;
private windowNum: number = 0;
onCreate(want: Want): void {
console.log(TAG, 'onCreate');
globalThis.windowNum = 0;
this.windowNum = 0;
this.mContext = this.context;
}
onRequest(want, startId): void {
onRequest(want: Want, startId: number): void {
console.log(TAG, 'onRequest execute');
globalThis.abilityWant = want;
display.getDefaultDisplay().then(() => {
let dialogRect = {
left: 50,
top: 900,
width: 300,
height: 300,
};
let windowConfig = {
name:'inputmethod Dialog',
windowType:window.WindowType.TYPE_FLOAT,
ctx:this.context
};
this.getInputMethods().then(() => {
this.createWindow(windowConfig, dialogRect);
});
}).catch((err) => {
console.log(TAG + 'getDefaultDisplay err:' + JSON.stringify(err));
let dialogRect: DialogRect = {
left: 50,
top: 900,
width: 300,
height: 300,
};
let windowConfig: window.Configuration = {
name: 'inputmethod Dialog',
windowType: window.WindowType.TYPE_FLOAT,
ctx: this.mContext
};
this.getInputMethods().then(() => {
this.createWindow(windowConfig, dialogRect);
});
commonEvent.createSubscriber(subscribeInfo, (error, subcriber) => {
commonEvent.subscribe(subcriber, (error, commonEventData) => {
if (commonEventData.event === commonEvent1 || commonEventData.event === commonEvent2) {
console.log(TAG + 'commonEvent:' + JSON.stringify(commonEvent1));
commonEvent.createSubscriber(subscribeInfo, (error: BusinessError, subcriber: commonEvent.CommonEventSubscriber) => {
commonEvent.subscribe(subcriber, (error: BusinessError, commonEventData: commonEvent.CommonEventData) => {
console.log(TAG + 'commonEvent:' + JSON.stringify(commonEventData.event));
if (commonEventData.event === PACKAGE_ADDED || commonEventData.event === PACKAGE_REMOVED) {
this.updateImeList();
}
});
});
globalThis.chooseInputMethods = ((prop: inputMethod.InputMethodProperty): void => {
inputMethod.switchInputMethod(prop).then((err) => {
if (!err) {
console.log(TAG + 'switchInputMethod failed,' + JSON.stringify(err));
prompt.showToast({
message: 'switch failed', duration: 200
});
} else {
console.log(TAG + 'switchInputMethod success');
prompt.showToast({
message: 'switch success', duration: 200
});
}
setTimeout(() => {
this.releaseContext();
}, EXIT_TIME);
});
});
}
onDestroy(): void {
@ -88,50 +77,43 @@ export default class ServiceExtAbility extends ServiceExtensionAbility {
this.releaseContext();
}
private async createWindow(config: window.Configuration, rect): Promise<void> {
private async createWindow(config: window.Configuration, rect: DialogRect): Promise<void> {
console.log(TAG + 'createWindow execute');
try {
if (globalThis.windowNum > 0) {
if (this.windowNum > 0) {
this.updateImeList();
return;
}
try {
await window.createWindow(config, async (err, data) => {
if (err.code) {
console.error('Failed to create the window. Cause: ' + JSON.stringify(err));
return;
this.extensionWin = await window.createWindow(config);
console.info(TAG + 'Succeeded in creating the window. Data: ' + JSON.stringify(this.extensionWin));
this.extensionWin.on('windowEvent', async (data: window.WindowEventType) => {
console.log(TAG + 'windowEvent:' + JSON.stringify(data));
if (data === window.WindowEventType.WINDOW_INACTIVE) {
await this.releaseContext();
}
const win = data;
globalThis.extensionWin = win;
console.info('Succeeded in creating the window. Data: ' + JSON.stringify(data));
win.on('windowEvent', async (data) => {
console.log(TAG + 'windowEvent:' + JSON.stringify(data));
if (data === window.WindowEventType.WINDOW_INACTIVE) {
await this.releaseContext();
}
});
await win.moveTo(rect.left, rect.top);
await win.resetSize(rect.width, rect.height);
await win.loadContent('pages/index');
await win.show();
globalThis.windowNum++;
console.log(TAG + 'window create successfully');
});
await this.extensionWin.moveWindowTo(rect.left, rect.top);
await this.extensionWin.resize(rect.width, rect.height);
await this.extensionWin.setUIContent('pages/index');
await this.extensionWin.showWindow();
this.windowNum++;
console.log(TAG + 'window create successfully');
} catch (exception) {
console.error('Failed to create the window. Cause: ' + JSON.stringify(exception));
}
globalThis.context = this.context;
} catch {
console.info(TAG + 'window create failed');
}
}
private async getInputMethods(): Promise<void> {
globalThis.inputMethodList = [];
let inputMethodList: Array<inputMethod.InputMethodProperty> = [];
try {
let enableList = await inputMethod.getSetting().getInputMethods(true);
let disableList = await inputMethod.getSetting().getInputMethods(false);
globalThis.inputMethodList = [...enableList, ...disableList];
inputMethodList = enableList.concat(disableList);
AppStorage.setOrCreate('inputMethodList', inputMethodList);
} catch {
console.log(TAG + 'getInputMethods failed');
}
@ -139,18 +121,19 @@ export default class ServiceExtAbility extends ServiceExtensionAbility {
private async updateImeList(): Promise<void> {
await this.getInputMethods().then(async () => {
await globalThis.extensionWin.loadContent('pages/index');
if (!globalThis.extensionWin.isWindowShowing()) {
await globalThis.extensionWin.show();
if (this.extensionWin) {
await this.extensionWin.setUIContent('pages/index');
if (!this.extensionWin.isWindowShowing()) {
await this.extensionWin.showWindow();
}
}
});
}
private async releaseContext(): Promise<void> {
if (globalThis.context !== null) {
await globalThis.extensionWin.destroy();
await globalThis.context.terminateSelf();
globalThis.context = null;
public async releaseContext(): Promise<void> {
if (this.mContext && this.extensionWin) {
await this.extensionWin.destroyWindow();
await this.mContext.terminateSelf();
}
}
};
};

View File

@ -12,33 +12,39 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import common from '@ohos.app.ability.common';
import inputMethod from '@ohos.inputMethod';
import prompt from '@ohos.promptAction';
import { BusinessError } from '@ohos.base';
const EXIT_TIME = 1000;
@Entry
@Component
struct Dialog {
private arr: string[] = []
struct Index {
private arr: string[] = [];
private propertyMap: Map<string, inputMethod.InputMethodProperty> = new Map();
private inputMethods: Array<inputMethod.InputMethodProperty> = null
private TAG = "[InputMethodChooseDialog]"
@StorageLink('inputMethodList') inputMethods: Array<inputMethod.InputMethodProperty> | undefined = [];
private TAG = "[InputMethodChooseDialog]";
aboutToAppear() {
console.log(this.TAG, "dialog page appears")
this.inputMethods = globalThis.inputMethodList
for (let inputmethod of this.inputMethods) {
let name = inputmethod.packageName
this.arr.push(name)
this.propertyMap.set(name, inputmethod)
this.inputMethods = AppStorage.get('inputMethodList');
if (this.inputMethods) {
for (let inputmethod of this.inputMethods) {
let name = inputmethod.packageName;
this.arr.push(name);
this.propertyMap.set(name, inputmethod);
}
}
}
onPrint() {
console.log(this.TAG + "print file or text")
onPrint(): void {
console.log(this.TAG + "print file or text");
}
onCopy() {
console.log(this.TAG + "copy file and html")
onCopy(): void {
console.log(this.TAG + "copy file and html");
}
build() {
@ -53,7 +59,7 @@ struct Dialog {
.backgroundColor(Color.Pink)
}.sticky(Sticky.Normal)
ForEach(this.arr, (item, index) => {
ForEach(this.arr, (item: string, index: number) => {
ListItem() {
Text(item.split('.').length > 2 ? item.split('.')[2] : item.split('.')[-1])
.width('100%')
@ -64,14 +70,36 @@ struct Dialog {
.backgroundColor($r("app.color.btn_default"))
.onClick(async () => {
if (this.propertyMap.has(item)) {
let prop = this.propertyMap.get(item)
globalThis.chooseInputMethods(prop)
let prop = this.propertyMap.get(item);
this.chooseInputMethods(prop);
}
})
}
.sticky(0 == index ? Sticky.Opacity : Sticky.None)
}, item => item)
}, (item: string) => item)
}
.width('100%')
.height('100%')
}.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
}
}
chooseInputMethods(prop: inputMethod.InputMethodProperty): void {
let context = getContext(this) as common.ServiceExtensionContext;
inputMethod.switchInputMethod(prop).then(() => {
console.log(this.TAG + 'switchInputMethod success');
prompt.showToast({
message: 'switch success', duration: 200
});
setTimeout(() => {
context.terminateSelf();
}, EXIT_TIME);
}).catch((err: BusinessError) => {
if (!err) {
console.log(this.TAG + 'switchInputMethod failed,' + JSON.stringify(err));
prompt.showToast({
message: 'switch failed', duration: 200
});
}
});
}
}