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 <common/screen.h>
17
18 #include "swipe_view.h"
19
20 namespace OHOS {
21 AppEvent* AppEvent::appEvent_ = { nullptr };
22 SwipeView* AppEvent::nativeView_ = { nullptr };
23 static constexpr int32_t PERIOD_TIME = 60 * 1000; // 60 seconds
24
SwipeView(UILabel * titlellable,UILabel * taillable)25 SwipeView::SwipeView(UILabel* titlellable, UILabel* taillable)
26 {
27 lableTitle_ = titlellable;
28 lableTail_ = taillable;
29 groupCount_ = 0;
30 for (int i = 0; i < MAX_VIEWGROUP; i++) {
31 arrPage_[i] = nullptr;
32 }
33 arrViewListener_ = nullptr;
34 swipeLisstener_ = nullptr;
35 appManage_ = new AppManage();
36 lpView_ = new LongPressView(AppEvent::UninstallApp);
37 Task::Init();
38 Task::SetPeriod(PERIOD_TIME);
39 }
40
~SwipeView()41 SwipeView::~SwipeView()
42 {
43 OnStop();
44 }
45
SetUpSwipe()46 void SwipeView::SetUpSwipe()
47 {
48 swipe_ = new UISwipeView();
49 swipe_->SetPosition(0, LABLE_TITLE_HEIGHT, Screen::GetInstance().GetWidth(),
50 Screen::GetInstance().GetHeight() - LABLE_TITLE_HEIGHT - LABLE_TAIL_HEIGHT);
51 swipe_->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
52 swipe_->SetLoopState(true);
53 swipe_->SetAnimatorTime(20); // set swipe view animator time 20s
54 }
55
AddViewGroup()56 UIViewGroup* SwipeView::AddViewGroup()
57 {
58 if (groupCount_ >= MAX_VIEWGROUP) {
59 return nullptr;
60 }
61 UIViewGroup* viewGroup = new UIViewGroup();
62 if (viewGroup == nullptr) {
63 return viewGroup;
64 }
65 viewGroup->SetPosition(0, LABLE_TITLE_HEIGHT, Screen::GetInstance().GetWidth(),
66 Screen::GetInstance().GetHeight() - LABLE_TITLE_HEIGHT - LABLE_TAIL_HEIGHT);
67 viewGroup->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
68 groupCount_++;
69 ViewGroupPage* page = new ViewGroupPage(viewGroup);
70 arrPage_[groupCount_ - 1] = page;
71 swipe_->Add(viewGroup);
72 return viewGroup;
73 }
74
AddFirstViewGroup()75 UIViewGroup* SwipeView::AddFirstViewGroup()
76 {
77 UIViewGroup* firstView = new UIViewGroup();
78 firstView->SetPosition(0, LABLE_TITLE_HEIGHT, Screen::GetInstance().GetWidth(),
79 Screen::GetInstance().GetHeight() - LABLE_TITLE_HEIGHT - LABLE_TAIL_HEIGHT);
80 firstView->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
81
82 UIViewGroup* viewTimeWeather = new UIViewGroup();
83 // 2: set first view to 2 piece
84 viewTimeWeather->SetPosition(0, 0, firstView->GetWidth() / 2, firstView->GetHeight());
85 viewTimeWeather->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
86 timeWeatherView_ = new TimeWeatherView(viewTimeWeather);
87 timeWeatherView_->SetUpView();
88 firstView->Add(viewTimeWeather);
89
90 UIViewGroup* viewGroup = new UIViewGroup();
91 // 2 : get left && right view width and height
92 viewGroup->SetPosition(firstView->GetWidth() / 2, 0, firstView->GetWidth() / 2, firstView->GetHeight());
93 viewGroup->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
94 firstView->Add(viewGroup);
95 groupCount_++;
96 ViewGroupPage *page = new ViewGroupPage(viewGroup);
97 arrPage_[groupCount_ - 1] = page;
98
99 swipe_->Add(firstView);
100 firstView->Invalidate();
101 Task::TaskExecute();
102 return firstView;
103 }
104
OnSetUpView()105 void SwipeView::OnSetUpView()
106 {
107 SetUpSwipe();
108 swipeLisstener_ = new SwipeLisstener(lpView_, swipe_, lableTail_);
109 swipe_->SetOnSwipeListener(swipeLisstener_);
110 arrViewListener_ = new ViewPageListener(lpView_);
111 swipe_->SetOnClickListener(arrViewListener_);
112
113 AddFirstViewGroup();
114 AddViewGroup();
115 AddViewGroup();
116 double scale0 = 0.6;
117 double scale1 = 0.69;
118 // Reserved. Touch and hold to add a page.
119 arrPage_[0]->SetMatrix(APP_ROW_COUNT, APP_COL_COUNT);
120 arrPage_[0]->SetScale(scale0);
121 for (int16_t i = 1; i < groupCount_; i++) {
122 arrPage_[i]->SetMatrix(APP_ROW_COUNT, 2 * APP_COL_COUNT); // 2 scale of first view's col count
123 arrPage_[i]->SetScale(scale1); // 0.69 blank/icon width
124 }
125 AppEvent::GetInstance(this);
126 AppManage::SetViewGroup(AppEvent::ClickEvent, AppEvent::LongPressEvent, arrPage_, groupCount_);
127 BundleInfo* pBundleInfos = nullptr;
128 int count = 0;
129 if (appManage_->LauncherApp(&pBundleInfos, count)) {
130 BundleInfoScan(pBundleInfos, count);
131 }
132 swipe_->SetCurrentPage(0);
133 }
134
BundleInfoScan(BundleInfo * pBundleInfos,int count)135 void SwipeView::BundleInfoScan(BundleInfo* pBundleInfos, int count)
136 {
137 for (int j = 0; j < count; j++) {
138 for (int i = 0; i < groupCount_; i++) {
139 if (memcmp(LAUNCHER_BUNDLE_NAME, pBundleInfos[j].bundleName, strlen(pBundleInfos[j].bundleName)) == 0) {
140 break;
141 }
142 if (memcmp(SCREENSAVER_BUNDLE_NAME, pBundleInfos[j].bundleName, strlen(pBundleInfos[j].bundleName)) == 0) {
143 break;
144 }
145 if (pBundleInfos[j].abilityInfos->abilityType == SERVICE) {
146 break;
147 }
148 AppInfo* app = new AppInfo();
149 app->funcclick_ = AppEvent::ClickEvent;
150 app->funclPress_ = AppEvent::LongPressEvent;
151 if (pBundleInfos[j].bundleName) {
152 (void)memcpy_s(app->appName_, sizeof(app->appName_), pBundleInfos[j].bundleName,
153 strlen(pBundleInfos[j].bundleName));
154 app->appName_[strlen(pBundleInfos[j].bundleName)] = 0;
155 }
156 if (pBundleInfos[j].abilityInfos[0].name) {
157 (void)memcpy_s(app->abilityName_, sizeof(app->abilityName_), pBundleInfos[j].abilityInfos[0].name,
158 strlen(pBundleInfos[j].abilityInfos[0].name));
159 app->abilityName_[strlen(pBundleInfos[j].abilityInfos[0].name)] = 0;
160 }
161 if (pBundleInfos[j].bigIconPath) {
162 (void)memcpy_s(app->appIconDir_, sizeof(app->appIconDir_), pBundleInfos[j].bigIconPath,
163 strlen(pBundleInfos[j].bigIconPath));
164 app->appIconDir_[strlen(pBundleInfos[j].bigIconPath)] = 0;
165 }
166 if (arrPage_[i]->AddApp(app)) {
167 break;
168 }
169 }
170 }
171 }
172
StartApp(AppInfo * app)173 void SwipeView::StartApp(AppInfo* app)
174 {
175 if (lpView_->GetStatus() == true) {
176 lpView_->RemoveLview();
177 return;
178 }
179 appManage_->StartApp(app);
180 }
181
ShowLongPressView(AppInfo * app)182 void SwipeView::ShowLongPressView(AppInfo* app)
183 {
184 lpView_->Show(static_cast<UIViewGroup*>(app->button_->GetParent()), app);
185 }
186
UninstallApp(AppInfo * app)187 void SwipeView::UninstallApp(AppInfo* app)
188 {
189 if (appManage_->UnInstallApp(app)) {
190 for (int16_t i = 0; i < groupCount_; i++) {
191 if (arrPage_[i]) {
192 if (arrPage_[i]->RemoveApp(app->appName_)) {
193 swipe_->Invalidate();
194 return;
195 }
196 }
197 }
198 }
199 }
200
InstallApp(AppInfo * app)201 void SwipeView::InstallApp(AppInfo* app)
202 {
203 appManage_->InstallApp(app);
204 AppInfo* pApp = new AppInfo();
205 if (pApp == nullptr) {
206 return;
207 }
208 app->funcclick_ = AppEvent::ClickEvent;
209 app->funclPress_ = AppEvent::LongPressEvent;
210 int16_t i;
211 for (i = 0; i < groupCount_; i++) {
212 if (arrPage_[i]->AddApp(pApp)) {
213 break;
214 }
215 }
216 if (i == groupCount_) {
217 delete pApp;
218 pApp = nullptr;
219 }
220 }
221
OnStop()222 void SwipeView::OnStop()
223 {
224 if (lpView_) {
225 delete lpView_;
226 lpView_ = nullptr;
227 }
228 if (appManage_) {
229 delete appManage_;
230 appManage_ = nullptr;
231 }
232 if (arrViewListener_) {
233 delete arrViewListener_;
234 arrViewListener_ = nullptr;
235 }
236 if (swipeLisstener_) {
237 delete swipeLisstener_;
238 swipeLisstener_ = nullptr;
239 }
240 if (timeWeatherView_) {
241 delete timeWeatherView_;
242 timeWeatherView_ = nullptr;
243 }
244 for (int i = 0; i < MAX_VIEWGROUP; i++) {
245 if (arrPage_[i]) {
246 delete arrPage_[i];
247 arrPage_[i] = nullptr;
248 }
249 }
250 DeleteChildren(swipe_);
251 }
252 } // namespace OHOS
253