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 <ctime>
17 #include <securec.h>
18
19 #include "time_weather_view.h"
20
21 namespace OHOS {
22 static constexpr int16_t DISPLATE_PICESE = 2;
23 static constexpr int16_t BLANK_H = 5;
24 static constexpr int16_t BLANK_TW = 15;
25 static constexpr int16_t BLANK_W = 100;
26 static constexpr int16_t BIGLABEL_H = 100;
27 static constexpr int16_t SMALLLABEL_H = 35;
28 static constexpr int16_t IMAGE_H = 40;
29 static constexpr int16_t IMAGE_W = 40;
30 const char* g_weekDate[WEEK_DAY_MAX] = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
31
TimeWeatherView(UIViewGroup * viewGroup)32 TimeWeatherView::TimeWeatherView(UIViewGroup* viewGroup)
33 {
34 viewGroup_ = viewGroup;
35 }
~TimeWeatherView()36 TimeWeatherView::~TimeWeatherView() {}
37
SetStyle(Style sty)38 void TimeWeatherView::SetStyle(Style sty)
39 {
40 viewGroup_->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
41 viewGroup_->Invalidate();
42 }
43
SetPosion(int16_t x,int16_t y,int16_t height,int16_t width)44 void TimeWeatherView::SetPosion(int16_t x, int16_t y, int16_t height, int16_t width)
45 {
46 viewGroup_->SetPosition(x, y, width, height);
47 }
48
SetUpView()49 void TimeWeatherView::SetUpView()
50 {
51 SetUpTimeView();
52 SetUpWeatherView();
53 viewGroup_->Invalidate();
54 }
55
SetUpTimeView()56 void TimeWeatherView::SetUpTimeView()
57 {
58 char hour_min[TMP_BUF_SIZE] = { 0 };
59 char mont_day[TMP_BUF_SIZE] = { 0 };
60 char week_day[TMP_BUF_SIZE] = { 0 };
61 char date[TMP_BUF_SIZE] = { 0 };
62 const int16_t january = 1;
63 const int16_t commonYear = 1970;
64 time_t t = time(nullptr);
65 struct tm* st = localtime(&t);
66 if (st == nullptr) {
67 return;
68 }
69 int ret = sprintf_s(hour_min, sizeof(hour_min), "%02d : %02d", st->tm_hour, st->tm_min);
70 if (ret == LAUNCHER_PARAMERROR) {
71 return;
72 }
73 ret = sprintf_s(mont_day, sizeof(mont_day), "%02d月%02d日", st->tm_mon + january, st->tm_mday);
74 if (ret == LAUNCHER_PARAMERROR) {
75 return;
76 }
77 GetWeekdayByYearday(st->tm_year + commonYear, st->tm_mon + january, st->tm_mday, week_day, sizeof(week_day));
78 ret = sprintf_s(date, sizeof(date), "%s %s", mont_day, week_day);
79 if (ret == LAUNCHER_PARAMERROR) {
80 return;
81 }
82 if (viewTime_ == nullptr) {
83 viewTime_ = new UIViewGroup();
84 viewTime_->SetPosition(BLANK_TW, BLANK_H, viewGroup_->GetWidth() - BLANK_W,
85 viewGroup_->GetHeight() / DISPLATE_PICESE - SMALLLABEL_H);
86 viewTime_->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
87 UILabel* lable = new UILabel();
88 lable->SetPosition(BLANK_TW, BLANK_H, viewTime_->GetWidth(), BIGLABEL_H);
89 lable->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_BOTTOM);
90 lable->SetText(hour_min);
91 lable->SetFont(FOND_PATH, BIGLAUNCHER_FOND_ID);
92 lable->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
93 lable->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS);
94 lable->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
95 lable->SetViewId("labletime");
96
97 UILabel* lable2 = new UILabel();
98 lable2->SetPosition(BLANK_TW, BLANK_H + BIGLABEL_H + BLANK_H, viewTime_->GetWidth(), SMALLLABEL_H);
99 lable2->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_TOP);
100 lable2->SetText(date);
101 lable2->SetFont(FOND_PATH, LAUNCHER_FOND_ID);
102
103 lable2->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
104 lable2->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS);
105 lable2->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
106 lable2->SetViewId("labledate");
107
108 viewTime_->Add(lable);
109 viewTime_->Add(lable2);
110 viewGroup_->Add(viewTime_);
111 } else {
112 UILabel* label = nullptr;
113 label = static_cast<UILabel*>(viewTime_->GetChildById("labletime"));
114 if (label) {
115 label->SetText(hour_min);
116 }
117 label = static_cast<UILabel*>(viewTime_->GetChildById("labledate"));
118 if (label) {
119 label->SetText(date);
120 }
121 viewTime_->Invalidate();
122 }
123 }
124
GetWeekdayByYearday(int iY,int iM,int iD,char * date,int size)125 void TimeWeatherView::GetWeekdayByYearday(int iY, int iM, int iD, char* date, int size)
126 {
127 if (date == nullptr) {
128 return;
129 }
130 const int16_t months = 12;
131 const int16_t january = 1;
132 const int16_t february = 2;
133 const int16_t oneHundred = 100;
134 const int16_t fourHundred = 400;
135 int iWeekDay = -1;
136 if (january == iM || february == iM) {
137 iM += months;
138 iY--;
139 }
140 // 1 : MONDAY_LAUNCHER, 2 : TUESDAY_LAUNCHER, 3 : WEDNESDAY_LAUNCHER, 4 : , 5 : ect
141 iWeekDay = (iD + 1 + 2 * iM + 3 * (iM + 1) / 5 + iY + iY / 4 - iY / oneHundred + iY / fourHundred) % WEEKEND_LAUNCHER;
142 for (int i = 0; i < WEEK_DAY_MAX; i++) {
143 if (iWeekDay == i) {
144 if (memcpy_s(date, size, g_weekDate[i], strlen(g_weekDate[i])) == LAUNCHER_SUCCESS) {
145 date[strlen(g_weekDate[i])] = 0;
146 break;
147 }
148 }
149 }
150 return;
151 }
152
SetUpWeatherView()153 void TimeWeatherView::SetUpWeatherView()
154 {
155 const int16_t countTimes = 6;
156 viewweather_ = new UIViewGroup();
157 viewweather_->SetPosition(BLANK_W, viewGroup_->GetHeight() / DISPLATE_PICESE - SMALLLABEL_H,
158 viewGroup_->GetWidth() / DISPLATE_PICESE + BLANK_TW, DISPLATE_PICESE * (BLANK_H + SMALLLABEL_H) + BLANK_H);
159 viewweather_->SetStyle(STYLE_BACKGROUND_OPA, HALF_OPACITY);
160 viewweather_->SetStyle(STYLE_BORDER_RADIUS, GROUP_VIEW_RADIUS);
161 viewweather_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Gray()));
162
163 UIImageView* uiImageView = new UIImageView();
164 uiImageView->SetPosition(BLANK_TW, BLANK_H * countTimes, IMAGE_W, IMAGE_H);
165 uiImageView->SetSrc(RES_WEATHER);
166 uiImageView->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY);
167
168 UILabel* lable = new UILabel();
169 lable->SetPosition(BLANK_TW + IMAGE_W, BLANK_H,
170 viewweather_->GetWidth() - IMAGE_W - BLANK_TW - BLANK_TW - BLANK_TW, SMALLLABEL_H);
171 lable->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER);
172 lable->SetText("室内温度 26℃");
173 lable->SetFont(FOND_PATH, LAUNCHER_FOND_ID);
174 lable->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
175 lable->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS);
176 lable->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
177
178 UILabel* lable2 = new UILabel();
179 lable2->SetPosition(BLANK_TW + IMAGE_W, SMALLLABEL_H + BLANK_H + BLANK_H,
180 viewweather_->GetWidth() - IMAGE_W - BLANK_TW - BLANK_TW - BLANK_TW, SMALLLABEL_H);
181 lable2->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER);
182 lable2->SetText("空气污染指数 136");
183 lable2->SetFont(FOND_PATH, LAUNCHER_FOND_ID);
184 lable2->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
185 lable2->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS);
186 lable2->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
187
188 viewweather_->Add(uiImageView);
189 viewweather_->Add(lable);
190 viewweather_->Add(lable2);
191 viewGroup_->Add(viewweather_);
192 }
193 } // namespace OHOS
194