• 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 * titlellabel,UILabel * taillabel)25 SwipeView::SwipeView(UILabel* titlellabel, UILabel* taillabel)
26 {
27     labelTitle_ = titlellabel;
28     labelTail_ = taillabel;
29     groupCount_ = 0;
30     for (int i = 0; i < MAX_VIEWGROUP; i++) {
31         arrPage_[i] = nullptr;
32     }
33     arrViewListener_ = nullptr;
34     swipeListener_ = 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, LABEL_TITLE_HEIGHT, Screen::GetInstance().GetWidth(),
50         Screen::GetInstance().GetHeight() - LABEL_TITLE_HEIGHT - LABEL_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     viewGroup->SetPosition(0, LABEL_TITLE_HEIGHT, Screen::GetInstance().GetWidth(),
63         Screen::GetInstance().GetHeight() - LABEL_TITLE_HEIGHT - LABEL_TAIL_HEIGHT);
64     viewGroup->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
65     groupCount_++;
66     ViewGroupPage* page = new ViewGroupPage(viewGroup);
67     arrPage_[groupCount_ - 1] = page;
68     swipe_->Add(viewGroup);
69     return viewGroup;
70 }
71 
AddFirstViewGroup()72 UIViewGroup* SwipeView::AddFirstViewGroup()
73 {
74     UIViewGroup* firstView = new UIViewGroup();
75     firstView->SetPosition(0, LABEL_TITLE_HEIGHT, Screen::GetInstance().GetWidth(),
76         Screen::GetInstance().GetHeight() - LABEL_TITLE_HEIGHT - LABEL_TAIL_HEIGHT);
77     firstView->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
78 
79     UIViewGroup* viewTimeWeather = new UIViewGroup();
80     // 2: set first view to 2 piece
81     viewTimeWeather->SetPosition(0, 0, firstView->GetWidth() / 2, firstView->GetHeight());
82     viewTimeWeather->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
83     timeWeatherView_ = new TimeWeatherView(viewTimeWeather);
84     timeWeatherView_->SetUpView();
85     firstView->Add(viewTimeWeather);
86 
87     UIViewGroup* viewGroup = new UIViewGroup();
88     // 2 : get left && right view width and height
89     viewGroup->SetPosition(firstView->GetWidth() / 2, 0, firstView->GetWidth() / 2, firstView->GetHeight());
90     viewGroup->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
91     firstView->Add(viewGroup);
92     groupCount_++;
93     ViewGroupPage *page = new ViewGroupPage(viewGroup);
94     arrPage_[groupCount_ - 1] = page;
95 
96     swipe_->Add(firstView);
97     firstView->Invalidate();
98     Task::TaskExecute();
99     return firstView;
100 }
101 
OnSetUpView()102 void SwipeView::OnSetUpView()
103 {
104     SetUpSwipe();
105     swipeListener_ = new SwipeListener(lpView_, swipe_, labelTail_);
106     swipe_->SetOnSwipeListener(swipeListener_);
107     arrViewListener_ = new ViewPageListener(lpView_);
108     swipe_->SetOnClickListener(arrViewListener_);
109 
110     AddFirstViewGroup();
111     AddViewGroup();
112     AddViewGroup();
113     // Reserved. Touch and hold to add a page.
114     arrPage_[0]->SetMatrix(APP_ROW_COUNT, APP_COL_COUNT);
115     arrPage_[0]->SetScale(0.6);    // 0.6 blank/icon width
116     for (int16_t i = 1; i < groupCount_; i++) {
117         arrPage_[i]->SetMatrix(APP_ROW_COUNT, 2 * APP_COL_COUNT); // 2 scale of first view's col count
118         arrPage_[i]->SetScale(0.69);    // 0.69 blank/icon width
119     }
120     AppEvent::GetInstance(this);
121     AppManage::SetViewGroup(AppEvent::ClickEvent, AppEvent::LongPressEvent, arrPage_, groupCount_);
122     BundleInfo* pBundleInfos = nullptr;
123     int count = 0;
124     if (appManage_->LauncherApp(&pBundleInfos, count)) {
125         for (int j = 0; j < count; j++) {
126             for (int i = 0; i < groupCount_; i++) {
127                 if (memcmp(LAUNCHER_BUNDLE_NAME, pBundleInfos[j].bundleName, strlen(pBundleInfos[j].bundleName)) == 0) {
128                     break;
129                 }
130                 if (memcmp(SCREENSAVER_BUNDLE_NAME, pBundleInfos[j].bundleName, strlen(pBundleInfos[j].bundleName)) == 0) {
131                     break;
132                 }
133 
134                 AppInfo* app = new AppInfo();
135                 app->funcclick_ = AppEvent::ClickEvent;
136                 app->funclPress_ = AppEvent::LongPressEvent;
137                 if (pBundleInfos[j].bundleName) {
138                     memcpy_s(app->appName_, sizeof(app->appName_), pBundleInfos[j].bundleName,
139                         strlen(pBundleInfos[j].bundleName));
140                     app->appName_[strlen(pBundleInfos[j].bundleName)] = 0;
141                 }
142                 if (pBundleInfos[j].abilityInfos[0].name) {
143                     memcpy_s(app->abilityName_, sizeof(app->abilityName_), pBundleInfos[j].abilityInfos[0].name,
144                         strlen(pBundleInfos[j].abilityInfos[0].name));
145                     app->abilityName_[strlen(pBundleInfos[j].abilityInfos[0].name)] = 0;
146                 }
147                 if (pBundleInfos[j].bigIconPath) {
148                     memcpy_s(app->appIconDir_, sizeof(app->appIconDir_), pBundleInfos[j].bigIconPath,
149                         strlen(pBundleInfos[j].bigIconPath));
150                     app->appIconDir_[strlen(pBundleInfos[j].bigIconPath)] = 0;
151                 }
152                 if (arrPage_[i]->AddApp(app)) {
153                     break;
154                 }
155             }
156         }
157     }
158     swipe_->SetCurrentPage(0);
159 }
160 
StartApp(AppInfo * app)161 void SwipeView::StartApp(AppInfo* app)
162 {
163     if (lpView_->GetStatus() == true) {
164         lpView_->RemoveLview();
165         return;
166     }
167     appManage_->StartApp(app);
168 }
169 
ShowLongPressView(AppInfo * app)170 void SwipeView::ShowLongPressView(AppInfo* app)
171 {
172     lpView_->Show(static_cast<UIViewGroup*>(app->button_->GetParent()), app);
173 }
174 
UninstallApp(AppInfo * app)175 void SwipeView::UninstallApp(AppInfo* app)
176 {
177     if (appManage_->UnInstallApp(app)) {
178         for (int16_t i = 0; i < groupCount_; i++) {
179             if (arrPage_[i]) {
180                 if (arrPage_[i]->RemoveApp(app->appName_)) {
181                     swipe_->Invalidate();
182                     return;
183                 }
184             }
185         }
186     }
187 }
188 
InstallApp(AppInfo * app)189 void SwipeView::InstallApp(AppInfo* app)
190 {
191     appManage_->InstallApp(app);
192     AppInfo* pApp = new AppInfo();
193     if (pApp == nullptr) {
194         return;
195     }
196     app->funcclick_ = AppEvent::ClickEvent;
197     app->funclPress_ = AppEvent::LongPressEvent;
198     int16_t i;
199     for (i = 0; i < groupCount_; i++) {
200         if (arrPage_[i]->AddApp(pApp)) {
201             break;
202         }
203     }
204     if (i == groupCount_) {
205         delete pApp;
206         pApp = nullptr;
207     }
208 }
209 
OnStop()210 void SwipeView::OnStop()
211 {
212     if (lpView_) {
213         delete lpView_;
214         lpView_ = nullptr;
215     }
216     if (appManage_) {
217         delete appManage_;
218         appManage_ = nullptr;
219     }
220     if (arrViewListener_) {
221         delete arrViewListener_;
222         arrViewListener_ = nullptr;
223     }
224     if (swipeListener_) {
225         delete swipeListener_;
226         swipeListener_ = nullptr;
227     }
228     if (timeWeatherView_) {
229         delete timeWeatherView_;
230         timeWeatherView_ = nullptr;
231     }
232     for (int i = 0; i < MAX_VIEWGROUP; i++) {
233         if (arrPage_[i]) {
234             delete arrPage_[i];
235             arrPage_[i] = nullptr;
236         }
237     }
238     DeleteChildren(swipe_);
239 }
240 } // namespace OHOS
241