1/*
2 * Copyright (c) 2020 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "picture_ability_slice.h"
17#include "ability_env.h"
18#include "gallery_config.h"
19#include "gfx_utils/file.h"
20#include "securec.h"
21
22namespace OHOS {
23REGISTER_AS(PictureAbilitySlice)
24
25PictureAbilitySlice::~PictureAbilitySlice()
26{
27    Clear();
28}
29
30void PictureAbilitySlice::Clear()
31{
32    printf("PictureAbilitySlice::Clear() | start \n");
33    if (backIcon_ != nullptr) {
34        delete backIcon_;
35        backIcon_ = nullptr;
36    }
37    if (backIconListener_ != nullptr) {
38        delete backIconListener_;
39        backIconListener_ = nullptr;
40    }
41    if (backArea_ != nullptr) {
42        delete backArea_;
43        backArea_ = nullptr;
44    }
45    if (picture_ != nullptr) {
46        delete picture_;
47        picture_ = nullptr;
48    }
49    if (rootView_ != nullptr) {
50        RootView::DestroyWindowRootView(rootView_);
51        rootView_ = nullptr;
52    }
53    printf("PictureAbilitySlice::Clear() | end \n");
54}
55
56void PictureAbilitySlice::InitTitle()
57{
58    printf("PictureAbilitySlice::InitTitle | start \n");
59    backIcon_ = new UIImageView();
60    backIcon_->SetPosition(BACK_ICON_POSITION_X, BACK_ICON_POSITION_Y);
61    backIcon_->SetSrc(backIconAbsolutePath);
62    backIcon_->SetTouchable(true);
63
64    backArea_ = new UIViewGroup();
65    backArea_->SetPosition(0, 0, LABEL_POSITION_X, LABEL_HEIGHT);
66    backArea_->SetStyle(STYLE_BACKGROUND_OPA, 0);
67    backArea_->SetTouchable(true);
68
69    auto onClick = [this] (UIView &view, const Event &event) -> bool {
70        printf("############  terminate AS enter  #############\n");
71        Terminate();
72        printf("############  terminate AS exit  #############\n");
73        return true;
74    };
75    backIconListener_ = new EventListener(onClick, nullptr);
76    backIcon_->SetOnClickListener(backIconListener_);
77    backArea_->SetOnClickListener(backIconListener_);
78
79    backArea_->Add(backIcon_);
80    rootView_->Add(backArea_);
81}
82
83void PictureAbilitySlice::InitPicture(const char* path)
84{
85    printf("PictureAbilitySlice::InitPicture | start | %s\n", path);
86    picture_ = new UIImageView();
87    picture_->SetSrc(path);
88    int16_t imageWidth = picture_->GetWidth();
89    int16_t imageHeight = picture_->GetHeight();
90    if (imageWidth > ROOT_VIEW_WIDTH || imageHeight > ROOT_VIEW_HEIGHT) {
91        TransformMap transMap(picture_->GetOrigRect());
92        float scaleWidth = 1.0;
93        float scaleHeight = 1.0;
94        if (imageWidth > ROOT_VIEW_WIDTH) {
95            scaleWidth = static_cast<float>(ROOT_VIEW_WIDTH) / imageWidth;
96            printf("########## scaleWidth: %f \n", scaleWidth);
97        }
98        if (imageHeight > ROOT_VIEW_HEIGHT) {
99            scaleHeight = static_cast<float>(ROOT_VIEW_HEIGHT) / imageHeight;
100            printf("########## scaleHeight: %f \n", scaleHeight);
101        }
102        float scale = (scaleWidth < scaleHeight) ? scaleWidth : scaleHeight;
103        printf("########## scale: %f \n", scale);
104        transMap.Scale(Vector2<float>(scale, scale), Vector2<float>(0, 0));
105        picture_->SetTransformMap(transMap);
106        picture_->SetTransformAlgorithm(TransformAlgorithm::NEAREST_NEIGHBOR);
107        imageWidth = imageWidth * scale;
108        imageHeight = imageHeight * scale;
109    }
110    int16_t imagePosX = (ROOT_VIEW_WIDTH - imageWidth) / 2; // 2: half
111    int16_t imagePosY = (ROOT_VIEW_HEIGHT - imageHeight) / 2; // 2: half
112    printf("########## image pos x: %d  | y: %d \n", imagePosX, imagePosY);
113    picture_->SetPosition(imagePosX, imagePosY);
114
115    rootView_->Add(picture_);
116}
117
118void PictureAbilitySlice::OnStart(const Want &want)
119{
120    printf("######### PictureAbilitySlice::OnStart\n");
121    printf("receive the data -> %s\n", reinterpret_cast<char*>(want.data));
122    AbilitySlice::OnStart(want);
123
124    rootView_ = RootView::GetWindowRootView();
125    rootView_->SetPosition(ROOT_VIEW_POSITION_X, ROOT_VIEW_POSITION_Y);
126    rootView_->Resize(ROOT_VIEW_WIDTH, ROOT_VIEW_HEIGHT);
127    rootView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Black().full);
128
129    uint16_t imagePathLen = strlen(PHOTO_DIRECTORY) + strlen(reinterpret_cast<char*>(want.data)) + 1;
130    if (imagePathLen > MAX_PATH_LENGTH) {
131        printf("---- imagePathLen > MAX_PATH_LENGTH | %d", imagePathLen);
132        return;
133    }
134    char* imagePath = new char[imagePathLen + 1]();
135    if (sprintf_s(imagePath, imagePathLen + 1, "%s/%s", PHOTO_DIRECTORY, reinterpret_cast<char*>(want.data)) < 0) {
136        printf("PictureAbilitySlice::OnStart | imagePath\n");
137        delete[] imagePath;
138        imagePath = nullptr;
139        return;
140    }
141
142    const char* pathHeader = GetSrcPath();
143    if (sprintf_s(backIconAbsolutePath, MAX_PATH_LENGTH, "%s%s", pathHeader, BACK_ICON_PATH) < 0) {
144        printf("PictureAbilitySlice::OnStart | backIconAbsolutePath\n");
145        delete[] imagePath;
146        imagePath = nullptr;
147        return;
148    }
149
150    InitPicture(imagePath);
151    InitTitle();
152    delete[] imagePath;
153
154    SetUIContent(rootView_);
155}
156
157void PictureAbilitySlice::OnInactive()
158{
159    printf("PictureAbilitySlice::OnInactive\n");
160    AbilitySlice::OnInactive();
161}
162
163void PictureAbilitySlice::OnActive(const Want &want)
164{
165    printf("PictureAbilitySlice::OnActive\n");
166    AbilitySlice::OnActive(want);
167}
168
169void PictureAbilitySlice::OnBackground()
170{
171    printf("PictureAbilitySlice::OnBackground\n");
172    AbilitySlice::OnBackground();
173}
174
175void PictureAbilitySlice::OnStop()
176{
177    printf("PictureAbilitySlice::OnStop\n");
178    AbilitySlice::OnStop();
179    Clear();
180}
181}
182