Tizen 2.0 Release
[apps/osp/Phone.git] / src / PhnEmergencyDialForm.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        PhnEmergencyDialForm.cpp
19  * @brief       Base class for all Call forms
20  */
21 #include <FApp.h>
22 #include "PhnAppUtility.h"
23 #include "PhnCommonUtils.h"
24 #include "PhnCallPresentationModel.h"
25 #include "PhnDialerKeypad.h"
26 #include "PhnErrorMsgPopup.h"
27 #include "PhnEmergencyDialForm.h"
28 #include "PhnPhoneApp.h"
29 #include "PhnTypes.h"
30
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::App;
34 using namespace Tizen::Ui;
35 using namespace Tizen::Ui::Controls;
36 using namespace Tizen::Ui::Scenes;
37 using namespace Tizen::Graphics;
38
39 //resources
40 const wchar_t* IDL_EMERGENCY_DIAL_FORM = L"IDL_EMERGENCY_DIAL_FORM";
41 const wchar_t* IDC_LABEL_EMERGENCY = L"IDC_LABEL_EMERGENCY";
42 const wchar_t* IDC_PANEL_EMERGENCY_DISPLAY_DATA = L"IDC_PANEL_EMERGENCY_DISPLAY_DATA";
43 const wchar_t* IDC_EDITFIELD_NUMBER = L"IDC_EDITFIELD_NUMBER";
44 //color
45 const unsigned int COLOR_LABEL_BG = Color32<43, 107, 144>::Value;
46 const int MAX_NUMBER_WITH_SEPERATOR = 11;
47 const int MIN_NUMBER_WITH_TWO_SEPERATOR = 7;
48 const int MIN_NUMBER_WITH_ONE_SEPERATOR = 3;
49 const int MAX_TXT_LENGTH_WITH_SEPERATOR = 12;
50 const int MAX_VISIBLE_TXT_WITH_MIN_FONT = 18;
51 const int IDI_DELTA_FONT_SIZE = 5;
52 const int IDI_MAX_TXT_FONT = 94;
53 const int IDI_MIN_TXT_FONT = 67;
54 const int IDI_MAX_LIMIT_LENGTH = 18;
55
56 EmergencyDialForm::EmergencyDialForm(void)
57 {
58         __pDialKeypad = null;
59         __pErrorMsgPopup = null;
60 }
61
62 EmergencyDialForm::~EmergencyDialForm(void)
63 {
64         if(__pDialKeypad != null)
65         {
66                 //RemoveControl() also deletes the child control.
67                 RemoveControl(*__pDialKeypad);
68                 __pDialKeypad = null;
69         }
70 }
71
72 bool
73 EmergencyDialForm::Initialize(void)
74 {
75         Construct(IDL_EMERGENCY_DIAL_FORM);
76         return true;
77 }
78
79 result
80 EmergencyDialForm::OnInitializing(void)
81 {
82         result r = E_SUCCESS;
83         ConstructKeypad();
84         return r;
85 }
86
87 void
88 EmergencyDialForm::ConstructKeypad(void)
89 {
90         AppLogDebug("ENTER");
91         //Construct Alpha numeric Keypad
92         if(__pDialKeypad == null)
93         {
94                 __pDialKeypad = new (std::nothrow) DialerKeypad(DialerKeypad::DIALER_STATE_EMERGENCY_APPCONTROL);
95                 __pDialKeypad->ConstructPanel(this, this);
96                 AddControl(*__pDialKeypad);
97         }
98         AppLogDebug("EXIT");
99         return;
100 }
101
102 void
103 EmergencyDialForm::ShowFormattedText(EditField* pEditField, int actionId)
104 {
105         AppLogDebug("ENTER");
106         String contactNumber = pEditField->GetText();
107         contactNumber.Replace(L"-",L"");
108         if(actionId == CLEAR_KEY)
109         {
110                 contactNumber.Remove(contactNumber.GetLength() - 1, 1);
111         }
112         else
113         {
114                 contactNumber.Append(DialUtil::ConvertDialedNumberToString(actionId));
115         }
116
117         int textLength = contactNumber.GetLength();
118         if(textLength == 0)
119         {
120                 pEditField->Clear();
121                 AppLogDebug("EXIT");
122                 return;
123         }
124         //Insert separator if required
125         if(textLength < MAX_NUMBER_WITH_SEPERATOR)
126         {
127                 if(textLength > MIN_NUMBER_WITH_TWO_SEPERATOR)
128                 {
129                         contactNumber.Insert(L"-",MIN_NUMBER_WITH_TWO_SEPERATOR);
130                 }
131                 if(textLength > MIN_NUMBER_WITH_ONE_SEPERATOR)
132                 {
133                         contactNumber.Insert(L"-",MIN_NUMBER_WITH_ONE_SEPERATOR);
134                 }
135         }
136         //update text length
137         textLength = contactNumber.GetLength();
138         //need to change font to fit inside text box
139         //With size 94 -> 12 chars, 67 -> 18 chars
140         int fontSize = pEditField->GetTextSize();
141         if((actionId == CLEAR_KEY) && (textLength <= MAX_VISIBLE_TXT_WITH_MIN_FONT))
142         {
143                 //clearing
144                 fontSize += IDI_DELTA_FONT_SIZE;
145         }
146         else if(textLength > MAX_TXT_LENGTH_WITH_SEPERATOR)
147         {
148                 //appending
149                 fontSize -= IDI_DELTA_FONT_SIZE;
150         }
151
152         if(fontSize > IDI_MAX_TXT_FONT)
153         {
154                 fontSize = IDI_MAX_TXT_FONT;
155         }
156         if(fontSize < IDI_MIN_TXT_FONT)
157         {
158                 fontSize = IDI_MIN_TXT_FONT;
159         }
160         pEditField->SetTextSize(fontSize);
161         //set text to edit field
162         pEditField->SetText(contactNumber);
163
164         AppLogDebug("EXIT");
165         return;
166 }
167
168 void
169 EmergencyDialForm::OnActionPerformed(const Control& source, int actionId)
170 {
171         EditField* pEditField = static_cast<EditField*>(GetControl(IDC_EDITFIELD_NUMBER, true));
172         if (pEditField == null)
173         {
174                 return;
175         }
176
177         switch (actionId)
178         {
179         case NUM_KEY1:
180         case NUM_KEY2:
181         case NUM_KEY3:
182         case NUM_KEY4:
183         case NUM_KEY5:
184         case NUM_KEY6:
185         case NUM_KEY7:
186         case NUM_KEY8:
187         case NUM_KEY9:
188         case NUM_KEY0:
189         case CLEAR_KEY:
190         {
191                 if(pEditField->GetTextLength() >= IDI_MAX_LIMIT_LENGTH && actionId != CLEAR_KEY)
192                 {
193                         //Maximum capacity reached, Don't append any more numbers.
194                         AppLogDebug("EXIT");
195                         return;
196                 }
197                 //append / clear last character to contact
198                 ShowFormattedText(pEditField, actionId);
199         }
200         break;
201
202         case CALL_KEY:
203         {
204                 if (pEditField->GetTextLength() > 0)
205                 {
206                         String contactNo = pEditField->GetText();
207                         contactNo.Replace(L"-",L"");
208                         SwitchToOutgoingCallScreen(contactNo);
209                         return;
210                 }
211         }
212         break;
213
214         case PAUSE_KEY:
215         {
216                 pEditField->AppendText(L"*");
217         }
218         break;
219
220         case HASH_KEY:
221         {
222                 pEditField->AppendText(L"#");
223         }
224         break;
225
226         case VIDEOCALL_KEY:
227         {
228                 //Not yet Supported
229         }
230         break;
231
232         case CANCEL_KEY:
233         {
234                 PhoneApp* pApp = static_cast<PhoneApp*>(UiApp::GetInstance());
235                 pApp->Terminate();
236         }
237         break;
238
239         default:
240         break;
241         }
242         pEditField->Invalidate(false);
243
244         return;
245 }
246
247 void
248 EmergencyDialForm::OnLongPressGestureDetected(TouchLongPressGestureDetector& gestureDetector)
249 {
250         if (gestureDetector.GetControl() != null)
251         {
252                 Button* pButton = static_cast<Button*>(gestureDetector.GetControl());
253                 int actionId = pButton->GetActionId();
254                 switch(actionId)
255                 {
256                         case CLEAR_KEY:
257                         {
258                                 EditField* pEditField = static_cast<EditField*>(GetControl(IDC_EDITFIELD_NUMBER, true));
259                                 if (pEditField != null && pEditField->GetTextLength() > 0)
260                                 {
261                                         pEditField->Clear();
262                                         pEditField->SetTextSize(IDI_MAX_TXT_FONT);
263                                         pEditField->Invalidate(false);
264                                 }
265                         }
266                         break;
267                 }
268         }
269 }
270
271 result
272 EmergencyDialForm::OnTerminating(void)
273 {
274         result r = E_SUCCESS;
275         return r;
276 }
277
278 void
279 EmergencyDialForm::OnSceneActivatedN(const SceneId& previousSceneId, const SceneId& currentSceneId, IList* pArgs)
280 {
281 }
282
283 void
284 EmergencyDialForm::OnSceneDeactivated(const SceneId& currentSceneId, const SceneId& nextSceneId)
285 {
286 }
287
288 void
289 EmergencyDialForm::SwitchToOutgoingCallScreen(const String& contactNumber)
290 {
291         AppLogDebug("ENTER");
292         SceneManager* pSceneManager = SceneManager::GetInstance();
293         AppAssert(pSceneManager);
294
295         CallPresentationModel* pCallPresentor = CallPresentationModel::GetInstance();
296         //Check if dialing a call is possible
297         bool isSimInitialized = pCallPresentor->CheckSimInitializationIsCompleted();
298         if (isSimInitialized)
299         {
300                 //make an outgoing call with given number
301                 String* contactTxt = new (std::nothrow) String(contactNumber);
302                 //switch to dialing outgoing call form
303                 ArrayList* pContact = new (std::nothrow) ArrayList(SingleObjectDeleter);
304                 pContact->Construct(1);
305                 pContact->Add(contactTxt);
306                 //Check if dialing an emergency call
307                 bool isEmergencyCall = pCallPresentor->IsEmergencyNumber(*contactTxt, true);
308                 if (isEmergencyCall)
309                 {
310                         pSceneManager->GoForward( ForwardSceneTransition( IDSCN_SCENE_OUT_EMERGENCYCALL), pContact);
311                 }
312                 else
313                 {
314                         //Show error message Popup
315                         if (__pErrorMsgPopup == null)
316                         {
317                                 __pErrorMsgPopup = new (std::nothrow) ErrorMsgPopup(this);
318                         }
319                         __pErrorMsgPopup->ShowErrorMsgPopupN(ERROR_NOT_EMERGENCY_NUM);
320                 }
321         }
322         else
323         {
324                 //TODO: check what to do for SOS calls
325                 //Show error message Popup
326                 if (__pErrorMsgPopup == null)
327                 {
328                         __pErrorMsgPopup = new (std::nothrow) ErrorMsgPopup(this);
329                 }
330                 __pErrorMsgPopup->ShowErrorMsgPopupN(ERROR_CODE_SIM_INITIALIZATION_FAILED);
331         }
332         AppLogDebug("EXIT");
333         return;
334 }
335
336 void
337 EmergencyDialForm::HandlePopupClosed(void)
338 {
339         EditField* pEditField = static_cast<EditField*>(GetControl(IDC_EDITFIELD_NUMBER, true));
340         //clear text
341         pEditField->Clear();
342         Draw();
343         delete __pErrorMsgPopup;
344         __pErrorMsgPopup = null;
345         return;
346 }