summaryrefslogtreecommitdiffstats
path: root/src/org/codeaurora/bluetooth/map/MapUtils/EmailUtils.java
blob: e333b34b62bac6cf5f85a8077c823d9889ba9aa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
/*
 * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *        * Redistributions of source code must retain the above copyright
 *          notice, this list of conditions and the following disclaimer.
 *        * Redistributions in binary form must reproduce the above copyright
 *          notice, this list of conditions and the following disclaimer in the
 *          documentation and/or other materials provided with the distribution.
 *        * Neither the name of The Linux Foundation nor
 *          the names of its contributors may be used to endorse or promote
 *          products derived from this software without specific prior written
 *          permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT ARE DISCLAIMED.    IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.codeaurora.bluetooth.map.MapUtils;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.Html;
import android.text.format.Time;
import android.util.Log;
import android.util.TimeFormatException;

import org.codeaurora.bluetooth.map.BluetoothMasAppParams;
import org.codeaurora.bluetooth.map.BluetoothMasService;
import org.codeaurora.bluetooth.map.MapUtils.CommonUtils.BluetoothMasMessageRsp;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.Locale;

public class EmailUtils {
    public static final String TAG = "EmailUtils";
    public static final boolean V = BluetoothMasService.VERBOSE;

    public static final int BIT_SUBJECT = 0x1;
    public static final int BIT_DATETIME = 0x2;
    public static final int BIT_SENDER_NAME = 0x4;
    public static final int BIT_SENDER_ADDRESSING = 0x8;

    public static final int BIT_RECIPIENT_NAME = 0x10;
    public static final int BIT_RECIPIENT_ADDRESSING = 0x20;
    public static final int BIT_TYPE = 0x40;
    public static final int BIT_SIZE = 0x80;

    public static final int BIT_RECEPTION_STATUS = 0x100;
    public static final int BIT_TEXT = 0x200;
    public static final int BIT_ATTACHMENT_SIZE = 0x400;
    public static final int BIT_PRIORITY = 0x800;

    public static final int BIT_READ = 0x1000;
    public static final int BIT_SENT = 0x2000;
    public static final int BIT_PROTECTED = 0x4000;
    public static final int BIT_REPLYTO_ADDRESSING = 0x8000;

    public static final String BMW = "BMW";

    public static List<String> folderListEmail(List<String> folderList, Context context) {
        String[] projection = new String[] {"displayName"};
        Uri uri = Uri.parse("content://com.android.email.provider/mailbox");
        Cursor cr = context.getContentResolver().query(uri, projection, null, null, null);

        if (cr != null && cr.moveToFirst()) {
            do {
                if (V){
                    Log.v(TAG, " Column Name: "+ cr.getColumnName(0) + " Value: " + cr.getString(0));
                }
                int folderFlag = 0;
                for(int i=0; i< folderList.size(); i++){
                    if(folderList.get(i).equalsIgnoreCase(cr.getString(0))){
                        folderFlag = 1;
                        break;
                    }
                }
                if(cr.getString(0).equalsIgnoreCase("Drafts")){ //TODO need to remove this hardcoded value
                    folderFlag = 1;
                }
                if(folderFlag == 0){
                    folderList.add(cr.getString(0));
                }

            } while ( cr.moveToNext());
        }
        if (V){
            Log.v(TAG, " Folder Listing of SMS,MMS and EMAIL: "+folderList);
        }
        if (cr != null) {
            cr.close();
        }
        return folderList;
    }

    public static String getWhereIsQueryForTypeEmail(String folder, Context context, int masId) {
        String query = "mailboxKey = -1";
        String folderId;
        Uri uri = Uri.parse("content://com.android.email.provider/mailbox");
        if (folder == null) {
            return query;
        }
        if (folder.contains("'")){
            folder = folder.replace("'", "''");
        }
        Cursor cr = context.getContentResolver().query(
                uri, null, "(" + ACCOUNT_KEY + "=" + getAccountId(masId) +
                ") AND (UPPER(displayName) = '"+ folder.toUpperCase()+"')" , null, null);
        if (cr != null) {
            if ( cr.moveToFirst()) {
                do {
                    folderId = cr.getString(cr.getColumnIndex("_id"));
                    query = "mailboxKey = "+ folderId;
                    break;
                } while ( cr.moveToNext());
            }
            cr.close();
        }
        return query;
    }

    public static int getMessageSizeEmail(long messageId, Context context) {
        if (V){
            Log.v(TAG, ":: Message Id in getMessageSizeEmail ::"+ messageId);
        }
        int msgSize = -1;
        String[] EMAIL_MSGSIZE_PROJECTION = new String[] { "LENGTH(textContent)", "LENGTH(htmlContent)" };
        String textContent, htmlContent;
        Uri uri = Uri.parse("content://com.android.email.provider/body");

        Cursor cr = context.getContentResolver().query(
                uri, EMAIL_MSGSIZE_PROJECTION, "messageKey = "+ messageId , null, null);

        if (cr != null && cr.moveToFirst()) {
            do {
                msgSize = cr.getInt(0);
                if(msgSize == -1 || msgSize == 0)
                   msgSize = cr.getInt(1);
                break;
            } while (cr.moveToNext());
        }
        if (cr != null) {
            cr.close();
        }
        return msgSize;
    }

    public static int getAttachmentSizeEmail(long messageId, Context context) {
        if (V){
            Log.v(TAG, ":: Message Id in getAttachmentSizeEmail ::"+ messageId);
        }
        int attchSize = 0;
        Uri uri = Uri.parse("content://com.android.email.provider/attachment");

        Cursor cr = context.getContentResolver().query(
                uri, new String[]{"size"}, "messageKey = "+ messageId , null, null);
        if (cr != null && cr.moveToFirst()) {
            do {
                attchSize += cr.getInt(0);
            } while (cr.moveToNext());
        }
        if (cr != null) {
            cr.close();
        }
        return attchSize;
    }

    public static String getFolderName(String[] splitStringsEmail) {
        String folderName=" ";
        int len = splitStringsEmail.length;
        if (V){
            Log.v(TAG, ":: Split Strings Array in getFolderName ::"+ splitStringsEmail);
        }
        folderName = splitStringsEmail[len -1];
        if (V){
            Log.v(TAG, "folderName :: " + folderName);
        }
        return folderName;
    }

    public static String getConditionString(String folderName, Context context,
            BluetoothMasAppParams appParams, int masId) {
        String whereClauseEmail = getWhereIsQueryForTypeEmail(folderName, context, masId);

        /* Filter readstatus: 0 no filtering, 0x01 get unread, 0x10 get read */
        if (appParams.FilterReadStatus != 0) {
            if ((appParams.FilterReadStatus & 0x1) != 0) {
                if (whereClauseEmail.length() != 0) {
                    whereClauseEmail += " AND ";
                }
                whereClauseEmail += " flagRead = 0 ";
            }
            if (V){
                Log.v(TAG, "Filter Read Status Value:"+appParams.FilterReadStatus);
                Log.v(TAG, "Filter Read Status Condition Value:"+(appParams.FilterReadStatus & 0x02));
            }
            if ((appParams.FilterReadStatus & 0x02) != 0) {
                if (whereClauseEmail.length() != 0) {
                    whereClauseEmail += " AND ";
                }
                whereClauseEmail += " flagRead = 1 ";
            }
        }
        if (V){
            Log.v(TAG, "Filter Recipient Value:"+appParams.FilterRecipient);
            Log.v(TAG, "Filter Recipient Condition 1 :"+(appParams.FilterRecipient != null));
            if (appParams.FilterRecipient != null) {
                Log.v(TAG, "Filter Recipient Condition 2 :"+(appParams.FilterRecipient.length() != 0));
            }
        }
        //Filter Recipient
        if ((appParams.FilterRecipient != null) && (appParams.FilterRecipient.length() > 0)
                && !(appParams.FilterRecipient.equalsIgnoreCase("*"))) {
                if(appParams.FilterRecipient.contains("*")){
                    appParams.FilterRecipient = appParams.FilterRecipient.replace('*', '%');
                }
                if (whereClauseEmail.length() != 0) {
                    whereClauseEmail += " AND ";
                }
            whereClauseEmail += " toList LIKE '%"+appParams.FilterRecipient.trim()+"%'";
        }

        // TODO Filter Originator

        if ((appParams.FilterOriginator != null)
                && (appParams.FilterOriginator.length() > 0)
                && !(appParams.FilterOriginator.equalsIgnoreCase("*"))) {
                if(appParams.FilterOriginator.contains("*")){
                    appParams.FilterOriginator = appParams.FilterOriginator.replace('*', '%');
                }
            // For incoming message
            if (whereClauseEmail.length() != 0) {
                whereClauseEmail += " AND ";
            }
            String str1 = appParams.FilterOriginator;
            whereClauseEmail += "fromList LIKE '%"+appParams.FilterOriginator.trim()+"%'";
        }
        if (V){
            Log.v(TAG, "whereClauseEmail :" + whereClauseEmail);
        }// TODO Filter priority?

        /* Filter Period Begin */
        if ((appParams.FilterPeriodBegin != null)
                && (appParams.FilterPeriodBegin.length() != 0)) {
            Time time = new Time();

            try {
                time.parse(appParams.FilterPeriodBegin.trim());
                if (whereClauseEmail.length() != 0) {
                    whereClauseEmail += " AND ";
                }
                whereClauseEmail += "timeStamp >= " + time.toMillis(false);

            } catch (TimeFormatException e) {
                Log.d(TAG, "Bad formatted FilterPeriodBegin, Ignore"
                        + appParams.FilterPeriodBegin);
            }
        }

        /* Filter Period End */
        if ((appParams.FilterPeriodEnd != null)
                && (appParams.FilterPeriodEnd.length() != 0)) {
            Time time = new Time();
            try {
                time.parse(appParams.FilterPeriodEnd.trim());
                if (whereClauseEmail.length() != 0) {
                    whereClauseEmail += " AND ";
                }
                whereClauseEmail += "timeStamp < " + time.toMillis(false);
            } catch (TimeFormatException e) {
                Log.d(TAG, "Bad formatted FilterPeriodEnd, Ignore"
                        + appParams.FilterPeriodEnd);
            }
        }

        return whereClauseEmail;
    }

    public static MsgListingConsts bldEmailMsgLstItem(Context context, String folderName,
                BluetoothMasAppParams appParams, String subject, String timestamp,
                String senderName, String senderAddressing, String recipientName,
                String recipientAddressing, String msgId, String readStatus, String replyToStr,
                long offset) {

        MsgListingConsts emailMsg = new MsgListingConsts();
        emailMsg.setMsg_handle(Long.valueOf(msgId)+ offset);

        Time time = new Time();
        time.set(Long.valueOf(timestamp));

        String datetimeStr = time.toString().substring(0, 15);

        emailMsg.msgInfo.setDateTime(datetimeStr);

        if (V){
            Log.v(TAG, "bldEmailMsgLstItem");
            Log.v(TAG, "Subject: " + subject);
            Log.v(TAG, "timestamp: " + timestamp);
            Log.v(TAG, "senderName: " + senderName);
            Log.v(TAG, "senderAddressing: " + senderAddressing);
            Log.v(TAG, "recipientName: " + recipientName);
            Log.v(TAG, "recipientAddressing: " + recipientAddressing);
            Log.v(TAG, "msgId: " + msgId);
            Log.v(TAG, "readStatus: " + readStatus);
            Log.v(TAG, "replyToStr: " + replyToStr);
            Log.v(TAG, "offset: " + offset);
        }

        if ((appParams.ParameterMask & BIT_SUBJECT) != 0) {

            if (V){
                Log.v(TAG, "bldEmailMsgLstItem :: Subject " + subject);
            }
            if ((subject != null && appParams.SubjectLength > 0)
                    && (subject.length() > appParams.SubjectLength)) {
                subject = subject.substring(0,
                        appParams.SubjectLength);
            }
            if(subject != null){
                emailMsg.setSubject(subject.trim());
            }
            emailMsg.sendSubject = true;
       }

        if ((appParams.ParameterMask & BIT_DATETIME) != 0) {
            /*emailMsg.setDatetime(time.toString().substring(0, 15)
                    + "-0700");*/
            emailMsg.setDatetime(datetimeStr);
        }

        if ((appParams.ParameterMask & BIT_SENDER_NAME) != 0) {
            if(senderName != null) {
                if(senderName.contains("")){
                    String[] senderStr = senderName.split("");
                    if(senderStr !=null && senderStr.length > 0){
                        if (V){
                            Log.v(TAG, " ::Sender name split String 0:: " + senderStr[0]
                                    + "::Sender name split String 1:: " + senderStr[1]);
                        }
                        emailMsg.setSender_name(senderStr[1].trim());
                    }
                }
                else{
                    emailMsg.setSender_name(senderName.trim());
                }
            }
       }

        if ((appParams.ParameterMask & BIT_SENDER_ADDRESSING) != 0) {
            if(senderAddressing != null) {
                if(senderAddressing.contains("")){
                    String[] senderAddrStr = senderAddressing.split("");
                    if(senderAddrStr !=null && senderAddrStr.length > 0){
                        if (V){
                            Log.v(TAG, " ::Sender Addressing split String 0:: " + senderAddrStr[0]
                                    + "::Sender Addressing split String 1:: " + senderAddrStr[1]);
                        }
                        emailMsg.setSender_addressing(senderAddrStr[0].trim());
                    }
                }
                else{
                    emailMsg.setSender_addressing(senderAddressing.trim());
                }
            }
        }

        if ((appParams.ParameterMask & BIT_RECIPIENT_NAME) != 0) {
            String multiRecepients = "";
            if(recipientName != null){
                if(recipientName.contains("")){
                    List<String> recipientNameArr = new ArrayList<String>();
                    List<String> recipientEmailArr = new ArrayList<String>();
                    String[] multiRecipientStr = recipientName.split("");
                    for(int i=0; i < multiRecipientStr.length ; i++){
                        if(multiRecipientStr[i].contains("")){
                            String[] recepientStr = multiRecipientStr[i].split("");
                            recipientNameArr.add(recepientStr[1]);
                            recipientEmailArr.add(recepientStr[0]);
                        }
                    }
                    if(recipientNameArr != null && recipientNameArr.size() > 0){
                        for(int i=0; i < recipientNameArr.size() ; i++){
                            if(i < (recipientNameArr.size()-1)){
                                multiRecepients += recipientNameArr.get(i)+";";
                            }
                            else{
                                multiRecepients += recipientNameArr.get(i);
                            }
                        }
                    }
                    emailMsg.setRecepient_name(multiRecepients.trim());
                }
                else if(recipientName.contains("")){
                    String[] recepientStr = recipientName.split("");
                    if(recepientStr !=null && recepientStr.length > 0){
                        if (V){
                            Log.v(TAG, " ::Recepient name split String 0:: " + recepientStr[0]
                                    + "::Recepient name split String 1:: " + recepientStr[1]);
                        }
                        emailMsg.setRecepient_name(recepientStr[1].trim());
                    }
                }
                else{
                    emailMsg.setRecepient_name(recipientName.trim());
                }
            }
        }

        if ((appParams.ParameterMask & BIT_RECIPIENT_ADDRESSING) != 0) {
            String multiRecepientAddrs = "";

            if (recipientAddressing != null) {
                if (recipientAddressing.contains("")) {
                    List<String> recipientNameArr = new ArrayList<String>();
                    List<String> recipientEmailArr = new ArrayList<String>();
                    if  (recipientName != null) {
                        String[] multiRecipientStr = recipientName.split("");
                        for (int i=0; i < multiRecipientStr.length ; i++) {
                            if (multiRecipientStr[i].contains("")) {
                                String[] recepientStr = multiRecipientStr[i].split("");
                                recipientNameArr.add(recepientStr[1]);
                                recipientEmailArr.add(recepientStr[0]);
                            }
                        }
                    }
                    final int recipientEmailArrSize = recipientEmailArr.size();
                    if (recipientEmailArrSize > 0) {
                        for (int i=0; i < recipientEmailArrSize ; i++) {
                            if (i < (recipientEmailArrSize-1)) {
                                multiRecepientAddrs += recipientEmailArr.get(i)+";";
                            } else {
                                multiRecepientAddrs += recipientEmailArr.get(i);
                            }
                        }
                    }
                    emailMsg.setRecepient_addressing(multiRecepientAddrs.trim());
                    emailMsg.setSendRecipient_addressing(true);
                } else if (recipientAddressing.contains("")) {
                    String[] recepientAddrStr = recipientAddressing.split("");
                    if (recepientAddrStr !=null && recepientAddrStr.length > 0) {
                        if (V){
                            Log.v(TAG, " ::Recepient addressing split String 0:: " + recepientAddrStr[0]
                                    + "::Recepient addressing split String 1:: " + recepientAddrStr[1]);
                        }
                        emailMsg.setRecepient_addressing(recepientAddrStr[0].trim());
                        emailMsg.setSendRecipient_addressing(true);
                    }
                } else {
                    emailMsg.setRecepient_addressing(recipientAddressing.trim());
                    emailMsg.setSendRecipient_addressing(true);
                }
            }
        }

        if ((appParams.ParameterMask & BIT_TYPE) != 0) {
            emailMsg.setType("EMAIL");
        }

        if ((appParams.ParameterMask & BIT_SIZE) != 0) {
            int  msgSize = 0;
            msgSize = getMessageSizeEmail(Long.valueOf(msgId), context);
            emailMsg.setSize(msgSize);
        }

        if ((appParams.ParameterMask & BIT_RECEPTION_STATUS) != 0) {
            emailMsg.setReception_status("complete");
        }

        if ((appParams.ParameterMask & BIT_TEXT) != 0) {
            // TODO Set text to "yes"
            emailMsg.setContains_text("yes");
        }

        if ((appParams.ParameterMask & BIT_ATTACHMENT_SIZE) != 0) {
            emailMsg.setAttachment_size(getAttachmentSizeEmail(Long.valueOf(msgId), context));
        }

        if ((appParams.ParameterMask & BIT_PRIORITY) != 0) {
            // TODO Get correct priority
            emailMsg.setPriority("no");
        }

        if ((appParams.ParameterMask & BIT_READ) != 0) {
            if (V){
                Log.v(TAG, " ::Read Status:: " + readStatus);
            }
            if (readStatus.equalsIgnoreCase("1")) {
                emailMsg.setRead("yes");
            } else {
                emailMsg.setRead("no");
            }
        }

        if ((appParams.ParameterMask & BIT_SENT) != 0) {
            // TODO Get sent status?
            if (folderName.equalsIgnoreCase("sent") || folderName.toUpperCase().contains("SENT")) {
                emailMsg.setSent("yes");
            } else {
                emailMsg.setSent("no");
            }
        }

        if ((appParams.ParameterMask & BIT_PROTECTED) != 0) {
            emailMsg.setMsg_protected("no");
        }

        if ((appParams.ParameterMask & BIT_REPLYTO_ADDRESSING) != 0) {
            //TODO need to test
            if (V){
                Log.v(TAG, " ::Reply To addressing:: " + replyToStr);
            }
            if (replyToStr !=null && replyToStr.length() != 0){
                if (replyToStr.contains("")){
                    String replyToStrArr[] = replyToStr.split("");
                    replyToStr = replyToStrArr[0];
                }
                emailMsg.setReplyTo_addressing(replyToStr);
            } else{
                emailMsg.setReplyTo_addressing(emailMsg.getSender_addressing());
            }
        }
        return emailMsg;
    }

    public static String bldEmailBmsg(long msgHandle, BluetoothMasMessageRsp rsp, Context context,
            String remoteDeviceName) {
        String str = null;
        //Query the message table for obtaining the message related details
        Cursor cr1 = null;
        int folderId;
        String timeStamp = null;
        String subjectText = null;
        Uri uri1 = Uri.parse("content://com.android.email.provider/message");
        String whereClause = " _id = " + msgHandle;

        // Create a bMessage
        BmessageConsts bmsg = new BmessageConsts();

        cr1 = context.getContentResolver().query(uri1, null, whereClause, null,
                null);
        if (cr1 != null && cr1.getCount() > 0) {
            cr1.moveToFirst();
            folderId = cr1.getInt(cr1.getColumnIndex("mailboxKey"));
            String containingFolder = getContainingFolderEmail(folderId, context);
            timeStamp = cr1.getString(cr1.getColumnIndex("timeStamp"));
            subjectText = cr1.getString(cr1.getColumnIndex("subject"));

            // TODO Get Current type
            bmsg.setType("EMAIL");

            bmsg.setBmsg_version("1.0");
            if (cr1.getString(cr1.getColumnIndex("flagRead")).equalsIgnoreCase("1")) {
                bmsg.setStatus("READ");
            } else {
                bmsg.setStatus("UNREAD");
            }

            bmsg.setFolder(MapUtilsConsts.Telecom + "/" + MapUtilsConsts.Msg +
                        "/" + containingFolder);

            bmsg.setVcard_version("2.1");

            String senderName = null;
            if((senderName = cr1.getString(cr1.getColumnIndex("fromList"))) != null ){
                if(senderName.contains("")){
                    String[] senderStr = senderName.split("");
                    if(senderStr !=null && senderStr.length > 0){
                        bmsg.setOriginatorVcard_name(senderStr[1].trim());
                        bmsg.setOriginatorVcard_email(senderStr[0].trim());
                    }
                }
                else{
                    bmsg.setOriginatorVcard_name(senderName.trim());
                    bmsg.setOriginatorVcard_email(senderName.trim());
                }
            }
            String recipientName = null;
            String multiRecepients = null;
            if((recipientName = cr1.getString(cr1.getColumnIndex("toList"))) != null){
                if(recipientName.contains("")){
                    String[] recepientStr = recipientName.split("");
                    if(recepientStr !=null && recepientStr.length > 0){
                        bmsg.setRecipientVcard_name(recepientStr[1].trim());
                        bmsg.setRecipientVcard_email(recepientStr[0].trim());
                    }
                }
                else if(recipientName.contains("")){
                    multiRecepients = recipientName.replace('', ';');
                    if(multiRecepients != null){
                        if (V){
                            Log.v(TAG, " ::Recepient name :: " + multiRecepients);
                        }
                        bmsg.setRecipientVcard_name(multiRecepients.trim());
                        bmsg.setRecipientVcard_email(multiRecepients.trim());
                    }
                }
                else{
                    bmsg.setRecipientVcard_name(recipientName.trim());
                    bmsg.setRecipientVcard_email(recipientName.trim());
                }
            }
        }
        if (cr1 != null) {
            cr1.close();
        }

        //Query the body table for obtaining the message body
        Cursor cr2 = null;
        Uri uri2 = Uri.parse("content://com.android.email.provider/body");
        String whereStr = " messageKey = " + msgHandle;
        cr2 = context.getContentResolver().query(uri2, null, whereStr, null,
                null);
        if (cr2 != null) {
            StringBuilder sb = new StringBuilder();
            String emailBody = null;

            //Set content type according to content from body table
            String contenType = "text/plain";
            if (cr2.getCount() > 0) {
                cr2.moveToFirst();
                emailBody = cr2.getString(cr2.getColumnIndex("textContent"));
                if (emailBody == null || emailBody.length() == 0){
                    String msgBody = cr2.getString(cr2.getColumnIndex("htmlContent"));
                    if (msgBody != null){
                        CharSequence msgText = Html.fromHtml(msgBody);
                        emailBody = msgText.toString();
                    }
                }
            }

            Date date = new Date(Long.parseLong(timeStamp));
            sb.append("From:").append(bmsg.getOriginatorVcard_email()).append("\r\n");
            sb.append("To:").append(bmsg.getRecipientVcard_email()).append("\r\n");
            if (remoteDeviceName != null && remoteDeviceName.startsWith(BMW)) {
                sb.append("Mime-Version: 1.0").append("\r\n");
                sb.append("Content-Type: text/plain; charset=\"UTF-8\"").append("\r\n");
                sb.append("Content-Transfer-Encoding: 8bit").append("\r\n");
                // BMW 14692 carkit accepts Date format in "EEE, dd MMM yyyy HH:mm:ss Z"
                sb.append("Date:");
                sb.append(new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US).format(date));
                sb.append("\r\n");
                sb.append("Subject:").append(subjectText).append("\r\n").append("\r\n");
                sb.append(emailBody).append("\r\n");
            } else {
                Random randomGenerator = new Random();
                int randomInt = randomGenerator.nextInt(1000);
                String boundary = "MessageBoundary."+randomInt;
                if (emailBody != null){
                    while (emailBody.contains(boundary)){
                        randomInt = randomGenerator.nextInt(1000);
                        boundary = "MessageBoundary."+randomInt;
                    }
                }
                sb.append("Date: ").append(date.toString()).append("\r\n");
                sb.append("Subject:").append(subjectText).append("\r\n");
                sb.append("Mime-Version: 1.0").append("\r\n");
                sb.append(
                        "Content-Type: multipart/mixed; boundary=\""+boundary+"\"")
                        .append("\r\n");
                sb.append("Content-Transfer-Encoding: 8bit").append("\r\n")
                        .append("\r\n");
                sb.append("MIME Message").append("\r\n");
                sb.append("--"+boundary).append("\r\n");
                sb.append("Content-Type: text/plain; charset=\"UTF-8\"").append("\r\n");
                sb.append("Content-Transfer-Encoding: 8bit").append("\r\n");
                sb.append("Content-Disposition:inline").append("\r\n")
                        .append("\r\n");
                sb.append(emailBody).append("\r\n");
                sb.append("--"+boundary+"--").append("\r\n");
            }

            bmsg.setBody_msg(sb.toString());
            bmsg.setBody_length(sb.length() + 22);
            // Send a bMessage
            if (V){
                Log.v(TAG, "bMessageEmail test\n");
                Log.v(TAG, "=======================\n\n");
            }
            str = MapUtils.toBmessageEmail(bmsg);
            cr2.close();
        }
        if (V){
            Log.v(TAG, "======FINAL BMSG=================\n\n");
            Log.v(TAG, str);
            Log.v(TAG, "\n\n");
        }
        return str;
    }

    /**
     * Get the folder name (MAP representation) for Email based on the
     * mailboxKey value in message table
     */
    public static String getContainingFolderEmail(int folderId, Context context) {
        Cursor cr;
        String folderName = null;
        String whereClause = "_id = " + folderId;
        cr = context.getContentResolver().query(
                Uri.parse("content://com.android.email.provider/mailbox"),
                null, whereClause, null, null);
        if (cr != null) {
            if (cr.getCount() > 0) {
                cr.moveToFirst();
                folderName = cr.getString(cr.getColumnIndex("displayName"));
            }
            cr.close();
        }
        return folderName;
    }

    public static final String AUTHORITY = "com.android.email.provider";
    public static final Uri EMAIL_URI = Uri.parse("content://" + AUTHORITY);
    public static final Uri EMAIL_ACCOUNT_URI = Uri.withAppendedPath(EMAIL_URI, "account");
    public static final Uri EMAIL_BOX_URI = Uri.withAppendedPath(EMAIL_URI, "mailbox");
    public static final Uri EMAIL_MESSAGE_URI = Uri.withAppendedPath(EMAIL_URI, "message");
    public static final String RECORD_ID = "_id";
    public static final String DISPLAY_NAME = "displayName";
    public static final String SERVER_ID = "serverId";
    public static final String ACCOUNT_KEY = "accountKey";
    public static final String MAILBOX_KEY = "mailboxKey";
    public static final String EMAIL_ADDRESS = "emailAddress";
    public static final String IS_DEFAULT = "isDefault";
    public static final String TYPE = "type";
    public static final String[] EMAIL_BOX_PROJECTION = new String[] {
        RECORD_ID, DISPLAY_NAME, ACCOUNT_KEY, TYPE
    };
    public static final int EMAIL_BOX_COLUMN_RECORD_ID = 0;
    public static final int EMAIL_BOX_COLUMN_DISPLAY_NAME = 1;
    public static final int EMAIL_BOX_COLUMN_ACCOUNT_KEY = 2;
    public static final int EMAIL_BOX_COLUMN_TYPE = 3;
    public static final String[] EMAIL_MESSAGE_PROJECTION = new String[] {
        RECORD_ID, MAILBOX_KEY, ACCOUNT_KEY
    };
    public static final int MSG_COL_RECORD_ID = 0;
    public static final int MSG_COL_MAILBOX_KEY = 1;
    public static final int MSG_COL_ACCOUNT_KEY = 2;
    private static final String[] ACCOUNT_ID_PROJECTION = new String[] {
        RECORD_ID, EMAIL_ADDRESS, IS_DEFAULT
    };
    private static final String[] ACCOUNT_ID_NAME_PROJECTION = new String[] {
       RECORD_ID, EMAIL_ADDRESS, DISPLAY_NAME
    };
    // Types of mailboxes. From EmailContent.java
    // inbox
    public static final int TYPE_INBOX = 0;
    // draft
    public static final int TYPE_DRAFT = 3;
    // outbox
    public static final int TYPE_OUTBOX = 4;
    // sent
    public static final int TYPE_SENT = 5;
    // deleted
    public static final int TYPE_DELETED = 6;

    public static HashMap<Long, Integer> sAccToMas = new HashMap<Long, Integer>();
    public static HashMap<Integer, Long> sMasToAcc = new HashMap<Integer, Long>();

    public static void clearMapTable() {
        sAccToMas.clear();
        sMasToAcc.clear();
    }

    public static void updateMapTable(long accountId, int masId) {
        if (sAccToMas.containsKey(accountId)) {
            sAccToMas.remove(accountId);
        }
        if (sMasToAcc.containsKey(masId)) {
            sMasToAcc.remove(masId);
        }
        sAccToMas.put(accountId, masId);
        sMasToAcc.put(masId, accountId);
    }

    public static long getAccountId(int masId) {
        Long accountId = sMasToAcc.get(masId);
        return (accountId != null) ? accountId : -1;
    }

    public static int getMasId(long accountId) {
        Integer masId = sAccToMas.get(accountId);
        return (masId != null) ? masId : -1;
    }

    public static void removeMasIdIfNotPresent(List<Long> accountIdList) {
        Collection<Long> oldList = sMasToAcc.values();
        ArrayList<Long> toRemove = new ArrayList<Long>();
        for (long oldId : oldList) {
            if (!accountIdList.contains(oldId)) {
                // remove it
                toRemove.add(oldId);
            }
        }
        for (long accountId : toRemove) {
            Integer masId = sAccToMas.remove(accountId);
            if (masId != null) {
                sMasToAcc.remove(masId);
            }
        }
    }

    public static int countEmailAccount(Context context) {
        return SqlHelper.count(context, EMAIL_ACCOUNT_URI, null, null);
    }

    /**
     * Returns whether Email account exists
     * @param context the calling Context
     * @return true if any Email account exists; false otherwise
     */
    public static boolean hasEmailAccount(Context context) {
        int numAccounts = SqlHelper.count(context, EMAIL_ACCOUNT_URI, null, null);
        if (numAccounts > 0) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean hasEmailAccount(Context context, long accountId) {
        String where = RECORD_ID + "=" + accountId;
        int numAccounts = SqlHelper.count(context, EMAIL_ACCOUNT_URI, where, null);
        if (numAccounts > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Returns the first Email account id that satisfies where condition
     * @param context the calling Context
     * @param where the condition respect to {@link #RECORD_ID}, {@link #EMAIL_ADDRESS}, {@link #IS_DEFAULT}
     * @return Email account id
     */
    public static long getEmailAccountId(Context context, String where) {
        if (V) Log.v(TAG, "getEmailAccountId(" + where + ")");
        long id = -1;
        Cursor cursor = context.getContentResolver().query(EMAIL_ACCOUNT_URI,
                ACCOUNT_ID_PROJECTION, where, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                id = cursor.getLong(0);
            }
            cursor.close();
        }
        if (V) Log.v(TAG, "id = " + id);
        return id;
    }

    /**
     * Returns the first Email account id name that satisfies where condition
     * @param context the calling Context
     * @param where the condition respect to {@link #RECORD_ID}, {@link #EMAIL_ADDRESS}, {@link #IS_DEFAULT}
     * @return Email account id Email
     */
    public static String getEmailAccountIdEmail(Context context, String where) {
        if (V) Log.v(TAG, "getEmailAccountIdName(" + where + ")");
        String idEmail = null;
        Cursor cursor = context.getContentResolver().query(EMAIL_ACCOUNT_URI,
                ACCOUNT_ID_NAME_PROJECTION, where, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                idEmail = cursor.getString(1);
            }
            cursor.close();
        }
        if (V) Log.v(TAG, "idEmail = " + idEmail);
        return idEmail;
    }

    /**
     * Returns the first Email account id name that satisfies where condition
     * @param context the calling Context
     * @param where the condition respect to {@link #RECORD_ID}, {@link #EMAIL_ADDRESS}, {@link #IS_DEFAULT}
     * @return Email account id Display Name
     */
    public static String getEmailAccountDisplayName(Context context, String where) {
        if (V) Log.v(TAG, "getEmailAccountIdName(" + where + ")");
        String displayName = null;
        Cursor cursor = context.getContentResolver().query(EMAIL_ACCOUNT_URI,
                ACCOUNT_ID_NAME_PROJECTION, where, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                displayName = cursor.getString(2);
            }
            cursor.close();
        }
        if (V) Log.v(TAG, "displayName = " + displayName);
        return displayName;
    }


    /**
     * Returns the default Email account id; the first account id if no default account
     * @param context the calling Context
     * @return the default Email account id
     */
    public static long getDefaultEmailAccountId(Context context) {
        if (V) Log.v(TAG, "getDefaultEmailAccountId()");
        long id = getEmailAccountId(context, IS_DEFAULT + "=1");
        if (id == -1) {
            id = getEmailAccountId(context, null);
        }
        if (V) Log.v(TAG, "id = " + id);
        return id;
    }

    public static List<Long> getEmailAccountIdList(Context context) {
        if (V) Log.v(TAG, "getEmailAccountIdList()");
        long id = -1;
        ArrayList<Long> list = new ArrayList<Long>();
        Cursor cursor = context.getContentResolver().query(EMAIL_ACCOUNT_URI,
                ACCOUNT_ID_PROJECTION, null, null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    id = cursor.getLong(0);
                    list.add(id);
                    if (V) Log.v(TAG, "id = " + id);
                } while (cursor.moveToNext());
            }
            cursor.close();
        }
        return list;
    }

    /**
     * Return Email sub folder list for the id and serverId path
     * @param context the calling Context
     * @param id the email account id
     * @return the list of  server id's of Email sub folder
     */
    public static List<String> getEmailFolderListAtPath(Context context, long id, String path) {
        if (V) Log.v(TAG, "getEmailFolderListAtPath: id = " + id + "path: " + path);
        StringBuilder sb = new StringBuilder();
        if (id > 0) {
            sb.append(ACCOUNT_KEY);
            sb.append("=");
            sb.append(id);
            sb.append(" AND ");
        }
        //Return Default List for no path
        if(path.equals("")) {
            sb.append(SERVER_ID);
            sb.append("=");
            sb.append(DISPLAY_NAME);
        }
        else {
            sb.append(SERVER_ID);
            sb.append(" LIKE '");
            sb.append(path);
            sb.append("/%'");
        }
        return SqlHelper.getListForColumn(context, EMAIL_BOX_URI, SERVER_ID, sb.toString(), null);
    }
    /**
     * Return Email folder list for the id
     * @param context the calling Context
     * @param id the email account id
     * @return the list of Email folder
     */
    public static List<String> getEmailFolderList(Context context, long id) {
        if (V) Log.v(TAG, "getEmailFolderList: id = " + id);
        StringBuilder sb = new StringBuilder();
        if (id > 0) {
            sb.append(ACCOUNT_KEY);
            sb.append("=");
            sb.append(id);
        }
        return SqlHelper.getListForColumn(context, EMAIL_BOX_URI, DISPLAY_NAME, sb.toString(), null);
    }

    /**
     * Return folder name for the type of mailbox
     * @param context the calling Context
     * @param id the email account id
     * @param type
     * @return
     */
    public static String getFolderForType(Context context, long id, int type) {
        if (V) Log.v(TAG, "getFolderForType: id = " + id + ", type = " + type);
        StringBuilder sb = new StringBuilder();
        if (id > 0) {
            sb.append(ACCOUNT_KEY);
            sb.append("=");
            sb.append(id);
            sb.append(" AND ");
        }
        sb.append(TYPE);
        sb.append("=");
        sb.append(type);
        return SqlHelper.getFirstValueForColumn(context, EMAIL_BOX_URI, DISPLAY_NAME, sb.toString(), null);
    }

    /**
     * Return list of folder names for the type of mailbox
     * @param context the calling Context
     * @param id the email account id
     * @param type
     * @return
     */
    public static List<String> getFoldersForType(Context context, long id, int type) {
        if (V) Log.v(TAG, "getFolderForType: id = " + id + ", type = " + type);
        StringBuilder sb = new StringBuilder();
        if (id > 0) {
            sb.append(ACCOUNT_KEY);
            sb.append("=");
            sb.append(id);
            sb.append(" AND ");
        }
        sb.append(TYPE);
        sb.append("=");
        sb.append(type);
        return SqlHelper.getListForColumn(context, EMAIL_BOX_URI, DISPLAY_NAME, sb.toString(), null);
    }

    public static int getTypeForFolder(Context context, long id, String folderName) {
        if (V) Log.v(TAG, "getTypeForFolder: id = " + id + ", folderName = " + folderName);
        StringBuilder sb = new StringBuilder();
        if (id > 0) {
            sb.append(ACCOUNT_KEY);
            sb.append("=");
            sb.append(id);
            sb.append(" AND ");
        }
        sb.append(DISPLAY_NAME);
        sb.append("=");
        sb.append("'"+folderName+"'");
        return SqlHelper.getFirstIntForColumn(context, EMAIL_BOX_URI, TYPE, sb.toString(), null);
    }
}