diff --git a/cameraApp/BUILD.gn b/cameraApp/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..aaf41d3ae4bf74c5f870205a459a6daadb0fd2e3 --- /dev/null +++ b/cameraApp/BUILD.gn @@ -0,0 +1,78 @@ +# Copyright (c) 2020 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. + +import("//build/lite/config/component/lite_component.gni") +import("//build/lite/config/hap_pack.gni") + +shared_library("cameraApp") { + sources = [ + "cameraApp/src/main/cpp/camera_ability.cpp", + "cameraApp/src/main/cpp/camera_ability_slice.cpp", + "cameraApp/src/main/cpp/camera_manager.cpp" + ] + + deps = [ + "//foundation/aafwk/frameworks/ability_lite:aafwk_abilitykit_lite", + "//foundation/appexecfwk/frameworks/bundle_lite:bundle", + "//foundation/communication/frameworks/ipc_lite:liteipc_adapter", + "//foundation/distributedschedule/services/samgr_lite/samgr:samgr", + "//utils/native/lite/kv_store:kv_store", + "//foundation/graphic/lite/frameworks/ui:ui", + "//foundation/graphic/lite/utils:graphic_utils", + "//foundation/graphic/lite/frameworks/surface", + "//foundation/multimedia/frameworks/camera_lite:camera", + "//foundation/multimedia/frameworks/recorder_lite:recorder", + ] + + include_dirs = [ + "cameraApp/src/main/cpp", + "//foundation/aafwk/interfaces/kits/ability_lite", + "//foundation/graphic/lite/interfaces/kits/ui/components", + "//foundation/graphic/lite/interfaces/kits/ui", + "//foundation/graphic/lite/interfaces/kits/utils", + "//foundation/graphic/lite/interfaces/kits/config", + "//foundation/appexecfwk/interfaces/kits/bundle_lite", + "//foundation/aafwk/interfaces/kits/want_lite", + "//base/startup/interfaces/kits/syspara_lite", + "//foundation/multimedia/interfaces/kits/camera_lite", + "//foundation/multimedia/interfaces/kits/camera_lite" + ] + ldflags = [ + "-L$ohos_root_path/sysroot/usr/lib", + "-Wl,-rpath-link=$ohos_root_path/sysroot/usr/lib", + "-lstdc++", "-lcamera", "-lsurface", "-lrecorder" + ] + defines = [ + "ENABLE_WINDOW=1", + "ABILITY_WINDOW_SUPPORT" + ] +} + +lite_component("cameraApp_component") { + + features = [ + ":cameraApp", + ] +} + +hap_pack("cameraApp_hap") +{ + deps = [":cameraApp"] + mode = "hap" + json_path = "cameraApp/src/main/config.json" + ability_so_path = "$root_out_dir/libcameraApp.so" + force = "true" + cert_profile = "cert/camera_HarmonyAppProvision_Release.p7b" + resources_path = "cameraApp/src/main/resources" + hap_name = "cameraApp" +} diff --git a/cameraApp/cameraApp/src/main/config.json b/cameraApp/cameraApp/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..37cf09cbb91b4a70663324fb187c4bbcb211884b --- /dev/null +++ b/cameraApp/cameraApp/src/main/config.json @@ -0,0 +1,99 @@ +{ + "app": { + "bundleName": "com.huawei.camera", + "vendor": "huawei", + "version": { + "code": 2, + "name": "2" + }, + "apiVersion": { + "compatible": 3, + "target": 4 + } + }, + "deviceConfig": { + "default": { + } + }, + "module": { + "package": "com.huawei.camera", + "name": ".MyHarmonyAbilityPackage", + "deviceType": [ + "phone", + "tv", + "tablet", + "pc", + "car", + "smartWatch", + "sportsWatch", + "smartVision" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "cameraApp", + "moduleType": "entry" + }, + "abilities": [ + { + "name": "CameraAbility", + "icon": "assets/cameraApp/resources/base/media/camera.png", + "label": "test app 1", + "launchType": "standard", + "type": "page", + "visible": true + } + ], + "reqPermissions": [ + { + "name": "ohos.permission.CAMERA", + "reason": "USER_GRANT", + "usedScene": { + "ability": [ + ".FormAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.MODIFY_AUDIO_SETTINGS", + "reason": "SYSTEM_GRANT", + "usedScene": { + "ability": [ + ".FormAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.READ_MEDIA", + "reason": "USER_GRANT", + "usedScene": { + "ability": [ + ".FormAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.MICROPHONE", + "reason": "USER_GRANT", + "usedScene": { + "ability": [ + ".FormAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.WRITE_MEDIA", + "reason": "USER_GRANT", + "usedScene": { + "ability": [ + ".FormAbility" + ], + "when": "inuse" + } + } + ] + } +} diff --git a/cameraApp/cameraApp/src/main/cpp/camera_ability.cpp b/cameraApp/cameraApp/src/main/cpp/camera_ability.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3edaa35be9e68f034773e93f08bbd30c642011ef --- /dev/null +++ b/cameraApp/cameraApp/src/main/cpp/camera_ability.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2020 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. +*/ + +#include "camera_ability.h" + +namespace OHOS { +REGISTER_AA(CameraAbility) + +void CameraAbility::OnStart(const Want &want) +{ + printf("CameraAbility::OnStart\n"); + SetMainRoute("CameraAbilitySlice"); + + Ability::OnStart(want); +} + +void CameraAbility::OnInactive() +{ + printf("CameraAbility::OnInactive\n"); + Ability::OnInactive(); +} + +void CameraAbility::OnActive(const Want &want) +{ + printf("CameraAbility::OnActive\n"); + Ability::OnActive(want); +} + +void CameraAbility::OnBackground() +{ + printf("CameraAbility::OnBackground\n"); + Ability::OnBackground(); +} + +void CameraAbility::OnStop() +{ + printf("CameraAbility::OnStop\n"); + Ability::OnStop(); +} +} \ No newline at end of file diff --git a/cameraApp/cameraApp/src/main/cpp/camera_ability.h b/cameraApp/cameraApp/src/main/cpp/camera_ability.h new file mode 100644 index 0000000000000000000000000000000000000000..92510c7bf9de9d02b1ce4c2fbe57208004670b9f --- /dev/null +++ b/cameraApp/cameraApp/src/main/cpp/camera_ability.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2020 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. +*/ + +#ifndef OHOS_MAIN_ABILITY_H +#define OHOS_MAIN_ABILITY_H + +#include + +namespace OHOS { +class CameraAbility : public Ability { +protected: + void OnStart(const Want &want) override; + void OnInactive() override; + void OnActive(const Want &want) override; + void OnBackground() override; + void OnStop() override; +}; +} + +#endif // OHOS_MAIN_ABILITY_H diff --git a/cameraApp/cameraApp/src/main/cpp/camera_ability_slice.cpp b/cameraApp/cameraApp/src/main/cpp/camera_ability_slice.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c757642cdcb8293a12baad8f04df5d397813346a --- /dev/null +++ b/cameraApp/cameraApp/src/main/cpp/camera_ability_slice.cpp @@ -0,0 +1,578 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "camera_ability_slice.h" +#include +#include +#include +#include + +#include "ability_manager.h" +#include "ui_config.h" +#include "securec.h" + +namespace OHOS { +REGISTER_AS(CameraAbilitySlice) + +static constexpr const char *VIEWIMAGE = "/userdata/photo/tmp.jpg"; +// button idx +static uint8_t g_curButtonIdx = 1; +// video info +static int g_isRecording = 0; // 0--stop recording 1--start/resume recording 2--pause recording + +class SliderAnimator : public Animator, public AnimatorCallback { +public: + explicit SliderAnimator(UISlider *slider, UIImageView *backview, UISurfaceView *surface, + SampleCameraManager *cManager, uint16_t duration) + : Animator(this, slider, duration, true), slider_(slider), backgroundView_(backview), mSurfaceview(surface), + camManager(cManager), duration_(duration) {} + virtual ~SliderAnimator() {} + + void Callback(UIView *view) override + { + if (runType_ == 1) { /* 1 is normal photo */ + if (camManager->SampleCameraCaptrueIsFinish()) { + if (ss == 0) { /* 0 first record times */ + BackViewSetImage(VIEWIMAGE); + backgroundView_->SetVisible(true); + backgroundView_->Invalidate(); + ss = GetRunTime(); + } else { + if ((GetRunTime()-ss) > 1000) { /* 1000 = 1s */ + backgroundView_->SetVisible(false); + backgroundView_->Invalidate(); + Stop(); + } + } + } + } else if (runType_ == 2) { /* 2 is record mode */ + int ms = GetRunTime(); + if (ss == 0) { + ss = GetRunTime(); + return; + } + if ((ms - ss) > 1000 && camRestart == false) { /* 1000 = 1s */ + camManager->SampleCameraStopRecord(); + printf("after stop record!! \n"); + printf("before SampleCameraStart!! \n"); + camManager->SampleCameraStart(mSurfaceview->GetSurface()); + printf("after SampleCameraStart!! \n"); + camRestart = true; + } else if ((ms - ss) > 2000) { /* 2000 = 2s */ + backgroundView_->SetVisible(false); + backgroundView_->Invalidate(); + Stop(); + } + } + } + + void OnStop(UIView &view) override + { + runType_ = 0; + ss = 0; + camRestart = false; + } + + void SetStart(int type) + { + runType_ = type; + camRestart = false; + } +private: + UIImageView *backgroundView_; + UISlider *slider_; + UISurfaceView *mSurfaceview; + SampleCameraManager *camManager; + uint32_t duration_; + uint32_t ss = 0; + uint32_t runType_ = 0; + bool camRestart; + + void BackViewSetImage(const char *image) + { + backgroundView_->SetSrc(image); + int16_t imageWidth = backgroundView_->GetWidth(); + int16_t imageHeight = backgroundView_->GetHeight(); + if (imageWidth > SCREEN_WIDTH || imageHeight > SCREEN_HEIGHT) { + TransformMap transMap(backgroundView_->GetOrigRect()); + float scaleWidth = 1.0; + float scaleHeight = 1.0; + if (imageWidth > SCREEN_WIDTH) + scaleWidth = static_cast(SCREEN_WIDTH) / imageWidth; + if (imageHeight > SCREEN_HEIGHT) + scaleHeight = static_cast(SCREEN_HEIGHT) / imageHeight; + float scale = (scaleWidth < scaleHeight) ? scaleWidth : scaleHeight; + + transMap.Scale(Vector2(scale, scale), Vector2(0, 0)); + backgroundView_->SetTransformMap(transMap); + backgroundView_->SetTransformAlgorithm(TransformAlgorithm::NEAREST_NEIGHBOR); + imageWidth = imageWidth * scale; + imageHeight = imageHeight * scale; + } + int16_t imagePosX = (SCREEN_WIDTH - imageWidth) / 2; /* 2 half */ + int16_t imagePosY = (SCREEN_HEIGHT - imageHeight) / 2; /* 2 half */ + backgroundView_->SetPosition(imagePosX, imagePosY); + } +}; + +class CameraImageButtonOnClickListener : public UIView::OnClickListener { +public: + CameraImageButtonOnClickListener(UIView *uiView, UISurfaceView *surface, + UIImageView *iamgeview, TaskView *taskView, SliderAnimator *animator) : uiView_(uiView), + mSurfaceview(surface), backgroundView_(iamgeview), gTaskView_(taskView), animator_(animator) + { + bttnLeft = nullptr; + bttnRight = nullptr; + bttnMidle = nullptr; + bttnRecord = nullptr; + recordImage = nullptr; + } + virtual ~CameraImageButtonOnClickListener(){} + + bool OnClick(UIView &view, const ClickEvent &event) override + { + if (cManager_ == nullptr || tmLabel == nullptr || bttnLeft == nullptr || bttnMidle == nullptr || + bttnRight == nullptr || bttnRecord == nullptr || recordImage == nullptr) { + printf("nullptr point!! \n"); + return false; + } + switch (bttnIdx_) { + case 0: { /* 0 thumb */ + StartGallery(); + return true; + } + break; + case 1: /* 1 photo */ + StartTakePhoto(); + break; + case 2: /* 2 record */ + RecorderButtonDeal(); + break; + case 3: /* 3 pause/resume */ + if (g_isRecording == 1) { /* 1 start */ + g_isRecording = 2; /* 2 pause */ + gTaskView_->SetPause(); + bttnRecord->SetSrc(UI_IMAGE_PATH"ic_camera_record_continue.png"); + } else { + bttnRecord->SetSrc(UI_IMAGE_PATH"ic_camera_record_pause.png"); + g_isRecording = 1; /* 1 start */ + gTaskView_->SetResume(); + } + bttnRecord->Invalidate(); + return true; + default: + return true; + } + + if (g_curButtonIdx != 2) { /* 2 is record button */ + recordImage->SetVisible(false); + recordImage->Invalidate(); + + tmLabel->SetVisible(false); + tmLabel->Invalidate(); + + bttnRecord->SetVisible(false); + bttnRecord->Invalidate(); + } else { + tmLabel->SetVisible(true); + tmLabel->Invalidate(); + } + + uiView_->Invalidate(); + + return true; + } + + // idx 0--left, 1--midle, 2--right, 3--record, 4--rview + void SetImageView(UIImageView *imgButton, int idx) + { + if (idx == 0) bttnLeft = imgButton; /* 0 thumb */ + if (idx == 1) bttnMidle = imgButton; /* 1 photo */ + if (idx == 2) bttnRight = imgButton; /* 2 record */ + if (idx == 3) bttnRecord = imgButton; /* 3 pause */ + if (idx == 4) recordImage = imgButton; /* 4 icon */ + } + + void SetButtonIdxCamera(SampleCameraManager *cManager, int idx) + { + cManager_ = cManager; + bttnIdx_ = idx; + } + + void SetLabel(UILabel *l) + { + tmLabel = l; + } + +private: + UIView *uiView_; + UIImageView *backgroundView_; + UISurfaceView *mSurfaceview; + SliderAnimator *animator_; + int16_t bttnIdx_; + + SampleCameraManager *cManager_; + UIImageView *bttnLeft; + UIImageView *bttnRight; + UIImageView *bttnMidle; + UIImageView *bttnRecord; + UIImageView *recordImage; + UILabel *tmLabel; + TaskView *gTaskView_; + void StartGallery(void) + { + Want want1 = { nullptr }; + ElementName element = { nullptr }; + SetElementBundleName(&element, "com.huawei.gallery"); + SetElementAbilityName(&element, "GalleryAbility"); + SetWantElement(&want1, element); + SetWantData(&want1, "WantData", strlen("WantData") + 1); + StartAbility(&want1); + } + + void StartTakePhoto(void) + { + if (g_curButtonIdx != 1) { /* 1 photo */ + if (g_isRecording) { + return; + } + g_curButtonIdx = 1; + bttnLeft->SetPosition(LEFT_BUTTON_X, LEFT_BUTTON_Y, LEFT_BUTTON_W, LEFT_BUTTON_H); + bttnMidle->SetPosition(MID_BUTTON_X, MID_BUTTON_Y, MID_BUTTON_W, MID_BUTTON_H); + bttnRight->SetPosition(RIGHT_BUTTON_X, RIGHT_BUTTON_Y, RIGHT_BUTTON_W, RIGHT_BUTTON_H); + bttnMidle->SetSrc(UI_IMAGE_PATH"ic_camera_shutter.png"); + bttnRight->SetSrc(UI_IMAGE_PATH"ic_camera_video.png"); + } else { + cManager_->SampleCameraCaptrue(0); + animator_->SetStart(1); + animator_->Start(); + } + } + + void RecorderButtonDeal(void) + { + if (g_curButtonIdx != 2) { /* 2 record */ + g_curButtonIdx = 2; /* 2 record */ + bttnRight->SetPosition(MID_BUTTON_X, MID_BUTTON_Y, MID_BUTTON_W, MID_BUTTON_H); + bttnMidle->SetPosition(RIGHT_BUTTON_X, RIGHT_BUTTON_Y, RIGHT_BUTTON_W, RIGHT_BUTTON_H); + + bttnMidle->SetSrc(UI_IMAGE_PATH"ic_camera_record_camra.png"); + bttnRight->SetSrc(UI_IMAGE_PATH"ic_camera_record.png"); + } else { + if (g_isRecording) { + g_isRecording = 0; /* 0 stop */ + animator_->Stop(); + + cManager_->SampleCameraStopRecord(); + printf("after stop record!! \n"); + printf("before SampleCameraStart!! \n"); + cManager_->SampleCameraStart(mSurfaceview->GetSurface()); + printf("after SampleCameraStart!! \n"); + + gTaskView_->SetStop(); + recordImage->SetVisible(false); + recordImage->Invalidate(); + + bttnRecord->SetVisible(false); + bttnRecord->Invalidate(); + bttnMidle->SetVisible(true); + bttnMidle->Invalidate(); + bttnLeft->SetVisible(true); + bttnLeft->Invalidate(); + + bttnRight->SetSrc(UI_IMAGE_PATH"ic_camera_record.png"); + } else { + g_isRecording = 1; /* 1 start */ + cManager_->SampleCameraCaptrue(1); /* 1 start */ + cManager_->SampleCameraStartRecord(mSurfaceview->GetSurface()); + + recordImage->SetVisible(true); + recordImage->Invalidate(); + bttnMidle->SetVisible(false); + bttnMidle->Invalidate(); + gTaskView_->SetStart(); + bttnLeft->SetVisible(false); + bttnLeft->Invalidate(); + + bttnRight->SetSrc(UI_IMAGE_PATH"ic_camera_record_stop.png"); + } + bttnRight->Invalidate(); + } + } + + void BackViewSetImage(const char *image) + { + backgroundView_->SetSrc(image); + int16_t imageWidth = backgroundView_->GetWidth(); + int16_t imageHeight = backgroundView_->GetHeight(); + if (imageWidth > SCREEN_WIDTH || imageHeight > SCREEN_HEIGHT) { + TransformMap transMap(backgroundView_->GetOrigRect()); + float scaleWidth = 1.0; + float scaleHeight = 1.0; + if (imageWidth > SCREEN_WIDTH) + scaleWidth = static_cast(SCREEN_WIDTH) / imageWidth; + if (imageHeight > SCREEN_HEIGHT) + scaleHeight = static_cast(SCREEN_HEIGHT) / imageHeight; + float scale = (scaleWidth < scaleHeight) ? scaleWidth : scaleHeight; + + transMap.Scale(Vector2(scale, scale), Vector2(0, 0)); + backgroundView_->SetTransformMap(transMap); + backgroundView_->SetTransformAlgorithm(TransformAlgorithm::NEAREST_NEIGHBOR); + imageWidth = imageWidth * scale; + imageHeight = imageHeight * scale; + } + int16_t imagePosX = (SCREEN_WIDTH - imageWidth) / 2; /* 2 half */ + int16_t imagePosY = (SCREEN_HEIGHT - imageHeight) / 2; /* 2 half */ + backgroundView_->SetPosition(imagePosX, imagePosY); + } +}; + +CameraAbilitySlice::~CameraAbilitySlice() +{ + gTaskView_->SetPause(); + delete gTaskView_; + for (int i = 0; i < BUTTON_NUMS; i++) + delete bttnImageClick[i]; + delete cam_manager; + delete backBttn; + delete backIcon; + delete txtMsgLabel; + delete recordImage; + delete tmLabel; + delete bttnLeft; + delete bttnMidle; + delete bttnRight; + delete bttnRecord; + delete scroll; + animator_->Stop(); + AnimatorManager::GetInstance()->Remove(animator_); + delete animator_; + delete slider; + delete buttonListener_; + delete surfaceView; + delete background_; +} + +void CameraAbilitySlice::SetHead(void) +{ + backIcon = new UIImageView(); + backIcon->SetTouchable(true); + backIcon->SetSrc(UI_IMAGE_PATH"ic_back.png"); + backIcon->SetPosition(BACK_LABEL_X, BACK_LABEL_Y, BACK_LABEL_W, BACK_LABEL_H); + + backBttn = new UIImageView(); + backBttn->SetTouchable(true); + backBttn->SetPosition(0, 0, BACK_LABEL_W * 4, BACK_LABEL_H * 4); /* 4 cups of icon size */ + backBttn->SetStyle(STYLE_BACKGROUND_OPA, 0); + auto backBttnonClick = [this](UIView &view, const Event &event) -> bool { + printf("############ from launcher enter #############\n"); + TerminateAbility(); + printf("############ to launcher #############\n"); + return true; + }; + + buttonListener_ = new EventListener(backBttnonClick, nullptr); + backBttn->SetOnClickListener(buttonListener_); + backIcon->SetOnClickListener(buttonListener_); + + txtMsgLabel = new UILabel(); + txtMsgLabel->SetPosition(TXT_LABEL_X, TXT_LABEL_Y, TXT_LABEL_W, TXT_LABEL_H); + txtMsgLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER); + + txtMsgLabel->SetFont((const char *)TTF_PATH, FONT_SIZE); + + txtMsgLabel->SetAlign(TEXT_ALIGNMENT_LEFT); + txtMsgLabel->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + txtMsgLabel->SetStyle(STYLE_BACKGROUND_OPA, 0); + txtMsgLabel->SetText((char *)"相机"); + + recordImage = new UIImageView(); + recordImage->SetTouchable(false); + recordImage->SetSrc(UI_IMAGE_PATH"ic_timer.png"); + recordImage->SetPosition(RECORD_IMAGE_X, RECORD_IMAGE_Y, RECORD_IMAGE_W, RECORD_IMAGE_H); + recordImage->SetStyle(STYLE_BACKGROUND_OPA, 0); + + tmLabel = new UILabel(); + tmLabel->SetPosition(TIME_LABEL_X, TIME_LABEL_Y, TIME_LABEL_W, TIME_LABEL_H); + tmLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER); + tmLabel->SetText("00:00"); + + tmLabel->SetFont((const char *)TTF_PATH, FONT_SIZE); + + tmLabel->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + tmLabel->SetStyle(STYLE_BACKGROUND_OPA, 0); + + recordImage->SetVisible(false); + recordImage->Invalidate(); + tmLabel->SetVisible(false); + tmLabel->Invalidate(); +} + +void CameraAbilitySlice::SetBottom(void) +{ + scroll = new UIScrollView(); + scroll->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::White())); + scroll->SetStyle(STYLE_BACKGROUND_OPA, 0); + scroll->SetPosition(SCROLL_VIEW_X, SCROLL_VIEW_Y, SCROLL_VIEW_W, SCROLL_VIEW_H); + scroll->SetHorizontalScrollState(true); + scroll->SetVerticalScrollState(false); + scroll->SetXScrollBarVisible(false); + scroll->SetYScrollBarVisible(false); + + bttnLeft = new UIImageView(); + bttnLeft->SetTouchable(true); + bttnLeft->SetPosition(LEFT_BUTTON_X, LEFT_BUTTON_Y, LEFT_BUTTON_W, LEFT_BUTTON_H); + bttnLeft->SetSrc(UI_IMAGE_PATH"ic_camera_photo.png"); + bttnLeft->SetStyle(STYLE_BACKGROUND_OPA, 0); + + bttnMidle = new UIImageView(); + bttnMidle->SetTouchable(true); + bttnMidle->SetSrc(UI_IMAGE_PATH"ic_camera_shutter.png"); + bttnMidle->SetPosition(MID_BUTTON_X, MID_BUTTON_Y, MID_BUTTON_W, MID_BUTTON_H); + bttnMidle->SetStyle(STYLE_BACKGROUND_OPA, 0); + + bttnRight = new UIImageView(); + bttnRight->SetTouchable(true); + bttnRight->SetPosition(RIGHT_BUTTON_X, RIGHT_BUTTON_Y, RIGHT_BUTTON_W, RIGHT_BUTTON_H); + bttnRight->SetSrc(UI_IMAGE_PATH"ic_camera_video.png"); + bttnRight->SetStyle(STYLE_BACKGROUND_OPA, 0); + + bttnRecord = new UIImageView(); + bttnRecord->SetTouchable(true); + bttnRecord->SetPosition(RIGHT_BUTTON_X, RIGHT_BUTTON_Y, RIGHT_BUTTON_W, RIGHT_BUTTON_H); + bttnRecord->SetSrc(UI_IMAGE_PATH"ic_camera_record_pause.png"); + bttnRecord->SetStyle(STYLE_BACKGROUND_OPA, 0); + bttnRecord->SetVisible(false); + bttnRecord->Invalidate(); + + slider = new UISlider(); + slider->SetPosition(-1, -1, 1, 1); + + gTaskView_ = new TaskView(tmLabel); + gTaskView_->TaskStart(); + + animator_ = new SliderAnimator(slider, background_, surfaceView, cam_manager, 10000); /* 10000 = 10s */ + AnimatorManager::GetInstance()->Add(animator_); + + UIImageView *imageV[BUTTON_NUMS] = {bttnLeft, bttnMidle, bttnRight, bttnRecord}; + for (int i = 0; i < BUTTON_NUMS; i++) { + bttnImageClick[i] = + new CameraImageButtonOnClickListener((UIView *)scroll, surfaceView, background_, gTaskView_, + (SliderAnimator*)animator_); + ((CameraImageButtonOnClickListener *)bttnImageClick[i])->SetButtonIdxCamera(cam_manager, i); + ((CameraImageButtonOnClickListener *)bttnImageClick[i])->SetLabel(tmLabel); + ((CameraImageButtonOnClickListener *)bttnImageClick[i])->SetImageView(bttnLeft, 0); /* 0 */ + ((CameraImageButtonOnClickListener *)bttnImageClick[i])->SetImageView(bttnMidle, 1); /* 1 */ + ((CameraImageButtonOnClickListener *)bttnImageClick[i])->SetImageView(bttnRight, 2); /* 2 */ + ((CameraImageButtonOnClickListener *)bttnImageClick[i])->SetImageView(bttnRecord, 3); /* 3 */ + ((CameraImageButtonOnClickListener *)bttnImageClick[i])->SetImageView(recordImage, 4); /* 4 */ + imageV[i]->SetOnClickListener(bttnImageClick[i]); + } +} + +void CameraAbilitySlice::OnStart(const Want &want) +{ + AbilitySlice::OnStart(want); + printf("CameraAbilitySlice onstart \n"); + + surfaceView = new UISurfaceView(); + surfaceView->SetPosition(V_GROUP_X, V_GROUP_Y, V_GROUP_W, V_GROUP_H); + surfaceView->GetSurface()->SetWidthAndHeight(IMAGE_WIDTH, IMAGE_HEIGHT); + + background_ = new UIImageView(); + background_->SetTouchable(false); + background_->SetSrc("/userdata/tmp.jpg"); + background_->SetPosition(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); + background_->SetVisible(true); + background_->Invalidate(); + + cam_manager = new SampleCameraManager(0); + cam_manager->SampleCameraCreate(0); + + SetHead(); + + SetBottom(); + + scroll->Add(bttnLeft); + scroll->Add(bttnMidle); + scroll->Add(bttnRight); + scroll->Add(bttnRecord); + + RootView *rootView = RootView::GetWindowRootView(); + rootView->SetPosition(0, 0); + rootView->Resize(SCREEN_WIDTH, SCREEN_HEIGHT); + rootView->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Black())); + + rootView->Add(surfaceView); + rootView->Add(background_); + rootView->Add(backBttn); + rootView->Add(backIcon); + rootView->Add(txtMsgLabel); + rootView->Add(recordImage); + rootView->Add(tmLabel); + rootView->Add(scroll); + rootView->Add(slider); + + int timecnt = 0; + while (1) { + if (++timecnt > 5) { /* 5s timeout */ + printf("wait camera timeout!! \n"); + break; + } + if (cam_manager->SampleCameraIsReady()) break; + sleep(1); + } + SetUIContent(rootView); +} + +void CameraAbilitySlice::OnInactive() +{ + printf("CameraAbilitySlice::OnInactive\n"); + AbilitySlice::OnInactive(); +} + +void CameraAbilitySlice::OnActive(const Want &want) +{ + printf("CameraAbilitySlice::OnActive\n"); + AbilitySlice::OnActive(want); + + if (cam_manager) { + cam_manager->SampleCameraStart(surfaceView->GetSurface()); + if (background_) { + background_->SetVisible(false); + background_->Invalidate(); + } + } +} + +void CameraAbilitySlice::OnBackground() +{ + printf("CameraAbilitySlice::OnBackground\n"); + AbilitySlice::OnBackground(); + if (background_) { + background_->SetSrc("/userdata/tmp.jpg"); + background_->SetVisible(true); + background_->Invalidate(); + } + if (cam_manager) + cam_manager->SampleCameraStop(); +} + +void CameraAbilitySlice::OnStop() +{ + printf("CameraAbilitySlice::OnStop\n"); + AbilitySlice::OnStop(); +} +} diff --git a/cameraApp/cameraApp/src/main/cpp/camera_ability_slice.h b/cameraApp/cameraApp/src/main/cpp/camera_ability_slice.h new file mode 100644 index 0000000000000000000000000000000000000000..2c30c02c675e7f044fcb234b3c6e2f9ef78272a7 --- /dev/null +++ b/cameraApp/cameraApp/src/main/cpp/camera_ability_slice.h @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2020 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. +*/ + +#ifndef OHOS_MAIN_ABILITY_SLICE_H +#define OHOS_MAIN_ABILITY_SLICE_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "event_listener.h" +#include "camera_manager.h" + +namespace OHOS { +class TaskView : public Task { +public: + TaskView() = delete; + TaskView(UILabel* tmLabel):timeLabel_(tmLabel) + { + runEnable_ = false; + gTimeCount_ = 0; + Task::Init(); + } + + virtual ~TaskView(){} + void TaskStart(void) + { + Task::SetPeriod(1000); /* 1000=1s */ + Task::TaskExecute(); + } + + void SetStart(void) + { + runEnable_ = true; + gTimeCount_ = 0; + } + + void SetPause(void) + { + runEnable_ = false; + } + + void SetResume(void) + { + runEnable_ = true; + } + + void SetStop(void) + { + gTimeCount_ = 0; + runEnable_ = false; + UpdateTimeLabel(gTimeCount_); + } + + void Callback() override + { + if (runEnable_) + UpdateTimeLabel(gTimeCount_++); + } +private: + UILabel* timeLabel_; + bool runEnable_; + uint32_t gTimeCount_; + + void UpdateTimeLabel(int ss) + { + char buff[20] = { 0 }; + + if (timeLabel_ == nullptr) return; + + sprintf_s(buff, sizeof(buff), "%02d : %02d", ss / 60, ss % 60); /* 60=1s */ + timeLabel_->SetText(buff); + } +}; + +class CameraAbilitySlice : public AbilitySlice { +public: + CameraAbilitySlice() = default; + ~CameraAbilitySlice() override; + +protected: + void OnStart(const Want &want) override; + void OnInactive() override; + void OnActive(const Want &want) override; + void OnBackground() override; + void OnStop() override; + +private: + static constexpr int BUTTON_NUMS = 4; + static constexpr int FONT_SIZE = 28; + + EventListener *buttonListener_ { nullptr }; + SampleCameraManager *cam_manager; + UIImageView *background_; + UISurfaceView* surfaceView; + UIImageView *backBttn; + UIImageView *backIcon; + UILabel *txtMsgLabel; + UIImageView *recordImage; + UILabel *tmLabel; + + UIImageView* bttnLeft; + UIImageView* bttnMidle; + UIImageView* bttnRight; + UIImageView* bttnRecord; + UIScrollView* scroll; + UISlider *slider; + Animator *animator_; + TaskView *gTaskView_; + UIView::OnClickListener *bttnImageClick[BUTTON_NUMS]; + + void SetHead(); + void SetBottom(); +}; +} + +#endif // OHOS_MAIN_ABILITY_SLICE_H diff --git a/cameraApp/cameraApp/src/main/cpp/camera_manager.cpp b/cameraApp/cameraApp/src/main/cpp/camera_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c6fc92fd3efa5995a464588a4de8e10e587e7c5 --- /dev/null +++ b/cameraApp/cameraApp/src/main/cpp/camera_manager.cpp @@ -0,0 +1,715 @@ +/* + * Copyright (c) 2020 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. + */ + +#include +#include +#include +#include + +#include "camera_manager.h" +#include "securec.h" +#include "ui_config.h" + +using namespace std; +using namespace OHOS; +using namespace OHOS::Media; + +static constexpr int TEMP_BUF_LEN = 8; +static constexpr int MAX_THM_SIZE = (64 * PAGE_SIZE); +static constexpr int FILE_NAME_LEN = 128; +static constexpr int MILLI_SECONDS = 1000; + +char* g_dstBuf = nullptr; + +static int32_t SampleDealThumb(char* psrc, uint32_t srcSize, uint32_t* dstSize, uint16_t u16THMLen) +{ + int32_t endpos = 0; + int32_t s32I = 0; + int32_t startpos = 0; + char tempbuf[TEMP_BUF_LEN] = { 0 }; + int32_t bufpos = 0; + char startflag[2] = { 0xff, 0xd8 }; + char endflag[2] = { 0xff, 0xd9 }; + + while (s32I < srcSize) { + tempbuf[bufpos] = psrc[s32I++]; + if (bufpos > 0) { + if (0 == memcmp(tempbuf + bufpos - 1, startflag, sizeof(startflag))) { + startpos = s32I - 2; /* 2 thumb head offset */ + if (startpos < 0) { + startpos = 0; + } + } + if (0 == memcmp(tempbuf + bufpos - 1, endflag, sizeof(endflag))) { + if (u16THMLen == s32I) { + endpos = s32I; + break; + } else { + endpos = s32I; + break; + } + } + } + bufpos++; + if (bufpos == (TEMP_BUF_LEN - 1)) { + if (tempbuf[bufpos - 1] != 0xFF) { + bufpos = 0; + } + } else if (bufpos > (TEMP_BUF_LEN - 1)) { + bufpos = 0; + } + } + + if ((endpos - startpos <= 0) || (endpos - startpos >= srcSize)) { + return -1; + } + + char* temp = psrc + startpos; + if (MAX_THM_SIZE < (endpos - startpos)) { + return -1; + } + char* cDstBuf = (char*)malloc(endpos - startpos); + if (cDstBuf == nullptr) { + return -1; + } + + (void)memcpy_s(cDstBuf, endpos - startpos, temp, endpos - startpos); + + g_dstBuf = cDstBuf; + *dstSize = endpos - startpos; + + return 0; +} + +static int32_t SampleGetThmFromJpg(char* jpegPath, uint32_t* dstSize) +{ + int32_t s32RtnVal = 0; + FILE* fpJpg = nullptr; + fpJpg = fopen(jpegPath, "rb"); + char* pszFile = nullptr; + int32_t fd = 0; + struct stat stStat; + (void)memset_s(&stStat, sizeof(struct stat), 0, sizeof(struct stat)); + if (fpJpg == nullptr) { + printf("file %s not exist!\n", jpegPath); + return -1; + } + fd = fileno(fpJpg); + fstat(fd, &stStat); + pszFile = (char*)malloc(stStat.st_size); + if ((pszFile == nullptr) || (stStat.st_size < 6)) { /* 6 min size of thumb head */ + fclose(fpJpg); + printf("memory malloc fail!\n"); + return -1; + } + + if (fread(pszFile, stStat.st_size, 1, fpJpg) <= 0) { + fclose(fpJpg); + free(pszFile); + printf("fread jpeg src fail!\n"); + return -1; + } + fclose(fpJpg); + + // The fourth byte is shifted to the left by eight bits then the fifth byte is added. + uint16_t u16THMLen = (pszFile[4] << 8) + pszFile[5]; + if (SampleDealThumb(pszFile, stStat.st_size, dstSize, u16THMLen) < 0) { + printf("get jpg thumb failed! \n"); + free(pszFile); + return -1; + } + free(pszFile); + return 0; +} + +int32_t SampleGetdcfinfo(const char* srcJpgPath, const char* dstThmPath) +{ + int32_t s32RtnVal = 0; + char jpegSrcPath[FILE_NAME_LEN] = { 0 }; + char jpegDesPath[FILE_NAME_LEN] = { 0 }; + uint32_t dstSize = 0; + if (sprintf_s(jpegSrcPath, sizeof(jpegSrcPath), "%s", srcJpgPath) < 0) { + return -1; + } + if (sprintf_s(jpegDesPath, sizeof(jpegDesPath), "%s", dstThmPath) < 0) { + return -1; + } + s32RtnVal = SampleGetThmFromJpg(jpegSrcPath, &dstSize); + if ((s32RtnVal != 0) || (dstSize == 0)) { + printf("fail to get thm\n"); + return -1; + } + FILE* fpTHM = fopen(jpegDesPath, "w"); + if (fpTHM == 0) { + printf("file to create file %s\n", jpegDesPath); + return -1; + } + int32_t u32WritenSize = 0; + while (u32WritenSize < dstSize) { + s32RtnVal = fwrite(g_dstBuf + u32WritenSize, 1, dstSize, fpTHM); + if (s32RtnVal <= 0) { + printf("fail to wirte file, rtn=%d\n", s32RtnVal); + break; + } + u32WritenSize += s32RtnVal; + } + if (fpTHM != nullptr) { + fclose(fpTHM); + fpTHM = 0; + } + if (g_dstBuf != nullptr) { + free(g_dstBuf); + g_dstBuf = nullptr; + } + return 0; +} + +static void SampleSaveCapture(const char* p, uint32_t size, int type, char *timeStamp, int length) +{ + char acFileDcf[FILE_NAME_LEN] = {0}; + FILE *fp = nullptr; + char acFile[FILE_NAME_LEN] = { 0 }; + int ws = 0; + + if (type == 0) { + char tmpFile[FILE_NAME_LEN] = {0}; + if (sprintf_s(tmpFile, sizeof(tmpFile), "%s/photo%s.jpg", PHOTO_PATH, timeStamp) < 0) { + return; + } + fp = fopen(tmpFile, "w+"); + if (fp) { + ws = fwrite(p, 1, size, fp); + fclose(fp); + } + } + + if (sprintf_s(acFile, sizeof(acFile), "%s/tmp.jpg", PHOTO_PATH) < 0) { + return; + } + remove(acFile); + + fp = fopen(acFile, "w+"); + if (fp == NULL) { + return; + } + ws = fwrite(p, 1, size, fp); + fclose(fp); + + if (type == 0) { + if (sprintf_s(acFileDcf, sizeof(acFileDcf), "%s/photo%s.jpg", THUMB_PATH, timeStamp) < 0) { + return; + } + } else if (type == 1) { + if (sprintf_s(acFileDcf, sizeof(acFileDcf), "%s/video%s.jpg", THUMB_PATH, timeStamp) < 0) { + return; + } + } else { + return; + } + + SampleGetdcfinfo(static_cast(acFile), static_cast(acFileDcf)); +} + +static int CameraGetRecordFd(char* p) +{ + int fd = -1; + char pname[128] = {0}; + char *pe = strrchr(p, '.'); + char *ps = strrchr(p, '/'); + + if (strcpy_s(static_cast(pname), sizeof(pname), VIDEO_PATH) != 0) { + return -1; + } + + if (strncat_s(pname, sizeof(pname), ps, pe - ps) != 0) { + return -1; + } + + if (strcat_s(pname, sizeof(pname), ".mp4") < 0) { + return -1; + } + + fd = open(pname, O_RDWR | O_CREAT | O_CLOEXEC | O_TRUNC, S_IRUSR | S_IWUSR); + if (fd <= 0) { + return -1; + } + + return fd; +} + +Recorder *SampleCreateRecorder(int w, int h) +{ + int ret = 0; + AudioCodecFormat audioFormat = AAC_LC; + AudioSourceType inputSource = AUDIO_MIC; + VideoSourceType source = VIDEO_SOURCE_SURFACE_ES; + int32_t sourceId = 0; + int32_t audioSourceId = 0; + + VideoCodecFormat encoder = HEVC; + + Recorder *recorder = new Recorder(); + if ((ret = recorder->SetVideoSource(source, sourceId)) != SUCCESS) { + goto EXIT_; + } + if ((ret = recorder->SetVideoEncoder(sourceId, encoder)) != SUCCESS) { + goto EXIT_; + } + if ((ret = recorder->SetVideoSize(sourceId, w, h)) != SUCCESS) { + goto EXIT_; + } + if ((ret = recorder->SetVideoFrameRate(sourceId, FRAME_RATE)) != SUCCESS) { + goto EXIT_; + } + if ((ret = recorder->SetVideoEncodingBitRate(sourceId, BIT_RATE)) != SUCCESS) { + goto EXIT_; + } + if ((ret = recorder->SetCaptureRate(sourceId, FRAME_RATE)) != SUCCESS) { + goto EXIT_; + } + if ((ret = recorder->SetAudioSource(inputSource, audioSourceId)) != SUCCESS) { + goto EXIT_; + } + if ((ret = recorder->SetAudioEncoder(audioSourceId, audioFormat)) != SUCCESS) { + goto EXIT_; + } + if ((ret = recorder->SetAudioSampleRate(audioSourceId, SAMPLE_RATE)) != SUCCESS) { + goto EXIT_; + } + if ((ret = recorder->SetAudioChannels(audioSourceId, CHANNEL_COUNT)) != SUCCESS) { + goto EXIT_; + } + if ((ret = recorder->SetAudioEncodingBitRate(audioSourceId, AUDIO_ENCODING_BITRATE)) != SUCCESS) { + goto EXIT_; + } + + if ((ret = recorder->SetMaxDuration(36000)) != SUCCESS) { // 36000=10h + goto EXIT_; + } +EXIT_: + if (ret != SUCCESS) { + delete recorder; + recorder = nullptr; + } + return recorder; +} + +// TestFrameStateCallback +void TestFrameStateCallback::OnFrameFinished(Camera &camera, FrameConfig &fc, FrameResult &result) +{ + if (fc.GetFrameConfigType() == FRAME_CONFIG_CAPTURE) { + cout << "Capture frame received." << endl; + list surfaceList = fc.GetSurfaces(); + + for (Surface *surface : surfaceList) { + SurfaceBuffer *buffer = surface->AcquireBuffer(); + if (buffer != nullptr) { + char *virtAddr = static_cast(buffer->GetVirAddr()); + if (virtAddr != nullptr) { + SampleSaveCapture(virtAddr, buffer->GetSize(), gPhotoType_, timeStamp_, sizeof(timeStamp_)); + } + surface->ReleaseBuffer(buffer); + } else { + printf("ERROR:surface buffer is NULL!! \n"); + } + delete surface; + + gIsFinished_ = true; + } + delete &fc; + } else if (fc.GetFrameConfigType() == FRAME_CONFIG_RECORD || fc.GetFrameConfigType() == FRAME_CONFIG_PREVIEW) { + delete &fc; + } +} + +void TestFrameStateCallback::SetPhotoType(int type) +{ + gPhotoType_ = type; + gIsFinished_ = false; +} + +bool TestFrameStateCallback::IsFinish(void) +{ + return gIsFinished_; +} + +void TestFrameStateCallback::GetVideoName(char *pName, int length) +{ + if (strlen(videoName_) <= 0) + return; + if (pName == nullptr || length < strlen(videoName_)) + return; + if (strcpy_s(pName, length, videoName_) != 0) { + return; + } +} + +void TestFrameStateCallback::InitTimeStamp() +{ + struct timeval tv; + struct tm *ltm = nullptr; + gettimeofday(&tv, nullptr); + ltm = localtime(&tv.tv_sec); + if (ltm != nullptr) { + if (sprintf_s(timeStamp_, sizeof(timeStamp_), "%02d-%02d-%02d-%lld", + ltm->tm_hour, ltm->tm_min, ltm->tm_sec, tv.tv_usec / MILLI_SECONDS) < 0) { + return; + } + } +} + +void TestFrameStateCallback::InitVideoName() +{ + if (sprintf_s(videoName_, sizeof(videoName_), "%s/video%s.jpg", THUMB_PATH, timeStamp_) < 0) { + return; + } +} + +// SampleCameraStateMng class +SampleCameraStateMng::~SampleCameraStateMng() +{ + if (recorder_ != nullptr) { + if (gRecordSta_ != MEDIA_STATE_IDLE) { + recorder_->Stop(false); + } + recorder_->Release(); + delete recorder_; + } + if (gRecFd_ >= 0) + close (gRecFd_); + if (fc_) { + delete fc_; + fc_ = nullptr; + } +} + +void SampleCameraStateMng::OnCreated(Camera &c) +{ + cout << "Sample recv OnCreate camera." << endl; + auto config = CameraConfig::CreateCameraConfig(); + if (config == nullptr) { + cout << "New object failed." << endl; + return; + } + config->SetFrameStateCallback(&fsCb_, &eventHdlr_); + c.Configure(*config); + cam_ = &c; +} + +void SampleCameraStateMng::StartRecord(Surface *mSurface) +{ + int ret = 0; + if (gRecordSta_ == MEDIA_STATE_START) { + return; + } + if (recorder_ == nullptr) { + recorder_ = SampleCreateRecorder(IMAGE_WIDTH, IMAGE_HEIGHT); + } + if (recorder_ == nullptr) { + return; + } + if (gRecFd_ > 0) { + close(gRecFd_); + gRecFd_ = -1; + } + + char vName[MAX_NAME_LEN] = {0}; + fsCb_.GetVideoName(vName, sizeof(vName)); + gRecFd_ = CameraGetRecordFd(static_cast(vName)); + if (gRecFd_ < 0) { + return; + } + + if (gRecordSta_ == MEDIA_STATE_PAUSE) { + recorder_->SetNextOutputFile(gRecFd_); + recorder_->Resume(); + } else { + ret = recorder_->SetOutputFile(gRecFd_); + if (ret != SUCCESS) { + return; + } + if (recorder_->Prepare() != SUCCESS || recorder_->Start() != SUCCESS) { + return; + } + } + + Surface *surface = (recorder_->GetSurface(0)).get(); + if (surface == nullptr) { + return; + } + + surface->SetWidthAndHeight(IMAGE_WIDTH, IMAGE_HEIGHT); + surface->SetQueueSize(3); /* queueSize is 3 */ + surface->SetSize(PAGE_SIZE * PAGE_SIZE); + + FrameConfig *fc = new FrameConfig(FRAME_CONFIG_RECORD); + fc->AddSurface(*surface); + ret = cam_->TriggerLoopingCapture(*fc); + if (ret != 0) { + delete fc; + return; + } + gRecordSta_ = MEDIA_STATE_START; +} + +void SampleCameraStateMng::StartPreview(Surface *surface) +{ + printf("enter StartPreview ##################################### \n"); + if (cam_ == nullptr) { + cout << "Camera is not ready." << endl; + return; + } + if (gPreviewSta_ == MEDIA_STATE_START) { + cout << "Camera is already previewing." << endl; + return; + } + if (surface == nullptr) { + cout << "surface is NULL." << endl; + return; + } + + FrameConfig *fc = new FrameConfig(FRAME_CONFIG_PREVIEW); + fc->AddSurface(*surface); + int32_t ret = cam_->TriggerLoopingCapture(*fc); + if (ret != 0) { + delete fc; + cout << "camera start preview failed. ret=" << ret << endl; + return; + } + + gPreviewSta_ = MEDIA_STATE_START; + cout << "camera start preview succeed." << endl; +} + +void SampleCameraStateMng::Capture(int type) +{ + printf("camera start Capture ##################################### \n"); + if (cam_ == nullptr) { + cout << "Camera is not ready." << endl; + return; + } + + fsCb_.SetPhotoType(type); + fsCb_.InitTimeStamp(); + fsCb_.InitVideoName(); + + FrameConfig *fc = new FrameConfig(FRAME_CONFIG_CAPTURE); + Surface *surface = Surface::CreateSurface(); + if (surface == nullptr) { + delete fc; + return; + } + + surface->SetWidthAndHeight(IMAGE_WIDTH, IMAGE_HEIGHT); + fc->AddSurface(*surface); + printf("Capture before TriggerSingleCapture. ##################################### \n"); + cam_->TriggerSingleCapture(*fc); + printf("camera start Capture over. ##################################### \n"); +} + +bool SampleCameraStateMng::IsCaptureOver(void) +{ + return fsCb_.IsFinish(); +} + +void SampleCameraStateMng::SetPause() +{ + +} + +void SampleCameraStateMng::SetResume(Surface *mSurface) +{ + +} + +void SampleCameraStateMng::SetStop(int s) +{ + if (cam_ == nullptr) { + cout << "Camera is not ready." << endl; + return; + } + + cam_->StopLoopingCapture(); + if (gRecordSta_ == MEDIA_STATE_START) { + if (s) { + recorder_->Stop(false); + gRecordSta_ = MEDIA_STATE_IDLE; + } else { + recorder_->Pause(); + gRecordSta_ = MEDIA_STATE_PAUSE; + } + } + + gPreviewSta_ = MEDIA_STATE_IDLE; +} + +bool SampleCameraStateMng::RecordState() +{ + return (gRecordSta_ == MEDIA_STATE_START); +} + +bool SampleCameraStateMng::CameraIsReady() +{ + return (cam_ == nullptr) ? false : true; +} + +// SampleCameraManager class +SampleCameraManager::~SampleCameraManager() +{ + if (CamStateMng) { + CamStateMng->SetStop(1); + delete CamStateMng; + CamStateMng = NULL; + } +} + +int SampleCameraManager::SampleCameraCreate(int picMode) +{ + int retval = 0; + int timeout = 0; + printf("camera start init!!! \n"); + camKit = CameraKit::GetInstance(); + if (camKit == nullptr) { + cout << "Can not get CameraKit instance" << endl; + return -1; + } + + list camList = camKit->GetCameraIds(); + for (auto &cam : camList) { + cout << "camera name:" << cam << endl; + const CameraAbility *ability = camKit->GetCameraAbility(cam); + /* find camera which fits user's ability */ + list sizeList = ability->GetSupportedSizes(0); + if (find(sizeList.begin(), sizeList.end(), CAM_PIC_1080P) != sizeList.end()) { + camId = cam; + break; + } + } + + if (camId.empty()) { + cout << "No available camera.(1080p wanted)" << endl; + printf("No available camera.(1080p wanted)####################### \n"); + return -1; + } + + CamStateMng = new SampleCameraStateMng(eventHdlr_); + if (CamStateMng == NULL) { + printf("create SampleCameraStateMng failed! \n"); + return -1; + } + + printf("before CreateCamera \n"); + camKit->CreateCamera(camId, *CamStateMng, eventHdlr_); + printf("after CreateCamera \n"); + if (!access("/userdata/", F_OK | R_OK | W_OK)) { + if (access(PHOTO_PATH, F_OK)) { + mkdir(PHOTO_PATH, FILE_MODE); + } + if (access(THUMB_PATH, F_OK)) { + mkdir(THUMB_PATH, FILE_MODE); + } + if (access(VIDEO_PATH, F_OK)) { + mkdir(VIDEO_PATH, FILE_MODE); + } + } + printf("camera init ok! \n"); + return retval; +} + +bool SampleCameraManager::SampleCameraExist(void) +{ + return camId.empty() ? false : true; +} + +int SampleCameraManager::SampleCameraStart(Surface *surface) +{ + if (CamStateMng == NULL) + return -1; + CamStateMng->StartPreview(surface); + + return 0; +} + +int SampleCameraManager::SampleCameraStop(void) +{ + if (CamStateMng == nullptr) + return -1; + + CamStateMng->SetStop(1); + + return 0; +} + +int SampleCameraManager::SampleCameraCaptrue(int type) +{ + if (CamStateMng == NULL) + return -1; + + CamStateMng->Capture(type); + + return 0; +} + +int SampleCameraManager::SampleCameraStartRecord(Surface *surface) +{ + if (CamStateMng == NULL) + return -1; + CamStateMng->StartRecord(surface); + + return 0; +} + +int SampleCameraManager::SampleCameraPauseRecord(void) +{ + if (CamStateMng == NULL) + return -1; + CamStateMng->SetPause(); + + return 0; +} + +int SampleCameraManager::SampleCameraResumeRecord(Surface *mSurface) +{ + if (CamStateMng == NULL) + return -1; + CamStateMng->SetResume(mSurface); + + return 0; +} + +int SampleCameraManager::SampleCameraStopRecord(void) +{ + if (CamStateMng == NULL) + return -1; + CamStateMng->SetStop(0); + + return 0; +} +bool SampleCameraManager::SampleCameraGetRecord(void) +{ + if (CamStateMng == NULL) + return false; + return CamStateMng->RecordState(); +} + +bool SampleCameraManager::SampleCameraCaptrueIsFinish(void) +{ + return CamStateMng->IsCaptureOver(); +} + +bool SampleCameraManager::SampleCameraIsReady(void) +{ + return CamStateMng->CameraIsReady(); +} diff --git a/cameraApp/cameraApp/src/main/cpp/camera_manager.h b/cameraApp/cameraApp/src/main/cpp/camera_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..b21d85690d7b725f9f45b9bde7569f13c04029d9 --- /dev/null +++ b/cameraApp/cameraApp/src/main/cpp/camera_manager.h @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2020 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. +*/ + +#ifndef __CAMERA_MANAGER_H__ +#define __CAMERA_MANAGER_H__ + +#include +#include +#include +#include +#include +#include +#include + +#include "camera_kit.h" +#include "recorder.h" + +using namespace std; +using namespace OHOS; +using namespace OHOS::Media; + +typedef enum { + PHOTO_TYPE_NORMAL, + PHOTO_TYPE_VIDEO, + PHOTO_TYPE_TMP, + + PHOTO_TYPE_NBR +}PHOTO_TYPE; + +typedef enum { + MEDIA_STATE_IDLE = 0, + MEDIA_STATE_START, + MEDIA_STATE_PAUSE, + MEDIA_STATE_STOP +} CAMERA_MEDIA_STATUS; + +class TestFrameStateCallback : public FrameStateCallback { +public: + TestFrameStateCallback(){} + ~TestFrameStateCallback(){} + + void OnFrameFinished(Camera &camera, FrameConfig &fc, FrameResult &result) override; + void SetPhotoType(int type); + bool IsFinish(void); + void GetVideoName(char *pName, int length); + void InitVideoName(); + void InitTimeStamp(); +private: + int gPhotoType_; + bool gIsFinished_; + char videoName_[256]; + char timeStamp_[256]; +}; + +class SampleCameraStateMng : public CameraStateCallback { +public: + SampleCameraStateMng() = delete; + SampleCameraStateMng(EventHandler &eventHdlr) : eventHdlr_(eventHdlr) {} + ~SampleCameraStateMng(); + + void OnCreated(Camera &c) override; + void OnCreateFailed(const std::string cameraId, int32_t errorCode) override {} + void OnReleased(Camera &c) override {} + void StartRecord(Surface *mSurface); + void StartPreview(Surface *surface); + void Capture(int type); + void SetPause(); + void SetResume(Surface *mSurface); + void SetStop(int s); + bool RecordState(); + bool CameraIsReady(); + bool IsCaptureOver(void); +private: + + int gRecordSta_ = 0; + int gPreviewSta_ = 0; + int gRecFd_ = -1; + EventHandler &eventHdlr_; + Camera *cam_ = nullptr; + Recorder *recorder_ = nullptr; + TestFrameStateCallback fsCb_; + FrameConfig *fc_ = nullptr; +}; + +class SampleCameraManager { +public: + SampleCameraManager() = delete; + SampleCameraManager(int mode) : picMode(mode) {} + ~SampleCameraManager(); + + int SampleCameraCreate(int picMode); + bool SampleCameraExist(void); + int SampleCameraStart(Surface *surface); + int SampleCameraStop(void); + int SampleCameraCaptrue(int type); + int SampleCameraStartRecord(Surface *surface); + int SampleCameraPauseRecord(void); + int SampleCameraResumeRecord(Surface *mSurface); + int SampleCameraStopRecord(void); + bool SampleCameraGetRecord(void); + bool SampleCameraIsReady(void); + bool SampleCameraCaptrueIsFinish(void); +private: + CameraKit *camKit; + string camId; + SampleCameraStateMng *CamStateMng; + int picMode = 0; + EventHandler eventHdlr_; +}; + +#endif /* __CAMERA_MANAGER_H__ */ + diff --git a/cameraApp/cameraApp/src/main/cpp/event_listener.h b/cameraApp/cameraApp/src/main/cpp/event_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..1249046864a51b1fd6aab1dbc406dd30b4da139e --- /dev/null +++ b/cameraApp/cameraApp/src/main/cpp/event_listener.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2020 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. +*/ + +#ifndef OHOS_EVENT_LISTENER_H +#define OHOS_EVENT_LISTENER_H + +#include +#include +#include + +namespace OHOS { +using OnEventFunc = std::function; + +class EventListener : +public UIView::OnClickListener, +public UIView::OnLongPressListener { +public: + EventListener() = delete; + ~EventListener() override = default; + + EventListener(OnEventFunc onClick, OnEventFunc onLongPress) + { + onClick_ = std::move(onClick); + onLongPress_ = std::move(onLongPress); + } + + bool OnClick(UIView &view, const ClickEvent &event) override + { + if (!onClick_) { + return false; + } + return onClick_(view, event); + } + + bool OnLongPress(UIView &view, const LongPressEvent &event) override + { + if (!onLongPress_) { + return false; + } + return onLongPress_(view, event); + } + +private: + OnEventFunc onClick_ {}; + OnEventFunc onLongPress_ {}; +}; +} +#endif // OHOS_EVENT_LISTENER_H diff --git a/cameraApp/cameraApp/src/main/cpp/ui_config.h b/cameraApp/cameraApp/src/main/cpp/ui_config.h new file mode 100644 index 0000000000000000000000000000000000000000..188ba27b68fc2bd53c5e5785c759cd040c0d5845 --- /dev/null +++ b/cameraApp/cameraApp/src/main/cpp/ui_config.h @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef __UI_CONFIG_H__ +#define __UI_CONFIG_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#define LCD_CH_55 0 +#define LCD_CH_70 1 +#define LCD_CH_SEL LCD_CH_55 + +#define LCD_MODE_HORIZONTAL 0 +#define LCD_MODE_VERTICAL 1 +#define LCD_MODE_SELECT LCD_MODE_HORIZONTAL + +#if (LCD_CH_SEL==LCD_CH_55) +#if (LCD_MODE_SELECT==LCD_MODE_HORIZONTAL) +#define SCREEN_WIDTH 960 +#define SCREEN_HEIGHT 480 +#else +#define SCREEN_WIDTH 480 +#define SCREEN_HEIGHT 960 +#endif +#elif (LCD_CH_SEL==LCD_CH_70) +#if (LCD_MODE_SELECT==LCD_MODE_HORIZONTAL) +#define SCREEN_WIDTH 1024 +#define SCREEN_HEIGHT 600 +#else +#define SCREEN_WIDTH 600 +#define SCREEN_HEIGHT 1024 +#endif +#else +#error "no such type LCD SIZE select!!!" +// exit (1) +#endif + +#define START_X 0 +#define START_Y 0 +#define BUTTON_ICON_SW 60 +#define BUTTON_ICON_SH 60 +#define BUTTON_ICON_BW 114 +#define BUTTON_ICON_BH 114 + +#define TITLE_HEIGHT 80 + +#define ICON_B_WIDTH 50 +#define ICON_B_HEIGHT 50 +#define ICON_M_WIDTH 36 +#define ICON_M_HEIGHT 36 +#define ICON_S_WIDTH 12 +#define ICON_S_HEIGHT 12 + +// coord define +#define V_GROUP_X START_X +#define V_GROUP_Y START_Y +#define V_GROUP_W SCREEN_WIDTH +#define V_GROUP_H SCREEN_HEIGHT + +#if (LCD_MODE_SELECT==LCD_MODE_HORIZONTAL) +#define BACK_LABEL_X (START_X + 30) +#define BACK_LABEL_Y ((TITLE_HEIGHT - ICON_M_HEIGHT) / 2) +#define BACK_LABEL_W ICON_M_WIDTH +#define BACK_LABEL_H ICON_M_HEIGHT + +#define TXT_LABEL_X (BACK_LABEL_X + BUTTON_ICON_SW) +#define TXT_LABEL_Y (START_Y + (TITLE_HEIGHT - ICON_B_HEIGHT) / 2) +#define TXT_LABEL_W (ICON_B_WIDTH * 3) +#define TXT_LABEL_H ICON_B_HEIGHT + +#define RECORD_IMAGE_X (SCREEN_WIDTH / 2 - BUTTON_ICON_SW) +#define RECORD_IMAGE_Y (START_Y + (TITLE_HEIGHT - ICON_S_HEIGHT) / 2) +#define RECORD_IMAGE_W ICON_S_WIDTH +#define RECORD_IMAGE_H ICON_S_HEIGHT + +#define TIME_LABEL_X (RECORD_IMAGE_X + RECORD_IMAGE_W + 14) +#define TIME_LABEL_Y (START_Y + (TITLE_HEIGHT - ICON_B_HEIGHT) / 2) +#define TIME_LABEL_W (SCREEN_WIDTH-(RECORD_IMAGE_X + RECORD_IMAGE_W + 14)) +#define TIME_LABEL_H ICON_B_HEIGHT + +#define SCROLL_VIEW_X START_X +#define SCROLL_VIEW_Y ((SCREEN_HEIGHT - BUTTON_ICON_BH) - 30) +#define SCROLL_VIEW_W SCREEN_WIDTH +#define SCROLL_VIEW_H (BUTTON_ICON_BH + 6) + +#define LEFT_BUTTON_X (SCREEN_WIDTH / 3) +#define LEFT_BUTTON_Y ((SCROLL_VIEW_H - BUTTON_ICON_SH) / 2) +#define LEFT_BUTTON_W BUTTON_ICON_SW +#define LEFT_BUTTON_H BUTTON_ICON_SH +#define MID_BUTTON_X (((SCREEN_WIDTH / 3) - BUTTON_ICON_BW) / 2 + 1 * (SCREEN_WIDTH / 3)) +#define MID_BUTTON_Y ((SCROLL_VIEW_H - BUTTON_ICON_BH) / 2) +#define MID_BUTTON_W BUTTON_ICON_BW +#define MID_BUTTON_H BUTTON_ICON_BH +#define RIGHT_BUTTON_X (2 * (SCREEN_WIDTH / 3) - BUTTON_ICON_SW) +#define RIGHT_BUTTON_Y ((SCROLL_VIEW_H - BUTTON_ICON_SH) / 2) +#define RIGHT_BUTTON_W BUTTON_ICON_SW +#define RIGHT_BUTTON_H BUTTON_ICON_SH + +#else +#define BACK_LABEL_X (START_X + 30) +#define BACK_LABEL_Y ((BUTTON_ICON_SH - ICON_M_HEIGHT) / 2) +#define BACK_LABEL_W ICON_M_WIDTH +#define BACK_LABEL_H ICON_M_HEIGHT + +#define TXT_LABEL_X (BACK_LABEL_X + BUTTON_ICON_SW) +#define TXT_LABEL_Y (START_Y + (BUTTON_ICON_SH - ICON_B_HEIGHT) / 2) +#define TXT_LABEL_W (ICON_B_WIDTH * 3) +#define TXT_LABEL_H ICON_B_HEIGHT + +#define RECORD_IMAGE_X (SCREEN_WIDTH / 2 - BUTTON_ICON_SW) +#define RECORD_IMAGE_Y (START_Y + (BUTTON_ICON_SH - ICON_S_HEIGHT) / 2) +#define RECORD_IMAGE_W ICON_S_WIDTH +#define RECORD_IMAGE_H ICON_S_HEIGHT + +#define TIME_LABEL_X (RECORD_IMAGE_X + RECORD_IMAGE_W + 14) +#define TIME_LABEL_Y (START_Y + (BUTTON_ICON_SH - ICON_B_HEIGHT) / 2) +#define TIME_LABEL_W (BUTTON_ICON_SW * 2) +#define TIME_LABEL_H ICON_B_HEIGHT + +#define SCROLL_VIEW_X START_X +#define SCROLL_VIEW_Y ((SCREEN_HEIGHT - BUTTON_ICON_BH) - 10) +#define SCROLL_VIEW_W SCREEN_WIDTH +#define SCROLL_VIEW_H (BUTTON_ICON_BH + 6) + +#define LEFT_BUTTON_X (SCREEN_WIDTH / 3) +#define LEFT_BUTTON_Y ((SCROLL_VIEW_H - BUTTON_ICON_SH) / 2) +#define LEFT_BUTTON_W BUTTON_ICON_SW +#define LEFT_BUTTON_H BUTTON_ICON_SH +#define MID_BUTTON_X (((SCREEN_WIDTH / 3) - BUTTON_ICON_BW) / 2 + 1 * (SCREEN_WIDTH / 3)) +#define MID_BUTTON_Y ((SCROLL_VIEW_H - BUTTON_ICON_SH) / 2) +#define MID_BUTTON_W BUTTON_ICON_SW +#define MID_BUTTON_H BUTTON_ICON_SH +#define RIGHT_BUTTON_X (2 * (SCREEN_WIDTH / 3) - BUTTON_ICON_SW) +#define RIGHT_BUTTON_Y ((SCROLL_VIEW_H - BUTTON_ICON_SH) / 2) +#define RIGHT_BUTTON_W BUTTON_ICON_SW +#define RIGHT_BUTTON_H BUTTON_ICON_SH +#endif + +#define UI_IMAGE_PATH \ + "/storage/app/run/com.huawei.camera/cameraApp/assets/cameraApp/resources/base/media/" +#define TTF_PATH "SourceHanSansSC-Regular.otf" + +#define PHOTO_PATH "/userdata/photo/" +#define VIDEO_PATH "/userdata/video/" +#define THUMB_PATH "/userdata/thumb/" + +#define MAX_NAME_LEN 256 +#define PAGE_SIZE 1024 + +#define IMAGE_WIDTH 1920 +#define IMAGE_HEIGHT 1080 + +#define SAMPLE_RATE 48000 +#define CHANNEL_COUNT 1 +#define AUDIO_ENCODING_BITRATE SAMPLE_RATE +#define FRAME_RATE 30 +#define BIT_RATE 4096 + +#define FILE_MODE 777 + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/cameraApp/cameraApp/src/main/resources/base/media/camera.png b/cameraApp/cameraApp/src/main/resources/base/media/camera.png new file mode 100644 index 0000000000000000000000000000000000000000..b8ad44638e90f42ab8def211241d624bfe90dfcb Binary files /dev/null and b/cameraApp/cameraApp/src/main/resources/base/media/camera.png differ diff --git a/cameraApp/cameraApp/src/main/resources/base/media/ic_back.png b/cameraApp/cameraApp/src/main/resources/base/media/ic_back.png new file mode 100644 index 0000000000000000000000000000000000000000..8a16a4e501fdac2880e8c0646b03dec1ae7f6a2b Binary files /dev/null and b/cameraApp/cameraApp/src/main/resources/base/media/ic_back.png differ diff --git a/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_photo.png b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_photo.png new file mode 100644 index 0000000000000000000000000000000000000000..6746636acb693ead080d845ad8dcdaa268744b1e Binary files /dev/null and b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_photo.png differ diff --git a/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record.png b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record.png new file mode 100644 index 0000000000000000000000000000000000000000..f55834d8d33f0b592b1cf70e41b12549fe044a22 Binary files /dev/null and b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record.png differ diff --git a/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_camra.png b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_camra.png new file mode 100644 index 0000000000000000000000000000000000000000..d21af50a235364c5165ddda803affe4f4b3710ff Binary files /dev/null and b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_camra.png differ diff --git a/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_continue.png b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_continue.png new file mode 100644 index 0000000000000000000000000000000000000000..576b33db1f35bfda2830641a1788fbe63533f77c Binary files /dev/null and b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_continue.png differ diff --git a/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_pause.png b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_pause.png new file mode 100644 index 0000000000000000000000000000000000000000..7e10f05b18cccdbb858cbaf472e9db92119f5f20 Binary files /dev/null and b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_pause.png differ diff --git a/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_shutter.png b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_shutter.png new file mode 100644 index 0000000000000000000000000000000000000000..c40c8103bdac8cd8be01a5da43555d24caa65643 Binary files /dev/null and b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_shutter.png differ diff --git a/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_stop.png b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_stop.png new file mode 100644 index 0000000000000000000000000000000000000000..1f21c84443d0302abfc6c2896c1ade08398d94a0 Binary files /dev/null and b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_record_stop.png differ diff --git a/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_shutter.png b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_shutter.png new file mode 100644 index 0000000000000000000000000000000000000000..193bea970984cb59f15d810dc49b458b1cfd165d Binary files /dev/null and b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_shutter.png differ diff --git a/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_video.png b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_video.png new file mode 100644 index 0000000000000000000000000000000000000000..44c5a1ce0fbe1b644c92f42198a53b4ad8a3e1c8 Binary files /dev/null and b/cameraApp/cameraApp/src/main/resources/base/media/ic_camera_video.png differ diff --git a/cameraApp/cameraApp/src/main/resources/base/media/ic_timer.png b/cameraApp/cameraApp/src/main/resources/base/media/ic_timer.png new file mode 100644 index 0000000000000000000000000000000000000000..03041ca48828aeefaca1aa85a45e3aa5be50cbc1 Binary files /dev/null and b/cameraApp/cameraApp/src/main/resources/base/media/ic_timer.png differ diff --git a/cameraApp/cert/camera_HarmonyAppProvision_Release.p7b b/cameraApp/cert/camera_HarmonyAppProvision_Release.p7b new file mode 100644 index 0000000000000000000000000000000000000000..1a43946b6189153b6a0ddd015ae44acf956ae610 Binary files /dev/null and b/cameraApp/cert/camera_HarmonyAppProvision_Release.p7b differ diff --git a/gallery/BUILD.gn b/gallery/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..3c7e28c3610265bec853a385847f7dd31336ec62 --- /dev/null +++ b/gallery/BUILD.gn @@ -0,0 +1,89 @@ +# Copyright (c) 2020 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. + +import("//build/lite/config/component/lite_component.gni") +import("//build/lite/config/hap_pack.gni") + +shared_library("gallery") { + sources = [ + "src/gallery_ability.cpp", + "src/gallery_ability_slice.cpp", + "src/picture_ability_slice.cpp", + "src/player_ability_slice.cpp", + ] + + include_dirs = [ + "include", + "//foundation/aafwk/interfaces/kits/ability_lite", + "//foundation/graphic/lite/interfaces/kits/ui/components", + "//foundation/graphic/lite/interfaces/kits/ui", + "//foundation/graphic/lite/interfaces/kits/utils", + "//foundation/graphic/lite/interfaces/kits/config", + "//foundation/appexecfwk/interfaces/kits/bundle_lite", + "//foundation/aafwk/interfaces/kits/want_lite", + "//foundation/multimedia/interfaces/kits/player_lite", + "//base/startup/interfaces/kits/syspara_lite" + ] + + deps = [ + "//foundation/aafwk/frameworks/ability_lite:aafwk_abilitykit_lite", + "//foundation/appexecfwk/frameworks/bundle_lite:bundle", + "//foundation/communication/frameworks/ipc_lite:liteipc_adapter", + "//foundation/distributedschedule/services/samgr_lite/samgr:samgr", + "//utils/native/lite/kv_store:kv_store", + "//foundation/graphic/lite/frameworks/ui:ui", + "//foundation/graphic/lite/frameworks/surface", + "//foundation/graphic/lite/utils:graphic_utils", + "//foundation/multimedia/frameworks/recorder_lite:recorder", + "//foundation/multimedia/frameworks/player_lite:player" + ] + + ldflags = [ + "-shared", + ] + + ldflags += ["-lstdc++"] + ldflags += ["-lpthread"] + ldflags += ["-L$ohos_root_path/sysroot/usr/lib"] + ldflags += ["-Wl,-rpath-link=$ohos_root_path/sysroot/usr/lib"] + ldflags += ["-lui", + "-lsurface", + "-lplayer", + "-lability" + ] + + defines = [ + "OHOS_APPEXECFWK_BMS_BUNDLEMANAGER", + "ENABLE_WINDOW=1", + "ABILITY_WINDOW_SUPPORT" + ] +} + +lite_component("gallery_component") { + + features = [ + ":gallery", + ] +} + +hap_pack("gallery_hap") +{ + deps = [":gallery"] + mode = "hap" + json_path = "config.json" + ability_so_path = "$root_out_dir/libgallery.so" + force = "true" + cert_profile = "cert/gallery_HarmonyAppProvision_Release.p7b" + resources_path = "resources" + hap_name = "gallery" +} diff --git a/gallery/cert/gallery_HarmonyAppProvision_Release.p7b b/gallery/cert/gallery_HarmonyAppProvision_Release.p7b new file mode 100644 index 0000000000000000000000000000000000000000..e324c96264d7fea2450495fffc2a77054b6f64dc Binary files /dev/null and b/gallery/cert/gallery_HarmonyAppProvision_Release.p7b differ diff --git a/gallery/config.json b/gallery/config.json new file mode 100644 index 0000000000000000000000000000000000000000..53c68e33fe25d7c7ac1d8e6ccf9d6b52c7804cf2 --- /dev/null +++ b/gallery/config.json @@ -0,0 +1,99 @@ +{ + "app": { + "bundleName": "com.huawei.gallery", + "vendor": "huawei", + "version": { + "code": 1, + "name": "1.0" + }, + "apiVersion": { + "compatible": 3, + "target": 4 + } + }, + "deviceConfig": { + "default": { + } + }, + "module": { + "package": "com.huawei.gallery", + "name": ".MyHarmonyAbilityPackage", + "deviceType": [ + "phone", + "tv", + "tablet", + "pc", + "car", + "smartWatch", + "sportsWatch", + "smartVision" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "gallery", + "moduleType": "entry" + }, + "abilities": [ + { + "name": "GalleryAbility", + "icon": "assets/gallery/resources/drawable/com.huawei.photos.png", + "label": "test app 1", + "launchType": "standard", + "type": "page", + "visible": true + } + ], + "reqPermissions": [ + { + "name": "ohos.permission.MODIFY_AUDIO_SETTINGS", + "reason": "SYSTEM_GRANT", + "usedScene": { + "ability": [ + ".FormAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.READ_MEDIA", + "reason": "USER_GRANT", + "usedScene": { + "ability": [ + ".FormAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.READ_MEDIA_AUDIO", + "reason": "USER_GRANT", + "usedScene": { + "ability": [ + ".FormAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.READ_MEDIA_IMAGES", + "reason": "USER_GRANT", + "usedScene": { + "ability": [ + ".FormAbility" + ], + "when": "inuse" + } + }, + { + "name": "ohos.permission.READ_MEDIA_VIDEO", + "reason": "USER_GRANT", + "usedScene": { + "ability": [ + ".FormAbility" + ], + "when": "inuse" + } + } + ] + } +} \ No newline at end of file diff --git a/gallery/include/event_listener.h b/gallery/include/event_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..2c4403f7ca4de2afe26b6338a3db809511f3db61 --- /dev/null +++ b/gallery/include/event_listener.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_EVENT_LISTENER_H +#define OHOS_EVENT_LISTENER_H + +#include +#include +#include + +namespace OHOS { +using OnEventFunc = std::function; + +class EventListener : + public UIView::OnClickListener, + public UIView::OnLongPressListener { +public: + EventListener() = delete; + ~EventListener() override = default; + + EventListener(OnEventFunc onClick, OnEventFunc onLongPress) + { + onClick_ = std::move(onClick); + onLongPress_ = std::move(onLongPress); + } + + bool OnClick(UIView &view, const ClickEvent &event) override + { + if (!onClick_) { + return false; + } + return onClick_(view, event); + } + + bool OnLongPress(UIView &view, const LongPressEvent &event) override + { + if (!onLongPress_) { + return false; + } + return onLongPress_(view, event); + } + +private: + OnEventFunc onClick_ {}; + OnEventFunc onLongPress_ {}; +}; +} +#endif // OHOS_EVENT_LISTENER_H \ No newline at end of file diff --git a/gallery/include/gallery_ability.h b/gallery/include/gallery_ability.h new file mode 100644 index 0000000000000000000000000000000000000000..2db4348eeb58e70739a97208e242ea5c06a74ca9 --- /dev/null +++ b/gallery/include/gallery_ability.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_GALLERY_ABILITY_H +#define OHOS_GALLERY_ABILITY_H + +#include + +namespace OHOS { +class GalleryAbility : public Ability { +protected: + void OnStart(const Want &want) override; + void OnInactive() override; + void OnActive(const Want &want) override; + void OnBackground() override; + void OnStop() override; +}; +} + +#endif // OHOS_GALLERY_ABILITY_H \ No newline at end of file diff --git a/gallery/include/gallery_ability_slice.h b/gallery/include/gallery_ability_slice.h new file mode 100644 index 0000000000000000000000000000000000000000..0222f52210ab9c4604499ec1f8901f4cc9e221c3 --- /dev/null +++ b/gallery/include/gallery_ability_slice.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_GALLERY_ABILITY_SLICE_H +#define OHOS_GALLERY_ABILITY_SLICE_H + +#include +#include +#include +#include +#include "event_listener.h" +#include "gallery_config.h" + +namespace OHOS { +class GalleryAbilitySlice : public AbilitySlice { +public: + GalleryAbilitySlice() = default; + ~GalleryAbilitySlice() override; + +protected: + void OnStart(const Want &want) override; + void OnInactive() override; + void OnActive(const Want &want) override; + void OnBackground() override; + void OnStop() override; + +private: + void Clear(); + void ClearThumb(); + void ClearPictureList(const UIView* view); + void InitTitle(); + void InitPictureList(); + void AddAllPictures(const Point& pos, int16_t numInLine); + UIView* CreateImageItem(const Point& pos, const char* imageName, const char* imagePath); + EventListener* GetImageClickListener(const char* path); + void DeleteAllData(); + void DeleteAllFilesInDir(const char* path); + + RootView* rootView_ { nullptr }; + uint16_t pictureCount_ { 0 }; + UIViewGroup* backArea_ { nullptr }; + UIImageView* backIcon_ { nullptr }; + EventListener* backIconListener_ { nullptr }; + EventListener* deleteClickListener_ { nullptr }; + UILabel* titleLabel_ { nullptr }; + UILabel* deleteLabel_ { nullptr }; + UIScrollView* picContainer_ { nullptr }; + UIViewGroup* picList_ { nullptr }; + char* pictureName_[MAX_PICTURE_COUNT] = { nullptr }; + uint16_t pictureOnClickListenerCount_ { 0 }; + EventListener* pictureOnClickListener_[MAX_PICTURE_COUNT] = { nullptr }; +}; +} +#endif // OHOS_GALLERY_ABILITY_SLICE_H \ No newline at end of file diff --git a/gallery/include/gallery_config.h b/gallery/include/gallery_config.h new file mode 100644 index 0000000000000000000000000000000000000000..b4ba13dbe63f875babc592c7c4d737baa60cc5fd --- /dev/null +++ b/gallery/include/gallery_config.h @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_GALLERY_CONFIG_H +#define OHOS_GALLERY_CONFIG_H + +namespace OHOS { +/** icon resource file path */ +static const char* const BACK_ICON_PATH = "/gallery/assets/gallery/resources/drawable/ic_back.png"; +static const char* const VIDEO_TAG_ICON_PATH = "/gallery/assets/gallery/resources/drawable/ic_gallery_video_tag.png"; +static const char* const VIDEO_PALY_PATH = "/gallery/assets/gallery/resources/drawable/ic_gallery_play.png"; +static const char* const VIDEO_PAUSE_PATH = "/gallery/assets/gallery/resources/drawable/ic_gallery_pause.png"; + +static constexpr int16_t MAX_PICTURE_COUNT = 256; +static constexpr uint16_t MAX_PATH_LENGTH = 512; +static char g_backIconAbsolutePath[MAX_PATH_LENGTH] = {0}; +static char g_videoTagIconAbsolutePath[MAX_PATH_LENGTH] = {0}; + +/** thumb, photo, and video folder path */ +static const char* const THUMBNAIL_DIRECTORY = "/userdata/thumb"; +static const char* const PHOTO_DIRECTORY = "/userdata/photo"; +static const char* const VIDEO_SOURCE_DIRECTORY = "/userdata/video"; + +/** general page configuration */ +static constexpr int ROOT_VIEW_POSITION_X = 0; +static constexpr int ROOT_VIEW_POSITION_Y = 0; +static constexpr int ROOT_VIEW_WIDTH = 960; +static constexpr int ROOT_VIEW_HEIGHT = 480; +static constexpr uint16_t ROOT_VIEW_OPACITY = 255; + +static const char* const FONT_NAME = "SourceHanSansSC-Regular.otf"; + +/** back icon 36 x 36 */ +static constexpr int16_t BACK_ICON_POSITION_X = 38; +static constexpr int16_t BACK_ICON_POSITION_Y = 17; + +/** THUMBNAIL */ +static constexpr int16_t THUMBNAIL_RESOLUTION_X = 120; +static constexpr int16_t THUMBNAIL_RESOLUTION_Y = 120; +static constexpr int16_t THUMBNAIL_SPACE = 4; +static constexpr int16_t THUMBNAIL_COLUMN = 3; + +static constexpr int16_t VIDEO_TAG_POSITION_X = 10; +static constexpr int16_t VIDEO_TAG_POSITION_Y = THUMBNAIL_RESOLUTION_Y - 37; + +/** title */ +static constexpr int16_t LABEL_POSITION_X = BACK_ICON_POSITION_X + 60; +static constexpr int16_t LABEL_POSITION_Y = 0; +static constexpr int16_t LABEL_WIDTH = 100; +static constexpr int16_t LABEL_HEIGHT = 70; +static constexpr uint16_t GALLERY_FONT_SIZE = 25; +static constexpr uint16_t GALLERY_DELETE_FONT_SIZE = 22; +static constexpr int16_t DELETE_LABEL_WIDTH = 150; + +/** prefix and File Type */ +static const char* const PHOTO_PREFIX = "photo"; +static const char* const AVAILABEL_SOURCE_TYPE = ".mp4"; +static const char* const AVAILABEL_SOURCE_TYPE_MP4 = ".MP4"; + +/** player */ +static char g_videoPlayAbsolutePath[MAX_PATH_LENGTH] = {0}; +static char g_videoPauseAbsolutePath[MAX_PATH_LENGTH] = {0}; + +/** playback status bar */ +static constexpr uint16_t STATUS_BAR_GROUP_HEIGHT = 96; +static constexpr uint16_t TOGGLE_BUTTON_OFFSET_X = 36; +static constexpr uint16_t TOGGLE_BUTTON_OFFSET_Y = 18; +static constexpr uint16_t TOGGLE_BUTTON_WIDTH = 60; +static constexpr uint16_t TOGGLE_BUTTON_HEIGHT = 60; + +static constexpr uint16_t CURRENT_TIME_LABEL_X = TOGGLE_BUTTON_OFFSET_X + TOGGLE_BUTTON_WIDTH + TOGGLE_BUTTON_OFFSET_Y; +static constexpr uint16_t CURRENT_TIME_LABEL_Y = 0; +static constexpr uint16_t CURRENT_TIME_LABEL_WIDTH = 60; +static constexpr uint16_t CURRENT_TIME_LABEL_HEIGHT = STATUS_BAR_GROUP_HEIGHT; + +static constexpr uint16_t TOTAL_TIME_LABEL_WIDTH = 90; +static constexpr uint16_t TOTAL_TIME_LABEL_HEIGHT = STATUS_BAR_GROUP_HEIGHT; +static constexpr uint16_t TOTAL_TIME_LABEL_X = ROOT_VIEW_WIDTH - TOTAL_TIME_LABEL_WIDTH; +static constexpr uint16_t TOTAL_TIME_LABEL_Y = 0; + +static constexpr uint16_t SLIDER_X = CURRENT_TIME_LABEL_X + CURRENT_TIME_LABEL_WIDTH; +static constexpr uint16_t SLIDER_Y = 2; +static constexpr uint16_t SLIDER_HEIGHT = 3; +static constexpr uint16_t SLIDER_WIDTH = ROOT_VIEW_WIDTH - SLIDER_X - TOTAL_TIME_LABEL_WIDTH - 20; +static constexpr uint16_t KNOB_WIDTH = 25; + +static constexpr uint16_t PLAYER_FONT_SIZE = 18; +} // namespace OHOS +#endif // OHOS_GALLERY_CONFIG_H \ No newline at end of file diff --git a/gallery/include/picture_ability_slice.h b/gallery/include/picture_ability_slice.h new file mode 100644 index 0000000000000000000000000000000000000000..1e63818d4cdcad572f8474bd936494f0a3d743d4 --- /dev/null +++ b/gallery/include/picture_ability_slice.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_PICTURE_ABILITY_SLICE_H +#define OHOS_PICTURE_ABILITY_SLICE_H + +#include +#include "components/ui_image_view.h" +#include "components/ui_view_group.h" +#include "event_listener.h" + +namespace OHOS { +class PictureAbilitySlice : public AbilitySlice { +public: + ~PictureAbilitySlice() override; + +protected: + void OnStart(const Want &want) override; + void OnInactive() override; + void OnActive(const Want &want) override; + void OnBackground() override; + void OnStop() override; + +private: + void Clear(); + void InitTitle(); + void InitPicture(const char* path); + + RootView* rootView_ { nullptr }; + UIViewGroup* backArea_ { nullptr }; + UIImageView* backIcon_ { nullptr }; + EventListener* backIconListener_ { nullptr }; + UIImageView* picture_ { nullptr }; +}; +} + +#endif // OHOS_PICTURE_ABILITY_SLICE_H \ No newline at end of file diff --git a/gallery/include/player_ability_slice.h b/gallery/include/player_ability_slice.h new file mode 100644 index 0000000000000000000000000000000000000000..79981d9f0efd2ca5a12d2b37a224e26f9137a573 --- /dev/null +++ b/gallery/include/player_ability_slice.h @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_PLAYER_ABILITY_SLICE_H +#define OHOS_PLAYER_ABILITY_SLICE_H + +#include "ability_loader.h" +#include "animator/animator.h" +#include "animator/easing_equation.h" +#include "components/root_view.h" +#include "components/ui_label.h" +#include "components/ui_slider.h" +#include "components/ui_surface_view.h" +#include "components/ui_toggle_button.h" +#include "event_listener.h" +#include "gallery_config.h" +#include "player.h" +#include "securec.h" +#include "source.h" + +namespace OHOS { +using OHOS::Media::Player; +using OHOS::Media::Source; +using namespace OHOS::Media; +struct PlayerAdapter { + std::shared_ptr adapter; + int32_t sourceType; + char filePath[MAX_PATH_LENGTH]; +}; + +class ToggleBtnListener : public UIView::OnClickListener { +public: + explicit ToggleBtnListener(UIToggleButton* btn, + PlayerAdapter* sample, + Animator* animator, + UISurfaceView* surfaceView) + : button_(btn), + videoPlayer_(sample), + animator_(animator) {} + + virtual ~ToggleBtnListener() {} + + bool OnClick(UIView& view, const ClickEvent& event) override; + + void SetCompleteFlag(bool state) + { + completeFlag_ = state; + } + +private: + UIToggleButton* button_; + PlayerAdapter* videoPlayer_; + Animator* animator_; + bool completeFlag_ { false }; +}; + +class SliderAnimator : public Animator, public AnimatorCallback { +public: + explicit SliderAnimator(PlayerAdapter *sample, + UISlider *slider, + UILabel *label, + int64_t duration, + UISurfaceView* surfaceView) + : Animator(this, slider, duration, true), + videoPlayer_(sample), + slider_(slider), + timeLabel_(label), + duration_(duration), + surfaceView_(surfaceView), + needRefreshPlayer_(false) {} + + virtual ~SliderAnimator() {} + + void Callback(UIView* view) override; + + void SetToggleButton(UIToggleButton* toggleButton) + { + toggleButton_ = toggleButton; + } + + void SetToggleBtnListener(ToggleBtnListener* listener) + { + listener_ = listener; + } + +private: + PlayerAdapter* videoPlayer_; + UISlider* slider_; + UILabel* timeLabel_; + int64_t duration_; + UISurfaceView* surfaceView_; + UIToggleButton* toggleButton_ { nullptr }; + ToggleBtnListener* listener_ { nullptr }; + bool needRefreshPlayer_; +}; + +class PlayerAbilitySlice : public AbilitySlice { +public: + PlayerAbilitySlice() = default; + ~PlayerAbilitySlice() override; + +protected: + void OnStart(const Want &want) override; + void OnInactive() override; + void OnActive(const Want &want) override; + void OnBackground() override; + void OnStop() override; + +private: + void Clear(); + void ShowErrorTips(); + void SetUpRootView(); + void SetUpBackArea(const char* pathHeader); + void SetUpVideoPlayer(const Want &want); + bool SetUpSurfaceView(); + void SetUpProgress(int64_t duration); + void SetUpAnimatorGroup(const char* pathHeader); + void SetUpToggleButton(const char* pathHeader); + + PlayerAdapter* videoPlayer_ { nullptr }; + SliderAnimator* animator_ { nullptr }; + EventListener* backIconListener_ { nullptr }; + ToggleBtnListener* onClickListener_ { nullptr }; + + RootView* rootView_ { nullptr }; + UIViewGroup* backArea_ { nullptr }; + UIImageView* backIcon_ { nullptr }; + UISurfaceView* surfaceView_ { nullptr }; + UIViewGroup* animatorGroup_ { nullptr }; + UIToggleButton* toggleButton_ { nullptr }; + UIViewGroup* toggleButtonArea_ { nullptr }; + UILabel* currentTimeLabel_ { nullptr }; + UISlider* slider_ { nullptr }; + UILabel* totalTimeLabel_ { nullptr }; + UILabel* errorTips_ { nullptr }; +}; +} // namespace OHOS +#endif // OHOS_PLAYER_ABILITY_SLICE_H \ No newline at end of file diff --git a/gallery/resources/drawable/bg_gradient_mask.png b/gallery/resources/drawable/bg_gradient_mask.png new file mode 100644 index 0000000000000000000000000000000000000000..ad132c18be75169732188981e62190c83c95af85 Binary files /dev/null and b/gallery/resources/drawable/bg_gradient_mask.png differ diff --git a/gallery/resources/drawable/com.huawei.photos.png b/gallery/resources/drawable/com.huawei.photos.png new file mode 100644 index 0000000000000000000000000000000000000000..0b262f62ac697c74b61a1431558464a24b19a4bd Binary files /dev/null and b/gallery/resources/drawable/com.huawei.photos.png differ diff --git a/gallery/resources/drawable/ctrl_progressbar_dot.png b/gallery/resources/drawable/ctrl_progressbar_dot.png new file mode 100644 index 0000000000000000000000000000000000000000..f20694d38a7a19a7cc9579db00e6e2195e85e72c Binary files /dev/null and b/gallery/resources/drawable/ctrl_progressbar_dot.png differ diff --git a/gallery/resources/drawable/ctrl_seekbar_dot.png b/gallery/resources/drawable/ctrl_seekbar_dot.png new file mode 100644 index 0000000000000000000000000000000000000000..d30137c47c3d3333c92fcdf25ee304f1941600f4 Binary files /dev/null and b/gallery/resources/drawable/ctrl_seekbar_dot.png differ diff --git a/gallery/resources/drawable/ic_back.png b/gallery/resources/drawable/ic_back.png new file mode 100644 index 0000000000000000000000000000000000000000..8a16a4e501fdac2880e8c0646b03dec1ae7f6a2b Binary files /dev/null and b/gallery/resources/drawable/ic_back.png differ diff --git a/gallery/resources/drawable/ic_gallery_pause.png b/gallery/resources/drawable/ic_gallery_pause.png new file mode 100644 index 0000000000000000000000000000000000000000..702b32eec497355add4394d0d5deca7c6c157afc Binary files /dev/null and b/gallery/resources/drawable/ic_gallery_pause.png differ diff --git a/gallery/resources/drawable/ic_gallery_play.png b/gallery/resources/drawable/ic_gallery_play.png new file mode 100644 index 0000000000000000000000000000000000000000..640cd97381a0994f53d03b27bb5b59351db49749 Binary files /dev/null and b/gallery/resources/drawable/ic_gallery_play.png differ diff --git a/gallery/resources/drawable/ic_gallery_video_tag.png b/gallery/resources/drawable/ic_gallery_video_tag.png new file mode 100644 index 0000000000000000000000000000000000000000..d079610ec730b608cb7e7dba865b6004cb55d927 Binary files /dev/null and b/gallery/resources/drawable/ic_gallery_video_tag.png differ diff --git a/gallery/src/gallery_ability.cpp b/gallery/src/gallery_ability.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1961b38e1c783b2743c894337332441a913bfcd9 --- /dev/null +++ b/gallery/src/gallery_ability.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "gallery_ability.h" + +namespace OHOS { +REGISTER_AA(GalleryAbility) + +void GalleryAbility::OnStart(const Want &want) +{ + printf("GalleryAbility::OnStart\n"); + SetMainRoute("GalleryAbilitySlice"); + + Ability::OnStart(want); +} + +void GalleryAbility::OnInactive() +{ + printf("GalleryAbility::OnInactive\n"); + Ability::OnInactive(); +} + +void GalleryAbility::OnActive(const Want &want) +{ + printf("GalleryAbility::OnActive\n"); + Ability::OnActive(want); +} + +void GalleryAbility::OnBackground() +{ + printf("GalleryAbility::OnBackground\n"); + Ability::OnBackground(); +} + +void GalleryAbility::OnStop() +{ + printf("GalleryAbility::OnStop\n"); + Ability::OnStop(); +} +} \ No newline at end of file diff --git a/gallery/src/gallery_ability_slice.cpp b/gallery/src/gallery_ability_slice.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e610b3171a5b5750b8428a0c0a9e5fc97446fa5 --- /dev/null +++ b/gallery/src/gallery_ability_slice.cpp @@ -0,0 +1,398 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "gallery_ability_slice.h" +#include "ability_env.h" +#include "ability_manager.h" +#include "picture_ability_slice.h" + +#include "file.h" +#include "securec.h" + +namespace OHOS { +REGISTER_AS(GalleryAbilitySlice) + +GalleryAbilitySlice::~GalleryAbilitySlice() +{ + printf("~GalleryAbilitySlice() | start \n"); + Clear(); + printf("~GalleryAbilitySlice() | end \n"); +} + +void GalleryAbilitySlice::Clear() +{ + printf("GalleryAbilitySlice::Clear() | start \n"); + if (backIcon_ != nullptr) { + delete backIcon_; + backIcon_ = nullptr; + } + if (backIconListener_ != nullptr) { + delete backIconListener_; + backIconListener_ = nullptr; + } + if (deleteClickListener_ != nullptr) { + delete deleteClickListener_; + deleteClickListener_ = nullptr; + } + if (backArea_ != nullptr) { + delete backArea_; + backArea_ = nullptr; + } + if (titleLabel_ != nullptr) { + delete titleLabel_; + titleLabel_ = nullptr; + } + if (deleteLabel_ != nullptr) { + delete deleteLabel_; + deleteLabel_ = nullptr; + } + + ClearThumb(); + + if (rootView_ != nullptr) { + RootView::DestoryWindowRootView(rootView_); + rootView_ = nullptr; + } + printf("GalleryAbilitySlice::Clear() | end \n"); +} + +void GalleryAbilitySlice::ClearThumb() +{ + printf("GalleryAbilitySlice::ClearThumb() | start \n"); + if (picContainer_ != nullptr) { + delete picContainer_; + picContainer_ = nullptr; + } + if (picList_ != nullptr) { + ClearPictureList(picList_); + delete picList_; + picList_ = nullptr; + } + for (uint16_t i = 0; i < pictureCount_; i++) { + if (pictureName_[i] != nullptr) { + delete[] pictureName_[i]; + pictureName_[i] = nullptr; + } + } + for (uint16_t i = 0; i < pictureOnClickListenerCount_; i++) { + if (pictureOnClickListener_[i] != nullptr) { + delete pictureOnClickListener_[i]; + pictureOnClickListener_[i] = nullptr; + } + } + pictureCount_ = 0; + pictureOnClickListenerCount_ = 0; + printf("GalleryAbilitySlice::ClearThumb() | end \n"); +} + +void GalleryAbilitySlice::ClearPictureList(const UIView* view) +{ + printf("GalleryAbilitySlice::ClearPictureList() | start \n"); + if (view == nullptr || !(view->IsViewGroup())) { + return; + } + UIView* child = static_cast(view)->GetChildrenHead(); + UIView* childNext = nullptr; + while (child != nullptr) { + childNext = child->GetNextSibling(); + if (child->IsViewGroup()) { + ClearPictureList(child); + } + delete child; + child = childNext; + } + printf("GalleryAbilitySlice::ClearPictureList() | end \n"); +} + +void GalleryAbilitySlice::InitTitle() +{ + printf("GalleryAbilitySlice::InitTitle | start \n"); + backIcon_ = new UIImageView(); + backIcon_->SetPosition(BACK_ICON_POSITION_X, BACK_ICON_POSITION_Y); + backIcon_->SetSrc(g_backIconAbsolutePath); + backIcon_->SetTouchable(true); + + backArea_ = new UIViewGroup(); + backArea_->SetPosition(0, 0, LABEL_POSITION_X, LABEL_HEIGHT); + backArea_->SetStyle(STYLE_BACKGROUND_OPA, 0); + backArea_->SetTouchable(true); + + auto onClick = [this] (UIView& view, const Event& event) -> bool { + printf("############ Next AS enter #############\n"); + TerminateAbility(); + printf("############ Next AS exit #############\n"); + return true; + }; + backIconListener_ = new EventListener(onClick, nullptr); + backIcon_->SetOnClickListener(backIconListener_); + backArea_->SetOnClickListener(backIconListener_); + + titleLabel_ = new UILabel(); + titleLabel_->SetPosition(LABEL_POSITION_X, LABEL_POSITION_Y, LABEL_WIDTH, LABEL_HEIGHT); + titleLabel_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER); + titleLabel_->SetFont(FONT_NAME, GALLERY_FONT_SIZE); + titleLabel_->SetText("照片"); + + deleteLabel_ = new UILabel(); + deleteLabel_->SetPosition(ROOT_VIEW_WIDTH - DELETE_LABEL_WIDTH, LABEL_POSITION_Y, + DELETE_LABEL_WIDTH, LABEL_HEIGHT); + deleteLabel_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, + UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER); + deleteLabel_->SetFont(FONT_NAME, GALLERY_DELETE_FONT_SIZE); + deleteLabel_->SetText("全部删除"); + deleteLabel_->SetTouchable(true); + auto deleteClick = [this] (UIView& view, const Event& event) -> bool { + printf("############ DeleteAllData click enter #############\n"); + DeleteAllData(); + printf("############ DeleteAllData click exit #############\n"); + return true; + }; + deleteClickListener_ = new EventListener(deleteClick, nullptr); + deleteLabel_->SetOnClickListener(deleteClickListener_); + + backArea_->Add(backIcon_); + rootView_->Add(backArea_); + rootView_->Add(titleLabel_); + rootView_->Add(deleteLabel_); +} + +void GalleryAbilitySlice::InitPictureList() +{ + printf("GalleryAbilitySlice::InitPictureList | start \n"); + picContainer_ = new UIScrollView(); + picContainer_->SetPosition(0, LABEL_POSITION_Y + LABEL_HEIGHT); + picContainer_->Resize(ROOT_VIEW_WIDTH, (THUMBNAIL_RESOLUTION_Y + THUMBNAIL_SPACE) * THUMBNAIL_COLUMN); + picContainer_->SetStyle(STYLE_BACKGROUND_OPA, 0); + rootView_->Add(picContainer_); + + picList_ = new UIViewGroup(); + picList_->SetPosition(0, 0, ROOT_VIEW_WIDTH, ROOT_VIEW_HEIGHT); + picList_->SetStyle(STYLE_BACKGROUND_OPA, 0); + + int16_t numInLine = (ROOT_VIEW_WIDTH + THUMBNAIL_SPACE) / (THUMBNAIL_RESOLUTION_X + THUMBNAIL_SPACE); + int16_t offset = ((ROOT_VIEW_WIDTH + THUMBNAIL_SPACE) % (THUMBNAIL_RESOLUTION_X + THUMBNAIL_SPACE)) / 2; // 2: half + AddAllPictures(Point { offset, 0 }, numInLine); + + int16_t totalHeight = (pictureCount_ / numInLine) * (THUMBNAIL_RESOLUTION_Y + THUMBNAIL_SPACE); + if ((pictureCount_ % numInLine) != 0) { + totalHeight += THUMBNAIL_RESOLUTION_Y + THUMBNAIL_SPACE; + } + picList_->Resize(ROOT_VIEW_WIDTH, totalHeight); + printf("------------ totalHeight : %d ------------", totalHeight); + picContainer_->Add(picList_); +} + +void GalleryAbilitySlice::AddAllPictures(const Point& pos, int16_t numInLine) +{ + printf("GalleryAbilitySlice::AddAllPictures | start | %d\n", numInLine); + Point imagePos = pos; + void* drip = FileOpenDir(THUMBNAIL_DIRECTORY); + if (drip == nullptr) { + return; + } + FileDirentInfo* info = new FileDirentInfo(); + while (FileReadDir(drip, info) >= 0 && pictureCount_ < MAX_PICTURE_COUNT) { + uint16_t imageNameLen = static_cast(strlen(info->name)); + if (imageNameLen > MAX_PATH_LENGTH) { + printf("GalleryAbilitySlice::AddAllPictures | imageNameLen > MAX_PATH_LENGTH | %d\n", imageNameLen); + continue; + } + char* imageName = new char[imageNameLen + 1](); + memcpy_s(imageName, imageNameLen + 1, info->name, imageNameLen + 1); + pictureName_[pictureCount_] = imageName; + pictureCount_++; + + uint16_t pathLen = static_cast(strlen(THUMBNAIL_DIRECTORY)) + imageNameLen + 1; + if (pathLen > MAX_PATH_LENGTH) { + printf("GalleryAbilitySlice::AddAllPictures | pathLen > MAX_PATH_LENGTH | %d\n", pathLen); + continue; + } + char* imagePath = new char[pathLen + 1](); + if (sprintf_s(imagePath, pathLen + 1, "%s/%s", THUMBNAIL_DIRECTORY, info->name) < 0) { + printf("GalleryAbilitySlice::AddAllPictures | sprintf_s error\n"); + delete[] imagePath; + continue; + } + + picList_->Add(CreateImageItem(imagePos, imageName, imagePath)); + delete[] imagePath; + + if ((pictureCount_ % numInLine) == 0) { + imagePos.x = pos.x; + imagePos.y += THUMBNAIL_RESOLUTION_Y + THUMBNAIL_SPACE; + } else { + imagePos.x += THUMBNAIL_RESOLUTION_X + THUMBNAIL_SPACE; + } + } + delete info; + FileCloseDir(drip); +} + +UIView* GalleryAbilitySlice::CreateImageItem(const Point& pos, const char* imageName, const char* imagePath) +{ + UIImageView* imageView = new UIImageView(); + imageView->SetAutoEnable(false); + imageView->Resize(THUMBNAIL_RESOLUTION_X, THUMBNAIL_RESOLUTION_Y); + imageView->SetSrc(imagePath); + pictureOnClickListener_[pictureOnClickListenerCount_] = GetImageClickListener(imageName); + imageView->SetOnClickListener(pictureOnClickListener_[pictureOnClickListenerCount_++]); + imageView->SetTouchable(true); + + if (strncmp(imageName, PHOTO_PREFIX, strlen(PHOTO_PREFIX)) == 0) { + imageView->SetPosition(pos.x, pos.y); + return imageView; + } + imageView->SetPosition(0, 0); + + UIViewGroup* imageItem = new UIViewGroup(); + imageItem->SetStyle(STYLE_BACKGROUND_OPA, 0); + imageItem->SetPosition(pos.x, pos.y, THUMBNAIL_RESOLUTION_X, THUMBNAIL_RESOLUTION_Y); + imageItem->SetTouchable(true); + imageItem->SetOnClickListener(imageView->GetOnClickListener()); + + UIImageView* videoTag = new UIImageView(); + videoTag->SetPosition(VIDEO_TAG_POSITION_X, VIDEO_TAG_POSITION_Y); + videoTag->SetSrc(g_videoTagIconAbsolutePath); + videoTag->SetTouchable(true); + videoTag->SetOnClickListener(imageView->GetOnClickListener()); + + imageItem->Add(imageView); + imageItem->Add(videoTag); + + return imageItem; +} + +EventListener* GalleryAbilitySlice::GetImageClickListener(const char* path) +{ + auto onClick = [this, path] (UIView& view, const Event& event) -> bool { + printf("############ Next AS enter #############\n"); + Want wantData = { nullptr }; + printf("------- imagePath: %s \n", path); + bool ret = SetWantData(&wantData, path, strlen(path) + 1); + if (!ret) { + printf("############ SetWantData error #############\n"); + return ret; + } + AbilitySlice* nextSlice = nullptr; + if (strncmp(path, PHOTO_PREFIX, strlen(PHOTO_PREFIX)) == 0) { + printf("--------- enter PictureAbilitySlice \n"); + nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("PictureAbilitySlice"); + } else { + printf("--------- enter PlayerAbilitySlice \n"); + nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("PlayerAbilitySlice"); + } + if (nextSlice == nullptr) { + printf("undefined nextSlice\n"); + } else { + Present(*nextSlice, wantData); + } + printf("############ Next AS exit #############\n"); + return true; + }; + return new EventListener(onClick, nullptr); +} + +void GalleryAbilitySlice::DeleteAllData() +{ + picContainer_->Invalidate(); + rootView_->Remove(picContainer_); + ClearThumb(); + + DeleteAllFilesInDir(THUMBNAIL_DIRECTORY); + DeleteAllFilesInDir(PHOTO_DIRECTORY); + DeleteAllFilesInDir(VIDEO_SOURCE_DIRECTORY); + + InitPictureList(); +} + +void GalleryAbilitySlice::DeleteAllFilesInDir(const char* path) +{ + void* drip = FileOpenDir(path); + if (drip == nullptr) { + return; + } + FileDirentInfo* info = new FileDirentInfo(); + while (FileReadDir(drip, info) >= 0) { + uint16_t fileNameLen = static_cast(strlen(info->name)); + uint16_t pathLen = static_cast(strlen(path)) + fileNameLen + 1; + if (pathLen > MAX_PATH_LENGTH) { + printf("GalleryAbilitySlice::AddAllPictures | pathLen > MAX_PATH_LENGTH | %d\n", pathLen); + continue; + } + char* filePath = new char[pathLen + 1](); + if (sprintf_s(filePath, pathLen + 1, "%s/%s", path, info->name) < 0) { + printf("GalleryAbilitySlice::AddAllPictures | sprintf_s error\n"); + delete[] filePath; + continue; + } + if (FileUnlink(filePath) != 0) { + printf("unlink file error | %s\n", filePath); + } + delete[] filePath; + } + delete info; + FileCloseDir(drip); + printf("GalleryAbilitySlice::DeleteAllFilesInDir() | success | %s\n", path); +} + +void GalleryAbilitySlice::OnStart(const Want &want) +{ + AbilitySlice::OnStart(want); + + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(ROOT_VIEW_POSITION_X, ROOT_VIEW_POSITION_Y); + rootView_->Resize(ROOT_VIEW_WIDTH, ROOT_VIEW_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Black().full); + + const char* pathHeader = GetSrcPath(); + if (sprintf_s(g_backIconAbsolutePath, MAX_PATH_LENGTH, "%s%s", pathHeader, BACK_ICON_PATH) < 0) { + printf("GalleryAbilitySlice::OnStart | g_backIconAbsolutePath error"); + return; + } + if (sprintf_s(g_videoTagIconAbsolutePath, MAX_PATH_LENGTH, "%s%s", pathHeader, VIDEO_TAG_ICON_PATH) < 0) { + printf("GalleryAbilitySlice::OnStart | g_videoTagIconAbsolutePath error"); + return; + } + + InitTitle(); + InitPictureList(); + SetUIContent(rootView_); +} + +void GalleryAbilitySlice::OnInactive() +{ + printf("GalleryAbilitySlice::OnInactive\n"); + AbilitySlice::OnInactive(); +} + +void GalleryAbilitySlice::OnActive(const Want &want) +{ + printf("GalleryAbilitySlice::OnActive\n"); + AbilitySlice::OnActive(want); +} + +void GalleryAbilitySlice::OnBackground() +{ + printf("GalleryAbilitySlice::OnBackground\n"); + AbilitySlice::OnBackground(); +} + +void GalleryAbilitySlice::OnStop() +{ + printf("GalleryAbilitySlice::OnStop\n"); + AbilitySlice::OnStop(); + Clear(); +} +} \ No newline at end of file diff --git a/gallery/src/picture_ability_slice.cpp b/gallery/src/picture_ability_slice.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e038eacf78bcf02e39b4ed618e8825703a02736c --- /dev/null +++ b/gallery/src/picture_ability_slice.cpp @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "picture_ability_slice.h" +#include "ability_env.h" +#include "file.h" +#include "gallery_config.h" +#include "securec.h" + +namespace OHOS { +REGISTER_AS(PictureAbilitySlice) + +PictureAbilitySlice::~PictureAbilitySlice() +{ + Clear(); +} + +void PictureAbilitySlice::Clear() +{ + printf("PictureAbilitySlice::Clear() | start \n"); + if (backIcon_ != nullptr) { + delete backIcon_; + backIcon_ = nullptr; + } + if (backIconListener_ != nullptr) { + delete backIconListener_; + backIconListener_ = nullptr; + } + if (backArea_ != nullptr) { + delete backArea_; + backArea_ = nullptr; + } + if (picture_ != nullptr) { + delete picture_; + picture_ = nullptr; + } + if (rootView_ != nullptr) { + RootView::DestoryWindowRootView(rootView_); + rootView_ = nullptr; + } + printf("PictureAbilitySlice::Clear() | end \n"); +} + +void PictureAbilitySlice::InitTitle() +{ + printf("PictureAbilitySlice::InitTitle | start \n"); + backIcon_ = new UIImageView(); + backIcon_->SetPosition(BACK_ICON_POSITION_X, BACK_ICON_POSITION_Y); + backIcon_->SetSrc(g_backIconAbsolutePath); + backIcon_->SetTouchable(true); + + backArea_ = new UIViewGroup(); + backArea_->SetPosition(0, 0, LABEL_POSITION_X, LABEL_HEIGHT); + backArea_->SetStyle(STYLE_BACKGROUND_OPA, 0); + backArea_->SetTouchable(true); + + auto onClick = [this] (UIView &view, const Event &event) -> bool { + printf("############ terminate AS enter #############\n"); + Terminate(); + printf("############ terminate AS exit #############\n"); + return true; + }; + backIconListener_ = new EventListener(onClick, nullptr); + backIcon_->SetOnClickListener(backIconListener_); + backArea_->SetOnClickListener(backIconListener_); + + backArea_->Add(backIcon_); + rootView_->Add(backArea_); +} + +void PictureAbilitySlice::InitPicture(const char* path) +{ + printf("PictureAbilitySlice::InitPicture | start | %s\n", path); + picture_ = new UIImageView(); + picture_->SetSrc(path); + int16_t imageWidth = picture_->GetWidth(); + int16_t imageHeight = picture_->GetHeight(); + if (imageWidth > ROOT_VIEW_WIDTH || imageHeight > ROOT_VIEW_HEIGHT) { + TransformMap transMap(picture_->GetOrigRect()); + float scaleWidth = 1.0; + float scaleHeight = 1.0; + if (imageWidth > ROOT_VIEW_WIDTH) { + scaleWidth = static_cast(ROOT_VIEW_WIDTH) / imageWidth; + printf("########## scaleWidth: %f \n", scaleWidth); + } + if (imageHeight > ROOT_VIEW_HEIGHT) { + scaleHeight = static_cast(ROOT_VIEW_HEIGHT) / imageHeight; + printf("########## scaleHeight: %f \n", scaleHeight); + } + float scale = (scaleWidth < scaleHeight) ? scaleWidth : scaleHeight; + printf("########## scale: %f \n", scale); + transMap.Scale(Vector2(scale, scale), Vector2(0, 0)); + picture_->SetTransformMap(transMap); + picture_->SetTransformAlgorithm(TransformAlgorithm::NEAREST_NEIGHBOR); + imageWidth = imageWidth * scale; + imageHeight = imageHeight * scale; + } + int16_t imagePosX = (ROOT_VIEW_WIDTH - imageWidth) / 2; // 2: half + int16_t imagePosY = (ROOT_VIEW_HEIGHT - imageHeight) / 2; // 2: half + printf("########## image pos x: %d | y: %d \n", imagePosX, imagePosY); + picture_->SetPosition(imagePosX, imagePosY); + + rootView_->Add(picture_); +} + +void PictureAbilitySlice::OnStart(const Want &want) +{ + printf("######### PictureAbilitySlice::OnStart\n"); + printf("receive the data -> %s\n", reinterpret_cast(want.data)); + AbilitySlice::OnStart(want); + + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(ROOT_VIEW_POSITION_X, ROOT_VIEW_POSITION_Y); + rootView_->Resize(ROOT_VIEW_WIDTH, ROOT_VIEW_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Black().full); + + uint16_t imagePathLen = strlen(PHOTO_DIRECTORY) + strlen(reinterpret_cast(want.data)) + 1; + if (imagePathLen > MAX_PATH_LENGTH) { + printf("---- imagePathLen > MAX_PATH_LENGTH | %d", imagePathLen); + return; + } + char* imagePath = new char[imagePathLen + 1](); + if (sprintf_s(imagePath, imagePathLen + 1, "%s/%s", PHOTO_DIRECTORY, reinterpret_cast(want.data)) < 0) { + printf("PictureAbilitySlice::OnStart | imagePath\n"); + return; + } + + const char* pathHeader = GetSrcPath(); + if (sprintf_s(g_backIconAbsolutePath, MAX_PATH_LENGTH, "%s%s", pathHeader, BACK_ICON_PATH) < 0) { + printf("PictureAbilitySlice::OnStart | g_backIconAbsolutePath\n"); + return; + } + + InitPicture(imagePath); + InitTitle(); + delete[] imagePath; + + SetUIContent(rootView_); +} + +void PictureAbilitySlice::OnInactive() +{ + printf("PictureAbilitySlice::OnInactive\n"); + AbilitySlice::OnInactive(); +} + +void PictureAbilitySlice::OnActive(const Want &want) +{ + printf("PictureAbilitySlice::OnActive\n"); + AbilitySlice::OnActive(want); +} + +void PictureAbilitySlice::OnBackground() +{ + printf("PictureAbilitySlice::OnBackground\n"); + AbilitySlice::OnBackground(); +} + +void PictureAbilitySlice::OnStop() +{ + printf("PictureAbilitySlice::OnStop\n"); + AbilitySlice::OnStop(); + Clear(); +} +} \ No newline at end of file diff --git a/gallery/src/player_ability_slice.cpp b/gallery/src/player_ability_slice.cpp new file mode 100644 index 0000000000000000000000000000000000000000..589bc49aa95477a21047d95dfc09d6199a15c6e4 --- /dev/null +++ b/gallery/src/player_ability_slice.cpp @@ -0,0 +1,453 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "player_ability_slice.h" +#include +#include + +#include "ability_env.h" +#include "ability_manager.h" +#include "components/ui_image_view.h" +#include "file.h" + +using OHOS::Media::Player; +using OHOS::Media::Source; +using namespace OHOS::Media; + +namespace OHOS { +REGISTER_AS(PlayerAbilitySlice) + + +PlayerAbilitySlice::~PlayerAbilitySlice() +{ + printf("################ ~PlayerAbilitySlice enter\n"); + + /** released in DestoryPlayer(). */ + + printf("################ ~PlayerAbilitySlice exit\n"); +} + +void PlayerAbilitySlice::Clear() +{ + printf("PlayerAbilitySlice::Clear | enter\n"); + if (backIconListener_ != nullptr) { + delete backIconListener_; + backIconListener_ = nullptr; + } + if (onClickListener_ != nullptr) { + delete onClickListener_; + onClickListener_ = nullptr; + } + if (surfaceView_ != nullptr) { + delete surfaceView_; + surfaceView_ = nullptr; + } + if (animatorGroup_ != nullptr) { + delete animatorGroup_; + animatorGroup_ = nullptr; + } + if (backIcon_ != nullptr) { + delete backIcon_; + backIcon_ = nullptr; + } + if (backArea_ != nullptr) { + delete backArea_; + backArea_ = nullptr; + } + if (errorTips_ != nullptr) { + delete errorTips_; + errorTips_ = nullptr; + } + if (totalTimeLabel_ != nullptr) { + delete totalTimeLabel_; + totalTimeLabel_ = nullptr; + } + if (currentTimeLabel_ != nullptr) { + delete currentTimeLabel_; + currentTimeLabel_ = nullptr; + } + if (slider_ != nullptr) { + delete slider_; + slider_ = nullptr; + } + if (toggleButton_ != nullptr) { + delete toggleButton_; + toggleButton_ = nullptr; + } + if (toggleButtonArea_ != nullptr) { + delete toggleButtonArea_; + toggleButtonArea_ = nullptr; + } + if (rootView_ != nullptr) { + RootView::DestoryWindowRootView(rootView_); + rootView_ = nullptr; + } + printf("PlayerAbilitySlice::Clear() | end\n"); +} + +void PlayerAbilitySlice::ShowErrorTips() +{ + errorTips_ = new UILabel(); + errorTips_->SetPosition(ROOT_VIEW_POSITION_X, ROOT_VIEW_POSITION_Y, ROOT_VIEW_WIDTH, ROOT_VIEW_HEIGHT); + errorTips_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER, + UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER); + errorTips_->SetFont(FONT_NAME, GALLERY_FONT_SIZE); + errorTips_->SetText("视频播放错误"); + + rootView_->Add(backArea_); + rootView_->Add(errorTips_); + rootView_->Add(backIcon_); + SetUIContent(rootView_); +} + +void PlayerAbilitySlice::SetUpRootView() +{ + if (rootView_ != nullptr) { + return; + } + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(ROOT_VIEW_POSITION_X, ROOT_VIEW_POSITION_Y); + rootView_->Resize(ROOT_VIEW_WIDTH, ROOT_VIEW_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Black().full); +} + +void PlayerAbilitySlice::SetUpBackArea(const char* pathHeader) +{ + auto onClick = [this] (UIView &view, const Event &event) -> bool { + printf("############ PlayerAbilitySlice terminate AS enter #############\n"); + Terminate(); + printf("############ PlayerAbilitySlice terminate AS exit #############\n"); + return true; + }; + backIcon_ = new UIImageView(); + backIcon_->SetPosition(BACK_ICON_POSITION_X, BACK_ICON_POSITION_Y); + + if (sprintf_s(g_backIconAbsolutePath, MAX_PATH_LENGTH, "%s%s", pathHeader, BACK_ICON_PATH) < 0) { + printf("PlayerAbilitySlice::OnStart | g_backIconAbsolutePath | %s\n", pathHeader); + return; + } + backIcon_->SetSrc(g_backIconAbsolutePath); + backIcon_->SetTouchable(true); + backIconListener_ = new EventListener(onClick, nullptr); + backIcon_->SetOnClickListener(backIconListener_); + + backArea_ = new UIViewGroup(); + backArea_->SetPosition(0, 0, LABEL_POSITION_X, LABEL_HEIGHT); + backArea_->SetStyle(STYLE_BACKGROUND_OPA, 0); + backArea_->SetTouchable(true); + backArea_->SetOnClickListener(backIconListener_); + + rootView_->Add(backArea_); + rootView_->Add(backIcon_); +} + +void PlayerAbilitySlice::SetUpVideoPlayer(const Want &want) +{ + if (videoPlayer_ == nullptr) { + videoPlayer_ = new PlayerAdapter(); + } + videoPlayer_->sourceType = 1; + + uint16_t videoPathLen = strlen(VIDEO_SOURCE_DIRECTORY) + strlen(reinterpret_cast(want.data)) + 1; + int8_t ret = sprintf_s(videoPlayer_->filePath, videoPathLen + 1, "%s/%s", VIDEO_SOURCE_DIRECTORY, + reinterpret_cast(want.data)); + if (ret < 0) { + printf("PlayerAbilitySlice::OnStart | videoPlayer_->filePath | %s\n", reinterpret_cast(want.data)); + return; + } + ret = sprintf_s(&videoPlayer_->filePath[videoPathLen - strlen(AVAILABEL_SOURCE_TYPE)], + strlen(AVAILABEL_SOURCE_TYPE) + 1, "%s", AVAILABEL_SOURCE_TYPE); + if (ret < 0) { + printf("PlayerAbilitySlice::OnStart | videoPlayer_->filePath \n"); + return; + } + printf("------########### mp4 file path | %s\n", videoPlayer_->filePath); + + videoPlayer_->adapter = std::make_shared(); + std::string uri(videoPlayer_->filePath); + std::map header; + Source source(uri, header); + videoPlayer_->adapter->SetSource(source); +} + +bool PlayerAbilitySlice::SetUpSurfaceView() +{ + if (surfaceView_ != nullptr) { + return true; + } + int32_t width = 0; + int32_t height = 0; + videoPlayer_->adapter->GetVideoWidth(width); + printf("[%s,%d] width:%d\n", __func__, __LINE__, width); + videoPlayer_->adapter->GetVideoHeight(height); + printf("[%s,%d] height:%d\n", __func__, __LINE__, height); + + if (width <= 0 || height <= 0) { + videoPlayer_->adapter->Release(); + delete videoPlayer_; + videoPlayer_ = nullptr; + printf("******** width <= 0 || height <= 0 | return \n"); + ShowErrorTips(); + return false; + } + float ratio_x = static_cast(width) / ROOT_VIEW_WIDTH; + float ratio_y = static_cast(height) / ROOT_VIEW_HEIGHT; + uint16_t surfaceViewWidth; + uint16_t surfaceViewHeight; + uint16_t surfaceViewPositionX = 0; + uint16_t surfaceViewPositionY = 0; + if (ratio_x > ratio_y) { + surfaceViewWidth = ROOT_VIEW_WIDTH; + surfaceViewHeight = height / ratio_x; + surfaceViewPositionY = (ROOT_VIEW_HEIGHT - surfaceViewHeight) / 2; // 2: half + } else { + surfaceViewWidth = width / ratio_y; + surfaceViewHeight = ROOT_VIEW_HEIGHT; + surfaceViewPositionX = (ROOT_VIEW_WIDTH - surfaceViewWidth) / 2; // 2: half + } + + surfaceView_ = new UISurfaceView(); + surfaceView_->SetPosition(surfaceViewPositionX, surfaceViewPositionY); + surfaceView_->SetWidth(surfaceViewWidth - 1); + surfaceView_->SetHeight(surfaceViewHeight); + videoPlayer_->adapter->SetVideoSurface(surfaceView_->GetSurface()); + + rootView_->Add(surfaceView_); + + return true; +} + +void PlayerAbilitySlice::SetUpProgress(int64_t duration) +{ + slider_ = new UISlider(); + slider_->SetPosition(SLIDER_X, SLIDER_Y, SLIDER_WIDTH, STATUS_BAR_GROUP_HEIGHT); + slider_->SetValidHeight(SLIDER_HEIGHT); + slider_->SetValidWidth(SLIDER_WIDTH - KNOB_WIDTH); + slider_->SetRange(SLIDER_WIDTH, 0); + slider_->SetValue(0); + slider_->SetKnobWidth(KNOB_WIDTH); + slider_->SetSliderRadius(SLIDER_HEIGHT, SLIDER_HEIGHT, KNOB_WIDTH / 2); // 2: half + slider_->SetKnobStyle(STYLE_BACKGROUND_COLOR, Color::White().full); + slider_->SetBackgroundStyle(STYLE_BACKGROUND_COLOR, 0x1A888888); + slider_->SetBackgroundStyle(STYLE_BACKGROUND_OPA, 90); // 90: opacity is 90 + slider_->SetDirection(UISlider::Direction::DIR_LEFT_TO_RIGHT); + slider_->SetTouchable(false); + animatorGroup_->Add(slider_); + + animator_ = new SliderAnimator(videoPlayer_, slider_, currentTimeLabel_, duration, surfaceView_); + AnimatorManager::GetInstance()->Add(animator_); +} + +void PlayerAbilitySlice::SetUpAnimatorGroup(const char* pathHeader) +{ + int64_t duration = 0; + videoPlayer_->adapter->GetDuration(duration); + printf("[%s,%d] GetDuration:%lld\n", __func__, __LINE__, duration); + + animatorGroup_ = new UIViewGroup(); + animatorGroup_->SetPosition(0, ROOT_VIEW_HEIGHT - STATUS_BAR_GROUP_HEIGHT, + ROOT_VIEW_WIDTH, STATUS_BAR_GROUP_HEIGHT); + animatorGroup_->SetStyle(STYLE_BACKGROUND_OPA, 0); + + totalTimeLabel_ = new UILabel(); + totalTimeLabel_->SetPosition(TOTAL_TIME_LABEL_X, TOTAL_TIME_LABEL_Y, + TOTAL_TIME_LABEL_WIDTH, TOTAL_TIME_LABEL_HEIGHT); + totalTimeLabel_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, + UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER); + totalTimeLabel_->SetFont(FONT_NAME, PLAYER_FONT_SIZE); + int64_t second = duration / 1000; // 1000: 1s = 1000ms + char timer[6]; // 6: length of time label + if (sprintf_s(timer, sizeof(timer), "%02lld:%02lld", second / 60, second % 60) < 0) { // 60: 1minute = 60s + return; + } + + totalTimeLabel_->SetText(timer); + totalTimeLabel_->SetTextColor(Color::White()); + animatorGroup_->Add(totalTimeLabel_); + + currentTimeLabel_ = new UILabel(); + currentTimeLabel_->SetPosition(CURRENT_TIME_LABEL_X, CURRENT_TIME_LABEL_Y, + CURRENT_TIME_LABEL_WIDTH, CURRENT_TIME_LABEL_HEIGHT); + currentTimeLabel_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full); + currentTimeLabel_->SetFont(FONT_NAME, PLAYER_FONT_SIZE); + currentTimeLabel_->SetText("00:00"); + currentTimeLabel_->SetTextColor(Color::White()); + currentTimeLabel_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, + UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER); + animatorGroup_->Add(currentTimeLabel_); + + SetUpProgress(duration); + + SetUpToggleButton(pathHeader); + + rootView_->Add(animatorGroup_); +} + +void PlayerAbilitySlice::SetUpToggleButton(const char* pathHeader) +{ + toggleButton_ = new UIToggleButton(); + toggleButton_->SetTouchable(false); + toggleButton_->SetPosition(TOGGLE_BUTTON_OFFSET_X, TOGGLE_BUTTON_OFFSET_Y, + TOGGLE_BUTTON_WIDTH, TOGGLE_BUTTON_HEIGHT); + toggleButton_->SetState(true); + + if (sprintf_s(g_videoPlayAbsolutePath, MAX_PATH_LENGTH, "%s%s", pathHeader, VIDEO_PALY_PATH) < 0) { + printf("PlayerAbilitySlice::OnStart | g_videoPlayAbsolutePath\n"); + return; + } + + if (sprintf_s(g_videoPauseAbsolutePath, MAX_PATH_LENGTH, "%s%s", pathHeader, VIDEO_PAUSE_PATH) < 0) { + printf("PlayerAbilitySlice::OnStart | g_videoPauseAbsolutePath\n"); + return; + } + toggleButton_->SetImages(g_videoPauseAbsolutePath, g_videoPlayAbsolutePath); + onClickListener_ = new ToggleBtnListener(toggleButton_, videoPlayer_, animator_, surfaceView_); + + toggleButtonArea_ = new UIViewGroup(); + toggleButtonArea_->SetStyle(STYLE_BACKGROUND_OPA, 0); + toggleButtonArea_->SetPosition(0, 0, TOGGLE_BUTTON_OFFSET_X + TOGGLE_BUTTON_WIDTH, STATUS_BAR_GROUP_HEIGHT); + toggleButtonArea_->SetTouchable(true); + toggleButtonArea_->SetOnClickListener(onClickListener_); + toggleButtonArea_->Add(toggleButton_); + + animatorGroup_->Add(toggleButtonArea_); +} + +void PlayerAbilitySlice::OnStart(const Want &want) +{ + printf("@@@@@ PlayerAbilitySlice::OnStart\n"); + AbilitySlice::OnStart(want); + + SetUpRootView(); + const char* pathHeader = GetSrcPath(); + SetUpVideoPlayer(want); + + videoPlayer_->adapter->Prepare(); + + if (!SetUpSurfaceView()) { + return; + } + SetUpBackArea(pathHeader); + SetUpAnimatorGroup(pathHeader); + + SetUIContent(rootView_); + + videoPlayer_->adapter->Play(); + animator_->SetToggleButton(toggleButton_); + animator_->SetToggleBtnListener(onClickListener_); + animator_->Start(); + + printf("## @@@@@ PlayerAbilitySlice::OnStart | end \n"); +} + +void PlayerAbilitySlice::OnInactive() +{ + printf("PlayerAbilitySlice::OnInactive\n"); + AbilitySlice::OnInactive(); +} + +void PlayerAbilitySlice::OnActive(const Want &want) +{ + printf("PlayerAbilitySlice::OnActive\n"); + AbilitySlice::OnActive(want); +} + +void PlayerAbilitySlice::OnBackground() +{ + printf("PlayerAbilitySlice::OnBackground\n"); + AbilitySlice::OnBackground(); +} + +void PlayerAbilitySlice::OnStop() +{ + if (animator_ != nullptr) { + animator_->Stop(); + AnimatorManager::GetInstance()->Remove(animator_); + delete animator_; + animator_ = nullptr; + } + + if (videoPlayer_ != nullptr && videoPlayer_->adapter.get() != nullptr) { + videoPlayer_->adapter->Stop(); + videoPlayer_->adapter->Release(); + delete videoPlayer_; + videoPlayer_ = nullptr; + } + Clear(); + printf("PlayerAbilitySlice::OnStop\n"); + AbilitySlice::OnStop(); +} + +void SliderAnimator::Callback(UIView* view) +{ + if (needRefreshPlayer_) { + videoPlayer_->adapter->Stop(); + videoPlayer_->adapter->Release(); + + videoPlayer_->adapter = std::make_shared(); + std::string uri(videoPlayer_->filePath); + std::map header; + Source source(uri, header); + videoPlayer_->adapter->SetSource(source); + videoPlayer_->adapter->Prepare(); + videoPlayer_->adapter->SetVideoSurface(surfaceView_->GetSurface()); + videoPlayer_->adapter->Play(); + needRefreshPlayer_ = false; + } + + int64_t currentTime = 0; + videoPlayer_->adapter->GetCurrentTime(currentTime); + int64_t currentSecond = currentTime / 1000; // 1000: 1s = 1000ms + + char time[6]; // 6: length of time label + sprintf_s(time, sizeof(time), "%02lld:%02lld", currentSecond / 60, currentSecond % 60); // 60: 1minute = 60s + timeLabel_->SetText(time); + timeLabel_->Invalidate(); + + int64_t curPosition = currentTime * slider_->GetRangeMax() / duration_; + slider_->SetValue(curPosition); + slider_->Invalidate(); + + if (currentTime >= duration_) { + listener_->SetCompleteFlag(true); + toggleButton_->SetState(false); + needRefreshPlayer_ = true; + Stop(); + } +} + +bool ToggleBtnListener::OnClick(UIView& view, const ClickEvent& event) +{ + button_->OnClickEvent(event); + if (completeFlag_) { + animator_->Start(); + button_->Invalidate(); + completeFlag_ = false; + return true; + } + + if (button_->GetState()) { + videoPlayer_->adapter->Play(); + animator_->Resume(); + printf("ToggleBtnListener::OnClick | play\n"); + } else { + videoPlayer_->adapter->Pause(); + animator_->Pause(); + printf("ToggleBtnListener::OnClick | pause\n"); + } + button_->Invalidate(); + return true; +} +} \ No newline at end of file diff --git a/hap/BUILD.gn b/hap/BUILD.gn deleted file mode 100755 index e1cf3a23f60448425c2b4382d925da4d3c4eb02a..0000000000000000000000000000000000000000 --- a/hap/BUILD.gn +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright (c) 2020 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. - - -copy("copy_hap") { - sources = [ - "//applications/sample/camera/hap/launcher.hap", - "//applications/sample/camera/hap/setting.hap", - "//applications/sample/camera/hap/camera.hap", - "//applications/sample/camera/hap/gallery.hap", - ] - outputs = [ - "$root_out_dir/system/internal/{{source_file_part}}", - ] -} diff --git a/hap/camera.hap b/hap/camera.hap deleted file mode 100755 index 9719ed9b7a49020cdd2e0aa5388757068b516b57..0000000000000000000000000000000000000000 Binary files a/hap/camera.hap and /dev/null differ diff --git a/hap/gallery.hap b/hap/gallery.hap deleted file mode 100755 index 901b0b8e054e4fc6ac81aa4278fa90242b4ac486..0000000000000000000000000000000000000000 Binary files a/hap/gallery.hap and /dev/null differ diff --git a/hap/launcher.hap b/hap/launcher.hap deleted file mode 100755 index bed7ecf89576647c495a3edb019397c47b00772c..0000000000000000000000000000000000000000 Binary files a/hap/launcher.hap and /dev/null differ diff --git a/hap/setting.hap b/hap/setting.hap deleted file mode 100755 index 478efa0c5ccfac3b6a7bc3898919d10507ebd635..0000000000000000000000000000000000000000 Binary files a/hap/setting.hap and /dev/null differ diff --git a/launcher/BUILD.gn b/launcher/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0e672f5080a4eaea48ef029e068f77e0420ee48e --- /dev/null +++ b/launcher/BUILD.gn @@ -0,0 +1,75 @@ +# Copyright (c) 2020 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. +import("//build/lite/config/component/lite_component.gni") +import("//build/lite/config/hap_pack.gni") + +shared_library("launcher") { + sources = [ + "launcher/src/main/cpp/main_ability.cpp", + "launcher/src/main/cpp/main_ability_slice.cpp", + "launcher/src/main/cpp/swipe_view.cpp", + "launcher/src/main/cpp/app_info.cpp", + "launcher/src/main/cpp/long_press_view.cpp", + "launcher/src/main/cpp/app_manage.cpp", + "launcher/src/main/cpp/view_group_page.cpp", + "launcher/src/main/cpp/time_weather_view.cpp" + ] + + deps = [ + "//foundation/aafwk/frameworks/ability_lite:aafwk_abilitykit_lite", + "//foundation/appexecfwk/frameworks/bundle_lite:bundle", + "//foundation/communication/frameworks/ipc_lite:liteipc_adapter", + "//foundation/distributedschedule/services/samgr_lite/samgr:samgr", + "//utils/native/lite/kv_store:kv_store", + "//foundation/graphic/lite/frameworks/ui:ui", + "//foundation/graphic/lite/utils:graphic_utils", + "//foundation/graphic/lite/frameworks/surface", + ] + + include_dirs = [ + "launcher/src/main/cpp", + "//foundation/aafwk/interfaces/kits/ability_lite", + "//foundation/graphic/lite/interfaces/kits/ui/components", + "//foundation/graphic/lite/interfaces/kits/ui", + "//foundation/graphic/lite/interfaces/kits/utils", + "//foundation/graphic/lite/interfaces/kits/config", + "//foundation/appexecfwk/interfaces/kits/bundle_lite", + "//foundation/aafwk/interfaces/kits/want_lite", + "//base/startup/interfaces/kits/syspara_lite", + ] + + defines = [ + "ENABLE_WINDOW=1", + "ABILITY_WINDOW_SUPPORT", + "OHOS_APPEXECFWK_BMS_BUNDLEMANAGER" + ] +} + +lite_component("launcher_component") { + + features = [ + ":launcher", + ] +} + +hap_pack("launcher_hap") +{ + deps = [":launcher"] + mode = "hap" + json_path = "launcher/src/main/config.json" + ability_so_path = "$root_out_dir/liblauncher.so" + force = "true" + cert_profile = "cert/com.huawei.launcher_HarmonyAppProvision_release.p7b" + resources_path = "launcher/src/main/resources" + hap_name = "launcher" +} diff --git a/launcher/cert/com.huawei.launcher_HarmonyAppProvision_release.p7b b/launcher/cert/com.huawei.launcher_HarmonyAppProvision_release.p7b new file mode 100644 index 0000000000000000000000000000000000000000..fb05882743ff79ca7883b603d3aaf13d01d4bafa Binary files /dev/null and b/launcher/cert/com.huawei.launcher_HarmonyAppProvision_release.p7b differ diff --git a/launcher/launcher/src/main/config.json b/launcher/launcher/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..2cc4c390977e9c51dcad579ffe67a9ae4850da3d --- /dev/null +++ b/launcher/launcher/src/main/config.json @@ -0,0 +1,43 @@ +{ + "app": { + "bundleName": "com.huawei.launcher", + "vendor": "huawei", + "version": { + "code": 1, + "name": "1.0" + }, + "apiVersion": { + "compatible": 3, + "target": 4 + } + }, + "deviceConfig": { + "default": { + "keepAlive": true + }, + "smartCamera": { + "keepAlive": true + } + }, + "module": { + "package": "com.huawei.launcher", + "name": ".MyHarmonyAbilityPackage", + "deviceType": [ + "phone", "tv","tablet", "pc","car","smartWatch","sportsWatch","smartVision" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "launcher", + "moduleType": "entry" + }, + "abilities": [{ + "name": "MainAbility", + "icon": "assets/launcher/resources/base/media/background.png", + "label": "launcher", + "launchType": "standard", + "type": "page", + "visible":true + } + ] + } +} diff --git a/launcher/launcher/src/main/cpp/app_info.cpp b/launcher/launcher/src/main/cpp/app_info.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ea58fc031b3b6622c205234715d2f4ef4385e99 --- /dev/null +++ b/launcher/launcher/src/main/cpp/app_info.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "app_info.h" +#include "ui_config.h" + +namespace OHOS { +AppInfo::AppInfo() +{ + ReSet(); +} +void AppInfo::Release() +{ + if (button_) { + delete button_; + button_ = nullptr; + } + if (lable_) { + delete lable_; + lable_ = nullptr; + } + if (appClickListener_) { + delete appClickListener_; + appClickListener_ = nullptr; + } + if (appLpListener_) { + delete appLpListener_; + appLpListener_ = nullptr; + } + if (appDListener_) { + delete appDListener_; + appDListener_ = nullptr; + } +} + +AppInfo::~AppInfo() +{ + Release(); +} + +void AppInfo::ReSet() +{ + button_ = nullptr; + lable_ = nullptr; + appClickListener_ = nullptr; + appLpListener_ = nullptr; + appDListener_ = nullptr; +} + +void AppInfo::SetButton(UILabelButton* button) +{ + button->SetPosition(buttonXY_.x, buttonXY_.y, buttonHV_.x, buttonHV_.y); + button->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + button->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_RADIUS, UIButton::PRESSED); + button->SetStyleForState(STYLE_BACKGROUND_OPA, TOTAL_OPACITY, UIButton::PRESSED); + button->SetStyleForState(STYLE_BORDER_OPA, TOTAL_OPACITY, UIButton::PRESSED); + button->SetStyle(STYLE_BORDER_RADIUS, BUTTON_RADIUS); + button->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + button->SetStyle(STYLE_BORDER_OPA, TOTAL_OPACITY); + button->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + button->SetImageSrc(appIconDir_, appIconDir_); + button_ = button; +} + +void AppInfo::SetLable(UILabel* lable) +{ + lable->SetPosition(lableXY_.x, lableXY_.y, lableHV_.x, lableHV_.y); + char* p = strrchr(appName_, static_cast('.')); + if (p != nullptr) { + lable->SetText(p + 1); + } else { + lable->SetText(appName_); + } + lable->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_TOP); + lable->SetFont(FOND_PATH, APP_FOND_ID); + lable->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS); + lable->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + lable_ = lable; +} + +void AppInfo::SetListener(AppInfo* app) +{ + AppClickListener* appClickListener = new AppClickListener(app->funcclick_, this); + button_->SetOnClickListener(appClickListener); + appClickListener_ = static_cast(appClickListener); + AppLongPressListener* appLpListener = new AppLongPressListener(app->funclPress_, this); + button_->SetOnLongPressListener(appLpListener); + appLpListener_ = static_cast(appLpListener); + AppDragListener* appDListener = new AppDragListener(this); + button_->SetDraggable(true); + button_->SetOnDragListener(appDListener); + appDListener_ = static_cast(appDListener); +} + +void AppInfo::SetLocation(int16_t r, int16_t c) +{ + row_col_.x = r; + row_col_.y = c; +} +} // namespace OHOS diff --git a/launcher/launcher/src/main/cpp/app_info.h b/launcher/launcher/src/main/cpp/app_info.h new file mode 100644 index 0000000000000000000000000000000000000000..fecf807ceb11209d824633d24e330dea44a0119b --- /dev/null +++ b/launcher/launcher/src/main/cpp/app_info.h @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_APP_INFO_H +#define OHOS_APP_INFO_H + +#include +#include +#include + +#include "native_base.h" +#include "ui_config.h" + +namespace OHOS { + +class AppInfo; +typedef bool (*funcLongPress)(AppInfo *app); +typedef bool (*funcClick)(AppInfo *app); +typedef bool (*UninstallApp)(AppInfo *app); +typedef bool (*AddApp)(AppInfo *app); + +struct MyPoint { + int16_t x; // the x coordinate of the point + int16_t y; // the y coordinate of the point +}; + +class AppInfo { +public: + AppInfo(); + virtual ~AppInfo(); + void Release(); + void ReSet(); + void SetButton(UILabelButton *button); + void SetLable(UILabel *lable); + void SetListener(AppInfo *app); + void SetLocation(int16_t r, int16_t c); + + UILabelButton* button_ { nullptr }; + UILabel* lable_ { nullptr }; + + UIView::OnLongPressListener* appLpListener_ { nullptr }; + UIView::OnDragListener* appDListener_ { nullptr }; + UIView::OnClickListener* appClickListener_ { nullptr }; + + funcClick funcclick_ { nullptr }; + funcLongPress funclPress_ { nullptr }; + MyPoint lableXY_ { 0 }; + MyPoint lableHV_ { 0 }; + MyPoint buttonXY_ { 0 }; + MyPoint buttonHV_ { 0 }; + MyPoint row_col_ { 0 }; + char appName_[TMP_BUF_SIZE]; + char abilityName_[TMP_BUF_SIZE]; + char appIconDir_[TMP_BUF_SIZE]; +}; + +class AppDragListener : public UIView::OnDragListener { +public: + AppDragListener(AppInfo* app) : appInfo_(app) {} + virtual ~AppDragListener() {} + bool OnDrag(UIView &view, const DragEvent &event) override + { + return true; + } + bool OnDragEnd(UIView& view, const DragEvent& event) override + { + return true; + } + bool OnDragStart(UIView& view, const DragEvent& event) override + { + return true; + } + +private: + AppInfo* appInfo_ { nullptr }; +}; + +class AppClickListener : public UIView::OnClickListener { +public: + AppClickListener(funcClick func, AppInfo* app) : funcClick_(func), appInfo_(app) {} + virtual ~AppClickListener() {} + bool OnClick(UIView& view, const ClickEvent& event) override + { + funcClick_(appInfo_); + return true; + } + +private: + funcClick funcClick_ { nullptr }; + AppInfo *appInfo_ { nullptr }; +}; + +class AppLongPressListener : public UIView::OnLongPressListener { +public: + AppLongPressListener(funcClick func, AppInfo* app) : appInfo_(app), funcLongPress_(func) {} + virtual ~AppLongPressListener() {} + bool OnLongPress(UIView& view, const LongPressEvent& event) override + { + funcLongPress_(appInfo_); + return true; + } + +private: + AppInfo* appInfo_ { nullptr }; + funcLongPress funcLongPress_ { nullptr }; +}; +} // namespace OHOS +#endif diff --git a/launcher/launcher/src/main/cpp/app_manage.cpp b/launcher/launcher/src/main/cpp/app_manage.cpp new file mode 100644 index 0000000000000000000000000000000000000000..768a152a401856416210715261e8a90cbd09a0dd --- /dev/null +++ b/launcher/launcher/src/main/cpp/app_manage.cpp @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2020 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. + */ + +#include +#include + +#include "app_manage.h" + +namespace OHOS { +ViewGroupPage* AppManage::viewPage_[MAX_VIEWGROUP] = { nullptr }; +funcClick AppManage::installFuncclick_ = { nullptr }; +funcLongPress AppManage::installFunclPress_ = { nullptr }; +int AppManage::size_ = 0; + +AppManage::~AppManage() +{ + UnregisterCallback(); +} + +bool AppManage::GetAailityInfosByBundleName(const char* bundleName, AppInfo* pApp) +{ + if (bundleName == nullptr || pApp == nullptr) { + return false; + } + uint8_t ret = -1; + BundleInfo* pBundleInfos = nullptr; + int count = 0; + ret = GetBundleInfos(1, &pBundleInfos, &count); + if (ret == 0) { + BundleInfo* pBundleInfo = pBundleInfos; + for (int i = 0; i < count; i++, pBundleInfo++) { + if (memcmp(bundleName, pBundleInfo->bundleName, strlen(pBundleInfo->bundleName)) == 0) { + memcpy_s( + pApp->appName_, sizeof(pApp->appName_), pBundleInfo->bundleName, strlen(pBundleInfo->bundleName)); + pApp->appName_[strlen(pBundleInfo->bundleName)] = 0; + if (pBundleInfo->abilityInfos[0].name) { + memcpy_s(pApp->abilityName_, sizeof(pApp->abilityName_), pBundleInfo->abilityInfos[0].name, + strlen(pBundleInfo->abilityInfos[0].name)); + pApp->abilityName_[strlen(pBundleInfo->abilityInfos[0].name)] = 0; + } + if (pBundleInfo->bigIconPath) { + memcpy_s(pApp->appIconDir_, sizeof(pApp->appIconDir_), pBundleInfo->bigIconPath, + strlen(pBundleInfo->bigIconPath)); + pApp->appIconDir_[strlen(pBundleInfo->bigIconPath)] = 0; + } + return true; + } + } + } + return false; +} + +bool AppManage::GetAppInstallInfo(const char* bundleName) +{ + if (bundleName == nullptr) { + return false; + } + AppInfo* pApp = new AppInfo(); + pApp->funcclick_ = installFuncclick_; + pApp->funclPress_ = installFunclPress_; + if (GetAailityInfosByBundleName(bundleName, pApp) == true) { + int i = 0; + for (; i < size_; i++) { + if (viewPage_[i]->FindApp(pApp)) { + break; + } + } + if (i == size_) { + for (i = 0; i < size_; i++) { + if (viewPage_[i]->AddApp(pApp)) { + return true; + } + } + } + } + delete pApp; + pApp = nullptr; + return false; +} + +void AppManage::MyBundleStateCallback( + const uint8_t installType, const uint8_t resultCode, const void* resultMessage, const char* bundleName, void* data) +{ + if (installType == 0) { // install update + if (resultCode == 0 && bundleName != nullptr) { + char tmpName[TMP_BUF_SIZE] = {0}; + memcpy_s(tmpName, sizeof(tmpName), bundleName, strlen(bundleName)); + tmpName[strlen(bundleName)] = 0; + GetAppInstallInfo(tmpName); + } + } +} + +void AppManage::MyBundleOwnCallback(const uint8_t resultCode, const void* resultMessage) +{ + // todo uninstall callback +} + +bool AppManage::LauncherApp(BundleInfo** info, int& count) +{ + callBackParam_.bundleName = nullptr; + callBackParam_.data = nullptr; + callBackParam_.callBack = MyBundleStateCallback; + RegisterCallback(&callBackParam_); + + BundleInfo* pBundleInfos = nullptr; + uint8_t ret = GetBundleInfos(1, &pBundleInfos, &count); + if (ret == 0) { + *info = pBundleInfos; + return true; + } else { + *info = nullptr; + return false; + } +} + +bool AppManage::InstallApp(AppInfo* app) +{ + return true; +} + +bool AppManage::UnInstallApp(AppInfo* app) +{ + return Uninstall(app->appName_, nullptr, MyBundleOwnCallback); +} + +bool AppManage::StartApp(AppInfo* app) +{ + Want want1 = { nullptr }; + ElementName element = { nullptr }; + SetElementBundleName(&element, app->appName_); + SetElementAbilityName(&element, app->abilityName_); + SetWantElement(&want1, element); + SetWantData(&want1, "WantData", strlen("WantData") + 1); + StartAbility(&want1); + ClearElement(&element); + ClearWant(&want1); + return true; +} + +void AppManage::SetViewGroup(funcClick click, funcLongPress press, ViewGroupPage* viewPage[MAX_VIEWGROUP], int size) +{ + if (click == nullptr || press == nullptr || viewPage == nullptr) { + return; + } + for (int i = 0; i < size; i++) { + viewPage_[i] = viewPage[i]; + } + size_ = size; + installFuncclick_ = click; + installFunclPress_ = press; +} +} // namespace OHOS diff --git a/launcher/launcher/src/main/cpp/app_manage.h b/launcher/launcher/src/main/cpp/app_manage.h new file mode 100644 index 0000000000000000000000000000000000000000..ae8c4a8d206e743cdc9adc6f30cedfc89e78b382 --- /dev/null +++ b/launcher/launcher/src/main/cpp/app_manage.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_APP_MANAGE_H +#define OHOS_APP_MANAGE_H + +#include +#include +#include +#include +#include +#include + +#include "app_info.h" +#include "ui_config.h" +#include "view_group_page.h" + +namespace OHOS { +class AppManage { +public: + AppManage() = default; + ~AppManage(); + // start all app ,get all app info + + bool LauncherApp(BundleInfo** info, int& count); + bool InstallApp(AppInfo* app); + bool UnInstallApp(AppInfo* app); + bool StartApp(AppInfo* app); + static void SetViewGroup(funcClick click, funcLongPress press, ViewGroupPage* arrPage[MAX_VIEWGROUP], int size); + +private: + static void MyBundleStateCallback(const uint8_t installType, const uint8_t resultCode, const void* resultMessage, + const char* bundleName, void* data); + static void MyBundleOwnCallback(const uint8_t resultCode, const void* resultMessage); + static bool GetAailityInfosByBundleName(const char* bundleName, AppInfo* pApp); + static bool GetAppInstallInfo(const char* bundleName); + +private: + BundleStatusCallback callBackParam_ { nullptr }; + static ViewGroupPage* viewPage_[MAX_VIEWGROUP]; + static int size_; + static funcClick installFuncclick_; + static funcLongPress installFunclPress_; +}; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/launcher/launcher/src/main/cpp/event_listener.h b/launcher/launcher/src/main/cpp/event_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..38c321ef8d0d55c302bc919df7edb8cff190c0e1 --- /dev/null +++ b/launcher/launcher/src/main/cpp/event_listener.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_EVENT_LISTENER_H +#define OHOS_EVENT_LISTENER_H + +#include +#include +#include + +namespace OHOS { +using OnEventFunc = std::function; + +class EventListener : public UIView::OnClickListener, public UIView::OnLongPressListener { +public: + EventListener() = delete; + ~EventListener() override = default; + + EventListener(OnEventFunc onClick, OnEventFunc onLongPress) + { + onClick_ = std::move(onClick); + onLongPress_ = std::move(onLongPress); + } + + bool OnClick(UIView& view, const ClickEvent& event) override + { + if (!onClick_) { + return false; + } + return onClick_(view, event); + } + + bool OnLongPress(UIView& view, const LongPressEvent& event) override + { + if (!onLongPress_) { + return false; + } + return onLongPress_(view, event); + } + +private: + OnEventFunc onClick_ { nullptr }; + OnEventFunc onLongPress_ { nullptr }; +}; +} // namespace OHOS +#endif diff --git a/launcher/launcher/src/main/cpp/long_press_view.cpp b/launcher/launcher/src/main/cpp/long_press_view.cpp new file mode 100644 index 0000000000000000000000000000000000000000..421ebe52ebda62ea3410206b304ece3167dc7a02 --- /dev/null +++ b/launcher/launcher/src/main/cpp/long_press_view.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "long_press_view.h" +#include "ui_config.h" + +namespace OHOS { +LongPressView::LongPressView(UninstallApp uninstall) +{ + bStatus_ = false; + uninstall_ = uninstall; + viewGroup_ = new UIViewGroup(); + viewGroup_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Black())); + viewGroup_->SetStyle(STYLE_BORDER_RADIUS, GROUP_VIEW_RADIUS); + viewGroup_->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY); + + buttUninstall_ = new UILabelButton(); + buttUninstall_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Gray())); + buttUninstall_->SetStyle(STYLE_BORDER_RADIUS, BUTTON_RADIUS); + buttUninstall_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + buttUninstall_->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_RADIUS, UIButton::PRESSED); + buttUninstall_->SetStyleForState(STYLE_BACKGROUND_OPA, HALF_OPACITY, UIButton::PRESSED); + buttUninstall_->SetText("卸载"); + buttUninstall_->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + buttUninstall_->SetOnClickListener(this); + + buttCancle_ = new UILabelButton(); + buttCancle_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Gray())); + buttCancle_->SetStyle(STYLE_BORDER_RADIUS, BUTTON_RADIUS); + buttCancle_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + buttCancle_->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_RADIUS, UIButton::PRESSED); + buttCancle_->SetStyleForState(STYLE_BACKGROUND_OPA, HALF_OPACITY, UIButton::PRESSED); + buttCancle_->SetText("取消"); + buttCancle_->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + buttCancle_->SetOnClickListener(this); + + viewGroup_->Add(buttUninstall_); + viewGroup_->Add(buttCancle_); + viewGroup_->SetVisible(false); +} + +LongPressView::~LongPressView() +{ + DeleteChildren(viewGroup_); +} + +void LongPressView::RemoveLview() +{ + if (bStatus_ == false) { + return; + } + viewParent_->Remove(viewGroup_); + viewGroup_->SetVisible(false); + viewParent_->Invalidate(); + bStatus_ = false; +} + +void LongPressView::Show(UIViewGroup* viewParent, AppInfo* pApp) +{ + const int16_t HEIGHT_DISCOUNT = 3; + const int16_t WIDTH_DISCOUNT = 2; + bStatus_ = true; + viewParent_ = viewParent; + app_ = pApp; + viewGroup_->SetPosition(pApp->buttonXY_.x / WIDTH_DISCOUNT + pApp->button_->GetWidth(), + pApp->buttonXY_.y / WIDTH_DISCOUNT + pApp->button_->GetHeight(), pApp->button_->GetWidth(), + (pApp->button_->GetHeight() * WIDTH_DISCOUNT) / HEIGHT_DISCOUNT + pApp->button_->GetHeight() / WIDTH_DISCOUNT); + buttUninstall_->SetPosition(0, 0, + pApp->button_->GetWidth(), pApp->button_->GetHeight() / WIDTH_DISCOUNT); + buttCancle_->SetPosition(0, (pApp->button_->GetHeight() * WIDTH_DISCOUNT) / HEIGHT_DISCOUNT, + pApp->button_->GetWidth(), pApp->button_->GetHeight() / WIDTH_DISCOUNT); + viewGroup_->SetVisible(true); + viewParent_->Add(viewGroup_); + viewParent_->Invalidate(); +} + +bool LongPressView::OnClick(UIView& view, const ClickEvent& event) +{ + UILabelButton* lbutt = nullptr; + lbutt = static_cast(&view); + RemoveLview(); + if (&view == buttUninstall_) { + uninstall_(app_); + } + return true; +} +} // namespace OHOS \ No newline at end of file diff --git a/launcher/launcher/src/main/cpp/long_press_view.h b/launcher/launcher/src/main/cpp/long_press_view.h new file mode 100644 index 0000000000000000000000000000000000000000..1d231e60b612862a48710586bc6c10c89a934599 --- /dev/null +++ b/launcher/launcher/src/main/cpp/long_press_view.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_LISTENTER_H +#define OHOS_LISTENTER_H + +#include "app_info.h" +#include "view_group_page.h" +#include "app_manage.h" + +namespace OHOS { +class LongPressView : public UIView::OnClickListener, public NativeBase { +public: + LongPressView(UninstallApp uninstall); + virtual ~LongPressView(); + bool OnClick(UIView &view, const ClickEvent& event) override; + void RemoveLview(); + void Show(UIViewGroup* viewParent, AppInfo* pApp); + void SetStatus(bool status) + { + bStatus_ = status; + } + + bool GetStatus() + { + return bStatus_; + } + +private: + UIViewGroup* viewParent_ { nullptr }; + UIViewGroup* viewGroup_ { nullptr }; + UILabelButton* buttUninstall_ { nullptr }; + UILabelButton* buttCancle_ { nullptr }; + UninstallApp uninstall_ { nullptr }; + AppInfo* app_ { nullptr }; + bool bStatus_ { false }; +}; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/launcher/launcher/src/main/cpp/main_ability.cpp b/launcher/launcher/src/main/cpp/main_ability.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0609cb0570b988c81c47e327d4bd6eca74bff6b7 --- /dev/null +++ b/launcher/launcher/src/main/cpp/main_ability.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "main_ability.h" + +namespace OHOS { +REGISTER_AA(MainAbility) + +void MainAbility::OnStart(const Want& want) +{ + SetMainRoute("MainAbilitySlice"); + Ability::OnStart(want); +} + +void MainAbility::OnInactive() +{ + Ability::OnInactive(); +} + +void MainAbility::OnActive(const Want& want) +{ + Ability::OnActive(want); +} + +void MainAbility::OnBackground() +{ + Ability::OnBackground(); +} + +void MainAbility::OnStop() +{ + Ability::OnStop(); +} +} // namespace OHOS \ No newline at end of file diff --git a/launcher/launcher/src/main/cpp/main_ability.h b/launcher/launcher/src/main/cpp/main_ability.h new file mode 100644 index 0000000000000000000000000000000000000000..bab86e3b3c29a9d20708b58de28965bfc6785a57 --- /dev/null +++ b/launcher/launcher/src/main/cpp/main_ability.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_MAIN_ABILITY_H +#define OHOS_MAIN_ABILITY_H + +#include + +namespace OHOS { +class MainAbility : public Ability { +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; +}; +} // namespace OHOS +#endif diff --git a/launcher/launcher/src/main/cpp/main_ability_slice.cpp b/launcher/launcher/src/main/cpp/main_ability_slice.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b0f8d4923a8b272d1b3740d6e8a2b7a36c424f2 --- /dev/null +++ b/launcher/launcher/src/main/cpp/main_ability_slice.cpp @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2020 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. + */ + +#include +#include +#include +#include + +#include "main_ability_slice.h" +#include "ability_manager.h" + +namespace OHOS { +REGISTER_AS(MainAbilitySlice) + +MainAbilitySlice::~MainAbilitySlice() +{ + ReleaseView(); +} + +void MainAbilitySlice::ReleaseView() +{ + if (swipeView_) { + delete swipeView_; + swipeView_ = nullptr; + } + if (uiImageView_) { + delete uiImageView_; + uiImageView_ = nullptr; + } + if (lableHead_) { + delete lableHead_; + lableHead_ = nullptr; + } + if (lableTail_) { + delete lableTail_; + lableTail_ = nullptr; + } +} + +void MainAbilitySlice::SetHead() +{ + char tmp[TMP_BUF_SIZE] = { 0 }; + time_t t = time(nullptr); + struct tm* st = nullptr; + st = localtime(&t); + sprintf_s(tmp, sizeof(tmp), "%02d : %02d", st->tm_hour, st->tm_min); + UILabel* label = new UILabel(); + rootview_->Add(label); + label->SetPosition(0, 0, LA_HORIZONTAL_RESOLUTION, LABLE_TITLE_HEIGHT); + label->SetText(tmp); + label->SetAlign(TEXT_ALIGNMENT_RIGHT, TEXT_ALIGNMENT_TOP); + label->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + label->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + label->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + + lableHead_ = label; +} + +void MainAbilitySlice::SetTail() +{ + UILabel* label = new UILabel(); + rootview_->Add(label); + label->SetPosition(0, LA_VERTICAL_RESOLUTION - LABLE_TAIL_HEIGHT, LA_HORIZONTAL_RESOLUTION, LABLE_TAIL_HEIGHT); + char buf[TMP_BUF_SIZE] = { 0 }; + sprintf_s(buf, sizeof(buf), ".%d.", 1); + label->SetText(buf); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + label->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + label->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + label->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + + lableTail_ = label; +} + +void MainAbilitySlice::SetImageView() +{ + uiImageView_ = new UIImageView(); + // modify image view height + + uiImageView_->SetPosition(0, 0, LA_HORIZONTAL_RESOLUTION, LA_VERTICAL_RESOLUTION); + uiImageView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::White())); + uiImageView_->SetSrc(TABLE_BACKGROUND); + uiImageView_->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY); + rootview_->Add(uiImageView_); +} + +void MainAbilitySlice::SetSwipe() +{ + swipeView_ = new SwipeView(lableHead_, lableTail_); + swipeView_->OnSetUpView(); + rootview_->Add(swipeView_->GetSwipeView()); +} + +void MainAbilitySlice::OnStart(const Want& want) +{ + AbilitySlice::OnStart(want); + LA_HORIZONTAL_RESOLUTION = Screen::GetInstance().GetWidth(); + LA_VERTICAL_RESOLUTION = Screen::GetInstance().GetHeight(); + rootview_ = RootView::GetWindowRootView(); + rootview_->SetPosition(0, 0); + rootview_->Resize(LA_HORIZONTAL_RESOLUTION, LA_VERTICAL_RESOLUTION); + rootview_->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY); + rootview_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::GetColorFromRGB(0x30, 0x30, 0x30))); + + SetHead(); + SetTail(); + SetSwipe(); + SetUIContent(rootview_); + rootview_->Invalidate(); +} + +void MainAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void MainAbilitySlice::OnActive(const Want& want) +{ + AbilitySlice::OnActive(want); +} + +void MainAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void MainAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS diff --git a/launcher/launcher/src/main/cpp/main_ability_slice.h b/launcher/launcher/src/main/cpp/main_ability_slice.h new file mode 100644 index 0000000000000000000000000000000000000000..c34caac866c766fb35f58fb8df0d8932b4da6a92 --- /dev/null +++ b/launcher/launcher/src/main/cpp/main_ability_slice.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_MAIN_ABILITY_SLICE_H +#define OHOS_MAIN_ABILITY_SLICE_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "event_listener.h" +#include "swipe_view.h" +#include "ui_config.h" + +namespace OHOS { +class MainAbilitySlice : public AbilitySlice { +public: + MainAbilitySlice() = default; + ~MainAbilitySlice() override; + +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; + void SetHead(); + void SetTail(); + void SetSwipe(); + void ReleaseView(); + void SetImageView(); + +private: + SwipeView* swipeView_ { nullptr }; + UIImageView* uiImageView_ { nullptr }; + UILabel* lableHead_ { nullptr }; + UILabel* lableTail_ { nullptr }; + RootView *rootview_ { nullptr }; +}; +} // namespace OHOS +#endif diff --git a/launcher/launcher/src/main/cpp/native_base.h b/launcher/launcher/src/main/cpp/native_base.h new file mode 100644 index 0000000000000000000000000000000000000000..d88dfe7240e2df36674fbad8baa715b75d9f4bda --- /dev/null +++ b/launcher/launcher/src/main/cpp/native_base.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_NATIVE_BASE_H +#define OHOS_NATIVE_BASE_H + +#include + +namespace OHOS { +class NativeBase : public HeapBase { +public: + void DeleteChildren(UIView *view) + { + if (view == nullptr) { + return; + } + while (view) { + UIView *tempView = view; + view = view->GetNextSibling(); + if (tempView->IsViewGroup()) { + DeleteChildren(static_cast(tempView)->GetChildrenHead()); + } + if (tempView->GetParent()) { + static_cast(tempView->GetParent())->Remove(tempView); + } + delete tempView; + } + } +}; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/launcher/launcher/src/main/cpp/swipe_view.cpp b/launcher/launcher/src/main/cpp/swipe_view.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46ffbbe32594c736e329815ebe939d3bb7eb81e3 --- /dev/null +++ b/launcher/launcher/src/main/cpp/swipe_view.cpp @@ -0,0 +1,230 @@ +/* + * Copyright (c) 2020 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. + */ + +#include + +#include "swipe_view.h" + +namespace OHOS { +AppEvent* AppEvent::appEvent_ = { nullptr }; +SwipeView* AppEvent::nativeView_ = { nullptr }; +static constexpr int32_t PERIOD_TIME = 60 * 1000; // 60 seconds + +SwipeView::SwipeView(UILabel* titlellable, UILabel* taillable) +{ + lableTitle_ = titlellable; + lableTail_ = taillable; + groupCount_ = 0; + for (int i = 0; i < MAX_VIEWGROUP; i++) { + arrPage_[i] = nullptr; + } + arrViewListener_ = nullptr; + swipeLisstener_ = nullptr; + appManage_ = new AppManage(); + lpView_ = new LongPressView(AppEvent::UninstallApp); + Task::Init(); + Task::SetPeriod(PERIOD_TIME); +} + +SwipeView::~SwipeView() +{ + OnStop(); +} + +void SwipeView::SetUpSwipe() +{ + LA_HORIZONTAL_RESOLUTION = Screen::GetInstance().GetWidth(); + LA_VERTICAL_RESOLUTION = Screen::GetInstance().GetHeight(); + swipe_ = new UISwipeView(); + swipe_->SetPosition(0, LABLE_TITLE_HEIGHT, LA_HORIZONTAL_RESOLUTION, + LA_VERTICAL_RESOLUTION - LABLE_TITLE_HEIGHT - LABLE_TAIL_HEIGHT); + swipe_->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + swipe_->SetLoopState(true); + swipe_->SetAnimatorTime(20); // set swipe view animator time 20s +} + +UIViewGroup* SwipeView::AddViewGroup() +{ + if (groupCount_ >= MAX_VIEWGROUP) { + return nullptr; + } + UIViewGroup* viewGroup = new UIViewGroup(); + viewGroup->SetPosition(0, LABLE_TITLE_HEIGHT, LA_HORIZONTAL_RESOLUTION, + LA_VERTICAL_RESOLUTION - LABLE_TITLE_HEIGHT - LABLE_TAIL_HEIGHT); + viewGroup->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + groupCount_++; + ViewGroupPage* page = new ViewGroupPage(viewGroup); + arrPage_[groupCount_ - 1] = page; + swipe_->Add(viewGroup); + return viewGroup; +} + +UIViewGroup* SwipeView::AddFirstViewGroup() +{ + UIViewGroup* firstView = new UIViewGroup(); + firstView->SetPosition(0, LABLE_TITLE_HEIGHT, LA_HORIZONTAL_RESOLUTION, + LA_VERTICAL_RESOLUTION - LABLE_TITLE_HEIGHT - LABLE_TAIL_HEIGHT); + firstView->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + + UIViewGroup* viewTimeWeather = new UIViewGroup(); + // 2: set first view to 2 piece + viewTimeWeather->SetPosition(0, 0, firstView->GetWidth() / 2, firstView->GetHeight()); + viewTimeWeather->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + timeWeatherView_ = new TimeWeatherView(viewTimeWeather); + timeWeatherView_->SetUpView(); + firstView->Add(viewTimeWeather); + + UIViewGroup* viewGroup = new UIViewGroup(); + // 2 : get left && right view width and height + viewGroup->SetPosition(firstView->GetWidth() / 2, 0, firstView->GetWidth() / 2, firstView->GetHeight()); + viewGroup->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + firstView->Add(viewGroup); + groupCount_++; + ViewGroupPage *page = new ViewGroupPage(viewGroup); + arrPage_[groupCount_ - 1] = page; + + swipe_->Add(firstView); + firstView->Invalidate(); + Task::TaskExecute(); + return firstView; +} + +void SwipeView::OnSetUpView() +{ + SetUpSwipe(); + swipeLisstener_ = new SwipeLisstener(lpView_, swipe_, lableTail_); + swipe_->SetOnSwipeListener(swipeLisstener_); + arrViewListener_ = new ViewPageListener(lpView_); + swipe_->SetOnClickListener(arrViewListener_); + + AddFirstViewGroup(); + AddViewGroup(); + AddViewGroup(); + // Reserved. Touch and hold to add a page. + arrPage_[0]->SetMatrix(APP_ROW_COUNT, APP_COL_COUNT); + arrPage_[0]->SetScale(0.6); // 0.6 blank/icon width + for (int16_t i = 1; i < groupCount_; i++) { + arrPage_[i]->SetMatrix(APP_ROW_COUNT, 2 * APP_COL_COUNT); // 2 scale of first view's col count + arrPage_[i]->SetScale(0.69); // 0.69 blank/icon width + } + AppEvent::GetInstance(this); + AppManage::SetViewGroup(AppEvent::ClickEvent, AppEvent::LongPressEvent, arrPage_, groupCount_); + BundleInfo* pBundleInfos = nullptr; + int count = 0; + if (appManage_->LauncherApp(&pBundleInfos, count)) { + for (int j = 0; j < count; j++) { + for (int i = 0; i < groupCount_; i++) { + if (memcmp(LAUNCHER_BUNDLE_NAME, pBundleInfos[j].bundleName, strlen(pBundleInfos[j].bundleName)) == 0) { + break; + } + AppInfo* app = new AppInfo(); + app->funcclick_ = AppEvent::ClickEvent; + app->funclPress_ = AppEvent::LongPressEvent; + if (pBundleInfos[j].bundleName) { + memcpy_s(app->appName_, sizeof(app->appName_), pBundleInfos[j].bundleName, + strlen(pBundleInfos[j].bundleName)); + app->appName_[strlen(pBundleInfos[j].bundleName)] = 0; + } + if (pBundleInfos[j].abilityInfos[0].name) { + memcpy_s(app->abilityName_, sizeof(app->abilityName_), pBundleInfos[j].abilityInfos[0].name, + strlen(pBundleInfos[j].abilityInfos[0].name)); + app->abilityName_[strlen(pBundleInfos[j].abilityInfos[0].name)] = 0; + } + if (pBundleInfos[j].bigIconPath) { + memcpy_s(app->appIconDir_, sizeof(app->appIconDir_), pBundleInfos[j].bigIconPath, + strlen(pBundleInfos[j].bigIconPath)); + app->appIconDir_[strlen(pBundleInfos[j].bigIconPath)] = 0; + } + if (arrPage_[i]->AddApp(app)) { + break; + } + } + } + } + swipe_->SetCurrentPage(0); +} + +void SwipeView::StartApp(AppInfo* app) +{ + if (lpView_->GetStatus() == true) { + lpView_->RemoveLview(); + return; + } + appManage_->StartApp(app); +} + +void SwipeView::ShowLongPressView(AppInfo* app) +{ + lpView_->Show(static_cast(app->button_->GetParent()), app); +} + +void SwipeView::UninstallApp(AppInfo* app) +{ + if (appManage_->UnInstallApp(app)) { + for (int16_t i = 0; i < groupCount_; i++) { + if (arrPage_[i]) { + if (arrPage_[i]->RemoveApp(app->appName_)) { + swipe_->Invalidate(); + return; + } + } + } + } +} + +void SwipeView::InstallApp(AppInfo* app) +{ + appManage_->InstallApp(app); + AppInfo* pApp = new AppInfo(); + app->funcclick_ = AppEvent::ClickEvent; + app->funclPress_ = AppEvent::LongPressEvent; + for (int16_t i = 0; i < groupCount_; i++) { + if (arrPage_[i]->AddApp(pApp)) { + break; + } + } +} + +void SwipeView::OnStop() +{ + if (lpView_) { + delete lpView_; + lpView_ = nullptr; + } + if (appManage_) { + delete appManage_; + appManage_ = nullptr; + } + if (arrViewListener_) { + delete arrViewListener_; + arrViewListener_ = nullptr; + } + if (swipeLisstener_) { + delete swipeLisstener_; + swipeLisstener_ = nullptr; + } + if (timeWeatherView_) { + delete timeWeatherView_; + timeWeatherView_ = nullptr; + } + for (int i = 0; i < MAX_VIEWGROUP; i++) { + if (arrPage_[i]) { + delete arrPage_[i]; + arrPage_[i] = nullptr; + } + } + DeleteChildren(swipe_); +} +} // namespace OHOS diff --git a/launcher/launcher/src/main/cpp/swipe_view.h b/launcher/launcher/src/main/cpp/swipe_view.h new file mode 100644 index 0000000000000000000000000000000000000000..9e8c81392abf6536cc73b62f756f5b51e3d5d24a --- /dev/null +++ b/launcher/launcher/src/main/cpp/swipe_view.h @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_SWIPE_VIEW_H +#define OHOS_SWIPE_VIEW_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ui_config.h" +#include "app_info.h" +#include "view_group_page.h" +#include "native_base.h" +#include "long_press_view.h" +#include "app_manage.h" +#include "time_weather_view.h" + +namespace OHOS { +class ViewPageListener : public UIView::OnClickListener { +public: + ViewPageListener(LongPressView* view) : view_(view) {} + virtual ~ViewPageListener() {} + bool OnClick(UIView& view, const ClickEvent& event) override + { + view_->RemoveLview(); + return true; + } + +private: + LongPressView *view_ { nullptr }; +}; + +class SwipeLisstener : public UISwipeView::OnSwipeListener { +public: + SwipeLisstener(LongPressView* view, UISwipeView* swipe, UILabel* lable) + : view_(view), swipe_(swipe), lable_(lable) {}; + ~SwipeLisstener() {}; + virtual void OnSwipe(UISwipeView& view) override + { + char buf[TMP_BUF_SIZE] = { 0 }; + sprintf_s(buf, sizeof(buf), ".%d.", swipe_->GetCurrentPage() + 1); + lable_->SetText(buf); + view_->RemoveLview(); + } + +private: + UISwipeView* swipe_ { nullptr }; + UILabel* lable_ { nullptr }; + LongPressView* view_ { nullptr }; +}; + +class SwipeView : public Task, public NativeBase { +public: + SwipeView() = delete; + SwipeView(UILabel* titlellable, UILabel* taillable); + virtual ~SwipeView(); + void OnSetUpView(); + void StartApp(AppInfo* app); + void ShowLongPressView(AppInfo* app); + void UninstallApp(AppInfo* app); + void InstallApp(AppInfo* app); + void Callback() override + { + char tmp[TMP_BUF_SIZE] = { 0 }; + time_t t = time(nullptr); + struct tm* st = localtime(&t); + sprintf_s(tmp, sizeof(tmp), "%02d : %02d", st->tm_hour, st->tm_min); + lableTitle_->SetText(tmp); + timeWeatherView_->SetUpTimeView(); + } + + UISwipeView* GetSwipeView() + { + return swipe_; + } + +private: + void OnStop(); + void SetUpSwipe(); + UIViewGroup* AddViewGroup(); + UIViewGroup* AddFirstViewGroup(); + ViewGroupPage* arrPage_[MAX_VIEWGROUP] { nullptr }; + UISwipeView* swipe_ { nullptr }; + UILabel* lableTitle_ { nullptr }; // view title time label + UILabel* lableTail_ { nullptr }; + int groupCount_ { 0 }; + ViewPageListener* arrViewListener_ { nullptr }; + SwipeLisstener* swipeLisstener_ { nullptr }; + AppManage* appManage_ { nullptr }; + LongPressView* lpView_ { nullptr }; + TimeWeatherView* timeWeatherView_ {nullptr}; +}; + +class AppEvent { +public: + static AppEvent* GetInstance(SwipeView* nativeView) + { + if (appEvent_ == nullptr) { + appEvent_ = new AppEvent(); + nativeView_ = nativeView; + } + return appEvent_; + } + // app click + + static bool ClickEvent(AppInfo* app) + { + nativeView_->StartApp(app); + return true; + } + // app long press show window + + static bool LongPressEvent(AppInfo* app) + { + nativeView_->ShowLongPressView(app); + return true; + } + // app uninstall click + + static bool UninstallApp(AppInfo* app) + { + nativeView_->UninstallApp(app); + return true; + } + // none install app used appmanage::InstallApp, this function is invailate + + static bool InstallApp(AppInfo* app) + { + nativeView_->InstallApp(app); + return true; + } + +private: + AppEvent() {} + ~AppEvent() + { + if (appEvent_) { + delete appEvent_; + appEvent_ = nullptr; + } + } + +private: + static AppEvent* appEvent_; + static SwipeView* nativeView_; +}; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/launcher/launcher/src/main/cpp/time_weather_view.cpp b/launcher/launcher/src/main/cpp/time_weather_view.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2737052373fe1c136d6e256b7f6fbc6b68073df8 --- /dev/null +++ b/launcher/launcher/src/main/cpp/time_weather_view.cpp @@ -0,0 +1,199 @@ +/* + * Copyright (c) 2020 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. + */ + +#include +#include + +#include "time_weather_view.h" + +namespace OHOS { +static constexpr int16_t DISPLATE_PICESE = 2; +static constexpr int16_t BLANK_H = 5; +static constexpr int16_t BLANK_TW = 15; +static constexpr int16_t BLANK_W = 100; +static constexpr int16_t BIGLABEL_H = 100; +static constexpr int16_t SMALLLABEL_H = 35; +static constexpr int16_t IMAGE_H = 40; +static constexpr int16_t IMAGE_W = 40; + +TimeWeatherView::TimeWeatherView(UIViewGroup* viewGroup) +{ + viewGroup_ = viewGroup; +} +TimeWeatherView::~TimeWeatherView() +{ + // todo other release +} + +void TimeWeatherView::SetStyle(Style sty) +{ + viewGroup_->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + viewGroup_->Invalidate(); +} + +void TimeWeatherView::SetPosion(int16_t x, int16_t y, int16_t height, int16_t width) +{ + viewGroup_->SetPosition(x, y, width, height); +} + +void TimeWeatherView::SetUpView() +{ + SetUpTimeView(); + SetUpWeatherView(); + viewGroup_->Invalidate(); +} + +void TimeWeatherView::SetUpTimeView() +{ + char hour_min[TMP_BUF_SIZE] = { 0 }; + char mont_day[TMP_BUF_SIZE] = { 0 }; + char week_day[TMP_BUF_SIZE] = { 0 }; + char date[TMP_BUF_SIZE] = { 0 }; + const int16_t january = 1; + const int16_t commonYear = 1970; + time_t t = time(nullptr); + struct tm* st = localtime(&t); + sprintf_s(hour_min, sizeof(hour_min), "%02d : %02d", st->tm_hour, st->tm_min); + sprintf_s(mont_day, sizeof(mont_day), "%02d月%02d日", st->tm_mon + january, st->tm_mday); + GetWeekdayByYearday(st->tm_year + commonYear, st->tm_mon + january, st->tm_mday, week_day, sizeof(week_day)); + sprintf_s(date, sizeof(date), "%s %s", mont_day, week_day); + if (viewTime_ == nullptr) { + viewTime_ = new UIViewGroup(); + viewTime_->SetPosition(BLANK_TW, BLANK_H, viewGroup_->GetWidth() - BLANK_W, + viewGroup_->GetHeight() / DISPLATE_PICESE - SMALLLABEL_H); + viewTime_->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + UILabel* lable = new UILabel(); + lable->SetPosition(BLANK_TW, BLANK_H, viewTime_->GetWidth(), BIGLABEL_H); + lable->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_BOTTOM); + lable->SetText(hour_min); + lable->SetFont(FOND_PATH, BIGLAUNCHER_FOND_ID); + lable->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + lable->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS); + lable->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + lable->SetViewId("labletime"); + + UILabel* lable2 = new UILabel(); + lable2->SetPosition(BLANK_TW, BLANK_H + BIGLABEL_H + BLANK_H, viewTime_->GetWidth(), SMALLLABEL_H); + lable2->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_TOP); + lable2->SetText(date); + lable2->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + + lable2->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + lable2->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS); + lable2->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + lable2->SetViewId("labledate"); + + viewTime_->Add(lable); + viewTime_->Add(lable2); + viewGroup_->Add(viewTime_); + } else { + static_cast(viewTime_->GetChildById("labletime"))->SetText(hour_min); + static_cast(viewTime_->GetChildById("labledate"))->SetText(date); + viewTime_->Invalidate(); + } +} + +void TimeWeatherView::GetWeekdayByYearday(int iY, int iM, int iD, char* date, int size) +{ + const int16_t months = 12; + const int16_t january = 1; + const int16_t february = 2; + const int16_t oneHundred = 100; + const int16_t fourHundred = 400; + int iWeekDay = -1; + if (january == iM || february == iM) { + iM += months; + iY--; + } + // 1 : MONDAY_LAUNCHER, 2 : TUESDAY_LAUNCHER, 3 : WEDNESDAY_LAUNCHER, 4 : , 5 : ect + iWeekDay = (iD + 1 + 2 * iM + 3 * (iM + 1) / 5 + iY + iY / 4 - iY / oneHundred + iY / fourHundred) % WEEKEND_LAUNCHER; + switch (iWeekDay) { + case SUNDAY_LAUNCHER: + memcpy_s(date, size, "星期天", strlen("星期天")); + date[strlen("星期天")] = 0; + break; + case MONDAY_LAUNCHER: + memcpy_s(date, size, "星期一", strlen("星期一")); + date[strlen("星期一")] = 0; + break; + case TUESDAY_LAUNCHER: + memcpy_s(date, size, "星期二", strlen("星期二")); + date[strlen("星期二")] = 0; + break; + case WEDNESDAY_LAUNCHER: + memcpy_s(date, size, "星期三", strlen("星期三")); + date[strlen("星期三")] = 0; + break; + case THURSDAY_LAUNCHER: + memcpy_s(date, size, "星期四", strlen("星期四")); + date[strlen("星期四")] = 0; + break; + case FRIDAY_LAUNCHER: + memcpy_s(date, size, "星期五", strlen("星期五")); + date[strlen("星期五")] = 0; + break; + case STAURDAY_LAUNCHER: + memcpy_s(date, size, "星期六", strlen("星期六")); + date[strlen("星期六")] = 0; + break; + default: + memcpy_s(date, size, "星期天", strlen("星期天")); + date[strlen("星期天")] = 0; + break; + } + return; +} + +void TimeWeatherView::SetUpWeatherView() +{ + const int16_t countTimes = 6; + viewweather_ = new UIViewGroup(); + viewweather_->SetPosition(BLANK_W, viewGroup_->GetHeight() / DISPLATE_PICESE - SMALLLABEL_H, + viewGroup_->GetWidth() / DISPLATE_PICESE + BLANK_TW, DISPLATE_PICESE * (BLANK_H + SMALLLABEL_H) + BLANK_H); + viewweather_->SetStyle(STYLE_BACKGROUND_OPA, HALF_OPACITY); + viewweather_->SetStyle(STYLE_BORDER_RADIUS, GROUP_VIEW_RADIUS); + viewweather_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Gray())); + + UIImageView* uiImageView = new UIImageView(); + uiImageView->SetPosition(BLANK_TW, BLANK_H * countTimes, IMAGE_W, IMAGE_H); + uiImageView->SetSrc(RES_WEATHER); + uiImageView->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY); + + UILabel* lable = new UILabel(); + lable->SetPosition(BLANK_TW + IMAGE_W, BLANK_H, + viewweather_->GetWidth() - IMAGE_W - BLANK_TW - BLANK_TW - BLANK_TW, SMALLLABEL_H); + lable->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER); + lable->SetText("室内温度 26℃"); + lable->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + lable->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + lable->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS); + lable->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + + UILabel* lable2 = new UILabel(); + lable2->SetPosition(BLANK_TW + IMAGE_W, SMALLLABEL_H + BLANK_H + BLANK_H, + viewweather_->GetWidth() - IMAGE_W - BLANK_TW - BLANK_TW - BLANK_TW, SMALLLABEL_H); + lable2->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER); + lable2->SetText("空气污染指数 136"); + lable2->SetFont(FOND_PATH, LAUNCHER_FOND_ID); + lable2->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White())); + lable2->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS); + lable2->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY); + + viewweather_->Add(uiImageView); + viewweather_->Add(lable); + viewweather_->Add(lable2); + viewGroup_->Add(viewweather_); +} +} // namespace OHOS diff --git a/launcher/launcher/src/main/cpp/time_weather_view.h b/launcher/launcher/src/main/cpp/time_weather_view.h new file mode 100644 index 0000000000000000000000000000000000000000..c572ea75b273dbe76d1e61ec3771fb1c721a766f --- /dev/null +++ b/launcher/launcher/src/main/cpp/time_weather_view.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_TIME_WEATHER_H +#define OHOS_TIME_WEATHER_H + +#include +#include +#include +#include + +#include "app_info.h" +#include "native_base.h" +#include "ui_config.h" + +namespace OHOS { +enum DATE_LAUNCHER { + SUNDAY_LAUNCHER = 0, + MONDAY_LAUNCHER, + TUESDAY_LAUNCHER, + WEDNESDAY_LAUNCHER, + THURSDAY_LAUNCHER, + FRIDAY_LAUNCHER, + STAURDAY_LAUNCHER, + WEEKEND_LAUNCHER +}; + +class TimeWeatherView : public NativeBase { +public: + TimeWeatherView() = delete; + TimeWeatherView(UIViewGroup* viewGroup); + virtual ~TimeWeatherView(); + void SetStyle(Style sty); + void SetPosion(int16_t width, int16_t height, int16_t x, int16_t y); + void SetUpView(); + void SetUpTimeView(); + +protected: + void SetUpWeatherView(); + void GetWeekdayByYearday(int iY, int iM, int iD, char* date, int size); + +private: + UIViewGroup* viewTime_ { nullptr }; + UIViewGroup* viewweather_ { nullptr }; + UIViewGroup* viewGroup_ { nullptr }; +}; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/launcher/launcher/src/main/cpp/ui_config.h b/launcher/launcher/src/main/cpp/ui_config.h new file mode 100644 index 0000000000000000000000000000000000000000..32062ad30ac09a9cb03070f83390f72a7777803a --- /dev/null +++ b/launcher/launcher/src/main/cpp/ui_config.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_UI_CONFIG_H +#define OHOS_UI_CONFIG_H + +#include + +namespace OHOS { +static int16_t LA_HORIZONTAL_RESOLUTION = 960; // launcher table width +static int16_t LA_VERTICAL_RESOLUTION = 480; // launcher table height +static constexpr int16_t LABLE_TITLE_HEIGHT = 30; // tail lable height +static constexpr int16_t LABLE_TAIL_HEIGHT = 30; +static constexpr int16_t APP_WIDTH_COUNT = 7; // blank + app + blank + app + blank +static constexpr int16_t APP_HEIGHT_COUNT = 4; // app + lable + app + lable +static constexpr int16_t MAX_VIEWGROUP = 3; // swipe window count +static constexpr int16_t APP_ROW_COUNT = 2; // a swipe view app count in row +static constexpr int16_t APP_COL_COUNT = 3; // a sswipe view app count in col +static constexpr int16_t LAUNCHER_FOND_ID = 16; // other view fond id +static constexpr int16_t APP_FOND_ID = 16; // app name fond id +static constexpr int16_t BIGLAUNCHER_FOND_ID = 48; // time big fond id +static constexpr int16_t TOTAL_OPACITY = 0; // transparent +static constexpr int16_t HALF_OPACITY = 50; // diaphanous +static constexpr int16_t UN_OPACITY = 255; // opaque +static constexpr int16_t BUTTON_RADIUS = 20; // app icon radius +static constexpr int16_t LABLE_RADIUS = 0; // lable icon radius +static constexpr int16_t TITLE_LABLE_OPACITY = 255; // translucent +static constexpr int16_t GROUP_VIEW_RADIUS = 20; // view radius +#define TMP_BUF_SIZE 128 +#define LAUNCHER_BUNDLE_NAME "com.huawei.launcher" +#define TABLE_BACKGROUND \ + "/storage/app/run/com.huawei.launcher/launcher/assets/launcher/resources/base/media/background.png" +#define RES_WEATHER "/storage/app/run/com.huawei.launcher/launcher/assets/launcher/resources/base/media/weather.png" +#define FOND_PATH "SourceHanSansSC-Regular.otf" +} // namespace OHOS +#endif diff --git a/launcher/launcher/src/main/cpp/view_group_page.cpp b/launcher/launcher/src/main/cpp/view_group_page.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2fa92315b88acd8dfd83d4b84e958637344508a --- /dev/null +++ b/launcher/launcher/src/main/cpp/view_group_page.cpp @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "view_group_page.h" +#include "ui_config.h" + +namespace OHOS { +ViewGroupPage::ViewGroupPage(UIViewGroup* viewGroup) +{ + viewGroup_ = viewGroup; +} + +ViewGroupPage::~ViewGroupPage() +{ + ListNode* app = appInfo_.Begin(); + while (app != appInfo_.End()) { + delete app->data_; + app = app->next_; + } + appInfo_.Clear(); + if (row_col_) { + delete[] row_col_; + } +} + +bool ViewGroupPage::IsFull(int16_t& row, int16_t& col) +{ + for (int16_t i = 0; i < row_; i++) { + for (int16_t j = 0; j < col_; j++) { + if (row_col_[i][j] == false) { + row = i; + col = j; + return false; + } + } + } + return true; +} + +void ViewGroupPage::SetStyle(Style sty) +{ + viewGroup_->SetStyle(sty); + viewGroup_->Invalidate(); +} + +void ViewGroupPage::SetPosion(int16_t width, int16_t height, int16_t x, int16_t y) +{ + viewGroup_->SetPosition(x, y, width, height); +} + +void ViewGroupPage::SetScale(double scale) +{ + scale_ = scale; +} + +void ViewGroupPage::SetMatrix(int16_t rows, int16_t cols) +{ + row_col_ = new bool* [rows]; + for (int i = 0; i < rows; i++) { + row_col_[i] = new bool[cols](); + } + row_ = rows; + col_ = cols; +} + +void ViewGroupPage::CalculateAppPosition(AppInfo* pAppInfo, int16_t row, int16_t col) +{ + int16_t h = viewGroup_->GetHeight(); + int16_t w = viewGroup_->GetWidth(); + + const double scale = scale_; + const int16_t blank1 = 10; + const int16_t blank2 = 30; + const int16_t labelH = 2; + int16_t width = static_cast(static_cast(w) / static_cast(scale * col_ + col_ + scale)); + int16_t heightB = width; + int16_t heightL = heightB / labelH; + int16_t xB = scale * width + (scale + 1) * width * col; + int16_t yB = blank1 + (blank2 + heightL + heightB) * row; + int16_t xL = xB; + int16_t yL = yB + heightB + blank1; + + pAppInfo->buttonXY_.x = xB; + pAppInfo->buttonXY_.y = yB; + pAppInfo->buttonHV_.x = width; + pAppInfo->buttonHV_.y = heightB; + + pAppInfo->lableXY_.x = xL; + pAppInfo->lableXY_.y = yL; + pAppInfo->lableHV_.x = width; + pAppInfo->lableHV_.y = heightL; +} + +void ViewGroupPage::SetUpApp(AppInfo *pAppInfo) +{ + UILabelButton *button = new UILabelButton(); + UILabel *lable = new UILabel(); + lable->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Red())); + lable->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY); + pAppInfo->SetButton(button); + pAppInfo->SetLable(lable); + + pAppInfo->SetListener(pAppInfo); + viewGroup_->Add(button); + viewGroup_->Add(lable); + viewGroup_->Invalidate(); +} + +bool ViewGroupPage::AddApp(AppInfo* pAppInfo) +{ + int16_t row = 0; + int16_t col = 0; + + if (FindApp(pAppInfo)) { + return true; + } + + if (IsFull(row, col)) { + return false; + } + + pAppInfo->SetLocation(row, col); + CalculateAppPosition(pAppInfo, row, col); + + SetUpApp(pAppInfo); + appInfo_.PushBack(pAppInfo); + row_col_[row][col] = true; + return true; +} + +bool ViewGroupPage::FindApp(AppInfo* pApp) +{ + ListNode* app = appInfo_.Begin(); + while (app != appInfo_.End()) { + if (memcmp(app->data_->appName_, pApp->appName_, strlen(pApp->appName_)) == 0) { + return true; + } + app = app->next_; + } + return false; +} + +bool ViewGroupPage::RemoveApp(const char* pAppName) +{ + ListNode* app = appInfo_.Begin(); + while (app != appInfo_.End()) { + if (memcmp(app->data_->appName_, pAppName, strlen(pAppName)) == 0) { + row_col_[app->data_->row_col_.x][app->data_->row_col_.y] = false; + viewGroup_->Remove(app->data_->button_); + viewGroup_->Remove(app->data_->lable_); + viewGroup_->Invalidate(); + appInfo_.Remove(app); + return true; + } + app = app->next_; + } + return false; +} +} // namespace OHOS diff --git a/launcher/launcher/src/main/cpp/view_group_page.h b/launcher/launcher/src/main/cpp/view_group_page.h new file mode 100644 index 0000000000000000000000000000000000000000..a26c501127ae79c12d8d053a543a011dd43b6a9a --- /dev/null +++ b/launcher/launcher/src/main/cpp/view_group_page.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_VIEWGROUP_PAGE_H +#define OHOS_VIEWGROUP_PAGE_H + +#include +#include +#include +#include + +#include "ui_config.h" +#include "app_info.h" +#include "native_base.h" + +namespace OHOS { +class ViewGroupPage { +public: + ViewGroupPage() = delete; + ViewGroupPage(UIViewGroup* viewGroup); + virtual ~ViewGroupPage(); + void SetMatrix(int16_t rows, int16_t cols); + bool AddApp(AppInfo* pAppInfo); + bool RemoveApp(const char* appName); + bool FindApp(AppInfo* pAppInfo); + void SetScale(double scale); + +protected: + void SetPosion(int16_t width, int16_t height, int16_t x = 0, int16_t y = 0); + void SetStyle(Style sty); + bool IsFull(int16_t& row, int16_t& col); + void SetUpApp(AppInfo* pAppInfo); + void CalculateAppPosition(AppInfo* pAppInfo, int16_t row, int16_t col); + +private: + List appInfo_; + UIViewGroup* viewGroup_ { nullptr }; + bool** row_col_ { nullptr }; + int16_t row_ { 0 }; + int16_t col_ { 0 }; + double scale_ { 0.0 }; +}; +} // namespace OHOS +#endif \ No newline at end of file diff --git a/launcher/launcher/src/main/resources/base/media/background.png b/launcher/launcher/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..f0da5425f6ebfc4527fa9cf41fc147da99cef61a Binary files /dev/null and b/launcher/launcher/src/main/resources/base/media/background.png differ diff --git a/launcher/launcher/src/main/resources/base/media/weather.png b/launcher/launcher/src/main/resources/base/media/weather.png new file mode 100644 index 0000000000000000000000000000000000000000..44496372be2d1a4518a5550276b3317b4e9c3728 Binary files /dev/null and b/launcher/launcher/src/main/resources/base/media/weather.png differ diff --git a/setting/BUILD.gn b/setting/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..9e2e3409b5cf12d183ae3c4fdfd641af662a4a12 --- /dev/null +++ b/setting/BUILD.gn @@ -0,0 +1,88 @@ +# Copyright (c) 2020 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. + +import("//build/lite/config/component/lite_component.gni") +import("//build/lite/config/hap_pack.gni") + +shared_library("setting") { + sources = [ + "setting/src/main/cpp/setting_main_ability.cpp", + "setting/src/main/cpp/main_ability_slice.cpp", + "setting/src/main/cpp/setting_about_ability_slice.cpp", + "setting/src/main/cpp/setting_wifi_ability_slice.cpp", + "setting/src/main/cpp/setting_wifi_input_password_ability_slice.cpp", + "setting/src/main/cpp/setting_utils.cpp", + "setting/src/main/cpp/app_info_ability_slice.cpp", + "setting/src/main/cpp/app_ability_slice.cpp", + "setting/src/main/cpp/wpa_work.c" + ] + + deps = [ + "//foundation/aafwk/frameworks/ability_lite:aafwk_abilitykit_lite", + "//foundation/appexecfwk/frameworks/bundle_lite:bundle", + "//foundation/communication/frameworks/ipc_lite:liteipc_adapter", + "//foundation/distributedschedule/services/samgr_lite/samgr:samgr", + "//utils/native/lite/kv_store:kv_store", + "//foundation/graphic/lite/frameworks/ui:ui", + "//foundation/graphic/lite/utils:graphic_utils", + "//foundation/graphic/lite/frameworks/surface", + "//third_party/wpa_supplicant/wpa_supplicant-2.9:wpa_supplicant", + "//base/startup/frameworks/syspara_lite/parameter:parameter" + ] + + include_dirs = [ + "setting/src/main/cpp", + "//foundation/aafwk/interfaces/kits/ability_lite", + "//foundation/graphic/lite/interfaces/kits/ui/components", + "//foundation/graphic/lite/interfaces/kits/ui", + "//foundation/graphic/lite/interfaces/kits/utils", + "//foundation/graphic/lite/interfaces/kits/config", + "//foundation/appexecfwk/interfaces/kits/bundle_lite", + "//foundation/aafwk/interfaces/kits/want_lite", + "//base/startup/interfaces/kits/syspara_lite", + "//base/security/interfaces/kits/iam_lite", + "//third_party/wpa_supplicant/wpa_supplicant-2.9/src/common" + ] + + ldflags = [ + "-lwpa", + "-lwpa_client", + "-lsysparam", + "-lpms_client" + ] + + defines = [ + "ENABLE_WINDOW=1", + "ABILITY_WINDOW_SUPPORT", + "OHOS_APPEXECFWK_BMS_BUNDLEMANAGER", + ] +} + +lite_component("setting_component") { + + features = [ + ":setting", + ] +} + +hap_pack("setting_hap") +{ + deps = [":setting"] + mode = "hap" + json_path = "setting/src/main/config.json" + ability_so_path = "$root_out_dir/libsetting.so" + force = "true" + cert_profile = "cert/com.huawei.setting_HarmonyAppProvision_release.p7b" + resources_path = "setting/src/main/resources" + hap_name = "setting" +} diff --git a/setting/cert/com.huawei.setting_HarmonyAppProvision_release.p7b b/setting/cert/com.huawei.setting_HarmonyAppProvision_release.p7b new file mode 100644 index 0000000000000000000000000000000000000000..7892e1e915a561a1da777362c41972481bb88c1a Binary files /dev/null and b/setting/cert/com.huawei.setting_HarmonyAppProvision_release.p7b differ diff --git a/setting/setting/src/main/config.json b/setting/setting/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..c0de2bb4903a31c4809992fc8629d7c8d1f6aeb3 --- /dev/null +++ b/setting/setting/src/main/config.json @@ -0,0 +1,40 @@ +{ + "app": { + "bundleName": "com.huawei.setting", + "vendor": "huawei", + "version": { + "code": 1, + "name": "1.0" + }, + "apiVersion": { + "compatible": 3, + "target": 4 + } + }, + "deviceConfig": { + "default": { + + } + }, + "module": { + "package": "com.huawei.setting", + "name": ".MyHarmonyAbilityPackage", + "deviceType": [ + "phone", "tv","tablet", "pc","car","smartWatch","sportsWatch","smartVision" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "setting", + "moduleType": "entry" + }, + "abilities": [{ + "name": "SettingMainAbility", + "icon": "assets/setting/resources/base/media/setting.png", + "label": "test testapp2 1", + "launchType": "standard", + "type": "page", + "visible":true + } + ] + } +} diff --git a/setting/setting/src/main/cpp/app_ability_slice.cpp b/setting/setting/src/main/cpp/app_ability_slice.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fa1d1afbf0d35f4280718843078d51f31954ce0 --- /dev/null +++ b/setting/setting/src/main/cpp/app_ability_slice.cpp @@ -0,0 +1,201 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "app_ability_slice.h" +#include "style.h" + +namespace OHOS { +REGISTER_AS(AppAbilitySlice) + +AppAbilitySlice::~AppAbilitySlice() +{ + if (scrollView_) { + DeleteChildren(scrollView_); + scrollView_ = nullptr; + } + if (headView_) { + DeleteChildren(headView_); + headView_ = nullptr; + } + if (buttonBackListener_) { + delete buttonBackListener_; + buttonBackListener_ = nullptr; + } + if (buttonAppInfoListener_) { + delete buttonAppInfoListener_; + buttonAppInfoListener_ = nullptr; + } + if (pBundleInfos_) { + ClearBundleInfo(pBundleInfos_); + } +} + +void AppAbilitySlice::SetButtonListener(void) +{ + auto onClick1 = [this](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("MainAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined SettingWifiAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonBackListener_ = new EventListener(onClick1, nullptr); +} + +void AppAbilitySlice::SetAppButtonListener(const char* appName) +{ + auto onClick2 = [this, appName](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + bool ret = SetWantData(&want1, appName, strlen(appName) + 1); + StartAbility(want1); + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("AppInfoAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined SettingWifiAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + ClearWant(&want1); + return true; + }; + buttonAppInfoListener_ = new EventListener(onClick2, nullptr); +} + +void AppAbilitySlice::SetHead() +{ + headView_ = new UIViewGroup(); + rootView_->Add(headView_); + headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT); + headView_->SetStyle(STYLE_BACKGROUND_OPA, 0); + headView_->SetTouchable(true); + headView_->SetOnClickListener(buttonBackListener_); + + UIImageView* imageView = new UIImageView(); + headView_->Add(imageView); + imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_BACK); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT); + lablelFont->SetText("应用"); + lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR); + headView_->Add(lablelFont); +} + +void AppAbilitySlice::SetAnAppInfo(const int count, BundleInfo& pBundleInfo) +{ + UIViewGroup* itemView = new UIViewGroup(); + char buff[64] = {0}; + int useX = 0; + int useY = count * DE_ITEM_INTERVAL; + itemView->SetPosition(useX, useY, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + itemView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + itemView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + itemView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + itemView->SetTouchable(true); + + int err = strcpy_s(buff, sizeof(buff), pBundleInfo.bundleName); + if (err != EOK) { + printf("[ERROR]strcpy_s pBundleInfo.bundleName failed, err = %d\n", err); + return; + } + for (int i = 0; i < strlen(pBundleInfo.bundleName); i++) { + buff[i] = pBundleInfo.bundleName[i]; + } + SetAppButtonListener(pBundleInfo.bundleName); + itemView->SetOnClickListener(buttonAppInfoListener_); + scrollView_->Add(itemView); + + UIImageView* imageIdView = new UIImageView(); + imageIdView->SetPosition(APP_IMAGE_X, APP_IMAGE_Y, APP_IMAGE_WIDTH, APP_IMAGE_HEIGHT); + imageIdView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + imageIdView->SetSrc(DE_IMAGE_APP); + itemView->Add(imageIdView); + + int bundleNameOffset = 11; + UILabel* name = new UILabel(); + name->SetPosition(APP_NAME_X, APP_NAME_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + name->SetText(pBundleInfo.bundleName + bundleNameOffset); + name->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + name->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + itemView->Add(name); + + UIImageView* imageView = new UIImageView(); + imageView->SetPosition(DE_FORWARD_IMG_X, DE_FORWARD_IMG_Y, DE_FORWARD_IMG_WIDTH, DE_FORWARD_IMG_HEIGHT); + imageView->SetSrc(DE_IMAGE_FORWORD); + itemView->Add(imageView); +} + + +void AppAbilitySlice::SetScrollView() +{ + scrollView_ = new UIScrollView(); + scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR); + scrollView_->SetPosition(DE_SCROLL_X, DE_SCROLL_Y, DE_SCROLL_WIDTH, DE_SCROLL_HEIGHT); + scrollView_->SetXScrollBarVisible(false); + scrollView_->SetYScrollBarVisible(true); + rootView_->Add(scrollView_); + + uint8_t ret = -1; + int num = 0; + ret = GetBundleInfos(1, &pBundleInfos_, &num); + if (ret == 0) { + BundleInfo* pBundleInfo = pBundleInfos_; + for (int count = 0; count < num; count++, pBundleInfo++) { + printf("[LOG]pBundleInfo.bundleName->%s versionName->%s \n", + pBundleInfo->bundleName, pBundleInfo->versionName); + if (pBundleInfo->isSystemApp == false) { + SetAnAppInfo(count, *pBundleInfo); + } + } + } +} + +void AppAbilitySlice::OnStart(const Want& want) +{ + AbilitySlice::OnStart(want); + + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR); + SetButtonListener(); + SetHead(); + SetScrollView(); + SetUIContent(rootView_); +} + +void AppAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void AppAbilitySlice::OnActive(const Want &want) +{ + AbilitySlice::OnActive(want); +} + +void AppAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void AppAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS diff --git a/setting/setting/src/main/cpp/app_ability_slice.h b/setting/setting/src/main/cpp/app_ability_slice.h new file mode 100644 index 0000000000000000000000000000000000000000..e657a334b4ac9802bbf10ac79085d7b2ea31703a --- /dev/null +++ b/setting/setting/src/main/cpp/app_ability_slice.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_APP_ABILITY_SLICE_H +#define OHOS_APP_ABILITY_SLICE_H + +#include "ability_info.h" +#include "ability_loader.h" +#include "ability_manager.h" +#include "ability_slice.h" +#include "bundle_manager.h" +#include "components/ui_image_view.h" +#include "components/ui_label.h" +#include "components/ui_label_button.h" +#include "components/ui_list.h" +#include "components/ui_scroll_view.h" +#include "components/ui_toggle_button.h" +#include "element_name.h" +#include "event_listener.h" +#include "list.h" +#include "module_info.h" +#include "setting_utils.h" +#include "want.h" +#include "securec.h" + +namespace OHOS { +class AppAbilitySlice : public AbilitySlice { +public: + AppAbilitySlice() + : headView_(nullptr), scrollView_(nullptr), rootView_(nullptr), pBundleInfos_(nullptr), + buttonBackListener_(nullptr), buttonAppInfoListener_(nullptr) {} + virtual ~AppAbilitySlice(); +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; + +private: + void SetButtonListener(); + void SetAppButtonListener(const char* appName); + void SetHead(); + void SetScrollView(); + void SetAnAppInfo(int count, BundleInfo& pBundleInfo); + UIViewGroup* headView_; + UIScrollView* scrollView_; + RootView* rootView_; + BundleInfo* pBundleInfos_; + EventListener* buttonBackListener_; + EventListener* buttonAppInfoListener_; + + constexpr static int APP_IMAGE_X = 12; + constexpr static int APP_IMAGE_Y = 12; + constexpr static int APP_IMAGE_WIDTH = 64; + constexpr static int APP_IMAGE_HEIGHT = 64; + constexpr static int APP_NAME_X = 94; + constexpr static int APP_NAME_Y = 28; +}; +} // namespace OHOS +#endif diff --git a/setting/setting/src/main/cpp/app_info_ability_slice.cpp b/setting/setting/src/main/cpp/app_info_ability_slice.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e882e28b2a9f06205decf630fd8f3eb3a3d86362 --- /dev/null +++ b/setting/setting/src/main/cpp/app_info_ability_slice.cpp @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "app_info_ability_slice.h" +#include "style.h" + +namespace OHOS { +REGISTER_AS(AppInfoAbilitySlice) + +AppInfoAbilitySlice::~AppInfoAbilitySlice() +{ + if (scrollView_) { + DeleteChildren(scrollView_); + scrollView_ = nullptr; + } + + if (headView_) { + DeleteChildren(headView_); + headView_ = nullptr; + } + + if (!buttonBackListener_) { + delete buttonBackListener_; + buttonBackListener_ = nullptr; + } + + if (permissions_) { + free(permissions_); + } + ListNode* node = listListener_.Begin(); + while (node != listListener_.End()) { + delete node->data_; + node = node->next_; + } + listListener_.Clear(); +} + +void AppInfoAbilitySlice::SetButtonListener(void) +{ + auto onClick = [this](UIView& view, const Event& event) -> bool { + Terminate(); + return true; + }; + buttonBackListener_ = new EventListener(onClick, nullptr); +} + +void AppInfoAbilitySlice::SetHead() +{ + headView_ = new UIViewGroup(); + rootView_->Add(headView_); + headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT); + headView_->SetStyle(STYLE_BACKGROUND_OPA, 0); + headView_->SetTouchable(true); + headView_->SetOnClickListener(buttonBackListener_); + + UIImageView* imageView = new UIImageView(); + headView_->Add(imageView); + imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_BACK); + + printf("[LOG] bundleName_-> %s +11->%s \n", bundleName_, bundleName_ + 11); // 11 + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT); + lablelFont->SetText(bundleName_ + 11); // use 11 + lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR); + headView_->Add(lablelFont); +} + +void AppInfoAbilitySlice::SetAppPermissionInfo(int index, PermissionSaved& permissions) +{ + UIViewGroup* itemView = new UIViewGroup(); + int useX = 0; + int useY = index * DE_ITEM_INTERVAL; + itemView->SetPosition(useX, useY, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + itemView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + itemView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + itemView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + scrollView_->Add(itemView); + + UILabel* nameLabel = new UILabel(); + nameLabel->SetPosition(DE_TITLE_TEXT_X, DE_TITLE_TEXT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + nameLabel->SetText(permissions.name + 16); // 16 is get offset name + nameLabel->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + nameLabel->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER); + nameLabel->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + itemView->Add(nameLabel); + UIToggleButton* togglebutton = new UIToggleButton(); + togglebutton->SetPosition(DE_TOGGLE_BUTTON_X, DE_TOGGLE_BUTTON_Y); + if (permissions.granted == 0) { + togglebutton->SetState(false); + } else { + togglebutton->SetState(true); + } + ToggBtnOnListener* listern = new ToggBtnOnListener(togglebutton, permissions.name, strlen(permissions.name), + bundleName_, strlen(bundleName_)); + togglebutton->SetOnClickListener(listern); + listListener_.PushBack(listern); + itemView->Add(togglebutton); +} + +void AppInfoAbilitySlice::PermissionInfoList() +{ + int permNum = 0; + scrollView_ = new UIScrollView(); + scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR); + scrollView_->SetPosition(DE_SCROLL_X, DE_SCROLL_Y, DE_SCROLL_WIDTH, DE_SCROLL_HEIGHT); + scrollView_->SetXScrollBarVisible(false); + scrollView_->SetYScrollBarVisible(true); + rootView_->Add(scrollView_); + int ret = QueryPermission(bundleName_, &permissions_, &permNum); + if (ret == 0) { + printf("[LOG]PermissionInfoList bundleName_ -> %s ,permNum->%d\n", bundleName_, permNum); + for (int i = 0; i < permNum; i++) { + if (permissions_ != nullptr) { + printf("[LOG]PermissionInfoList xxx -> name %s \n", permissions_->name); + SetAppPermissionInfo(i, permissions_[i]); + } + } + } +} + +void AppInfoAbilitySlice::OnStart(const Want& want) +{ + printf("[LOG]receive the data -> %s\n", static_cast(want.data)); + AbilitySlice::OnStart(want); + + memcpy_s(bundleName_, sizeof(bundleName_), want.data, want.dataLength); + + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR); + SetButtonListener(); + SetHead(); + PermissionInfoList(); + SetUIContent(rootView_); +} + +void AppInfoAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void AppInfoAbilitySlice::OnActive(const Want &want) +{ + AbilitySlice::OnActive(want); +} + +void AppInfoAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void AppInfoAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS diff --git a/setting/setting/src/main/cpp/app_info_ability_slice.h b/setting/setting/src/main/cpp/app_info_ability_slice.h new file mode 100644 index 0000000000000000000000000000000000000000..7a18ac2e14a6add075db7301f5b1784dcfa10653 --- /dev/null +++ b/setting/setting/src/main/cpp/app_info_ability_slice.h @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_APP_INFO_ABILITY_SLICE_H +#define OHOS_APP_INFO_ABILITY_SLICE_H + +#include "ability_info.h" +#include "ability_loader.h" +#include "ability_manager.h" +#include "ability_slice.h" +#include "bundle_manager.h" +#include "components/ui_image_view.h" +#include "components/ui_label.h" +#include "components/ui_label_button.h" +#include "components/ui_list.h" +#include "components/ui_scroll_view.h" +#include "components/ui_toggle_button.h" +#include "element_name.h" +#include "event_listener.h" +#include "list.h" +#include "module_info.h" +#include "setting_utils.h" +#include "want.h" +#include "pms_interface.h" +#include +#include + +namespace OHOS { + +class ToggBtnOnListener : public UIView::OnClickListener { +public: + ToggBtnOnListener(UIToggleButton* togglebutton, const char* name, int len1, const char* bundleName, int len2) + { + memcpy_s(name_, sizeof(name_), name, len1); + name_[len1] = 0; + memcpy_s(bundleName_, sizeof(bundleName_), bundleName, len2); + bundleName_[len2] = 0; + status_ = false; + togglebutton_ = togglebutton; + } + + bool OnClick(UIView& view, const ClickEvent& event) override + { + int ret = -1; + if (status_) { + if ((ret = RevokePermission(bundleName_, name_)) == 0) { + status_ = false; + togglebutton_->SetState(false); + } else { + } + } else { + if ((ret = GrantPermission(bundleName_, name_)) == 0) { + status_ = true; + togglebutton_->SetState(true); + } else { + } + } + return true; + } +private: + char name_[128]; + char bundleName_[128]; + bool status_; + UIToggleButton* togglebutton_; +}; + +class AppInfoAbilitySlice : public AbilitySlice { +public: + AppInfoAbilitySlice() + : headView_(nullptr), scrollView_(nullptr), rootView_(nullptr), permissions_(nullptr), + buttonBackListener_(nullptr) {} + virtual ~AppInfoAbilitySlice(); +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; +private: + void SetAppButtonListener(const char* appName); + void SetButtonListener(void); + void SetHead(void); + UIViewGroup* headView_; + UIScrollView* scrollView_; + RootView* rootView_; + PermissionSaved* permissions_; + EventListener* buttonBackListener_; + List listListener_; + + void PermissionInfoList(); + void SetAppPermissionInfo(int index, PermissionSaved& permissions); + char bundleName_[128]; + +}; +} // namespace OHOS +#endif diff --git a/setting/setting/src/main/cpp/event_listener.h b/setting/setting/src/main/cpp/event_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..ba7d323ffb63278a006d6f1f2e1b05ac97ae555a --- /dev/null +++ b/setting/setting/src/main/cpp/event_listener.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_EVENT_LISTENER_H +#define OHOS_EVENT_LISTENER_H + +#include +#include + +#include "components/ui_view.h" + +namespace OHOS { +using OnEventFunc = std::function; + +class EventListener : public UIView::OnClickListener, public UIView::OnLongPressListener { +public: + EventListener() = delete; + ~EventListener() override = default; + + EventListener(OnEventFunc onClick, OnEventFunc onLongPress) + { + onClick_ = std::move(onClick); + onLongPress_ = std::move(onLongPress); + } + + bool OnClick(UIView &view, const ClickEvent &event) override + { + if (!onClick_) { + return false; + } + return onClick_(view, event); + } + + bool OnLongPress(UIView &view, const LongPressEvent &event) override + { + if (!onLongPress_) { + return false; + } + return onLongPress_(view, event); + } + +private: + OnEventFunc onClick_ {}; + OnEventFunc onLongPress_ {}; +}; +} // namespace OHOS +#endif diff --git a/setting/setting/src/main/cpp/main_ability_slice.cpp b/setting/setting/src/main/cpp/main_ability_slice.cpp new file mode 100644 index 0000000000000000000000000000000000000000..188d3c3474dff4183b6553651f4fca397c5ba12f --- /dev/null +++ b/setting/setting/src/main/cpp/main_ability_slice.cpp @@ -0,0 +1,314 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "main_ability_slice.h" +#include "ability_loader.h" +#include "ability_slice.h" +#include "ability_info.h" +#include "ability_manager.h" +#include "event_listener.h" +#include "bundle_manager.h" +#include "module_info.h" +#include "element_name.h" +#include "wpa_work.h" +#include "style.h" +#include +#include + +namespace OHOS { +REGISTER_AS(MainAbilitySlice) + +MainAbilitySlice::~MainAbilitySlice() +{ + if (scrollView_) { + DeleteChildren(scrollView_); + scrollView_ = nullptr; + } + + if (headView_) { + DeleteChildren(headView_); + headView_ = nullptr; + } + if (buttonBackListener_) { + delete buttonBackListener_; + buttonBackListener_ = nullptr; + } + + if (buttonWifiListener_) { + delete buttonWifiListener_; + buttonWifiListener_ = nullptr; + } + + if (buttonAppListener_) { + delete buttonAppListener_; + buttonAppListener_ = nullptr; + } + + if (buttonAboutListener_) { + delete buttonAboutListener_; + buttonAboutListener_ = nullptr; + } +} + +void MainAbilitySlice::SetButtonListenerWifi(void) +{ + auto onClick1 = [this](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("SettingWifiAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined SettingWifiAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonWifiListener_ = new EventListener(onClick1, nullptr); +} + +void MainAbilitySlice::SetButtonListenerApp(void) +{ + auto onClick2 = [this](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + ElementName element = { nullptr }; + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("AppAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined AppInfoAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonAppListener_ = new EventListener(onClick2, nullptr); +} + +void MainAbilitySlice::SetButtonListenerAbout(void) +{ + auto onClick3 = [this](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("SettingAboutAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined NextAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonAboutListener_ = new EventListener(onClick3, nullptr); +} + +void MainAbilitySlice::SetHead(void) +{ + auto toLaunher = [this] (UIView &view, const Event &event) -> bool { + TerminateAbility(); + return true; + }; + buttonBackListener_ = new EventListener(toLaunher, nullptr); + + headView_ = new UIViewGroup(); + rootView_->Add(headView_); + headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT); + headView_->SetStyle(STYLE_BACKGROUND_OPA, 0); + headView_->SetTouchable(true); + headView_->SetOnClickListener(buttonBackListener_); + + UIImageView* imageView = new UIImageView(); + headView_->Add(imageView); + imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_BACK); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT); + lablelFont->SetText("设置"); + lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR); + headView_->Add(lablelFont); +} + +void MainAbilitySlice::SetWifiButtonView(void) +{ + UIViewGroup* buttonView = new UIViewGroup(); + buttonView->SetPosition(WIFI_BUTTON_X, WIFI_BUTTON_Y, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + buttonView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + buttonView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + buttonView->SetTouchable(true); + buttonView->SetOnClickListener(buttonWifiListener_); + scrollView_->Add(buttonView); + + UILabel* lablelFontWifi = new UILabel(); + lablelFontWifi->SetPosition(DE_TITLE_TEXT_X, WIFI_BUTTON_TEXT_WIFI_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFontWifi->SetText("WiFi"); + lablelFontWifi->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + lablelFontWifi->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + buttonView->Add(lablelFontWifi); + + char buff[64] = {0}; + int myX = WIFI_BUTTON_TEXT_SSID_X; + int myY = WIFI_BUTTON_TEXT_SSID_Y; + int ret = GetCurrentConnInfo(buff, sizeof(buff)); + lablelFontSsid_ = new UILabel(); + lablelFontSsid_->SetPosition(myX, myY, DE_SUBTITLE_TEXT_WIDTH, DE_SUBTITLE_TEXT_HEIGHT); + if (ret == 0) { + lablelFontSsid_->SetText(buff); + } else { + lablelFontSsid_->SetText("未连接"); + } + + lablelFontSsid_->SetFont(DE_FONT_OTF, DE_SUBTITLE_TEXT_SIZE); + lablelFontSsid_->SetStyle(STYLE_TEXT_COLOR, DE_SUBTITLE_TEXT_COLOR); + buttonView->Add(lablelFontSsid_); + + UIImageView* imageView = new UIImageView(); + imageView->SetPosition(DE_FORWARD_IMG_X, DE_FORWARD_IMG_Y, DE_FORWARD_IMG_WIDTH, DE_FORWARD_IMG_HEIGHT); + imageView->SetSrc(DE_IMAGE_FORWORD); + buttonView->Add(imageView); +} + +void MainAbilitySlice::SetAppButtonView(void) +{ + UIViewGroup* buttonView = new UIViewGroup(); + buttonView->SetPosition(APP_BUTTON_X, APP_BUTTON_Y, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + buttonView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + buttonView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + buttonView->SetTouchable(true); + buttonView->SetOnClickListener(buttonAppListener_); + scrollView_->Add(buttonView); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_TITLE_TEXT_X, DE_TITLE_TEXT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFont->SetText("应用"); + lablelFont->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + buttonView->Add(lablelFont); + + UIImageView* imageView = new UIImageView(); + imageView->SetPosition(DE_FORWARD_IMG_X, DE_FORWARD_IMG_Y, DE_FORWARD_IMG_WIDTH, DE_FORWARD_IMG_HEIGHT); + imageView->SetSrc(DE_IMAGE_FORWORD); + buttonView->Add(imageView); +} + +static void setAboutTest(UIViewGroup *buttonView, int positionX, int positionY, const char *setText) +{ + UILabel* lablelFontSystem = new UILabel(); + lablelFontSystem->SetPosition(positionX, positionY, DE_SUBTITLE_TEXT_WIDTH, DE_SUBTITLE_TEXT_HEIGHT); + lablelFontSystem->SetText(setText); + lablelFontSystem->SetFont(DE_FONT_OTF, DE_SUBTITLE_TEXT_SIZE); + lablelFontSystem->SetStyle(STYLE_TEXT_COLOR, DE_SUBTITLE_TEXT_COLOR); + buttonView->Add(lablelFontSystem); +} + +void MainAbilitySlice::SetAboutButtonView(void) +{ + UIViewGroup* buttonView = new UIViewGroup(); + buttonView->SetPosition(ABOUT_BUTTON_X, ABOUT_BUTTON_Y, DE_BUTTON_WIDTH, ABOUT_BUTTON_HEIGHT); + buttonView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + buttonView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + buttonView->SetTouchable(true); + buttonView->SetOnClickListener(buttonAboutListener_); + scrollView_->Add(buttonView); + + UILabel* lablelFontAbout = new UILabel(); + lablelFontAbout->SetPosition(DE_TITLE_TEXT_X, ABOUT_BUTTON_TEXT_ABOUT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFontAbout->SetText("关于"); + lablelFontAbout->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + lablelFontAbout->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + buttonView->Add(lablelFontAbout); + + char buff[62]; + char* gDV = GetDisplayVersion(); + int err = sprintf_s(buff, sizeof(buff), "系统版本: %s", gDV); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + return; + } + free(gDV); + setAboutTest(buttonView, ABOUT_BUTTON_TEXT_SYSTEM_X, ABOUT_BUTTON_TEXT_SYSTEM_Y, buff); + + err = memset_s(buff, sizeof(buff), 0, sizeof(buff)); + if (err < EOK) { + printf("[ERROR]memset_s failed, err = %d\n", err); + return; + } + char* gPT = GetProductType(); + err = sprintf_s(buff, sizeof(buff), "设备名称: %s", gPT); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + return; + } + free(gPT); + setAboutTest(buttonView, ABOUT_BUTTON_TEXT_DEVICE_X, ABOUT_BUTTON_TEXT_DEVICE_Y, buff); + + UIImageView* imageView = new UIImageView(); + imageView->SetPosition(DE_FORWARD_IMG_X, ABOUT_BUTTON_IMAGE_Y, DE_FORWARD_IMG_WIDTH, DE_FORWARD_IMG_HEIGHT); + imageView->SetSrc(DE_IMAGE_FORWORD); + buttonView->Add(imageView); +} + +void MainAbilitySlice::SetScrollView() +{ + scrollView_ = new UIScrollView(); + scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR); + scrollView_->SetPosition(DE_SCROLL_X, DE_SCROLL_Y, DE_SCROLL_WIDTH, DE_SCROLL_HEIGHT); + scrollView_->SetXScrollBarVisible(false); + scrollView_->SetYScrollBarVisible(false); + rootView_->Add(scrollView_); + SetWifiButtonView(); + SetAppButtonView(); + SetAboutButtonView(); +} + +void MainAbilitySlice::OnStart(const Want& want) +{ + AbilitySlice::OnStart(want); + SetButtonListenerWifi(); + SetButtonListenerApp(); + SetButtonListenerAbout(); + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR); + + SetHead(); + SetScrollView(); + SetUIContent(rootView_); +} + +void MainAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void MainAbilitySlice::OnActive(const Want& want) +{ + char buff[64] = {0}; + int ret = GetCurrentConnInfo(buff, sizeof(buff)); + if (ret == 0) { + printf("##### SetText -> %s \n", buff); + lablelFontSsid_->SetText(buff); + } else { + lablelFontSsid_->SetText("未连接"); + } + AbilitySlice::OnActive(want); +} + +void MainAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void MainAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS \ No newline at end of file diff --git a/setting/setting/src/main/cpp/main_ability_slice.h b/setting/setting/src/main/cpp/main_ability_slice.h new file mode 100644 index 0000000000000000000000000000000000000000..a5911b829f771d3e302f3da2fc6d450a80b72bfe --- /dev/null +++ b/setting/setting/src/main/cpp/main_ability_slice.h @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_MAIN_ABILITY_SLICE_H +#define OHOS_MAIN_ABILITY_SLICE_H + +#include +#include "ability_loader.h" +#include "event_listener.h" +#include "list.h" +#include "components/ui_image_view.h" +#include "components/ui_scroll_view.h" +#include "components/ui_label_button.h" +#include "components/ui_label.h" +#include "components/ui_list.h" +#include "components/ui_toggle_button.h" +#include "bundle_manager.h" +#include "ability_slice.h" +#include "module_info.h" +#include "ability_info.h" +#include "element_name.h" +#include "want.h" +#include "ability_manager.h" +#include "parameter.h" +#include "setting_utils.h" +#include "wpa_work.h" +#include "pthread.h" + + +namespace OHOS { +class MainAbilitySlice : public AbilitySlice { +public: + MainAbilitySlice() + : headView_(nullptr), scrollView_(nullptr), rootView_(nullptr), lablelFontSsid_(nullptr), + buttonWifiListener_(nullptr), buttonAppListener_(nullptr), buttonAboutListener_(nullptr), + buttonBackListener_(nullptr) {} + virtual ~MainAbilitySlice(); + +protected: + void OnStart(const Want &want) override; + void OnInactive() override; + void OnActive(const Want &want) override; + void OnBackground() override; + void OnStop() override; + +private: + void SetButtonListenerWifi(); + void SetButtonListenerApp(); + void SetButtonListenerAbout(); + void SetAboutButtonView(); + void SetAppButtonView(); + void SetWifiButtonView(); + void SetScrollView(); + void SetHead(); + + UIViewGroup* headView_; + UIScrollView* scrollView_; + RootView* rootView_; + UILabel* lablelFontSsid_; + char ssidBuff[64]; + EventListener* buttonWifiListener_; + EventListener* buttonAppListener_; + EventListener* buttonAboutListener_; + EventListener* buttonBackListener_; + + constexpr static int WIFI_BUTTON_X = 0; + constexpr static int WIFI_BUTTON_Y = 0; + constexpr static int WIFI_BUTTON_TEXT_WIFI_Y = 13; + constexpr static int WIFI_BUTTON_TEXT_SSID_X = 18; + constexpr static int WIFI_BUTTON_TEXT_SSID_Y = 45; + + constexpr static int APP_BUTTON_X = 0; + constexpr static int APP_BUTTON_Y = 95; + + constexpr static int ABOUT_BUTTON_X = 0; + constexpr static int ABOUT_BUTTON_Y = 190; + constexpr static int ABOUT_BUTTON_HEIGHT = 113; + constexpr static int ABOUT_BUTTON_TEXT_ABOUT_Y = 5; + constexpr static int ABOUT_BUTTON_TEXT_SYSTEM_X = 18; + constexpr static int ABOUT_BUTTON_TEXT_SYSTEM_Y = 39; + constexpr static int ABOUT_BUTTON_TEXT_DEVICE_X = 18; + constexpr static int ABOUT_BUTTON_TEXT_DEVICE_Y = 72; + constexpr static int ABOUT_BUTTON_IMAGE_Y = 34; +}; +} + +#endif // OHOS_MAIN_ABILITY_SLICE_H diff --git a/setting/setting/src/main/cpp/setting_about_ability_slice.cpp b/setting/setting/src/main/cpp/setting_about_ability_slice.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd8be1a65ea7d0981f17135cd33e73554411a0c5 --- /dev/null +++ b/setting/setting/src/main/cpp/setting_about_ability_slice.cpp @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "setting_about_ability_slice.h" +#include "style.h" + +namespace OHOS { +REGISTER_AS(SettingAboutAbilitySlice) + +SettingAboutAbilitySlice::~SettingAboutAbilitySlice() +{ + if (scrollView_) { + DeleteChildren(scrollView_); + scrollView_ = nullptr; + } + + if (headView_) { + DeleteChildren(headView_); + headView_ = nullptr; + } + if (buttonBackListener_) { + delete buttonBackListener_; + buttonBackListener_ = nullptr; + } + for (int count = 0; count < SCROLL_ITEM_NUM; count++) { + if (!itemInfo_[count][1]) { + free(itemInfo_[count][1]); + itemInfo_[count][1] = nullptr; + } + } +} + +void SettingAboutAbilitySlice::SetButtonListener() +{ + auto onClick = [this](UIView& view, const Event& event) -> bool { + Terminate(); + return true; + }; + buttonBackListener_ = new EventListener(onClick, nullptr); +} + +void SettingAboutAbilitySlice::SetItemInfo() +{ + itemInfo_[0][0] = (char*) "设备名称"; // 0 + itemInfo_[0][1] = GetProductType(); // 0 + itemInfo_[1][0] = (char*) "厂家信息"; // 1 + itemInfo_[1][1] = GetManufacture(); // 1 + itemInfo_[2][0] = (char*) "品牌信息"; // 2 + itemInfo_[2][1] = GetBrand(); // 2 + itemInfo_[3][0] = (char*) "硬件版本号"; // 3 + itemInfo_[3][1] = GetHardwareModel(); // 3 + itemInfo_[4][0] = (char*) "设备序列号"; // 4 + itemInfo_[4][1] = GetSerial(); // 4 + itemInfo_[5][0] = (char*) "操作系统名"; // 5 + itemInfo_[5][1] = GetOsName(); // 5 + itemInfo_[6][0] = (char*) "软件版本号"; // 6 + itemInfo_[6][1] = GetDisplayVersion(); // 6 + itemInfo_[7][0] = (char*) "BootLoader版本号"; // 7 + itemInfo_[7][1] = GetBootloaderVersion(); // 7 + itemInfo_[8][0] = (char*) "构建时间"; // 8 + itemInfo_[8][1] = GetBuildTime(); // 8 +} + +void SettingAboutAbilitySlice::SetHead() +{ + headView_ = new UIViewGroup(); + rootView_->Add(headView_); + headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT); + headView_->SetStyle(STYLE_BACKGROUND_OPA, 0); + headView_->SetTouchable(true); + headView_->SetOnClickListener(buttonBackListener_); + + UIImageView* imageView = new UIImageView(); + headView_->Add(imageView); + imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_BACK); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT); + lablelFont->SetText("关于"); + lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR); + headView_->Add(lablelFont); +} + +void SettingAboutAbilitySlice::SetScrollItem(int count) +{ + int myPositonY = count * DE_ITEM_INTERVAL; + + UIViewGroup* itemView = new UIViewGroup(); + itemView->SetPosition(ITEM_X, myPositonY, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + itemView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + itemView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + scrollView_->Add(itemView); + + UILabel* lablelFontName = new UILabel(); + lablelFontName->SetPosition(DE_TITLE_TEXT_X, DE_TITLE_TEXT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFontName->SetText(itemInfo_[count][0]); + lablelFontName->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + lablelFontName->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + itemView->Add(lablelFontName); + + UILabel* lablelFontInfo = new UILabel(); + lablelFontInfo->SetPosition(ITEM_INFO_X, ITEM_INFO_Y, DE_SUBTITLE_TEXT_WIDTH, DE_SUBTITLE_TEXT_HEIGHT); + lablelFontInfo->SetText(itemInfo_[count][1]); + lablelFontInfo->SetFont(DE_FONT_OTF, DE_SUBTITLE_TEXT_SIZE); + lablelFontInfo->SetAlign(TEXT_ALIGNMENT_RIGHT); + lablelFontInfo->SetStyle(STYLE_TEXT_COLOR, DE_SUBTITLE_TEXT_COLOR); + itemView->Add(lablelFontInfo); +} + +void SettingAboutAbilitySlice::SetScroll() +{ + scrollView_ = new UIScrollView(); + scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR); + scrollView_->SetPosition(DE_SCROLL_X, DE_SCROLL_Y, DE_SCROLL_WIDTH, DE_SCROLL_HEIGHT); + scrollView_->SetXScrollBarVisible(false); + scrollView_->SetYScrollBarVisible(true); + rootView_->Add(scrollView_); + for (int count = 0; count < SCROLL_ITEM_NUM; count++) { + SetScrollItem(count); + } +} + +void SettingAboutAbilitySlice::OnStart(const Want& want) +{ + AbilitySlice::OnStart(want); + SetButtonListener(); + SetItemInfo(); + + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR); + SetHead(); + SetScroll(); + + SetUIContent(rootView_); +} + +void SettingAboutAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void SettingAboutAbilitySlice::OnActive(const Want& want) +{ + AbilitySlice::OnActive(want); +} + +void SettingAboutAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void SettingAboutAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS diff --git a/setting/setting/src/main/cpp/setting_about_ability_slice.h b/setting/setting/src/main/cpp/setting_about_ability_slice.h new file mode 100644 index 0000000000000000000000000000000000000000..b508fa07a81738dbab15113e2db844f37fe05ede --- /dev/null +++ b/setting/setting/src/main/cpp/setting_about_ability_slice.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_SETTING_ABOUT_ABILITY_SLICE_H +#define OHOS_SETTING_ABOUT_ABILITY_SLICE_H + +#include "ability_info.h" +#include "ability_loader.h" +#include "ability_slice.h" +#include "bundle_manager.h" +#include "components/ui_image_view.h" +#include "components/ui_label.h" +#include "components/ui_label_button.h" +#include "components/ui_list.h" +#include "components/ui_scroll_view.h" +#include "components/ui_toggle_button.h" +#include "element_name.h" +#include "event_listener.h" +#include "list.h" +#include "module_info.h" +#include "parameter.h" +#include "setting_utils.h" +#include "want.h" + +namespace OHOS { +class SettingAboutAbilitySlice : public AbilitySlice { +public: + SettingAboutAbilitySlice() + : headView_(nullptr), scrollView_(nullptr), rootView_(nullptr), buttonBackListener_(nullptr) {} + virtual ~SettingAboutAbilitySlice(); +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; +private: + void SetItemInfo(); + void SetButtonListener(); + void SetScrollItem(int count); + void SetScroll(); + void SetHead(); + + UIViewGroup* headView_; + UIScrollView* scrollView_; + RootView* rootView_; + EventListener *buttonBackListener_; + constexpr static int SCROLL_ITEM_NUM = 9; + char *itemInfo_[SCROLL_ITEM_NUM][2]; + constexpr static int ITEM_X = 0; + constexpr static int ITEM_INFO_X = 465; + constexpr static int ITEM_INFO_Y = 36; +}; +} // namespace OHOS +#endif diff --git a/setting/setting/src/main/cpp/setting_main_ability.cpp b/setting/setting/src/main/cpp/setting_main_ability.cpp new file mode 100644 index 0000000000000000000000000000000000000000..080a87fc62de72c40bef750807b7a25ef68002d9 --- /dev/null +++ b/setting/setting/src/main/cpp/setting_main_ability.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "setting_main_ability.h" + +namespace OHOS { +REGISTER_AA(SettingMainAbility) + +void SettingMainAbility::OnStart(const Want& want) +{ + SetMainRoute("MainAbilitySlice"); + Ability::OnStart(want); +} + +void SettingMainAbility::OnInactive() +{ + Ability::OnInactive(); +} + +void SettingMainAbility::OnActive(const Want& want) +{ + Ability::OnActive(want); +} + +void SettingMainAbility::OnBackground() +{ + Ability::OnBackground(); +} + +void SettingMainAbility::OnStop() +{ + + Ability::OnStop(); +} +} // namespace OHOS \ No newline at end of file diff --git a/setting/setting/src/main/cpp/setting_main_ability.h b/setting/setting/src/main/cpp/setting_main_ability.h new file mode 100644 index 0000000000000000000000000000000000000000..30038f999fedbff6f731af66e115a65162573ef0 --- /dev/null +++ b/setting/setting/src/main/cpp/setting_main_ability.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_MAIN_ABILITY_H +#define OHOS_MAIN_ABILITY_H + +#include "ability_loader.h" + +namespace OHOS { +class SettingMainAbility : public Ability { +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; +}; +} // namespace OHOS +#endif diff --git a/setting/setting/src/main/cpp/setting_utils.cpp b/setting/setting/src/main/cpp/setting_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43e6cf33221f5bc1d4b257e83d5ebfdc9e2b90a0 --- /dev/null +++ b/setting/setting/src/main/cpp/setting_utils.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "setting_utils.h" + +namespace OHOS { +void DeleteChildren(UIView *view) +{ + if (view == nullptr) { + return; + } + while (view != nullptr) { + UIView *tempView = view; + view = view->GetNextSibling(); + if (tempView->IsViewGroup()) { + DeleteChildren(static_cast(tempView)->GetChildrenHead()); + } + if (tempView->GetParent()) { + static_cast(tempView->GetParent())->Remove(tempView); + } + delete tempView; + tempView = nullptr; + } +} +} // namespace OHOS diff --git a/setting/setting/src/main/cpp/setting_utils.h b/setting/setting/src/main/cpp/setting_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..b291e8df7c1de669af8b174985a00b26fb352168 --- /dev/null +++ b/setting/setting/src/main/cpp/setting_utils.h @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_SETTING_UTILS_H +#define OHOS_SETTING_UTILS_H + +#include "components/ui_view_group.h" + +namespace OHOS { +#define DE_IMAGE_BACK "/storage/app/run/com.huawei.setting/setting/assets/setting/resources/base/media/back.png" +#define DE_IMAGE_FORWORD \ + "/storage/app/run/com.huawei.setting/setting/assets/setting/resources/base/media/forward.png" +#define DE_IMAGE_ENTER "/storage/app/run/com.huawei.setting/setting/assets/setting/resources/base/media/enter.png" +#define DE_IMAGE_APP "/storage/app/run/com.huawei.setting/setting/assets/setting/resources/base/media/app.png" +#define DE_FONT_OTF "SourceHanSansSC-Regular.otf" + +#define DE_ROOT_X 0 +#define DE_ROOT_Y 0 +#define DE_ROOT_WIDTH 960 +#define DE_ROOT_HEIGHT 480 +#define DE_ROOT_BACKGROUND_COLOR Color::ColorTo32(Color::Black()) + +#define DE_SCROLL_X 36 +#define DE_SCROLL_Y 72 +#define DE_SCROLL_WIDTH 888 +#define DE_SCROLL_HEIGHT 408 +#define DE_SCROLL_COLOR Color::ColorTo32(Color::Black()) + +#define DE_HEAD_X 0 +#define DE_HEAD_Y 0 +#define DE_HEAD_WIDTH 300 +#define DE_HEAD_HEIGHT 72 + +#define DE_HEAD_IMAGE_X 39 +#define DE_HEAD_IMAGE_Y 16 +#define DE_HEAD_IMAGE_WIDTH 30 +#define DE_HEAD_IMAGE_HEIGHT 30 + +#define DE_HEAD_TEXT_X 100 +#define DE_HEAD_TEXT_Y 15 +#define DE_HEAD_TEXT_WIDTH 180 +#define DE_HEAD_TEXT_HEIGHT 60 +#define DE_HEAD_TEXT_SIZE 32 +#define DE_HEAD_TEXT_COLOR Color::ColorTo32(Color::White()) + +#define DE_BUTTON_WIDTH 888 +#define DE_BUTTON_HEIGHT 89 +#define DE_BUTTON_BACKGROUND_COLOR Color::ColorTo32(Color::GetColorFromRGB(0x33, 0x33, 0x33)) +#define DE_BUTTON_RADIUS 16 + +#define DE_TITLE_TEXT_X 18 +#define DE_TITLE_TEXT_Y 28 +#define DE_TITLE_TEXT_WIDTH 400 +#define DE_TITLE_TEXT_HEIGHT 50 +#define DE_TITLE_TEXT_SIZE 26 +#define DE_TITLE_TEXT_COLOR Color::ColorTo32(Color::White()) + +#define DE_SUBTITLE_TEXT_WIDTH 400 +#define DE_SUBTITLE_TEXT_HEIGHT 40 +#define DE_SUBTITLE_TEXT_COLOR Color::ColorTo32(Color::GetColorFromRGB(0x9F, 0x9F, 0x9F)) +#define DE_SUBTITLE_TEXT_SIZE 24 + +#define DE_FORWARD_IMG_X 855 +#define DE_FORWARD_IMG_Y 24 +#define DE_FORWARD_IMG_WIDTH 12 +#define DE_FORWARD_IMG_HEIGHT 12 + +#define DE_TOGGLE_BUTTON_X 816 +#define DE_TOGGLE_BUTTON_Y 14 + +#define DE_ITEM_INTERVAL 95 +#define DE_CONTENT_FONT_SIZE 28 +#define DE_OPACITY_ALL 255 + +void DeleteChildren(UIView *view); +} +#endif diff --git a/setting/setting/src/main/cpp/setting_wifi_ability_slice.cpp b/setting/setting/src/main/cpp/setting_wifi_ability_slice.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79b168e6d1d95ebb80d92da01d2bdb1882628593 --- /dev/null +++ b/setting/setting/src/main/cpp/setting_wifi_ability_slice.cpp @@ -0,0 +1,265 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "setting_wifi_ability_slice.h" +#include +#include +#include "style.h" + +namespace OHOS { +REGISTER_AS(SettingWifiAbilitySlice) +static int g_wifiStatus = 0; + +SettingWifiAbilitySlice::SettingWifiAbilitySlice() + : headView_(nullptr), toggleButtonView_(nullptr), scrollView_(nullptr), rootView_(nullptr), + changeListener_(nullptr), buttonBackListener_(nullptr), buttonInputListener_(nullptr) +{ + int taskPeriod = 5000; + Task::Init(); + SetPeriod(taskPeriod); +} + +SettingWifiAbilitySlice::~SettingWifiAbilitySlice() +{ + ExitWpa(); + ExitWpaScan(); + if (toggleButtonView_) { + DeleteChildren(toggleButtonView_); + toggleButtonView_ = nullptr; + } + + if (scrollView_) { + DeleteChildren(scrollView_); + scrollView_ = nullptr; + } + + if (headView_) { + DeleteChildren(headView_); + headView_ = nullptr; + } + + if (changeListener_) { + delete changeListener_; + changeListener_ = nullptr; + } + + if (buttonBackListener_) { + delete buttonBackListener_; + buttonBackListener_ = nullptr; + } + + if (buttonInputListener_) { + delete buttonInputListener_; + buttonInputListener_ = nullptr; + } +} + +void SettingWifiAbilitySlice::SetButtonListener(void) +{ + auto onClick = [this](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("MainAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined SettingWifiAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonBackListener_ = new EventListener(onClick, nullptr); +} + +void SettingWifiAbilitySlice::SetWifiButtonListener(char* ssid) +{ + auto onClick2 = [this, ssid](UIView& view, const Event& event) -> bool { + Want want1 = { nullptr }; + bool ret = SetWantData(&want1, ssid, strlen(ssid) + 1); + StartAbility(want1); + AbilitySlice* nextSlice = + AbilityLoader::GetInstance().GetAbilitySliceByName("SettingWifiInputPasswordAbilitySlice"); + if (nextSlice == nullptr) { + printf("[warning]undefined SettingWifiAbilitySlice\n"); + } else { + Present(*nextSlice, want1); + } + return true; + }; + buttonInputListener_ = new EventListener(onClick2, nullptr); +} + +void SettingWifiAbilitySlice::SetHead(void) +{ + headView_ = new UIViewGroup(); + rootView_->Add(headView_); + headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT); + headView_->SetStyle(STYLE_BACKGROUND_OPA, 0); + headView_->SetTouchable(true); + headView_->SetOnClickListener(buttonBackListener_); + + UIImageView* imageView = new UIImageView(); + headView_->Add(imageView); + imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_BACK); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT); + lablelFont->SetText("WiFi"); + lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR); + headView_->Add(lablelFont); +} + +void SettingWifiAbilitySlice::SetToggleButton(void) +{ + toggleButtonView_ = new UIViewGroup(); + toggleButtonView_->SetPosition(TOGGLE_X, TOGGLE_Y, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + toggleButtonView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + toggleButtonView_->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + toggleButtonView_->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + rootView_->Add(toggleButtonView_); + + auto lablelFont = new UILabel(); + lablelFont->SetPosition(DE_TITLE_TEXT_X, DE_TITLE_TEXT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFont->SetText("WiFi"); + lablelFont->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + toggleButtonView_->Add(lablelFont); + + UIToggleButton* togglebutton = new UIToggleButton(); + changeListener_ = new TestBtnOnStateChangeListener(reinterpret_cast(scrollView_)); + togglebutton->SetOnClickListener(changeListener_); + togglebutton->SetPosition(DE_TOGGLE_BUTTON_X, DE_TOGGLE_BUTTON_Y); + togglebutton->SetState(true); + scrollView_->SetVisible(true); + + toggleButtonView_->Add(togglebutton); +} + +void SettingWifiAbilitySlice::SetUseWifi(void) +{ + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(USE_WIFI_FONT_X, USE_WIFI_FONT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFont->SetText("可用WiFi列表"); + lablelFont->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_SUBTITLE_TEXT_COLOR); + rootView_->Add(lablelFont); +} + +void SettingWifiAbilitySlice::AddWifi(void) +{ + int ssidIndex, ssidCount; + + char* buff = nullptr; + ssidCount = GetIdNum(); + if (ssidCount == 0) { + printf("[LOG]SettingWifiAbilitySlice::AddWifi ssidCount == 0 \n"); + return; + } + if (scrollView_ == nullptr) { + printf("[LOG]SettingWifiAbilitySlice::AddWifi scrollView_ == nullptr \n"); + return; + } + if (g_wifiStatus == 1) { + printf("[LOG]SettingWifiAbilitySlice::AddWifi wifiStatus == 1 \n"); + return; + } + for (ssidIndex = 0; ssidIndex < ssidCount; ssidIndex++) { + UIViewGroup *useWifiView = new UIViewGroup(); + useWifiView->SetPosition(ADD_WIFI_X, DE_ITEM_INTERVAL * ssidIndex, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT); + useWifiView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + useWifiView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + useWifiView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + useWifiView->SetTouchable(true); + char* buff = GetSsid(ssidIndex); // GetSsid need return point + SetWifiButtonListener(buff); + useWifiView->SetOnClickListener(buttonInputListener_); + scrollView_->Add(useWifiView); + + auto lablelFont = new UILabel(); + lablelFont->SetPosition(DE_TITLE_TEXT_X, DE_TITLE_TEXT_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT); + lablelFont->SetText(buff); + lablelFont->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + useWifiView->Add(lablelFont); + g_wifiStatus = 1; + } + + scrollView_->Invalidate(); +} + +void SettingWifiAbilitySlice::SetScrollWifi(void) +{ + scrollView_ = new UIScrollView(); + scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR); + scrollView_->SetPosition(DE_SCROLL_X, SCROLL_WIFI_Y, DE_SCROLL_WIDTH, SCROLL_WIFI_HEIGHT); + scrollView_->SetXScrollBarVisible(false); + scrollView_->SetYScrollBarVisible(true); + rootView_->Add(scrollView_); + g_wifiStatus = 0; + AddWifi(); +} + +void SettingWifiAbilitySlice::Callback() +{ + if (GetAndResetScanStat() == 1) { + LockWifiData(); + AddWifi(); + UnLockWifiData(); + } +} + +void SettingWifiAbilitySlice::OnStart(const Want& want) +{ + static int wpaCount = 0; + if (wpaCount == 0) { + WpaClientStart(); + WpaScanReconnect(nullptr, nullptr, HIDDEN_CLOSE); + wpaCount = 1; + } + AbilitySlice::OnStart(want); + + rootView_ = RootView::GetWindowRootView(); + rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR); + SetButtonListener(); + SetHead(); + SetUseWifi(); + SetScrollWifi(); + SetToggleButton(); + + TaskExecute(); + SetUIContent(rootView_); +} + +void SettingWifiAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void SettingWifiAbilitySlice::OnActive(const Want& want) +{ + AbilitySlice::OnActive(want); +} + +void SettingWifiAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void SettingWifiAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS diff --git a/setting/setting/src/main/cpp/setting_wifi_ability_slice.h b/setting/setting/src/main/cpp/setting_wifi_ability_slice.h new file mode 100644 index 0000000000000000000000000000000000000000..a340431a9464b910a9c4a85eef9dd9e0a79012f5 --- /dev/null +++ b/setting/setting/src/main/cpp/setting_wifi_ability_slice.h @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_SETTING_WIFI_ABILITY_SLICE_H +#define OHOS_SETTING_WIFI_ABILITY_SLICE_H + +#include +#include + +#include "ability_loader.h" +#include "components/ui_label.h" +#include "components/ui_label_button.h" +#include "components/ui_list.h" +#include "components/ui_scroll_view.h" +#include "components/ui_toggle_button.h" +#include "event_listener.h" +#include "list.h" +#include "parameter.h" +#include "pthread.h" +#include "setting_utils.h" +#include "stddef.h" +#include "wpa_work.h" +#include "common/task.h" + +namespace OHOS { +class TestBtnOnStateChangeListener : public OHOS::UICheckBox::OnChangeListener, public OHOS::UIView::OnClickListener { +public: + ~TestBtnOnStateChangeListener() {} + explicit TestBtnOnStateChangeListener(UIView* uiView) : myUiView(uiView) {} + + bool OnChange(UICheckBox::UICheckBoxState state) override + { + return true; + } + bool OnClick(UIView& view, const ClickEvent& event) override + { + bool ret = myUiView->IsVisible(); + if (ret == false) { + myUiView->SetVisible(true); + } else { + myUiView->SetVisible(false); + } + myUiView->Invalidate(); + return true; + } + +private: + UIView* myUiView; +}; + +class SettingWifiAbilitySlice : public AbilitySlice, Task { +public: + SettingWifiAbilitySlice(); + virtual ~SettingWifiAbilitySlice(); + void AddWifi(void); + void Callback() override; + +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; + +private: + void MyThread(); + void SetWifiButtonListener(char* ssid); + void SetButtonListener(); + void SetHead(); + void SetToggleButton(); + void SetUseWifi(); + void SetScrollWifi(); + + UIViewGroup* headView_; + UIViewGroup* toggleButtonView_; + UIScrollView* scrollView_; + RootView* rootView_; + TestBtnOnStateChangeListener* changeListener_; + EventListener* buttonBackListener_; + EventListener* buttonInputListener_; + + constexpr static int TOGGLE_X = 36; + constexpr static int TOGGLE_Y = 72; + + constexpr static int USE_WIFI_FONT_X = 54; + constexpr static int USE_WIFI_FONT_Y = 187; + + constexpr static int ADD_WIFI_X = 0; + constexpr static int SCROLL_WIFI_X = 36; + constexpr static int SCROLL_WIFI_Y = 242; + constexpr static int SCROLL_WIFI_WIDTH = 960; + constexpr static int SCROLL_WIFI_HEIGHT = 238; +}; +} // namespace OHOS +#endif diff --git a/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.cpp b/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3162174f49a00d1d6179a444b0e395def84c11e --- /dev/null +++ b/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.cpp @@ -0,0 +1,288 @@ +/* + * Copyright (c) 2020 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. + */ + +#include +#include "style.h" + +namespace OHOS { +REGISTER_AS(SettingWifiInputPasswordAbilitySlice) + +static UIView::OnClickListener* clickLeftListener_ = nullptr; + +static const int g_maxPassword = 10; // Maximum length of a password. +static char* g_inputSsid; +static char g_inputPassword[g_maxPassword + 1] = { 0 }; +static int g_inputCount = 0; +static int g_cursorPositionX = 20; // Initial position of cursor X + +SettingWifiInputPasswordAbilitySlice::~SettingWifiInputPasswordAbilitySlice() +{ + if (scrollView_) { + DeleteChildren(scrollView_); + scrollView_ = nullptr; + } + if (inputView_) { + DeleteChildren(inputView_); + inputView_ = nullptr; + } + if (headView_) { + DeleteChildren(headView_); + headView_ = nullptr; + }; + if (buttonBackListener_) { + delete buttonBackListener_; + buttonBackListener_ = nullptr; + } + if (clickLeftListener_) { + delete clickLeftListener_; + clickLeftListener_ = nullptr; + } +} + +class TestBtnOnClickInputPasswordChangeListener : public OHOS::UIView::OnClickListener { +public: + ~TestBtnOnClickInputPasswordChangeListener() {} + TestBtnOnClickInputPasswordChangeListener(UILabel* uiLabel, UILabel* uiCursor, const int ii, + const int cursorOffset) : myUiLabel(uiLabel), myUiCursor(uiCursor), mIi(ii), myCursorOffset(cursorOffset) {} + bool OnClick(UIView& view, const ClickEvent& event) override + { + if (g_inputCount >= g_maxPassword) { + return true; + } + g_inputPassword[g_inputCount] = '0' + mIi; + myUiLabel->SetText(g_inputPassword); + g_inputCount++; + g_inputPassword[g_inputCount] = '\0'; + g_cursorPositionX += myCursorOffset; + myUiCursor->SetX(g_cursorPositionX); + return true; + } + +private: + UILabel* myUiLabel; + UILabel* myUiCursor; + int mIi; + int myCursorOffset; +}; + +class TestBtnOnClickEnterChangeListener : public OHOS::UIView::OnClickListener { +public: + explicit TestBtnOnClickEnterChangeListener(SettingWifiInputPasswordAbilitySlice* slice) : mySlice(slice) {} + ~TestBtnOnClickEnterChangeListener() {} + bool OnClick(UIView& view, const ClickEvent& event) override + { + WpaScanReconnect(g_inputSsid, g_inputPassword, HIDDEN_OPEN); + int err = memset_s(g_inputSsid, sizeof(g_inputSsid), 0, sizeof(g_inputSsid)); + if (err != EOK) { + printf("[ERROR]memset_s failed, err = %d\n", err); + return false; + } + err = memset_s(g_inputPassword, sizeof(g_inputPassword), 0, sizeof(g_inputPassword)); + if (err != EOK) { + printf("[ERROR]memset_s failed, err = %d\n", err); + return false; + } + mySlice->Terminate(); + return true; + } + SettingWifiInputPasswordAbilitySlice* mySlice; +}; + +void SettingWifiInputPasswordAbilitySlice::SetButtonListener(void) +{ + auto onClick = [this](UIView& view, const Event& event) -> bool { + Terminate(); + return true; + }; + buttonBackListener_ = new EventListener(onClick, nullptr); +} + +void SettingWifiInputPasswordAbilitySlice::SetHead(void) +{ + headView_ = new UIViewGroup(); + rootView_->Add(headView_); + headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT); + headView_->SetStyle(STYLE_BACKGROUND_OPA, 0); + headView_->SetTouchable(true); + headView_->SetOnClickListener(buttonBackListener_); + + UIImageView* imageView = new UIImageView(); + headView_->Add(imageView); + imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_BACK); + + UILabel* lablelFont = new UILabel(); + lablelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT); + lablelFont->SetText(g_inputSsid); + lablelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE); + lablelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR); + headView_->Add(lablelFont); +} + +void SettingWifiInputPasswordAbilitySlice::SetInput(void) +{ + inputView_ = new UIViewGroup(); + inputView_->SetPosition(INPUT_X, INPUT_Y, INPUT_WIDTH, INPUT_HEIGHT); + + inputView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + inputView_->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + inputView_->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + rootView_->Add(inputView_); + + lablelInputText_ = new UILabel(); + lablelInputText_->SetPosition(INPUT_TEXT_X, INPUT_TEXT_Y, INPUT_TEXT_WIDTH, INPUT_TEXT_HEIGHT); + lablelInputText_->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + lablelInputText_->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + lablelInputText_->SetText("输入密码"); + lablelInputText_->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + inputView_->Add(lablelInputText_); + + lablelCursorText_ = new UILabel(); + lablelCursorText_->SetPosition(g_cursorPositionX, INPUT_CURSOR_Y, INPUT_CURSOR_WIDTH, INPUT_CURSOR_HEIGHT); + lablelCursorText_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::GetColorFromRGB(0x0D, 0x9F, 0xF8))); + lablelCursorText_->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + inputView_->Add(lablelCursorText_); + + UIViewGroup* enterView = new UIViewGroup(); + enterView->SetPosition(INPUT_ENTER_X, INPUT_ENTER_Y, INPUT_ENTER_WIDTH, INPUT_ENTER_HEIGHT); + enterView->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::GetColorFromRGB(0x0D, 0x9F, 0xF8))); + enterView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL); + enterView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS); + inputView_->Add(enterView); + + UIImageView* imageView = new UIImageView(); + imageView->SetPosition(INPUT_IMAGE_X, INPUT_IMAGE_Y, INPUT_IMAGE_WIDTH, INPUT_IMAGE_HEIGHT); + imageView->SetSrc(DE_IMAGE_ENTER); + enterView->Add(imageView); + imageView->SetTouchable(true); + clickLeftListener_ = new TestBtnOnClickEnterChangeListener(this); + imageView->SetOnClickListener(clickLeftListener_); +} + +void SettingWifiInputPasswordAbilitySlice::AddInputKeyBoardZero(void) +{ + char buf[8] = {0}; + int myUseX = BUTTON_INTERVAL_X; + int myUseY = 198; + int inputNum = 0; + UILabelButton* inputButton = new UILabelButton(); + + inputButton->SetPosition(myUseX, myUseY); + sprintf_s(buf, sizeof(buf), "%d", inputNum); + inputButton->SetWidth(BUTTON_WIDTH); + inputButton->SetHeight(BUTTON_HEIGHT); + inputButton->SetText(buf); + inputButton->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + inputButton->SetStyle(STYLE_BORDER_RADIUS, RECT_RADIUS); + inputButton->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + inputButton->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE); + + clickLeftListener_ = new TestBtnOnClickInputPasswordChangeListener((UILabel*)lablelInputText_, (UILabel*)lablelCursorText_, inputNum, CURSOR_POSITION_OFFSET); + inputButton->SetOnClickListener(clickLeftListener_); + scrollView_->Add(inputButton); +} + +void SettingWifiInputPasswordAbilitySlice::SetScrollView(void) +{ + char buf[8] = {0}; + int inputNum; + scrollView_ = new UIScrollView(); + scrollView_->SetPosition(SCROLL_WIFI_INPUT_X, SCROLL_WIFI_INPUT_Y, SCROLL_WIFI_INPUT_WIDTH, + SCROLL_WIFI_INPUT_WIDTH); + scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR); + scrollView_->SetXScrollBarVisible(false); + scrollView_->SetYScrollBarVisible(false); + rootView_->Add(scrollView_); + for (int countFirst = 0; countFirst < BUTTON_NUM; countFirst++) { + for (int countSecound = 0; countSecound < BUTTON_NUM; countSecound++) { + int myUseX = countSecound * BUTTON_INTERVAL_X; + int myUseY = countFirst * BUTTON_INTERVAL_Y; + inputNum = ((countFirst * BUTTON_NUM) + countSecound + 1); + UILabelButton* inputButton = new UILabelButton(); + inputButton->SetPosition(myUseX, myUseY, BUTTON_WIDTH, BUTTON_HEIGHT); + int err = sprintf_s(buf, sizeof(buf), "%d", inputNum); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + return; + } + inputButton->SetText(buf); // SetText is system functions + inputButton->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR); + inputButton->SetStyle(STYLE_BORDER_RADIUS, RECT_RADIUS); + inputButton->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR); + inputButton->SetFont(DE_FONT_OTF, DE_CONTENT_FONT_SIZE); + UIView::OnClickListener* clickLeftListener = nullptr; + clickLeftListener = new TestBtnOnClickInputPasswordChangeListener((UILabel*)lablelInputText_, + (UILabel*)lablelCursorText_, inputNum, CURSOR_POSITION_OFFSET); + inputButton->SetOnClickListener(clickLeftListener); + scrollView_->Add(inputButton); + } + } + AddInputKeyBoardZero(); +} + +void SettingWifiInputPasswordAbilitySlice::OnStart(const Want& want) +{ + printf("[LOG]receive the data -> %s\n", reinterpret_cast(want.data)); + AbilitySlice::OnStart(want); + + g_inputSsid = reinterpret_cast(want.data); + rootView_ = RootView::GetWindowRootView(); + + int err = memset_s(g_inputPassword, sizeof(g_inputPassword), 0, g_maxPassword); + if (err != EOK) { + printf("[ERROR]memcpy_s failed, err = %d\n", err); + return; + } + g_inputCount = 0; + g_cursorPositionX = 20; // 20 + rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT); + rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR); + SetButtonListener(); + SetHead(); + SetInput(); + SetScrollView(); + SetUIContent(rootView_); +} + +void SettingWifiInputPasswordAbilitySlice::OnInactive() +{ + AbilitySlice::OnInactive(); +} + +void SettingWifiInputPasswordAbilitySlice::OnActive(const Want& want) +{ + int err; + lablelInputText_->SetText("输入密码"); + g_cursorPositionX = 20; // 20 + lablelCursorText_->SetX(g_cursorPositionX); + g_inputCount = 0; + err = memset_s(g_inputPassword, sizeof(g_inputPassword), 0, sizeof(g_inputPassword)); + if (err != EOK) { + printf("[ERROR]memset_s g_inputPassword failed, err = %d\n", err); + return; + } + AbilitySlice::OnActive(want); +} + +void SettingWifiInputPasswordAbilitySlice::OnBackground() +{ + AbilitySlice::OnBackground(); +} + +void SettingWifiInputPasswordAbilitySlice::OnStop() +{ + AbilitySlice::OnStop(); +} +} // namespace OHOS diff --git a/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.h b/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.h new file mode 100644 index 0000000000000000000000000000000000000000..118d3fc09847de7718d9361ea705335626d7a397 --- /dev/null +++ b/setting/setting/src/main/cpp/setting_wifi_input_password_ability_slice.h @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_SETTING_WIFI_INPUT_PASSWORD_ABILITY_SLICE_H +#define OHOS_SETTING_WIFI_INPUT_PASSWORD_ABILITY_SLICE_H +#include "components/ui_checkbox.h" +#include "ability_loader.h" +#include "components/ui_image_view.h" +#include "components/ui_label.h" +#include "components/ui_label_button.h" +#include "components/ui_list.h" +#include "components/ui_scroll_view.h" +#include "components/ui_view_group.h" +#include "event_listener.h" +#include "parameter.h" +#include "setting_utils.h" +#include "wpa_work.h" +#include "common/task.h" +#include "setting_wifi_input_password_ability_slice.h" + +namespace OHOS { +class SettingWifiInputPasswordAbilitySlice : public AbilitySlice { +public: + SettingWifiInputPasswordAbilitySlice() : headView_(nullptr), inputView_(nullptr), scrollView_(nullptr), + rootView_(nullptr), lablelInputText_(nullptr), + buttonBackListener_(nullptr), lablelCursorText_(nullptr) {} + virtual ~SettingWifiInputPasswordAbilitySlice(); + +protected: + void OnStart(const Want& want) override; + void OnInactive() override; + void OnActive(const Want& want) override; + void OnBackground() override; + void OnStop() override; + +private: + void SetHead(); + void SetInput(); + void AddInputKeyBoardZero(); + void SetScrollView(); + void SetButtonListener(); + UIViewGroup* headView_; + UIViewGroup* inputView_; + UILabel* lablelCursorText_; + UIScrollView* scrollView_; + RootView* rootView_; + UILabel* lablelInputText_; + EventListener* buttonBackListener_; + + constexpr static int CURSOR_POSITION_OFFSET = 16; + constexpr static int RECT_RADIUS = 6; + constexpr static int INPUT_X = 36; + constexpr static int INPUT_Y = 108; + constexpr static int INPUT_WIDTH = 888; + constexpr static int INPUT_HEIGHT = 54; + constexpr static int INPUT_RADIUS = 32; + constexpr static int INPUT_CURSOR_X = 20; + constexpr static int INPUT_CURSOR_Y = 10; + constexpr static int INPUT_CURSOR_WIDTH = 2; + constexpr static int INPUT_CURSOR_HEIGHT = 36; + constexpr static int INPUT_TEXT_X = 24; + constexpr static int INPUT_TEXT_Y = 11; + constexpr static int INPUT_TEXT_WIDTH = 480; + constexpr static int INPUT_TEXT_HEIGHT = 38; + constexpr static int INPUT_FONT_SIZE = 28; + constexpr static int INPUT_ENTER_X = 824; + constexpr static int INPUT_ENTER_Y = 6; + constexpr static int INPUT_ENTER_WIDTH = 58; + constexpr static int INPUT_ENTER_HEIGHT = 42; + constexpr static int INPUT_ENTER_RADIUS = 32; + + constexpr static int INPUT_IMAGE_X = 10; + constexpr static int INPUT_IMAGE_Y = 2; + constexpr static int INPUT_IMAGE_WIDTH = 38; + constexpr static int INPUT_IMAGE_HEIGHT = 38; + + constexpr static int BUTTON_NUM = 3; + + constexpr static int BUTTON_INTERVAL_X = 172; + constexpr static int BUTTON_INTERVAL_Y = 66; + constexpr static int BUTTON_WIDTH = 160; + constexpr static int BUTTON_HEIGHT = 54; + + constexpr static int SCROLL_WIFI_INPUT_X = 228; + constexpr static int SCROLL_WIFI_INPUT_Y = 198; + constexpr static int SCROLL_WIFI_INPUT_WIDTH = 530; + constexpr static int SCROLL_WIFI_INPUT_HEIGHT = 252; +}; + +} // namespace OHOS +#endif diff --git a/setting/setting/src/main/cpp/wpa_work.c b/setting/setting/src/main/cpp/wpa_work.c new file mode 100644 index 0000000000000000000000000000000000000000..c78d8dc31bcecf0658e74c482efb8e01a35d70b7 --- /dev/null +++ b/setting/setting/src/main/cpp/wpa_work.c @@ -0,0 +1,472 @@ +/* + * Copyright (c) 2020 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. + */ + +#include "wpa_work.h" +#include + +#define WPA_IFACE_NAME "wlan0" +#define WIFI_AUTH_FAILED_REASON_STR "WRONG_KEY" +#define WIFI_AUTH_FAILED_REASON_CODE "reason=2" +#define WPA_CTRL_REQUEST_OK "OK" +#define WPA_CTRL_REQUEST_FAIL "FAIL" + +#define SAMPLE_INFO(format, args...) \ + do { \ + fprintf(stderr, "\033[1;32m WpaCliSample(%s:%d):\t\033[0m" format, __func__, __LINE__, ##args); \ + printf("\n"); \ + } while (0) + +#define SAMPLE_ERROR(format, args...) \ + do { \ + fprintf(stderr, "\033[1;31m WpaCliSample(%s:%d):\t\033[0m" format, __func__, __LINE__, ##args); \ + printf("\n"); \ + } while (0) + + +static struct wpa_ctrl *g_monitorConn = NULL; +static pthread_t g_threadId = 0; +pthread_mutex_t g_mutex; +static char g_mySsidD[40][40] = {0}; +static int g_ssidCount = 0; + +static char g_useSsidD[40][40] = {0}; +static int g_useSsidCount = 0; + +static struct wpa_ctrl *g_ctrlConn = NULL; +static pthread_t g_wpaThreadId = 0; +static pthread_t g_scanThreadId = 0; +static int g_scanAvailable = 0; +static int g_Connect = 0; + +static void DumpString(const char *buf, int len, const char *tag) +{ + SAMPLE_INFO("%s dump start.", tag); + for (int i = 0; i < len; i++) { + printf("%c", buf[i]); + } + printf("\n"); + + printf("\n"); + SAMPLE_INFO("%s dump end.", tag); +} + +static int StrMatch(const char *a, const char *b) +{ + return strncmp(a, b, strlen(b)) == 0; +} + +static int SendCtrlCommand(const char *cmd, char *reply, size_t *replyLen) +{ + size_t len = *replyLen - 1; + wpa_ctrl_request(g_ctrlConn, cmd, strlen(cmd), reply, &len, 0); + DumpString(reply, len, "SendCtrlCommand raw return"); + if (len != 0 && !StrMatch(reply, WPA_CTRL_REQUEST_FAIL)) { + *replyLen = len; + return 0; + } + SAMPLE_ERROR("send ctrl request [%s] failed.", cmd); + return -1; +} + +static void printUseSsid(void) +{ + for (int i = 0; i < g_useSsidCount; i++) { + printf("[LOG]thegUseSsidD[%d]->%s\n", i, g_useSsidD[i]); + } +} + +static void ProcessScanResult(const char *buf, int len) +{ + int myUse1 = 1; + int myUse2 = 2; + int myUse5 = 5; + + int err = memset_s(g_mySsidD, sizeof(g_mySsidD), 0, sizeof(g_mySsidD)); + g_ssidCount = 0; + if (err != EOK) { + printf("[ERROR]memset_s g_ssidCount failed, err = %d\n", err); + return; + } + for (int i = 0; i < len; i++) { + if (buf[i] == 'E' && buf[i + myUse1] == 'S' && buf[i + myUse2] == 'S') { + int j; + printf("[ERROR]get the ESS i->%d\n", i); + i = i + myUse5; + for (j = 0; i < len && buf[i] != '\n'; i++) { + g_mySsidD[g_ssidCount][j] = buf[i]; + j++; + } + + g_ssidCount++; + } + } +} + +void ResetSSIDBuff(void) +{ + g_useSsidCount = 0; +} + +int GetIdNum(void) +{ + return g_useSsidCount; +} + +int GetCurrentConnInfo(char *ssid, int len) +{ + int offset = 5; + char connInfo[2048] = {0}; + size_t infoLen = sizeof(connInfo); + if (g_Connect == 0) { + printf("[LOG]GetCurrentConnInfo g_Connect -> 0 \n"); + return -1; + } + + int ret = SendCtrlCommand("STATUS", connInfo, &infoLen); + if (ret == -1) { + printf("[ERROR]GetCurrentConnInfo Command(STATUS) Error \n"); + return -1; + } + DumpString(connInfo, infoLen, "connInfo"); + printf("[LOG]end the DumpStrint\n"); + char *pos = strstr(connInfo, "ssid="); + if (pos == NULL) { + printf("[ERROR]strstr(ssid) is null"); + return -1; + } + pos += offset; + pos = strstr(pos, "ssid="); + if (pos == NULL) { + printf("[ERROR]secound strstr(ssid) is null"); + return -1; + } + char *end = strchr(pos, '\n'); + if (end == NULL) { + printf("[ERROR]secound strstr(end) is null"); + return -1; + } + int ssidLen = end - pos - offset; + if (len < ssidLen) { + SAMPLE_ERROR("ssid len = %d, buffer len = %d", ssidLen, len); + return -1; + } + int i = 0; + int myOffset = 5; + for (pos += myOffset; pos < end; pos++, i++) { + ssid[i] = *pos; + } + printf("[LOG]getssid->%s\n", ssid); + return 0; +} + +void ExitWpaScan(void) +{ + int ret; + if (g_wpaThreadId != 0) { + ret = pthread_cancel(g_wpaThreadId); + if (ret != 0) { + printf("[ERROR]pthread_cancel(g_wpaThreadId) ret -> %d \n", ret); + } + } +} + +void ExitWpa(void) +{ + int ret; + DeinitWifiService(); + if (g_threadId != 0) { + ret = pthread_cancel(g_threadId); + if (ret != 0) { + printf("[ERROR]pthread_cancel(g_threadId) ret -> %d \n", ret); + } + } + + ret = pthread_mutex_destroy(&g_mutex); + if (ret != 0) { + printf("[ERROR]pthread_mutex_destroy ret -> %d \n", ret); + } +} + +int GetAndResetScanStat(void) +{ + int ret = g_scanAvailable; + g_scanAvailable = 0; + return ret; +} + +char *GetSsid(const int ssidNum) +{ + return g_useSsidD[ssidNum]; +} + +void LockWifiData() +{ + pthread_mutex_lock(&g_mutex); +} + +void UnLockWifiData() +{ + pthread_mutex_unlock(&g_mutex); +} + +static void CheckSsid(void) +{ + int i, ret, err; + int chrckSsid = 0x5C; + + err = memset_s(g_useSsidD, sizeof(g_useSsidD), 0, sizeof(g_useSsidD)); + if (err != EOK) { + printf("[ERROR]memset_s g_useSsidD failed, err = %d\n", err); + return; + } + g_useSsidCount = 0; + for (i = 0; i < g_ssidCount; i++) { + int j = i + 1; + for (; j < g_ssidCount; j++) { + ret = strcmp(g_mySsidD[i], g_useSsidD[j]); + if (ret == 0) { + break; + } + } + if ((g_mySsidD[i][0] == chrckSsid) || (g_mySsidD[i][0] == 0)) { + continue; + } + if (j == g_ssidCount) { + err = strcpy_s(g_useSsidD[g_useSsidCount], sizeof(g_useSsidD[g_useSsidCount]), g_mySsidD[i]); + if (err != EOK) { + printf("[ERROR]strcpy_s g_useSsidD failed, err = %d\n", err); + continue; + } + g_useSsidCount++; + } + } +} + +static void WifiEventHandler(char *rawEvent, int len) +{ + char *pos = rawEvent; + if (*pos == '<') { + pos = strchr(pos, '>'); + if (pos) { + pos++; + } else { + pos = rawEvent; + } + } + if (StrMatch(pos, WPA_EVENT_CONNECTED)) { + SAMPLE_INFO("WIFI_EVENT_CONNECTED"); + g_Connect = 1; + return; + } + if (StrMatch(pos, WPA_EVENT_SCAN_RESULTS)) { + pthread_mutex_lock(&g_mutex); + SAMPLE_INFO("WIFI_EVENT_SCAN_DONE"); + char scanResult[4096] = {0}; + size_t scanLen = sizeof(scanResult); + SendCtrlCommand("SCAN_RESULTS", scanResult, &scanLen); + + ProcessScanResult(scanResult, scanLen); + CheckSsid(); + g_scanAvailable = 1; + printUseSsid(); + pthread_mutex_unlock(&g_mutex); + return; + } + if (StrMatch(pos, WPA_EVENT_TEMP_DISABLED) && strstr(pos, WIFI_AUTH_FAILED_REASON_STR)) { + SAMPLE_INFO("WIFI_EVENT_WRONG_KEY"); + return; + } + if (StrMatch(pos, WPA_EVENT_DISCONNECTED) && !strstr(pos, WIFI_AUTH_FAILED_REASON_CODE)) { + SAMPLE_INFO("WIFI_EVENT_DISCONNECTED"); + return; + } +} + +static void CliRecvPending(void) +{ + while (wpa_ctrl_pending(g_monitorConn)) { + char buf[4096]; + size_t len = sizeof(buf) - 1; + if (wpa_ctrl_recv(g_monitorConn, buf, &len) == 0) { + buf[len] = '\0'; + SAMPLE_INFO("event received %s", buf); + WifiEventHandler(buf, len); + } else { + SAMPLE_INFO("could not read pending message."); + break; + } + } +} + +static void* MonitorTask(void *args) +{ + (void)args; + int fd, ret; + fd_set rfd; + while (1) { + fd = wpa_ctrl_get_fd(g_monitorConn); + FD_ZERO(&rfd); + FD_SET(fd, &rfd); + ret = select(fd + 1, &rfd, NULL, NULL, NULL); + if (ret <= 0) { + SAMPLE_INFO("select failed ret = %d\n", ret); + break; + } + CliRecvPending(); + sleep(1); + } + return NULL; +} + +static void TestNetworkConfig(const char *gSsid, const char *gPassWord) +{ + char networkId[20] = {0}; + size_t networkIdLen = sizeof(networkId); + int ret = SendCtrlCommand("DISCONNECT", networkId, &networkIdLen); + ret += SendCtrlCommand("ADD_NETWORK", networkId, &networkIdLen); + if (ret != 0) { + SAMPLE_ERROR("add network failed."); + return; + } + SAMPLE_INFO("add network success, network id [%.*s]", networkIdLen, networkId); + char reply[100] = {0}; + size_t replyLen = sizeof(reply); + char cmd[200] = {0}; + int err = sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s ssid \"%s\"", networkIdLen, networkId, gSsid); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + return; + } + ret += SendCtrlCommand(cmd, reply, &replyLen); + replyLen = sizeof(reply); + err = sprintf_s(cmd, sizeof(cmd), "SET_NETWORK %.*s psk \"%s\"", networkIdLen, networkId, gPassWord); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + return; + } + ret += SendCtrlCommand(cmd, reply, &replyLen); + replyLen = sizeof(reply); + err = sprintf_s(cmd, sizeof(cmd), "ENABLE_NETWORK %.*s", networkIdLen, networkId); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + return; + } + ret += SendCtrlCommand(cmd, reply, &replyLen); + replyLen = sizeof(reply); + ret += SendCtrlCommand("RECONNECT", reply, &replyLen); + replyLen = sizeof(reply); + if (ret == 0) { + SAMPLE_INFO("network config success."); + return; + } + err = sprintf_s(cmd, sizeof(cmd), "REMOVE_NETWORK %.*s", networkIdLen, networkId); + if (err < 0) { + printf("[ERROR]sprintf_s failed, err = %d\n", err); + return; + } + SendCtrlCommand(cmd, reply, &replyLen); + SAMPLE_ERROR("network config failed remove network [%.*s].", networkIdLen, networkId); +} + +int InitControlInterface() +{ + int reTime = 20; + int i = 0; + while (i++ < reTime) { // check wpa init success + g_ctrlConn = wpa_ctrl_open(WPA_IFACE_NAME); + if (g_ctrlConn > 0) { + break; + } + sleep(1); + } + g_monitorConn = wpa_ctrl_open(WPA_IFACE_NAME); // create control interface for event monitor + if (!g_ctrlConn || !g_monitorConn) { + SAMPLE_ERROR("open wpa control interface failed."); + return -1; + } + if (wpa_ctrl_attach(g_monitorConn) == 0) { // start monitor + int ret = pthread_create(&g_wpaThreadId, NULL, MonitorTask, NULL); // create thread for read event + if (ret != 0) { + printf("[ERROR]thread error %s\n", strerror(ret)); + return -1; + } + return 0; + } + return -1; +} + +void* WpaScanThread(void *args) +{ + int mySleep = 2; + sleep(mySleep); + if (g_ctrlConn == NULL) { + InitControlInterface(); + } + char reply[100] = {0}; + size_t replyLen = sizeof(reply); + g_scanAvailable = 0; + SendCtrlCommand("SCAN", reply, &replyLen); + return NULL; +} + +void WpaScanReconnect(const char *gSsid, const char *gPassWord, const int hiddenSwitch) +{ + if (HIDDEN_OPEN == hiddenSwitch) { + TestNetworkConfig(gSsid, gPassWord); + } else { + int ret = pthread_create(&g_scanThreadId, NULL, WpaScanThread, NULL); // create thread for read event + if (ret != 0) { + printf("[ERROR]thread error %s\n", strerror(ret)); + return; + } + } +} + +static void *ThreadMain() +{ + prctl(PR_SET_NAME, "WPA_THREAD"); + int i = 0; + int myfor = 5; + char *arg[20] = {0}; + arg[i] = (char*)"testhhf"; + arg[++i] = (char*)"-i"; + arg[++i] = (char*)"wlan0"; + arg[++i] = (char*)"-c"; + arg[++i] = (char*)"/storage/app/run/com.huawei.setting/setting/assets/setting/resources/base/element/wpa_supplicant.conf"; + + for (i = 0; i < myfor; i++) { + printf("[LOG]arg[%d]->%s \n", i, arg[i]); + } + wpa_main(myfor, arg); + return NULL; +} + +void WpaClientStart(void) +{ + static int runStatus = 0; + if (runStatus == 0) { + int ret = pthread_create(&g_threadId, NULL, ThreadMain, NULL); + if (ret != 0) { + printf("[ERROR]thread error %s\n", strerror(ret)); + return; + } + ret = pthread_mutex_init(&g_mutex, NULL); + if (ret != 0) { + printf("[ERROR]pthread_mutex_init error %s\n", strerror(ret)); + return; + } + runStatus = 1; + } +} diff --git a/setting/setting/src/main/cpp/wpa_work.h b/setting/setting/src/main/cpp/wpa_work.h new file mode 100644 index 0000000000000000000000000000000000000000..c8506e618dbf601cbcdabeb18efd4f232dd5af6a --- /dev/null +++ b/setting/setting/src/main/cpp/wpa_work.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2020 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. + */ + +#ifndef OHOS_WPA_WORK_H +#define OHOS_WPA_WORK_H + +#include +#include +#include + +#include "parameter.h" +#include "pthread.h" +#include "securec.h" +#include "wpa_ctrl.h" +#include + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif /* __cplusplus */ + #define HIDDEN_CLOSE 0 + #define HIDDEN_OPEN 1 + void ExitWpaScan(void); + void ExitWpa(void); + int wpa_main(int argc, char *argv[]); + int GetCurrentConnInfo(char *ssid, int len); + void DeinitWifiService(); + void ResetSSIDBuff(void); + int GetIdNum(void); + int GetAndResetScanStat(void); + char *GetSsid(int ssidNum); + void WpaScanReconnect(const char *gSsid, const char *gPassWord, const int hiddenSwitch); + void WpaClientStart(void); + void LockWifiData(void); + void UnLockWifiData(void); +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ +#endif diff --git a/setting/setting/src/main/resources/base/element/wpa_supplicant.conf b/setting/setting/src/main/resources/base/element/wpa_supplicant.conf new file mode 100644 index 0000000000000000000000000000000000000000..029e475da026b94752af4abe78f745f93d3d4d20 --- /dev/null +++ b/setting/setting/src/main/resources/base/element/wpa_supplicant.conf @@ -0,0 +1,6 @@ +country=GB +ctrl_interface=udp +network={ +} + + diff --git a/setting/setting/src/main/resources/base/media/app.png b/setting/setting/src/main/resources/base/media/app.png new file mode 100644 index 0000000000000000000000000000000000000000..8a5d6c0daa77187134e49187e2703d221fcc3d86 Binary files /dev/null and b/setting/setting/src/main/resources/base/media/app.png differ diff --git a/setting/setting/src/main/resources/base/media/back.png b/setting/setting/src/main/resources/base/media/back.png new file mode 100644 index 0000000000000000000000000000000000000000..9dc41df327d0f11048e646df6d1467e9681703b7 Binary files /dev/null and b/setting/setting/src/main/resources/base/media/back.png differ diff --git a/setting/setting/src/main/resources/base/media/enter.png b/setting/setting/src/main/resources/base/media/enter.png new file mode 100644 index 0000000000000000000000000000000000000000..ffbfa9684cdc85799b3f3a58492d1c9fc62fc65d Binary files /dev/null and b/setting/setting/src/main/resources/base/media/enter.png differ diff --git a/setting/setting/src/main/resources/base/media/forward.png b/setting/setting/src/main/resources/base/media/forward.png new file mode 100644 index 0000000000000000000000000000000000000000..9d63c114d24c744b46ccf72a820f832b86c2b63b Binary files /dev/null and b/setting/setting/src/main/resources/base/media/forward.png differ diff --git a/setting/setting/src/main/resources/base/media/setting.png b/setting/setting/src/main/resources/base/media/setting.png new file mode 100644 index 0000000000000000000000000000000000000000..f5008902b93f0f14e8deb3b4aec35c9d0db4b92d Binary files /dev/null and b/setting/setting/src/main/resources/base/media/setting.png differ