fedilab-Android-App/app/src/main/java/app/fedilab/android/sqlite/StatusCacheDAO.java

872 lines
43 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.sqlite;
2018-02-15 07:55:24 +01:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-02-15 07:55:24 +01:00
*
* 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.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2018-02-15 07:55:24 +01:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2018-02-15 07:55:24 +01:00
* see <http://www.gnu.org/licenses>. */
import android.content.ContentValues;
import android.content.Context;
2018-02-17 08:44:13 +01:00
import android.content.SharedPreferences;
2018-02-15 07:55:24 +01:00
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
2019-07-29 15:18:41 +02:00
2018-02-15 07:55:24 +01:00
import java.util.ArrayList;
2019-07-28 15:55:25 +02:00
import java.util.Calendar;
2018-02-17 08:49:28 +01:00
import java.util.Date;
2019-07-24 17:48:24 +02:00
import java.util.HashMap;
2019-07-30 17:23:32 +02:00
import java.util.LinkedHashMap;
2018-02-15 07:55:24 +01:00
import java.util.List;
2019-07-23 18:22:52 +02:00
import java.util.concurrent.TimeUnit;
2018-02-15 07:55:24 +01:00
2019-07-28 15:55:25 +02:00
import app.fedilab.android.client.Entities.Charts;
2019-07-23 18:22:52 +02:00
import app.fedilab.android.client.Entities.Statistics;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.Status;
2019-07-24 17:48:24 +02:00
import app.fedilab.android.client.Entities.Tag;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.helper.FilterToots;
import app.fedilab.android.helper.Helper;
2018-02-15 07:55:24 +01:00
/**
* Created by Thomas on 15/02/2018.
* Manage Status in cache
*/
public class StatusCacheDAO {
//Type of cache
public static int BOOKMARK_CACHE = 0;
public static int ARCHIVE_CACHE = 1;
2019-08-25 11:30:30 +02:00
public static int NOTIFICATION_CACHE = 2;
2019-11-15 16:32:25 +01:00
public Context context;
private SQLiteDatabase db;
2018-02-15 07:55:24 +01:00
public StatusCacheDAO(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
2018-09-28 19:35:13 +02:00
/**
* Insert a status in database
2019-09-06 17:55:14 +02:00
*
2018-09-28 19:35:13 +02:00
* @param cacheType int cache type
2019-09-06 17:55:14 +02:00
* @param status Status
* @param userId String
2018-09-28 19:35:13 +02:00
* @return boolean
*/
public long insertStatus(int cacheType, Status status, String userId, String instance) {
ContentValues values = new ContentValues();
values.put(Sqlite.COL_USER_ID, userId);
values.put(Sqlite.COL_CACHED_ACTION, cacheType);
values.put(Sqlite.COL_INSTANCE, instance);
values.put(Sqlite.COL_STATUS_ID, status.getId());
values.put(Sqlite.COL_URI, status.getUri());
2018-11-18 16:01:46 +01:00
values.put(Sqlite.COL_CARD, Helper.cardToStringStorage(status.getCard()));
2018-09-28 19:35:13 +02:00
values.put(Sqlite.COL_URL, status.getUrl());
values.put(Sqlite.COL_ACCOUNT, Helper.accountToStringStorage(status.getAccount()));
values.put(Sqlite.COL_IN_REPLY_TO_ID, status.getIn_reply_to_id());
values.put(Sqlite.COL_IN_REPLY_TO_ACCOUNT_ID, status.getIn_reply_to_account_id());
2020-06-06 18:42:12 +02:00
try {
values.put(Sqlite.COL_REBLOG, status.getReblog() != null ? Helper.statusToStringStorage(status.getReblog()) : "");
} catch (Exception ignored) {
}
2018-09-28 19:35:13 +02:00
values.put(Sqlite.COL_CONTENT, status.getContent());
2019-09-06 17:55:14 +02:00
values.put(Sqlite.COL_EMOJIS, status.getEmojis() != null ? Helper.emojisToStringStorage(status.getEmojis()) : null);
values.put(Sqlite.COL_POLL, status.getPoll() != null ? Helper.pollToStringStorage(status.getPoll()) : null);
2018-09-28 19:35:13 +02:00
values.put(Sqlite.COL_REBLOGS_COUNT, status.getReblogs_count());
values.put(Sqlite.COL_FAVOURITES_COUNT, status.getFavourites_count());
values.put(Sqlite.COL_REBLOGGED, status.isReblogged());
values.put(Sqlite.COL_FAVOURITED, status.isFavourited());
values.put(Sqlite.COL_MUTED, status.isMuted());
values.put(Sqlite.COL_CREATED_AT, Helper.dateToString(status.getCreated_at()));
values.put(Sqlite.COL_DATE_BACKUP, Helper.dateToString(new Date()));
values.put(Sqlite.COL_SENSITIVE, status.isSensitive());
values.put(Sqlite.COL_SPOILER_TEXT, status.getSpoiler_text());
values.put(Sqlite.COL_VISIBILITY, status.getVisibility());
2019-09-06 17:55:14 +02:00
values.put(Sqlite.COL_MEDIA_ATTACHMENTS, status.getMedia_attachments() != null ? Helper.attachmentToStringStorage(status.getMedia_attachments()) : null);
values.put(Sqlite.COL_MENTIONS, status.getMentions() != null ? Helper.mentionToStringStorage(status.getMentions()) : null);
values.put(Sqlite.COL_TAGS, status.getTags() != null ? Helper.tagToStringStorage(status.getTags()) : null);
values.put(Sqlite.COL_APPLICATION, status.getApplication() != null ? Helper.applicationToStringStorage(status.getApplication()) : null);
2018-09-28 19:35:13 +02:00
values.put(Sqlite.COL_LANGUAGE, status.getLanguage());
values.put(Sqlite.COL_PINNED, status.isPinned());
//Inserts cached status
long last_id;
2019-09-06 17:55:14 +02:00
try {
2018-09-28 19:35:13 +02:00
last_id = db.insert(Sqlite.TABLE_STATUSES_CACHE, null, values);
2019-09-06 17:55:14 +02:00
} catch (Exception e) {
last_id = -1;
2018-11-18 16:01:46 +01:00
e.printStackTrace();
2018-09-28 19:35:13 +02:00
}
return last_id;
}
2018-02-15 07:55:24 +01:00
/**
* Insert a status in database
2019-09-06 17:55:14 +02:00
*
2018-02-15 07:55:24 +01:00
* @param cacheType int cache type
2019-09-06 17:55:14 +02:00
* @param status Status
2018-02-15 07:55:24 +01:00
* @return boolean
*/
public long insertStatus(int cacheType, Status status) {
2018-11-18 16:01:46 +01:00
2018-02-15 07:55:24 +01:00
ContentValues values = new ContentValues();
2018-02-17 08:44:13 +01:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2018-02-15 07:55:24 +01:00
String instance = Helper.getLiveInstance(context);
2018-02-17 08:44:13 +01:00
values.put(Sqlite.COL_USER_ID, userId);
2018-02-15 07:55:24 +01:00
values.put(Sqlite.COL_CACHED_ACTION, cacheType);
values.put(Sqlite.COL_INSTANCE, instance);
2018-02-15 07:55:24 +01:00
values.put(Sqlite.COL_STATUS_ID, status.getId());
values.put(Sqlite.COL_URI, status.getUri());
2018-02-15 15:27:46 +01:00
values.put(Sqlite.COL_URL, status.getUrl());
2018-02-15 07:55:24 +01:00
values.put(Sqlite.COL_ACCOUNT, Helper.accountToStringStorage(status.getAccount()));
2018-11-18 13:33:12 +01:00
values.put(Sqlite.COL_CARD, Helper.cardToStringStorage(status.getCard()));
2018-02-15 07:55:24 +01:00
values.put(Sqlite.COL_IN_REPLY_TO_ID, status.getIn_reply_to_id());
values.put(Sqlite.COL_IN_REPLY_TO_ACCOUNT_ID, status.getIn_reply_to_account_id());
2019-09-06 17:55:14 +02:00
values.put(Sqlite.COL_REBLOG, status.getReblog() != null ? Helper.statusToStringStorage(status.getReblog()) : null);
2018-02-15 07:55:24 +01:00
values.put(Sqlite.COL_CONTENT, status.getContent());
2019-09-06 17:55:14 +02:00
values.put(Sqlite.COL_EMOJIS, status.getEmojis() != null ? Helper.emojisToStringStorage(status.getEmojis()) : null);
values.put(Sqlite.COL_POLL, status.getPoll() != null ? Helper.pollToStringStorage(status.getPoll()) : null);
2018-02-15 07:55:24 +01:00
values.put(Sqlite.COL_REBLOGS_COUNT, status.getReblogs_count());
values.put(Sqlite.COL_FAVOURITES_COUNT, status.getFavourites_count());
values.put(Sqlite.COL_REBLOGGED, status.isReblogged());
values.put(Sqlite.COL_FAVOURITED, status.isFavourited());
values.put(Sqlite.COL_MUTED, status.isMuted());
2018-04-28 16:54:06 +02:00
values.put(Sqlite.COL_CREATED_AT, Helper.dateToString(status.getCreated_at()));
values.put(Sqlite.COL_DATE_BACKUP, Helper.dateToString(new Date()));
2018-02-15 07:55:24 +01:00
values.put(Sqlite.COL_SENSITIVE, status.isSensitive());
values.put(Sqlite.COL_SPOILER_TEXT, status.getSpoiler_text());
values.put(Sqlite.COL_VISIBILITY, status.getVisibility());
2019-09-06 17:55:14 +02:00
values.put(Sqlite.COL_MEDIA_ATTACHMENTS, status.getMedia_attachments() != null ? Helper.attachmentToStringStorage(status.getMedia_attachments()) : null);
values.put(Sqlite.COL_MENTIONS, status.getMentions() != null ? Helper.mentionToStringStorage(status.getMentions()) : null);
values.put(Sqlite.COL_TAGS, status.getTags() != null ? Helper.tagToStringStorage(status.getTags()) : null);
values.put(Sqlite.COL_APPLICATION, status.getApplication() != null ? Helper.applicationToStringStorage(status.getApplication()) : null);
2018-02-15 07:55:24 +01:00
values.put(Sqlite.COL_LANGUAGE, status.getLanguage());
values.put(Sqlite.COL_PINNED, status.isPinned());
//Inserts cached status
2018-02-15 07:55:24 +01:00
long last_id;
2019-09-06 17:55:14 +02:00
try {
2018-02-15 07:55:24 +01:00
last_id = db.insert(Sqlite.TABLE_STATUSES_CACHE, null, values);
2019-09-06 17:55:14 +02:00
} catch (Exception e) {
last_id = -1;
2018-11-18 16:01:46 +01:00
e.printStackTrace();
2018-02-15 07:55:24 +01:00
}
return last_id;
}
//------- UPDATES -------
/**
* Update a Status cached in database
2019-09-06 17:55:14 +02:00
*
2018-02-15 07:55:24 +01:00
* @param status Status
* @return boolean
*/
2019-09-06 17:55:14 +02:00
public int updateStatus(int cacheType, Status status) {
2018-02-15 07:55:24 +01:00
ContentValues values = new ContentValues();
String instance = Helper.getLiveInstance(context);
values.put(Sqlite.COL_REBLOGS_COUNT, status.getReblogs_count());
values.put(Sqlite.COL_FAVOURITES_COUNT, status.getFavourites_count());
values.put(Sqlite.COL_REBLOGGED, status.isReblogged());
values.put(Sqlite.COL_FAVOURITED, status.isFavourited());
values.put(Sqlite.COL_MUTED, status.isMuted());
values.put(Sqlite.COL_PINNED, status.isPinned());
values.put(Sqlite.COL_POLL, status.getPoll() != null ? Helper.pollToStringStorage(status.getPoll()) : null);
return db.update(Sqlite.TABLE_STATUSES_CACHE,
values, Sqlite.COL_STATUS_ID + " = ? AND " + Sqlite.COL_INSTANCE + " = ? AND " + Sqlite.COL_CACHED_ACTION + " = ?",
new String[]{String.valueOf(status.getId()), instance, String.valueOf(cacheType)});
2018-02-15 07:55:24 +01:00
}
//------- REMOVE -------
/***
* Remove stored status
2018-02-15 07:55:24 +01:00
* @return int
*/
2019-09-06 17:55:14 +02:00
public int remove(int cacheType, Status status) {
2018-02-17 08:44:13 +01:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2018-02-15 07:55:24 +01:00
String instance = Helper.getLiveInstance(context);
2019-09-06 17:55:14 +02:00
return db.delete(Sqlite.TABLE_STATUSES_CACHE, Sqlite.COL_CACHED_ACTION + " = \"" + cacheType + "\" AND " + Sqlite.COL_STATUS_ID + " = \"" + status.getId() + "\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance + "\" AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null);
2018-02-15 07:55:24 +01:00
}
2019-09-06 17:55:14 +02:00
public void removeDuplicate() {
db.execSQL("DELETE FROM " + Sqlite.TABLE_STATUSES_CACHE + " WHERE " + Sqlite.COL_ID + " NOT IN (SELECT MIN(" + Sqlite.COL_ID + ") FROM " + Sqlite.TABLE_STATUSES_CACHE + " GROUP BY " + Sqlite.COL_STATUS_ID + "," + Sqlite.COL_INSTANCE + ")");
2019-07-22 12:27:46 +02:00
}
2018-09-28 19:35:13 +02:00
/***
* Remove stored status
* @return int
*/
2019-09-06 17:55:14 +02:00
public int remove(int cacheType, Status status, String userId, String instance) {
return db.delete(Sqlite.TABLE_STATUSES_CACHE, Sqlite.COL_CACHED_ACTION + " = \"" + cacheType + "\" AND " + Sqlite.COL_STATUS_ID + " = \"" + status.getId() + "\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance + "\" AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null);
2018-09-28 19:35:13 +02:00
}
2019-09-06 17:55:14 +02:00
public int removeAllStatus(int cacheType) {
2018-02-17 08:44:13 +01: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_STATUSES_CACHE, Sqlite.COL_CACHED_ACTION + " = \"" + cacheType + "\" AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null);
2018-02-15 07:55:24 +01:00
}
2019-09-06 17:55:14 +02:00
public int removeAll(int cacheType) {
return db.delete(Sqlite.TABLE_STATUSES_CACHE, Sqlite.COL_CACHED_ACTION + " = \"" + cacheType + "\"", null);
2019-08-23 09:29:30 +02:00
}
2018-02-15 07:55:24 +01:00
//------- GETTERS -------
/**
* Returns all cached Statuses in db depending of their cache type
2019-09-06 17:55:14 +02:00
*
2018-02-15 07:55:24 +01:00
* @return stored status List<StoredStatus>
*/
2019-09-06 17:55:14 +02:00
public List<Status> getAllStatus(int cacheType) {
2018-02-17 08:44:13 +01:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2018-02-15 07:55:24 +01:00
String instance = Helper.getLiveInstance(context);
try {
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_STATUSES_CACHE, null, Sqlite.COL_CACHED_ACTION + " = '" + cacheType + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null, null, null, Sqlite.COL_CREATED_AT + " DESC", null);
2018-02-15 07:55:24 +01:00
return cursorToListStatuses(c);
2018-11-17 14:31:03 +01:00
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
2018-11-18 16:01:46 +01:00
2019-01-02 13:31:11 +01:00
/**
* Returns all cached Statuses in db depending of their cache type
2019-09-06 17:55:14 +02:00
*
2019-01-02 13:31:11 +01:00
* @return stored status List<StoredStatus>
*/
2019-09-06 17:55:14 +02:00
public List<String> getAllStatusId(int cacheType) {
2019-01-02 13:31:11 +01: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_STATUSES_CACHE, new String[]{Sqlite.COL_STATUS_ID}, Sqlite.COL_CACHED_ACTION + " = '" + cacheType + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null, null, null, Sqlite.COL_CREATED_AT + " DESC", null);
2019-01-02 13:31:11 +01:00
return cursorToListStatusesId(c);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
2018-02-17 11:28:52 +01:00
/**
* Returns all cached Statuses in db depending of their cache type
2019-09-06 17:55:14 +02:00
*
2018-02-17 11:28:52 +01:00
* @return stored status List<StoredStatus>
*/
2019-09-06 17:55:14 +02:00
public List<Status> getStatusFromID(int cacheType, FilterToots filterToots, String max_id) {
2018-02-17 11:28:52 +01: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);
2018-02-17 14:43:12 +01:00
//That the basic selection for all toots
2018-02-17 15:14:54 +01:00
StringBuilder selection = new StringBuilder(Sqlite.COL_CACHED_ACTION + " = '" + cacheType + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'");
2019-09-06 17:55:14 +02:00
if (max_id != null)
2018-02-17 15:14:54 +01:00
selection.append(" AND " + Sqlite.COL_STATUS_ID + " < '").append(max_id).append("'");
2018-02-17 14:43:12 +01:00
//BOOST
2019-09-06 17:55:14 +02:00
if (filterToots.getBoosts() == FilterToots.typeFilter.NONE)
2018-02-17 15:14:54 +01:00
selection.append(" AND (" + Sqlite.COL_REBLOG + " IS NULL OR " + Sqlite.COL_REBLOG + " = 'null')");
2019-09-06 17:55:14 +02:00
else if (filterToots.getBoosts() == FilterToots.typeFilter.ONLY)
2018-02-17 15:14:54 +01:00
selection.append(" AND " + Sqlite.COL_REBLOG + " IS NOT NULL AND " + Sqlite.COL_REBLOG + " <> 'null'");
2018-02-17 14:43:12 +01:00
//REPLIES
2019-09-06 17:55:14 +02:00
if (filterToots.getReplies() == FilterToots.typeFilter.NONE)
2018-02-17 15:14:54 +01:00
selection.append(" AND (" + Sqlite.COL_IN_REPLY_TO_ID + " IS NULL OR " + Sqlite.COL_IN_REPLY_TO_ID + " = 'null')");
2019-09-06 17:55:14 +02:00
else if (filterToots.getReplies() == FilterToots.typeFilter.ONLY)
2018-02-17 15:14:54 +01:00
selection.append(" AND " + Sqlite.COL_IN_REPLY_TO_ID + " IS NOT NULL AND " + Sqlite.COL_IN_REPLY_TO_ID + " <> 'null'");
2018-02-17 14:43:12 +01:00
//PINNED
2019-09-06 17:55:14 +02:00
if (filterToots.getPinned() == FilterToots.typeFilter.NONE)
2018-02-17 15:14:54 +01:00
selection.append(" AND " + Sqlite.COL_PINNED + " = 0");
2019-09-06 17:55:14 +02:00
else if (filterToots.getPinned() == FilterToots.typeFilter.ONLY)
2018-02-17 15:14:54 +01:00
selection.append(" AND " + Sqlite.COL_PINNED + " = 1");
2018-02-17 14:43:12 +01:00
//PINNED
2019-09-06 17:55:14 +02:00
if (filterToots.getMedia() == FilterToots.typeFilter.NONE)
2018-02-17 15:14:54 +01:00
selection.append(" AND " + Sqlite.COL_MEDIA_ATTACHMENTS + " = '[]'");
2019-09-06 17:55:14 +02:00
else if (filterToots.getMedia() == FilterToots.typeFilter.ONLY)
2018-02-17 15:14:54 +01:00
selection.append(" AND " + Sqlite.COL_MEDIA_ATTACHMENTS + " <> '[]'");
2019-09-06 17:55:14 +02:00
if (!filterToots.isV_direct())
2018-02-17 15:14:54 +01:00
selection.append(" AND " + Sqlite.COL_VISIBILITY + " <> 'direct'");
2019-09-06 17:55:14 +02:00
if (!filterToots.isV_private())
2018-02-17 15:14:54 +01:00
selection.append(" AND " + Sqlite.COL_VISIBILITY + " <> 'private'");
2019-09-06 17:55:14 +02:00
if (!filterToots.isV_public())
2018-02-17 15:14:54 +01:00
selection.append(" AND " + Sqlite.COL_VISIBILITY + " <> 'public'");
2019-09-06 17:55:14 +02:00
if (!filterToots.isV_unlisted())
2018-02-17 15:14:54 +01:00
selection.append(" AND " + Sqlite.COL_VISIBILITY + " <> 'unlisted'");
2019-09-06 17:55:14 +02:00
if (filterToots.getDateIni() != null)
2018-02-17 15:14:54 +01:00
selection.append(" AND " + Sqlite.COL_CREATED_AT + " >= '").append(filterToots.getDateIni()).append("'");
2019-09-06 17:55:14 +02:00
if (filterToots.getDateEnd() != null)
2018-02-17 15:14:54 +01:00
selection.append(" AND " + Sqlite.COL_CREATED_AT + " <= '").append(filterToots.getDateEnd()).append("'");
2019-09-06 17:55:14 +02:00
if (filterToots.getFilter() != null) {
2018-02-17 15:14:54 +01:00
String[] keywords = filterToots.getFilter().split(" ");
selection.append(" AND (");
int i = 0;
2019-09-06 17:55:14 +02:00
for (String kw : keywords) {
2018-02-17 15:14:54 +01:00
2019-09-06 17:55:14 +02:00
if (i == 0 && keywords.length == 1)
2018-02-17 15:14:54 +01:00
selection.append(Sqlite.COL_CONTENT + " LIKE '%").append(kw).append("%'");
2019-09-06 17:55:14 +02:00
else if (i == 0 && keywords.length > 1)
2018-02-17 15:14:54 +01:00
selection.append(Sqlite.COL_CONTENT + " LIKE '%").append(kw).append("%' OR ");
2019-09-06 17:55:14 +02:00
else if (i == keywords.length - 1)
2018-02-17 15:14:54 +01:00
selection.append(Sqlite.COL_CONTENT + " LIKE '%").append(kw).append("%'");
i++;
}
selection.append(")");
}
String order = Sqlite.COL_CREATED_AT + " DESC";
if (filterToots.getOrder() != null) {
2019-11-15 16:32:25 +01:00
if (filterToots.getOrder() == FilterToots.typeOrder.ASC) {
order = Sqlite.COL_CREATED_AT + " ASC";
2019-11-15 16:32:25 +01:00
} else if (filterToots.getOrder() == FilterToots.typeOrder.DESC) {
order = Sqlite.COL_CREATED_AT + " DESC";
}
}
2018-02-17 14:43:12 +01:00
2018-02-17 11:28:52 +01:00
try {
Cursor c = db.query(Sqlite.TABLE_STATUSES_CACHE, null, selection.toString(), null, null, null, order, "40");
2018-02-17 11:28:52 +01:00
return cursorToListStatuses(c);
} catch (Exception e) {
2018-02-17 14:43:12 +01:00
e.printStackTrace();
2018-02-17 11:28:52 +01:00
return null;
}
}
2018-02-17 09:30:44 +01:00
/**
* Returns the last date of backup for a user depending of the type of cache
2019-09-06 17:55:14 +02:00
*
2018-02-17 09:30:44 +01:00
* @return Date
*/
2019-09-06 17:55:14 +02:00
public Date getLastDateCache(int cacheType) {
2018-02-17 09:30:44 +01: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_STATUSES_CACHE, null, Sqlite.COL_CACHED_ACTION + " = '" + cacheType + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null, null, null, Sqlite.COL_DATE_BACKUP + " DESC", "1");
2018-02-17 09:30:44 +01:00
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
2018-02-17 09:30:44 +01:00
return null;
2019-08-17 14:34:33 +02:00
}
2018-02-17 09:30:44 +01:00
//Take the first element
c.moveToFirst();
String date = c.getString(c.getColumnIndex(Sqlite.COL_DATE_BACKUP));
c.close();
return Helper.stringToDate(context, date);
} catch (Exception e) {
return null;
}
}
2018-02-17 12:35:54 +01:00
/**
* Returns the smaller date
2019-09-06 17:55:14 +02:00
*
2018-02-17 12:35:54 +01:00
* @return Date
*/
2019-09-06 17:55:14 +02:00
public Date getSmallerDate(int cacheType) {
2018-02-17 12:35:54 +01: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_STATUSES_CACHE, null, Sqlite.COL_CACHED_ACTION + " = '" + cacheType + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null, null, null, Sqlite.COL_CREATED_AT + " ASC", "1");
2018-02-17 12:35:54 +01:00
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
2018-02-17 12:35:54 +01:00
return null;
2019-08-17 14:34:33 +02:00
}
2018-02-17 12:35:54 +01:00
//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
*
2018-02-17 12:35:54 +01:00
* @return Date
*/
2019-09-06 17:55:14 +02:00
public Date getGreaterDate(int cacheType) {
2018-02-17 12:35:54 +01: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_STATUSES_CACHE, null, Sqlite.COL_CACHED_ACTION + " = '" + cacheType + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null, null, null, Sqlite.COL_CREATED_AT + " DESC", "1");
2018-02-17 12:35:54 +01:00
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
2018-02-17 12:35:54 +01:00
return null;
2019-08-17 14:34:33 +02:00
}
2018-02-17 12:35:54 +01:00
//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;
}
}
2018-02-17 09:30:44 +01:00
/**
* Returns the last id of backup for a user depending of the type of cache
2019-09-06 17:55:14 +02:00
*
2018-02-17 09:30:44 +01:00
* @return Date
*/
2019-09-06 17:55:14 +02:00
public String getLastTootIDCache(int cacheType) {
2018-02-17 09:30:44 +01: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_STATUSES_CACHE, null, Sqlite.COL_CACHED_ACTION + " = '" + cacheType + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null, null, null, Sqlite.COL_STATUS_ID + " DESC", "1");
2018-02-17 09:30:44 +01:00
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
2018-02-17 09:30:44 +01:00
return null;
2019-08-17 14:34:33 +02:00
}
2018-02-17 09:30:44 +01:00
//Take the first element
c.moveToFirst();
String last_id = c.getString(c.getColumnIndex(Sqlite.COL_STATUS_ID));
c.close();
return last_id;
} catch (Exception e) {
return null;
}
}
2019-08-07 10:24:35 +02:00
/**
* Returns the last date of backup for a user depending of the type of cache
2019-09-06 17:55:14 +02:00
*
2019-08-07 10:24:35 +02:00
* @return Date
*/
2019-09-06 17:55:14 +02:00
public Date getLastTootDateCache(int cacheType, String userId, String instance) {
2019-08-07 10:24:35 +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-07 13:50:53 +02:00
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
instance = Helper.getLiveInstance(context);
}
2019-08-07 10:24:35 +02:00
try {
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_STATUSES_CACHE, null, Sqlite.COL_CACHED_ACTION + " = '" + cacheType + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null, null, null, Sqlite.COL_CREATED_AT + " DESC", "1");
2019-08-07 10:24:35 +02:00
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
2019-08-07 10:24:35 +02:00
return null;
2019-08-17 14:34:33 +02:00
}
2019-08-07 10:24:35 +02:00
//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;
}
}
2018-02-15 07:55:24 +01:00
/**
* Returns a cached status by id in db
2019-09-06 17:55:14 +02:00
*
2018-02-15 07:55:24 +01:00
* @return stored status StoredStatus
*/
2019-09-06 17:55:14 +02:00
public Status getStatus(int cacheType, String id) {
2018-02-17 08:44:13 +01: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);
2018-02-15 07:55:24 +01:00
try {
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_STATUSES_CACHE, null, Sqlite.COL_CACHED_ACTION + " = '" + cacheType + "' AND " + Sqlite.COL_STATUS_ID + " = '" + id + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null, null, null, null, null);
2018-02-15 07:55:24 +01:00
return cursorToStoredStatus(c);
} catch (Exception e) {
return null;
}
}
2019-08-27 18:22:34 +02:00
/**
* Returns a cached status by id in db
2019-09-06 17:55:14 +02:00
*
2019-08-27 18:22:34 +02:00
* @return stored status StoredStatus
*/
2019-09-06 17:55:14 +02:00
public Status getStatus(String id) {
2019-08-27 18:22: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);
try {
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_STATUSES_CACHE, null, Sqlite.COL_STATUS_ID + " = '" + id + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null, null, null, null, null);
2019-08-27 18:22:34 +02:00
return cursorToStoredStatus(c);
} catch (Exception e) {
return null;
}
}
2019-07-28 15:55:25 +02:00
2019-09-06 17:55:14 +02:00
public Charts getCharts(Date dateIni, Date dateEnd) {
2019-07-28 15:55:25 +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);
Charts charts = new Charts();
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-07-28 15:55:25 +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-07-28 15:55:25 +02:00
StringBuilder selection = new StringBuilder(Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + 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<Status> data = new ArrayList<>();
try {
Cursor c = db.query(Sqlite.TABLE_STATUSES_CACHE, null, selection.toString(), null, null, null, Sqlite.COL_CREATED_AT + " ASC");
data = cursorToListStatuses(c);
} catch (Exception e) {
e.printStackTrace();
}
List<String> xLabel = new ArrayList<>();
2019-07-30 17:23:32 +02:00
charts.setStatuses(new LinkedHashMap<>());
charts.setBoosts(new LinkedHashMap<>());
charts.setReplies(new LinkedHashMap<>());
2019-09-06 17:55:14 +02:00
if (data != null) {
2019-07-30 15:28:29 +02:00
for (Status status : data) {
Calendar tempdate = Calendar.getInstance();
tempdate.setTime(status.getCreated_at());
tempdate.set(Calendar.HOUR_OF_DAY, 0);
tempdate.set(Calendar.MINUTE, 0);
tempdate.set(Calendar.SECOND, 0);
2019-07-30 17:23:32 +02:00
long date = tempdate.getTimeInMillis();
2019-07-30 15:28:29 +02:00
if (status.getReblog() != null) {
2019-09-06 17:55:14 +02:00
if (charts.getBoosts().containsKey(date)) {
charts.getBoosts().put(date, (charts.getBoosts().get(date) + 1));
} else {
charts.getBoosts().put(date, 1);
2019-07-30 17:23:32 +02:00
}
2019-07-30 15:28:29 +02:00
} else if (status.getIn_reply_to_id() != null && !status.getIn_reply_to_id().trim().equals("null")) {
2019-09-06 17:55:14 +02:00
if (charts.getReplies().containsKey(date)) {
charts.getReplies().put(date, (charts.getReplies().get(date) + 1));
} else {
charts.getReplies().put(date, 1);
2019-07-30 17:23:32 +02:00
}
2019-07-30 15:28:29 +02:00
} else {
2019-09-06 17:55:14 +02:00
if (charts.getStatuses().containsKey(date)) {
charts.getStatuses().put(date, (charts.getStatuses().get(date) + 1));
} else {
charts.getStatuses().put(date, 1);
2019-07-30 17:23:32 +02:00
}
2019-07-28 15:55:25 +02:00
}
}
}
charts.setxLabels(xLabel);
return charts;
}
2018-09-28 19:35:13 +02:00
/**
* Returns a cached status by id in db
2019-09-06 17:55:14 +02:00
*
2018-09-28 19:35:13 +02:00
* @return stored status StoredStatus
*/
2019-09-06 17:55:14 +02:00
public Status getStatus(int cacheType, String id, String userId, String instance) {
2018-09-28 19:35:13 +02:00
try {
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_STATUSES_CACHE, null, Sqlite.COL_CACHED_ACTION + " = '" + cacheType + "' AND " + Sqlite.COL_STATUS_ID + " = '" + id + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null, null, null, null, null);
2018-09-28 19:35:13 +02:00
return cursorToStoredStatus(c);
} catch (Exception e) {
return null;
}
}
2019-09-06 17:55:14 +02:00
public Statistics getStat() {
2018-11-18 16:01:46 +01:00
2019-07-23 18:22:52 +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);
Statistics statistics = new Statistics();
//Count All
Cursor mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_STATUSES_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "'"
2019-07-23 18:22:52 +02:00
, null);
mCount.moveToFirst();
statistics.setTotal_statuses(mCount.getInt(0));
mCount.close();
//Count boosts
mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_STATUSES_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND "
+ Sqlite.COL_REBLOG + " IS NOT NULL" + " AND " + Sqlite.COL_REBLOG + " != ''"
2019-07-23 18:22:52 +02:00
, null);
mCount.moveToFirst();
statistics.setNumber_boosts(mCount.getInt(0));
mCount.close();
//Count replies
mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_STATUSES_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND "
+ Sqlite.COL_IN_REPLY_TO_ID + " IS NOT NULL" + " AND " + Sqlite.COL_IN_REPLY_TO_ID + " != 'null'" + " AND " + Sqlite.COL_REBLOG + " IS NULL"
2019-07-23 18:22:52 +02:00
, null);
mCount.moveToFirst();
statistics.setNumber_replies(mCount.getInt(0));
mCount.close();
statistics.setNumber_status(statistics.getTotal_statuses() - statistics.getNumber_boosts() - statistics.getNumber_replies());
//Count media
mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_STATUSES_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND "
+ Sqlite.COL_MEDIA_ATTACHMENTS + " IS NOT NULL" + " AND " + Sqlite.COL_MEDIA_ATTACHMENTS + " !='" + Helper.attachmentToStringStorage(new ArrayList<>()) + "' " + " AND " + Sqlite.COL_REBLOG + " IS NULL"
2019-07-23 18:22:52 +02:00
, null);
mCount.moveToFirst();
statistics.setNumber_with_media(mCount.getInt(0));
mCount.close();
//Count sensitive
mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_STATUSES_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND "
+ Sqlite.COL_SENSITIVE + "= 1 AND " + Sqlite.COL_MEDIA_ATTACHMENTS + " IS NOT NULL" + " AND " + Sqlite.COL_MEDIA_ATTACHMENTS + " !='" + Helper.attachmentToStringStorage(new ArrayList<>()) + "' " + " AND " + Sqlite.COL_REBLOG + " IS NULL"
2019-07-23 18:22:52 +02:00
, null);
mCount.moveToFirst();
statistics.setNumber_with_sensitive_media(mCount.getInt(0));
mCount.close();
//Count sensitive
mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_STATUSES_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND "
+ Sqlite.COL_SPOILER_TEXT + " IS NOT NULL" + " AND " + Sqlite.COL_SPOILER_TEXT + " != '' " + " AND " + Sqlite.COL_REBLOG + " IS NULL"
2019-07-23 18:22:52 +02:00
, null);
mCount.moveToFirst();
statistics.setNumber_with_cw(mCount.getInt(0));
mCount.close();
//Count public
mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_STATUSES_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND "
+ Sqlite.COL_VISIBILITY + "='public'" + " AND " + Sqlite.COL_REBLOG + " IS NULL"
2019-07-23 18:22:52 +02:00
, null);
mCount.moveToFirst();
statistics.setV_public(mCount.getInt(0));
mCount.close();
//Count unlisted
mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_STATUSES_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND "
+ Sqlite.COL_VISIBILITY + "='unlisted'" + " AND " + Sqlite.COL_REBLOG + " IS NULL"
2019-07-23 18:22:52 +02:00
, null);
mCount.moveToFirst();
statistics.setV_unlisted(mCount.getInt(0));
mCount.close();
//Count private
mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_STATUSES_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND "
+ Sqlite.COL_VISIBILITY + "='private'" + " AND " + Sqlite.COL_REBLOG + " IS NULL"
2019-07-23 18:22:52 +02:00
, null);
mCount.moveToFirst();
statistics.setV_private(mCount.getInt(0));
mCount.close();
//Count private
mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_STATUSES_CACHE
2019-09-06 17:55:14 +02:00
+ " where " + Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND "
+ Sqlite.COL_VISIBILITY + "='direct'" + " AND " + Sqlite.COL_REBLOG + " IS NULL"
2019-07-23 18:22:52 +02:00
, null);
mCount.moveToFirst();
statistics.setV_direct(mCount.getInt(0));
mCount.close();
2019-07-24 17:48:24 +02:00
HashMap<String, Integer> countTags = new HashMap<>();
//Get tags
Cursor c = db.query(Sqlite.TABLE_STATUSES_CACHE, new String[]{Sqlite.COL_TAGS}, Sqlite.COL_CACHED_ACTION + " = '" + ARCHIVE_CACHE + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_REBLOG + " IS NULL", null, null, null, null);
if (c.getCount() > 0) {
while (c.moveToNext()) {
//Restore cached status
List<Tag> tags = Helper.restoreTagFromString(c.getString(c.getColumnIndex(Sqlite.COL_TAGS)));
if (tags != null && tags.size() > 0) {
for (Tag tag : tags) {
if (countTags.containsKey(tag.getName())) {
int val = countTags.get(tag.getName());
countTags.put(tag.getName(), val + 1);
} else {
countTags.put(tag.getName(), 1);
}
}
}
}
}
//Close the cursor
c.close();
2019-09-06 17:55:14 +02:00
if (countTags.size() > 0) {
2019-07-24 17:48:24 +02:00
statistics.setTagsTrend(Helper.sortByValue(countTags));
2019-09-06 17:55:14 +02:00
} else {
2019-07-24 17:48:24 +02:00
statistics.setTagsTrend(countTags);
}
2019-07-23 18:22:52 +02:00
statistics.setFirstTootDate(getSmallerDate(ARCHIVE_CACHE));
statistics.setLastTootDate(getGreaterDate(ARCHIVE_CACHE));
2019-07-24 17:48:24 +02:00
long days = 1;
2019-09-06 17:55:14 +02:00
if (statistics.getLastTootDate() != null && statistics.getFirstTootDate() != null) {
2019-07-24 17:48:24 +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_statuses() / days);
2019-07-23 18:22:52 +02:00
return statistics;
}
2018-11-18 16:01:46 +01:00
2018-02-15 07:55:24 +01:00
/***
* Method to hydrate statuses from database
2018-02-15 07:55:24 +01:00
* @param c Cursor
* @return Status
2018-02-15 07:55:24 +01:00
*/
2019-09-06 17:55:14 +02:00
private Status cursorToStoredStatus(Cursor c) {
2018-02-15 07:55:24 +01:00
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
2018-02-15 07:55:24 +01:00
return null;
2019-08-17 14:34:33 +02:00
}
2018-02-15 07:55:24 +01:00
//Take the first element
c.moveToFirst();
//New status
Status status = new Status();
2019-08-18 19:22:12 +02:00
status.setViewType(context);
status.setId(c.getString(c.getColumnIndex(Sqlite.COL_STATUS_ID)));
status.setUri(c.getString(c.getColumnIndex(Sqlite.COL_URI)));
status.setUrl(c.getString(c.getColumnIndex(Sqlite.COL_URL)));
status.setAccount(Helper.restoreAccountFromString(c.getString(c.getColumnIndex(Sqlite.COL_ACCOUNT))));
2018-11-18 13:33:12 +01:00
status.setCard(Helper.restoreCardFromString(c.getString(c.getColumnIndex(Sqlite.COL_CARD))));
status.setIn_reply_to_id(c.getString(c.getColumnIndex(Sqlite.COL_IN_REPLY_TO_ID)));
status.setIn_reply_to_account_id(c.getString(c.getColumnIndex(Sqlite.COL_IN_REPLY_TO_ACCOUNT_ID)));
status.setReblog(Helper.restoreStatusFromString(c.getString(c.getColumnIndex(Sqlite.COL_REBLOG))));
2019-11-30 08:53:37 +01:00
status.setContent(context, c.getString(c.getColumnIndex(Sqlite.COL_CONTENT)));
status.setCreated_at(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT))));
status.setEmojis(Helper.restoreEmojisFromString(c.getString(c.getColumnIndex(Sqlite.COL_EMOJIS))));
status.setPoll(Helper.restorePollFromString(c.getString(c.getColumnIndex(Sqlite.COL_POLL))));
status.setReblogs_count(c.getInt(c.getColumnIndex(Sqlite.COL_REBLOGS_COUNT)));
status.setFavourites_count(c.getInt(c.getColumnIndex(Sqlite.COL_FAVOURITES_COUNT)));
2019-09-06 17:55:14 +02:00
status.setReblogged(c.getInt(c.getColumnIndex(Sqlite.COL_REBLOGGED)) == 1);
status.setFavourited(c.getInt(c.getColumnIndex(Sqlite.COL_FAVOURITED)) == 1);
status.setMuted(c.getInt(c.getColumnIndex(Sqlite.COL_MUTED)) == 1);
status.setSensitive(c.getInt(c.getColumnIndex(Sqlite.COL_SENSITIVE)) == 1);
status.setPinned(c.getInt(c.getColumnIndex(Sqlite.COL_PINNED)) == 1);
status.setSpoiler_text(c.getString(c.getColumnIndex(Sqlite.COL_SPOILER_TEXT)));
status.setVisibility(c.getString(c.getColumnIndex(Sqlite.COL_VISIBILITY)));
status.setMedia_attachments(Helper.restoreAttachmentFromString(c.getString(c.getColumnIndex(Sqlite.COL_MEDIA_ATTACHMENTS))));
status.setMentions(Helper.restoreMentionFromString(c.getString(c.getColumnIndex(Sqlite.COL_MENTIONS))));
status.setTags(Helper.restoreTagFromString(c.getString(c.getColumnIndex(Sqlite.COL_TAGS))));
status.setApplication(Helper.restoreApplicationFromString(c.getString(c.getColumnIndex(Sqlite.COL_APPLICATION))));
status.setLanguage(c.getString(c.getColumnIndex(Sqlite.COL_LANGUAGE)));
2018-02-15 07:55:24 +01:00
//Close the cursor
c.close();
//Cached status is returned
return status;
2018-02-15 07:55:24 +01:00
}
/***
* Method to hydrate cached statuses from database
2018-02-15 07:55:24 +01:00
* @param c Cursor
* @return List<Status>
2018-02-15 07:55:24 +01:00
*/
2019-09-06 17:55:14 +02:00
private List<Status> cursorToListStatuses(Cursor c) {
2018-02-15 07:55:24 +01:00
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
2018-02-15 07:55:24 +01:00
return null;
2019-08-17 14:34:33 +02:00
}
List<Status> statuses = new ArrayList<>();
2019-09-06 17:55:14 +02:00
while (c.moveToNext()) {
//Restore cached status
Status status = new Status();
2019-08-18 19:22:12 +02:00
status.setViewType(context);
status.setId(c.getString(c.getColumnIndex(Sqlite.COL_STATUS_ID)));
status.setUri(c.getString(c.getColumnIndex(Sqlite.COL_URI)));
status.setUrl(c.getString(c.getColumnIndex(Sqlite.COL_URL)));
status.setAccount(Helper.restoreAccountFromString(c.getString(c.getColumnIndex(Sqlite.COL_ACCOUNT))));
2018-11-18 16:01:46 +01:00
status.setCard(Helper.restoreCardFromString(c.getString(c.getColumnIndex(Sqlite.COL_CARD))));
status.setIn_reply_to_id(c.getString(c.getColumnIndex(Sqlite.COL_IN_REPLY_TO_ID)));
status.setIn_reply_to_account_id(c.getString(c.getColumnIndex(Sqlite.COL_IN_REPLY_TO_ACCOUNT_ID)));
status.setReblog(Helper.restoreStatusFromString(c.getString(c.getColumnIndex(Sqlite.COL_REBLOG))));
2019-11-30 08:53:37 +01:00
status.setContent(context, c.getString(c.getColumnIndex(Sqlite.COL_CONTENT)));
status.setCreated_at(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT))));
status.setEmojis(Helper.restoreEmojisFromString(c.getString(c.getColumnIndex(Sqlite.COL_EMOJIS))));
status.setPoll(Helper.restorePollFromString(c.getString(c.getColumnIndex(Sqlite.COL_POLL))));
status.setReblogs_count(c.getInt(c.getColumnIndex(Sqlite.COL_REBLOGS_COUNT)));
status.setFavourites_count(c.getInt(c.getColumnIndex(Sqlite.COL_FAVOURITES_COUNT)));
2019-09-06 17:55:14 +02:00
status.setReblogged(c.getInt(c.getColumnIndex(Sqlite.COL_REBLOGGED)) == 1);
status.setFavourited(c.getInt(c.getColumnIndex(Sqlite.COL_FAVOURITED)) == 1);
status.setMuted(c.getInt(c.getColumnIndex(Sqlite.COL_MUTED)) == 1);
status.setSensitive(c.getInt(c.getColumnIndex(Sqlite.COL_SENSITIVE)) == 1);
status.setPinned(c.getInt(c.getColumnIndex(Sqlite.COL_PINNED)) == 1);
status.setSpoiler_text(c.getString(c.getColumnIndex(Sqlite.COL_SPOILER_TEXT)));
status.setVisibility(c.getString(c.getColumnIndex(Sqlite.COL_VISIBILITY)));
status.setMedia_attachments(Helper.restoreAttachmentFromString(c.getString(c.getColumnIndex(Sqlite.COL_MEDIA_ATTACHMENTS))));
status.setMentions(Helper.restoreMentionFromString(c.getString(c.getColumnIndex(Sqlite.COL_MENTIONS))));
status.setTags(Helper.restoreTagFromString(c.getString(c.getColumnIndex(Sqlite.COL_TAGS))));
status.setApplication(Helper.restoreApplicationFromString(c.getString(c.getColumnIndex(Sqlite.COL_APPLICATION))));
status.setLanguage(c.getString(c.getColumnIndex(Sqlite.COL_LANGUAGE)));
statuses.add(status);
2018-02-15 07:55:24 +01:00
}
//Close the cursor
c.close();
//Statuses list is returned
return statuses;
2018-02-15 07:55:24 +01:00
}
2019-01-02 13:31:11 +01:00
/***
* Method to get cached statuses ID from database
* @param c Cursor
* @return List<Status>
*/
2019-09-06 17:55:14 +02:00
private List<String> cursorToListStatusesId(Cursor c) {
2019-01-02 13:31:11 +01:00
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
2019-01-02 13:31:11 +01:00
return null;
2019-08-17 14:34:33 +02:00
}
2019-01-02 13:31:11 +01:00
List<String> statusesId = new ArrayList<>();
2019-09-06 17:55:14 +02:00
while (c.moveToNext()) {
2019-01-02 13:31:11 +01:00
//Restore cached status
statusesId.add(c.getString(c.getColumnIndex(Sqlite.COL_STATUS_ID)));
}
//Close the cursor
c.close();
//Statuses list is returned
return statusesId;
}
2018-02-15 07:55:24 +01:00
}