2019-05-18 11:10:30 +02:00
package app.fedilab.android.sqlite ;
2019-05-12 09:17:22 +02:00
/ * 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 ;
2019-05-12 09:52:56 +02:00
import android.content.SharedPreferences ;
2019-05-12 09:17:22 +02:00
import android.database.Cursor ;
import android.database.sqlite.SQLiteDatabase ;
import org.json.JSONException ;
import org.json.JSONObject ;
import java.util.ArrayList ;
import java.util.Date ;
import java.util.List ;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.API ;
import app.fedilab.android.client.Entities.Status ;
import app.fedilab.android.helper.Helper ;
2019-05-12 09:17:22 +02:00
/ * *
* Created by Thomas on 12 / 05 / 2019 .
* Manage Timeline Cache
* /
public class TimelineCacheDAO {
public Context context ;
2021-01-19 17:43:51 +01:00
private final SQLiteDatabase db ;
2019-05-12 09:17:22 +02:00
public TimelineCacheDAO ( 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-05-12 09:17:22 +02:00
/ * *
* Insert a status in database
2019-09-06 17:55:14 +02:00
*
2019-05-12 09:17:22 +02:00
* @return boolean
* /
2019-05-31 17:25:17 +02:00
public long insert ( String statusId , String jsonString , String userId , String instance ) {
2019-05-12 09:17:22 +02:00
ContentValues values = new ContentValues ( ) ;
values . put ( Sqlite . COL_INSTANCE , instance ) ;
values . put ( Sqlite . COL_STATUS_ID , statusId ) ;
2019-05-12 09:52:56 +02:00
values . put ( Sqlite . COL_USER_ID , userId ) ;
2019-05-12 09:17:22 +02:00
values . put ( Sqlite . COL_DATE , Helper . dateToString ( new Date ( ) ) ) ;
values . put ( Sqlite . COL_CACHE , jsonString ) ;
//Inserts cached status
long last_id ;
2019-09-06 17:55:14 +02:00
try {
2019-05-12 09:17:22 +02:00
last_id = db . insert ( Sqlite . TABLE_TIMELINE_CACHE , null , values ) ;
2019-09-06 17:55:14 +02:00
} catch ( Exception e ) {
last_id = - 1 ;
2019-05-12 11:39:25 +02:00
e . printStackTrace ( ) ;
2019-05-12 09:17:22 +02:00
}
return last_id ;
}
2019-05-12 17:35:14 +02:00
//------- UPDATE -------
2019-09-06 17:55:14 +02:00
2019-05-12 17:35:14 +02:00
/ * *
* Update a status in database
* /
2019-05-31 17:25:17 +02:00
public void update ( String statusId , String jsonString , String userId , String instance ) {
2019-05-12 17:35:14 +02:00
ContentValues values = new ContentValues ( ) ;
values . put ( Sqlite . COL_DATE , Helper . dateToString ( new Date ( ) ) ) ;
values . put ( Sqlite . COL_CACHE , jsonString ) ;
2019-09-06 17:55:14 +02:00
try {
db . update ( Sqlite . TABLE_TIMELINE_CACHE , values , Sqlite . COL_INSTANCE + " = ? AND " + Sqlite . COL_STATUS_ID + " = ? AND " + Sqlite . COL_USER_ID + " = ? " , new String [ ] { instance , statusId , userId } ) ;
} catch ( Exception ignored ) {
}
2019-05-12 17:35:14 +02:00
}
2019-05-12 09:17:22 +02:00
//------- REMOVE -------
/ * * *
* Remove stored status
* @return int
* /
2019-05-12 09:52:56 +02:00
public int remove ( String statusId ) {
SharedPreferences sharedpreferences = context . getSharedPreferences ( Helper . APP_PREFS , Context . MODE_PRIVATE ) ;
String userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
String instance = Helper . getLiveInstance ( context ) ;
2020-04-16 12:22:48 +02:00
try {
return db . delete ( Sqlite . TABLE_TIMELINE_CACHE , Sqlite . COL_STATUS_ID + " = \" " + statusId + " \" AND " + Sqlite . COL_INSTANCE + " = \" " + instance + " \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" " , null ) ;
2020-04-16 14:34:45 +02:00
} catch ( Exception ignored ) {
}
2020-04-16 12:22:48 +02:00
return - 1 ;
2019-05-12 09:17:22 +02:00
}
2019-05-21 18:21:58 +02:00
/ * * *
* Remove stored status
* /
2020-04-11 18:14:20 +02:00
public void removeAfterDate ( String date ) {
2019-05-21 18:21:58 +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 ) ;
2020-04-11 18:14:20 +02:00
try {
db . delete ( Sqlite . TABLE_TIMELINE_CACHE , Sqlite . COL_DATE + " < \" " + date + " \" AND " + Sqlite . COL_INSTANCE + " = \" " + instance + " \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" " , null ) ;
2020-04-16 14:34:45 +02:00
} catch ( Exception ignored ) {
}
2019-05-21 18:21:58 +02:00
}
2019-05-12 09:17:22 +02:00
/ * * *
* Remove stored status
* /
2020-04-11 18:14:20 +02:00
public void removeAllConnected ( ) {
2019-08-22 19:57:51 +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 ) ;
2020-04-11 18:14:20 +02:00
db . delete ( Sqlite . TABLE_TIMELINE_CACHE , Sqlite . COL_INSTANCE + " = \" " + instance + " \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" " , null ) ;
2019-05-12 09:17:22 +02:00
}
2019-08-22 19:57:51 +02:00
/ * * *
* Remove stored status
* @return int
* /
2019-09-06 17:55:14 +02:00
public int removeAll ( ) {
return db . delete ( Sqlite . TABLE_TIMELINE_CACHE , null , null ) ;
2019-08-22 19:57:51 +02:00
}
2019-05-12 09:17:22 +02:00
//------- GETTERS -------
/ * *
* Returns all cached Statuses
2019-09-06 17:55:14 +02:00
*
2019-05-12 09:17:22 +02:00
* @return stored Status List < Status >
* /
2019-09-06 17:55:14 +02:00
public List < Status > get ( String max_id ) {
2019-05-12 09:52:56 +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-05-12 09:17:22 +02:00
try {
Cursor c ;
2019-09-06 17:55:14 +02:00
if ( max_id ! = null )
c = db . query ( Sqlite . TABLE_TIMELINE_CACHE , null , Sqlite . COL_INSTANCE + " = \" " + instance + " \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" AND " + Sqlite . COL_STATUS_ID + " <= " + max_id , null , null , null , Sqlite . COL_STATUS_ID + " DESC " , " 40 " ) ;
2019-05-12 09:17:22 +02:00
else
2019-09-06 17:55:14 +02:00
c = db . query ( Sqlite . TABLE_TIMELINE_CACHE , null , Sqlite . COL_INSTANCE + " = \" " + instance + " \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" " , null , null , null , Sqlite . COL_STATUS_ID + " DESC " , " 40 " ) ;
2019-05-12 09:17:22 +02:00
return cursorToListStatus ( c ) ;
} catch ( Exception e ) {
return null ;
}
}
2019-08-19 14:50:30 +02:00
/ * *
* Returns all cached Statuses
2019-09-06 17:55:14 +02:00
*
2019-08-19 14:50:30 +02:00
* @return stored Status List < Status >
* /
2019-09-06 17:55:14 +02:00
public List < Status > search ( String word , String max_id ) {
2019-08-19 14:50:30 +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 {
Cursor c ;
2019-09-06 17:55:14 +02:00
if ( word ! = null ) {
2019-08-19 16:02:31 +02:00
StringBuilder query ;
2019-09-06 17:55:14 +02:00
if ( ! word . contains ( " \" " ) ) {
2019-08-19 16:02:31 +02:00
String [ ] searches = word . split ( " " ) ;
query = new StringBuilder ( " ( " ) ;
for ( String search : searches ) {
query . append ( Sqlite . COL_CACHE + " LIKE '% " ) . append ( search ) . append ( " %' OR " ) ;
}
query = new StringBuilder ( query . substring ( 0 , query . length ( ) - 3 ) ) ;
query . append ( " ) " ) ;
2019-09-06 17:55:14 +02:00
} else {
String search = word . replace ( " \" " , " " ) ;
2019-08-19 16:02:31 +02:00
query = new StringBuilder ( Sqlite . COL_CACHE + " LIKE '% " ) . append ( search ) . append ( " %' " ) ;
2019-08-19 15:17:18 +02:00
}
if ( max_id ! = null ) {
c = db . query ( Sqlite . TABLE_TIMELINE_CACHE , null , Sqlite . COL_INSTANCE + " = \" " + instance + " \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" AND " + Sqlite . COL_STATUS_ID + " < ' " + max_id + " ' AND " + query , null , null , null , Sqlite . COL_STATUS_ID + " DESC " , " 40 " ) ;
return cursorToListStatus ( c ) ;
} else {
c = db . query ( Sqlite . TABLE_TIMELINE_CACHE , null , Sqlite . COL_INSTANCE + " = \" " + instance + " \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" AND " + query , null , null , null , Sqlite . COL_STATUS_ID + " DESC " , " 40 " ) ;
return cursorToListStatus ( c ) ;
}
2019-08-19 14:50:30 +02:00
}
return null ;
} catch ( Exception e ) {
return null ;
}
}
2019-05-12 09:36:28 +02:00
/ * *
* Returns one cached Statuses
2019-09-06 17:55:14 +02:00
*
2019-05-12 09:36:28 +02:00
* @return stored Status List < Status >
* /
2019-09-06 17:55:14 +02:00
public Status getSingle ( String statusId ) {
2019-05-12 09:52:56 +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-05-12 09:36:28 +02:00
try {
2019-09-06 17:55:14 +02:00
Cursor c = db . query ( Sqlite . TABLE_TIMELINE_CACHE , null , Sqlite . COL_INSTANCE + " = \" " + instance + " \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" AND " + Sqlite . COL_STATUS_ID + " =' " + statusId + " ' " , null , null , null , Sqlite . COL_STATUS_ID + " DESC " , " 1 " ) ;
2019-05-12 09:36:28 +02:00
return cursorToSingleStatus ( c ) ;
} catch ( Exception e ) {
return null ;
}
}
2019-05-12 09:17:22 +02:00
2019-05-12 09:36:28 +02:00
/ * * *
* Method to hydrate one cached status from database
* @param c Cursor
* @return Status
* /
2019-09-06 17:55:14 +02:00
private Status cursorToSingleStatus ( Cursor c ) {
2019-05-12 09:36:28 +02:00
//No element found
2019-08-16 17:35:18 +02:00
if ( c . getCount ( ) = = 0 ) {
c . close ( ) ;
2019-05-12 09:36:28 +02:00
return null ;
2019-08-16 17:35:18 +02:00
}
2019-05-12 09:36:28 +02:00
c . moveToFirst ( ) ;
Status status = null ;
try {
status = API . parseStatuses ( context , new JSONObject ( c . getString ( c . getColumnIndex ( Sqlite . COL_CACHE ) ) ) ) ;
2019-05-12 11:39:25 +02:00
status . setcached ( true ) ;
2019-05-12 09:36:28 +02:00
} catch ( JSONException e ) {
e . printStackTrace ( ) ;
}
//Close the cursor
c . close ( ) ;
//Statuses list is returned
return status ;
}
2019-05-12 09:17:22 +02:00
/ * * *
* Method to hydrate cached statuses from database
* @param c Cursor
* @return List < Status >
* /
2019-09-06 17:55:14 +02:00
private List < Status > cursorToListStatus ( Cursor c ) {
2019-05-12 09:17:22 +02:00
//No element found
2019-08-16 17:35:18 +02:00
if ( c . getCount ( ) = = 0 ) {
c . close ( ) ;
2019-05-12 09:17:22 +02:00
return null ;
2019-08-16 17:35:18 +02:00
}
2019-05-12 09:17:22 +02:00
List < Status > statuses = new ArrayList < > ( ) ;
2019-09-06 17:55:14 +02:00
while ( c . moveToNext ( ) ) {
2019-05-12 09:17:22 +02:00
//Restore cached status
try {
Status status = API . parseStatuses ( context , new JSONObject ( c . getString ( c . getColumnIndex ( Sqlite . COL_CACHE ) ) ) ) ;
2019-05-12 11:39:25 +02:00
status . setcached ( true ) ;
2019-08-18 19:22:12 +02:00
status . setViewType ( context ) ;
2019-05-12 09:17:22 +02:00
statuses . add ( status ) ;
} catch ( JSONException e ) {
e . printStackTrace ( ) ;
}
}
//Close the cursor
c . close ( ) ;
//Statuses list is returned
return statuses ;
}
}