2019-08-25 12:08:18 +02:00
package app.fedilab.android.sqlite ;
/ * Copyright 2019 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software ; you can redistribute it and / or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation ; either version 3 of the
* License , or ( at your option ) any later version .
*
* Fedilab is distributed in the hope that it will be useful , but WITHOUT ANY WARRANTY ; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU General
* Public License for more details .
*
* You should have received a copy of the GNU General Public License along with Fedilab ; if not ,
* see < http : //www.gnu.org/licenses>. */
import android.content.ContentValues ;
import android.content.Context ;
import android.content.SharedPreferences ;
import android.database.Cursor ;
import android.database.sqlite.SQLiteDatabase ;
import java.util.ArrayList ;
2019-08-26 18:27:23 +02:00
import java.util.Calendar ;
2019-08-25 12:08:18 +02:00
import java.util.Date ;
2019-08-26 18:27:23 +02:00
import java.util.LinkedHashMap ;
2019-08-25 12:08:18 +02:00
import java.util.List ;
import java.util.concurrent.TimeUnit ;
import app.fedilab.android.client.Entities.Notification ;
2019-08-26 18:27:23 +02:00
import app.fedilab.android.client.Entities.NotificationCharts ;
2019-08-26 17:47:00 +02:00
import app.fedilab.android.client.Entities.StatisticsNotification ;
2019-08-25 12:08:18 +02:00
import app.fedilab.android.client.Entities.Status ;
2019-08-25 17:21:34 +02:00
import app.fedilab.android.helper.FilterNotifications ;
2019-08-25 12:08:18 +02:00
import app.fedilab.android.helper.Helper ;
/ * *
* Created by Thomas on 24 / 28 / 2019 .
* Manage notification in cache
* /
public class NotificationCacheDAO {
public Context context ;
2019-11-15 16:32:25 +01:00
private SQLiteDatabase db ;
2019-08-25 12:08:18 +02:00
public NotificationCacheDAO ( Context context , SQLiteDatabase db ) {
//Creation of the DB with tables
this . context = context ;
this . db = db ;
}
//------- INSERTIONS -------
2019-09-06 17:55:14 +02:00
2019-08-25 12:08:18 +02:00
/ * *
* Insert a status in database
2019-09-06 17:55:14 +02:00
*
2019-08-25 12:08:18 +02:00
* @param notification Notification
2019-09-06 17:55:14 +02:00
* @param userId String
* @param instance String
2019-08-25 12:08:18 +02:00
* @return long
* /
public long insertNotification ( Notification notification , String userId , String instance ) {
ContentValues values = new ContentValues ( ) ;
values . put ( Sqlite . COL_NOTIFICATION_ID , notification . getId ( ) ) ;
values . put ( Sqlite . COL_USER_ID , userId ) ;
values . put ( Sqlite . COL_INSTANCE , instance ) ;
long id = - 1 ;
2019-09-06 17:55:14 +02:00
if ( notification . getStatus ( ) ! = null ) {
2019-08-25 12:08:18 +02:00
values . put ( Sqlite . COL_STATUS_ID , notification . getStatus ( ) . getId ( ) ) ;
//Check if status exists in cache;
Status status = new StatusCacheDAO ( context , db ) . getStatus ( StatusCacheDAO . NOTIFICATION_CACHE , notification . getStatus ( ) . getId ( ) , userId , instance ) ;
2019-09-06 17:55:14 +02:00
if ( status = = null ) {
2019-08-25 12:08:18 +02:00
id = new StatusCacheDAO ( context , db ) . insertStatus ( StatusCacheDAO . NOTIFICATION_CACHE , notification . getStatus ( ) , userId , instance ) ;
2019-09-06 17:55:14 +02:00
} else {
2019-08-25 12:08:18 +02:00
id = status . getDb_id ( ) ;
}
2019-09-06 17:55:14 +02:00
if ( notification . getType ( ) . equals ( " mention " ) ) {
2019-08-27 18:22:34 +02:00
values . put ( Sqlite . COL_IN_REPLY_TO_ID , notification . getStatus ( ) . getIn_reply_to_id ( ) ) ;
}
2019-08-25 12:08:18 +02:00
}
values . put ( Sqlite . COL_STATUS_ID_CACHE , id ) ;
values . put ( Sqlite . COL_ACCOUNT , Helper . accountToStringStorage ( notification . getAccount ( ) ) ) ;
values . put ( Sqlite . COL_CREATED_AT , Helper . dateToString ( notification . getCreated_at ( ) ) ) ;
values . put ( Sqlite . COL_TYPE , notification . getType ( ) ) ;
//Inserts cached status
long last_id ;
2019-09-06 17:55:14 +02:00
try {
2019-08-25 12:08:18 +02:00
last_id = db . insert ( Sqlite . TABLE_NOTIFICATION_CACHE , null , values ) ;
2019-09-06 17:55:14 +02:00
} catch ( Exception e ) {
last_id = - 1 ;
2019-08-25 12:08:18 +02:00
e . printStackTrace ( ) ;
}
return last_id ;
}
2019-08-25 17:21:34 +02:00
/ * *
* Returns all cached notifications in db after filter
2019-09-06 17:55:14 +02:00
*
2019-08-25 17:21:34 +02:00
* @return stored notifications List < Notification >
* /
2019-09-06 17:55:14 +02:00
public List < Notification > getNotificationsFromID ( FilterNotifications filterNotifications , String max_id ) {
2019-08-25 17:21:34 +02:00
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
String userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
String instance = Helper . getLiveInstance ( context ) ;
//That the basic selection for all toots
2019-09-06 17:55:14 +02:00
StringBuilder selection = new StringBuilder ( Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " ) ;
if ( max_id ! = null )
2019-08-25 17:21:34 +02:00
selection . append ( " AND " + Sqlite . COL_NOTIFICATION_ID + " < ' " ) . append ( max_id ) . append ( " ' " ) ;
//BOOST
2019-09-06 17:55:14 +02:00
if ( filterNotifications = = null )
2019-08-25 17:55:07 +02:00
filterNotifications = new FilterNotifications ( ) ;
2019-09-06 17:55:14 +02:00
if ( filterNotifications . isBoost ( ) | | filterNotifications . isFavorite ( ) | | filterNotifications . isFollow ( ) | | filterNotifications . isMention ( ) | | filterNotifications . isPoll ( ) ) {
2019-08-25 17:21:34 +02:00
selection . append ( " AND ( " ) ;
2019-09-06 17:55:14 +02:00
if ( filterNotifications . isBoost ( ) ) {
2019-08-25 18:31:14 +02:00
selection . append ( Sqlite . COL_TYPE + " ='reblog' OR " ) ;
2019-08-25 17:21:34 +02:00
}
2019-09-06 17:55:14 +02:00
if ( filterNotifications . isPoll ( ) ) {
2019-08-25 17:21:34 +02:00
selection . append ( Sqlite . COL_TYPE + " ='poll' OR " ) ;
}
2019-09-06 17:55:14 +02:00
if ( filterNotifications . isFollow ( ) ) {
2019-08-25 17:21:34 +02:00
selection . append ( Sqlite . COL_TYPE + " ='follow' OR " ) ;
}
2019-09-06 17:55:14 +02:00
if ( filterNotifications . isMention ( ) ) {
2019-08-25 17:21:34 +02:00
selection . append ( Sqlite . COL_TYPE + " ='mention' OR " ) ;
}
2019-09-06 17:55:14 +02:00
if ( filterNotifications . isFavorite ( ) ) {
2019-08-25 18:31:14 +02:00
selection . append ( Sqlite . COL_TYPE + " ='favourite' OR " ) ;
2019-08-25 17:21:34 +02:00
}
2019-09-06 17:55:14 +02:00
String selectionStr = selection . toString ( ) . substring ( 0 , selection . toString ( ) . length ( ) - 3 ) ;
2019-08-25 17:21:34 +02:00
selection = new StringBuilder ( selectionStr ) ;
selection . append ( " ) " ) ;
}
2019-09-06 17:55:14 +02:00
if ( filterNotifications . getDateIni ( ) ! = null )
2019-08-25 17:21:34 +02:00
selection . append ( " AND " + Sqlite . COL_CREATED_AT + " >= ' " ) . append ( filterNotifications . getDateIni ( ) ) . append ( " ' " ) ;
2019-09-06 17:55:14 +02:00
if ( filterNotifications . getDateEnd ( ) ! = null )
2019-08-25 17:21:34 +02:00
selection . append ( " AND " + Sqlite . COL_CREATED_AT + " <= ' " ) . append ( filterNotifications . getDateEnd ( ) ) . append ( " ' " ) ;
try {
Cursor c = db . query ( Sqlite . TABLE_NOTIFICATION_CACHE , null , selection . toString ( ) , null , null , null , Sqlite . COL_CREATED_AT + " DESC " , " 40 " ) ;
return cursorToListNotifications ( c ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
return null ;
}
}
2019-08-25 12:08:18 +02:00
//------- INSERTIONS -------
2019-09-06 17:55:14 +02:00
2019-08-25 12:08:18 +02:00
/ * *
* Insert a status in database
2019-09-06 17:55:14 +02:00
*
2019-08-25 12:08:18 +02:00
* @param notification Notification
* @return long
* /
public long insertNotification ( Notification notification ) {
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
String userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
String instance = Helper . getLiveInstance ( context ) ;
ContentValues values = new ContentValues ( ) ;
values . put ( Sqlite . COL_NOTIFICATION_ID , notification . getId ( ) ) ;
values . put ( Sqlite . COL_USER_ID , userId ) ;
values . put ( Sqlite . COL_INSTANCE , instance ) ;
long id = - 1 ;
2019-09-06 17:55:14 +02:00
if ( notification . getStatus ( ) ! = null ) {
2019-08-25 12:08:18 +02:00
values . put ( Sqlite . COL_STATUS_ID , notification . getStatus ( ) . getId ( ) ) ;
//Check if status exists in cache;
Status status = new StatusCacheDAO ( context , db ) . getStatus ( StatusCacheDAO . NOTIFICATION_CACHE , notification . getStatus ( ) . getId ( ) , userId , instance ) ;
2019-09-06 17:55:14 +02:00
if ( status = = null ) {
2019-08-25 12:08:18 +02:00
id = new StatusCacheDAO ( context , db ) . insertStatus ( StatusCacheDAO . NOTIFICATION_CACHE , notification . getStatus ( ) , userId , instance ) ;
2019-09-06 17:55:14 +02:00
} else {
2019-08-25 12:08:18 +02:00
id = status . getDb_id ( ) ;
}
2019-09-06 17:55:14 +02:00
if ( notification . getType ( ) . equals ( " mention " ) ) {
2019-08-27 18:22:34 +02:00
values . put ( Sqlite . COL_IN_REPLY_TO_ID , notification . getStatus ( ) . getIn_reply_to_id ( ) ) ;
}
2019-08-25 12:08:18 +02:00
}
values . put ( Sqlite . COL_STATUS_ID_CACHE , id ) ;
values . put ( Sqlite . COL_ACCOUNT , Helper . accountToStringStorage ( notification . getAccount ( ) ) ) ;
values . put ( Sqlite . COL_CREATED_AT , Helper . dateToString ( notification . getCreated_at ( ) ) ) ;
values . put ( Sqlite . COL_TYPE , notification . getType ( ) ) ;
//Inserts cached notification
long last_id ;
2019-09-06 17:55:14 +02:00
try {
2019-08-25 12:08:18 +02:00
last_id = db . insert ( Sqlite . TABLE_NOTIFICATION_CACHE , null , values ) ;
2019-09-06 17:55:14 +02:00
} catch ( Exception e ) {
last_id = - 1 ;
2019-08-25 12:08:18 +02:00
e . printStackTrace ( ) ;
}
return last_id ;
}
//------- REMOVE -------
/ * * *
* Remove stored notification
* @return int
* /
2019-09-06 17:55:14 +02:00
public int remove ( Notification notification ) {
2019-08-25 12:08:18 +02:00
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
String userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
String instance = Helper . getLiveInstance ( context ) ;
2019-09-06 17:55:14 +02:00
return db . delete ( Sqlite . TABLE_NOTIFICATION_CACHE , Sqlite . COL_NOTIFICATION_ID + " = \" " + notification . getId ( ) + " \" AND " + Sqlite . COL_INSTANCE + " = \" " + instance + " \" AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " , null ) ;
2019-08-25 12:08:18 +02:00
}
2019-09-06 17:55:14 +02:00
public void removeDuplicate ( ) {
db . execSQL ( " DELETE FROM " + Sqlite . TABLE_NOTIFICATION_CACHE + " WHERE " + Sqlite . COL_ID + " NOT IN (SELECT MIN( " + Sqlite . COL_ID + " ) FROM " + Sqlite . TABLE_NOTIFICATION_CACHE + " GROUP BY " + Sqlite . COL_NOTIFICATION_ID + " , " + Sqlite . COL_INSTANCE + " ) " ) ;
2019-08-25 12:08:18 +02:00
}
/ * * *
* Remove stored notifications
* @return int
* /
2019-09-06 17:55:14 +02:00
public int remove ( Notification notification , String userId , String instance ) {
return db . delete ( Sqlite . TABLE_NOTIFICATION_CACHE , Sqlite . COL_NOTIFICATION_ID + " = \" " + notification . getId ( ) + " \" AND " + Sqlite . COL_INSTANCE + " = \" " + instance + " \" AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " , null ) ;
2019-08-25 12:08:18 +02:00
}
2019-09-06 17:55:14 +02:00
public int removeAllNotification ( ) {
2019-08-25 12:08:18 +02:00
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
String userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
String instance = Helper . getLiveInstance ( context ) ;
2019-09-06 17:55:14 +02:00
return db . delete ( Sqlite . TABLE_NOTIFICATION_CACHE , Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " , null ) ;
2019-08-25 12:08:18 +02:00
}
2019-09-06 17:55:14 +02:00
public int removeAll ( ) {
return db . delete ( Sqlite . TABLE_NOTIFICATION_CACHE , null , null ) ;
2019-08-25 12:08:18 +02:00
}
//------- GETTERS -------
/ * *
* Returns all cached Notification in db
2019-09-06 17:55:14 +02:00
*
2019-08-25 12:08:18 +02:00
* @return stored notifications List < Notification >
* /
2019-09-06 17:55:14 +02:00
public List < Notification > getAllNotifications ( ) {
2019-08-25 12:08:18 +02:00
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
String userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
String instance = Helper . getLiveInstance ( context ) ;
try {
2019-09-06 17:55:14 +02:00
Cursor c = db . query ( Sqlite . TABLE_NOTIFICATION_CACHE , null , Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " , null , null , null , Sqlite . COL_CREATED_AT + " DESC " , null ) ;
2019-08-25 12:08:18 +02:00
return cursorToListNotifications ( c ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
return null ;
}
}
/ * *
* Returns all cached Notification in db
2019-09-06 17:55:14 +02:00
*
2019-08-25 12:08:18 +02:00
* @return stored notification List < String >
* /
2019-09-06 17:55:14 +02:00
public List < String > getAllNotificationsId ( ) {
2019-08-25 12:08:18 +02:00
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
String userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
String instance = Helper . getLiveInstance ( context ) ;
try {
2019-09-06 17:55:14 +02:00
Cursor c = db . query ( Sqlite . TABLE_NOTIFICATION_CACHE , new String [ ] { Sqlite . COL_NOTIFICATION_ID } , Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " , null , null , null , Sqlite . COL_CREATED_AT + " DESC " , null ) ;
2019-08-25 12:08:18 +02:00
return cursorToListNotificationsId ( c ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
return null ;
}
}
/ * *
* Returns the smaller date
2019-09-06 17:55:14 +02:00
*
2019-08-25 12:08:18 +02:00
* @return Date
* /
2019-09-06 17:55:14 +02:00
public Date getSmallerDate ( ) {
2019-08-25 12:08:18 +02:00
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
String userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
String instance = Helper . getLiveInstance ( context ) ;
try {
2019-09-06 17:55:14 +02:00
Cursor c = db . query ( Sqlite . TABLE_NOTIFICATION_CACHE , null , Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " , null , null , null , Sqlite . COL_CREATED_AT + " ASC " , " 1 " ) ;
2019-08-25 12:08:18 +02:00
//No element found
if ( c . getCount ( ) = = 0 ) {
c . close ( ) ;
return null ;
}
//Take the first element
c . moveToFirst ( ) ;
String date = c . getString ( c . getColumnIndex ( Sqlite . COL_CREATED_AT ) ) ;
c . close ( ) ;
return Helper . stringToDate ( context , date ) ;
} catch ( Exception e ) {
return null ;
}
}
/ * *
* Returns the smaller date
2019-09-06 17:55:14 +02:00
*
2019-08-25 12:08:18 +02:00
* @return Date
* /
2019-09-06 17:55:14 +02:00
public Date getGreaterDate ( ) {
2019-08-25 12:08:18 +02:00
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
String userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
String instance = Helper . getLiveInstance ( context ) ;
try {
2019-09-06 17:55:14 +02:00
Cursor c = db . query ( Sqlite . TABLE_NOTIFICATION_CACHE , null , Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " , null , null , null , Sqlite . COL_CREATED_AT + " DESC " , " 1 " ) ;
2019-08-25 12:08:18 +02:00
//No element found
if ( c . getCount ( ) = = 0 ) {
c . close ( ) ;
return null ;
}
//Take the first element
c . moveToFirst ( ) ;
String date = c . getString ( c . getColumnIndex ( Sqlite . COL_CREATED_AT ) ) ;
c . close ( ) ;
return Helper . stringToDate ( context , date ) ;
} catch ( Exception e ) {
return null ;
}
}
/ * *
* Returns the last id of backup for a use
2019-09-06 17:55:14 +02:00
*
2019-08-25 12:08:18 +02:00
* @return Date
* /
2019-09-06 17:55:14 +02:00
public String getLastNotificationIDCache ( String userId , String instance ) {
2019-08-25 12:08:18 +02:00
try {
2019-09-06 17:55:14 +02:00
Cursor c = db . query ( Sqlite . TABLE_NOTIFICATION_CACHE , null , Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " , null , null , null , Sqlite . COL_NOTIFICATION_ID + " DESC " , " 1 " ) ;
2019-08-25 12:08:18 +02:00
//No element found
if ( c . getCount ( ) = = 0 ) {
c . close ( ) ;
return null ;
}
//Take the first element
c . moveToFirst ( ) ;
String last_id = c . getString ( c . getColumnIndex ( Sqlite . COL_NOTIFICATION_ID ) ) ;
c . close ( ) ;
return last_id ;
} catch ( Exception e ) {
return null ;
}
}
/ * *
* Returns the last date of backup for a user
2019-09-06 17:55:14 +02:00
*
2019-08-25 12:08:18 +02:00
* @return Date
* /
2019-09-06 17:55:14 +02:00
public Date getLastNotificationDateCache ( String userId , String instance ) {
2019-08-25 12:08:18 +02:00
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
2019-09-06 17:55:14 +02:00
if ( userId = = null | | instance = = null ) {
2019-08-25 12:08:18 +02:00
userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
instance = Helper . getLiveInstance ( context ) ;
}
try {
2019-09-06 17:55:14 +02:00
Cursor c = db . query ( Sqlite . TABLE_NOTIFICATION_CACHE , null , Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " , null , null , null , Sqlite . COL_CREATED_AT + " DESC " , " 1 " ) ;
2019-08-25 12:08:18 +02:00
//No element found
if ( c . getCount ( ) = = 0 ) {
c . close ( ) ;
return null ;
}
//Take the first element
c . moveToFirst ( ) ;
Date last_id = Helper . stringToDate ( context , c . getString ( c . getColumnIndex ( Sqlite . COL_CREATED_AT ) ) ) ;
c . close ( ) ;
return last_id ;
} catch ( Exception e ) {
return null ;
}
}
/ * *
* Returns a cached notification by id in db
2019-09-06 17:55:14 +02:00
*
2019-08-25 12:08:18 +02:00
* @return Notification
* /
2019-09-06 17:55:14 +02:00
public Notification getNotification ( String id ) {
2019-08-25 12:08:18 +02:00
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
String userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
String instance = Helper . getLiveInstance ( context ) ;
try {
2019-09-06 17:55:14 +02:00
Cursor c = db . query ( Sqlite . TABLE_NOTIFICATION_CACHE , null , Sqlite . COL_NOTIFICATION_ID + " = ' " + id + " ' AND " + Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " , null , null , null , null , null ) ;
2019-08-25 12:08:18 +02:00
return cursorToStoredNotification ( c ) ;
} catch ( Exception e ) {
return null ;
}
}
/ * *
* Returns a cached notification by id in db
2019-09-06 17:55:14 +02:00
*
2019-08-25 12:08:18 +02:00
* @return Notification
* /
2019-09-06 17:55:14 +02:00
public Notification getNotification ( int cacheType , String id , String userId , String instance ) {
2019-08-25 12:08:18 +02:00
try {
2019-09-06 17:55:14 +02:00
Cursor c = db . query ( Sqlite . TABLE_NOTIFICATION_CACHE , null , Sqlite . COL_NOTIFICATION_ID + " = ' " + id + " ' AND " + Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " , null , null , null , null , null ) ;
2019-08-25 12:08:18 +02:00
return cursorToStoredNotification ( c ) ;
} catch ( Exception e ) {
return null ;
}
}
2019-09-06 17:55:14 +02:00
public StatisticsNotification getStat ( ) {
2019-08-26 17:47:00 +02:00
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
String userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
String instance = Helper . getLiveInstance ( context ) ;
StatisticsNotification statistics = new StatisticsNotification ( ) ;
//Count All
Cursor mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite . COL_USER_ID + " = ' " + userId + " ' AND " + Sqlite . COL_INSTANCE + " = ' " + instance + " ' "
2019-08-26 17:47:00 +02:00
, null ) ;
mCount . moveToFirst ( ) ;
statistics . setTotal_notification ( mCount . getInt ( 0 ) ) ;
mCount . close ( ) ;
//Count boosts
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite . COL_USER_ID + " = ' " + userId + " ' AND " + Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND "
2019-08-26 17:47:00 +02:00
+ Sqlite . COL_TYPE + " = 'reblog' "
, null ) ;
mCount . moveToFirst ( ) ;
statistics . setNumber_reblog ( mCount . getInt ( 0 ) ) ;
mCount . close ( ) ;
//Count favorites
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite . COL_USER_ID + " = ' " + userId + " ' AND " + Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND "
2019-08-26 17:47:00 +02:00
+ Sqlite . COL_TYPE + " = 'favourite' "
, null ) ;
mCount . moveToFirst ( ) ;
statistics . setNumber_favourite ( mCount . getInt ( 0 ) ) ;
mCount . close ( ) ;
//Count mentions
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite . COL_USER_ID + " = ' " + userId + " ' AND " + Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND "
2019-08-26 17:47:00 +02:00
+ Sqlite . COL_TYPE + " = 'mention' "
, null ) ;
mCount . moveToFirst ( ) ;
statistics . setNumber_mentions ( mCount . getInt ( 0 ) ) ;
mCount . close ( ) ;
//Count follows
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite . COL_USER_ID + " = ' " + userId + " ' AND " + Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND "
2019-08-26 17:47:00 +02:00
+ Sqlite . COL_TYPE + " = 'follow' "
, null ) ;
mCount . moveToFirst ( ) ;
statistics . setNumber_follow ( mCount . getInt ( 0 ) ) ;
mCount . close ( ) ;
//Count polls
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite . COL_USER_ID + " = ' " + userId + " ' AND " + Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND "
2019-08-26 17:47:00 +02:00
+ Sqlite . COL_TYPE + " = 'poll' "
, null ) ;
mCount . moveToFirst ( ) ;
statistics . setNumber_poll ( mCount . getInt ( 0 ) ) ;
mCount . close ( ) ;
statistics . setFirstTootDate ( getSmallerDate ( ) ) ;
statistics . setLastTootDate ( getGreaterDate ( ) ) ;
long days = 1 ;
2019-09-06 17:55:14 +02:00
if ( statistics . getLastTootDate ( ) ! = null & & statistics . getFirstTootDate ( ) ! = null ) {
2019-08-26 17:47:00 +02:00
long diff = statistics . getLastTootDate ( ) . getTime ( ) - statistics . getFirstTootDate ( ) . getTime ( ) ;
days = TimeUnit . DAYS . convert ( diff , TimeUnit . MILLISECONDS ) ;
}
2019-09-06 17:55:14 +02:00
statistics . setFrequency ( ( float ) statistics . getTotal_notification ( ) / days ) ;
2019-08-26 17:47:00 +02:00
return statistics ;
}
2019-08-26 18:27:23 +02:00
2019-09-06 17:55:14 +02:00
public NotificationCharts getCharts ( Date dateIni , Date dateEnd ) {
2019-08-26 18:27:23 +02:00
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
String userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
String instance = Helper . getLiveInstance ( context ) ;
NotificationCharts charts = new NotificationCharts ( ) ;
Calendar start = Calendar . getInstance ( ) ;
start . setTime ( dateIni ) ;
2019-09-06 17:55:14 +02:00
start . set ( Calendar . HOUR_OF_DAY , 0 ) ;
start . set ( Calendar . MINUTE , 0 ) ;
start . set ( Calendar . SECOND , 0 ) ;
2019-08-26 18:27:23 +02:00
Calendar end = Calendar . getInstance ( ) ;
end . setTime ( dateEnd ) ;
2019-09-06 17:55:14 +02:00
end . set ( Calendar . HOUR_OF_DAY , 23 ) ;
end . set ( Calendar . MINUTE , 59 ) ;
end . set ( Calendar . SECOND , 59 ) ;
2019-08-26 18:27:23 +02:00
StringBuilder selection = new StringBuilder ( Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " ) ;
selection . append ( " AND " + Sqlite . COL_CREATED_AT + " >= ' " ) . append ( Helper . dateToString ( start . getTime ( ) ) ) . append ( " ' " ) ;
selection . append ( " AND " + Sqlite . COL_CREATED_AT + " <= ' " ) . append ( Helper . dateToString ( end . getTime ( ) ) ) . append ( " ' " ) ;
List < Notification > data = new ArrayList < > ( ) ;
try {
Cursor c = db . query ( Sqlite . TABLE_NOTIFICATION_CACHE , null , selection . toString ( ) , null , null , null , Sqlite . COL_CREATED_AT + " ASC " ) ;
data = cursorToListNotifications ( c ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
List < String > xLabel = new ArrayList < > ( ) ;
charts . setFavourites ( new LinkedHashMap < > ( ) ) ;
charts . setFollows ( new LinkedHashMap < > ( ) ) ;
charts . setMentions ( new LinkedHashMap < > ( ) ) ;
charts . setPolls ( new LinkedHashMap < > ( ) ) ;
charts . setReblogs ( new LinkedHashMap < > ( ) ) ;
2019-09-06 17:55:14 +02:00
if ( data ! = null ) {
2019-08-26 18:27:23 +02:00
for ( Notification notification : data ) {
Calendar tempdate = Calendar . getInstance ( ) ;
tempdate . setTime ( notification . getCreated_at ( ) ) ;
tempdate . set ( Calendar . HOUR_OF_DAY , 0 ) ;
tempdate . set ( Calendar . MINUTE , 0 ) ;
tempdate . set ( Calendar . SECOND , 0 ) ;
long date = tempdate . getTimeInMillis ( ) ;
if ( notification . getType ( ) . equals ( " reblog " ) ) {
2019-09-06 17:55:14 +02:00
if ( charts . getReblogs ( ) . containsKey ( date ) ) {
charts . getReblogs ( ) . put ( date , ( charts . getReblogs ( ) . get ( date ) + 1 ) ) ;
} else {
charts . getReblogs ( ) . put ( date , 1 ) ;
2019-08-26 18:27:23 +02:00
}
} else if ( notification . getType ( ) . equals ( " favourite " ) ) {
2019-09-06 17:55:14 +02:00
if ( charts . getFavourites ( ) . containsKey ( date ) ) {
charts . getFavourites ( ) . put ( date , ( charts . getFavourites ( ) . get ( date ) + 1 ) ) ;
} else {
charts . getFavourites ( ) . put ( date , 1 ) ;
2019-08-26 18:27:23 +02:00
}
} else if ( notification . getType ( ) . equals ( " follow " ) ) {
2019-09-06 17:55:14 +02:00
if ( charts . getFollows ( ) . containsKey ( date ) ) {
charts . getFollows ( ) . put ( date , ( charts . getFollows ( ) . get ( date ) + 1 ) ) ;
} else {
charts . getFollows ( ) . put ( date , 1 ) ;
2019-08-26 18:27:23 +02:00
}
} else if ( notification . getType ( ) . equals ( " mention " ) ) {
2019-09-06 17:55:14 +02:00
if ( charts . getMentions ( ) . containsKey ( date ) ) {
charts . getMentions ( ) . put ( date , ( charts . getMentions ( ) . get ( date ) + 1 ) ) ;
} else {
charts . getMentions ( ) . put ( date , 1 ) ;
2019-08-26 18:27:23 +02:00
}
2019-09-06 17:55:14 +02:00
} else if ( notification . getType ( ) . equals ( " poll " ) ) {
if ( charts . getPolls ( ) . containsKey ( date ) ) {
charts . getPolls ( ) . put ( date , ( charts . getPolls ( ) . get ( date ) + 1 ) ) ;
} else {
charts . getPolls ( ) . put ( date , 1 ) ;
2019-08-26 18:27:23 +02:00
}
}
}
}
charts . setxLabels ( xLabel ) ;
return charts ;
}
2019-09-06 17:55:14 +02:00
public NotificationCharts getChartsEvolution ( String status_id , Date dateIni , Date dateEnd ) {
2019-08-27 11:02:47 +02:00
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
String userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
String instance = Helper . getLiveInstance ( context ) ;
NotificationCharts charts = new NotificationCharts ( ) ;
Calendar start = Calendar . getInstance ( ) ;
Calendar end = Calendar . getInstance ( ) ;
2019-09-06 17:55:14 +02:00
if ( status_id = = null ) {
2019-08-27 11:43:20 +02:00
start . setTime ( dateIni ) ;
2019-09-06 17:55:14 +02:00
start . set ( Calendar . HOUR_OF_DAY , 0 ) ;
start . set ( Calendar . MINUTE , 0 ) ;
start . set ( Calendar . SECOND , 0 ) ;
2019-08-27 11:43:20 +02:00
end . setTime ( dateEnd ) ;
end . set ( Calendar . HOUR_OF_DAY , 23 ) ;
end . set ( Calendar . MINUTE , 59 ) ;
end . set ( Calendar . SECOND , 59 ) ;
2019-09-06 17:55:14 +02:00
} else {
2019-08-27 18:22:34 +02:00
start . setTime ( dateIni ) ;
2019-08-27 11:43:20 +02:00
2019-08-27 18:22:34 +02:00
end . setTime ( dateIni ) ;
end . add ( Calendar . HOUR , 24 ) ;
2019-08-27 11:43:20 +02:00
}
2019-08-27 11:02:47 +02:00
long msDiff = end . getTimeInMillis ( ) - start . getTimeInMillis ( ) ;
long daysDiff = TimeUnit . MILLISECONDS . toDays ( msDiff ) ;
List < String > xLabel = new ArrayList < > ( ) ;
charts . setFavourites ( new LinkedHashMap < > ( ) ) ;
charts . setFollows ( new LinkedHashMap < > ( ) ) ;
charts . setMentions ( new LinkedHashMap < > ( ) ) ;
charts . setPolls ( new LinkedHashMap < > ( ) ) ;
charts . setReblogs ( new LinkedHashMap < > ( ) ) ;
int reblogCount = 0 ;
int favCount = 0 ;
2019-09-06 17:55:14 +02:00
// int pollCount = 0;
2019-08-27 11:02:47 +02:00
int followCount = 0 ;
int mentionCount = 0 ;
Date smallestDate = getSmallerDate ( ) ;
int minYVal = 0 ;
2019-09-06 17:55:14 +02:00
if ( status_id = = null ) {
2019-08-27 18:22:34 +02:00
StringBuilder selection = new StringBuilder ( Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " ) ;
selection . append ( " AND " + Sqlite . COL_CREATED_AT + " >= ' " ) . append ( Helper . dateToString ( smallestDate ) ) . append ( " ' " ) ;
selection . append ( " AND " + Sqlite . COL_CREATED_AT + " <= ' " ) . append ( Helper . dateToString ( start . getTime ( ) ) ) . append ( " ' " ) ;
2019-08-27 11:02:47 +02:00
try {
Cursor mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'reblog' "
, null ) ;
mCount . moveToFirst ( ) ;
2019-08-27 18:22:34 +02:00
reblogCount = mCount . getInt ( 0 ) ;
2019-08-27 11:02:47 +02:00
mCount . close ( ) ;
2019-08-27 18:22:34 +02:00
minYVal = reblogCount ;
2019-08-27 11:02:47 +02:00
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'favourite' "
, null ) ;
mCount . moveToFirst ( ) ;
2019-08-27 18:22:34 +02:00
favCount = mCount . getInt ( 0 ) ;
2019-08-27 11:02:47 +02:00
mCount . close ( ) ;
2019-08-27 18:22:34 +02:00
if ( favCount < minYVal ) {
minYVal = favCount ;
}
2019-08-27 11:02:47 +02:00
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'mention' "
, null ) ;
mCount . moveToFirst ( ) ;
2019-08-27 18:22:34 +02:00
mentionCount = mCount . getInt ( 0 ) ;
2019-08-27 11:02:47 +02:00
mCount . close ( ) ;
2019-08-27 18:22:34 +02:00
if ( mentionCount < minYVal ) {
minYVal = mentionCount ;
}
2019-08-27 11:02:47 +02:00
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'follow' "
, null ) ;
mCount . moveToFirst ( ) ;
2019-08-27 18:22:34 +02:00
mCount . moveToFirst ( ) ;
followCount = mCount . getInt ( 0 ) ;
2019-08-27 11:02:47 +02:00
mCount . close ( ) ;
2019-08-27 18:22:34 +02:00
if ( followCount < minYVal ) {
minYVal = followCount ;
}
/ * mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'poll' "
, null ) ;
mCount . moveToFirst ( ) ;
mCount . moveToFirst ( ) ;
pollCount = mCount . getInt ( 0 ) ;
mCount . close ( ) ;
if ( pollCount < minYVal ) {
minYVal = pollCount ;
} * /
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
for ( Date date = start . getTime ( ) ; start . before ( end ) ; start . add ( Calendar . DATE , 1 ) , date = start . getTime ( ) ) {
Calendar startTmp = Calendar . getInstance ( ) ;
startTmp . setTime ( date ) ;
2019-09-06 17:55:14 +02:00
startTmp . set ( Calendar . HOUR_OF_DAY , 0 ) ;
startTmp . set ( Calendar . MINUTE , 0 ) ;
startTmp . set ( Calendar . SECOND , 0 ) ;
2019-08-27 18:22:34 +02:00
Calendar endTmp = Calendar . getInstance ( ) ;
endTmp . setTime ( date ) ;
2019-09-06 17:55:14 +02:00
endTmp . set ( Calendar . HOUR_OF_DAY , 23 ) ;
endTmp . set ( Calendar . MINUTE , 59 ) ;
endTmp . set ( Calendar . SECOND , 59 ) ;
2019-08-27 18:22:34 +02:00
selection = new StringBuilder ( Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " ) ;
selection . append ( " AND " + Sqlite . COL_CREATED_AT + " >= ' " ) . append ( Helper . dateToString ( startTmp . getTime ( ) ) ) . append ( " ' " ) ;
selection . append ( " AND " + Sqlite . COL_CREATED_AT + " <= ' " ) . append ( Helper . dateToString ( endTmp . getTime ( ) ) ) . append ( " ' " ) ;
try {
Cursor mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'reblog' "
, null ) ;
mCount . moveToFirst ( ) ;
reblogCount + = mCount . getInt ( 0 ) ;
charts . getReblogs ( ) . put ( date . getTime ( ) , reblogCount ) ;
mCount . close ( ) ;
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'favourite' "
, null ) ;
mCount . moveToFirst ( ) ;
favCount + = mCount . getInt ( 0 ) ;
2019-09-06 17:55:14 +02:00
charts . getFavourites ( ) . put ( date . getTime ( ) , favCount ) ;
2019-08-27 18:22:34 +02:00
mCount . close ( ) ;
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'mention' "
, null ) ;
mCount . moveToFirst ( ) ;
mentionCount + = mCount . getInt ( 0 ) ;
charts . getMentions ( ) . put ( date . getTime ( ) , mentionCount ) ;
mCount . close ( ) ;
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'follow' "
, null ) ;
mCount . moveToFirst ( ) ;
followCount + = mCount . getInt ( 0 ) ;
2019-09-06 17:55:14 +02:00
charts . getFollows ( ) . put ( date . getTime ( ) , followCount ) ;
2019-08-27 18:22:34 +02:00
mCount . close ( ) ;
2019-08-27 11:02:47 +02:00
/ * mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'poll' "
, null ) ;
mCount . moveToFirst ( ) ;
pollCount + = mCount . getInt ( 0 ) ;
charts . getPolls ( ) . put ( date . getTime ( ) , pollCount ) ;
mCount . close ( ) ; * /
2019-08-27 18:22:34 +02:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
2019-09-06 17:55:14 +02:00
} else {
2019-08-27 18:22:34 +02:00
start . add ( Calendar . HOUR , - 1 ) ;
charts . getReblogs ( ) . put ( start . getTimeInMillis ( ) , 0 ) ;
charts . getFavourites ( ) . put ( start . getTimeInMillis ( ) , 0 ) ;
charts . getMentions ( ) . put ( start . getTimeInMillis ( ) , 0 ) ;
charts . getFollows ( ) . put ( start . getTimeInMillis ( ) , 0 ) ;
start . add ( Calendar . HOUR , 1 ) ;
for ( Date date = start . getTime ( ) ; start . before ( end ) ; start . add ( Calendar . HOUR , 1 ) , date = start . getTime ( ) ) {
Calendar startTmp = Calendar . getInstance ( ) ;
startTmp . setTime ( date ) ;
Calendar endTmp = Calendar . getInstance ( ) ;
endTmp . setTime ( date ) ;
endTmp . add ( Calendar . MINUTE , 59 ) ;
endTmp . add ( Calendar . SECOND , 59 ) ;
StringBuilder selection = new StringBuilder ( Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " ) ;
selection . append ( " AND " + Sqlite . COL_CREATED_AT + " >= ' " ) . append ( Helper . dateToString ( startTmp . getTime ( ) ) ) . append ( " ' " ) ;
selection . append ( " AND " + Sqlite . COL_CREATED_AT + " <= ' " ) . append ( Helper . dateToString ( endTmp . getTime ( ) ) ) . append ( " ' " ) ;
2019-09-06 17:55:14 +02:00
selection . append ( " AND ( " + Sqlite . COL_STATUS_ID + " = ' " ) . append ( status_id ) . append ( " ' " ) . append ( " OR " ) ;
selection . append ( Sqlite . COL_IN_REPLY_TO_ID + " = ' " ) . append ( status_id ) . append ( " ' " ) . append ( " ) " ) ;
2019-08-27 18:22:34 +02:00
try {
Cursor mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'reblog' "
, null ) ;
mCount . moveToFirst ( ) ;
reblogCount + = mCount . getInt ( 0 ) ;
charts . getReblogs ( ) . put ( date . getTime ( ) , reblogCount ) ;
mCount . close ( ) ;
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'favourite' "
, null ) ;
mCount . moveToFirst ( ) ;
favCount + = mCount . getInt ( 0 ) ;
2019-09-06 17:55:14 +02:00
charts . getFavourites ( ) . put ( date . getTime ( ) , favCount ) ;
2019-08-27 18:22:34 +02:00
mCount . close ( ) ;
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'mention' "
, null ) ;
mCount . moveToFirst ( ) ;
mentionCount + = mCount . getInt ( 0 ) ;
charts . getMentions ( ) . put ( date . getTime ( ) , mentionCount ) ;
mCount . close ( ) ;
StringBuilder selectionFollow = new StringBuilder ( Sqlite . COL_INSTANCE + " = ' " + instance + " ' AND " + Sqlite . COL_USER_ID + " = ' " + userId + " ' " ) ;
selectionFollow . append ( " AND " + Sqlite . COL_CREATED_AT + " >= ' " ) . append ( Helper . dateToString ( startTmp . getTime ( ) ) ) . append ( " ' " ) ;
selectionFollow . append ( " AND " + Sqlite . COL_CREATED_AT + " <= ' " ) . append ( Helper . dateToString ( endTmp . getTime ( ) ) ) . append ( " ' " ) ;
mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selectionFollow . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'follow' "
, null ) ;
mCount . moveToFirst ( ) ;
followCount + = mCount . getInt ( 0 ) ;
2019-09-06 17:55:14 +02:00
charts . getFollows ( ) . put ( date . getTime ( ) , followCount ) ;
2019-08-27 18:22:34 +02:00
mCount . close ( ) ;
/ * mCount = db . rawQuery ( " select count(*) from " + Sqlite . TABLE_NOTIFICATION_CACHE
+ " where " + selection . toString ( ) + " AND "
+ Sqlite . COL_TYPE + " = 'poll' "
, null ) ;
mCount . moveToFirst ( ) ;
pollCount + = mCount . getInt ( 0 ) ;
charts . getPolls ( ) . put ( date . getTime ( ) , pollCount ) ;
mCount . close ( ) ; * /
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2019-08-27 11:02:47 +02:00
}
}
2019-08-27 18:22:34 +02:00
2019-08-27 11:02:47 +02:00
charts . setMinYVal ( minYVal ) ;
charts . setxLabels ( xLabel ) ;
return charts ;
}
2019-08-25 12:08:18 +02:00
/ * * *
* Method to hydrate notification from database
* @param c Cursor
* @return Notification
* /
2019-09-06 17:55:14 +02:00
private Notification cursorToStoredNotification ( Cursor c ) {
2019-08-25 12:08:18 +02:00
//No element found
if ( c . getCount ( ) = = 0 ) {
c . close ( ) ;
return null ;
}
//Take the first element
c . moveToFirst ( ) ;
//New status
Notification notification = new Notification ( ) ;
notification . setId ( c . getString ( c . getColumnIndex ( Sqlite . COL_NOTIFICATION_ID ) ) ) ;
notification . setAccount ( Helper . restoreAccountFromString ( c . getString ( c . getColumnIndex ( Sqlite . COL_ACCOUNT ) ) ) ) ;
notification . setCreated_at ( Helper . stringToDate ( context , c . getString ( c . getColumnIndex ( Sqlite . COL_CREATED_AT ) ) ) ) ;
notification . setType ( c . getString ( c . getColumnIndex ( Sqlite . COL_TYPE ) ) ) ;
2019-09-06 17:55:14 +02:00
if ( c . getString ( c . getColumnIndex ( Sqlite . COL_STATUS_ID ) ) ! = null ) {
2019-08-25 12:08:18 +02:00
String status_id = c . getString ( c . getColumnIndex ( Sqlite . COL_STATUS_ID ) ) ;
2019-09-06 17:55:14 +02:00
Status status = new StatusCacheDAO ( context , db ) . getStatus ( StatusCacheDAO . NOTIFICATION_CACHE , status_id ) ;
2019-08-25 12:08:18 +02:00
notification . setStatus ( status ) ;
}
//Close the cursor
c . close ( ) ;
//Cached notification is returned
return notification ;
}
/ * * *
* Method to hydrate cached notification from database
* @param c Cursor
* @return List < Notification >
* /
2019-09-06 17:55:14 +02:00
private List < Notification > cursorToListNotifications ( Cursor c ) {
2019-08-25 12:08:18 +02:00
//No element found
if ( c . getCount ( ) = = 0 ) {
c . close ( ) ;
return null ;
}
List < Notification > notifications = new ArrayList < > ( ) ;
2019-09-06 17:55:14 +02:00
while ( c . moveToNext ( ) ) {
2019-08-25 12:08:18 +02:00
//Restore cached notification
Notification notification = new Notification ( ) ;
notification . setId ( c . getString ( c . getColumnIndex ( Sqlite . COL_NOTIFICATION_ID ) ) ) ;
notification . setAccount ( Helper . restoreAccountFromString ( c . getString ( c . getColumnIndex ( Sqlite . COL_ACCOUNT ) ) ) ) ;
notification . setCreated_at ( Helper . stringToDate ( context , c . getString ( c . getColumnIndex ( Sqlite . COL_CREATED_AT ) ) ) ) ;
notification . setType ( c . getString ( c . getColumnIndex ( Sqlite . COL_TYPE ) ) ) ;
2019-09-06 17:55:14 +02:00
if ( c . getString ( c . getColumnIndex ( Sqlite . COL_STATUS_ID ) ) ! = null ) {
2019-08-25 12:08:18 +02:00
String status_id = c . getString ( c . getColumnIndex ( Sqlite . COL_STATUS_ID ) ) ;
2019-09-06 17:55:14 +02:00
Status status = new StatusCacheDAO ( context , db ) . getStatus ( StatusCacheDAO . NOTIFICATION_CACHE , status_id ) ;
2019-08-25 12:08:18 +02:00
notification . setStatus ( status ) ;
}
notifications . add ( notification ) ;
}
//Close the cursor
c . close ( ) ;
//notifications list is returned
return notifications ;
}
/ * * *
* Method to get cached notifications ID from database
* @param c Cursor
* @return List < String >
* /
2019-09-06 17:55:14 +02:00
private List < String > cursorToListNotificationsId ( Cursor c ) {
2019-08-25 12:08:18 +02:00
//No element found
if ( c . getCount ( ) = = 0 ) {
c . close ( ) ;
return null ;
}
List < String > notificationsId = new ArrayList < > ( ) ;
2019-09-06 17:55:14 +02:00
while ( c . moveToNext ( ) ) {
2019-08-25 12:08:18 +02:00
//Restore cached notification
notificationsId . add ( c . getString ( c . getColumnIndex ( Sqlite . COL_NOTIFICATION_ID ) ) ) ;
}
//Close the cursor
c . close ( ) ;
//Notification ids list is returned
return notificationsId ;
}
}