License and Privilege changes
[apps/osp/Call.git] / src / CallCommonUtils.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.1 (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   CallCommonUtils.cpp
19  *  @brief  commonly used functions
20  */
21
22 #include <FApp.h>
23 #include <FMedia.h>
24 #include <FGraphics.h>
25 #include "CallAppUtility.h"
26 #include "CallCommonUtils.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         FloatDimension 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         //scale bitmap to button size
396         pNormalBgBmp->SetScalingQuality(BITMAP_SCALING_QUALITY_MID);
397         pNormalBgBmp->Scale(pButton->GetSize());
398
399         pButton->SetNormalBackgroundBitmap(*pNormalBgBmp);
400
401         if (pCanvasPressed != null)
402         {
403                 Bitmap* pPressedBgBmp = new (std::nothrow) Bitmap();
404                 pPressedBgBmp->Construct(*pCanvasPressed, pCanvasPressed->GetBounds());
405                 //scale bitmap to button size
406                 pPressedBgBmp->SetScalingQuality(BITMAP_SCALING_QUALITY_MID);
407                 pPressedBgBmp->Scale(pButton->GetSize());
408                 pButton->SetPressedBackgroundBitmap(*pPressedBgBmp);
409                 delete pPressedBgBmp;
410                 pPressedBgBmp = null;
411         }
412         else
413         {
414                 pButton->SetPressedBackgroundBitmap(*pNormalBgBmp);
415         }
416
417         pButton->SetDisabledBitmap(Point(0, 0), *pNormalBgBmp);
418         pButton->Invalidate(true);
419         delete pNormalBgBmp;
420         pNormalBgBmp = null;
421
422         return;
423 }
424
425 String
426 DialUtil::ConvertDialedNumberToString(int dialNumber)
427 {
428         switch (dialNumber)
429         {
430         case NUM_KEY1:
431         {
432                 return L"1";
433         }
434         case NUM_KEY2:
435         {
436                 return L"2";
437         }
438         case NUM_KEY3:
439         {
440                 return L"3";
441         }
442         case NUM_KEY4:
443         {
444                 return L"4";
445         }
446         case NUM_KEY5:
447         {
448                 return L"5";
449         }
450         case NUM_KEY6:
451         {
452                 return L"6";
453         }
454         case NUM_KEY7:
455         {
456                 return L"7";
457         }
458         case NUM_KEY8:
459         {
460                 return L"8";
461         }
462         case NUM_KEY9:
463         {
464                 return L"9";
465         }
466         break;
467         case NUM_KEY0:
468         {
469                 return L"0";
470         }
471         break;
472         }
473         return null;
474 }
475
476 IList*
477 DialUtil::FetchPossibleNamesListN(const String& prevSearchStr, int nextDigit)
478 {
479         //Fetch matching names for mapping char for digit, e.g. 2->ABC, 3->DEF.
480         ArrayList* pNamesList = new (std::nothrow) ArrayList(SingleObjectDeleter);
481         pNamesList->Construct(8);
482
483         if(nextDigit >=2 && nextDigit <= 9)
484         {
485                 switch(nextDigit)
486                 {
487                 case 2:
488                 {
489                         pNamesList->Add((new String(prevSearchStr+L"a")));
490                         pNamesList->Add((new String(prevSearchStr+L"b")));
491                         pNamesList->Add((new String(prevSearchStr+L"c")));
492                         pNamesList->Add((new String(prevSearchStr+L"A")));
493                         pNamesList->Add((new String(prevSearchStr+L"B")));
494                         pNamesList->Add((new String(prevSearchStr+L"C")));
495                 }
496                 break;
497
498                 case 3:
499                 {
500                         pNamesList->Add((new String(prevSearchStr+L"d")));
501                         pNamesList->Add((new String(prevSearchStr+L"e")));
502                         pNamesList->Add((new String(prevSearchStr+L"f")));
503                         pNamesList->Add((new String(prevSearchStr+L"D")));
504                         pNamesList->Add((new String(prevSearchStr+L"E")));
505                         pNamesList->Add((new String(prevSearchStr+L"F")));
506                 }
507                 break;
508
509                 case 4:
510                 {
511                         pNamesList->Add((new String(prevSearchStr+L"g")));
512                         pNamesList->Add((new String(prevSearchStr+L"h")));
513                         pNamesList->Add((new String(prevSearchStr+L"i")));
514                         pNamesList->Add((new String(prevSearchStr+L"G")));
515                         pNamesList->Add((new String(prevSearchStr+L"H")));
516                         pNamesList->Add((new String(prevSearchStr+L"I")));
517                 }
518                 break;
519
520                 case 5:
521                 {
522                         pNamesList->Add((new String(prevSearchStr+L"j")));
523                         pNamesList->Add((new String(prevSearchStr+L"k")));
524                         pNamesList->Add((new String(prevSearchStr+L"l")));
525                         pNamesList->Add((new String(prevSearchStr+L"J")));
526                         pNamesList->Add((new String(prevSearchStr+L"K")));
527                         pNamesList->Add((new String(prevSearchStr+L"L")));
528                 }
529                 break;
530
531                 case 6:
532                 {
533                         pNamesList->Add((new String(prevSearchStr+L"m")));
534                         pNamesList->Add((new String(prevSearchStr+L"n")));
535                         pNamesList->Add((new String(prevSearchStr+L"o")));
536                         pNamesList->Add((new String(prevSearchStr+L"M")));
537                         pNamesList->Add((new String(prevSearchStr+L"N")));
538                         pNamesList->Add((new String(prevSearchStr+L"O")));
539                 }
540                 break;
541
542                 case 7:
543                 {
544                         pNamesList->Add((new String(prevSearchStr+L"p")));
545                         pNamesList->Add((new String(prevSearchStr+L"q")));
546                         pNamesList->Add((new String(prevSearchStr+L"r")));
547                         pNamesList->Add((new String(prevSearchStr+L"s")));
548                         pNamesList->Add((new String(prevSearchStr+L"P")));
549                         pNamesList->Add((new String(prevSearchStr+L"Q")));
550                         pNamesList->Add((new String(prevSearchStr+L"R")));
551                         pNamesList->Add((new String(prevSearchStr+L"S")));
552                 }
553                 break;
554
555                 case 8:
556                 {
557                         pNamesList->Add((new String(prevSearchStr+L"t")));
558                         pNamesList->Add((new String(prevSearchStr+L"u")));
559                         pNamesList->Add((new String(prevSearchStr+L"v")));
560                         pNamesList->Add((new String(prevSearchStr+L"T")));
561                         pNamesList->Add((new String(prevSearchStr+L"U")));
562                         pNamesList->Add((new String(prevSearchStr+L"V")));
563                 }
564                 break;
565
566                 case 9:
567                 {
568                         pNamesList->Add((new String(prevSearchStr+L"w")));
569                         pNamesList->Add((new String(prevSearchStr+L"x")));
570                         pNamesList->Add((new String(prevSearchStr+L"y")));
571                         pNamesList->Add((new String(prevSearchStr+L"z")));
572                         pNamesList->Add((new String(prevSearchStr+L"W")));
573                         pNamesList->Add((new String(prevSearchStr+L"X")));
574                         pNamesList->Add((new String(prevSearchStr+L"Y")));
575                         pNamesList->Add((new String(prevSearchStr+L"Z")));
576                 }
577                 break;
578
579                 }
580         }
581         return pNamesList;
582 }
583
584 void
585 DialUtil::ConvertNameToNumericKeys(String& subContactName, String& possibleKeysTyped)
586 {
587         for(int charPos=0;charPos < subContactName.GetLength();charPos++)
588         {
589                 wchar_t nextChar;
590                 subContactName.GetCharAt(charPos,nextChar);
591                 switch(nextChar)
592                 {
593                 case 'a':case 'A':
594                 case 'b':case 'B':
595                 case 'c':case 'C':
596                         possibleKeysTyped.Append(2);
597                         break;
598
599                 case 'd':case 'D':
600                 case 'e':case 'E':
601                 case 'f':case 'F':
602                         possibleKeysTyped.Append(3);
603                         break;
604
605                 case 'g':case 'G':
606                 case 'h':case 'H':
607                 case 'i':case 'I':
608                         possibleKeysTyped.Append(4);
609                         break;
610
611                 case 'j':case 'J':
612                 case 'k':case 'K':
613                 case 'l':case 'L':
614                         possibleKeysTyped.Append(5);
615                         break;
616
617                 case 'm':case 'M':
618                 case 'n':case 'N':
619                 case 'o':case 'O':
620                         possibleKeysTyped.Append(6);
621                         break;
622
623                 case 'p':case 'P':
624                 case 'q':case 'Q':
625                 case 'r':case 'R':
626                 case 's':case 'S':
627                         possibleKeysTyped.Append(7);
628                         break;
629
630                 case 't':case 'T':
631                 case 'u':case 'U':
632                 case 'v':case 'V':
633                         possibleKeysTyped.Append(8);
634                         break;
635
636                 case 'w':case 'W':
637                 case 'x':case 'X':
638                 case 'y':case 'Y':
639                 case 'z':case 'Z':
640                         possibleKeysTyped.Append(9);
641                         break;
642
643                 default:
644                         break;
645                 }
646         }
647 }