add sample for AbilityForm

Signed-off-by: gaohui <gaohui34@huawei.com>
This commit is contained in:
gaohui 2021-07-06 21:38:48 +08:00
parent 3a9e553ba9
commit 7849998a62
23 changed files with 684 additions and 0 deletions

View File

@ -0,0 +1,5 @@
# AbilityForm<a name="EN-US_TOPIC_0000001080471308"></a>
- An **AbilityForm** displays brief information about a Page ability on the UI of another application and provides basic interactive features.
There are two roles involved in the presentation of an **AbilityForm** depending on who provides or displays the content. The AbilityForm supplier provides the content to display, and the AbilityForm client displays the content. A typical example AbilityForm client is the home screen.

View File

@ -0,0 +1,5 @@
# Ability Form<a name="ZH-CN_TOPIC_0000001080471308"></a>
- Ability Form即表单是 Page 形态的 Ability 的一种界面展示形式,用于嵌入到其他应用中作为其界面的一部分显示,并支持基础的交互功能。
表单使用方作为表单展示的宿主负责显示表单,表单使用方的典型应用就是桌面。

View File

@ -0,0 +1,18 @@
apply plugin: 'com.huawei.ohos.hap'
ohos {
compileSdkVersion 5
defaultConfig {
compatibleSdkVersion 4
}
buildTypes {
release {
proguardOpt {
proguardEnabled false
rulesFiles 'proguard-rules.pro'
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}

View File

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

View File

@ -0,0 +1,92 @@
/*
* 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.abilityformprovider;
import ohos.samples.abilityformprovider.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilityForm;
import ohos.aafwk.ability.OnClickListener;
import ohos.aafwk.ability.ViewsStatus;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.event.intentagent.IntentAgent;
import ohos.event.intentagent.IntentAgentConstant;
import ohos.event.intentagent.IntentAgentHelper;
import ohos.event.intentagent.IntentAgentInfo;
import ohos.event.intentagent.TriggerInfo;
import java.util.ArrayList;
import java.util.List;
/**
* FormAbility
*/
public class FormAbility extends Ability {
private static int clickTimes = 0;
@Override
protected AbilityForm onCreateForm() {
AbilityForm abilityForm = new AbilityForm(ResourceTable.Layout_form_layout, this);
abilityForm.setText(ResourceTable.Id_content_text, generateFormText());
abilityForm.registerViewListener(ResourceTable.Id_content_text, new OnClickListener() {
@Override
public void onClick(int viewId, AbilityForm form, ViewsStatus viewsStatus) {
clickTimes++;
form.setText(viewId, generateFormText());
if (MainAbilitySlice.text != null) {
MainAbilitySlice.text.setText("Client.Counter: " + clickTimes);
}
}
});
abilityForm.registerViewListener(ResourceTable.Id_container_layout, new OnClickListener() {
@Override
public void onClick(int i, AbilityForm abilityForm, ViewsStatus viewsStatus) {
setIntentAgent();
}
});
return abilityForm;
}
private void setIntentAgent() {
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder().withDeviceId("")
.withBundleName("ohos.samples.abilityformprovider")
.withAbilityName("ohos.samples.abilityformprovider.MainAbility")
.build();
intent.setOperation(operation);
List<Intent> intentList = new ArrayList<>();
intentList.add(intent);
int requestCode = 200;
List<IntentAgentConstant.Flags> flags = new ArrayList<>();
flags.add(IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG);
IntentAgentInfo paramsInfo = new IntentAgentInfo(requestCode, IntentAgentConstant.OperationType.START_ABILITY,
flags, intentList, null);
IntentAgent agent = IntentAgentHelper.getIntentAgent(this, paramsInfo);
IntentAgentHelper.triggerIntentAgent(this, agent, null, null, new TriggerInfo(null, null, null, requestCode));
}
/**
* get clickTimes
*
* @return clickTimes
*/
public static String generateFormText() {
return "Total: " + clickTimes;
}
}

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.abilityformprovider;
import ohos.samples.abilityformprovider.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
/**
* MainAbility
*/
public class MainAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbilitySlice.class.getName());
}
}

View File

@ -0,0 +1,64 @@
/*
* 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.abilityformprovider.slice;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;
import static ohos.agp.utils.LayoutAlignment.CENTER;
import ohos.samples.abilityformprovider.FormAbility;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
/**
* FormSlice
*/
public class MainAbilitySlice extends AbilitySlice {
/**
* Text show result
*/
public static Text text;
private DirectionalLayout directionalLayout;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
initComponents();
super.setUIContent(directionalLayout);
}
private void initComponents() {
directionalLayout = new DirectionalLayout(this);
directionalLayout.setWidth(MATCH_PARENT);
directionalLayout.setHeight(MATCH_PARENT);
directionalLayout.setAlignment(CENTER);
ShapeElement background = new ShapeElement();
background.setShape(ShapeElement.RECTANGLE);
background.setRgbColor(new RgbColor(0xFFFFFFFF));
directionalLayout.setBackground(background);
text = new Text(this);
text.setTextSize(60);
text.setText(FormAbility.generateFormText());
directionalLayout.addComponent(text);
}
}

View File

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

View File

@ -0,0 +1,39 @@
<?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:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:background_element="#0d000000"
ohos:id="$+id:container_layout"
ohos:orientation="vertical">
<Text
ohos:id="$+id:title_text"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="Counter"
ohos:text_color="#ff555555"
ohos:text_size="20fp"/>
<Text
ohos:id="$+id:content_text"
ohos:height="match_content"
ohos:width="match_content"
ohos:text_color="#ff0000ff"
ohos:text_size="20fp"/>
</DirectionalLayout>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

View File

@ -0,0 +1,33 @@
apply plugin: 'com.huawei.ohos.app'
ohos {
compileSdkVersion 5
defaultConfig {
compatibleSdkVersion 4
}
}
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,18 @@
apply plugin: 'com.huawei.ohos.hap'
ohos {
compileSdkVersion 5
defaultConfig {
compatibleSdkVersion 4
}
buildTypes {
release {
proguardOpt {
proguardEnabled false
rulesFiles 'proguard-rules.pro'
}
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}

View File

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

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.abilityform;
import ohos.samples.abilityform.slice.FormAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
/**
* MainAbility
*/
public class MainAbility extends Ability {
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(FormAbilitySlice.class.getName());
}
}

View File

@ -0,0 +1,130 @@
/*
* 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.abilityform.slice;
import ohos.samples.abilityform.ResourceTable;
import ohos.aafwk.ability.AbilityForm;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.account.AccountAbility;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.bundle.AbilityInfo;
import ohos.bundle.IBundleManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.RemoteException;
import java.util.List;
/**
* FormAbilitySlice
*/
public class FormAbilitySlice extends AbilitySlice {
private static final String TAG = FormAbilitySlice.class.getSimpleName();
private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG);
private DirectionalLayout containerLayout;
private AbilityInfo abilityInfo;
private AbilityForm abilityForm;
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_form_layout);
initComponents();
}
private void initComponents() {
findComponentById(ResourceTable.Id_load_form_button).setClickedListener(this::loadForm);
findComponentById(ResourceTable.Id_remove_form_button).setClickedListener(this::removeForm);
containerLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_container_layout);
}
private void removeForm(Component component) {
if (abilityForm == null) {
return;
}
releaseAbilityForm(abilityForm);
containerLayout.removeAllComponents();
}
private void loadForm(Component component) {
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder().withDeviceId("")
.withBundleName("ohos.samples.abilityformprovider")
.withAbilityName("ohos.samples.abilityformprovider.FormAbility")
.build();
intent.setOperation(operation);
try {
List<AbilityInfo> abilityInfos = getBundleManager().queryAbilityByIntent(intent,
IBundleManager.GET_ABILITY_INFO_WITH_PERMISSION,
AccountAbility.getAccountAbility().getOsAccountLocalIdFromProcess());
if (abilityInfos == null || abilityInfos.size() <= 0) {
new ToastDialog(this).setText("No abilityForm found").show();
return;
}
HiLog.info(LABEL_LOG, "%{public}s", "Found ability: " + abilityInfos.size());
abilityInfo = abilityInfos.get(0);
} catch (RemoteException exception) {
HiLog.error(LABEL_LOG, "%{public}s", "loadForm: remoteException");
}
showForm(intent);
}
private void showForm(Intent intent) {
TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
globalTaskDispatcher.asyncDispatch(() -> {
FormAbilitySlice.this.acquireAbilityFormAsync(intent, new AbilityForm.OnAcquiredCallback() {
@Override
public void onAcquired(AbilityForm form) {
if (form == null) {
HiLog.info(LABEL_LOG, "%{public}s", "showForm: Form is null");
return;
}
abilityForm = form;
addForm();
}
@Override
public void onDestroyed(AbilityForm form) {
abilityForm = null;
HiLog.error(LABEL_LOG, "%{public}s", "showForm: AbilityForm on destroy");
}
});
});
}
private void addForm() {
containerLayout.removeAllComponents();
DirectionalLayout directionalLayout = new DirectionalLayout(FormAbilitySlice.this, null);
directionalLayout.setHeight(abilityInfo.getDefaultFormHeight());
directionalLayout.setWidth(abilityInfo.getDefaultFormWidth());
directionalLayout.setAlignment(LayoutAlignment.CENTER);
directionalLayout.addComponent(abilityForm.getComponent());
containerLayout.addComponent(directionalLayout);
}
}

View File

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

View File

@ -0,0 +1,22 @@
<?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="75"/>
<solid
ohos:color="#0d000000"/>
</shape>

View File

@ -0,0 +1,56 @@
<?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:height="match_parent"
ohos:width="match_parent"
ohos:alignment="bottom"
ohos:orientation="vertical">
<DirectionalLayout
ohos:id="$+id:container_layout"
ohos:height="0vp"
ohos:width="match_parent"
ohos:alignment="center"
ohos:weight="1"/>
<Button
ohos:id="$+id:load_form_button"
ohos:height="match_content"
ohos:width="match_parent"
ohos:bottom_margin="24vp"
ohos:layout_alignment="bottom"
ohos:left_margin="24vp"
ohos:background_element="$graphic:button_background"
ohos:padding="10vp"
ohos:right_margin="24vp"
ohos:text="Load Form"
ohos:text_size="16fp"/>
<Button
ohos:id="$+id:remove_form_button"
ohos:height="match_content"
ohos:width="match_parent"
ohos:bottom_margin="24vp"
ohos:layout_alignment="bottom"
ohos:left_margin="24vp"
ohos:padding="10vp"
ohos:background_element="$graphic:button_background"
ohos:right_margin="24vp"
ohos:text="Remove Form"
ohos:text_size="16fp"
ohos:top_margin="10vp"/>
</DirectionalLayout>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

View File

@ -0,0 +1 @@
include ':entry',":abilityformprovider"