• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // Reserved. Touch and hold to add a page.
117     arrPage_[0]->SetMatrix(APP_ROW_COUNT, APP_COL_COUNT);
118     arrPage_[0]->SetScale(0.6);    // 0.6 blank/icon width
119     for (int16_t i = 1; i < groupCount_; i++) {
120         arrPage_[i]->SetMatrix(APP_ROW_COUNT, 2 * APP_COL_COUNT); // 2 scale of first view's col count
121         arrPage_[i]->SetScale(0.69);    // 0.69 blank/icon width
122     }
123     AppEvent::GetInstance(this);
124     AppManage::SetViewGroup(AppEvent::ClickEvent, AppEvent::LongPressEvent, arrPage_, groupCount_);
125     BundleInfo* pBundleInfos = nullptr;
126     int count = 0;
127     if (appManage_->LauncherApp(&pBundleInfos, count)) {
128         for (int j = 0; j < count; j++) {
129             for (int i = 0; i < groupCount_; i++) {
130                 if (memcmp(LAUNCHER_BUNDLE_NAME, pBundleInfos[j].bundleName, strlen(pBundleInfos[j].bundleName)) == 0) {
131                     break;
132                 }
133                 if (memcmp(SCREENSAVER_BUNDLE_NAME, pBundleInfos[j].bundleName, strlen(pBundleInfos[j].bundleName)) == 0) {
134                     break;
135                 }
136                 if (pBundleInfos[j].abilityInfos->abilityType == SERVICE) {
137                     break;
138                 }
139                 AppInfo* app = new AppInfo();
140                 app->funcclick_ = AppEvent::ClickEvent;
141                 app->funclPress_ = AppEvent::LongPressEvent;
142                 if (pBundleInfos[j].bundleName) {
143                     (void)memcpy_s(app->appName_, sizeof(app->appName_), pBundleInfos[j].bundleName,
144                         strlen(pBundleInfos[j].bundleName));
145                     app->appName_[strlen(pBundleInfos[j].bundleName)] = 0;
146                 }
147                 if (pBundleInfos[j].abilityInfos[0].name) {
148                     (void)memcpy_s(app->abilityName_, sizeof(app->abilityName_), pBundleInfos[j].abilityInfos[0].name,
149                         strlen(pBundleInfos[j].abilityInfos[0].name));
150                     app->abilityName_[strlen(pBundleInfos[j].abilityInfos[0].name)] = 0;
151                 }
152                 if (pBundleInfos[j].bigIconPath) {
153                     (void)memcpy_s(app->appIconDir_, sizeof(app->appIconDir_), pBundleInfos[j].bigIconPath,
154                         strlen(pBundleInfos[j].bigIconPath));
155                     app->appIconDir_[strlen(pBundleInfos[j].bigIconPath)] = 0;
156                 }
157                 if (arrPage_[i]->AddApp(app)) {
158                     break;
159                 }
160             }
161         }
162     }
163     swipe_->SetCurrentPage(0);
164 }
165 
StartApp(AppInfo * app)166 void SwipeView::StartApp(AppInfo* app)
167 {
168     if (lpView_->GetStatus() == true) {
169         lpView_->RemoveLview();
170         return;
171     }
172     appManage_->StartApp(app);
173 }
174 
ShowLongPressView(AppInfo * app)175 void SwipeView::ShowLongPressView(AppInfo* app)
176 {
177     lpView_->Show(static_cast<UIViewGroup*>(app->button_->GetParent()), app);
178 }
179 
UninstallApp(AppInfo * app)180 void SwipeView::UninstallApp(AppInfo* app)
181 {
182     if (appManage_->UnInstallApp(app)) {
183         for (int16_t i = 0; i < groupCount_; i++) {
184             if (arrPage_[i]) {
185                 if (arrPage_[i]->RemoveApp(app->appName_)) {
186                     swipe_->Invalidate();
187                     return;
188                 }
189             }
190         }
191     }
192 }
193 
InstallApp(AppInfo * app)194 void SwipeView::InstallApp(AppInfo* app)
195 {
196     appManage_->InstallApp(app);
197     AppInfo* pApp = new AppInfo();
198     if (pApp == nullptr) {
199         return;
200     }
201     app->funcclick_ = AppEvent::ClickEvent;
202     app->funclPress_ = AppEvent::LongPressEvent;
203     int16_t i;
204     for (i = 0; i < groupCount_; i++) {
205         if (arrPage_[i]->AddApp(pApp)) {
206             break;
207         }
208     }
209     if (i == groupCount_) {
210         delete pApp;
211         pApp = nullptr;
212     }
213 }
214 
OnStop()215 void SwipeView::OnStop()
216 {
217     if (lpView_) {
218         delete lpView_;
219         lpView_ = nullptr;
220     }
221     if (appManage_) {
222         delete appManage_;
223         appManage_ = nullptr;
224     }
225     if (arrViewListener_) {
226         delete arrViewListener_;
227         arrViewListener_ = nullptr;
228     }
229     if (swipeLisstener_) {
230         delete swipeLisstener_;
231         swipeLisstener_ = nullptr;
232     }
233     if (timeWeatherView_) {
234         delete timeWeatherView_;
235         timeWeatherView_ = nullptr;
236     }
237     for (int i = 0; i < MAX_VIEWGROUP; i++) {
238         if (arrPage_[i]) {
239             delete arrPage_[i];
240             arrPage_[i] = nullptr;
241         }
242     }
243     DeleteChildren(swipe_);
244 }
245 } // namespace OHOS
246