|
|
@@ -1,44 +1,53 @@
|
|
|
package com.fujica.abk.slice;
|
|
|
|
|
|
import com.fujica.abk.ResourceTable;
|
|
|
-import ohos.aafwk.ability.AbilitySlice;
|
|
|
-import ohos.aafwk.content.Intent;
|
|
|
-import com.huawei.hms.maps.harmony.HuaweiMap;
|
|
|
-import com.huawei.hms.maps.harmony.MapView;
|
|
|
-import com.huawei.hms.maps.harmony.HuaweiMapOptions;
|
|
|
-import com.huawei.hms.maps.harmony.OnMapClickListener;
|
|
|
-import com.huawei.hms.maps.harmony.OnInfoWindowClickListener;
|
|
|
-import com.huawei.hms.maps.harmony.OnMarkerClickListener;
|
|
|
-import com.huawei.hms.maps.harmony.CameraUpdate;
|
|
|
-import com.huawei.hms.maps.harmony.CameraUpdateFactory;
|
|
|
-import com.huawei.hms.maps.harmony.CommonContext;
|
|
|
+import com.fujica.abk.api.cache;
|
|
|
+import com.fujica.abk.utils.Log;
|
|
|
+import com.fujica.abk.utils.Toast;
|
|
|
+import com.fujica.abk.utils.api;
|
|
|
+import com.huawei.hms.accountsdk.constant.CommonConstant;
|
|
|
+import com.huawei.hms.accountsdk.exception.ApiException;
|
|
|
+import com.huawei.hms.accountsdk.support.account.AccountAuthManager;
|
|
|
+import com.huawei.hms.accountsdk.support.account.request.AccountAuthParams;
|
|
|
+import com.huawei.hms.accountsdk.support.account.request.AccountAuthParamsHelper;
|
|
|
+import com.huawei.hms.accountsdk.support.account.result.AuthAccount;
|
|
|
+import com.huawei.hms.accountsdk.support.account.service.AccountAuthService;
|
|
|
+import com.huawei.hms.accountsdk.support.account.tasks.OnFailureListener;
|
|
|
+import com.huawei.hms.accountsdk.support.account.tasks.OnSuccessListener;
|
|
|
+import com.huawei.hms.accountsdk.support.account.tasks.Task;
|
|
|
+import com.huawei.hms.maps.harmony.*;
|
|
|
import com.huawei.hms.maps.harmony.model.CameraPosition;
|
|
|
import com.huawei.hms.maps.harmony.model.LatLng;
|
|
|
import com.huawei.hms.maps.harmony.model.Marker;
|
|
|
import com.huawei.hms.maps.harmony.model.MarkerOptions;
|
|
|
-import ohos.agp.components.*;
|
|
|
+import ohos.aafwk.ability.AbilitySlice;
|
|
|
+import ohos.aafwk.ability.DataAbilityHelper;
|
|
|
+import ohos.aafwk.ability.IDataAbilityObserver;
|
|
|
+import ohos.aafwk.content.Intent;
|
|
|
+import ohos.account.AccountAbility;
|
|
|
import ohos.agp.colors.RgbColor;
|
|
|
+import ohos.agp.colors.StateColor;
|
|
|
+import ohos.agp.components.*;
|
|
|
import ohos.agp.components.element.ShapeElement;
|
|
|
import ohos.agp.utils.Color;
|
|
|
import ohos.agp.window.dialog.ToastDialog;
|
|
|
import ohos.agp.window.service.Window;
|
|
|
import ohos.agp.window.service.WindowManager;
|
|
|
-import ohos.hiviewdfx.HiLog;
|
|
|
-import ohos.hiviewdfx.HiLogLabel;
|
|
|
+import ohos.global.resource.NotExistException;
|
|
|
+import ohos.global.resource.Resource;
|
|
|
import ohos.location.Location;
|
|
|
import ohos.location.Locator;
|
|
|
import ohos.location.LocatorCallback;
|
|
|
import ohos.location.RequestParam;
|
|
|
-import ohos.global.resource.NotExistException;
|
|
|
-import ohos.global.resource.Resource;
|
|
|
+import ohos.sysappcomponents.settings.AppSettings;
|
|
|
+import ohos.sysappcomponents.settings.SystemSettings;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
public class MainAbilitySlice extends AbilitySlice {
|
|
|
- private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, MainAbilitySlice.class.getName());
|
|
|
-
|
|
|
// PageSlider相关
|
|
|
private PageSlider pageSlider;
|
|
|
private PageSliderProvider pageSliderProvider;
|
|
|
@@ -82,11 +91,21 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
* 当前位置标记
|
|
|
*/
|
|
|
private Marker currentLocationMarker;
|
|
|
+ DataAbilityHelper dataAbilityHelper;
|
|
|
+ IDataAbilityObserver dataAbilityObserver;
|
|
|
|
|
|
@Override
|
|
|
public void onStart(Intent intent) {
|
|
|
super.onStart(intent);
|
|
|
|
|
|
+ cache.setToken(getContext(), "sdfdsfdsf11111111111");
|
|
|
+ String token = cache.getToken(getContext());
|
|
|
+
|
|
|
+// api.http(getContext(), "http://www.baidu.com", "GET", null, null, null);
|
|
|
+
|
|
|
+ api.http(getContext(), "/park/near/page?longitude=0&latitude=0", "GET", null, null, null);
|
|
|
+
|
|
|
+
|
|
|
// 设置全屏显示
|
|
|
setFullScreen();
|
|
|
|
|
|
@@ -107,8 +126,112 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
|
|
|
// 初始化定位
|
|
|
initLocation();
|
|
|
+
|
|
|
+ Button mBtnHuaweiIdSignIn = findComponentById(ResourceTable.Id_btn_hwid_sign_in);
|
|
|
+ mBtnHuaweiIdSignIn.setClickedListener((Component c) -> {
|
|
|
+ huaweiIdSignIn();
|
|
|
+ });
|
|
|
+
|
|
|
+ final String key = "car_license_plate_number";
|
|
|
+// final String key = SystemSettings.Date.TIME_FORMAT;
|
|
|
+
|
|
|
+ dataAbilityHelper = DataAbilityHelper.creator(this);
|
|
|
+ dataAbilityObserver = () -> {
|
|
|
+ String timeFormat = SystemSettings.getValue(dataAbilityHelper, key);
|
|
|
+ setTimeFormat(timeFormat);
|
|
|
+ };
|
|
|
+ dataAbilityHelper.registerObserver(SystemSettings.getUri(key), dataAbilityObserver);
|
|
|
+ String timeFormat1 = SystemSettings.getValue(dataAbilityHelper, "car_license_plate_number");
|
|
|
+ String timeFormat = SystemSettings.getValue(dataAbilityHelper, key);
|
|
|
}
|
|
|
|
|
|
+ void setTimeFormat(String timeFormat) {
|
|
|
+ String timeStr = "12";
|
|
|
+ if (timeStr.equals(timeFormat)) {
|
|
|
+ // Display in 12-hour format
|
|
|
+ } else {
|
|
|
+ // Display in 24-hour format
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void huaweiIdSignIn() {
|
|
|
+ AccountAuthService accountAuthService;
|
|
|
+ // 1、配置登录请求参数AccountAuthParams,包括请求用户id(openid、unionid)、email、profile(昵称、头像)等。
|
|
|
+ // 2、DEFAULT_AUTH_REQUEST_PARAM默认包含了id和profile(昵称、头像)的请求。
|
|
|
+ // 3、如需要再获取用户邮箱,需要setEmail();
|
|
|
+ // 4、如需要获取其他受限信息,如国家和地区,则需要先申请scope,再设置请求参数。
|
|
|
+ AccountAuthParams accountAuthParams = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
|
|
|
+ .setEmail()
|
|
|
+ .createParams();
|
|
|
+ try {
|
|
|
+ accountAuthService = AccountAuthManager.getService(accountAuthParams);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ // 处理初始化登录授权服务失败,status code标识了失败的原因,请参考API中的错误码参考了解详细错误原因
|
|
|
+ e.getStatusCode();
|
|
|
+ Toast.error(getContext(),"Init Huawei accountAuthService FAILED.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Toast.info(getContext(),"Init Huawei accountAuthService SUCCESS");
|
|
|
+
|
|
|
+ if (accountAuthService == null) {
|
|
|
+ Toast.error(getContext(),"获取不到账号信息");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 调用静默登录接口。
|
|
|
+ // 如果华为系统帐号已经登录,并且已经授权,会登录成功;
|
|
|
+ // 否则静默登录失败,需要在失败监听中,显式地调用前台登录授权接口,完成登录授权。
|
|
|
+ Task<AuthAccount> taskSilentSignIn = accountAuthService.silentSignIn();
|
|
|
+// Task<AuthAccount> taskSilentSignIn = accountAuthService.silentSignIn();
|
|
|
+ Toast.info(getContext(),"SilentSign START.");
|
|
|
+ // 添加静默登录成功处理监听
|
|
|
+ taskSilentSignIn.addOnSuccessListener(authAccount -> updateUI(authAccount));
|
|
|
+ // 添加静默登录失败监听
|
|
|
+ taskSilentSignIn.addOnFailureListener(e -> {
|
|
|
+ if (e instanceof ApiException) {
|
|
|
+ ApiException apiException = (ApiException) e;
|
|
|
+ Toast.error(getContext(),"SilentSignIn FAILED, status code: " + apiException.getStatusCode() + ". Need to foreground sign in" + "\r\n" + apiException.getStatusMessage()
|
|
|
+ + "\r\n" + apiException.getMessage() + "\r\n" + apiException.getCause());
|
|
|
+
|
|
|
+ // 静默登录失败,显式地调用前台登录授权接口,完成登录授权。
|
|
|
+ Task<AuthAccount> taskSignIn = accountAuthService.signIn();
|
|
|
+ Toast.info(getContext(),"SignIn foreground START.");
|
|
|
+ if (taskSignIn == null) {
|
|
|
+ Toast.info(getContext(),"SignIn foreground task is null.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ taskSignIn.addOnSuccessListener(new OnSuccessListener<AuthAccount>() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(AuthAccount result) {
|
|
|
+ Toast.info(getContext(),"SignIn foreground SUCCESS.");
|
|
|
+ updateUI(result);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ taskSignIn.addOnFailureListener(new OnFailureListener() {
|
|
|
+ @Override
|
|
|
+ public void onFailure(Exception e) {
|
|
|
+ Toast.info(getContext(),"SignIn foreground FAILED.");
|
|
|
+ if (e instanceof ApiException) {
|
|
|
+ ApiException apiException = (ApiException) e;
|
|
|
+ // 登录失败,status code标识了失败的原因,请参考API中的错误码参考了解详细错误原因
|
|
|
+ apiException.getStatusCode();
|
|
|
+ Toast.info(getContext(),"SignIn foreground FAILED. status code: "
|
|
|
+ + apiException.getStatusCode()
|
|
|
+ + ".");
|
|
|
+ if (CommonConstant.RETCODE.SIGN_IN_CANCELLED == apiException.getStatusCode()) {
|
|
|
+ Toast.info(getContext(),"Error message: User click CANCEL or Return, user cancel login in.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateUI(AuthAccount authAccount) {
|
|
|
+ Toast.info(getContext(),authAccount.getOpenId() + " : " + authAccount.getUnionId());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 设置全屏显示
|
|
|
*/
|
|
|
@@ -124,7 +247,7 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
window.setLayoutConfig(layoutConfig);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- HiLog.error(TAG, "设置全屏失败: " + e.getMessage());
|
|
|
+ Log.error("设置全屏失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -167,7 +290,7 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
pages.add(page);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- HiLog.error(TAG, "创建页面失败: " + e.getMessage());
|
|
|
+ Log.error("创建页面失败: " + e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return page;
|
|
|
@@ -198,7 +321,7 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
// 页面切换完成
|
|
|
currentTab = index;
|
|
|
updateBottomNavState(index);
|
|
|
- HiLog.info(TAG, "切换到页面: " + index);
|
|
|
+ Log.info("切换到页面: " + index);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -248,7 +371,7 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
}
|
|
|
if (navCurrentLocation != null) {
|
|
|
navCurrentLocation.setClickedListener(component -> {
|
|
|
- HiLog.info(TAG, "点击了当前位置按钮");
|
|
|
+ Log.info("点击了当前位置按钮");
|
|
|
locateCurrentPosition();
|
|
|
});
|
|
|
}
|
|
|
@@ -359,21 +482,24 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
// Set Preference Maximum Zoom Level
|
|
|
.maxZoomPreference(13);
|
|
|
|
|
|
+ if (true) return;
|
|
|
+
|
|
|
// Initialize MapView Object. - 完全按照 MarkerDemo
|
|
|
mMapView = new MapView(this, huaweiMapOptions);
|
|
|
mMapView.onCreate();
|
|
|
|
|
|
+
|
|
|
// Obtains the HuaweiMap object. - 完全按照 MarkerDemo
|
|
|
mMapView.getMapAsync(huaweiMap -> {
|
|
|
mHuaweiMap = huaweiMap;
|
|
|
|
|
|
// If mHuaweiMap is null, the program stops running. - 完全按照 MarkerDemo
|
|
|
if (null == mHuaweiMap) {
|
|
|
- HiLog.error(TAG, "mHuaweiMap 为 null");
|
|
|
+ Log.error("mHuaweiMap 为 null");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- HiLog.info(TAG, "地图已就绪,开始获取定位");
|
|
|
+ Log.info("地图已就绪,开始获取定位");
|
|
|
|
|
|
mHuaweiMap.setOnMapClickListener(new OnMapClickListener() {
|
|
|
@Override
|
|
|
@@ -447,7 +573,7 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
// 加载MapView
|
|
|
myLayout.addComponent(mMapView);
|
|
|
} catch (Exception e) {
|
|
|
- HiLog.error(TAG, "初始化地图失败: " + e.getMessage());
|
|
|
+ Log.error("初始化地图失败: " + e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
@@ -493,7 +619,7 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
double latitude = location.getLatitude();
|
|
|
double longitude = location.getLongitude();
|
|
|
|
|
|
- HiLog.info(TAG, "定位成功: " + latitude + ", " + longitude);
|
|
|
+ Log.info("定位成功: " + latitude + ", " + longitude);
|
|
|
|
|
|
// 将坐标传递给地图
|
|
|
updateMapWithLocation(location);
|
|
|
@@ -501,19 +627,19 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
|
|
|
@Override
|
|
|
public void onStatusChanged(int type) {
|
|
|
- HiLog.info(TAG, "定位状态变化: " + type);
|
|
|
+ Log.info("定位状态变化: " + type);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onErrorReport(int errorCode) {
|
|
|
- HiLog.error(TAG, "定位错误: " + errorCode);
|
|
|
+ Log.error("定位错误: " + errorCode);
|
|
|
new ToastDialog(CommonContext.getContext())
|
|
|
.setText("定位失败,错误码: " + errorCode)
|
|
|
.show();
|
|
|
}
|
|
|
};
|
|
|
} catch (Exception e) {
|
|
|
- HiLog.error(TAG, "初始化定位服务失败: " + e.getMessage());
|
|
|
+ Log.error("初始化定位服务失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -522,7 +648,7 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
*/
|
|
|
private void locateCurrentPosition() {
|
|
|
if (locator == null || locatorCallback == null) {
|
|
|
- HiLog.error(TAG, "定位服务未初始化");
|
|
|
+ Log.error("定位服务未初始化");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -530,9 +656,9 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
// 请求定位
|
|
|
RequestParam requestParam = new RequestParam(RequestParam.PRIORITY_ACCURACY, 0, 0);
|
|
|
locator.startLocating(requestParam, locatorCallback);
|
|
|
- HiLog.info(TAG, "开始定位...");
|
|
|
+ Log.info("开始定位...");
|
|
|
} catch (Exception e) {
|
|
|
- HiLog.error(TAG, "定位失败: " + e.getMessage());
|
|
|
+ Log.error("定位失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -541,7 +667,7 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
*/
|
|
|
private void updateMapWithLocation(Location location) {
|
|
|
if (mHuaweiMap == null) {
|
|
|
- HiLog.warn(TAG, "地图未初始化完成,等待地图就绪");
|
|
|
+ Log.warn("地图未初始化完成,等待地图就绪");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -549,7 +675,7 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
double latitude = location.getLatitude();
|
|
|
double longitude = location.getLongitude();
|
|
|
|
|
|
- HiLog.info(TAG, "更新地图位置: " + latitude + ", " + longitude);
|
|
|
+ Log.info("更新地图位置: " + latitude + ", " + longitude);
|
|
|
|
|
|
// 创建坐标点
|
|
|
LatLng latLng = new LatLng(latitude, longitude);
|
|
|
@@ -574,7 +700,7 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
// move camera - 完全按照示例代码
|
|
|
mHuaweiMap.moveCamera(cameraUpdate);
|
|
|
|
|
|
- HiLog.info(TAG, "地图已更新到当前位置");
|
|
|
+ Log.info("地图已更新到当前位置");
|
|
|
|
|
|
// 显示提示
|
|
|
new ToastDialog(CommonContext.getContext())
|
|
|
@@ -582,7 +708,7 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
.show();
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
- HiLog.error(TAG, "更新地图位置失败: " + e.getMessage());
|
|
|
+ Log.error("更新地图位置失败: " + e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
@@ -635,12 +761,17 @@ public class MainAbilitySlice extends AbilitySlice {
|
|
|
try {
|
|
|
locator.stopLocating(locatorCallback);
|
|
|
} catch (Exception e) {
|
|
|
- HiLog.error(TAG, "停止定位失败: " + e.getMessage());
|
|
|
+ Log.error("停止定位失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
// 销毁地图 - 完全按照示例代码
|
|
|
if (mMapView != null) {
|
|
|
mMapView.onDestroy();
|
|
|
}
|
|
|
+
|
|
|
+ if (dataAbilityHelper != null) {
|
|
|
+
|
|
|
+ }
|
|
|
+ dataAbilityHelper.unregisterObserver(SystemSettings.getUri(SystemSettings.Date.TIME_FORMAT), dataAbilityObserver);
|
|
|
}
|
|
|
}
|