public bool UpdateOrInsertSender(TsumTsumSender sender)
        {
            try
            {
                using (SQLiteConnection connection = new SQLiteConnection(databasePath))
                {
                    if (ContainsSender(sender.SenderName))
                    {
                        connection.Update(sender);
                    }
                    else
                    {
                        connection.Insert(sender);
                    }

                    Log.Info(TAG, $"Succesfully inserted/replaced {sender} in the database!");

                    return(true);
                }
            }
            catch (SQLiteException exp)
            {
                Log.Error(TAG, $"SQLiteException - UpdateOrInsertSender, Message: {exp.Message}");
                return(false);
            }
        }
Пример #2
0
        private void UpdateHeartView()
        {
            TableLayout tableLayout = FindViewById <TableLayout>(Resource.Id.tableLayout1);

            tableLayout.RemoveAllViews();

            List <TsumTsumSender> tableList;

            int sortingType = Settings.GetInt("sortingType", 0);

            switch (sortingType)
            {
            case 0:     // Alphabetic
                tableList = tsumTsumDatabaseManager.PerformRawQuery <TsumTsumSender>($"SELECT * FROM Senders ORDER BY SenderName COLLATE NOCASE ASC, HeartCount DESC");
                break;

            case 1:     // Heartcount
                tableList = tsumTsumDatabaseManager.PerformRawQuery <TsumTsumSender>($"SELECT * FROM Senders ORDER BY HeartCount DESC, SenderName COLLATE NOCASE ASC");
                break;

            case 2:     // Time (TODO)
                tableList = tsumTsumDatabaseManager.PerformRawQuery <TsumTsumSender>($"SELECT * FROM Senders ORDER BY LastReceiveTimestamp DESC, SenderName COLLATE NOCASE ASC");
                break;

            default:
                throw new NotImplementedException();
            }

            tableLayout.AddView(CreateRow("#", "Name", "Total", "Last Received", true), 0);

            for (int i = 0; i < tableList.Count; i++)
            {
                TsumTsumSender sender = tableList[i];

                TableRow row = CreateRow(
                    (i + 1).ToString(),
                    sender.SenderName,
                    sender.HeartCount.ToString(),
                    string.Format("{0} {1}", sender.LastReceiveTimestamp.ToString("t"), sender.LastReceiveTimestamp.ToString("d"))
                    );


                tableLayout.AddView(row, i + 1);
            }
        }
        public override void OnNotificationPosted(StatusBarNotification sbn)
        {
#if __ANDROID_21__ // In previous versions this function was marked abstract, and thus the base call was not possible
            base.OnNotificationPosted(sbn);
#endif
            Log.Info(TAG, "OnNotificationPosted");

            if (IsTsumTsumNotification(sbn))
            {
                string senderName;
                if (TryGetTsumTsumHeartSender(sbn, out senderName))
                {
                    Log.Info(TAG, $"Succesfully identified sender as \"{senderName}\"");

                    TsumTsumSender sender = tsumTsumDatabaseManager.RetrieveOrCreateSenderByName(senderName);
                    if (sender != null)
                    {
                        sender.HeartCount++;
                        sender.LastReceiveTimestamp = DateTime.Now;
                        tsumTsumDatabaseManager.UpdateOrInsertSender(sender);

                        SendNotification(senderName, () => { Log.Info(TAG, $"Sender: {sender} send!"); }, () => { Log.Info(TAG, $"Sender: {sender} failed!"); });

                        // Send a broadcast, if the application is running it can pick up the broadcast message and update the view
                        Intent intent = new Intent(this, Java.Lang.Class.FromType(typeof(TsumTsumNotifcationBroadcastReceiver))); //new Intent("nl.pleduc.TsumTsumHeartTracker");
                        intent.PutExtra("sender_database_updated", senderName);
                        Android.Support.V4.Content.LocalBroadcastManager.GetInstance(this).SendBroadcast(intent);

                        // If we have more than a set number of TsumTsum notifications, remove the oldest!
                        StatusBarNotification[]      sbns = GetActiveNotifications();
                        List <StatusBarNotification> tsumTsumNotificationList = new List <StatusBarNotification>();
                        for (int i = 0; i < sbns.Length; i++)
                        {
                            if (IsTsumTsumNotification(sbns[i]))
                            {
                                tsumTsumNotificationList.Add(sbns[i]);
                            }
                        }

                        // Delete notifications in a FIFO way
                        Log.Info(TAG, $"Notification count {tsumTsumNotificationList.Count}");
                        if (tsumTsumNotificationList.Count > 5)
                        {
                            tsumTsumNotificationList.Sort((a, b) => { return(b.Notification.When.CompareTo(a.Notification.When)); });
                            //Android.Support.V4.App.NotificationManagerCompat notificationManager = Android.Support.V4.App.NotificationManagerCompat.From(this);

                            Log.Info(TAG, "Canceling Notification(s)");
                            while (tsumTsumNotificationList.Count > 5)
                            {
                                int idxToRemove = tsumTsumNotificationList.Count - 1;
                                StatusBarNotification notificationToRemove = tsumTsumNotificationList[idxToRemove];
                                CancelNotification(notificationToRemove.PackageName, notificationToRemove.Tag, notificationToRemove.Id);
                                //CancelNotification(notificationToRemove.Key);
                                tsumTsumNotificationList.RemoveAt(idxToRemove);
                            }
                        }
                    }
                }
                else
                {
                    Log.Error(TAG, "Unable to extract the sender's name from the notification, possibly another type of TsumTsum notification!");
                }
            }
            else
            {
                Log.Info(TAG, $"Not a {TargetAppName} notification, skipping it!");
            }
        }