namespace OHOS
{
+
+ //下载结构体状态
+ enum class EphemerisDownloadStatus {
+ NOT_DOWNLOADED,//未下载
+ DOWNLOADING,//下载中
+ DOWNLOADED,//下载成功
+ FAILED//下载失败
+ };
class EphemerisDownloadModel
{
public:
~EphemerisDownloadModel();
// 下载星历相关方法
void DownloadEphemeris();
- bool GetEphemerisDownloadStatus();
+ //获取下载状态
+ EphemerisDownloadStatus GetEphemerisDownloadStatus();
+ //设置下载状态
+ void SetEphemerisDownloadStatus(EphemerisDownloadStatus status);
+ //获取有效期
std::string GetEphemerisValidityPeriod();
- void SetEphemerisDownloadStatus(bool status);
+ //设置有效期
void SetEphemerisValidityPeriod(const std::string &period);
+ //获取下载进度
+ int GetEphemerisDownloadProgress();
private:
// 星历相关状态
- bool ephemerisDownloading{false};
- bool ephemerisDownloaded{false};
std::string ephemerisValidityPeriod;
+ EphemerisDownloadStatus ephemerisDownloadStatus;
};
} // OHOS
#include "Presenter.h"
#include "ephemeris_download/include/EphemerisDownloadModel.h"
#include "ephemeris_download/include/EphemerisDownloadView.h"
+#include "ModelListenerSample.h"
namespace OHOS
{
class EphemerisDownloadPresenter : public Presenter<EphemerisDownloadView, EphemerisDownloadModel>,
public UIView::OnClickListener,
public RootView::OnKeyActListener,
+ public ModelListenerSample,
public UIView::OnDragListener
{
public:
EphemerisDownloadPresenter(const EphemerisDownloadPresenter &) = delete;
EphemerisDownloadPresenter &operator=(const EphemerisDownloadPresenter &) = delete;
void OnResume() override;
+ void Notify() override;
bool OnClick(UIView &view, const ClickEvent &event) override;
bool OnDrag(UIView &view, const DragEvent &event) override;
bool OnKeyAct(UIView &view, const KeyEvent &event) override;
#include "ephemeris_download/include/EphemerisDownloadModel.h"
namespace OHOS {
+
+ //下载的Id
+ static constexpr const char * DOWNLOAD_ID = "download_id";
+ //返回的Id
+ static constexpr const char * BACK_ID = "back_id";
+
class EphemerisDownloadPresenter;
class EphemerisDownloadView : public View<EphemerisDownloadPresenter> {
public:
~EphemerisDownloadView() override;
EphemerisDownloadView(const EphemerisDownloadView &) = delete;
EphemerisDownloadView &operator=(const EphemerisDownloadView &) = delete;
+ static EphemerisDownloadView *GetInstance(void);
+
void OnStart() override;
bool EphemerisDownloadInit();
void OnClick(UIView &view);
-
+ //获取下载的label
+ UILabelExt *GetDownloadLabel();
+ //获取有效期的label
+ UILabelExt *GetValidityPeriodLabel();
+
private:
- UILabelExt labelTitle_;
- UILabelExt labelStatus_;
- UILabelExt labelValidity_;
- UILabelButtonExt btnDownload_;
- UILabelButtonExt btnBack_;
- UIImageView statusImage_;
- UILabelExt labelNote_;
+ UILabelExt *labelTitle_{nullptr};
+ UILabelExt *labelStatus_{nullptr};
+ UILabelExt *labelValidity_{nullptr};
+ UILabelButtonExt *btnDownload_{nullptr};
+ UILabelButtonExt *btnBack_{nullptr};
+ UILabelExt *labelNote_{nullptr};
};
} // OHOS
#include "wearable_log.h"
+#if defined(SUPPORT_GNSS)
+#include "gnss_http_server.h"
+#endif
+
namespace OHOS {
EphemerisDownloadModel::EphemerisDownloadModel() {}
EphemerisDownloadModel::~EphemerisDownloadModel() {}
-
-
void EphemerisDownloadModel::DownloadEphemeris() {
- if (ephemerisDownloading) {
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "星历正在下载中,请勿重复操作");
- return;
- }
-
- ephemerisDownloading = true;
- ephemerisDownloaded = false;
-
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "开始下载星历...");
-
- // 模拟下载过程(假接口)
- // 在实际实现中,这里应该调用真正的下载接口
-
- // 模拟下载完成
- ephemerisDownloading = false;
- ephemerisDownloaded = true;
-
- // 设置有效期(模拟7天有效期)
- time_t now = time(0);
- time_t validUntil = now + (7 * 24 * 60 * 60); // 7天后
- char buffer[32];
- strftime(buffer, sizeof(buffer), "%Y-%m-%d", localtime(&validUntil));
- ephemerisValidityPeriod = std::string(buffer);
-
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "星历下载完成,有效期至: %s", ephemerisValidityPeriod.c_str());
+ //调用下载接口
+ //修改状态为下载中
+ ephemerisDownloadStatus = EphemerisDownloadStatus::DOWNLOADING;
+#if defined(SUPPORT_GNSS)
+ do_gnss_http_server_send_data(GNSS_HTTP_SERVICE_CMD_DOWNLOAD_AGNSS, NULL, 0);
+#endif
}
+/*
+获取下载状态
+//返回值:过期返回8,有效返回剩余时间的分钟数,错误返回-1
+int gnss_check_agnss_status(void);
-bool EphemerisDownloadModel::GetEphemerisDownloadStatus() {
- return ephemerisDownloaded;
+//简化的下载进度状态位
+//0:正在建立HTTP连接
+//-1:下载失败
+//1-9:下载进度(10%,20%,...,90%)
+//10:下载完成
+int gnss_get_download_status(void);
+*/
+int EphemerisDownloadModel::GetEphemerisDownloadProgress() {
+#if defined(SUPPORT_GNSS)
+ int download_status = gnss_get_download_status();
+ if(download_status == -1)
+ {
+ ephemerisDownloadStatus = EphemerisDownloadStatus::FAILED;
+ }
+ else if(download_status == 0)
+ {
+ ephemerisDownloadStatus = EphemerisDownloadStatus::DOWNLOADING;
+ }
+ else if(download_status == 10)
+ {
+ ephemerisDownloadStatus = EphemerisDownloadStatus::DOWNLOADED;
+ }
+ return download_status;
+#endif
+ return -1;
}
-std::string EphemerisDownloadModel::GetEphemerisValidityPeriod() {
- return ephemerisValidityPeriod;
+EphemerisDownloadStatus EphemerisDownloadModel::GetEphemerisDownloadStatus() {
+ return ephemerisDownloadStatus;
}
-void EphemerisDownloadModel::SetEphemerisDownloadStatus(bool status) {
- ephemerisDownloaded = status;
+void EphemerisDownloadModel::SetEphemerisDownloadStatus(
+ EphemerisDownloadStatus status) {
+ ephemerisDownloadStatus = status;
}
-void EphemerisDownloadModel::SetEphemerisValidityPeriod(const std::string &period) {
- ephemerisValidityPeriod = period;
+//获取有效期
+std::string EphemerisDownloadModel::GetEphemerisValidityPeriod() {
+#if defined(SUPPORT_GNSS)
+ int validityPeriod = gnss_check_agnss_status();
+ if (validityPeriod > 0) {
+ ephemerisValidityPeriod =
+ "有效期: " + std::to_string(validityPeriod) + "分钟";
+ } else {
+ ephemerisValidityPeriod = "无星历数据";
+ }
+#endif
+ return ephemerisValidityPeriod;
}
} // namespace OHOS
#include "ephemeris_download/include/EphemerisDownloadModel.h"
#include "ephemeris_download/include/EphemerisDownloadView.h"
-#include "key/include/KeyInputListener.h"
#include "common/key_code.h"
#include "custom_utils.h"
+#include "key/include/KeyInputListener.h"
#include "AppViewIDs.h"
#include "NativeAbility.h"
#include "NativeRegisterManager.h"
namespace OHOS {
-REGIST_SLICE(VIEW_EPHEMERIS_DOWNLOAD, EphemerisDownloadView, EphemerisDownloadPresenter);
+REGIST_SLICE(VIEW_EPHEMERIS_DOWNLOAD, EphemerisDownloadView,
+ EphemerisDownloadPresenter);
EphemerisDownloadPresenter::EphemerisDownloadPresenter() {
- Attach(&EphemerisDownloadModel::GetInstance());
+ Attach(&EphemerisDownloadModel::GetInstance());
}
-EphemerisDownloadPresenter::~EphemerisDownloadPresenter() {
-
-}
+EphemerisDownloadPresenter::~EphemerisDownloadPresenter() {}
void EphemerisDownloadPresenter::OnResume() {
- RootView::GetInstance()->SetOnKeyActListener(this);
+ RootView::GetInstance()->SetOnKeyActListener(this);
}
-bool EphemerisDownloadPresenter::OnClick(UIView &view, const ClickEvent &event) {
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "EphemerisDownloadPresenter::OnClick");
- view_->OnClick(view);
- return true;
+bool EphemerisDownloadPresenter::OnClick(UIView &view,
+ const ClickEvent &event) {
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "EphemerisDownloadPresenter::OnClick");
+ UNUSED(event);
+ //点击下载按键
+ if (strcmp(view.GetViewId(), DOWNLOAD_ID) == 0) {
+ //进行下载
+ EphemerisDownloadModel::GetInstance().DownloadEphemeris();
+ }
+ //点击返回按键
+ else if (strcmp(view.GetViewId(), BACK_ID) == 0) {
+ NativeAbility::GetInstance().Back();
+ }
+ return true;
}
bool EphemerisDownloadPresenter::OnDrag(UIView &view, const DragEvent &event) {
- WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "EphemerisDownloadPresenter::OnDrag");
- UNUSED(view);
- if ((event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT) &&
- (event.GetDeltaX() > X_DRAG_OFFSET) &&
- (event.GetDeltaX() > (DOUBLES * MATH_ABS(event.GetDeltaY())))) {
- NativeAbility::GetInstance().Back();
- }
- return true;
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "EphemerisDownloadPresenter::OnDrag");
+ UNUSED(view);
+ if ((event.GetDragDirection() == DragEvent::DIRECTION_LEFT_TO_RIGHT) &&
+ (event.GetDeltaX() > X_DRAG_OFFSET) &&
+ (event.GetDeltaX() > (DOUBLES * MATH_ABS(event.GetDeltaY())))) {
+ NativeAbility::GetInstance().Back();
+ }
+ return true;
}
bool EphemerisDownloadPresenter::OnKeyAct(UIView &view, const KeyEvent &event) {
- unsigned short keyID = event.GetKeyId();
- unsigned short keyState = event.GetState();
- // 如果是power键,且是亮屏状态,按键释放就返回上一级
- if (keyID == static_cast<uint16_t>(ZliteKeyCode::ZLITE_KEY_POWER) &&
- keyState == 1 && uapi_get_screen_state() == 0) {
- // 回退
- NativeAbility::GetInstance().Back();
+ unsigned short keyID = event.GetKeyId();
+ unsigned short keyState = event.GetState();
+ // 如果是power键,且是亮屏状态,按键释放就返回上一级
+ if (keyID == static_cast<uint16_t>(ZliteKeyCode::ZLITE_KEY_POWER) &&
+ keyState == 1 && uapi_get_screen_state() == 0) {
+ // 回退
+ NativeAbility::GetInstance().Back();
+ }
+ return true;
+}
+
+void EphemerisDownloadPresenter::Notify() {
+ //根据下载状态更新视图
+ switch (EphemerisDownloadModel::GetInstance().GetEphemerisDownloadStatus()) {
+ case EphemerisDownloadStatus::DOWNLOADING:
+ if (EphemerisDownloadView::GetInstance()) {
+ if (EphemerisDownloadView::GetInstance()->GetDownloadLabel()) {
+ //判断连接状态
+ //连接成功
+ //连接失败
+ //显示下载进度等
+ if(EphemerisDownloadModel::GetInstance().GetEphemerisDownloadProgress() == -1)
+ {
+ EphemerisDownloadView::GetInstance()->GetDownloadLabel()->SetText(
+ "下载失败");
+ }
+ else if(EphemerisDownloadModel::GetInstance().GetEphemerisDownloadProgress() == 0)
+ {
+ EphemerisDownloadView::GetInstance()->GetDownloadLabel()->SetText(
+ "下载中,正在链接网络");
+ }
+ else if(EphemerisDownloadModel::GetInstance().GetEphemerisDownloadProgress() > 0)
+ {
+ std::string progress = "下载中,正在下载"+std::to_string(EphemerisDownloadModel::GetInstance().GetEphemerisDownloadProgress())+"%";
+ EphemerisDownloadView::GetInstance()->GetDownloadLabel()->SetText(progress.c_str());
+ }
+ }
+ }
+ break;
+ case EphemerisDownloadStatus::DOWNLOADED:
+ if (EphemerisDownloadView::GetInstance()) {
+ if (EphemerisDownloadView::GetInstance()->GetDownloadLabel()) {
+ EphemerisDownloadView::GetInstance()->GetDownloadLabel()->SetText(
+ "下载成功");
+ //更新有效期
+ }
+ }
+ break;
+ case EphemerisDownloadStatus::FAILED:
+ if (EphemerisDownloadView::GetInstance()) {
+ if (EphemerisDownloadView::GetInstance()->GetDownloadLabel()) {
+ EphemerisDownloadView::GetInstance()->GetDownloadLabel()->SetText(
+ "下载失败");
+ }
+ if (EphemerisDownloadView::GetInstance()->GetValidityPeriodLabel()) {
+ EphemerisDownloadView::GetInstance()->GetValidityPeriodLabel()->SetText(
+ "无星历数据");
+ }
+ }
+ break;
+ case EphemerisDownloadStatus::NOT_DOWNLOADED:
+ if (EphemerisDownloadView::GetInstance()) {
+ if (EphemerisDownloadView::GetInstance()->GetDownloadLabel()) {
+ EphemerisDownloadView::GetInstance()->GetDownloadLabel()->SetText(
+ "未下载");
+ }
+ if (EphemerisDownloadView::GetInstance()->GetValidityPeriodLabel()) {
+ EphemerisDownloadView::GetInstance()->GetValidityPeriodLabel()->SetText(
+ "无星历数据");
+ }
}
- return true;
+ break;
+ }
}
} // namespace OHOS
namespace OHOS {
-EphemerisDownloadView::EphemerisDownloadView() {}
+static EphemerisDownloadView *g_pEphemerisDownloadView{nullptr};
-EphemerisDownloadView::~EphemerisDownloadView() {}
+EphemerisDownloadView::EphemerisDownloadView() {
+ g_pEphemerisDownloadView = this;
+}
+
+EphemerisDownloadView::~EphemerisDownloadView() {
+ if (labelTitle_!=nullptr)
+ {
+ delete labelTitle_;
+ labelTitle_ = nullptr;
+ }
+ if (labelStatus_!=nullptr)
+ {
+ delete labelStatus_;
+ labelStatus_ = nullptr;
+ }
+ if (labelValidity_!=nullptr)
+ {
+ delete labelValidity_;
+ labelValidity_ = nullptr;
+ }
+ if (btnDownload_!=nullptr)
+ {
+ delete btnDownload_;
+ btnDownload_ = nullptr;
+ }
+ if (btnBack_!=nullptr)
+ {
+ delete btnBack_;
+ btnBack_ = nullptr;
+ }
+ if (labelNote_!=nullptr)
+ {
+ delete labelNote_;
+ labelNote_ = nullptr;
+ }
+ g_pEphemerisDownloadView = nullptr;
+}
void EphemerisDownloadView::OnStart() {
if (!EphemerisDownloadInit()) {
GetRootContainer()->SetTouchable(true);
}
+EphemerisDownloadView *EphemerisDownloadView::GetInstance() {
+ return g_pEphemerisDownloadView;
+}
+
bool EphemerisDownloadView::EphemerisDownloadInit() {
// 初始化标题
- labelTitle_.SetPosition(83, 50, 300, 60);
- labelTitle_.SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);
- labelTitle_.SetText("下载星历");
- labelTitle_.SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
- labelTitle_.SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
- AddViewToRootContainer(&labelTitle_);
+ labelTitle_ = new UILabelExt();
+ if (labelTitle_ == nullptr) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "labelTitle_ new failed");
+ return false;
+ }
+ labelTitle_->SetPosition(83, 50, 300, 60);
+ labelTitle_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);
+ labelTitle_->SetText("下载星历");
+ labelTitle_->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
+ labelTitle_->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
+ AddViewToRootContainer(labelTitle_);
// 初始化状态标签
- labelStatus_.SetPosition(90, 120, 200, 40);
- labelStatus_.SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24);
- labelStatus_.SetText("未下载");
- labelStatus_.SetTextColor(Color::Yellow());
- AddViewToRootContainer(&labelStatus_);
+ labelStatus_ = new UILabelExt();
+ if (labelStatus_ == nullptr) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "labelStatus_ new failed");
+ return false;
+ }
+ labelStatus_->SetPosition(90, 120, 200, 40);
+ labelStatus_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24);
+ labelStatus_->SetText("未下载");
+ labelStatus_->SetTextColor(Color::Yellow());
+ AddViewToRootContainer(labelStatus_);
// 初始化有效期标签
- labelValidity_.SetPosition(90, 170, 300, 40);
- labelValidity_.SetFont(DEFAULT_VECTOR_FONT_FILENAME, 20);
- labelValidity_.SetText("有效期: 无");
- labelValidity_.SetTextColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0x7f));
- AddViewToRootContainer(&labelValidity_);
+ labelValidity_ = new UILabelExt();
+ if (labelValidity_ == nullptr) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "labelValidity_ new failed");
+ return false;
+ }
+ labelValidity_->SetPosition(90, 170, 300, 40);
+ labelValidity_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 20);
+ labelValidity_->SetText("有效期: 无");
+ labelValidity_->SetTextColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0x7f));
+ AddViewToRootContainer(labelValidity_);
// 初始化下载按钮
- btnDownload_.SetPosition(50, 250, 300, 60);
- btnDownload_.SetText("下载");
- btnDownload_.SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24);
- btnDownload_.SetOnClickListener(presenter_);
- AddViewToRootContainer(&btnDownload_);
+ btnDownload_ = new UILabelButtonExt();
+ if (btnDownload_ == nullptr) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "btnDownload_ new failed");
+ return false;
+ }
+ btnDownload_->SetPosition(50, 250, 300, 60);
+ btnDownload_->SetText("下载");
+ btnDownload_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24);
+ btnDownload_->SetViewId(DOWNLOAD_ID);
+ btnDownload_->SetOnClickListener(presenter_);
+ AddViewToRootContainer(btnDownload_);
// 初始化返回按钮
- btnBack_.SetPosition(50, 350, 300, 60);
- btnBack_.SetText("返回");
- btnBack_.SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24);
- btnBack_.SetOnClickListener(presenter_);
- AddViewToRootContainer(&btnBack_);
+ btnBack_ = new UILabelButtonExt();
+ if (btnBack_ == nullptr) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "btnBack_ new failed");
+ return false;
+ }
+ btnBack_->SetPosition(50, 350, 300, 60);
+ btnBack_->SetText("返回");
+ btnBack_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 24);
+ btnBack_->SetViewId(BACK_ID);
+ btnBack_->SetOnClickListener(presenter_);
+ AddViewToRootContainer(btnBack_);
// 初始化说明标签
- labelNote_.SetPosition(53, 420, 360, 40);
- labelNote_.SetFont(DEFAULT_VECTOR_FONT_FILENAME, 18);
- labelNote_.SetText("星历数据用于提高GPS定位精度");
- labelNote_.SetTextColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0x5f));
- AddViewToRootContainer(&labelNote_);
+ labelNote_ = new UILabelExt();
+ if (labelNote_ == nullptr) {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "labelNote_ new failed");
+ return false;
+ }
+ labelNote_->SetPosition(80, 420, 360, 40);
+ labelNote_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 18);
+ labelNote_->SetText("星历数据用于提高GPS定位精度");
+ labelNote_->SetTextColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0x5f));
+ AddViewToRootContainer(labelNote_);
return true;
}
-void EphemerisDownloadView::OnClick(UIView &view) {
- const UILabelButtonExt *ptr = dynamic_cast<UILabelButtonExt *>(&view);
- if (ptr == nullptr) {
- return;
- }
-
- if (ptr->GetViewId() == "downloadButton") {
- EphemerisDownloadModel::GetInstance().DownloadEphemeris();
- // 更新状态
- bool isDownloaded = EphemerisDownloadModel::GetInstance().GetEphemerisDownloadStatus();
- if (isDownloaded) {
- labelStatus_.SetText("已下载");
- labelStatus_.SetTextColor(Color::Green());
- } else {
- labelStatus_.SetText("未下载");
- labelStatus_.SetTextColor(Color::Yellow());
- }
-
- // 更新有效期
- std::string validity = EphemerisDownloadModel::GetInstance().GetEphemerisValidityPeriod();
- if (validity.empty()) {
- labelValidity_.SetText("有效期: 无");
- } else {
- std::string text = "有效期: " + validity;
- labelValidity_.SetText(text.c_str());
- }
- }
+UILabelExt *EphemerisDownloadView::GetDownloadLabel() {
+ return labelStatus_;
+}
+
+UILabelExt *EphemerisDownloadView::GetValidityPeriodLabel() {
+ return labelValidity_;
}
} // namespace OHOS
else{
settingLabelExt.SetTextId(itemInfo.textId);
}
- else{
- settingLabelExt.SetTextId(itemInfo.textId);
- }
settingLabelExt.SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
settingLabelButtonExt.SetVisible(false);
SetTouchable(false);