private void loadProfilePicture(Guid contactId)
        {
            if (contactId != null) {
                ContactDB cont = new ContactDB ();
                if (contacts.ContainsKey (contactId))
                    cont = contacts [contactId];

                byte[] imgdata = cont.ContactUser.Picture;
                Bitmap original = BitmapFactory.DecodeByteArray (imgdata, 0, imgdata.Length);
                Bitmap mask = Bitmap.CreateScaledBitmap (BitmapFactory.DecodeResource (Resources, Resource.Drawable.emptybackground), original.Width, original.Height, true);
                Bitmap result = Bitmap.CreateBitmap (mask.Width, mask.Height, Bitmap.Config.Argb8888);
                Canvas canv = new Canvas (result);
                Paint paint = new Paint (PaintFlags.AntiAlias);

                paint.SetXfermode (new PorterDuffXfermode (PorterDuff.Mode.DstIn));
                canv.DrawBitmap (original, 0, 0, null);
                canv.DrawBitmap (mask, 0, 0, paint);
                paint.SetXfermode (null);

                RunOnUiThread (delegate {
                    ImageView pic = (ImageView)converse.FindViewWithTag (new Java.Lang.String ("profilepic_" + contactId.ToString ()));
                    pic.SetImageBitmap (result);
                    original.Dispose ();
                    mask.Dispose ();
                    paint.Dispose ();
                    canv.Dispose ();
                    result.Dispose ();
                });
            }
        }
        public static ContactDB ConvertFromContact(Contact item)
        {
            ContactDB toReturn = new ContactDB ();
            toReturn.Blocked = item.Blocked;
            toReturn.ContactAccountID = item.ContactAccountID;
            toReturn.ContactID = item.ContactID;

            if (null != item.ContactOAuths) {

                toReturn.ContactOAuthItems = new List<ContactOAuthDB> (item.ContactOAuths.Count);

                foreach (Contact.ContactOAuth eachOAuth in item.ContactOAuths) {
                    ContactOAuthDB contactOAuthDB = ContactOAuthDB.ConvertFromContactOAuth (eachOAuth);
                    contactOAuthDB.ContactGuid = toReturn.ContactGuid;
                    toReturn.ContactOAuthItems.Add (contactOAuthDB);
                }//end foreach

            }//end if

            toReturn.ContactUser = item.ContactUser;
            toReturn.Errors = item.Errors;
            toReturn.OwnerAccountID = item.OwnerAccountID;
            toReturn.DateLastUpdated = item.DateLastUpdated;
            toReturn.DateCreated = item.DateCreated;
            return toReturn;
        }
 public static Contact ConvertFromContactDB(ContactDB item)
 {
     Contact toReturn = new Contact ();
     toReturn.Blocked = item.Blocked;
     toReturn.ContactAccountID = item.ContactAccountID;
     toReturn.ContactID = item.ContactID;
     toReturn.ContactOAuths = item.ContactOAuths;
     toReturn.ContactUser = item.ContactUser;
     toReturn.Errors = item.Errors;
     toReturn.OwnerAccountID = item.OwnerAccountID;
     toReturn.DateLastUpdated = item.DateLastUpdated;
     toReturn.DateCreated = item.DateCreated;
     return toReturn;
 }
        private void CreateMessagesUI()
        {
            List<UserDB> participants = new List<UserDB> ();
            bool moreThanOne = false, isCurrentMe = false;

            if (MessageConversations.clearView) {
                listWrapper.RemoveAllViews ();
                MessageConversations.clearView = false;
            }

            if (Contacts.SelectContactsUtil.selectedContacts.Count != 0)
                Contacts.SelectContactsUtil.selectedContacts.Clear ();

            if (MessageConversations.currentConversationMessages [0].MessageRecipientDBList.Count > 1) {
                moreThanOne = true;
                for (int m = 0; m < MessageConversations.currentConversationMessages[0].MessageRecipientDBList.Count; ++m) {
                    if (MessageConversations.currentConversationMessages [0].MessageRecipientDBList [m].AccountGuid != AndroidData.CurrentUser.AccountID.ToString ()) {
                        UserDB userDetails = dbm.GetUserWithAccountID (MessageConversations.currentConversationMessages [0].MessageRecipientDBList [m].AccountGuid);
                        participants.Add (userDetails);
                        ContactDB contact = new ContactDB ();
                        contact.ContactUser = new LOLAccountManagement.User ();
                        contact.ContactUser.AccountID = userDetails.AccountID;
                        Contacts.SelectContactsUtil.selectedContacts.Add (contact);
                    } else {
                        UserDB userDetails = UserDB.ConvertFromUser (AndroidData.CurrentUser);
                        participants.Add (userDetails);
                    }
                }
            } else {
                UserDB userDetails = dbm.GetUserWithAccountID (MessageConversations.currentConversationMessages [0].MessageRecipientDBList [0].AccountGuid);
                ContactDB contact = new ContactDB ();
                contact.ContactUser = new LOLAccountManagement.User ();
                contact.ContactUser.AccountID = userDetails.AccountID;
                Contacts.SelectContactsUtil.selectedContacts.Add (contact);
            }

            if (moreThanOne) {
                string toReturn = "";
                List<UserDB> sortedList = new List<UserDB> ();
                sortedList = participants.OrderBy (s => s.LastName).OrderBy (s => s.FirstName).ToList ();
                foreach (UserDB eachItem in sortedList)
                    toReturn += string.Format ("{0} {1}, ", eachItem.FirstName, eachItem.LastName);
                int last = toReturn.LastIndexOf (", ");
                toReturn = toReturn.Remove (last);

                using (LinearLayout btnlayout = new LinearLayout (context)) {
                    btnlayout.Orientation = Android.Widget.Orientation.Vertical;
                    btnlayout.SetGravity (GravityFlags.Center);
                    btnlayout.LayoutParameters = new ViewGroup.LayoutParams (LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
                    btnlayout.SetPadding ((int)ImageHelper.convertDpToPixel (5f, context), 0, (int)ImageHelper.convertDpToPixel (5f, context), (int)ImageHelper.convertDpToPixel (10f, context));

                    using (TextView name = new TextView(context)) {
                        name.Text = toReturn;
                        name.SetTextSize (Android.Util.ComplexUnitType.Dip, 18f);
                        name.SetTextColor (Color.Black);
                        RunOnUiThread (() => btnlayout.AddView (name));
                    }

                    using (Button showAll = new Button (context)) {
                        showAll.Gravity = GravityFlags.CenterVertical;
                        showAll.Text = Application.Context.Resources.GetString (Resource.String.messageShowAllInConversation);
                        showAll.Click += (object sender, EventArgs e) => {
                            showParticipants (sender, e, participants); };
                        showAll.SetWidth ((int)ImageHelper.convertDpToPixel (180f, context));
                        showAll.SetHeight ((int)ImageHelper.convertDpToPixel (30f, context));
                        showAll.SetBackgroundResource (Resource.Drawable.button);
                        RunOnUiThread (() => btnlayout.AddView (showAll));
                    }
                    RunOnUiThread (() => listWrapper.AddView (btnlayout));
                }

            }
            foreach (MessageDB message in MessageConversations.currentConversationMessages) {
                isCurrentMe = message.FromAccountID != AndroidData.CurrentUser.AccountID ? false : true;
                LinearLayout shell = new LinearLayout (context);
                shell.LayoutParameters = new ViewGroup.LayoutParams (LinearLayout.LayoutParams.FillParent, (int)ImageHelper.convertDpToPixel (imageSize [0] + 50f, context));
                shell.Orientation = Orientation.Horizontal;
                shell.SetPadding (0, 0, 0, (int)ImageHelper.convertDpToPixel (5f, context));

                UserDB whoAmI = new UserDB ();
                whoAmI = message.FromAccountID != AndroidData.CurrentUser.AccountID ? dbm.GetUserWithAccountID (message.FromAccountGuid) : UserDB.ConvertFromUser (AndroidData.CurrentUser);

                LinearLayout imageViewLayout = new LinearLayout (context);
                imageViewLayout.LayoutParameters = new ViewGroup.LayoutParams (LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
                imageViewLayout.SetGravity (GravityFlags.CenterVertical);
                ImageView userImage = new ImageView (context);
                if (Contacts.ContactsUtil.contactFilenames.Contains (message.FromAccountGuid)) {
                    using (Bitmap bm = BitmapFactory.DecodeFile (System.IO.Path.Combine (wowZapp.LaffOutOut.Singleton.ImageDirectory, message.FromAccountGuid))) {
                        using (MemoryStream ms = new MemoryStream ()) {
                            bm.Compress (Bitmap.CompressFormat.Jpeg, 80, ms);
                            byte[] image = ms.ToArray ();
                            using (Bitmap myBitmap = ImageHelper.CreateUserProfileImageForDisplay(image, (int)imageSize[0], (int)imageSize[1], this.Resources)) {
                                RunOnUiThread (delegate {
                                    userImage.SetImageBitmap (myBitmap);
                                });
                            }
                        }
                    }
                } else {
                    userImage.LayoutParameters = new ViewGroup.LayoutParams ((int)ImageHelper.convertDpToPixel (imageSize [0], context), (int)ImageHelper.convertDpToPixel (imageSize [1], context));
                    userImage.SetScaleType (ImageView.ScaleType.FitXy);
                    userImage.SetImageResource (Resource.Drawable.defaultuserimage);
                }
                RunOnUiThread (() => imageViewLayout.AddView (userImage));

                LinearLayout messageShell = new LinearLayout (context);
                int left = (int)wowZapp.LaffOutOut.Singleton.ScreenXWidth - (int)ImageHelper.convertDpToPixel (imageSize [0] + 20f, context);
                int leftOver = (int)wowZapp.LaffOutOut.Singleton.ScreenXWidth - (int)ImageHelper.convertDpToPixel (imageSize [0] + 40f, context);
                messageShell.LayoutParameters = new ViewGroup.LayoutParams (!isCurrentMe ? LinearLayout.LayoutParams.FillParent : left, LinearLayout.LayoutParams.FillParent);
                messageShell.Orientation = Orientation.Vertical;

                LinearLayout from = new LinearLayout (context);
                using (LinearLayout.LayoutParams layParms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, (int)ImageHelper.convertDpToPixel(24f, context))) {
                    layParms.SetMargins (isCurrentMe ? 0 : (int)ImageHelper.convertDpToPixel (30f, context), 0, isCurrentMe ? (int)ImageHelper.convertDpToPixel (30f, context) : 0, 0);
                    layParms.Weight = 10f;
                    from.LayoutParameters = layParms;
                }

                TextView whoIsIt = new TextView (context);
                whoIsIt.SetTextColor (Color.Black);
                whoIsIt.SetTextSize (Android.Util.ComplexUnitType.Dip, 12f);
                whoIsIt.Gravity = !isCurrentMe ? GravityFlags.Left : GravityFlags.Right;
                whoIsIt.Text = whoAmI.FirstName + " " + whoAmI.LastName;
                RunOnUiThread (() => from.AddView (whoIsIt));

                LinearLayout messageBox = new LinearLayout (context);
                using (LinearLayout.LayoutParams linParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.FillParent)) {
                    messageBox.LayoutParameters = linParams;
                    linParams.Weight = 60f;
                }
                for (int m = 0; m < message.MessageStepDBList.Count; ++m) {
                    if (message.MessageStepDBList [m].StepType == LOLMessageDelivery.MessageStep.StepTypes.Text) {
                        TextView messageText = new TextView (context);
                        using (LinearLayout.LayoutParams layParam = new LinearLayout.LayoutParams (LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.FillParent)) {
                            messageText.LayoutParameters = layParam;
                        }
                        messageText.SetBackgroundResource (message.FromAccountID != AndroidData.CurrentUser.AccountID ? Resource.Drawable.bubblesolidleft :
                                                           Resource.Drawable.bubblesolidright);
                        int lr = (int)ImageHelper.convertDpToPixel (20f, context);
                        int tb = lr / 2;
                        messageText.SetPadding (lr, tb, lr, tb);
                        messageText.SetTextColor (Color.White);
                        messageText.SetTextSize (Android.Util.ComplexUnitType.Dip, 14f);
                        string messager = message.MessageStepDBList [m].MessageText;
                        if (messager.Length > 40)
                            messager = messager.Substring (0, 40) + "...";
                        messageText.Text = messager;
                        messageText.ContentDescription = message.MessageGuid;
                        RunOnUiThread (() => messageBox.AddView (messageText));
                        break;
                    }
                }

                LinearLayout messageLay = new LinearLayout (context);
                using (LinearLayout.LayoutParams messParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, (int)ImageHelper.convertDpToPixel(24f, context))) {
                    messParam.SetMargins (isCurrentMe ? 0 : (int)ImageHelper.convertDpToPixel (30f, context), 0, isCurrentMe ? (int)ImageHelper.convertDpToPixel (30f, context) : 0, 0);
                    messParam.Weight = 10f;
                    messageLay.LayoutParameters = messParam;
                }

                TextView messageDate = new TextView (context);
                messageDate.LayoutParameters = new ViewGroup.LayoutParams (LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
                messageDate.SetTextColor (Color.Black);
                messageDate.SetTextSize (Android.Util.ComplexUnitType.Dip, 10f);
                messageDate.Gravity = message.FromAccountID != AndroidData.CurrentUser.AccountID ? GravityFlags.Left : GravityFlags.Right;
                messageDate.Text = message.MessageSent.ToShortTimeString () + ", " + message.MessageSent.ToShortDateString ();
                RunOnUiThread (() => messageLay.AddView (messageDate));

                LinearLayout messageItems = new LinearLayout (context);
                messageItems.Orientation = Orientation.Horizontal;
                float messageBarSize = wowZapp.LaffOutOut.Singleton.resizeFonts == true ? ImageHelper.convertPixelToDp (((50f * imageSize [0]) / 100), context) :
                    ImageHelper.convertDpToPixel (40f, context);
                using (LinearLayout.LayoutParams layParams = new LinearLayout.LayoutParams (LinearLayout.LayoutParams.FillParent, (int)messageBarSize)) {
                    layParams.SetMargins ((int)ImageHelper.convertDpToPixel (14f, context), (int)ImageHelper.convertDpToPixel (3.3f, context),
                                          (int)ImageHelper.convertDpToPixel (12.7f, context), (int)ImageHelper.convertDpToPixel (4f, context));
                    messageItems.LayoutParameters = layParams;
                }
                messageItems.SetPadding ((int)ImageHelper.convertDpToPixel (10f, context), (int)ImageHelper.convertDpToPixel (3.3f, context),
                                         (int)ImageHelper.convertDpToPixel (10f, context), 0);
                messageItems.SetGravity (GravityFlags.Left);
                messageItems = createMessageBar (messageItems, message, leftOver);

                RunOnUiThread (delegate {
                    messageShell.AddView (from);
                    messageShell.AddView (messageBox);
                    messageShell.AddView (messageLay);
                    messageShell.AddView (messageItems);

                    if (whoAmI.AccountID == AndroidData.CurrentUser.AccountID) {
                        shell.AddView (messageShell);
                        shell.AddView (imageViewLayout);
                    } else {
                        shell.AddView (imageViewLayout);
                        shell.AddView (messageShell);
                    }
                    listWrapper.AddView (shell);
                });
            }
            if (progress != null)
                RunOnUiThread (() => progress.Dismiss ());
        }