// Implement OnLogin method
        public void OnRegister(object sender, EventArgs e)
        {
            IdentityKeyPair     identityKeyPair = KeyHelper.generateIdentityKeyPair();
            uint                registrationId  = KeyHelper.generateRegistrationId(false);
            List <PreKeyRecord> preKeys         = KeyHelper.generatePreKeys(registrationId, 100).ToList();
            Random              random          = new Random();
            SignedPreKeyRecord  signedPreKey    = KeyHelper.generateSignedPreKey(identityKeyPair, Convert.ToUInt32(random.Next()));

            InMemorySessionStore SessionStore = new InMemorySessionStore();
            //SessionStore.StoreSession(new SignalProtocolAddress("1234", 1), new SessionRecord());
            PreKeyStore              PreKeyStore       = new InMemoryPreKeyStore();
            SignedPreKeyStore        SignedPreKeyStore = new InMemorySignedPreKeyStore();
            InMemoryIdentityKeyStore IdentityStore     = new InMemoryIdentityKeyStore(identityKeyPair, registrationId);

            // Store preKeys in PreKeyStore.
            foreach (PreKeyRecord preKey in preKeys)
            {
                PreKeyStore.StorePreKey(preKey.getId(), preKey);
            }

            // Store signed prekey in SignedPreKeyStore.
            SignedPreKeyStore.StoreSignedPreKey(signedPreKey.getId(), signedPreKey);

            // Save stores in local Database
            ISharedPreferences       sharedprefs = PreferenceManager.GetDefaultSharedPreferences(this);
            ISharedPreferencesEditor editor      = sharedprefs.Edit();

            editor.PutString("PreKeyStore", JsonConvert.SerializeObject(PreKeyStore));
            editor.PutString("SignedPreKeyStore", JsonConvert.SerializeObject(SignedPreKeyStore));
            List <Session> AllSessions = SessionStore.GetAllSessions();

            editor.PutString("AllSessions", JsonConvert.SerializeObject(AllSessions));
            editor.PutString("SessionStore", JsonConvert.SerializeObject(SessionStore));
            editor.PutString("IdentityStore", JsonConvert.SerializeObject(IdentityStore));
            List <TrustedKey> AllTrustedKeys = IdentityStore.GetAllTrustedKeys();

            editor.PutString("AllTrustedKeys", JsonConvert.SerializeObject(AllTrustedKeys));
            editor.Apply();

            //var sessionStore = JsonConvert.DeserializeObject<InMemorySessionStore>(sharedprefs.GetString("SessionStore", string.Empty));
            //var allSessions = JsonConvert.DeserializeObject<List<Session>>(sharedprefs.GetString("AllSessions", string.Empty));
            //foreach (Session item in allSessions)
            //{
            //    sessionStore.StoreSession(item.Name, item.DeviceID, item.array);
            //}

            // method to regidter in database
            RequestRegister(registrationId, identityKeyPair, signedPreKey, preKeys);
            StartActivity(typeof(FriendsActivity));
        }
        protected override void OnCreate(Bundle bundle)
        {
            // Getting saved data
            ISharedPreferences sharedPref = PreferenceManager.GetDefaultSharedPreferences(this);
            User SelectedFriend           = JsonConvert.DeserializeObject <User>(sharedPref.GetString("SelectedFriend", string.Empty));
            List <Models.Message> SelectedFriendMessageList = new List <Models.Message>();

            SelectedFriendMessageList = JsonConvert.DeserializeObject <List <Models.Message> >(sharedPref.GetString("SelectedFriendMessageList", string.Empty));
            foreach (Models.Message m in SelectedFriendMessageList)
            {
                SelectedFriend.SelectedUserMessages.Add(m);
            }
            InMemorySessionStore sessionStore = JsonConvert.DeserializeObject <InMemorySessionStore>(sharedPref.GetString("SessionStore", string.Empty));
            var allSessions = JsonConvert.DeserializeObject <List <Session> >(sharedPref.GetString("AllSessions", string.Empty));

            foreach (Session item in allSessions)
            {
                sessionStore.StoreSession(item.Name, item.DeviceID, item.array);
            }
            PreKeyStore              preKeyStore       = JsonConvert.DeserializeObject <InMemoryPreKeyStore>(sharedPref.GetString("PreKeyStore", string.Empty));
            SignedPreKeyStore        signedPreKeyStore = JsonConvert.DeserializeObject <InMemorySignedPreKeyStore>(sharedPref.GetString("SignedPreKeyStore", string.Empty));
            InMemoryIdentityKeyStore identityStore     = JsonConvert.DeserializeObject <InMemoryIdentityKeyStore>(sharedPref.GetString("IdentityStore", string.Empty));
            IdentityKeyPair          KeyPair           = new IdentityKeyPair(JsonConvert.DeserializeObject <byte[]>(sharedPref.GetString("IdentityKeyPair", string.Empty)));
            uint RegistrationID = Convert.ToUInt32(sharedPref.GetString("RegistrationId", string.Empty));

            identityStore.PutValues(KeyPair, RegistrationID);
            var allTrustedKeys = JsonConvert.DeserializeObject <List <TrustedKey> >(sharedPref.GetString("AllTrustedKeys", string.Empty));

            foreach (TrustedKey item in allTrustedKeys)
            {
                identityStore.SaveIdentity(item.Name, new IdentityKey(item.Identity, 0));
            }
            SignalProtocolAddress SelectedFriendAddress = new SignalProtocolAddress(SelectedFriend.RegisterationID.ToString(), 1);

            // Get the messages from the server
            base.OnCreate(bundle);
            TheirMessages = GetMessages(sharedPref, SelectedFriend, sessionStore, preKeyStore, signedPreKeyStore, identityStore, SelectedFriendAddress);
            // Set our view from the "ChatList" layout resource
            Title = SelectedFriend.Username;
            SetContentView(Resource.Layout.Message);
            listView = FindViewById <ListView>(Resource.Id.messageList);
            //***display FriendsListItem in ListView using Adapter
            listView.Adapter = adapter = new Adapter(this, TheirMessages, UserID);
            messageText      = FindViewById <EditText>(Resource.Id.messageText);
            sendButton       = FindViewById <Button>(Resource.Id.sendButton);

            // **implement messages sending and receiving here
            //listView.Adapter = adapter = new Adapter(this);

            sendButton.Click += (sender, e) =>
            {
                SessionCipher     sessionCipher = new SessionCipher(sessionStore, preKeyStore, signedPreKeyStore, identityStore, SelectedFriendAddress);
                CiphertextMessage cipherMessage = sessionCipher.encrypt(Encoding.UTF8.GetBytes(messageText.Text));
                SelectedFriend.SelectedUserMessages.Add(new Models.Message()
                {
                    MessageID = 1,
                    MessageReceiverRegisID = SelectedFriend.RegisterationID,
                    MessageSenderRegisID   = RegistrationID,
                    MessageText            = messageText.Text,
                    MessageTimestamp       = DateTime.Now
                });

                // Save stores in local Database
                ISharedPreferences       sharedprefs = PreferenceManager.GetDefaultSharedPreferences(this);
                ISharedPreferencesEditor editor      = sharedprefs.Edit();
                SelectedFriendMessageList.Clear();
                foreach (Models.Message m in SelectedFriend.SelectedUserMessages)
                {
                    SelectedFriendMessageList.Add(m);
                }
                editor.PutString("SelectedFriendMessageList", JsonConvert.SerializeObject(SelectedFriendMessageList));
                editor.PutString("SelectedFriend", JsonConvert.SerializeObject(SelectedFriend));
                editor.PutString("PreKeyStore", JsonConvert.SerializeObject(preKeyStore));
                editor.PutString("SignedPreKeyStore", JsonConvert.SerializeObject(signedPreKeyStore));
                List <Session> AllSessions = sessionStore.GetAllSessions();
                editor.PutString("AllSessions", JsonConvert.SerializeObject(AllSessions));
                editor.PutString("SessionStore", JsonConvert.SerializeObject(sessionStore));
                editor.PutString("IdentityStore", JsonConvert.SerializeObject(identityStore));
                List <TrustedKey> AllTrustedKeys = identityStore.GetAllTrustedKeys();
                editor.PutString("AllTrustedKeys", JsonConvert.SerializeObject(AllTrustedKeys));
                editor.Apply();

                Models.Message message = new Models.Message();
                message.MessageID = 4;
                message.MessageReceiverRegisID = SelectedFriend.RegisterationID;
                message.MessageSenderRegisID   = Convert.ToUInt32(sharedPref.GetString("RegistrationId", string.Empty));
                message.MessageText            = JsonConvert.SerializeObject(cipherMessage.serialize());
                message.MessageTimestamp       = DateTime.Now;

                //  call SendMessage() to send the
                SendMessage(message);
                //  *display the messages in user's own screen using adapter (always display in MyMessageListItem).
                adapter.NotifyDataSetInvalidated();
                listView.SetSelection(adapter.Count);
            };
        }