4c88a66d9bc197421a1557b97b16ef953dadd3f3
[apps/osp/Internet.git] / src / IntBrightnessForm.cpp
1 //
2
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 //!Internet IntBrightness class
19 /*@file:        IntBrightness.cpp
20  *@brief:       This class defines IntBrightness used to change the brightness
21  *
22  */
23
24 #include <FApp.h>
25 #include <FAppUiApp.h>
26 #include <FUi.h>
27 #include "IntBrightnessForm.h"
28 #include "IntCommonLib.h"
29 #include "IntMultipleWindowPresentationModel.h"
30 #include "IntSceneRegister.h"
31
32 using namespace Tizen::App;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::System;
37 using namespace Tizen::Ui;
38 using namespace Tizen::Ui::Controls;
39 using namespace Tizen::Ui::Scenes;
40
41 const int IDA_AUTOBTN_CLICKED = 101;
42 const int IDA_MANUALBTN_CLICKED = 102;
43
44 BrightnessForm::BrightnessForm(void)
45 {
46         __manualBrightness =  true;
47         __pBrightnessSlider = null;
48         __pLabel = null;
49 }
50
51 BrightnessForm::~BrightnessForm(void)
52 {
53
54 }
55
56 bool
57 BrightnessForm::Initialize()
58 {
59         Form::Construct(L"IDL_BRIGHTNESS");
60
61         return true;
62 }
63
64 result
65 BrightnessForm::OnInitializing(void)
66 {
67         result r = E_FAILURE;
68         WindowInfo* pWindowInfo = null;
69
70         SceneManager* pSceneManager = SceneManager::GetInstance();
71         if (pSceneManager != null)
72         {
73                 r = pSceneManager->AddSceneEventListener(IDSCN_BRIGHTNESS, *this);
74                 if (IsFailed(r))
75                 {
76                         AppLogException("OnInitialized failed with %s", GetErrorMessage(r));
77                         return r;
78                 }
79         }
80         AddOrientationEventListener(*this);
81         SetFormBackEventListener(this);
82
83         __pLabel = static_cast< Label* >(GetControl(L"IDC_LABEL", true));
84
85         pWindowInfo = MultipleWindowPresentationModel::GetInstance()->GetActiveWindowInfo();
86         if (pWindowInfo != null && pWindowInfo->pWebCanvasBitmap != null  && __pLabel != null)
87         {
88                 int labelWidth = __pLabel->GetWidth();
89                 int labelHeigth = __pLabel->GetHeight();
90
91                 if(GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE || GetOrientationStatus() == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
92                 {
93                         labelWidth = pWindowInfo->pWebCanvasBitmap->GetWidth() * labelHeigth / pWindowInfo->pWebCanvasBitmap->GetHeight();
94                         __pLabel->SetBounds(__pLabel->GetX()  + (__pLabel->GetWidth() - labelWidth)/2, __pLabel->GetY(), labelWidth, labelHeigth);
95                 }
96                 else
97                 {
98                         labelHeigth = pWindowInfo->pWebCanvasBitmap->GetHeight() * labelWidth / pWindowInfo->pWebCanvasBitmap->GetWidth();
99                         __pLabel->SetBounds(__pLabel->GetX(), __pLabel->GetY()  + (__pLabel->GetHeight() - labelHeigth)/2, labelWidth, labelHeigth);
100                 }
101                 __pLabel->SetBackgroundBitmap(*pWindowInfo->pWebCanvasBitmap);
102         }
103         
104         AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
105         String sliderKey("SliderValue");
106         String tabKey("TabButton");
107         int sliderValue = 10;
108         int tabValue = 1;
109
110         r = pAppRegistry->Get(sliderKey, sliderValue);
111         if (r == E_KEY_NOT_FOUND)
112         {
113                 pAppRegistry->Add(sliderKey, sliderValue);
114                 pAppRegistry->Set(sliderKey,sliderValue);
115         }
116
117         r = pAppRegistry->Get(tabKey, tabValue);
118         if (r == E_KEY_NOT_FOUND)
119         {
120                 pAppRegistry->Add(tabKey, tabValue);
121                 pAppRegistry->Set(tabKey,tabValue);
122         }
123         r = pAppRegistry->Save();
124         if (IsFailed(r))
125         {
126                 // Failed to save data to registry
127                 AppLogDebug("OnInitialized failed to save data to registry with %s", GetErrorMessage(r));
128                 return E_FAILURE;
129         }
130
131
132         __pBrightnessSlider = static_cast< Slider* >(GetControl(L"IDC_FONT_SLIDER", true));
133         if (__pBrightnessSlider == null)
134         {
135                 return E_FAILURE;
136         }
137         __pBrightnessSlider->SetRange(1,10);
138         __pBrightnessSlider->SetValue(sliderValue);
139         __pBrightnessSlider->AddSliderEventListener(*this);
140
141         GetFooter()->AddActionEventListener(*this);
142         return r;
143 }
144
145 void
146 BrightnessForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
147 {
148         result r = E_FAILURE;
149         AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
150         String sliderKey("SliderValue");
151         String tabKey("TabButton");
152         int sliderValue = 10;
153         int tabValue = 0;
154
155         r = pAppRegistry->Get(sliderKey, sliderValue);
156         r = pAppRegistry->Get(tabKey, tabValue);
157         
158         AppLog("BrightnessForm tab value is %d",tabValue);
159         AppLog("BrightnessForm slider value is %d",sliderValue);
160
161         if(__pBrightnessSlider == null)
162         {
163                 return;
164         }
165
166         if(tabValue == 0)
167         {
168                 __pBrightnessSlider->SetEnabled(false);
169                 __pBrightnessSlider->Invalidate(true);
170         }
171         else
172         {
173                 __pBrightnessSlider->SetEnabled(true);
174                 AppLog("BatteryLvl %d",PowerManager::GetScreenBrightness());
175                 __pBrightnessSlider->SetValue(sliderValue);
176                 __pBrightnessSlider->Invalidate(true);
177         }
178
179         GetFooter()->SetItemSelected(tabValue);
180
181         __pBrightnessSlider->SetRange(1,10);
182 //      __pBrightnessSlider->SetValue(sliderValue);
183
184
185         AppLog("slider value is %d",PowerManager::GetScreenBrightness());
186
187         Invalidate(true);
188
189 }
190
191 void
192 BrightnessForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
193 {
194         AppLog("slider value is %d",PowerManager::GetScreenBrightness());
195         __pBrightnessSlider->SetValue(PowerManager::GetScreenBrightness());
196         Invalidate(true);
197 }
198
199 void
200 BrightnessForm::OnSliderBarMoved(Slider& source, int value)
201 {
202         AppLog("coming to OnSliderBarMoved");
203         if (__manualBrightness == true)
204         {
205                 PowerManager::SetScreenBrightness(value);
206                 AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
207                 String sliderKey("SliderValue");
208                 if (pAppRegistry)
209                 {
210                         pAppRegistry->Set(sliderKey,value);
211                 }
212         }
213 }
214
215 void
216 BrightnessForm::OnFormBackRequested(Form& source)
217 {
218         
219         result r = E_FAILURE;
220         SceneManager* pSceneManager = SceneManager::GetInstance();
221         if (pSceneManager != null)
222         {
223                 // save the slider value
224                 // save the tab button
225                 AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
226                 String sliderKey("SliderValue");
227                 String tabKey("TabButton");
228                 int sliderValue = __pBrightnessSlider->GetValue();
229                 r = pAppRegistry->Set(sliderKey,sliderValue);
230                 FooterItemStatus footerStatus;
231                 GetFooter()->GetItemStatus(0,footerStatus);
232                 if (footerStatus == FOOTER_ITEM_STATUS_SELECTED)
233                 {
234                         r = pAppRegistry->Set(tabKey,0);
235                 }
236                 else
237                 {
238                         r = pAppRegistry->Set(tabKey,1);
239                 }
240                 pAppRegistry->Save();
241                 r = pSceneManager->GoBackward(BackwardSceneTransition());
242                 if(IsFailed(r))
243                 {
244                         AppLogException("OnFormBackRequested failed with %s", GetErrorMessage(r));
245                 }
246         }
247
248 }
249
250 void
251 BrightnessForm::OnActionPerformed(const Control& source, int actionId)
252 {
253         result r = E_SUCCESS;
254         switch (actionId)
255         {
256         case IDA_AUTOBTN_CLICKED:
257                 __manualBrightness = false;
258                 __pBrightnessSlider->SetValue(6);
259 //              __pBrightnessSlider->SetShowState(false);
260                 PowerManager::SetScreenBrightness(6);
261                 __pBrightnessSlider->SetEnabled(false);
262                 __pBrightnessSlider->Invalidate(true);
263                 break;
264         case IDA_MANUALBTN_CLICKED:
265                 PowerManager::SetScreenBrightness(__pBrightnessSlider->GetValue());
266                 //__pBrightnessSlider->SetValue(__pBrightnessSlider->GetValue());
267                 __pBrightnessSlider->SetEnabled(true);
268                 __pBrightnessSlider->SetShowState(true);
269                 __pBrightnessSlider->Invalidate(true);
270
271                 __manualBrightness = true;
272                 break;
273         default:
274                 break;
275         }
276         // save the slider value
277         // save the tab button
278         AppRegistry* pAppRegistry = App::GetInstance()->GetAppRegistry();
279         String sliderKey("SliderValue");
280         String tabKey("TabButton");
281         int sliderValue = __pBrightnessSlider->GetValue();
282         r = pAppRegistry->Set(sliderKey,sliderValue);
283         FooterItemStatus footerStatus;
284         GetFooter()->GetItemStatus(0,footerStatus);
285         if (footerStatus == FOOTER_ITEM_STATUS_SELECTED)
286         {
287                 r = pAppRegistry->Set(tabKey,0);
288         }
289         else
290         {
291                 r = pAppRegistry->Set(tabKey,1);
292         }
293         pAppRegistry->Save();
294 }
295
296 void
297 BrightnessForm::OnOrientationChanged(const Control& source, OrientationStatus orientationStatus)
298 {
299         WindowInfo* pWindowInfo = MultipleWindowPresentationModel::GetInstance()->GetActiveWindowInfo();
300
301         if (pWindowInfo != null)
302         {
303                 if(pWindowInfo->pWebCanvasBitmap != null)
304                 {
305                         int labelWidth = __pLabel->GetWidth();
306                         int labelHeigth = __pLabel->GetHeight();
307
308                         if(orientationStatus == ORIENTATION_STATUS_LANDSCAPE || orientationStatus == ORIENTATION_STATUS_LANDSCAPE_REVERSE)
309                         {
310                                 labelWidth = pWindowInfo->pWebCanvasBitmap->GetWidth() * labelHeigth / pWindowInfo->pWebCanvasBitmap->GetHeight();
311                                 __pLabel->SetBounds(__pLabel->GetX()  + (__pLabel->GetWidth() - labelWidth)/2, __pLabel->GetY(), labelWidth, labelHeigth);
312                         }
313                         else
314                         {
315                                 labelHeigth = pWindowInfo->pWebCanvasBitmap->GetHeight() * labelWidth / pWindowInfo->pWebCanvasBitmap->GetWidth();
316                                 __pLabel->SetBounds(__pLabel->GetX(), __pLabel->GetY()  + (__pLabel->GetHeight() - labelHeigth)/2, labelWidth, labelHeigth);
317                         }
318                         Invalidate(true);
319                 }
320         }
321 }