public void DoWork () { db = new DataBaseWrapper (Resources); network = new AsyncNetwork (); network.SetBackgroundService (this); user = db.Table<User>().FirstOrDefault (); var connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService); if (user != null && user.user != null) { var t = new Thread (async () => { while (true) { Setting.Frequency f = db.Table<Setting>().FirstOrDefault().Synchronisation; if (f != Setting.Frequency.wlan || connectivityManager.GetNetworkInfo(ConnectivityType.Wifi).GetState() == NetworkInfo.State.Connected) { while(!await network.UpdateEvents (db, user)) { await network.Authenticate(db, user); } } // Wifi connection gets normal updates, other networks get 4 times worse update time. if (connectivityManager.GetNetworkInfo(ConnectivityType.Wifi).GetState() == NetworkInfo.State.Connected) { connectivityMode = 1; } else { connectivityMode = 4; } switch(f) { case Setting.Frequency.often: Mode = 1; break; case Setting.Frequency.normal: Mode = 2; break; case Setting.Frequency.rare: Mode = 4; break; case Setting.Frequency.wlan: Mode = 2; break; } Thread.Sleep (UpdateInterval * Mode* connectivityMode); if (UpdateInterval < 12000) { UpdateInterval += 1000; } else if (UpdateInterval < 30000) { UpdateInterval += 2000; } else { UpdateInterval = 120000; } } } ); t.Start (); } else { StopSelf (); } }
private async Task <int> HandleEvent(DataBaseWrapper db, AsyncNetwork network, User user, Event e) { if (e.type != "onMessage") { return(0); } var chat = db.Get <Chat> (e.msg); if (chat == null) { while (!await network.UpdateChats(db, user)) { await network.Authenticate(db, user); } chat = db.Get <Chat> (e.msg); } chat.time = e.time; db.Update(chat); var msg = new Message(); msg.conversation = chat.conversation; msg.author = e.author; msg.nick = e.nick; msg.text = e.text; msg.time = e.time; db.InsertIfNotContains <Message> (msg); ResetUpdateInterval(); if (e.msg != ActiveConversation && user.user != e.nick) { chat.Marked = true; db.Update(chat); if (chat.Notifications) { await Notify(network, e.nick, e.text); } } if (ChatActivity != null) { ChatActivity.OnUpdateRequested(); } else if (MainActivity != null) { MainActivity.OnUpdateRequested(); } return(0); }
private void ShowChats(User user) { TextView placeholder = FindViewById <TextView> (Resource.Id.placeholder); var x = db.Table <Chat> (); if (x.Count() > 0) { placeholder.Visibility = ViewStates.Gone; } else { placeholder.Visibility = ViewStates.Visible; placeholder.Text = "No chats found."; } LinearLayout chatList = FindViewById <LinearLayout> (Resource.Id.chatList); chatList.RemoveAllViews(); foreach (var elem in x.OrderByDescending(e => e.time)) { View v; if (setting.FontSize == Setting.Size.large) { v = LayoutInflater.Inflate(Resource.Layout.ChatLarge, null); } else { v = LayoutInflater.Inflate(Resource.Layout.Chat, null); } TextView name = v.FindViewById <TextView>(Resource.Id.chatName); TextView message = v.FindViewById <TextView>(Resource.Id.chatMessage); TextView time = v.FindViewById <TextView>(Resource.Id.chatTime); ImageView image = v.FindViewById <ImageView> (Resource.Id.chatImage); new Thread(async() => { try { string conv = elem.conversation; if (conv.Split(',').Length == 2) { if (conv.Split(',')[0] == user.user) { conv = conv.Split(',')[1]; } else { conv = conv.Split(',')[0]; } } var imageBitmap = await network.GetImageBitmapFromUrl(Resources.GetString(Resource.String.profileUrl) + conv + ".png", AsyncNetwork.MINI_PROFILE_SIZE, AsyncNetwork.MINI_PROFILE_SIZE); RunOnUiThread(() => image.SetImageBitmap(imageBitmap)); } catch (Exception e) { Log.Error("BlaChat", e.StackTrace); } }).Start(); name.Text = elem.name; var tmp = db.Table <Message> ().Where(q => q.conversation == elem.conversation).OrderByDescending(q => q.time); var lastMsg = tmp.FirstOrDefault(); if (lastMsg != null) { var escape = lastMsg.text.Replace(""", "\""); escape = escape.Replace("<", "<"); escape = escape.Replace(">", ">"); escape = escape.Replace("&", "&"); if (lastMsg.nick == user.user) { message.Text = "Du: " + escape; } else { if (elem.conversation.Split(',').Length == 2 && lastMsg.nick != "watchdog") { message.Text = escape; } else { message.Text = lastMsg.author + ": " + escape; } } time.Text = TimeConverter.AutoConvert(lastMsg.time); } else { time.Text = ""; message.Text = "No message"; } v.Clickable = true; v.Click += delegate { Finish(); var intent = new Intent(this, typeof(ChatActivity)); intent.PutExtra("conversation", elem.conversation); StartActivity(intent); var chat = db.Get <Chat> (elem.conversation); new Thread(async() => { if (textShare != null) { while (!await network.SendMessage(db, user, chat, "(Shared Text) " + textShare)) { await network.Authenticate(db, user); } } foreach (var file in fileset) { Bitmap img = MediaStore.Images.Media.GetBitmap(ContentResolver, file); while (!await network.SendImage(db, user, chat, img)) { await network.Authenticate(db, user); } } }).Start(); }; chatList.AddView(v); } chatList.Post(() => chatList.RequestLayout()); }
private async Task<int> HandleEvent(DataBaseWrapper db, AsyncNetwork network, User user, Event e) { if (e.type != "onMessage") { return 0; } var chat = db.Get<Chat> (e.msg); if (chat == null) { while(!await network.UpdateChats (db, user)) { await network.Authenticate(db, user); } chat = db.Get<Chat> (e.msg); } chat.time = e.time; db.Update (chat); var msg = new Message (); msg.conversation = chat.conversation; msg.author = e.author; msg.nick = e.nick; msg.text = e.text; msg.time = e.time; db.InsertIfNotContains<Message> (msg); ResetUpdateInterval (); if (e.msg != ActiveConversation && user.user != e.nick) { chat.Marked = true; db.Update (chat); if (chat.Notifications) { await Notify (network, e.nick, e.text); } } if (ChatActivity != null) { ChatActivity.OnUpdateRequested (); } else if (MainActivity != null) { MainActivity.OnUpdateRequested (); } return 0; }
private void initializeNotAuthenticated() { SetContentView(Resource.Layout.Authentication); Button login = FindViewById <Button> (Resource.Id.login); login.Click += async delegate { InputMethodManager manager = (InputMethodManager)GetSystemService(InputMethodService); manager.HideSoftInputFromWindow(FindViewById <EditText> (Resource.Id.username).WindowToken, 0); manager.HideSoftInputFromWindow(FindViewById <EditText> (Resource.Id.password).WindowToken, 0); manager.HideSoftInputFromWindow(FindViewById <EditText> (Resource.Id.server).WindowToken, 0); login.Text = "Connecting..."; login.Enabled = false; var user = new User() { user = FindViewById <EditText> (Resource.Id.username).Text.ToLower(), password = FindViewById <EditText> (Resource.Id.password).Text, server = !string.IsNullOrEmpty(FindViewById <EditText> (Resource.Id.server).Text) ? FindViewById <EditText> (Resource.Id.server).Text : FindViewById <EditText> (Resource.Id.server).Hint }; if (user.user == "" || user.password == "") { login.Text = "Login"; login.Enabled = true; return; } if (await network.Authenticate(db, user)) { login.Text = "Loading data..."; new Thread(async() => { int i = 0; db.Insert(user); while (!await network.UpdateChats(db, user)) { await network.Authenticate(db, user); } var x = db.Table <Chat> (); int count = x.Count(); var tasks = new List <Task <bool> >(); RunOnUiThread(() => { if (isUnbound()) { return; } initializeAuthenticated(user); }); foreach (var chat in x) { while (!await network.UpdateHistory(db, user, chat, 30)) { await network.Authenticate(db, user); } OnUpdateRequested(); } }).Start(); } else { login.Text = "Retry Login"; login.Enabled = true; } }; }
protected override void OnCreate(Bundle bundle) { db = new DataBaseWrapper(this.Resources); if ((setting = db.Table <Setting> ().FirstOrDefault()) == null) { db.Insert(setting = new Setting()); } StartupTheme = setting.Theme; SetTheme(setting.Theme); base.OnCreate(bundle); SetContentView(Resource.Layout.ChatActivity); conversation = Intent.GetStringExtra("conversation"); chat = db.Get <Chat> (conversation); visibleMessages = chat.VisibleMessages; if (visibleMessages <= 0) { visibleMessages = setting.VisibleMessages; } if (visibleMessages <= 0) { visibleMessages = 30; } Title = SmileyTools.GetSmiledTextUTF(chat.name); ActionBar.SetDisplayHomeAsUpEnabled(true); ActionBar.SetIcon(Resource.Drawable.Icon); user = db.Table <User>().FirstOrDefault(); Button send = FindViewById <Button>(Resource.Id.send); send.Click += delegate { TextView message = FindViewById <TextView>(Resource.Id.message); var msg = message.Text; message.Text = ""; if (msg.Equals("")) { return; } LinearLayout messageList = FindViewById <LinearLayout>(Resource.Id.messageLayout); AddMessage(messageList, new Message() { time = "sending", author = "Du", nick = user.user, text = msg, conversation = this.conversation }); ScrollView scrollView = FindViewById <ScrollView>(Resource.Id.messageScrollView); scrollView.FullScroll(FocusSearchDirection.Down); scrollView.Post(() => scrollView.FullScroll(FocusSearchDirection.Down)); OnBind(); new Thread(async() => { while (!await network.SendMessage(db, user, chat, msg)) { await network.Authenticate(db, user); } }).Start(); }; Button smiley = FindViewById <Button>(Resource.Id.smile); smiley.Click += delegate { // TODO implement smiley stuff... }; }
public void DoWork() { db = new DataBaseWrapper(Resources); network = new AsyncNetwork(); network.SetBackgroundService(this); user = db.Table <User>().FirstOrDefault(); var connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService); if (user != null && user.user != null) { var t = new Thread(async() => { while (true) { Setting.Frequency f = db.Table <Setting>().FirstOrDefault().Synchronisation; if (f != Setting.Frequency.wlan || connectivityManager.GetNetworkInfo(ConnectivityType.Wifi).GetState() == NetworkInfo.State.Connected) { while (!await network.UpdateEvents(db, user)) { await network.Authenticate(db, user); } } // Wifi connection gets normal updates, other networks get 4 times worse update time. if (connectivityManager.GetNetworkInfo(ConnectivityType.Wifi).GetState() == NetworkInfo.State.Connected) { connectivityMode = 1; } else { connectivityMode = 4; } switch (f) { case Setting.Frequency.often: Mode = 1; break; case Setting.Frequency.normal: Mode = 2; break; case Setting.Frequency.rare: Mode = 4; break; case Setting.Frequency.wlan: Mode = 2; break; } Thread.Sleep(UpdateInterval * Mode * connectivityMode); if (UpdateInterval < 12000) { UpdateInterval += 1000; } else if (UpdateInterval < 30000) { UpdateInterval += 2000; } else { UpdateInterval = 60000; } } } ); t.Start(); } else { StopSelf(); } }