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 "app_ability_slice.h"
17 #include "gfx_utils/style.h"
18
19 namespace OHOS {
REGISTER_AS(AppAbilitySlice)20 REGISTER_AS(AppAbilitySlice)
21
22 AppAbilitySlice::~AppAbilitySlice()
23 {
24 if (scrollView_) {
25 DeleteChildren(scrollView_);
26 scrollView_ = nullptr;
27 }
28 if (headView_) {
29 DeleteChildren(headView_);
30 headView_ = nullptr;
31 }
32 if (buttonBackListener_) {
33 delete buttonBackListener_;
34 buttonBackListener_ = nullptr;
35 }
36 if (buttonAppInfoListener_) {
37 delete buttonAppInfoListener_;
38 buttonAppInfoListener_ = nullptr;
39 }
40 if (pBundleInfos_) {
41 ClearBundleInfo(pBundleInfos_);
42 }
43 }
44
SetButtonListener(void)45 void AppAbilitySlice::SetButtonListener(void)
46 {
47 auto onClick1 = [this](UIView& view, const Event& event) -> bool {
48 Want want1 = { nullptr };
49 AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("MainAbilitySlice");
50 if (nextSlice == nullptr) {
51 printf("[warning]undefined SettingWifiAbilitySlice\n");
52 } else {
53 Present(*nextSlice, want1);
54 }
55 return true;
56 };
57 buttonBackListener_ = new EventListener(onClick1, nullptr);
58 }
59
SetAppButtonListener(const char * appName)60 void AppAbilitySlice::SetAppButtonListener(const char* appName)
61 {
62 auto onClick2 = [this, appName](UIView& view, const Event& event) -> bool {
63 Want want1 = { nullptr };
64 bool ret = SetWantData(&want1, appName, strlen(appName) + 1);
65 if (ret != true) {
66 return false;
67 }
68 StartAbility(want1);
69 AbilitySlice* nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName("AppInfoAbilitySlice");
70 if (nextSlice == nullptr) {
71 printf("[warning]undefined SettingWifiAbilitySlice\n");
72 } else {
73 Present(*nextSlice, want1);
74 }
75 ClearWant(&want1);
76 return true;
77 };
78 buttonAppInfoListener_ = new EventListener(onClick2, nullptr);
79 }
80
SetHead()81 void AppAbilitySlice::SetHead()
82 {
83 headView_ = new UIViewGroup();
84 rootView_->Add(headView_);
85 headView_->SetPosition(DE_HEAD_X, DE_HEAD_Y, DE_HEAD_WIDTH, DE_HEAD_HEIGHT);
86 headView_->SetStyle(STYLE_BACKGROUND_OPA, 0);
87 headView_->SetTouchable(true);
88 headView_->SetOnClickListener(buttonBackListener_);
89
90 UIImageView* imageView = new UIImageView();
91 headView_->Add(imageView);
92 imageView->SetPosition(DE_HEAD_IMAGE_X, DE_HEAD_IMAGE_Y, DE_HEAD_IMAGE_WIDTH, DE_HEAD_IMAGE_HEIGHT);
93 imageView->SetSrc(DE_IMAGE_BACK);
94
95 UILabel* labelFont = new UILabel();
96 labelFont->SetPosition(DE_HEAD_TEXT_X, DE_HEAD_TEXT_Y, DE_HEAD_TEXT_WIDTH, DE_HEAD_TEXT_HEIGHT);
97 labelFont->SetText("应用");
98 labelFont->SetFont(DE_FONT_OTF, DE_HEAD_TEXT_SIZE);
99 labelFont->SetStyle(STYLE_TEXT_COLOR, DE_HEAD_TEXT_COLOR);
100 headView_->Add(labelFont);
101 }
102
SetAnAppInfo(const int count,BundleInfo & pBundleInfo)103 void AppAbilitySlice::SetAnAppInfo(const int count, BundleInfo& pBundleInfo)
104 {
105 UIViewGroup* itemView = new UIViewGroup();
106 char buff[64] = {0};
107 int useX = 0;
108 int useY = count * DE_ITEM_INTERVAL;
109 itemView->SetPosition(useX, useY, DE_BUTTON_WIDTH, DE_BUTTON_HEIGHT);
110 itemView->SetStyle(STYLE_BACKGROUND_COLOR, DE_BUTTON_BACKGROUND_COLOR);
111 itemView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL);
112 itemView->SetStyle(STYLE_BORDER_RADIUS, DE_BUTTON_RADIUS);
113 itemView->SetTouchable(true);
114
115 int err = strcpy_s(buff, sizeof(buff), pBundleInfo.bundleName);
116 if (err != EOK) {
117 printf("[ERROR]strcpy_s pBundleInfo.bundleName failed, err = %d\n", err);
118 return;
119 }
120 for (size_t i = 0; i < strlen(pBundleInfo.bundleName); i++) {
121 buff[i] = pBundleInfo.bundleName[i];
122 }
123 SetAppButtonListener(pBundleInfo.bundleName);
124 itemView->SetOnClickListener(buttonAppInfoListener_);
125 scrollView_->Add(itemView);
126
127 UIImageView* imageIdView = new UIImageView();
128 imageIdView->SetPosition(APP_IMAGE_X, APP_IMAGE_Y, APP_IMAGE_WIDTH, APP_IMAGE_HEIGHT);
129 imageIdView->SetStyle(STYLE_BACKGROUND_OPA, DE_OPACITY_ALL);
130 imageIdView->SetSrc(DE_IMAGE_APP);
131 itemView->Add(imageIdView);
132
133 int bundleNameOffset = 11;
134 UILabel* name = new UILabel();
135 name->SetPosition(APP_NAME_X, APP_NAME_Y, DE_TITLE_TEXT_WIDTH, DE_TITLE_TEXT_HEIGHT);
136 name->SetText(pBundleInfo.bundleName + bundleNameOffset);
137 name->SetFont(DE_FONT_OTF, DE_TITLE_TEXT_SIZE);
138 name->SetStyle(STYLE_TEXT_COLOR, DE_TITLE_TEXT_COLOR);
139 itemView->Add(name);
140
141 UIImageView* imageView = new UIImageView();
142 imageView->SetPosition(DE_FORWARD_IMG_X, DE_FORWARD_IMG_Y, DE_FORWARD_IMG_WIDTH, DE_FORWARD_IMG_HEIGHT);
143 imageView->SetSrc(DE_IMAGE_FORWORD);
144 itemView->Add(imageView);
145 }
146
147
SetScrollView()148 void AppAbilitySlice::SetScrollView()
149 {
150 scrollView_ = new UIScrollView();
151 scrollView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_SCROLL_COLOR);
152 scrollView_->SetPosition(DE_SCROLL_X, DE_SCROLL_Y, DE_SCROLL_WIDTH, DE_SCROLL_HEIGHT);
153 scrollView_->SetXScrollBarVisible(false);
154 scrollView_->SetYScrollBarVisible(true);
155 rootView_->Add(scrollView_);
156
157 uint8_t ret = -1;
158 int num = 0;
159 ret = GetBundleInfos(1, &pBundleInfos_, &num);
160 if (ret == 0) {
161 BundleInfo* pBundleInfo = pBundleInfos_;
162 for (int count = 0; count < num; count++, pBundleInfo++) {
163 printf("[LOG]pBundleInfo.bundleName->%s versionName->%s \n",
164 pBundleInfo->bundleName, pBundleInfo->versionName);
165 if (pBundleInfo->isSystemApp == false) {
166 SetAnAppInfo(count, *pBundleInfo);
167 }
168 }
169 }
170 }
171
OnStart(const Want & want)172 void AppAbilitySlice::OnStart(const Want& want)
173 {
174 AbilitySlice::OnStart(want);
175
176 rootView_ = RootView::GetWindowRootView();
177 rootView_->SetPosition(DE_ROOT_X, DE_ROOT_Y, DE_ROOT_WIDTH, DE_ROOT_HEIGHT);
178 rootView_->SetStyle(STYLE_BACKGROUND_COLOR, DE_ROOT_BACKGROUND_COLOR);
179 SetButtonListener();
180 SetHead();
181 SetScrollView();
182 SetUIContent(rootView_);
183 }
184
OnInactive()185 void AppAbilitySlice::OnInactive()
186 {
187 AbilitySlice::OnInactive();
188 }
189
OnActive(const Want & want)190 void AppAbilitySlice::OnActive(const Want &want)
191 {
192 AbilitySlice::OnActive(want);
193 }
194
OnBackground()195 void AppAbilitySlice::OnBackground()
196 {
197 AbilitySlice::OnBackground();
198 }
199
OnStop()200 void AppAbilitySlice::OnStop()
201 {
202 AbilitySlice::OnStop();
203 }
204 } // namespace OHOS
205