• 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 
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) { return; }
67     int ret = sprintf_s(hour_min, sizeof(hour_min), "%02d : %02d", st->tm_hour, st->tm_min);
68     if (ret == LAUNCHER_PARAMERROR) { return; }
69     ret = sprintf_s(mont_day, sizeof(mont_day), "%02d月%02d日", st->tm_mon + january, st->tm_mday);
70     if (ret == LAUNCHER_PARAMERROR) { return; }
71     GetWeekdayByYearday(st->tm_year + commonYear, st->tm_mon + january, st->tm_mday, week_day, sizeof(week_day));
72     ret = sprintf_s(date, sizeof(date), "%s %s", mont_day, week_day);
73     if (ret == LAUNCHER_PARAMERROR) { return; }
74     if (viewTime_ == nullptr) {
75         viewTime_ = new UIViewGroup();
76         viewTime_->SetPosition(BLANK_TW, BLANK_H, viewGroup_->GetWidth() - BLANK_W,
77             viewGroup_->GetHeight() / DISPLATE_PICESE - SMALLLABEL_H);
78         viewTime_->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
79         UILabel* lable = new UILabel();
80         lable->SetPosition(BLANK_TW, BLANK_H, viewTime_->GetWidth(), BIGLABEL_H);
81         lable->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_BOTTOM);
82         lable->SetText(hour_min);
83         lable->SetFont(FOND_PATH, BIGLAUNCHER_FOND_ID);
84         lable->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
85         lable->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS);
86         lable->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
87         lable->SetViewId("labletime");
88 
89         UILabel* lable2 = new UILabel();
90         lable2->SetPosition(BLANK_TW, BLANK_H + BIGLABEL_H + BLANK_H, viewTime_->GetWidth(), SMALLLABEL_H);
91         lable2->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_TOP);
92         lable2->SetText(date);
93         lable2->SetFont(FOND_PATH, LAUNCHER_FOND_ID);
94 
95         lable2->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
96         lable2->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS);
97         lable2->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
98         lable2->SetViewId("labledate");
99 
100         viewTime_->Add(lable);
101         viewTime_->Add(lable2);
102         viewGroup_->Add(viewTime_);
103     } else {
104         UILabel* label = nullptr;
105         label = static_cast<UILabel*>(viewTime_->GetChildById("labletime"));
106         if (label) { label->SetText(hour_min); }
107         label = static_cast<UILabel*>(viewTime_->GetChildById("labledate"));
108         if (label) { label->SetText(date); }
109         viewTime_->Invalidate();
110     }
111 }
112 
GetWeekdayByYearday(int iY,int iM,int iD,char * date,int size)113 void TimeWeatherView::GetWeekdayByYearday(int iY, int iM, int iD, char* date, int size)
114 {
115     if (date == nullptr) {
116         return;
117     }
118     const int16_t months = 12;
119     const int16_t january = 1;
120     const int16_t february = 2;
121     const int16_t oneHundred = 100;
122     const int16_t fourHundred = 400;
123     int iWeekDay = -1;
124     if (january == iM || february == iM) {
125         iM += months;
126         iY--;
127     }
128     // 1 : MONDAY_LAUNCHER, 2 : TUESDAY_LAUNCHER, 3 : WEDNESDAY_LAUNCHER, 4 : , 5 : ect
129     iWeekDay = (iD + 1 + 2*iM + 3*(iM + 1)/5 + iY + iY/4 - iY/oneHundred + iY/fourHundred) % WEEKEND_LAUNCHER;
130     for (int i = 0; i < WEEK_DAY_MAX; i++) {
131         if (iWeekDay == i) {
132             if (memcpy_s(date, size, g_weekDate[i], strlen(g_weekDate[i])) == LAUNCHER_SUCCESS) {
133                 date[strlen(g_weekDate[i])] = 0;
134                 break;
135             }
136         }
137     }
138     return;
139 }
140 
SetUpWeatherView()141 void TimeWeatherView::SetUpWeatherView()
142 {
143     const int16_t countTimes = 6;
144     viewweather_ = new UIViewGroup();
145     viewweather_->SetPosition(BLANK_W, viewGroup_->GetHeight() / DISPLATE_PICESE - SMALLLABEL_H,
146         viewGroup_->GetWidth() / DISPLATE_PICESE + BLANK_TW, DISPLATE_PICESE * (BLANK_H + SMALLLABEL_H) + BLANK_H);
147     viewweather_->SetStyle(STYLE_BACKGROUND_OPA, HALF_OPACITY);
148     viewweather_->SetStyle(STYLE_BORDER_RADIUS, GROUP_VIEW_RADIUS);
149     viewweather_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Gray()));
150 
151     UIImageView* uiImageView = new UIImageView();
152     uiImageView->SetPosition(BLANK_TW, BLANK_H * countTimes, IMAGE_W, IMAGE_H);
153     uiImageView->SetSrc(RES_WEATHER);
154     uiImageView->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY);
155 
156     UILabel* lable = new UILabel();
157     lable->SetPosition(BLANK_TW + IMAGE_W, BLANK_H,
158         viewweather_->GetWidth() - IMAGE_W - BLANK_TW - BLANK_TW - BLANK_TW, SMALLLABEL_H);
159     lable->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER);
160     lable->SetText("室内温度 26℃");
161     lable->SetFont(FOND_PATH, LAUNCHER_FOND_ID);
162     lable->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
163     lable->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS);
164     lable->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
165 
166     UILabel* lable2 = new UILabel();
167     lable2->SetPosition(BLANK_TW + IMAGE_W, SMALLLABEL_H + BLANK_H + BLANK_H,
168         viewweather_->GetWidth() - IMAGE_W - BLANK_TW - BLANK_TW - BLANK_TW, SMALLLABEL_H);
169     lable2->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER);
170     lable2->SetText("空气污染指数 136");
171     lable2->SetFont(FOND_PATH, LAUNCHER_FOND_ID);
172     lable2->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
173     lable2->SetStyle(STYLE_BORDER_RADIUS, LABLE_RADIUS);
174     lable2->SetStyle(STYLE_BACKGROUND_OPA, TOTAL_OPACITY);
175 
176     viewweather_->Add(uiImageView);
177     viewweather_->Add(lable);
178     viewweather_->Add(lable2);
179     viewGroup_->Add(viewweather_);
180 }
181 } // namespace OHOS
182