From 92a9ad15043946d4b4f5d725828ac9f6300919a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=8B=A4=E6=B0=91?= Date: Tue, 31 Oct 2023 15:30:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 + entry/agconnect-services.json | 63 ++++++++++ entry/build.gradle | 6 + entry/src/main/config.json | 19 ++- .../bjca/trust/hm/demo/slice/BaseAbilitySlice.java | 130 +++++++++++++++++++++ .../trust/hm/demo/slice/LocationAbilitySlice.java | 29 ++++- entry/src/main/resources/base/element/string.json | 2 +- entry/src/main/resources/zh_CN/element/string.json | 2 +- 8 files changed, 247 insertions(+), 6 deletions(-) create mode 100644 entry/agconnect-services.json create mode 100644 entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/BaseAbilitySlice.java diff --git a/build.gradle b/build.gradle index f33a3ac..3a82e8a 100644 --- a/build.gradle +++ b/build.gradle @@ -38,6 +38,8 @@ buildscript { dependencies { classpath 'com.huawei.ohos:hap:3.1.5.0' classpath 'com.huawei.ohos:decctest:1.2.7.20' + // 添加agconnect服务依赖 + classpath 'com.huawei.agconnect:agcp-harmony:1.1.0.300' } } diff --git a/entry/agconnect-services.json b/entry/agconnect-services.json new file mode 100644 index 0000000..6f305fb --- /dev/null +++ b/entry/agconnect-services.json @@ -0,0 +1,63 @@ +{ + "agcgw_all":{ + "CN":"connect-drcn.dbankcloud.cn", + "CN_back":"connect-drcn.hispace.hicloud.com", + "DE":"connect-dre.dbankcloud.cn", + "DE_back":"connect-dre.hispace.hicloud.com", + "RU":"connect-drru.hispace.dbankcloud.ru", + "RU_back":"connect-drru.hispace.dbankcloud.cn", + "SG":"connect-dra.dbankcloud.cn", + "SG_back":"connect-dra.hispace.hicloud.com" + }, + "websocketgw_all":{ + "CN":"connect-ws-drcn.hispace.dbankcloud.cn", + "CN_back":"connect-ws-drcn.hispace.dbankcloud.com", + "DE":"connect-ws-dre.hispace.dbankcloud.cn", + "DE_back":"connect-ws-dre.hispace.dbankcloud.com", + "RU":"connect-ws-drru.hispace.dbankcloud.ru", + "RU_back":"connect-ws-drru.hispace.dbankcloud.cn", + "SG":"connect-ws-dra.hispace.dbankcloud.cn", + "SG_back":"connect-ws-dra.hispace.dbankcloud.com" + }, + "client":{ + "cp_id":"890086000300080329", + "product_id":"388421841221673862", + "client_id":"1242623101255039872", + "client_secret":"[!00186014B9DBBF78CBC66DB96BD9F6BA5FA0B06186E52664C9A785CA69B46B06FE3CFC1667C606AD52FEF6CC271A74E53052C00F9114ECED2C13D720FE1CDA51F6D88CEB94FD60CA4468D87B2FAA77BC0397DF22C6F59551715269AD38F5D07944]", + "project_id":"388421841221673862", + "app_id":"109162775", + "api_key":"[!00B9BAB46A26DBC81E7F5019D2792734E17C01917173CA393708A5F4847581A00D714DAA3C5CB083AF82FC94AAC2AACB301C26CE25A48EC61CF5864F4084C12E64FBDD9E0570AF8D72FF7144286EC86A74F2AED91A8D347AE300F9D5F07AE2512995E7DC37CACD258D110EBAC427483C807956532DE1617DD8E86A93604B22662B]", + "package_name":"cn.org.bjca.trust.hm.demo" + }, + "oauth_client":{ + "client_id":"109162775", + "client_type":30 + }, + "app_info":{ + "app_id":"1242623102521715392", + "package_name":"cn.org.bjca.trust.hm.demo" + }, + "code":{ + "code1":"E7D97860466CBF105E8A6924883D1522", + "code2":"BE58A167007D8A806B45A8E3989E85EE", + "code3":"4CF76090D01CB9DAEDE75F4710F97FEB", + "code4":"B95FEFC45DCF12673AD0C9AA10C9B2F9" + }, + "configuration_version":"3.0", + "appInfos":[ + { + "package_name":"cn.org.bjca.trust.hm.demo", + "client":{ + "client_secret":"[!00BC72E140C72800F9D52CC9A7EB0D410C4999419CEBF55037973BCAA1AE839DEE482BE8CBCE52B3AD8A7D295C1537D4B9428C840552DC0BDBCDD4F1B5C1742247A38AB6B0306CBB3FA3427C9F5B2A7ED093A5B18E5620AB4E437929DE073055F2]", + "app_id":"1242623102521715392", + "api_key":"[!005B83CBEB6E8E268106A66029D8240AB42B701EC48248ED7D9521952A0FB30E659B38A9CFD06AE6EADD945B037AD999471EF0BCC97E45598946CA59DA07ADC2238808F13D33D5BB516D136407EDF6B2B3A413A2DDBEB9D39387FBD7D026449513BC199E2D5CAE44AC114FC71ED074D33A100398C70D60173FE1956366142EFF7F]" + }, + "code":{ + "code1":"808E0534281B5D009DFF7294C3E37D330DCF4AFE48A1A134D9BA9C5DCD2390A4", + "code2":"6C6FBD320F67B4771037F90D79E40FF25E5B2D6C565B2C7018153683C5BCA5DB", + "code3":"DDFB39686B9A60D54DB37BA5CACA9706B6DC1E993C94E8BCA78B9649D3E6A350", + "code4":"74789036A77C4C36C260ECA32C7F9CA8" + } + } + ] +} \ No newline at end of file diff --git a/entry/build.gradle b/entry/build.gradle index 22f8515..c002e92 100644 --- a/entry/build.gradle +++ b/entry/build.gradle @@ -1,5 +1,7 @@ apply plugin: 'com.huawei.ohos.hap' apply plugin: 'com.huawei.ohos.decctest' +apply plugin: 'com.huawei.agconnect' + //For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/ide_debug_device-0000001053822404-V3#section1112183053510 ohos { compileSdkVersion 6 @@ -21,6 +23,10 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) testImplementation 'junit:junit:4.13.1' ohosTestImplementation 'com.huawei.ohos.testkit:runner:2.0.0.400' + // Location SDK 版本号 + implementation 'com.huawei.hms:location-harmony:6.7.0.300' + // agconnect依赖组件 + implementation 'com.huawei.agconnect:agconnect-core-harmony:1.1.0.300' } decc { supportType = ['html', 'xml'] diff --git a/entry/src/main/config.json b/entry/src/main/config.json index c11842b..850342d 100644 --- a/entry/src/main/config.json +++ b/entry/src/main/config.json @@ -28,11 +28,12 @@ ] } ], + "orientation": "portrait", "visible": true, "name": "cn.org.bjca.trust.hm.demo.MainAbility", "icon": "$media:icon", "description": "$string:mainability_description", - "formsEnabled": true, + "formsEnabled": false, "label": "$string:entry_MainAbility", "type": "page", "forms": [ @@ -55,12 +56,12 @@ "launchType": "standard" }, { + "orientation": "portrait", "name": "cn.org.bjca.trust.hm.demo.LocationAbility", "description": "$string:locationability_description", "icon": "$media:icon", "label": "$string:entry_LocationAbility", "launchType": "standard", - "orientation": "unspecified", "type": "page" } ], @@ -94,6 +95,20 @@ }, "type": "form" } + ], + "reqPermissions": [ + { + "name": "ohos.permission.GET_NETWORK_INFO", + "reason": "Allows programs to obtain network information status" + }, + { + "name": "ohos.permission.LOCATION", + "reason": "Allows applications to obtain location information when running in the foreground." + }, + { + "name": "ohos.permission.LOCATION_IN_BACKGROUND", + "reason": "Allows applications to obtain location information when running in the background" + } ] } } \ No newline at end of file diff --git a/entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/BaseAbilitySlice.java b/entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/BaseAbilitySlice.java new file mode 100644 index 0000000..322647e --- /dev/null +++ b/entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/BaseAbilitySlice.java @@ -0,0 +1,130 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved. + * + * 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 cn.org.bjca.trust.hm.demo.slice; + +import com.huawei.hms.location.harmony.FusedLocationClient; +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.agp.components.Text; +import ohos.bundle.IBundleManager; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +public class BaseAbilitySlice extends AbilitySlice { + private static final String TAG = "BaseAbilitySlice"; + + private static final int DOMAIN = 0xD001100; + + private static final int REQUEST_CODE = 0x1001; + + private static Map logLabelMap = new HashMap<>(); + + public FusedLocationClient fusedLocationClient; + + @Override + protected void onStart(Intent intent) { + super.onStart(intent); + fusedLocationClient = new FusedLocationClient(this); + } + + @Override + protected void onActive() { + super.onActive(); + } + + public void checkSelfPermission() { + if (verifySelfPermission("ohos.permission.LOCATION") != IBundleManager.PERMISSION_GRANTED) { + printLog(HiLog.INFO, TAG, "Self: LOCATION permission not granted!"); + if (canRequestPermission("ohos.permission.LOCATION")) { + printLog(HiLog.INFO, TAG, "Self: can request permission here"); + requestPermissionsFromUser( + new String[]{"ohos.permission.LOCATION"}, REQUEST_CODE); + } else { + printLog(HiLog.WARN, TAG, "Self: enter settings to set permission"); + } + } else { + printLog(HiLog.INFO, TAG, "Self: LOCATION permission granted!"); + } + + if (verifySelfPermission("ohos.permission.LOCATION_IN_BACKGROUND") != IBundleManager.PERMISSION_GRANTED) { + printLog(HiLog.INFO, TAG, "Self: LOCATION_IN_BACKGROUND permission not granted!"); + if (canRequestPermission("ohos.permission.LOCATION_IN_BACKGROUND")) { + printLog(HiLog.INFO, TAG, "Self: can request permission here"); + requestPermissionsFromUser( + new String[]{"ohos.permission.LOCATION_IN_BACKGROUND"}, REQUEST_CODE); + } else { + printLog(HiLog.WARN, TAG, "Self: enter settings to set permission"); + } + } else { + printLog(HiLog.INFO, TAG, "Self: LOCATION_IN_BACKGROUND permission granted!"); + } + } + + public void printLog(int level, String tag, String message) { + HiLogLabel hiLogLabel = getLogLabel(tag); + switch (level) { + case HiLog.INFO: + HiLog.info(hiLogLabel, message); + break; + case HiLog.WARN: + HiLog.warn(hiLogLabel, message); + break; + case HiLog.ERROR: + HiLog.error(hiLogLabel, message); + break; + default: + HiLog.debug(hiLogLabel, message); + break; + } + } + + private HiLogLabel getLogLabel(String tag) { + HiLogLabel label; + String tagStr; + + if (tag == null || tag.isEmpty()) { + tagStr = "TAG"; + } else { + tagStr = tag; + } + if (logLabelMap.containsKey(tagStr)) { + label = logLabelMap.getOrDefault(tagStr, new HiLogLabel(HiLog.LOG_APP, DOMAIN, "TAG")); + } else { + label = new HiLogLabel(HiLog.LOG_APP, DOMAIN, tagStr); + logLabelMap.put(tagStr, label); + } + return label; + } + + public void printScreenLog(Text showLocation, String msg) { + StringBuilder outputBuilder = new StringBuilder(); + SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); + Date curDate = new Date(System.currentTimeMillis()); + String dateStr = formatter.format(curDate); + outputBuilder.append(dateStr); + outputBuilder.append(" "); + outputBuilder.append(msg); + outputBuilder.append(System.lineSeparator()); + showLocation.setMultipleLine(true); + showLocation.append(outputBuilder.toString()); + } +} diff --git a/entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/LocationAbilitySlice.java b/entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/LocationAbilitySlice.java index 8f0d37f..3f35288 100644 --- a/entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/LocationAbilitySlice.java +++ b/entry/src/main/java/cn/org/bjca/trust/hm/demo/slice/LocationAbilitySlice.java @@ -1,14 +1,39 @@ package cn.org.bjca.trust.hm.demo.slice; import cn.org.bjca.trust.hm.demo.ResourceTable; -import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; +import ohos.agp.components.Text; +import ohos.hiviewdfx.HiLog; + +public class LocationAbilitySlice extends BaseAbilitySlice { + private static final String TAG = "LocationAbilitySlice"; + + private Text showLocation; -public class LocationAbilitySlice extends AbilitySlice { @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main); + checkSelfPermission(); + showLocation = (Text) findComponentById(ResourceTable.Id_text_helloworld); + showLocation.setClickedListener(component -> { + getLastLocation(); + }); + } + + private void getLastLocation() { + fusedLocationClient.getLastLocation().addOnSuccessListener(location -> { + if (null == location) { + printLog(HiLog.INFO, TAG, "[old]getLastLocation onSuccess: location is null"); + return; + } + String result = "[old]getLastLocation onSuccess location[Longitude,Latitude]:" + location.getLongitude() + "," + location.getLatitude(); + printLog(HiLog.INFO, TAG, result); + printScreenLog(showLocation, result); + }).addOnFailureListener(e -> { + printLog(HiLog.INFO, TAG, "getLastLocation onFailure:" + e.getMessage()); + printScreenLog(showLocation, "getLastLocation onFailure:" + e.getMessage()); + }); } @Override diff --git a/entry/src/main/resources/base/element/string.json b/entry/src/main/resources/base/element/string.json index 010662d..6418de8 100644 --- a/entry/src/main/resources/base/element/string.json +++ b/entry/src/main/resources/base/element/string.json @@ -2,7 +2,7 @@ "string": [ { "name": "entry_MainAbility", - "value": "entry_MainAbility" + "value": "元服务体验" }, { "name": "mainability_description", diff --git a/entry/src/main/resources/zh_CN/element/string.json b/entry/src/main/resources/zh_CN/element/string.json index 3799ece..cbcaf62 100644 --- a/entry/src/main/resources/zh_CN/element/string.json +++ b/entry/src/main/resources/zh_CN/element/string.json @@ -2,7 +2,7 @@ "string": [ { "name": "entry_MainAbility", - "value": "entry_MainAbility" + "value": "元服务体验" }, { "name": "mainability_description",