Tizen 2.0 Release
[apps/osp/Phone.git] / src / PhnCommonUtils.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   CommonUtils.cpp
19  *  @brief  commonly used functions
20  */
21
22 #include <FApp.h>
23 #include <FMedia.h>
24 #include <FGraphics.h>
25 #include "PhnAppUtility.h"
26 #include "PhnCommonUtils.h"
27
28 using namespace Tizen::App;
29 using namespace Tizen::Base;
30 using namespace Tizen::Graphics;
31 using namespace Tizen::Ui::Controls;
32 using namespace Tizen::Base::Collection;
33
34 const int ID_NAME_STRING = 100;
35 const int ID_NUMBER_STRING = 101;
36 const int ID_TIME_STRING = 102;
37 const int ID_CONTACT_BITMAP = 103;
38 const int ID_CALLTYPE_BITMAP = 104;
39
40
41 const int X_TEXT_LINE1_ITEM = 16;
42 const int Y_TEXT_LINE1_ITEM = 10;
43 const int H_TEXT_LINE1_ITEM = 60;
44 const int W_TEXT_LINE1_ITEM = 560;
45
46 const int X_TEXT_LINE2_ITEM = 64;
47 const int Y_TEXT_LINE2_ITEM = 70;
48 const int H_TEXT_LINE2_ITEM = 48;
49 const int W_TEXT_LINE2_ITEM = 256;
50
51 const int X_TEXT_TIME_ITEM = 320;
52 const int Y_TEXT_TIME_ITEM = 70;
53 const int H_TEXT_TIME_ITEM = 48;
54 const int W_TEXT_TIME_ITEM = 256;
55
56 const int X_CONTACT_BITMAP_ITEM = 592;
57 const int Y_CONTACT_BITMAP_ITEM = 0;
58 const int H_CONTACT_BITMAP_ITEM = 128;
59 const int W_CONTACT_BITMAP_ITEM = 128;
60
61 const int X_CALLTYPE_BITMAP_ITEM = 16;
62 const int Y_CALLTYPE_BITMAP_ITEM = 70 + 8;
63 const int H_CALLTYPE_BITMAP_ITEM = 32;
64 const int W_CALLTYPE_BITMAP_ITEM = 32;
65
66 const int X_DIALER_BTN_TXT = 13;
67 const int W_DIALER_BTN_NUM_TXT = 77;
68 const int W_DIALER_BTN_ALPHA_TXT = 121;
69
70 const int W_ENRICHED_TEXT = 190;
71 const int X_ENRICHED_TEXT = 143;
72
73 Font*
74 DialUtil::GetTextFontN(int textFontStyle)
75 {
76         Font* font = new (std::nothrow) Font();
77
78         switch (textFontStyle)
79         {
80         case FONT_CALLING_TEXT:
81         {
82                 font->Construct(FONT_STYLE_PLAIN, 94);
83         }
84         break;
85
86         case FONT_KEYPAD_NUMBER:
87         {
88                 font->Construct(FONT_STYLE_PLAIN, 116);
89         }
90         break;
91
92         case FONT_KEYPAD_TEXT:
93         {
94                 font->Construct(FONT_STYLE_BOLD, 40);
95         }
96         break;
97
98         case FONT_KEYPAD_P:
99         {
100                 font->Construct(FONT_STYLE_BOLD, 56);
101         }
102         break;
103
104         case FONT_KEYPAD_PLUS:
105         {
106                 font->Construct(FONT_STYLE_BOLD, 64);
107         }
108         break;
109
110         case FONT_KEYPAD_CANCEL:
111         {
112                 font->Construct(FONT_STYLE_PLAIN, 34);
113         }
114         break;
115
116         case FONT_SUGGESTION_NAME:
117         {
118                 font->Construct(FONT_STYLE_PLAIN, 40);
119         }
120         break;
121
122         case FONT_SUGGESTION_MOBILE:
123         {
124                 font->Construct(FONT_STYLE_PLAIN, 35);
125         }
126         break;
127
128         case FONT_SUGGESTION_NUMBER:
129         {
130                 font->Construct(FONT_STYLE_PLAIN, 35);
131         }
132         break;
133
134         case FONT_SUGGESTION_LIST_NUMBER:
135         {
136                 font->Construct(FONT_STYLE_PLAIN, 40);
137         }
138         break;
139         }
140         return font;
141 }
142
143 Color*
144 DialUtil::GetTextColorN(int textColor, int buttonStatus)
145 {
146         Color* color = new (std::nothrow) Color();
147
148         if (buttonStatus == BUTTON_STATUS_PRESSED)
149         {
150                 color->SetColorComponents(249, 249, 249);
151                 return color;
152         }
153
154         switch (textColor)
155         {
156         case FONT_CALLING_TEXT:
157         {
158                 color->SetColorComponents(0, 0, 0);
159         }
160         break;
161
162         case FONT_KEYPAD_NUMBER:
163         {
164                 color->SetColorComponents(255, 255, 255);
165         }
166         break;
167
168         case FONT_KEYPAD_TEXT:
169         case FONT_KEYPAD_P:
170         case FONT_KEYPAD_PLUS:
171         {
172                 color->SetColorComponents(148, 177, 200);
173         }
174         break;
175
176         case FONT_KEYPAD_CANCEL:
177         {
178                 color->SetColorComponents(255, 255, 255);
179         }
180         break;
181
182         case FONT_SUGGESTION_NAME:
183         {
184                 color->SetColorComponents(255, 255, 255);
185         }
186         break;
187
188         case FONT_SUGGESTION_MOBILE:
189         {
190                 color->SetColorComponents(148, 177, 200);
191         }
192         break;
193
194         case FONT_HIGHLIGHT_SEARCH_KEYWORD:
195         case FONT_SUGGESTION_NUMBER:
196         {
197                 color->SetColorComponents(26, 214, 227);
198         }
199         break;
200
201         case FONT_SUGGESTION_LIST_NUMBER:
202         {
203                 color->SetColorComponents(124, 189, 224);
204         }
205         break;
206         }
207         return color;
208 }
209
210 bool
211 DialUtil::IsNumericString(Tizen::Base::String& keyWord)
212 {
213         bool isDigit = false;
214         String digitStr(L"");
215         if(keyWord.SubString(0,1,digitStr) == E_SUCCESS)
216         {
217                 int digit;
218                 if(Integer::Parse(digitStr,digit) == E_SUCCESS)
219                 {
220                         isDigit = true;
221                 }
222         }
223         return isDigit;
224 }
225
226 void
227 DialUtil::Draw9PatchToCanvas(Canvas& pCanvas, const String& bgImg)
228 {
229         AppResource* pAppResource = AppResource::GetInstance();
230         Bitmap* pBitmap = pAppResource->GetBitmapN(bgImg);
231
232         if (pBitmap->IsNinePatchedBitmap())
233         {
234                 pCanvas.DrawNinePatchedBitmap(pCanvas.GetBounds(), *pBitmap);
235         }
236         delete pBitmap;
237
238         return;
239 }
240
241 void
242 DialUtil::DrawImageToCanvas(const String& image, Dimension imageDimention, Canvas& pCanvas, int pos)
243 {
244         //create and scale bitmap
245         Bitmap* pBitmap = AppUtility::GetBitmapFromResourcesN(image, imageDimention.width, imageDimention.height);
246
247         //Find position to draw bitmap
248         Point bitmapPos(0, 0);
249         switch (pos)
250         {
251         case POSITION_LEFT:
252         {
253                 bitmapPos.SetPosition(X_DIALER_BTN_TXT + (W_DIALER_BTN_NUM_TXT - pBitmap->GetWidth()) / 2, (pCanvas.GetBounds().height - pBitmap->GetHeight()) / 2);
254         }
255         break;
256
257         case POSITION_RIGHT:
258         {
259                 bitmapPos.SetPosition(X_DIALER_BTN_TXT + W_DIALER_BTN_NUM_TXT + ((W_DIALER_BTN_ALPHA_TXT - pBitmap->GetWidth()) / 2), (pCanvas.GetBounds().height - pBitmap->GetHeight()) / 2);
260         }
261         break;
262
263         case POSITION_CENTER:
264         {
265                 bitmapPos.SetPosition((pCanvas.GetBounds().width - pBitmap->GetWidth()) / 2, (pCanvas.GetBounds().height - pBitmap->GetHeight()) / 2);
266         }
267         break;
268
269         case POSITION_BOTTOM_CENTER:
270         {
271                 bitmapPos.SetPosition((pCanvas.GetBounds().width - pBitmap->GetWidth()) / 2, pCanvas.GetBounds().height / 2 + (pCanvas.GetBounds().height / 2 - pBitmap->GetHeight()) / 2);
272         }
273         break;
274         }
275
276         //draw bitmap
277         pCanvas.DrawBitmap(Rectangle(bitmapPos, Dimension(pBitmap->GetWidth(), pBitmap->GetHeight())), *pBitmap);
278         delete pBitmap;
279         pBitmap = null;
280
281         return;
282 }
283
284 void
285 DialUtil::DrawTextToCanvas(const String& buttonText, int textFontStyle, Canvas& pCanvas, int pos, int buttonStatus)
286 {
287         AppLogDebug("ENTER");
288         if (buttonText == null)
289         {
290                 return;
291         }
292
293         //get text font
294         Font* font = GetTextFontN(textFontStyle);
295
296         //get text color
297         Color* textColor = GetTextColorN(textFontStyle, buttonStatus);
298
299         //create text element
300         TextElement* pTextElement = new (std::nothrow) TextElement();
301         pTextElement->Construct(buttonText);
302         pTextElement->SetTextColor(*textColor);
303         pTextElement->SetFont(*font);
304         delete textColor;
305
306         //get dimensions of the text
307         Dimension textDimension;
308         font->GetTextExtent(buttonText, buttonText.GetLength(), textDimension);
309         if(textFontStyle != FONT_KEYPAD_NUMBER)
310         {
311                 textDimension.height += font->GetDescender();
312         }
313
314         //create enriched text
315         EnrichedText* pEnrichedText = new (std::nothrow) EnrichedText();
316         pEnrichedText->Construct(textDimension);
317         pEnrichedText->Add(*pTextElement);
318
319         //set the position of the text in canvas
320         Point textPos(0, 0);
321         switch (pos)
322         {
323         case POSITION_LEFT:
324         {
325                 //Margin 13, rectangle for left text (13,0,77, height of canvas), center aligned
326                 textPos.SetPosition(X_DIALER_BTN_TXT + ((W_DIALER_BTN_NUM_TXT - pEnrichedText->GetWidth()) / 2), (pCanvas.GetBounds().height - pEnrichedText->GetHeight()) / 2);
327         }
328         break;
329
330         case POSITION_RIGHT:
331         {
332                 //rectangle for right text (13+77,0, 121, height of canvas), center aligned
333                 textPos.SetPosition(X_DIALER_BTN_TXT + W_DIALER_BTN_NUM_TXT + ((121 - pEnrichedText->GetWidth()) / 2), (pCanvas.GetBounds().height - pEnrichedText->GetHeight()) / 2);
334         }
335         break;
336
337         case POSITION_CENTER:
338         {
339                 //rectangle for image : entire canvas, center aligned
340                 textPos.SetPosition((pCanvas.GetBounds().width - pEnrichedText->GetWidth()) / 2, (pCanvas.GetBounds().height - pEnrichedText->GetHeight()) / 2);
341         }
342         break;
343
344         case POSITION_TOP_LEFT:
345         {
346                 textPos.SetPosition(10, (pCanvas.GetBounds().height / 2 - pEnrichedText->GetHeight()) / 2);
347         }
348         break;
349
350         case POSITION_TOP_CENTER:
351         {
352                 textPos.SetPosition((pCanvas.GetBounds().width - pEnrichedText->GetWidth()) / 2, (pCanvas.GetBounds().height / 2 - pEnrichedText->GetHeight()) / 2);
353         }
354         break;
355
356         case POSITION_BOTTOM_LEFT:
357         {
358                 textPos.SetPosition(10, pCanvas.GetBounds().height / 2 + ((pCanvas.GetBounds().height / 2 - pEnrichedText->GetHeight()) / 2));
359         }
360         break;
361
362         case POSITION_BOTTOM_CENTER:
363         {
364                 int width = pEnrichedText->GetWidth();
365                 if(width > W_ENRICHED_TEXT)
366                 {
367                         textPos.SetPosition(X_ENRICHED_TEXT, pCanvas.GetBounds().height / 2 + ((pCanvas.GetBounds().height / 2 - pEnrichedText->GetHeight()) / 2));
368                 }
369                 else
370                 {
371                         textPos.SetPosition(10 + ((pCanvas.GetBounds().width - pEnrichedText->GetWidth()) / 2), pCanvas.GetBounds().height / 2 + ((pCanvas.GetBounds().height / 2 - pEnrichedText->GetHeight()) / 2));
372                 }
373
374         }
375         break;
376         }
377
378         //Draw EnrichedText to canvas
379         pCanvas.DrawText(textPos, *pEnrichedText);
380
381         // Cleans up
382         pEnrichedText->RemoveAll(true);
383         delete pEnrichedText;
384         delete font;
385
386         AppLogDebug("EXIT");
387         return;
388 }
389
390 void
391 DialUtil::DrawCanvasToButton(Canvas* pCanvasNormal, Canvas* pCanvasPressed, Button* pButton)
392 {
393         Bitmap* pNormalBgBmp = new (std::nothrow) Bitmap();
394         pNormalBgBmp->Construct(*pCanvasNormal, pCanvasNormal->GetBounds());
395
396         pButton->SetNormalBackgroundBitmap(*pNormalBgBmp);
397
398         if (pCanvasPressed != null)
399         {
400                 Bitmap* pPressedBgBmp = new (std::nothrow) Bitmap();
401                 pPressedBgBmp->Construct(*pCanvasPressed, pCanvasPressed->GetBounds());
402                 pButton->SetPressedBackgroundBitmap(*pPressedBgBmp);
403                 delete pPressedBgBmp;
404                 pPressedBgBmp = null;
405         }
406         else
407         {
408                 pButton->SetPressedBackgroundBitmap(*pNormalBgBmp);
409         }
410
411         pButton->SetDisabledBitmap(Point(0, 0), *pNormalBgBmp);
412         pButton->Invalidate(true);
413         delete pNormalBgBmp;
414         pNormalBgBmp = null;
415
416         return;
417 }
418
419 String
420 DialUtil::ConvertDialedNumberToString(int dialNumber)
421 {
422         switch (dialNumber)
423         {
424         case NUM_KEY1:
425         {
426                 return L"1";
427         }
428         case NUM_KEY2:
429         {
430                 return L"2";
431         }
432         case NUM_KEY3:
433         {
434                 return L"3";
435         }
436         case NUM_KEY4:
437         {
438                 return L"4";
439         }
440         case NUM_KEY5:
441         {
442                 return L"5";
443         }
444         case NUM_KEY6:
445         {
446                 return L"6";
447         }
448         case NUM_KEY7:
449         {
450                 return L"7";
451         }
452         case NUM_KEY8:
453         {
454                 return L"8";
455         }
456         case NUM_KEY9:
457         {
458                 return L"9";
459         }
460         break;
461         case NUM_KEY0:
462         {
463                 return L"0";
464         }
465         break;
466         }
467         return null;
468 }
469
470 IList*
471 DialUtil::FetchPossibleNamesListN(const String& prevSearchStr, int nextDigit)
472 {
473         //Fetch matching names for mapping char for digit, e.g. 2->ABC, 3->DEF.
474         ArrayList* pNamesList = new (std::nothrow) ArrayList(SingleObjectDeleter);
475         pNamesList->Construct(8);
476
477         if(nextDigit >=2 && nextDigit <= 9)
478         {
479                 switch(nextDigit)
480                 {
481                 case 2:
482                 {
483                         pNamesList->Add((new String(prevSearchStr+L"a")));
484                         pNamesList->Add((new String(prevSearchStr+L"b")));
485                         pNamesList->Add((new String(prevSearchStr+L"c")));
486                         pNamesList->Add((new String(prevSearchStr+L"A")));
487                         pNamesList->Add((new String(prevSearchStr+L"B")));
488                         pNamesList->Add((new String(prevSearchStr+L"C")));
489                 }
490                 break;
491
492                 case 3:
493                 {
494                         pNamesList->Add((new String(prevSearchStr+L"d")));
495                         pNamesList->Add((new String(prevSearchStr+L"e")));
496                         pNamesList->Add((new String(prevSearchStr+L"f")));
497                         pNamesList->Add((new String(prevSearchStr+L"D")));
498                         pNamesList->Add((new String(prevSearchStr+L"E")));
499                         pNamesList->Add((new String(prevSearchStr+L"F")));
500                 }
501                 break;
502
503                 case 4:
504                 {
505                         pNamesList->Add((new String(prevSearchStr+L"g")));
506                         pNamesList->Add((new String(prevSearchStr+L"h")));
507                         pNamesList->Add((new String(prevSearchStr+L"i")));
508                         pNamesList->Add((new String(prevSearchStr+L"G")));
509                         pNamesList->Add((new String(prevSearchStr+L"H")));
510                         pNamesList->Add((new String(prevSearchStr+L"I")));
511                 }
512                 break;
513
514                 case 5:
515                 {
516                         pNamesList->Add((new String(prevSearchStr+L"j")));
517                         pNamesList->Add((new String(prevSearchStr+L"k")));
518                         pNamesList->Add((new String(prevSearchStr+L"l")));
519                         pNamesList->Add((new String(prevSearchStr+L"J")));
520                         pNamesList->Add((new String(prevSearchStr+L"K")));
521                         pNamesList->Add((new String(prevSearchStr+L"L")));
522                 }
523                 break;
524
525                 case 6:
526                 {
527                         pNamesList->Add((new String(prevSearchStr+L"m")));
528                         pNamesList->Add((new String(prevSearchStr+L"n")));
529                         pNamesList->Add((new String(prevSearchStr+L"o")));
530                         pNamesList->Add((new String(prevSearchStr+L"M")));
531                         pNamesList->Add((new String(prevSearchStr+L"N")));
532                         pNamesList->Add((new String(prevSearchStr+L"O")));
533                 }
534                 break;
535
536                 case 7:
537                 {
538                         pNamesList->Add((new String(prevSearchStr+L"p")));
539                         pNamesList->Add((new String(prevSearchStr+L"q")));
540                         pNamesList->Add((new String(prevSearchStr+L"r")));
541                         pNamesList->Add((new String(prevSearchStr+L"s")));
542                         pNamesList->Add((new String(prevSearchStr+L"P")));
543                         pNamesList->Add((new String(prevSearchStr+L"Q")));
544                         pNamesList->Add((new String(prevSearchStr+L"R")));
545                         pNamesList->Add((new String(prevSearchStr+L"S")));
546                 }
547                 break;
548
549                 case 8:
550                 {
551                         pNamesList->Add((new String(prevSearchStr+L"t")));
552                         pNamesList->Add((new String(prevSearchStr+L"u")));
553                         pNamesList->Add((new String(prevSearchStr+L"v")));
554                         pNamesList->Add((new String(prevSearchStr+L"T")));
555                         pNamesList->Add((new String(prevSearchStr+L"U")));
556                         pNamesList->Add((new String(prevSearchStr+L"V")));
557                 }
558                 break;
559
560                 case 9:
561                 {
562                         pNamesList->Add((new String(prevSearchStr+L"w")));
563                         pNamesList->Add((new String(prevSearchStr+L"x")));
564                         pNamesList->Add((new String(prevSearchStr+L"y")));
565                         pNamesList->Add((new String(prevSearchStr+L"z")));
566                         pNamesList->Add((new String(prevSearchStr+L"W")));
567                         pNamesList->Add((new String(prevSearchStr+L"X")));
568                         pNamesList->Add((new String(prevSearchStr+L"Y")));
569                         pNamesList->Add((new String(prevSearchStr+L"Z")));
570                 }
571                 break;
572
573                 }
574         }
575         return pNamesList;
576 }
577
578 void
579 DialUtil::ConvertNameToNumericKeys(String& subContactName, String& possibleKeysTyped)
580 {
581         for(int charPos=0;charPos < subContactName.GetLength();charPos++)
582         {
583                 wchar_t nextChar;
584                 subContactName.GetCharAt(charPos,nextChar);
585                 switch(nextChar)
586                 {
587                 case 'a':case 'A':
588                 case 'b':case 'B':
589                 case 'c':case 'C':
590                         possibleKeysTyped.Append(2);
591                         break;
592
593                 case 'd':case 'D':
594                 case 'e':case 'E':
595                 case 'f':case 'F':
596                         possibleKeysTyped.Append(3);
597                         break;
598
599                 case 'g':case 'G':
600                 case 'h':case 'H':
601                 case 'i':case 'I':
602                         possibleKeysTyped.Append(4);
603                         break;
604
605                 case 'j':case 'J':
606                 case 'k':case 'K':
607                 case 'l':case 'L':
608                         possibleKeysTyped.Append(5);
609                         break;
610
611                 case 'm':case 'M':
612                 case 'n':case 'N':
613                 case 'o':case 'O':
614                         possibleKeysTyped.Append(6);
615                         break;
616
617                 case 'p':case 'P':
618                 case 'q':case 'Q':
619                 case 'r':case 'R':
620                 case 's':case 'S':
621                         possibleKeysTyped.Append(7);
622                         break;
623
624                 case 't':case 'T':
625                 case 'u':case 'U':
626                 case 'v':case 'V':
627                         possibleKeysTyped.Append(8);
628                         break;
629
630                 case 'w':case 'W':
631                 case 'x':case 'X':
632                 case 'y':case 'Y':
633                 case 'z':case 'Z':
634                         possibleKeysTyped.Append(9);
635                         break;
636
637                 default:
638                         break;
639                 }
640         }
641 }