diff --git a/UI/JsFACard/entry/build.gradle b/UI/JsFACard/entry/build.gradle index 83f59debbd68242b503a22270f4378d7843ca204..7321aa676e8d1768815335fe0813207cbda5f160 100644 --- a/UI/JsFACard/entry/build.gradle +++ b/UI/JsFACard/entry/build.gradle @@ -12,6 +12,7 @@ ohos { } } } + compileOptions { annotationEnabled true } } dependencies { diff --git a/UI/JsFACard/entry/src/main/config.json b/UI/JsFACard/entry/src/main/config.json index f6163e9afae65a1c22c1191cdad206fb8aa3a0de..0c7d85a3fdad8fae247593e0cee967f73d7846a0 100644 --- a/UI/JsFACard/entry/src/main/config.json +++ b/UI/JsFACard/entry/src/main/config.json @@ -6,11 +6,15 @@ "name": "1.0.0" } }, - "deviceConfig": {}, + "deviceConfig": { + "default": { + "keepAlive": true + } + }, "module": { "package": "ohos.samples.jsfacard", - "name": ".MainAbility", - "mainAbility": "ohos.samples.jsfacard.MainAbility", + "name": ".MyApplication", + "mainAbility": "ohos.samples.jsfacard.ClockAbility", "deviceType": [ "phone" ], @@ -20,6 +24,18 @@ "moduleType": "entry", "installationFree": false }, + "reqPermissions": [ + { + "name": "ohos.permission.KEEP_BACKGROUND_RUNNING", + "reason": "keep service ability backgroud running", + "usedScene": { + "ability": [ + "ohos.samples.jsfacard.TimerAbility" + ], + "when": "always" + } + } + ], "abilities": [ { "skills": [ @@ -32,36 +48,46 @@ ] } ], - "name": ".MainAbility", + "name": ".ClockAbility", "icon": "$media:icon", - "description": "$string:mainability_description", + "description": "$string:clockability_description", "formsEnabled": true, - "label": "$string:entry_MainAbility", + "label": "$string:app_name", "type": "page", + "launchType": "singleton", "forms": [ { - "jsComponentName": "jsmusictemplate", + "jsComponentName": "clock1x2", "isDefault": true, - "formConfigAbility": "ability://ohos.samples.jsfacard.MainAbility", "scheduledUpdateTime": "10:30", - "defaultDimension": "2*4", - "name": "jsmusictemplate", + "defaultDimension": "1*2", + "name": "clock1x2", "description": "This is a service widget", "colorMode": "auto", "type": "JS", "supportDimensions": [ - "2*4" + "1*2" ], "updateEnabled": true, "updateDuration": 1 - }, + } + ] + }, + { + "name": "ohos.samples.jsfacard.MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "$string:entry_MainAbility", + "type": "page", + "formsEnabled": true, + "launchType": "standard", + "forms": [ { - "jsComponentName": "card", + "jsComponentName": "jsmusictemplate", "isDefault": false, - "formConfigAbility": "ability://ohos.samples.jsfacard.MainAbility", "scheduledUpdateTime": "10:30", "defaultDimension": "2*4", - "name": "card", + "name": "jsmusictemplate", "description": "This is a service widget", "colorMode": "auto", "type": "JS", @@ -74,7 +100,6 @@ { "jsComponentName": "jscardtemplate", "isDefault": false, - "formConfigAbility": "ability://ohos.samples.jsfacard.MainAbility", "scheduledUpdateTime": "10:30", "defaultDimension": "4*4", "name": "jscardtemplate", @@ -86,9 +111,34 @@ ], "updateEnabled": true, "updateDuration": 1 + }, + { + "jsComponentName": "card", + "isDefault": true, + "scheduledUpdateTime": "10:30", + "defaultDimension": "2*4", + "name": "card", + "description": "This is a service widget", + "colorMode": "auto", + "type": "JS", + "supportDimensions": [ + "2*4" + ], + "updateEnabled": true, + "updateDuration": 1 } - ], - "launchType": "standard" + ] + }, + { + "name": "ohos.samples.jsfacard.TimerAbility", + "icon": "$media:icon", + "description": "$string:timerability_description", + "type": "service", + "visible": true, + "backgroundModes": [ + "dataTransfer", + "location" + ] } ], "js": [ @@ -134,6 +184,17 @@ "autoDesignWidth": true }, "type": "form" + }, + { + "pages": [ + "pages/index/index" + ], + "name": "clock1x2", + "window": { + "designWidth": 720, + "autoDesignWidth": true + }, + "type": "form" } ] } diff --git a/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/ClockAbility.java b/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/ClockAbility.java new file mode 100644 index 0000000000000000000000000000000000000000..105bc8e5bd96debb13f2663c4cee86568b7331e7 --- /dev/null +++ b/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/ClockAbility.java @@ -0,0 +1,115 @@ +/* + * 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.jsfacard; + +import ohos.aafwk.ability.Ability; +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.ability.FormBindingData; +import ohos.aafwk.ability.ProviderFormInfo; +import ohos.aafwk.content.Intent; +import ohos.aafwk.content.Operation; +import ohos.data.DatabaseHelper; +import ohos.data.orm.OrmContext; +import ohos.samples.jsfacard.database.Form; +import ohos.samples.jsfacard.database.FormDatabase; +import ohos.samples.jsfacard.slice.ClockAbilitySlice; +import ohos.samples.jsfacard.utils.DatabaseUtils; +import ohos.samples.jsfacard.utils.DateUtils; +import ohos.samples.jsfacard.utils.LogUtils; +import ohos.utils.zson.ZSONObject; + +/** + * ClockAbility + * + * @since 2021-08-20 + */ +public class ClockAbility extends Ability { + private static final String TAG = ClockAbility.class.getName(); + + private static final int INVALID_FORM_ID = -1; + + private static final String EMPTY_STRING = ""; + + private static final int DEFAULT_DIMENSION_1x2 = 1; + + private OrmContext connect; + + private DatabaseHelper helper = new DatabaseHelper(this); + + @Override + public void onStart(Intent intent) { + super.onStart(intent); + startTimerAbility(); + connect = helper.getOrmContext("FormDatabase", "FormDatabase.db", FormDatabase.class); + super.setMainRoute(ClockAbilitySlice.class.getName()); + } + + private void startTimerAbility(){ + Intent intentService = new Intent(); + Operation operation = new Intent.OperationBuilder() + .withDeviceId("") + .withBundleName(getBundleName()) + .withAbilityName(TimerAbility.class.getName()) + .build(); + intentService.setOperation(operation); + startAbility(intentService); + } + + @Override + protected ProviderFormInfo onCreateForm(Intent intent) { + startTimerAbility(); + ProviderFormInfo providerFormInfo = new ProviderFormInfo(); + LogUtils.info(TAG, "onCreateForm()"); + if (intent == null) { + return providerFormInfo; + } + + long formId = INVALID_FORM_ID; + if (intent.hasParameter(AbilitySlice.PARAM_FORM_IDENTITY_KEY)) { + formId = intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, INVALID_FORM_ID); + } else { + return providerFormInfo; + } + + String formName = EMPTY_STRING; + if (intent.hasParameter(AbilitySlice.PARAM_FORM_NAME_KEY)) { + formName = intent.getStringParam(AbilitySlice.PARAM_FORM_NAME_KEY); + } + + int dimension = DEFAULT_DIMENSION_1x2; + if (intent.hasParameter(AbilitySlice.PARAM_FORM_DIMENSION_KEY)) { + dimension = intent.getIntParam(AbilitySlice.PARAM_FORM_DIMENSION_KEY, DEFAULT_DIMENSION_1x2); + } + + if (connect == null) { + connect = helper.getOrmContext("FormDatabase", "FormDatabase.db", FormDatabase.class); + } + Form form = new Form(formId, formName, dimension); + DatabaseUtils.insertForm(form, connect); + + ZSONObject zsonObject = DateUtils.getZsonObject(); + LogUtils.info(TAG, "onCreateForm()" + zsonObject); + providerFormInfo.setJsBindingData(new FormBindingData(zsonObject)); + return providerFormInfo; + } + + @Override + protected void onDeleteForm(long formId) { + LogUtils.info(TAG, "onDeleteForm():formId=" + formId); + super.onDeleteForm(formId); + DatabaseUtils.deleteFormData(formId, connect); + } +} diff --git a/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/MainAbility.java b/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/MainAbility.java index a8e10f50492567523db1dc80a1b6c31a4061ad22..a958415834ebe1713182a90cdcf6a6a52e16165d 100644 --- a/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/MainAbility.java +++ b/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/MainAbility.java @@ -20,17 +20,16 @@ import ohos.aafwk.ability.FormException; import ohos.aafwk.ability.ProviderFormInfo; import ohos.aafwk.content.Intent; import ohos.ace.ability.AceAbility; -import ohos.hiviewdfx.HiLog; -import ohos.hiviewdfx.HiLogLabel; +import ohos.samples.jsfacard.utils.LogUtils; import ohos.utils.zson.ZSONObject; /** * Main ability * - * @since 2021-07-05 + * @since 2021-08-20 */ public class MainAbility extends AceAbility { - private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, MainAbility.class.getName()); + private static final String TAG = MainAbility.class.getName(); private static final String STATUS = "status"; @@ -42,30 +41,31 @@ public class MainAbility extends AceAbility { @Override public void onStart(Intent intent) { - HiLog.info(TAG, "onStart"); + LogUtils.info(TAG, "onStart"); super.onStart(intent); } @Override protected ProviderFormInfo onCreateForm(Intent intent) { - HiLog.info(TAG, "onCreateForm"); + LogUtils.info(TAG, "onCreateForm"); return super.onCreateForm(intent); } @Override protected void onUpdateForm(long formId) { - HiLog.info(TAG, "onUpdateForm"); + LogUtils.info(TAG, "onUpdateForm"); super.onUpdateForm(formId); } @Override protected void onDeleteForm(long formId) { - HiLog.info(TAG, "onDeleteForm: formId=" + formId); + LogUtils.info(TAG, "onDeleteForm: formId=" + formId); super.onDeleteForm(formId); } @Override protected void onTriggerFormEvent(long formId, String message) { + LogUtils.info(TAG, "onTriggerFormEvent: formId=" + formId); super.onTriggerFormEvent(formId, message); ZSONObject zsonObject = new ZSONObject(); if (isStatus) { @@ -79,7 +79,7 @@ public class MainAbility extends AceAbility { try { updateForm(formId, formBindingData); } catch (FormException e) { - HiLog.info(TAG, "onTriggerFormEvent:" + e.getMessage()); + LogUtils.info(TAG, "onTriggerFormEvent:" + e.getMessage()); } } -} +} \ No newline at end of file diff --git a/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/MyApplication.java b/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/MyApplication.java index 1fe528672f947fce0311e030dda7099e7f8c3ff2..0f5104439f69ae48a093735a838e5c0a3dca725b 100644 --- a/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/MyApplication.java +++ b/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/MyApplication.java @@ -20,7 +20,7 @@ import ohos.aafwk.ability.AbilityPackage; /** * MyApplication * - * @since 2021-07-05 + * @since 2021-08-20 */ public class MyApplication extends AbilityPackage { @Override diff --git a/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/TimerAbility.java b/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/TimerAbility.java new file mode 100644 index 0000000000000000000000000000000000000000..d8c0d21fead07733a1b7346a0dcbf04949ec3405 --- /dev/null +++ b/UI/JsFACard/entry/src/main/java/ohos/samples/jsfacard/TimerAbility.java @@ -0,0 +1,112 @@ +/* + * 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.jsfacard; + +import ohos.aafwk.ability.Ability; +import ohos.aafwk.ability.FormBindingData; +import ohos.aafwk.ability.FormException; +import ohos.aafwk.content.Intent; +import ohos.data.DatabaseHelper; +import ohos.data.orm.OrmContext; +import ohos.data.orm.OrmPredicates; +import ohos.event.notification.NotificationRequest; +import ohos.samples.jsfacard.database.Form; +import ohos.samples.jsfacard.database.FormDatabase; +import ohos.samples.jsfacard.utils.DatabaseUtils; +import ohos.samples.jsfacard.utils.DateUtils; +import ohos.samples.jsfacard.utils.LogUtils; +import ohos.utils.zson.ZSONObject; + +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; + +/** + * TimerAbility + * + * @since 2021-08-20 + */ +public class TimerAbility extends Ability { + private static final String TAG = TimerAbility.class.getName(); + + private static final long UPDATE_PERIOD = 1000L; + + private static final int NOTICE_ID = 1005; + + private OrmContext connect; + + private DatabaseHelper helper = new DatabaseHelper(this); + + @Override + public void onStart(Intent intent) { + LogUtils.info(TAG, "TimerAbility::onStart"); + connect = helper.getOrmContext("FormDatabase", "FormDatabase.db", FormDatabase.class); + startTimer(); + super.onStart(intent); + } + + private void startTimer() { + Timer timer = new Timer(); + timer.schedule(new TimerTask() { + @Override + public void run() { + updateForms(); + notice(); + } + }, 0, UPDATE_PERIOD); + } + + private void updateForms() { + OrmPredicates ormPredicates = new OrmPredicates(Form.class); + List