mirror of
https://gitee.com/openharmony/ability_dmsfwk
synced 2024-11-23 06:20:07 +00:00
Modify the arkts codex(dmsDemo, continuationManagerTestSuite, continuationManualTestSuite)
issue:https://gitee.com/openharmony/ability_dmsfwk/issues/I8L7UA Signed-off-by: hunili <lihucheng2@huawei.com>
This commit is contained in:
parent
76d22d4ba6
commit
dfaf6921f2
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
@ -12,12 +12,12 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export default {
|
||||
class LocalAbility {
|
||||
onCreate() {
|
||||
console.info('Application onCreate')
|
||||
},
|
||||
}
|
||||
onDestroy() {
|
||||
console.info('Application onDestroy')
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
export default new LocalAbility();
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
@ -13,26 +13,28 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export default {
|
||||
class RemoteAbility {
|
||||
onCreate() {
|
||||
console.info('Application onCreate')
|
||||
},
|
||||
}
|
||||
onDestroy() {
|
||||
console.info('Application onDestroy')
|
||||
},
|
||||
}
|
||||
onStartContinuation() {
|
||||
console.info('onStartContinuation');
|
||||
return true;
|
||||
},
|
||||
onRestoreData(data) {
|
||||
}
|
||||
onRestoreData(data: string) {
|
||||
console.info('onRestoreData' + data);
|
||||
return true;
|
||||
},
|
||||
onSaveData(data) {
|
||||
}
|
||||
onSaveData(data: string) {
|
||||
console.info('onSaveData');
|
||||
return true;
|
||||
},
|
||||
onCompleteContinuation(result) {
|
||||
}
|
||||
onCompleteContinuation(result: string) {
|
||||
console.info('onCompleteContinuation:' + result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new RemoteAbility();
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
@ -16,15 +16,17 @@
|
||||
import featureAbility from '@ohos.ability.featureAbility';
|
||||
import prompt from '@system.prompt';
|
||||
import rpc from "@ohos.rpc";
|
||||
import RemoteDeviceModel from '../../model/RemoteDeviceModel.ets';
|
||||
import RemoteDeviceModel from '../../model/RemoteDeviceModel';
|
||||
import Want from '@ohos.app.ability.Want';
|
||||
|
||||
var connectedAbility;
|
||||
var localDeviceId = "";
|
||||
var mRemote;
|
||||
var remoteDeviceModel = new RemoteDeviceModel;
|
||||
var mIndex;
|
||||
var mDiscoveredSize = 0;
|
||||
var mDeviceSize = 0;
|
||||
let connectedAbility: number|null;
|
||||
let localDeviceId = "";
|
||||
let mRemote: rpc.IRemoteObject;
|
||||
let remoteDeviceModel = new RemoteDeviceModel;
|
||||
let mIndex: number;
|
||||
let mValue: string;
|
||||
let mDiscoveredSize = 0;
|
||||
let mDeviceSize = 0;
|
||||
|
||||
enum Operation {
|
||||
AuthDeviceOperation,
|
||||
@ -65,7 +67,7 @@ function registerDeviceListCallback() {
|
||||
console.info('[dmsDemo] registerDeviceListCallback end');
|
||||
}
|
||||
|
||||
function authDevice(index) {
|
||||
function authDevice(index: number) {
|
||||
console.info('[dmsDemo] authDevice begin');
|
||||
if (remoteDeviceModel.deviceList.length >= 1 && remoteDeviceModel.discoverList.length == 0) {
|
||||
prompt.showToast({
|
||||
@ -97,7 +99,7 @@ function unregisterDeviceListCallback() {
|
||||
function onStartLocalAbility() {
|
||||
console.info('[dmsDemo] onStartLocalAbility begin');
|
||||
console.info('[dmsDemo] onStartLocalAbility deviceId is ' + localDeviceId);
|
||||
var wantValue = {
|
||||
let wantValue: Want = {
|
||||
deviceId: localDeviceId,
|
||||
bundleName: 'ohos.dms.etsDemo',
|
||||
abilityName: 'ohos.dms.etsDemo.LocalAbility',
|
||||
@ -110,7 +112,7 @@ function onStartLocalAbility() {
|
||||
console.info('[dmsDemo] onStartLocalAbility end');
|
||||
}
|
||||
|
||||
function onStartRemoteAbility(index) {
|
||||
function onStartRemoteAbility(index: number) {
|
||||
console.info('[dmsDemo] onStartRemoteAbility begin');
|
||||
let numDevices = remoteDeviceModel.deviceList.length;
|
||||
if (numDevices === 0) {
|
||||
@ -119,14 +121,12 @@ function onStartRemoteAbility(index) {
|
||||
});
|
||||
return;
|
||||
}
|
||||
let deviceIdForStart = remoteDeviceModel.deviceList[index - 1].deviceId;
|
||||
let deviceIdForStart: string = remoteDeviceModel.deviceList[index - 1].deviceId;
|
||||
console.info('[dmsDemo] onStartRemoteAbility deviceId is ' + deviceIdForStart);
|
||||
var params;
|
||||
var wantValue = {
|
||||
let wantValue: Want = {
|
||||
bundleName: 'ohos.dms.etsDemo',
|
||||
abilityName: 'ohos.dms.etsDemo.RemoteAbility',
|
||||
deviceId: deviceIdForStart,
|
||||
parameters: params
|
||||
deviceId: deviceIdForStart
|
||||
};
|
||||
console.info('[dmsDemo] onStartRemoteAbility want=' + JSON.stringify(wantValue));
|
||||
featureAbility.startAbility({
|
||||
@ -137,80 +137,9 @@ function onStartRemoteAbility(index) {
|
||||
console.info('[dmsDemo] onStartRemoteAbility end');
|
||||
}
|
||||
|
||||
async function onContinueLocalAbility() {
|
||||
console.info('[dmsDemo] onContinueLocalAbility begin');
|
||||
console.info('[dmsDemo] onContinueLocalAbility deviceId is ' + localDeviceId);
|
||||
let continueAbilityOptions = {
|
||||
reversible: false,
|
||||
deviceId: localDeviceId,
|
||||
};
|
||||
function ContinueAbilityCallback(err, data) {
|
||||
console.info("[dmsDemo] onContinueLocalAbility ContinueAbilityCallback, result err = " + JSON.stringify(err));
|
||||
console.info("[dmsDemo] onContinueLocalAbility ContinueAbilityCallback, result data= " + JSON.stringify(data));
|
||||
};
|
||||
await featureAbility.continueAbility(continueAbilityOptions, ContinueAbilityCallback);
|
||||
console.info('[dmsDemo] onContinueLocalAbility end');
|
||||
}
|
||||
|
||||
async function onContinueAbility(index) {
|
||||
console.info('[dmsDemo] onContinueAbility begin');
|
||||
let numDevices = remoteDeviceModel.deviceList.length;
|
||||
if (numDevices === 0) {
|
||||
prompt.showToast({
|
||||
message: "onContinueAbility no device found"
|
||||
});
|
||||
return;
|
||||
}
|
||||
let deviceIdForContinue = remoteDeviceModel.deviceList[index - 1].deviceId;
|
||||
console.info('[dmsDemo] onContinueAbility deviceId is ' + deviceIdForContinue);
|
||||
let continueAbilityOptions = {
|
||||
reversible: false,
|
||||
deviceId: deviceIdForContinue,
|
||||
};
|
||||
function ContinueAbilityCallback(err, data) {
|
||||
console.info("[dmsDemo] onContinueAbility ContinueAbilityCallback, result err = " + JSON.stringify(err));
|
||||
console.info("[dmsDemo] onContinueAbility ContinueAbilityCallback, result data= " + JSON.stringify(data));
|
||||
};
|
||||
await featureAbility.continueAbility(continueAbilityOptions, ContinueAbilityCallback);
|
||||
console.info('[dmsDemo] onContinueAbility end');
|
||||
}
|
||||
|
||||
function onConnectLocalService() {
|
||||
console.info('[dmsDemo] onConnectLocalService begin');
|
||||
console.info('[dmsDemo] onConnectLocalService deviceId is ' + localDeviceId);
|
||||
async function onConnectCallback(element, remote) {
|
||||
console.log('[dmsDemo] onConnectLocalService onConnectDone element: ' + element);
|
||||
console.log('[dmsDemo] onConnectLocalService onConnectDone remote: ' + remote);
|
||||
mRemote = remote;
|
||||
if (mRemote == null) {
|
||||
prompt.showToast({
|
||||
message: "onConnectLocalService not connected yet"
|
||||
});
|
||||
return;
|
||||
}
|
||||
let option = new rpc.MessageOption();
|
||||
let data = new rpc.MessageParcel();
|
||||
let reply = new rpc.MessageParcel();
|
||||
data.writeInt(1);
|
||||
data.writeInt(99);
|
||||
await mRemote.sendRequest(1, data, reply, option);
|
||||
let msg = reply.readInt();
|
||||
prompt.showToast({
|
||||
message: "onConnectLocalService connect result: " + msg,
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
|
||||
function onDisconnectCallback(element) {
|
||||
console.log('[dmsDemo] onConnectLocalService onDisconnectDone element: ' + element);
|
||||
}
|
||||
|
||||
function onFailedCallback(code) {
|
||||
console.log('[dmsDemo] onConnectLocalService onFailed errCode: ' + code)
|
||||
prompt.showToast({
|
||||
message: "onConnectLocalService onFailed: " + code
|
||||
});
|
||||
}
|
||||
|
||||
connectedAbility = featureAbility.connectAbility(
|
||||
{
|
||||
@ -219,49 +148,44 @@ function onConnectLocalService() {
|
||||
abilityName: "ohos.dms.js.ServiceAbility",
|
||||
},
|
||||
{
|
||||
onConnect: onConnectCallback,
|
||||
onDisconnect: onDisconnectCallback,
|
||||
onFailed: onFailedCallback,
|
||||
onConnect: async (element, remote) => {
|
||||
console.log('[dmsDemo] onConnectLocalService onConnectDone element: ' + element);
|
||||
console.log('[dmsDemo] onConnectLocalService onConnectDone remote: ' + remote);
|
||||
mRemote = remote;
|
||||
if (mRemote == null) {
|
||||
prompt.showToast({
|
||||
message: "onConnectLocalService not connected yet"
|
||||
});
|
||||
return;
|
||||
}
|
||||
let option = new rpc.MessageOption();
|
||||
let data = new rpc.MessageSequence();
|
||||
let reply = new rpc.MessageSequence();
|
||||
data.writeInt(1);
|
||||
data.writeInt(99);
|
||||
await mRemote.sendMessageRequest(1, data, reply, option);
|
||||
let msg = reply.readInt();
|
||||
prompt.showToast({
|
||||
message: "onConnectLocalService connect result: " + msg,
|
||||
duration: 3000
|
||||
});
|
||||
},
|
||||
onDisconnect: (element) => {
|
||||
console.log('[dmsDemo] onConnectLocalService onDisconnectDone element: ' + element);
|
||||
},
|
||||
onFailed: (code) => {
|
||||
console.log('[dmsDemo] onConnectLocalService onFailed errCode: ' + code)
|
||||
prompt.showToast({
|
||||
message: "onConnectLocalService onFailed: " + code
|
||||
});
|
||||
},
|
||||
},
|
||||
);
|
||||
console.log('[dmsDemo] onConnectLocalService connectedAbility: ' + connectedAbility)
|
||||
}
|
||||
|
||||
function onConnectRemoteService(index) {
|
||||
function onConnectRemoteService(index: number) {
|
||||
console.info('[dmsDemo] onConnectRemoteService begin');
|
||||
async function onConnectCallback(element, remote) {
|
||||
console.log('[dmsDemo] onConnectRemoteService onConnectDone element: ' + element);
|
||||
console.log('[dmsDemo] onConnectRemoteService onConnectDone remote: ' + remote);
|
||||
mRemote = remote;
|
||||
if (mRemote == null) {
|
||||
prompt.showToast({
|
||||
message: "onConnectRemoteService not connected yet"
|
||||
});
|
||||
return;
|
||||
}
|
||||
let option = new rpc.MessageOption();
|
||||
let data = new rpc.MessageParcel();
|
||||
let reply = new rpc.MessageParcel();
|
||||
data.writeInt(1);
|
||||
data.writeInt(99);
|
||||
await mRemote.sendRequest(1, data, reply, option);
|
||||
let msg = reply.readInt();
|
||||
prompt.showToast({
|
||||
message: "onConnectRemoteService connect result: " + msg,
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
|
||||
function onDisconnectCallback(element) {
|
||||
console.log('[dmsDemo] onConnectRemoteService onDisconnectDone element: ' + element);
|
||||
}
|
||||
|
||||
function onFailedCallback(code) {
|
||||
console.log('[dmsDemo] onConnectRemoteService onFailed errCode: ' + code)
|
||||
prompt.showToast({
|
||||
message: "onConnectRemoteService onFailed: " + code
|
||||
});
|
||||
}
|
||||
|
||||
let numDevices = remoteDeviceModel.deviceList.length;
|
||||
if (numDevices === 0) {
|
||||
@ -270,20 +194,53 @@ function onConnectRemoteService(index) {
|
||||
});
|
||||
return;
|
||||
}
|
||||
let deviceIdForConnect = remoteDeviceModel.deviceList[index - 1].deviceId;
|
||||
console.info('[dmsDemo] onConnectRemoteService deviceId is ' + deviceIdForConnect);
|
||||
connectedAbility = featureAbility.connectAbility(
|
||||
{
|
||||
deviceId: deviceIdForConnect,
|
||||
bundleName: "ohos.dms.js",
|
||||
abilityName: "ohos.dms.js.ServiceAbility",
|
||||
},
|
||||
{
|
||||
onConnect: onConnectCallback,
|
||||
onDisconnect: onDisconnectCallback,
|
||||
onFailed: onFailedCallback,
|
||||
},
|
||||
);
|
||||
let networkId = remoteDeviceModel.deviceList[index - 1].networkId;
|
||||
|
||||
if (networkId) {
|
||||
console.info('[dmsDemo] onConnectRemoteService networkId is ' + networkId);
|
||||
connectedAbility = featureAbility.connectAbility(
|
||||
{
|
||||
deviceId: networkId,
|
||||
bundleName: "ohos.dms.js",
|
||||
abilityName: "ohos.dms.js.ServiceAbility",
|
||||
},
|
||||
{
|
||||
onConnect: async (element, remote) => {
|
||||
console.log('[dmsDemo] onConnectRemoteService onConnectDone element: ' + element);
|
||||
console.log('[dmsDemo] onConnectRemoteService onConnectDone remote: ' + remote);
|
||||
mRemote = remote;
|
||||
if (mRemote == null) {
|
||||
prompt.showToast({
|
||||
message: "onConnectRemoteService not connected yet"
|
||||
});
|
||||
return;
|
||||
}
|
||||
let option = new rpc.MessageOption();
|
||||
let data = new rpc.MessageSequence();
|
||||
let reply = new rpc.MessageSequence();
|
||||
data.writeInt(1);
|
||||
data.writeInt(99);
|
||||
await mRemote.sendMessageRequest(1, data, reply, option);
|
||||
let msg = reply.readInt();
|
||||
prompt.showToast({
|
||||
message: "onConnectRemoteService connect result: " + msg,
|
||||
duration: 3000
|
||||
});
|
||||
},
|
||||
onDisconnect: (element) => {
|
||||
console.log('[dmsDemo] onConnectRemoteService onDisconnectDone element: ' + element);
|
||||
},
|
||||
onFailed: (code) => {
|
||||
console.log('[dmsDemo] onConnectRemoteService onFailed errCode: ' + code)
|
||||
prompt.showToast({
|
||||
message: "onConnectRemoteService onFailed: " + code
|
||||
});
|
||||
},
|
||||
},
|
||||
);
|
||||
} else {
|
||||
console.error('[dmsDemo] onConnectRemoteService get networkId error');
|
||||
}
|
||||
console.log('[dmsDemo] onConnectRemoteService connectedAbility: ' + connectedAbility)
|
||||
}
|
||||
|
||||
@ -305,8 +262,8 @@ async function onDisconnectService() {
|
||||
@CustomDialog
|
||||
struct CustomDialogExample {
|
||||
controller: CustomDialogController
|
||||
cancel: () => void
|
||||
confirm: () => void
|
||||
cancel: () => void = ()=>{}
|
||||
confirm: () => void = ()=>{}
|
||||
|
||||
build() {
|
||||
Column() {
|
||||
@ -315,7 +272,7 @@ struct CustomDialogExample {
|
||||
.enterKeyType(EnterKeyType.Send)
|
||||
.onChange((value) => {
|
||||
console.log('[dmsDemo] TextInput onChange:' + value);
|
||||
mIndex = value;
|
||||
mValue = value;
|
||||
}).onSubmit((enterKey) => {
|
||||
console.log('[dmsDemo] TextInput onSubmit:' + enterKey);
|
||||
}).onEditChanged((isEditing) => {
|
||||
@ -375,15 +332,6 @@ struct Index {
|
||||
return;
|
||||
}
|
||||
onStartRemoteAbility(mIndex);
|
||||
} else if (mOperation === Operation.ContinueAbilityOperation) {
|
||||
console.info('[dmsDemo] call onContinueAbility, device index: ' + mIndex);
|
||||
if (mIndex > mDeviceSize) {
|
||||
prompt.showToast({
|
||||
message: "Please input correct device index."
|
||||
});
|
||||
return;
|
||||
}
|
||||
onContinueAbility(mIndex);
|
||||
} else if (mOperation === Operation.ConnectRemoteServiceOperation){
|
||||
console.info('[dmsDemo] call onConnectRemoteService, device index: ' + mIndex);
|
||||
if (mIndex > mDeviceSize) {
|
||||
@ -469,19 +417,6 @@ struct Index {
|
||||
this.dialogController.open();
|
||||
})
|
||||
|
||||
Button() {
|
||||
Text('ContinueLocalAbility')
|
||||
.fontSize(20)
|
||||
.fontWeight(FontWeight.Bold)
|
||||
}.type(ButtonType.Capsule)
|
||||
.margin({
|
||||
top: 20
|
||||
})
|
||||
.backgroundColor('#0D9FFB')
|
||||
.onClick(() => {
|
||||
onContinueLocalAbility();
|
||||
})
|
||||
|
||||
Button() {
|
||||
Text('ContinueAbility')
|
||||
.fontSize(20)
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
@ -13,50 +13,48 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import deviceManager from '@ohos.distributedHardware.deviceManager';
|
||||
import distributedDeviceManager from '@ohos.distributedDeviceManager';
|
||||
import { Callback } from '@ohos.base';
|
||||
|
||||
var SUBSCRIBE_ID = 100;
|
||||
let SUBSCRIBE_ID = 100;
|
||||
let BundleName = 'com.ohos.distributedmusicplayer';
|
||||
|
||||
export default class RemoteDeviceModel {
|
||||
deviceList = [];
|
||||
discoverList = [];
|
||||
callback;
|
||||
authCallback = null;
|
||||
#deviceManager;
|
||||
deviceList: distributedDeviceManager.DeviceBasicInfo[] = [];
|
||||
discoverList: distributedDeviceManager.DeviceBasicInfo[] = [];
|
||||
callback: Callback<void> = ()=>{};
|
||||
authCallback: Callback<void>| null = null;
|
||||
deviceManager = distributedDeviceManager.createDeviceManager(BundleName);
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
registerDeviceListCallback(callback) {
|
||||
if (typeof (this.#deviceManager) === 'undefined') {
|
||||
registerDeviceListCallback(callback: Callback<void>) {
|
||||
if (typeof (this.deviceManager) === 'undefined') {
|
||||
this.deviceManager = distributedDeviceManager.createDeviceManager(BundleName);
|
||||
if (this.deviceManager) {
|
||||
console.info('create DeviceManager success');
|
||||
this.registerDeviceListCallback_(callback);
|
||||
} else {
|
||||
console.error('create DeviceManager error');
|
||||
}
|
||||
console.log('[dmsDemo] deviceManager.createDeviceManager begin');
|
||||
let self = this;
|
||||
deviceManager.createDeviceManager('com.ohos.distributedmusicplayer', (error, value) => {
|
||||
if (error) {
|
||||
console.error('createDeviceManager failed.');
|
||||
return;
|
||||
}
|
||||
self.#deviceManager = value;
|
||||
self.registerDeviceListCallback_(callback);
|
||||
console.log('[dmsDemo] createDeviceManager callback returned, error=' + error + ' value=' + value);
|
||||
});
|
||||
console.log('[dmsDemo] deviceManager.createDeviceManager end');
|
||||
} else {
|
||||
this.registerDeviceListCallback_(callback);
|
||||
}
|
||||
}
|
||||
|
||||
registerDeviceListCallback_(callback) {
|
||||
registerDeviceListCallback_(callback: Callback<void>) {
|
||||
console.info('[dmsDemo] registerDeviceListCallback');
|
||||
this.callback = callback;
|
||||
if (this.#deviceManager == undefined) {
|
||||
if (this.deviceManager == undefined) {
|
||||
console.error('[dmsDemo] deviceManager has not initialized');
|
||||
this.callback();
|
||||
return;
|
||||
}
|
||||
|
||||
console.info('[dmsDemo] getTrustedDeviceListSync begin');
|
||||
var list = this.#deviceManager.getTrustedDeviceListSync();
|
||||
let list = this.deviceManager.getAvailableDeviceListSync();
|
||||
console.info('[dmsDemo] getTrustedDeviceListSync end, deviceList=' + JSON.stringify(list));
|
||||
if (typeof (list) != 'undefined' && typeof (list.length) != 'undefined') {
|
||||
this.deviceList = list;
|
||||
@ -64,13 +62,14 @@ export default class RemoteDeviceModel {
|
||||
this.callback();
|
||||
console.info('[dmsDemo] callback finished');
|
||||
|
||||
registerDeviceStateChangeCallback();
|
||||
registerDeviceFoundCallback();
|
||||
registerDiscoverFailCallback();
|
||||
registerServiceDieCallback();
|
||||
this.registerDeviceStateChangeCallback();
|
||||
this.registerDeviceFoundCallback();
|
||||
this.registerDiscoverFailCallback();
|
||||
this.registerServiceDieCallback();
|
||||
|
||||
SUBSCRIBE_ID = Math.floor(65536 * Math.random());
|
||||
var info = {
|
||||
console.info('[dmsDemo] startDeviceDiscovery ' + SUBSCRIBE_ID);
|
||||
this.deviceManager.startDiscovering({
|
||||
subscribeId: SUBSCRIBE_ID,
|
||||
mode: 0xAA,
|
||||
medium: 2,
|
||||
@ -78,49 +77,46 @@ export default class RemoteDeviceModel {
|
||||
isSameAccount: false,
|
||||
isWakeRemote: true,
|
||||
capability: 0
|
||||
};
|
||||
console.info('[dmsDemo] startDeviceDiscovery ' + SUBSCRIBE_ID);
|
||||
this.#deviceManager.startDeviceDiscovery(info);
|
||||
})
|
||||
}
|
||||
|
||||
registerDeviceStateChangeCallback() {
|
||||
let self = this;
|
||||
this.#deviceManager.on('deviceStateChange', (data) => {
|
||||
this.deviceManager.on('deviceStateChange', (data) => {
|
||||
console.info('[dmsDemo] deviceStateChange data=' + JSON.stringify(data));
|
||||
switch (data.action) {
|
||||
case 0:
|
||||
self.deviceList[self.deviceList.length] = data.device;
|
||||
console.info('[dmsDemo] online, updated device list=' + JSON.stringify(self.deviceList));
|
||||
self.callback();
|
||||
if (self.authCallback != null) {
|
||||
self.authCallback();
|
||||
self.authCallback = null;
|
||||
this.deviceList[this.deviceList.length] = data.device;
|
||||
console.info('[dmsDemo] online, updated device list=' + JSON.stringify(this.deviceList));
|
||||
this.callback();
|
||||
if (this.authCallback != null) {
|
||||
this.authCallback();
|
||||
this.authCallback = null;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if (self.deviceList.length > 0) {
|
||||
for (var i = 0; i < self.deviceList.length; i++) {
|
||||
if (self.deviceList[i].deviceId === data.device.deviceId) {
|
||||
self.deviceList[i] = data.device;
|
||||
if (this.deviceList.length > 0) {
|
||||
for (let i = 0; i < this.deviceList.length; i++) {
|
||||
if (this.deviceList[i].deviceId === data.device.deviceId) {
|
||||
this.deviceList[i] = data.device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
console.info('[dmsDemo] change, updated device list=' + JSON.stringify(self.deviceList));
|
||||
self.callback();
|
||||
console.info('[dmsDemo] change, updated device list=' + JSON.stringify(this.deviceList));
|
||||
this.callback();
|
||||
break;
|
||||
case 1:
|
||||
if (self.deviceList.length > 0) {
|
||||
var list = [];
|
||||
for (var i = 0; i < self.deviceList.length; i++) {
|
||||
if (self.deviceList[i].deviceId != data.device.deviceId) {
|
||||
list[i] = data.device;
|
||||
if (this.deviceList.length > 0) {
|
||||
let list: distributedDeviceManager.DeviceBasicInfo[] = [];
|
||||
for (let i = 0; i < this.deviceList.length; i++) {
|
||||
if (this.deviceList[i].networkId != data.device.networkId) {
|
||||
list[i] = data.device as distributedDeviceManager.DeviceBasicInfo;
|
||||
}
|
||||
}
|
||||
self.deviceList = list;
|
||||
this.deviceList = list;
|
||||
}
|
||||
console.info('[dmsDemo] offline, updated device list=' + JSON.stringify(data.device));
|
||||
self.callback();
|
||||
this.callback();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -129,72 +125,70 @@ export default class RemoteDeviceModel {
|
||||
}
|
||||
|
||||
registerDeviceFoundCallback() {
|
||||
this.#deviceManager.on('deviceFound', (data) => {
|
||||
this.deviceManager.on('discoverSuccess', (data) => {
|
||||
console.info('[dmsDemo] deviceFound data=' + JSON.stringify(data));
|
||||
console.info('[dmsDemo] deviceFound self.deviceList=' + self.deviceList);
|
||||
console.info('[dmsDemo] deviceFound self.deviceList.length=' + self.deviceList.length);
|
||||
for (var i = 0; i < self.discoverList.length; i++) {
|
||||
if (self.discoverList[i].deviceId === data.device.deviceId) {
|
||||
console.info('[dmsDemo] deviceFound this.deviceList=' + this.deviceList);
|
||||
console.info('[dmsDemo] deviceFound this.deviceList.length=' + this.deviceList.length);
|
||||
for (let i = 0; i < this.discoverList.length; i++) {
|
||||
if (this.discoverList[i].deviceId === data.device.deviceId) {
|
||||
console.info('[dmsDemo] device founded, ignored');
|
||||
return;
|
||||
}
|
||||
}
|
||||
self.discoverList[self.discoverList.length] = data.device;
|
||||
self.callback();
|
||||
this.discoverList[this.discoverList.length] = data.device;
|
||||
this.callback();
|
||||
});
|
||||
}
|
||||
|
||||
registerDiscoverFailCallback() {
|
||||
this.#deviceManager.on('discoverFail', (data) => {
|
||||
this.deviceManager.on('discoverFailure', (data) => {
|
||||
console.info('[dmsDemo] discoverFail data=' + JSON.stringify(data));
|
||||
});
|
||||
}
|
||||
|
||||
registerServiceDieCallback() {
|
||||
this.#deviceManager.on('serviceDie', () => {
|
||||
this.deviceManager.on('serviceDie', () => {
|
||||
console.error('[dmsDemo] serviceDie');
|
||||
});
|
||||
}
|
||||
|
||||
authDevice(deviceId, callback) {
|
||||
authDevice(deviceId: string, callback: Callback<void>) {
|
||||
console.info('[dmsDemo] authDevice ' + deviceId);
|
||||
for (var i = 0; i < this.discoverList.length; i++) {
|
||||
for (let i = 0; i < this.discoverList.length; i++) {
|
||||
if (this.discoverList[i].deviceId === deviceId) {
|
||||
console.info('[dmsDemo] device founded, ignored');
|
||||
let extraInfo = {
|
||||
"targetPkgName": 'com.ohos.distributedmusicplayer',
|
||||
"appName": 'Music',
|
||||
"appDescription": 'Music player application',
|
||||
"business": '0'
|
||||
};
|
||||
let authParam = {
|
||||
"authType": 1,
|
||||
"appIcon": '',
|
||||
"appThumbnail": '',
|
||||
"extraInfo": extraInfo
|
||||
};
|
||||
console.info('[dmsDemo] authenticateDevice ' + JSON.stringify(this.discoverList[i]));
|
||||
let self = this;
|
||||
this.#deviceManager.authenticateDevice(this.discoverList[i], authParam, (err, data) => {
|
||||
if (err) {
|
||||
console.info('[dmsDemo] authenticateDevice failed, err=' + JSON.stringify(err));
|
||||
self.authCallback = null;
|
||||
} else {
|
||||
console.info('[dmsDemo] authenticateDevice succeed, data=' + JSON.stringify(data));
|
||||
self.authCallback = callback;
|
||||
}
|
||||
});
|
||||
this.deviceManager.bindTarget(this.discoverList[i].networkId,
|
||||
{
|
||||
"authType": 1,
|
||||
"appIcon": '',
|
||||
"appThumbnail": '',
|
||||
"extraInfo": {
|
||||
"targetPkgName": 'com.ohos.distributedmusicplayer',
|
||||
"appName": 'Music',
|
||||
"appDescription": 'Music player application',
|
||||
"business": '0'
|
||||
}
|
||||
}, (err, data) => {
|
||||
if (err) {
|
||||
console.info('[dmsDemo] authenticateDevice failed, err=' + JSON.stringify(err));
|
||||
this.authCallback = null;
|
||||
} else {
|
||||
console.info('[dmsDemo] authenticateDevice succeed, data=' + JSON.stringify(data));
|
||||
this.authCallback = callback;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unregisterDeviceListCallback() {
|
||||
console.info('[dmsDemo] stopDeviceDiscovery ' + SUBSCRIBE_ID);
|
||||
this.#deviceManager.stopDeviceDiscovery(SUBSCRIBE_ID);
|
||||
this.#deviceManager.off('deviceStateChange');
|
||||
this.#deviceManager.off('deviceFound');
|
||||
this.#deviceManager.off('discoverFail');
|
||||
this.#deviceManager.off('serviceDie');
|
||||
console.info('[dmsDemo] stopDeviceDiscovery ');
|
||||
this.deviceManager.stopDiscovering();
|
||||
this.deviceManager.off('deviceStateChange');
|
||||
this.deviceManager.off('discoverSuccess');
|
||||
this.deviceManager.off('discoverFailure');
|
||||
this.deviceManager.off('serviceDie');
|
||||
this.deviceList = [];
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"color": [
|
||||
{
|
||||
"name": "start_window_background",
|
||||
"value": "#FFFFFF"
|
||||
}
|
||||
]
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
|
@ -14,11 +14,13 @@
|
||||
*/
|
||||
|
||||
import Ability from '@ohos.app.ability.UIAbility'
|
||||
import abilityAccessCtrl from "@ohos.abilityAccessCtrl";
|
||||
import abilityAccessCtrl, { Permissions } from "@ohos.abilityAccessCtrl";
|
||||
import bundle from '@ohos.bundle';
|
||||
import continuationManager from '@ohos.continuation.continuationManager';
|
||||
import prompt from '@system.prompt';
|
||||
import rpc from '@ohos.rpc'
|
||||
import bundleManager from '@ohos.bundle.bundleManager';
|
||||
import Want from '@ohos.app.ability.Want';
|
||||
|
||||
const SHOW_TOAST_TIME = 3000; // ms
|
||||
const DEVICE_TYPE = "00E";
|
||||
@ -30,62 +32,30 @@ let mRemote;
|
||||
let connectedAbility;
|
||||
let token = -1;
|
||||
|
||||
async function requestPermission() {
|
||||
let permissions: Array<string> = [
|
||||
"ohos.permission.DISTRIBUTED_DATASYNC"
|
||||
];
|
||||
let needGrantPermission = false;
|
||||
let accessManger = abilityAccessCtrl.createAtManager();
|
||||
|
||||
let bundleInfo = await bundle.getApplicationInfo('ohos.samples.continuationmanager', 0, 100)
|
||||
for (const permission of permissions) {
|
||||
console.info('[Demo]app permission query grant status' + permission);
|
||||
try {
|
||||
let grantStatus = await accessManger.verifyAccessToken(bundleInfo.accessTokenId, permission);
|
||||
if (grantStatus === abilityAccessCtrl.GrantStatus.PERMISSION_DENIED) {
|
||||
needGrantPermission = true;
|
||||
break;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[Demo]app permission query grant status error' + JSON.stringify(err));
|
||||
needGrantPermission = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (needGrantPermission) {
|
||||
console.info('[Demo]app permission needGrantPermission');
|
||||
try {
|
||||
await globalThis.context.requestPermissionsFromUser(permissions);
|
||||
} catch (err) {
|
||||
console.error('[Demo]app permission' + JSON.stringify(err));
|
||||
}
|
||||
} else {
|
||||
console.info('[Demo]app permission already granted');
|
||||
}
|
||||
}
|
||||
|
||||
function register() {
|
||||
console.info('[Demo]register begin');
|
||||
continuationManager.register()
|
||||
function registerContinuation(): void {
|
||||
console.info('[Demo]registerContinuation begin');
|
||||
continuationManager.registerContinuation()
|
||||
.then((data) => {
|
||||
console.info('[Demo]register finished, ' + JSON.stringify(data));
|
||||
console.info('[Demo]registerContinuation finished, ' + JSON.stringify(data));
|
||||
token = data;
|
||||
prompt.showToast({
|
||||
message: "[Demo]register token: " + JSON.stringify(data),
|
||||
message: "[Demo]registerContinuation token: " + JSON.stringify(data),
|
||||
duration: SHOW_TOAST_TIME
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('[Demo]register failed, cause: ' + JSON.stringify(err));
|
||||
console.error('[Demo]registerContinuation failed, cause: ' + JSON.stringify(err));
|
||||
prompt.showToast({
|
||||
message: "[Demo]register err: " + err,
|
||||
message: "[Demo]registerContinuation err: " + err,
|
||||
duration: SHOW_TOAST_TIME
|
||||
});
|
||||
});
|
||||
console.info('[Demo]register end');
|
||||
console.info('[Demo]registerContinuation end');
|
||||
}
|
||||
|
||||
function registerWithExtraParams() {
|
||||
function registerWithExtraParams(): void {
|
||||
console.info('[Demo]registerWithExtraParams begin');
|
||||
let continuationExtraParams = {
|
||||
deviceType: [DEVICE_TYPE],
|
||||
@ -95,7 +65,7 @@ function registerWithExtraParams() {
|
||||
continuationMode: continuationManager.ContinuationMode.COLLABORATION_MULTIPLE,
|
||||
authInfo: {"name": "authInfo", "length": 8}
|
||||
};
|
||||
continuationManager.register(continuationExtraParams)
|
||||
continuationManager.registerContinuation(continuationExtraParams)
|
||||
.then((data) => {
|
||||
console.info('[Demo]registerWithExtraParams finished, ' + JSON.stringify(data));
|
||||
token = data;
|
||||
@ -114,7 +84,7 @@ function registerWithExtraParams() {
|
||||
console.info('[Demo]registerWithExtraParams end');
|
||||
}
|
||||
|
||||
function registerWithInvalidFilter() {
|
||||
function registerWithInvalidFilter(): void {
|
||||
console.info('[Demo]registerWithInvalidFilter begin');
|
||||
let continuationExtraParams = {
|
||||
deviceType: [DEVICE_TYPE],
|
||||
@ -124,7 +94,7 @@ function registerWithInvalidFilter() {
|
||||
continuationMode: continuationManager.ContinuationMode.COLLABORATION_MULTIPLE,
|
||||
authInfo: {"name": "authInfo", "length": 8}
|
||||
};
|
||||
continuationManager.register(continuationExtraParams)
|
||||
continuationManager.registerContinuation(continuationExtraParams)
|
||||
.then((data) => {
|
||||
console.info('[Demo]registerWithInvalidFilter finished, ' + JSON.stringify(data));
|
||||
token = data;
|
||||
@ -143,7 +113,7 @@ function registerWithInvalidFilter() {
|
||||
console.info('[Demo]registerWithInvalidFilter end');
|
||||
}
|
||||
|
||||
function registerWithoutFilter() {
|
||||
function registerWithoutFilter(): void {
|
||||
console.info('[Demo]registerWithoutFilter begin');
|
||||
let continuationExtraParams = {
|
||||
deviceType: [DEVICE_TYPE],
|
||||
@ -152,7 +122,7 @@ function registerWithoutFilter() {
|
||||
continuationMode: continuationManager.ContinuationMode.COLLABORATION_MULTIPLE,
|
||||
authInfo: {"name": "authInfo", "length": 8}
|
||||
};
|
||||
continuationManager.register(continuationExtraParams)
|
||||
continuationManager.registerContinuation(continuationExtraParams)
|
||||
.then((data) => {
|
||||
console.info('[Demo]registerWithoutFilter finished, ' + JSON.stringify(data));
|
||||
token = data;
|
||||
@ -171,7 +141,7 @@ function registerWithoutFilter() {
|
||||
console.info('[Demo]registerWithoutFilter end');
|
||||
}
|
||||
|
||||
function registerWithInvalidContinuationMode() {
|
||||
function registerWithInvalidContinuationMode(): void {
|
||||
console.info('[Demo]registerWithInvalidContinuationMode begin');
|
||||
let continuationExtraParams = {
|
||||
deviceType: [DEVICE_TYPE],
|
||||
@ -181,7 +151,7 @@ function registerWithInvalidContinuationMode() {
|
||||
continuationMode: INVALID_CONTINUATION_MODE,
|
||||
authInfo: {"name": "authInfo", "length": 8}
|
||||
};
|
||||
continuationManager.register(continuationExtraParams)
|
||||
continuationManager.registerContinuation(continuationExtraParams)
|
||||
.then((data) => {
|
||||
console.info('[Demo]registerWithInvalidContinuationMode finished, ' + JSON.stringify(data));
|
||||
token = data;
|
||||
@ -200,9 +170,9 @@ function registerWithInvalidContinuationMode() {
|
||||
console.info('[Demo]registerWithInvalidContinuationMode end');
|
||||
}
|
||||
|
||||
function onDeviceConnect() {
|
||||
function onDeviceConnect() : void{
|
||||
console.info('[Demo]onDeviceConnect begin');
|
||||
continuationManager.on("deviceConnect", token, (data) => {
|
||||
continuationManager.on("deviceSelected", token, (data) => {
|
||||
console.info('[Demo]onDeviceConnect len: ' + data.length);
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
console.info('[Demo]onDeviceConnect deviceId: ' + JSON.stringify(data[i].id));
|
||||
@ -218,9 +188,9 @@ function onDeviceConnect() {
|
||||
console.info('[Demo]onDeviceConnect end');
|
||||
}
|
||||
|
||||
function onDeviceConnectWithInvalidToken() {
|
||||
function onDeviceConnectWithInvalidToken(): void {
|
||||
console.info('[Demo]onDeviceConnectWithInvalidToken begin');
|
||||
continuationManager.on("deviceConnect", INVALID_TOKEN, (data) => {
|
||||
continuationManager.on("deviceSelected", INVALID_TOKEN, (data) => {
|
||||
console.info('[Demo]onDeviceConnectWithInvalidToken len: ' + data.length);
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
console.info('[Demo]onDeviceConnectWithInvalidToken deviceId: ' + JSON.stringify(data[i].id));
|
||||
@ -236,9 +206,9 @@ function onDeviceConnectWithInvalidToken() {
|
||||
console.info('[Demo]onDeviceConnectWithInvalidToken end');
|
||||
}
|
||||
|
||||
function onDeviceDisconnect() {
|
||||
function onDeviceDisconnect(): void {
|
||||
console.info('[Demo]onDeviceDisconnect begin');
|
||||
continuationManager.on("deviceDisconnect", token, (data) => {
|
||||
continuationManager.on("deviceUnselected", token, (data) => {
|
||||
console.info('[Demo]onDeviceDisconnect len: ' + data.length);
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
console.info('[Demo]onDeviceDisconnect deviceId: ' + JSON.stringify(data[i]));
|
||||
@ -252,9 +222,9 @@ function onDeviceDisconnect() {
|
||||
console.info('[Demo]onDeviceDisconnect end');
|
||||
}
|
||||
|
||||
function onDeviceDisconnectWithInvalidToken() {
|
||||
function onDeviceDisconnectWithInvalidToken(): void {
|
||||
console.info('[Demo]onDeviceDisconnectWithInvalidToken begin');
|
||||
continuationManager.on("deviceDisconnect", INVALID_TOKEN, (data) => {
|
||||
continuationManager.on("deviceUnselected", INVALID_TOKEN, (data) => {
|
||||
console.info('[Demo]onDeviceDisconnectWithInvalidToken len: ' + data.length);
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
console.info('[Demo]onDeviceDisconnectWithInvalidToken deviceId: ' + JSON.stringify(data[i]));
|
||||
@ -268,27 +238,27 @@ function onDeviceDisconnectWithInvalidToken() {
|
||||
console.info('[Demo]onDeviceDisconnectWithInvalidToken end');
|
||||
}
|
||||
|
||||
function startDeviceManager() {
|
||||
console.info('[Demo]startDeviceManager begin');
|
||||
continuationManager.startDeviceManager(token)
|
||||
function startContinuationDeviceManager(): void {
|
||||
console.info('[Demo]startContinuationDeviceManager begin');
|
||||
continuationManager.startContinuationDeviceManager(token)
|
||||
.then((data) => {
|
||||
console.info('[Demo]startDeviceManager finished, ' + JSON.stringify(data));
|
||||
console.info('[Demo]startContinuationDeviceManager finished, ' + JSON.stringify(data));
|
||||
prompt.showToast({
|
||||
message: "[Demo]startDeviceManager data: " + JSON.stringify(data),
|
||||
message: "[Demo]startContinuationDeviceManager data: " + JSON.stringify(data),
|
||||
duration: SHOW_TOAST_TIME
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('[Demo]startDeviceManager failed, cause: ' + JSON.stringify(err));
|
||||
console.error('[Demo]startContinuationDeviceManager failed, cause: ' + JSON.stringify(err));
|
||||
prompt.showToast({
|
||||
message: "[Demo]startDeviceManager err: " + err,
|
||||
message: "[Demo]startContinuationDeviceManager err: " + err,
|
||||
duration: SHOW_TOAST_TIME
|
||||
});
|
||||
});
|
||||
console.info('[Demo]startDeviceManager end');
|
||||
console.info('[Demo]startContinuationDeviceManager end');
|
||||
}
|
||||
|
||||
function startDeviceManagerWithExtraParam() {
|
||||
function startDeviceManagerWithExtraParam(): void {
|
||||
console.info('[Demo]startDeviceManagerWithExtraParam begin');
|
||||
let continuationExtraParams = {
|
||||
deviceType: [DEVICE_TYPE],
|
||||
@ -298,7 +268,7 @@ function startDeviceManagerWithExtraParam() {
|
||||
continuationMode: continuationManager.ContinuationMode.COLLABORATION_MULTIPLE,
|
||||
authInfo: {"name": "authInfo", "length": 8}
|
||||
};
|
||||
continuationManager.startDeviceManager(token, continuationExtraParams)
|
||||
continuationManager.startContinuationDeviceManager(token, continuationExtraParams)
|
||||
.then((data) => {
|
||||
console.info('[Demo]startDeviceManagerWithExtraParam finished, ' + JSON.stringify(data));
|
||||
prompt.showToast({
|
||||
@ -316,9 +286,9 @@ function startDeviceManagerWithExtraParam() {
|
||||
console.info('[Demo]startDeviceManagerWithExtraParam end');
|
||||
}
|
||||
|
||||
function startDeviceManagerWithInvalidToken() {
|
||||
function startDeviceManagerWithInvalidToken(): void {
|
||||
console.info('[Demo]startDeviceManagerWithInvalidToken begin');
|
||||
continuationManager.startDeviceManager(INVALID_TOKEN)
|
||||
continuationManager.startContinuationDeviceManager(INVALID_TOKEN)
|
||||
.then((data) => {
|
||||
console.info('[Demo]startDeviceManagerWithInvalidToken finished, ' + JSON.stringify(data));
|
||||
prompt.showToast({
|
||||
@ -336,7 +306,7 @@ function startDeviceManagerWithInvalidToken() {
|
||||
console.info('[Demo]startDeviceManagerWithInvalidToken end');
|
||||
}
|
||||
|
||||
function startDeviceManagerWithInvalidFilter() {
|
||||
function startDeviceManagerWithInvalidFilter(): void {
|
||||
console.info('[Demo]startDeviceManagerWithInvalidFilter begin');
|
||||
let continuationExtraParams = {
|
||||
deviceType: [DEVICE_TYPE],
|
||||
@ -346,7 +316,7 @@ function startDeviceManagerWithInvalidFilter() {
|
||||
continuationMode: continuationManager.ContinuationMode.COLLABORATION_MULTIPLE,
|
||||
authInfo: {"name": "authInfo", "length": 8}
|
||||
};
|
||||
continuationManager.startDeviceManager(token, continuationExtraParams)
|
||||
continuationManager.startContinuationDeviceManager(token, continuationExtraParams)
|
||||
.then((data) => {
|
||||
console.info('[Demo]startDeviceManagerWithInvalidFilter finished, ' + JSON.stringify(data));
|
||||
prompt.showToast({
|
||||
@ -364,7 +334,7 @@ function startDeviceManagerWithInvalidFilter() {
|
||||
console.info('[Demo]startDeviceManagerWithInvalidFilter end');
|
||||
}
|
||||
|
||||
function startDeviceManagerWithoutFilter() {
|
||||
function startDeviceManagerWithoutFilter(): void {
|
||||
console.info('[Demo]startDeviceManagerWithoutFilter begin');
|
||||
let continuationExtraParams = {
|
||||
deviceType: [DEVICE_TYPE],
|
||||
@ -373,7 +343,7 @@ function startDeviceManagerWithoutFilter() {
|
||||
continuationMode: continuationManager.ContinuationMode.COLLABORATION_MULTIPLE,
|
||||
authInfo: {"name": "authInfo", "length": 8}
|
||||
};
|
||||
continuationManager.startDeviceManager(token, continuationExtraParams)
|
||||
continuationManager.startContinuationDeviceManager(token, continuationExtraParams)
|
||||
.then((data) => {
|
||||
console.info('[Demo]startDeviceManagerWithoutFilter finished, ' + JSON.stringify(data));
|
||||
prompt.showToast({
|
||||
@ -391,7 +361,7 @@ function startDeviceManagerWithoutFilter() {
|
||||
console.info('[Demo]startDeviceManagerWithoutFilter end');
|
||||
}
|
||||
|
||||
function startDeviceManagerWithInvalidContinuationMode() {
|
||||
function startDeviceManagerWithInvalidContinuationMode(): void {
|
||||
console.info('[Demo]startDeviceManagerWithInvalidContinuationMode begin');
|
||||
let continuationExtraParams = {
|
||||
deviceType: [DEVICE_TYPE],
|
||||
@ -401,7 +371,7 @@ function startDeviceManagerWithInvalidContinuationMode() {
|
||||
continuationMode: INVALID_CONTINUATION_MODE,
|
||||
authInfo: {"name": "authInfo", "length": 8}
|
||||
};
|
||||
continuationManager.startDeviceManager(token, continuationExtraParams)
|
||||
continuationManager.startContinuationDeviceManager(token, continuationExtraParams)
|
||||
.then((data) => {
|
||||
console.info('[Demo]startDeviceManagerWithInvalidContinuationMode finished, ' + JSON.stringify(data));
|
||||
prompt.showToast({
|
||||
@ -419,33 +389,33 @@ function startDeviceManagerWithInvalidContinuationMode() {
|
||||
console.info('[Demo]startDeviceManagerWithInvalidContinuationMode end');
|
||||
}
|
||||
|
||||
function updateConnectStatus() {
|
||||
console.info('[Demo]updateConnectStatus begin');
|
||||
function updateContinuationState(): void {
|
||||
console.info('[Demo]updateContinuationState begin');
|
||||
let deviceId: string = "test deviceId";
|
||||
let deviceConnectStatus = continuationManager.DeviceConnectState.CONNECTED;
|
||||
continuationManager.updateConnectStatus(token, deviceId, deviceConnectStatus)
|
||||
continuationManager.updateContinuationState(token, deviceId, deviceConnectStatus)
|
||||
.then((data) => {
|
||||
console.info('[Demo]updateConnectStatus finished, ' + JSON.stringify(data));
|
||||
console.info('[Demo]updateContinuationState finished, ' + JSON.stringify(data));
|
||||
prompt.showToast({
|
||||
message: "[Demo]updateConnectStatus data: " + JSON.stringify(data),
|
||||
message: "[Demo]updateContinuationState data: " + JSON.stringify(data),
|
||||
duration: SHOW_TOAST_TIME
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('[Demo]updateConnectStatus failed, cause: ' + JSON.stringify(err));
|
||||
console.error('[Demo]updateContinuationState failed, cause: ' + JSON.stringify(err));
|
||||
prompt.showToast({
|
||||
message: "[Demo]updateConnectStatus err: " + err,
|
||||
message: "[Demo]updateContinuationState err: " + err,
|
||||
duration: SHOW_TOAST_TIME
|
||||
});
|
||||
});
|
||||
console.info('[Demo]updateConnectStatus end');
|
||||
console.info('[Demo]updateContinuationState end');
|
||||
}
|
||||
|
||||
function updateConnectStatusWithInvalidToken() {
|
||||
function updateConnectStatusWithInvalidToken(): void {
|
||||
console.info('[Demo]updateConnectStatusWithInvalidToken begin');
|
||||
let deviceId: string = "test deviceId";
|
||||
let deviceConnectStatus = continuationManager.DeviceConnectState.CONNECTED;
|
||||
continuationManager.updateConnectStatus(INVALID_TOKEN, deviceId, deviceConnectStatus)
|
||||
continuationManager.updateContinuationState(INVALID_TOKEN, deviceId, deviceConnectStatus)
|
||||
.then((data) => {
|
||||
console.info('[Demo]updateConnectStatusWithInvalidToken finished, ' + JSON.stringify(data));
|
||||
prompt.showToast({
|
||||
@ -463,11 +433,11 @@ function updateConnectStatusWithInvalidToken() {
|
||||
console.info('[Demo]updateConnectStatusWithInvalidToken end');
|
||||
}
|
||||
|
||||
function updateConnectStatusWithInvalidDeviceId() {
|
||||
function updateConnectStatusWithInvalidDeviceId(): void {
|
||||
console.info('[Demo]updateConnectStatusWithInvalidDeviceId begin');
|
||||
let deviceId: string = "";
|
||||
let deviceConnectStatus = continuationManager.DeviceConnectState.CONNECTED;
|
||||
continuationManager.updateConnectStatus(token, deviceId, deviceConnectStatus)
|
||||
continuationManager.updateContinuationState(token, deviceId, deviceConnectStatus)
|
||||
.then((data) => {
|
||||
console.info('[Demo]updateConnectStatusWithInvalidDeviceId finished, ' + JSON.stringify(data));
|
||||
prompt.showToast({
|
||||
@ -485,11 +455,11 @@ function updateConnectStatusWithInvalidDeviceId() {
|
||||
console.info('[Demo]updateConnectStatusWithInvalidDeviceId end');
|
||||
}
|
||||
|
||||
function updateConnectStatusWithInvalidConnectStatus() {
|
||||
function updateConnectStatusWithInvalidConnectStatus(): void {
|
||||
console.info('[Demo]updateConnectStatusWithInvalidConnectStatus begin');
|
||||
let deviceId: string = "test deviceId";
|
||||
let deviceConnectStatus = INVALID_CONNECT_STATUS;
|
||||
continuationManager.updateConnectStatus(token, deviceId, deviceConnectStatus)
|
||||
continuationManager.updateContinuationState(token, deviceId, deviceConnectStatus)
|
||||
.then((data) => {
|
||||
console.info('[Demo]updateConnectStatusWithInvalidConnectStatus finished, ' + JSON.stringify(data));
|
||||
prompt.showToast({
|
||||
@ -507,53 +477,53 @@ function updateConnectStatusWithInvalidConnectStatus() {
|
||||
console.info('[Demo]updateConnectStatusWithInvalidConnectStatus end');
|
||||
}
|
||||
|
||||
function offDeviceConnect() {
|
||||
function offDeviceConnect(): void {
|
||||
console.info('[Demo]offDeviceConnect begin');
|
||||
continuationManager.off("deviceConnect", token);
|
||||
continuationManager.off("deviceSelected", token);
|
||||
console.info('[Demo]offDeviceConnect end');
|
||||
}
|
||||
|
||||
function offDeviceConnectWithInvalidToken() {
|
||||
function offDeviceConnectWithInvalidToken(): void {
|
||||
console.info('[Demo]offDeviceConnectWithInvalidToken begin');
|
||||
continuationManager.off("deviceConnect", INVALID_TOKEN);
|
||||
continuationManager.off("deviceSelected", INVALID_TOKEN);
|
||||
console.info('[Demo]offDeviceConnectWithInvalidToken end');
|
||||
}
|
||||
|
||||
function offDeviceDisconnect() {
|
||||
function offDeviceDisconnect(): void {
|
||||
console.info('[Demo]offDeviceDisconnect begin');
|
||||
continuationManager.off("deviceDisconnect", token);
|
||||
continuationManager.off("deviceUnselected", token);
|
||||
console.info('[Demo]offDeviceDisconnect end');
|
||||
}
|
||||
|
||||
function offDeviceDisconnectWithInvalidToken() {
|
||||
function offDeviceDisconnectWithInvalidToken(): void {
|
||||
console.info('[Demo]offDeviceDisconnectWithInvalidToken begin');
|
||||
continuationManager.off("deviceDisconnect", INVALID_TOKEN);
|
||||
continuationManager.off("deviceUnselected", INVALID_TOKEN);
|
||||
console.info('[Demo]offDeviceDisconnectWithInvalidToken end');
|
||||
}
|
||||
|
||||
function unregister() {
|
||||
console.info('[Demo]unregister begin');
|
||||
continuationManager.unregister(token)
|
||||
function unregisterContinuation(): void {
|
||||
console.info('[Demo]unregisterContinuation begin');
|
||||
continuationManager.unregisterContinuation(token)
|
||||
.then((data) => {
|
||||
console.info('[Demo]unregister finished, ' + JSON.stringify(data));
|
||||
console.info('[Demo]unregisterContinuation finished, ' + JSON.stringify(data));
|
||||
prompt.showToast({
|
||||
message: "[Demo]unregister data: " + JSON.stringify(data),
|
||||
message: "[Demo]unregisterContinuation data: " + JSON.stringify(data),
|
||||
duration: SHOW_TOAST_TIME
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('[Demo]unregister failed, cause: ' + JSON.stringify(err));
|
||||
console.error('[Demo]unregisterContinuation failed, cause: ' + JSON.stringify(err));
|
||||
prompt.showToast({
|
||||
message: "[Demo]unregister err: " + err,
|
||||
message: "[Demo]unregisterContinuation err: " + err,
|
||||
duration: SHOW_TOAST_TIME
|
||||
});
|
||||
});
|
||||
console.info('[Demo]unregister end');
|
||||
console.info('[Demo]unregisterContinuation end');
|
||||
}
|
||||
|
||||
function unregisterWithInvalidToken() {
|
||||
function unregisterWithInvalidToken(): void {
|
||||
console.info('[Demo]unregisterWithInvalidToken begin');
|
||||
continuationManager.unregister(INVALID_TOKEN)
|
||||
continuationManager.unregisterContinuation(INVALID_TOKEN)
|
||||
.then((data) => {
|
||||
console.info('[Demo]unregisterWithInvalidToken finished, ' + JSON.stringify(data));
|
||||
prompt.showToast({
|
||||
@ -571,15 +541,15 @@ function unregisterWithInvalidToken() {
|
||||
console.info('[Demo]unregisterWithInvalidToken end');
|
||||
}
|
||||
|
||||
function startRemoteAbility() {
|
||||
function startRemoteAbility(): void {
|
||||
let token = -1;
|
||||
continuationManager.register()
|
||||
continuationManager.registerContinuation()
|
||||
.then((data) => {
|
||||
console.info('[Demo]register finished, ' + JSON.stringify(data));
|
||||
console.info('[Demo]registerContinuation finished, ' + JSON.stringify(data));
|
||||
token = data;
|
||||
globalThis.token = data;
|
||||
console.info('[Demo]startRemoteAbility register token = ' + globalThis.token);
|
||||
continuationManager.on("deviceConnect", token, (data) => {
|
||||
console.info('[Demo]startRemoteAbility registerContinuation token = ' + globalThis.token);
|
||||
continuationManager.on("deviceSelected", token, (data) => {
|
||||
console.info('[Demo]registerDeviceConnectCallback len: ' + data.length);
|
||||
if (data.length <= 0) {
|
||||
console.info('[Demo]no selected device');
|
||||
@ -589,22 +559,22 @@ function startRemoteAbility() {
|
||||
startRemoteAbilityWithDeviceId(data[i].id);
|
||||
}
|
||||
});
|
||||
continuationManager.startDeviceManager(token)
|
||||
continuationManager.startContinuationDeviceManager(token)
|
||||
.then((data) => {
|
||||
console.info('[Demo]startDeviceManager finished, ' + JSON.stringify(data));
|
||||
console.info('[Demo]startContinuationDeviceManager finished, ' + JSON.stringify(data));
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('[Demo]startDeviceManager failed, cause: ' + JSON.stringify(err));
|
||||
console.error('[Demo]startContinuationDeviceManager failed, cause: ' + JSON.stringify(err));
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('[Demo]register failed, cause: ' + JSON.stringify(err));
|
||||
console.error('[Demo]registerContinuation failed, cause: ' + JSON.stringify(err));
|
||||
});
|
||||
}
|
||||
|
||||
function startRemoteAbilityWithDeviceId(remoteDeviceId) {
|
||||
function startRemoteAbilityWithDeviceId(remoteDeviceId): void {
|
||||
console.info('[Demo]startRemoteAbilityWithDeviceId remoteDeviceId = ' + remoteDeviceId);
|
||||
let want = {
|
||||
let want: Want = {
|
||||
deviceId: remoteDeviceId,
|
||||
bundleName: 'ohos.samples.continuationmanager',
|
||||
abilityName: 'MainAbility'
|
||||
@ -614,15 +584,15 @@ function startRemoteAbilityWithDeviceId(remoteDeviceId) {
|
||||
});
|
||||
}
|
||||
|
||||
function connectRemoteAbility() {
|
||||
function connectRemoteAbility(): void {
|
||||
let token = -1;
|
||||
continuationManager.register()
|
||||
continuationManager.registerContinuation()
|
||||
.then((data) => {
|
||||
console.info('[Demo]register finished, ' + JSON.stringify(data));
|
||||
console.info('[Demo]registerContinuation finished, ' + JSON.stringify(data));
|
||||
token = data;
|
||||
globalThis.token = data;
|
||||
console.info('[Demo]connectRemoteAbility register token = ' + globalThis.token);
|
||||
continuationManager.on("deviceConnect", token, (data) => {
|
||||
console.info('[Demo]connectRemoteAbility registerContinuation token = ' + globalThis.token);
|
||||
continuationManager.on("deviceSelected", token, (data) => {
|
||||
console.info('[Demo]registerDeviceConnectCallback len: ' + data.length);
|
||||
if (data.length <= 0) {
|
||||
console.info('[Demo]no selected device');
|
||||
@ -632,20 +602,20 @@ function connectRemoteAbility() {
|
||||
connectRemoteAbilityWithDeviceId(data[i].id);
|
||||
}
|
||||
});
|
||||
continuationManager.startDeviceManager(token)
|
||||
continuationManager.startContinuationDeviceManager(token)
|
||||
.then((data) => {
|
||||
console.info('[Demo]startDeviceManager finished, ' + JSON.stringify(data));
|
||||
console.info('[Demo]startContinuationDeviceManager finished, ' + JSON.stringify(data));
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('[Demo]startDeviceManager failed, cause: ' + JSON.stringify(err));
|
||||
console.error('[Demo]startContinuationDeviceManager failed, cause: ' + JSON.stringify(err));
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('[Demo]register failed, cause: ' + JSON.stringify(err));
|
||||
console.error('[Demo]registerContinuation failed, cause: ' + JSON.stringify(err));
|
||||
});
|
||||
}
|
||||
|
||||
function connectRemoteAbilityWithDeviceId(remoteDeviceId) {
|
||||
function connectRemoteAbilityWithDeviceId(remoteDeviceId): void {
|
||||
console.info('[Demo]connectRemoteAbilityWithDeviceId remoteDeviceId = ' + remoteDeviceId);
|
||||
async function onConnectCallback(element, remote) {
|
||||
console.log('[Demo]connectRemoteAbilityWithDeviceId onConnectDone element: ' + element);
|
||||
@ -658,8 +628,8 @@ function connectRemoteAbilityWithDeviceId(remoteDeviceId) {
|
||||
return;
|
||||
}
|
||||
let option = new rpc.MessageOption();
|
||||
let data = new rpc.MessageParcel();
|
||||
let reply = new rpc.MessageParcel();
|
||||
let data = new rpc.MessageSequence();
|
||||
let reply = new rpc.MessageSequence();
|
||||
data.writeInt(1);
|
||||
data.writeInt(99);
|
||||
await mRemote.sendRequest(1, data, reply, option);
|
||||
@ -669,10 +639,10 @@ function connectRemoteAbilityWithDeviceId(remoteDeviceId) {
|
||||
duration: SHOW_TOAST_TIME
|
||||
});
|
||||
}
|
||||
function onDisconnectCallback(element) {
|
||||
function onDisconnectCallback(element): void {
|
||||
console.log('[Demo]connectRemoteAbilityWithDeviceId onDisconnectDone element: ' + element);
|
||||
}
|
||||
function onFailedCallback(code) {
|
||||
function onFailedCallback(code): void {
|
||||
console.log('[Demo]connectRemoteAbilityWithDeviceId onFailed errCode: ' + code);
|
||||
}
|
||||
connectedAbility = globalThis.context.connectAbility(
|
||||
@ -690,6 +660,53 @@ function connectRemoteAbilityWithDeviceId(remoteDeviceId) {
|
||||
}
|
||||
|
||||
export default class MainAbility extends Ability {
|
||||
//获取当前应用的权限的授予状态:grantStatus(授予返回:0,未授予:-1)
|
||||
async getGrantStatus(permission: Permissions): Promise<abilityAccessCtrl.GrantStatus>{
|
||||
let atManager = abilityAccessCtrl.createAtManager();
|
||||
let grantStatus : abilityAccessCtrl.GrantStatus = -1;
|
||||
|
||||
//获取tokenId:
|
||||
let tokenId :number = 0;
|
||||
try {
|
||||
let bundleInfo: bundleManager.BundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
|
||||
let appInfo: bundleManager.ApplicationInfo = bundleInfo.appInfo;
|
||||
tokenId = appInfo.accessTokenId;
|
||||
}catch (err){
|
||||
console.info('Failed to get bundle info for self,cause ${public}s' ,JSON.stringify(err)??'');
|
||||
}
|
||||
|
||||
// 检验应用是否被授予此权限,授予返回:PERMISSION_GRANTED = 0,未授予:PERMISSION_DENIED = -1
|
||||
try {
|
||||
grantStatus = await atManager.checkAccessToken(tokenId,permission);
|
||||
}catch (err){
|
||||
console.info('Failed to check Access Token ,cause %{public}s' ,JSON.stringify(err)??'');
|
||||
}
|
||||
|
||||
return grantStatus;
|
||||
}
|
||||
|
||||
//检验权限授予情况&动态申请权限
|
||||
async checkPermissions():Promise<void>{
|
||||
const permissions:Array<Permissions> = ['ohos.permission.DISTRIBUTED_DATASYNC'];
|
||||
|
||||
let grantStatus:abilityAccessCtrl.GrantStatus = await this.getGrantStatus(permissions[0]);
|
||||
// 检验权限授予情况
|
||||
if(grantStatus === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED){
|
||||
//已授予
|
||||
console.info('Permission already granted.');
|
||||
}else{
|
||||
//未授予,向用户弹框动态申请授权
|
||||
let atManager = abilityAccessCtrl.createAtManager();
|
||||
try {
|
||||
atManager.requestPermissionsFromUser(this.context, ['ohos.permission.DISTRIBUTED_DATASYNC'], (err, data) => {
|
||||
console.info('data: ' + JSON.stringify(data));
|
||||
});
|
||||
}catch (err){
|
||||
console.info('catch err ,' + JSON.stringify(err)??'');
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
onCreate(want, launchParam) {
|
||||
console.log("[Demo] MainAbility onCreate")
|
||||
globalThis.abilityWant = want;
|
||||
@ -703,9 +720,9 @@ export default class MainAbility extends Ability {
|
||||
onWindowStageCreate(windowStage) {
|
||||
// Main window is created, set main page for this ability
|
||||
console.log("[Demo] MainAbility onWindowStageCreate")
|
||||
requestPermission();
|
||||
this.checkPermissions();
|
||||
|
||||
globalThis.register = (()=>{ register(); })
|
||||
globalThis.registerContinuation = (()=>{ registerContinuation(); })
|
||||
globalThis.registerWithExtraParams = (()=>{ registerWithExtraParams(); })
|
||||
globalThis.registerWithInvalidFilter = (()=>{ registerWithInvalidFilter(); })
|
||||
globalThis.registerWithoutFilter = (()=>{ registerWithoutFilter(); })
|
||||
@ -714,13 +731,13 @@ export default class MainAbility extends Ability {
|
||||
globalThis.onDeviceConnectWithInvalidToken = (()=>{ onDeviceConnectWithInvalidToken(); })
|
||||
globalThis.onDeviceDisconnect = (()=>{ onDeviceDisconnect(); })
|
||||
globalThis.onDeviceDisconnectWithInvalidToken = (()=>{ onDeviceDisconnectWithInvalidToken(); })
|
||||
globalThis.startDeviceManager = (()=>{ startDeviceManager(); })
|
||||
globalThis.startContinuationDeviceManager = (()=>{ startContinuationDeviceManager(); })
|
||||
globalThis.startDeviceManagerWithExtraParam = (()=>{ startDeviceManagerWithExtraParam(); })
|
||||
globalThis.startDeviceManagerWithInvalidToken = (()=>{ startDeviceManagerWithInvalidToken(); })
|
||||
globalThis.startDeviceManagerWithInvalidFilter = (()=>{ startDeviceManagerWithInvalidFilter(); })
|
||||
globalThis.startDeviceManagerWithoutFilter = (()=>{ startDeviceManagerWithoutFilter(); })
|
||||
globalThis.startDeviceManagerWithInvalidContinuationMode = (()=>{ startDeviceManagerWithInvalidContinuationMode(); })
|
||||
globalThis.updateConnectStatus = (()=>{ updateConnectStatus(); })
|
||||
globalThis.updateContinuationState = (()=>{ updateContinuationState(); })
|
||||
globalThis.updateConnectStatusWithInvalidToken = (()=>{ updateConnectStatusWithInvalidToken(); })
|
||||
globalThis.updateConnectStatusWithInvalidDeviceId = (()=>{ updateConnectStatusWithInvalidDeviceId(); })
|
||||
globalThis.updateConnectStatusWithInvalidConnectStatus = (()=>{ updateConnectStatusWithInvalidConnectStatus(); })
|
||||
@ -728,7 +745,7 @@ export default class MainAbility extends Ability {
|
||||
globalThis.offDeviceConnectWithInvalidToken = (()=>{ offDeviceConnectWithInvalidToken(); })
|
||||
globalThis.offDeviceDisconnect = (()=>{ offDeviceDisconnect(); })
|
||||
globalThis.offDeviceDisconnectWithInvalidToken = (()=>{ offDeviceDisconnectWithInvalidToken(); })
|
||||
globalThis.unregister = (()=>{ unregister(); })
|
||||
globalThis.unregisterContinuation = (()=>{ unregisterContinuation(); })
|
||||
globalThis.unregisterWithInvalidToken = (()=>{ unregisterWithInvalidToken(); })
|
||||
globalThis.startRemoteAbility = (()=>{ startRemoteAbility(); })
|
||||
globalThis.connectRemoteAbility = (()=>{ connectRemoteAbility(); })
|
||||
@ -749,13 +766,13 @@ export default class MainAbility extends Ability {
|
||||
onBackground() {
|
||||
// Ability has back to background
|
||||
console.log("[Demo] MainAbility onBackground")
|
||||
console.info('[Demo]onBackground unregister, token = ' + globalThis.token);
|
||||
continuationManager.unregister(globalThis.token)
|
||||
console.info('[Demo]onBackground unregisterContinuation, token = ' + globalThis.token);
|
||||
continuationManager.unregisterContinuation(globalThis.token)
|
||||
.then((data) => {
|
||||
console.info('[Demo]unregister finished, ' + JSON.stringify(data));
|
||||
console.info('[Demo]unregisterContinuation finished, ' + JSON.stringify(data));
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('[Demo]unregister failed, cause: ' + JSON.stringify(err));
|
||||
console.error('[Demo]unregisterContinuation failed, cause: ' + JSON.stringify(err));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@ -16,6 +16,7 @@
|
||||
import Extension from '@ohos.app.ability.ServiceExtensionAbility'
|
||||
import rpc from '@ohos.rpc'
|
||||
|
||||
const START_DEVICE_MANAGER = 505;
|
||||
class StubTest extends rpc.RemoteObject {
|
||||
constructor(des) {
|
||||
super(des);
|
||||
|
@ -0,0 +1 @@
|
||||
export const GlobalThis = globalThis;
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
@ -14,13 +14,14 @@
|
||||
*/
|
||||
|
||||
import prompt from '@system.prompt';
|
||||
import { GlobalThis } from './GlobalThis';
|
||||
|
||||
@Entry
|
||||
@Component
|
||||
struct Index {
|
||||
private scroller: Scroller = new Scroller();
|
||||
scroller: Scroller = new Scroller();
|
||||
|
||||
private aboutToAppear() {
|
||||
aboutToAppear() {
|
||||
prompt.showToast({
|
||||
message: "MainAbility aboutToAppear"
|
||||
});
|
||||
@ -46,7 +47,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.register();
|
||||
GlobalThis.register();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -62,7 +63,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.registerWithExtraParams();
|
||||
GlobalThis.registerWithExtraParams();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -78,7 +79,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.registerWithInvalidFilter();
|
||||
GlobalThis.registerWithInvalidFilter();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -94,7 +95,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.registerWithoutFilter();
|
||||
GlobalThis.registerWithoutFilter();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -110,7 +111,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.registerWithInvalidContinuationMode();
|
||||
GlobalThis.registerWithInvalidContinuationMode();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -126,7 +127,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.onDeviceConnect();
|
||||
GlobalThis.onDeviceConnect();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -142,7 +143,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.onDeviceConnectWithInvalidToken();
|
||||
GlobalThis.onDeviceConnectWithInvalidToken();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -158,7 +159,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.onDeviceDisconnect();
|
||||
GlobalThis.onDeviceDisconnect();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -174,7 +175,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.onDeviceDisconnectWithInvalidToken();
|
||||
GlobalThis.onDeviceDisconnectWithInvalidToken();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -190,7 +191,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.startDeviceManager();
|
||||
GlobalThis.startDeviceManager();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -206,7 +207,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.startDeviceManagerWithExtraParam();
|
||||
GlobalThis.startDeviceManagerWithExtraParam();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -222,7 +223,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.startDeviceManagerWithInvalidToken();
|
||||
GlobalThis.startDeviceManagerWithInvalidToken();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -238,7 +239,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.startDeviceManagerWithInvalidFilter();
|
||||
GlobalThis.startDeviceManagerWithInvalidFilter();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -254,7 +255,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.startDeviceManagerWithoutFilter();
|
||||
GlobalThis.startDeviceManagerWithoutFilter();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -270,7 +271,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.startDeviceManagerWithInvalidContinuationMode();
|
||||
GlobalThis.startDeviceManagerWithInvalidContinuationMode();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -286,7 +287,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.updateConnectStatus();
|
||||
GlobalThis.updateConnectStatus();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -302,7 +303,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.updateConnectStatusWithInvalidToken();
|
||||
GlobalThis.updateConnectStatusWithInvalidToken();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -318,7 +319,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.updateConnectStatusWithInvalidDeviceId();
|
||||
GlobalThis.updateConnectStatusWithInvalidDeviceId();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -334,7 +335,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.updateConnectStatusWithInvalidConnectStatus();
|
||||
GlobalThis.updateConnectStatusWithInvalidConnectStatus();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -350,7 +351,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.offDeviceConnect();
|
||||
GlobalThis.offDeviceConnect();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -366,7 +367,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.offDeviceConnectWithInvalidToken();
|
||||
GlobalThis.offDeviceConnectWithInvalidToken();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -382,7 +383,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.offDeviceDisconnect();
|
||||
GlobalThis.offDeviceDisconnect();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -398,7 +399,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.offDeviceDisconnectWithInvalidToken();
|
||||
GlobalThis.offDeviceDisconnectWithInvalidToken();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -414,7 +415,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.unregister();
|
||||
GlobalThis.unregister();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -430,7 +431,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.unregisterWithInvalidToken();
|
||||
GlobalThis.unregisterWithInvalidToken();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -446,7 +447,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.startRemoteAbility();
|
||||
GlobalThis.startRemoteAbility();
|
||||
})
|
||||
|
||||
Button() {
|
||||
@ -462,7 +463,7 @@ struct Index {
|
||||
.width(350)
|
||||
.height(30)
|
||||
.onClick(() => {
|
||||
globalThis.connectRemoteAbility();
|
||||
GlobalThis.connectRemoteAbility();
|
||||
})
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
@ -17,7 +17,7 @@
|
||||
"module": {
|
||||
"name": "entry",
|
||||
"type": "entry",
|
||||
"srcEntrance": "./ets/Application/AbilityStage.ts",
|
||||
"srcEntry": "./ets/Application/AbilityStage.ts",
|
||||
"description": "$string:entry_desc",
|
||||
"mainElement": "MainAbility",
|
||||
"deviceTypes": [
|
||||
@ -28,15 +28,16 @@
|
||||
"deliveryWithInstall": true,
|
||||
"installationFree": false,
|
||||
"pages": "$profile:main_pages",
|
||||
"uiSyntax": "ets",
|
||||
"abilities": [
|
||||
{
|
||||
"name": "MainAbility",
|
||||
"srcEntrance": "./ets/MainAbility/MainAbility.ts",
|
||||
"srcEntry": "./ets/MainAbility/MainAbility.ts",
|
||||
"description": "$string:MainAbility_desc",
|
||||
"icon": "$media:icon",
|
||||
"label": "$string:MainAbility_label",
|
||||
"visible": true,
|
||||
"startWindowIcon": "$media:icon",
|
||||
"startWindowBackground": "$color:start_window_background",
|
||||
"exported": true,
|
||||
"launchType": "standard",
|
||||
"skills": [
|
||||
{
|
||||
@ -56,8 +57,8 @@
|
||||
"icon": "$media:icon",
|
||||
"description": "service",
|
||||
"type": "service",
|
||||
"visible": true,
|
||||
"srcEntrance": "./ets/ServiceExtAbility/ServiceExtAbility.ts"
|
||||
"exported": true,
|
||||
"srcEntry": "./ets/ServiceExtAbility/ServiceExtAbility.ts"
|
||||
}
|
||||
],
|
||||
"requestPermissions": [
|
||||
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"color": [
|
||||
{
|
||||
"name": "start_window_background",
|
||||
"value": "#FFFFFF"
|
||||
}
|
||||
]
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
@ -13,12 +13,15 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import Ability from '@ohos.app.ability.UIAbility'
|
||||
import UIAbility from '@ohos.app.ability.UIAbility'
|
||||
import AbilityConstant from '@ohos.app.ability.AbilityConstant'
|
||||
import accessControl from "@ohos.abilityAccessCtrl";
|
||||
import accessControl, { Permissions } from "@ohos.abilityAccessCtrl";
|
||||
import bundle from '@ohos.bundle';
|
||||
import distributedObject from '@ohos.data.distributedDataObject';
|
||||
import Logger from './Logger';
|
||||
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
|
||||
import bundleManager from '@ohos.bundle.bundleManager';
|
||||
import window from '@ohos.window';
|
||||
|
||||
const BUNDLE_NAME = "com.ohos.continuationManualTestSuite.hmservice"
|
||||
const PERMISSION_REJECT = -1
|
||||
@ -26,62 +29,69 @@ const PERMISSION_REJECT = -1
|
||||
class ContentStorage {
|
||||
}
|
||||
|
||||
const g_object = distributedObject.createDistributedObject({name: undefined});
|
||||
|
||||
export default class MainAbility extends Ability {
|
||||
export default class MainAbility extends UIAbility {
|
||||
|
||||
contentStorage : ContentStorage
|
||||
contentStorage : LocalStorage
|
||||
sessionId : string
|
||||
g_object: distributedObject.DataObject|null = null;
|
||||
|
||||
handleParam(want, launchParam) {
|
||||
if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) {
|
||||
this.sessionId = want.parameters.session
|
||||
Logger.info(`continuation sessionId: ${this.sessionId}`)
|
||||
|
||||
g_object.name = undefined;
|
||||
Logger.info(`set g_object.name undefined`)
|
||||
|
||||
g_object.setSessionId(this.sessionId); // set session id, so it will sync data from remote device
|
||||
Logger.info(`g_object_name = ${g_object.name}`);
|
||||
|
||||
AppStorage.SetOrCreate<string>('ContinueStudy', g_object.name)
|
||||
let workInput = want.parameters.work // get user data from want params
|
||||
Logger.info(`work input ${workInput}`)
|
||||
AppStorage.SetOrCreate<string>('ContinueWork', workInput)
|
||||
this.contentStorage = new ContentStorage();
|
||||
Logger.info('ready to restore');
|
||||
this.context.restoreWindowStage(this.contentStorage);
|
||||
async handleParam(want) {
|
||||
this.g_object = distributedObject.create(this.context, {data:undefined});
|
||||
if (this.g_object) {
|
||||
this.g_object = undefined;
|
||||
}
|
||||
if (!this.g_object) {
|
||||
this.g_object = distributedObject.create(this.context, {name: undefined});
|
||||
}
|
||||
this.sessionId = want.parameters.session
|
||||
Logger.info(`continuation sessionId: ${this.sessionId}`)
|
||||
|
||||
|
||||
await this.g_object.setSessionId(this.sessionId); // set session id, so it will sync data from remote device
|
||||
Logger.info(`this.g_object_name = ${this.g_object['name']}`);
|
||||
|
||||
AppStorage.setOrCreate<string>('ContinueStudy', this.g_object['name'])
|
||||
let workInput = want.parameters.work // get user data from want params
|
||||
Logger.info(`work input ${workInput}`)
|
||||
AppStorage.setOrCreate<string>('ContinueWork', workInput)
|
||||
this.contentStorage = new LocalStorage();
|
||||
Logger.info('ready to restore');
|
||||
this.context.restoreWindowStage(this.contentStorage);
|
||||
}
|
||||
|
||||
onCreate(want, launchParam) {
|
||||
onCreate(want, launchParam: AbilityConstant.LaunchParam) {
|
||||
Logger.info("[Demo] MainAbility onCreate")
|
||||
globalThis.abilityWant = want;
|
||||
this.handleParam(want, launchParam)
|
||||
if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) {
|
||||
this.handleParam(want);
|
||||
}
|
||||
}
|
||||
|
||||
onContinue(wantParam : {[key: string]: any}) {
|
||||
Logger.info(`onContinue version = ${wantParam.version}, targetDevice: ${wantParam.targetDevice}`)
|
||||
let workInput = AppStorage.Get<string>('ContinueWork');
|
||||
let workInput = AppStorage.get<string>('ContinueWork');
|
||||
Logger.info(`onContinue work input = ${workInput}`);
|
||||
|
||||
if (g_object.__sessionId === undefined) {
|
||||
this.sessionId = distributedObject.genSessionId()
|
||||
Logger.info(`onContinue generate new sessionId`)
|
||||
}
|
||||
else {
|
||||
this.sessionId = g_object.__sessionId;
|
||||
if (this.g_object) {
|
||||
this.g_object = undefined;
|
||||
}
|
||||
this.g_object = distributedObject.create(this.context, {name: undefined});
|
||||
|
||||
wantParam["session"] = this.sessionId
|
||||
g_object.name = AppStorage.Get<string>('ContinueStudy');
|
||||
Logger.info(`onContinue sessionId = ${this.sessionId}, name = ${g_object.name}`)
|
||||
g_object.setSessionId(this.sessionId);
|
||||
g_object.save(wantParam.targetDevice, (result, data)=>{
|
||||
Logger.info("save callback");
|
||||
Logger.info("save sessionId " + data.sessionId);
|
||||
Logger.info("save version " + data.version);
|
||||
Logger.info("save deviceId " + data.deviceId);
|
||||
this.sessionId = distributedObject.genSessionId();
|
||||
wantParam['session'] = this.sessionId;
|
||||
if (this.g_object != null) {
|
||||
// 分布式对象设置 sessionId
|
||||
this.g_object.setSessionId(this.sessionId, () => {
|
||||
Logger.info("join session");
|
||||
})
|
||||
}
|
||||
this.g_object['name'] = AppStorage.get<string>('ContinueStudy');
|
||||
Logger.info(`onContinue sessionId = ${this.sessionId}, name = ${this.g_object['name']}`)
|
||||
this.g_object.save(wantParam.targetDevice, (err, result)=>{
|
||||
if (err) {
|
||||
Logger.info(" save callback err: ", JSON.stringify(err));
|
||||
return;
|
||||
}
|
||||
Logger.info('save callback,result = ', JSON.stringify(result));
|
||||
});
|
||||
wantParam["work"] = workInput // set user input data into want params
|
||||
return 0;
|
||||
@ -89,41 +99,56 @@ export default class MainAbility extends Ability {
|
||||
|
||||
onNewWant(want, launchParam) {
|
||||
Logger.info("MainAbility onNewWant")
|
||||
this.handleParam(want, launchParam)
|
||||
if (launchParam.launchReason == AbilityConstant.LaunchReason.CONTINUATION) {
|
||||
this.handleParam(want);
|
||||
}
|
||||
}
|
||||
|
||||
requestPermissions = async () => {
|
||||
let permissions: Array<string> = [
|
||||
"ohos.permission.DISTRIBUTED_DATASYNC"
|
||||
];
|
||||
let needGrantPermission = false
|
||||
let accessManger = accessControl.createAtManager()
|
||||
Logger.info("app permission get bundle info")
|
||||
let bundleInfo = await bundle.getApplicationInfo(BUNDLE_NAME, 0, 100)
|
||||
Logger.info(`app permission query permission ${bundleInfo.accessTokenId.toString()}`)
|
||||
for (const permission of permissions) {
|
||||
Logger.info(`app permission query grant status ${permission}`)
|
||||
try {
|
||||
let grantStatus = await accessManger.verifyAccessToken(bundleInfo.accessTokenId, permission)
|
||||
if (grantStatus === PERMISSION_REJECT) {
|
||||
needGrantPermission = true
|
||||
break;
|
||||
}
|
||||
} catch (err) {
|
||||
Logger.error(`app permission query grant status error ${permission} ${JSON.stringify(err)}`)
|
||||
needGrantPermission = true
|
||||
break;
|
||||
}
|
||||
//获取当前应用的权限的授予状态:grantStatus(授予返回:0,未授予:-1)
|
||||
async getGrantStatus(permission: Permissions): Promise<abilityAccessCtrl.GrantStatus>{
|
||||
let atManager = abilityAccessCtrl.createAtManager();
|
||||
let grantStatus : abilityAccessCtrl.GrantStatus = -1;
|
||||
|
||||
//获取tokenId:
|
||||
let tokenId :number = 0;
|
||||
try {
|
||||
let bundleInfo: bundleManager.BundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
|
||||
let appInfo: bundleManager.ApplicationInfo = bundleInfo.appInfo;
|
||||
tokenId = appInfo.accessTokenId;
|
||||
}catch (err){
|
||||
Logger.info('Failed to get bundle info for self,cause ${public}s' ,JSON.stringify(err)??'');
|
||||
}
|
||||
if (needGrantPermission) {
|
||||
Logger.info("app permission needGrantPermission")
|
||||
|
||||
// 检验应用是否被授予此权限,授予返回:PERMISSION_GRANTED = 0,未授予:PERMISSION_DENIED = -1
|
||||
try {
|
||||
grantStatus = await atManager.checkAccessToken(tokenId,permission);
|
||||
}catch (err){
|
||||
Logger.info('Failed to check Access Token ,cause %{public}s' ,JSON.stringify(err)??'');
|
||||
}
|
||||
|
||||
return grantStatus;
|
||||
}
|
||||
|
||||
//检验权限授予情况&动态申请权限
|
||||
async checkPermissions():Promise<void>{
|
||||
const permissions:Array<Permissions> = ['ohos.permission.DISTRIBUTED_DATASYNC'];
|
||||
|
||||
let grantStatus:abilityAccessCtrl.GrantStatus = await this.getGrantStatus(permissions[0]);
|
||||
// 检验权限授予情况
|
||||
if(grantStatus === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED){
|
||||
//已授予
|
||||
Logger.info('Permission already granted.');
|
||||
}else{
|
||||
//未授予,向用户弹框动态申请授权
|
||||
let atManager = abilityAccessCtrl.createAtManager();
|
||||
try {
|
||||
await this.context.requestPermissionsFromUser(permissions)
|
||||
} catch (err) {
|
||||
Logger.error(`app permission ${JSON.stringify(err)}`)
|
||||
atManager.requestPermissionsFromUser(this.context, ['ohos.permission.DISTRIBUTED_DATASYNC'], (err, data) => {
|
||||
Logger.info('data: ' + JSON.stringify(data));
|
||||
});
|
||||
}catch (err){
|
||||
Logger.info('catch err ,' + JSON.stringify(err)??'');
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
Logger.info("app permission already granted")
|
||||
}
|
||||
}
|
||||
|
||||
@ -131,16 +156,22 @@ export default class MainAbility extends Ability {
|
||||
Logger.info("MainAbility onDestroy")
|
||||
}
|
||||
|
||||
onWindowStageCreate(windowStage) {
|
||||
onWindowStageCreate(windowStage: window.WindowStage) {
|
||||
// Main window is created, set main page for this ability
|
||||
Logger.info("MainAbility onWindowStageCreate")
|
||||
this.requestPermissions()
|
||||
windowStage.setUIContent(this.context, "pages/index", null)
|
||||
Logger.info("MainAbility onWindowStageCreate");
|
||||
this.checkPermissions()
|
||||
windowStage.loadContent('pages/Index', (err, data) => {
|
||||
if (err.code) {
|
||||
Logger.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
|
||||
return;
|
||||
}
|
||||
Logger.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
|
||||
});
|
||||
}
|
||||
|
||||
onWindowStageRestore(windowStage) {
|
||||
Logger.info("MainAbility onWindowStageRestore")
|
||||
this.requestPermissions()
|
||||
onWindowStageRestore(windowStage: window.WindowStage) {
|
||||
Logger.info("MainAbility onWindowStageRestore");
|
||||
this.checkPermissions();
|
||||
}
|
||||
|
||||
onWindowStageDestroy() {
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
@ -13,19 +13,16 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import router from '@system.router';
|
||||
import router from '@ohos.router';
|
||||
import pageName from '../MainAbility/stringConst';
|
||||
import Logger from '../MainAbility/Logger';
|
||||
|
||||
async function routePage(pageIndex) {
|
||||
async function routePage(pageIndex: string) : Promise<void> {
|
||||
Logger.info("routePage");
|
||||
let options = {
|
||||
uri: pageIndex
|
||||
}
|
||||
try {
|
||||
await router.push(options)
|
||||
await router.pushUrl({ url: pageIndex });
|
||||
} catch (err) {
|
||||
Logger.error(`fail callback, code: ${err.code}, msg: ${err.msg}`)
|
||||
Logger.error(`fail callback err ${JSON.stringify(err)}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
@ -13,7 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import router from '@system.router';
|
||||
import router from '@ohos.router';
|
||||
import pageName from '../MainAbility/stringConst';
|
||||
import Logger from '../MainAbility/Logger';
|
||||
|
||||
@ -33,7 +33,7 @@ struct Study {
|
||||
.onChange((value) => {
|
||||
Logger.info(`TextInput onChange: ${value}`);
|
||||
this.inputTxt2 = value;
|
||||
AppStorage.Set('ContinueStudy', value);
|
||||
AppStorage.set('ContinueStudy', value);
|
||||
})
|
||||
.width('50%')
|
||||
.height(50)
|
||||
@ -50,7 +50,7 @@ struct Study {
|
||||
})
|
||||
.backgroundColor('#0D9FFB')
|
||||
.onClick(() => {
|
||||
router.back()
|
||||
router.back();
|
||||
})
|
||||
}
|
||||
.width('100%')
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* Copyright (c) 2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
@ -13,7 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import router from '@system.router';
|
||||
import router from '@ohos.router';
|
||||
import pageName from '../MainAbility/stringConst';
|
||||
import Logger from '../MainAbility/Logger';
|
||||
|
||||
@ -23,9 +23,7 @@ struct Work {
|
||||
private content: string = pageName.page1;
|
||||
@StorageLink('ContinueWork') inputTxt: string = '';
|
||||
|
||||
controller: CustomDialogController
|
||||
cancel: () => void
|
||||
confirm: () => void
|
||||
controller: CustomDialogController|null = null;
|
||||
build() {
|
||||
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
|
||||
Text(`${this.content}`)
|
||||
@ -37,7 +35,7 @@ struct Work {
|
||||
.onChange((value) => {
|
||||
Logger.info(`TextInput onChange: ${value}`);
|
||||
this.inputTxt = value;
|
||||
AppStorage.Set('ContinueWork', value);
|
||||
AppStorage.set('ContinueWork', value);
|
||||
})
|
||||
.width('50%')
|
||||
.height(50)
|
||||
@ -53,7 +51,7 @@ struct Work {
|
||||
})
|
||||
.backgroundColor('#0D9FFB')
|
||||
.onClick(() => {
|
||||
router.back()
|
||||
router.back();
|
||||
})
|
||||
}
|
||||
.width('100%')
|
||||
|
@ -17,7 +17,7 @@
|
||||
"module": {
|
||||
"name": "entry",
|
||||
"type": "entry",
|
||||
"srcEntrance": "./ets/Application/AbilityStage.ts",
|
||||
"srcEntry": "./ets/Application/AbilityStage.ts",
|
||||
"description": "$string:entry_desc",
|
||||
"mainElement": "MainAbility",
|
||||
"deviceTypes": [
|
||||
@ -26,18 +26,18 @@
|
||||
"2in1"
|
||||
],
|
||||
"deliveryWithInstall": true,
|
||||
"installationFree": true,
|
||||
"pages": "$profile:main_pages",
|
||||
"uiSyntax": "ets",
|
||||
"abilities": [
|
||||
{
|
||||
"name": "MainAbility",
|
||||
"srcEntrance": "./ets/MainAbility/MainAbility.ts",
|
||||
"srcEntry": "./ets/MainAbility/MainAbility.ts",
|
||||
"description": "$string:MainAbility_desc",
|
||||
"icon": "$media:icon",
|
||||
"label": "$string:MainAbility_label",
|
||||
"visible": true,
|
||||
"exported": true,
|
||||
"continuable": true,
|
||||
"startWindowIcon": "$media:icon",
|
||||
"startWindowBackground": "$color:start_window_background",
|
||||
"launchType": "singleton",
|
||||
"skills": [
|
||||
{
|
||||
@ -54,7 +54,7 @@
|
||||
"requestPermissions": [
|
||||
{
|
||||
"name": "ohos.permission.DISTRIBUTED_DATASYNC"
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"color": [
|
||||
{
|
||||
"name": "start_window_background",
|
||||
"value": "#FFFFFF"
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user