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