!44 【HarmonyOS】新增DistributedAbility示例

Merge pull request !44 from adslk/master
This commit is contained in:
鲍亚永 2021-07-06 14:56:42 +00:00 committed by Gitee
commit fdacb4172c
35 changed files with 1362 additions and 0 deletions

View File

@ -0,0 +1,4 @@
# Device and Service Discovery & Connection<a name="EN-US_TOPIC_0000001080120260"></a>
- This sample demonstrates the discovery and connection of near-field devices and services based on distributed capabilities.

View File

@ -0,0 +1,4 @@
# 设备和服务的发现 & 连接<a name="ZH-CN_TOPIC_0000001080120260"></a>
- 本示例主要通过分布式能力,从而实现近场设备和服务的发现、连接。

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply plugin: 'com.huawei.ohos.app'
ohos {
compileSdkVersion 5
defaultConfig {
compatibleSdkVersion 5
}
}
buildscript {
repositories {
maven {
url 'https://repo.huaweicloud.com/repository/maven/'
}
maven {
url 'https://developer.huawei.com/repo/'
}
jcenter()
}
dependencies {
classpath 'com.huawei.ohos:hap:2.4.4.2'
}
}
allprojects {
repositories {
maven {
url 'https://repo.huaweicloud.com/repository/maven/'
}
maven {
url 'https://developer.huawei.com/repo/'
}
jcenter()
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
apply plugin: 'com.huawei.ohos.hap'
ohos {
compileSdkVersion 5
defaultConfig {
compatibleSdkVersion 5
}
buildTypes {
release {
proguardOpt {
proguardEnabled false
rulesFiles 'proguard-rules.pro'
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}

View File

@ -0,0 +1,62 @@
{
"app": {
"bundleName": "ohos.samples.distributedclient",
"version": {
"code": 1000000,
"name": "1.0"
}
},
"deviceConfig": {},
"module": {
"package": "ohos.samples.distributedclient",
"name": ".MainAbility",
"reqCapabilities": [
"video_support"
],
"deviceType": [
"default"
],
"distro": {
"deliveryWithInstall": true,
"moduleName": "client",
"moduleType": "entry",
"installationFree":false
},
"abilities": [
{
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
],
"orientation": "portrait",
"formsEnabled": false,
"name": ".MainAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "$string:app_name",
"type": "page",
"launchType": "standard"
}
],
"reqPermissions": [
{
"name": "ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE"
},
{
"name": "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO"
},
{
"name": "ohos.permission.GET_BUNDLE_INFO"
},
{
"name": "ohos.permission.DISTRIBUTED_DATASYNC"
}
]
}
}

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// IRemoteAgent.idl
// Declare any non-default types here with sequenceable or interface statements
interface ohos.samples.distributedserver.IRemoteAgent {
/*
* Demo service method use some parameters
*/
void setRemoteObject();
}

View File

@ -0,0 +1,183 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.samples.distributedclient;
import ohos.samples.distributedclient.interfaces.DeviceListener;
import ohos.samples.distributedclient.utils.LogUtil;
import ohos.samples.distributedserver.RemoteAgentProxy;
import ohos.samples.distributedserver.RemoteAgentStub;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.app.Context;
import ohos.bundle.ElementName;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;
import ohos.distributedschedule.interwork.IDeviceStateCallback;
import ohos.rpc.IRemoteObject;
import ohos.rpc.RemoteException;
import java.util.List;
/**
* Device Plugin class
*/
public class DevicePlugin {
private static final String TAG = DevicePlugin.class.getSimpleName();
private static final String REMOTE_BUNDLE_NAME = "ohos.samples.distributedserver";
private static final String REMOTE_ABILITY_NAME = "ohos.samples.distributedserver.RemoteAbility";
private static volatile DevicePlugin instance = null;
private List<DeviceInfo> deviceInfoList;
private Context context;
private String remoteDeviceId;
private DeviceListener listener;
private IAbilityConnection connection = new IAbilityConnection() {
@Override
public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int resultCode) {
LogUtil.info(TAG, "onAbilityConnectDone resultCode:" + resultCode);
RemoteAgentProxy remoteAgentProxy = new RemoteAgentProxy(iRemoteObject);
try {
remoteAgentProxy.setRemoteObject();
} catch (RemoteException e) {
LogUtil.error(TAG, "onAbilityConnectDone RemoteException");
}
listener.updateResult("Connect " + (resultCode == 0 ? "Success" : "Failed"));
}
@Override
public void onAbilityDisconnectDone(ElementName elementName, int resultCode) {
LogUtil.info(TAG, "ability disconnect done ");
listener.updateResult("Disconnect " + (resultCode == 0 ? "Success" : "Failed"));
}
};
private IDeviceStateCallback iDeviceStateCallback = new IDeviceStateCallback() {
@Override
public void onDeviceOffline(String deviceId, int deviceType) {
LogUtil.info(TAG, "onDeviceOffline , deviceId: " + deviceId);
DeviceInfo deviceInfo = DeviceManager.getDeviceInfo(deviceId);
listener.updateResult("Offline : " + (deviceInfo == null ? "Unknown" : deviceInfo.getDeviceName()));
}
@Override
public void onDeviceOnline(String deviceId, int deviceType) {
LogUtil.info(TAG, "onDeviceOnline , deviceId: " + deviceId);
DeviceInfo deviceInfo = DeviceManager.getDeviceInfo(deviceId);
listener.updateResult("Online : " + (deviceInfo == null ? "Unknown" : deviceInfo.getDeviceName()));
}
};
/**
* scan remote ability devices list
*
* @return current distribute net device list
*/
public List<DeviceInfo> scanRemoteAbility() {
deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ALL_DEVICE);
return deviceInfoList;
}
/**
* connect remote ability
*
* @param deviceInfo remoteAbility device info.
* @param context context.
*/
public void connectAbility(Context context, DeviceInfo deviceInfo) {
remoteDeviceId = deviceInfo.getDeviceId();
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder().withDeviceId(remoteDeviceId)
.withBundleName(REMOTE_BUNDLE_NAME)
.withAbilityName(REMOTE_ABILITY_NAME)
.withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
.build();
intent.setOperation(operation);
intent.setParam("ClientDeviceName", deviceInfo.getDeviceName());
context.connectAbility(intent, connection);
}
/**
* disconnect remote ability
*
* @return connect result
*/
public boolean stopRemoteConnectedAbility() {
if (connection != null) {
context.disconnectAbility(connection);
LogUtil.info(TAG, "stopRemoteConnectAbility");
}
return true;
}
/**
* init device listener
*
* @param deviceListener device listener
*/
public void initListener(DeviceListener deviceListener) {
listener = deviceListener;
}
/**
* register device state listener
*/
public void registerDeviceStateListener() {
boolean registerResult = DeviceManager.registerDeviceStateCallback(iDeviceStateCallback);
listener.updateResult("Register " + (registerResult ? "Success" : "Failed"));
}
/**
* unregister device state listener
*/
public void unRegisterDeviceStateListener() {
boolean unRegisterResult = DeviceManager.unregisterDeviceStateCallback(iDeviceStateCallback);
listener.updateResult("UnRegister " + (unRegisterResult ? "Success" : "Failed"));
}
/**
* Register context
*
* @param context context.
*/
public void register(Context context) {
this.context = context;
}
/**
* Constructor used to create a DeviceManagerPlugin instance.
*
* @return instance of DeviceManagerPlugin
*/
public static DevicePlugin getInstance() {
if (instance == null) {
synchronized (DevicePlugin.class) {
if (instance == null) {
instance = new DevicePlugin();
}
}
}
return instance;
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.samples.distributedclient;
import ohos.samples.distributedclient.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;
/**
* MainAbility
*/
public class MainAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbilitySlice.class.getName());
requestPermission();
}
private void requestPermission() {
if (verifySelfPermission(SystemPermission.DISTRIBUTED_DATASYNC) != IBundleManager.PERMISSION_GRANTED) {
requestPermissionsFromUser(new String[] {SystemPermission.DISTRIBUTED_DATASYNC}, 0);
}
}
}

View File

@ -0,0 +1,24 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.samples.distributedclient.interfaces;
/**
* WifiEventListener
* Handles the callbacks for wifi events
*/
public interface DeviceListener {
void updateResult(String message);
}

View File

@ -0,0 +1,91 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.samples.distributedclient.provider;
import ohos.samples.distributedclient.DevicePlugin;
import ohos.samples.distributedclient.ResourceTable;
import ohos.samples.distributedclient.slice.MainAbilitySlice;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RecycleItemProvider;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.distributedschedule.interwork.DeviceInfo;
import java.util.List;
/**
* DeviceProvider
*/
public class DeviceProvider extends RecycleItemProvider {
private Context context;
private List<DeviceInfo> deviceInfoList;
private DevicePlugin devicePlugin;
public DeviceProvider(MainAbilitySlice mainAbilitySlice, List<DeviceInfo> deviceInfoList,
DevicePlugin devicePlugin) {
this.context = mainAbilitySlice;
this.deviceInfoList = deviceInfoList;
this.devicePlugin = devicePlugin;
}
@Override
public int getCount() {
return deviceInfoList.size();
}
@Override
public Object getItem(int position) {
return deviceInfoList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
return getItemComponent(position);
}
private Component getItemComponent(int position) {
DeviceInfo item = deviceInfoList.get(position);
Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_list_item, null, false);
Text deviceName = (Text) container.findComponentById(ResourceTable.Id_device_name);
Text deviceId = (Text) container.findComponentById(ResourceTable.Id_device_id);
deviceName.setText(item.getDeviceName() == null ? "Unknown" : item.getDeviceName());
deviceId.setText(item.getDeviceId());
container.setClickedListener(component -> devicePlugin.connectAbility(context, item));
return container;
}
/**
* update provider data
*
* @param data deviceInfo list data
*/
public void updateData(List<DeviceInfo> data) {
this.deviceInfoList = data;
notifyDataChanged();
}
}

View File

@ -0,0 +1,127 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.samples.distributedclient.slice;
import ohos.samples.distributedclient.DevicePlugin;
import ohos.samples.distributedclient.ResourceTable;
import ohos.samples.distributedclient.interfaces.DeviceListener;
import ohos.samples.distributedclient.provider.DeviceProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import java.util.ArrayList;
import java.util.List;
/**
* MainAbilitySlice
*/
public class MainAbilitySlice extends AbilitySlice implements DeviceListener {
private static final int DIALOG_WIDTH = 900;
private static final int DIALOG_HEIGHT = 300;
private static final int DIALOG_CORNER_RADIUS = 30;
private static final int UPDATE_RESULT_MESSAGE = 0x1000001;
private DevicePlugin devicePlugin;
private List<DeviceInfo> deviceInfoList = new ArrayList<>();
private DeviceProvider deviceProvider;
private String dialogMessage;
private EventHandler handler = new EventHandler(EventRunner.current()) {
@Override
protected void processEvent(InnerEvent event) {
showDialog(dialogMessage);
scanRemoteAbility(false);
}
};
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_main_layout);
initDevicePlugin();
initComponents();
}
private void initDevicePlugin() {
devicePlugin = DevicePlugin.getInstance();
devicePlugin.register(this);
devicePlugin.initListener(this);
}
private void initComponents() {
Component scanBtn = findComponentById(ResourceTable.Id_scan_btn);
Component registerBtn = findComponentById(ResourceTable.Id_register_change_btn);
Component unRegisterBtn = findComponentById(ResourceTable.Id_unregister_btn);
Component unDisconnectBtn = findComponentById(ResourceTable.Id_disconnect_btn);
registerBtn.setClickedListener(component -> devicePlugin.registerDeviceStateListener());
unRegisterBtn.setClickedListener(component -> devicePlugin.unRegisterDeviceStateListener());
unDisconnectBtn.setClickedListener(component -> devicePlugin.stopRemoteConnectedAbility());
scanBtn.setClickedListener(component -> scanRemoteAbility(true));
deviceProvider = new DeviceProvider(this, deviceInfoList, devicePlugin);
ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_container_list);
listContainer.setItemProvider(deviceProvider);
}
private void scanRemoteAbility(boolean showToast) {
deviceInfoList = devicePlugin.scanRemoteAbility();
deviceProvider.updateData(deviceInfoList);
if (showToast) {
showTip("scan deviceInfoList size : " + deviceInfoList.size());
}
}
private void showTip(String msg) {
new ToastDialog(this).setContentText(msg).show();
}
@Override
public void updateResult(String message) {
dialogMessage = message;
handler.sendEvent(UPDATE_RESULT_MESSAGE);
}
private void showDialog(String message) {
Component container = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_dialog_layout, null, false);
Text content = (Text) container.findComponentById(ResourceTable.Id_message);
content.setText(message);
CommonDialog commonDialog = new CommonDialog(this);
commonDialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
commonDialog.setCornerRadius(DIALOG_CORNER_RADIUS);
commonDialog.setContentCustomComponent(container);
commonDialog.show();
Button btnOk = (Button) container.findComponentById(ResourceTable.Id_btn_ok);
btnOk.setClickedListener(component -> commonDialog.hide());
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.samples.distributedclient.utils;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import java.util.Locale;
/**
* Log utils
*/
public class LogUtil {
private static final String TAG_LOG = "[DistributedClient] ";
private static final int DOMAIN_ID = 0xD000F00;
private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, DOMAIN_ID, LogUtil.TAG_LOG);
private static final String LOG_FORMAT = "%s: %s";
private LogUtil() {
}
/**
* Print info log
*
* @param tag log tag
* @param msg log message
*/
public static void info(String tag, String msg) {
HiLog.info(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg));
}
/**
* Print error log
*
* @param tag log tag
* @param msg log message
*/
public static void error(String tag, String msg) {
HiLog.error(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg));
}
}

View File

@ -0,0 +1,12 @@
{
"string": [
{
"name": "app_name",
"value": "DistributedAbility"
},
{
"name": "mainability_description",
"value": "hap sample empty page"
}
]
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2021 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
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:shape="rectangle">
<corners
ohos:radius="15"/>
<solid
ohos:color="#ff00aa88"/>
<stroke
ohos:color="#ff00ff7f"
ohos:width="1vp"/>
</shape>

View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2021 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
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:width="300vp"
ohos:padding="5vp"
ohos:orientation="vertical"
ohos:height="match_content">
<Text
ohos:id="$+id:message"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:top_margin="10vp"
ohos:height="match_content"
ohos:text_size="16fp"
ohos:text_color="#0000ff"/>
<Text
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:height="1vp"
ohos:text_size="18fp"
ohos:top_margin="5vp"
ohos:bottom_margin="5vp"
ohos:background_element="#220000ff"/>
<Button
ohos:id="$+id:btn_ok"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:margin="10vp"
ohos:height="match_content"
ohos:text="O K"
ohos:text_size="18fp"
ohos:text_color="#0000ff"/>
</DirectionalLayout>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2021 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
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:width="match_parent"
ohos:height="match_content">
<Text
ohos:id="$+id:device_name"
ohos:text_color="#000000"
ohos:width="match_parent"
ohos:height="match_content"
ohos:text_size="15fp"
ohos:top_margin="5vp"/>
<Text
ohos:id="$+id:device_id"
ohos:text_color="#000000"
ohos:width="match_parent"
ohos:top_margin="5vp"
ohos:multiple_lines="true"
ohos:height="match_content"
ohos:text_size="12fp"/>
</DirectionalLayout>

View File

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2021 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
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:width="match_parent"
ohos:orientation="vertical"
ohos:padding="30vp"
ohos:height="match_parent">
<DirectionalLayout
ohos:width="match_parent"
ohos:orientation="vertical"
ohos:left_padding="20dp"
ohos:right_padding="20dp"
ohos:height="match_content">
<Button
ohos:id="$+id:scan_btn"
ohos:width="match_parent"
ohos:text="ScanRemoteAbility"
ohos:top_margin="20vp"
ohos:text_size="20fp"
ohos:background_element="$graphic:bg"
ohos:height="40vp"/>
<Button
ohos:id="$+id:register_change_btn"
ohos:width="match_parent"
ohos:background_element="$graphic:bg"
ohos:text="RegisterChangeListener"
ohos:top_margin="20vp"
ohos:text_size="20fp"
ohos:height="40vp"/>
<Button
ohos:id="$+id:unregister_btn"
ohos:width="match_parent"
ohos:text="UnRegisterChangeListener"
ohos:background_element="$graphic:bg"
ohos:top_margin="20vp"
ohos:text_size="20fp"
ohos:height="40vp"/>
<Button
ohos:id="$+id:disconnect_btn"
ohos:width="match_parent"
ohos:text="DisconnectRemoteAbility"
ohos:top_margin="20vp"
ohos:text_size="20fp"
ohos:background_element="$graphic:bg"
ohos:height="40vp"/>
</DirectionalLayout>
<DirectionalLayout
ohos:id="$+id:list_parent"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:orientation="vertical"
ohos:top_margin="20vp"
ohos:left_margin="10vp">
<ListContainer
ohos:id="$+id:container_list"
ohos:width="match_parent"
ohos:height="match_content"/>
</DirectionalLayout>
</DirectionalLayout>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 223 KiB

View File

@ -0,0 +1,24 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
apply plugin: 'com.huawei.ohos.hap'
ohos {
compileSdkVersion 5
defaultConfig {
compatibleSdkVersion 5
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}

View File

@ -0,0 +1,66 @@
{
"app": {
"bundleName": "ohos.samples.distributedserver",
"version": {
"code": 1000000,
"name": "1.0"
}
},
"deviceConfig": {},
"module": {
"package": "ohos.samples.distributedserver",
"name": ".MainAbility",
"reqCapabilities": [
"video_support"
],
"deviceType": [
"default"
],
"distro": {
"deliveryWithInstall": true,
"moduleName": "server",
"moduleType": "entry",
"installationFree":false
},
"abilities": [
{
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
],
"orientation": "portrait",
"formsEnabled": false,
"name": ".MainAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "$string:app_name",
"type": "page",
"launchType": "standard"
},
{
"visible": true,
"name": ".RemoteAbility",
"icon": "$media:icon",
"description": "$string:serviceability_description",
"type": "service"
}
],
"reqPermissions": [
{
"name": "ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE"
},
{
"name": "ohos.permission.DISTRIBUTED_DATASYNC"
},
{
"name": "ohos.permission.SYSTEM_FLOAT_WINDOW"
}
]
}
}

View File

@ -0,0 +1,24 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// IRemoteAgent.idl
// Declare any non-default types here with sequenceable or interface statements
interface ohos.samples.distributedserver.IRemoteAgent {
/*
* Demo service method use some parameters
*/
void setRemoteObject();
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.samples.distributedserver;
import ohos.samples.distributedserver.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;
/**
* MainAbility
*/
public class MainAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbilitySlice.class.getName());
requestPermission();
}
private void requestPermission() {
if (verifySelfPermission(SystemPermission.DISTRIBUTED_DATASYNC) != IBundleManager.PERMISSION_GRANTED) {
requestPermissionsFromUser(new String[] {SystemPermission.DISTRIBUTED_DATASYNC}, 0);
}
}
}

View File

@ -0,0 +1,106 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.samples.distributedserver;
import ohos.samples.distributedserver.utils.LogUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.CommonDialog;
import ohos.event.notification.NotificationHelper;
import ohos.event.notification.NotificationRequest;
import ohos.rpc.IRemoteObject;
import ohos.rpc.RemoteException;
/**
* Custom service type atomic ability for distributed input method.
*/
public final class RemoteAbility extends Ability {
private static final String TAG = RemoteAbility.class.getSimpleName();
private static final String ABILITY_DESCRIPTION = "ohos.samples.distributedserver.RemoteAbility";
private static final int NOTIFICATION_ID = 10013;
private static final int DIALOG_WIDTH = 900;
private static final int DIALOG_CORNER_RADIUS = 30;
private RemoteAgentStub remoteAgent = new RemoteAgentStub(ABILITY_DESCRIPTION) {
@Override
public void setRemoteObject() {
LogUtil.info(TAG, " RemoteAgentStub setRemoteObject");
}
};
@Override
public IRemoteObject onConnect(Intent intent) {
String connectedDeviceName = intent.getStringParam("ClientDeviceName");
LogUtil.info(TAG, "onConnect Success , ConnectedDeviceName : " + connectedDeviceName);
super.onConnect(intent);
showDialog("Connect Success");
showNotification(this, NOTIFICATION_ID, "DistributedServer message", "Connect Success");
return remoteAgent;
}
@Override
public void onDisconnect(Intent intent) {
super.onDisconnect(intent);
showNotification(this, NOTIFICATION_ID, "DistributedServer message", "Disconnected");
showDialog("Disconnected");
cancelBackgroundRunning();
}
@Override
public void onStop() {
LogUtil.info(TAG, "onStop");
super.onStop();
cancelBackgroundRunning();
}
private void showNotification(Ability ability, int notificationId, String title, String text) {
NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
content.setTitle(title).setText(text);
NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(
content);
NotificationRequest request = new NotificationRequest(notificationId);
request.setContent(notificationContent);
ability.keepBackgroundRunning(notificationId, request);
try {
NotificationHelper.publishNotification(request);
} catch (RemoteException e) {
LogUtil.error(TAG, "Exception in showNotification");
}
}
private void showDialog(String message) {
Component container = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_dialog_layout, null, false);
Text content = (Text) container.findComponentById(ResourceTable.Id_message);
content.setText(message);
CommonDialog commonDialog = new CommonDialog(this);
commonDialog.setSize(DIALOG_WIDTH, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
commonDialog.setCornerRadius(DIALOG_CORNER_RADIUS);
commonDialog.setContentCustomComponent(container);
commonDialog.show();
Button btnOk = (Button) container.findComponentById(ResourceTable.Id_btn_ok);
btnOk.setClickedListener(component -> commonDialog.hide());
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.samples.distributedserver.slice;
import ohos.samples.distributedserver.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
/**
* MainAbilitySlice
*/
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_main_layout);
}
}

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ohos.samples.distributedserver.utils;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import java.util.Locale;
/**
* Custom log util.
*/
public class LogUtil {
private static final String TAG_LOG = "DistributedServer";
private static final int DOMAIN_ID = 0xD000F00;
private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, DOMAIN_ID, LogUtil.TAG_LOG);
private static final String LOG_FORMAT = "%s: %s";
private static final String HI_LOG_FORMAT = "%{public}s";
private LogUtil() {
/* Do nothing */
}
/**
* Print info log message
*
* @param className class name
* @param msg info log message
*/
public static void info(String className, String msg) {
HiLog.info(LABEL_LOG, HI_LOG_FORMAT, String.format(Locale.ROOT, LOG_FORMAT, className, msg));
}
/**
* Print error log message
*
* @param className class name
* @param msg error log message
*/
public static void error(String className, String msg) {
HiLog.error(LABEL_LOG, HI_LOG_FORMAT, String.format(Locale.ROOT, LOG_FORMAT, className, msg));
}
}

View File

@ -0,0 +1,16 @@
{
"string": [
{
"name": "app_name",
"value": "DistributedServer"
},
{
"name": "mainability_description",
"value": "hap sample empty page"
},
{
"name": "serviceability_description",
"value": "hap sample empty service"
}
]
}

View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2021 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
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:width="300vp"
ohos:padding="5vp"
ohos:orientation="vertical"
ohos:height="match_content">
<Text
ohos:id="$+id:message"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:top_margin="10vp"
ohos:height="match_content"
ohos:text_size="16fp"
ohos:text_color="#0000ff"/>
<Text
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:height="1vp"
ohos:text_size="18fp"
ohos:top_margin="5vp"
ohos:bottom_margin="5vp"
ohos:background_element="#220000ff"/>
<Button
ohos:id="$+id:btn_ok"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:margin="10vp"
ohos:height="match_content"
ohos:text="O K"
ohos:text_size="18fp"
ohos:text_color="#0000ff"/>
</DirectionalLayout>

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2021 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
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:width="match_parent"
ohos:alignment="center"
ohos:padding="5vp"
ohos:orientation="vertical"
ohos:height="match_parent">
<Text
ohos:width="match_content"
ohos:text="Distribute Server"
ohos:height="match_content"
ohos:text_size="18fp"
ohos:text_color="#0000ff"/>
</DirectionalLayout>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

View File

@ -0,0 +1,15 @@
/*
* Copyright (c) 2021 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
include ':client', ':server'