fedilab-Android-App/app/src/main/java/app/fedilab/android/asynctasks/RetrieveFeedsAsyncTask.java

403 lines
18 KiB
Java
Raw Normal View History

2017-05-05 16:36:04 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-05-05 16:36:04 +02: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
2017-05-05 16:36:04 +02: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,
2017-05-05 16:36:04 +02:00
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.asynctasks;
2017-05-05 16:36:04 +02:00
import android.content.Context;
2018-02-15 16:59:52 +01:00
import android.database.sqlite.SQLiteDatabase;
2017-05-05 16:36:04 +02:00
import android.os.AsyncTask;
2017-10-27 15:09:26 +02:00
2019-05-11 19:58:41 +02:00
2017-10-27 15:09:26 +02:00
import java.lang.ref.WeakReference;
2018-02-15 16:59:52 +01:00
import java.util.List;
2017-10-27 15:09:26 +02:00
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.API;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.ManageTimelines;
import app.fedilab.android.client.Entities.Peertube;
import app.fedilab.android.client.Entities.RemoteInstance;
import app.fedilab.android.client.GNUAPI;
import app.fedilab.android.client.PeertubeAPI;
import app.fedilab.android.helper.FilterToots;
import app.fedilab.android.sqlite.InstancesDAO;
import app.fedilab.android.sqlite.PeertubeFavoritesDAO;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.StatusCacheDAO;
import app.fedilab.android.sqlite.TimelinesDAO;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.interfaces.OnRetrieveFeedsInterface;
2017-05-05 16:36:04 +02:00
/**
* Created by Thomas on 23/04/2017.
* Retrieves toots on the instance
*/
public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
2017-10-27 15:09:26 +02:00
2017-05-05 16:36:04 +02:00
private Type action;
2017-06-03 18:18:27 +02:00
private APIResponse apiResponse;
2017-05-05 16:36:04 +02:00
private String max_id;
private OnRetrieveFeedsInterface listener;
private String targetedID;
private String tag;
private boolean showMediaOnly = false;
private boolean showPinned = false;
2018-09-05 10:19:07 +02:00
private boolean showReply = false;
2017-10-27 15:09:26 +02:00
private WeakReference<Context> contextReference;
2018-02-17 14:43:12 +01:00
private FilterToots filterToots;
2018-10-22 19:19:39 +02:00
private String instanceName,remoteInstance, name;
private boolean cached = false;
2019-04-26 15:50:26 +02:00
private int timelineId;
2018-11-17 14:44:49 +01:00
2017-05-05 16:36:04 +02:00
public enum Type{
HOME,
LOCAL,
2018-10-10 08:33:36 +02:00
DIRECT,
2018-10-29 15:49:13 +01:00
CONVERSATION,
2017-05-05 16:36:04 +02:00
PUBLIC,
HASHTAG,
2017-12-14 18:28:06 +01:00
LIST,
2017-05-05 16:36:04 +02:00
USER,
FAVOURITES,
ONESTATUS,
CONTEXT,
2018-02-15 15:27:46 +01:00
TAG,
2018-12-05 12:04:49 +01:00
REMOTE_INSTANCE,
2018-12-22 16:03:27 +01:00
ART,
2019-04-21 09:12:10 +02:00
PEERTUBE,
2018-12-26 15:43:36 +01:00
NOTIFICATION,
2019-03-31 11:58:40 +02:00
SEARCH,
2019-02-03 09:27:02 +01:00
2019-01-03 12:15:42 +01:00
PSUBSCRIPTIONS,
POVERVIEW,
PTRENDING,
PRECENTLYADDED,
2019-01-07 17:00:03 +01:00
PMYVIDEOS,
2019-01-03 18:59:44 +01:00
PLOCAL,
2019-01-09 17:30:18 +01:00
CHANNEL,
2019-01-14 11:50:42 +01:00
MYVIDEOS,
2019-02-03 09:27:02 +01:00
PIXELFED,
2019-01-14 11:50:42 +01:00
PF_HOME,
PF_LOCAL,
2019-01-15 16:36:30 +01:00
PF_DISCOVER,
2019-01-19 19:41:55 +01:00
PF_NOTIFICATION,
2019-02-03 09:27:02 +01:00
GNU_HOME,
GNU_LOCAL,
GNU_WHOLE,
GNU_NOTIFICATION,
GNU_DM,
GNU_ART,
GNU_TAG,
SCHEDULED_TOOTS,
CACHE_BOOKMARKS,
CACHE_BOOKMARKS_PEERTUBE,
CACHE_STATUS,
2017-05-05 16:36:04 +02:00
}
2018-02-17 14:43:12 +01:00
public RetrieveFeedsAsyncTask(Context context, FilterToots filterToots, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
this.contextReference = new WeakReference<>(context);
this.action = Type.CACHE_STATUS;
this.max_id = max_id;
this.listener = onRetrieveFeedsInterface;
this.filterToots = filterToots;
}
2019-04-22 13:16:35 +02:00
2017-05-05 16:36:04 +02:00
public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
2017-10-27 15:09:26 +02:00
this.contextReference = new WeakReference<>(context);
2017-05-05 16:36:04 +02:00
this.action = action;
this.max_id = max_id;
this.listener = onRetrieveFeedsInterface;
}
2018-08-20 19:00:20 +02:00
public RetrieveFeedsAsyncTask(Context context, Type action, String instanceName, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
this.contextReference = new WeakReference<>(context);
this.action = action;
this.max_id = max_id;
this.listener = onRetrieveFeedsInterface;
this.instanceName = instanceName;
}
2019-04-26 15:50:26 +02:00
public RetrieveFeedsAsyncTask(Context context, Type action, int timelineId, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
this.contextReference = new WeakReference<>(context);
this.action = action;
this.max_id = max_id;
this.listener = onRetrieveFeedsInterface;
this.timelineId = timelineId;
}
public RetrieveFeedsAsyncTask(Context context, Type action, String targetedID, String max_id, boolean showMediaOnly, boolean showPinned, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
2017-10-27 15:09:26 +02:00
this.contextReference = new WeakReference<>(context);
2017-05-05 16:36:04 +02:00
this.action = action;
this.max_id = max_id;
this.listener = onRetrieveFeedsInterface;
this.targetedID = targetedID;
this.showMediaOnly = showMediaOnly;
this.showPinned = showPinned;
2017-05-05 16:36:04 +02:00
}
2018-09-05 10:19:07 +02:00
public RetrieveFeedsAsyncTask(Context context, Type action, String targetedID, String max_id, boolean showMediaOnly, boolean showPinned, boolean showReply, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
this.contextReference = new WeakReference<>(context);
this.action = action;
this.max_id = max_id;
this.listener = onRetrieveFeedsInterface;
this.targetedID = targetedID;
this.showMediaOnly = showMediaOnly;
this.showPinned = showPinned;
this.showReply = showReply;
}
2017-05-05 16:36:04 +02:00
public RetrieveFeedsAsyncTask(Context context, Type action, String tag, String targetedID, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
2017-10-27 15:09:26 +02:00
this.contextReference = new WeakReference<>(context);
2017-05-05 16:36:04 +02:00
this.action = action;
this.max_id = max_id;
this.listener = onRetrieveFeedsInterface;
this.targetedID = targetedID;
this.tag = tag;
}
2018-10-22 19:19:39 +02:00
public RetrieveFeedsAsyncTask(Context context, String remoteInstance, String name, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface){
this.contextReference = new WeakReference<>(context);
this.remoteInstance = remoteInstance;
this.max_id = max_id;
this.listener = onRetrieveFeedsInterface;
this.name = name;
this.action = Type.REMOTE_INSTANCE;
}
2017-05-05 16:36:04 +02:00
@Override
protected Void doInBackground(Void... params) {
2017-10-27 15:09:26 +02:00
API api = new API(this.contextReference.get());
2018-10-22 19:19:39 +02:00
SQLiteDatabase db = Sqlite.getInstance(this.contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2019-04-22 13:16:35 +02:00
if( action == null )
return null;
2017-05-05 16:36:04 +02:00
switch (action){
case HOME:
2019-05-12 09:53:10 +02:00
apiResponse = api.getHomeTimelineCache(max_id);
2017-05-05 16:36:04 +02:00
break;
case LOCAL:
2017-06-03 18:18:27 +02:00
apiResponse = api.getPublicTimeline(true, max_id);
2017-05-05 16:36:04 +02:00
break;
case PUBLIC:
2017-06-03 18:18:27 +02:00
apiResponse = api.getPublicTimeline(false, max_id);
2017-05-05 16:36:04 +02:00
break;
2019-01-19 19:41:55 +01:00
case SCHEDULED_TOOTS:
2019-01-20 15:20:07 +01:00
apiResponse = api.scheduledAction("GET", null, max_id, null);
2019-01-19 19:41:55 +01:00
break;
2018-10-10 08:33:36 +02:00
case DIRECT:
apiResponse = api.getDirectTimeline(max_id);
break;
2018-10-29 15:49:13 +01:00
case CONVERSATION:
apiResponse = api.getConversationTimeline(max_id);
break;
2018-08-20 19:00:20 +02:00
case REMOTE_INSTANCE:
2018-10-22 19:19:39 +02:00
if( this.name != null && this.remoteInstance != null){ //For Peertube channels
apiResponse = api.getPeertubeChannelVideos(this.remoteInstance, this.name);
}else{ //For other remote instance
List<RemoteInstance> remoteInstanceObj = new InstancesDAO(this.contextReference.get(), db).getInstanceByName(this.instanceName);
if( remoteInstanceObj != null && remoteInstanceObj.size() > 0 && remoteInstanceObj.get(0).getType().equals("MASTODON")) {
apiResponse = api.getPublicTimeline(this.instanceName, false, max_id);
2019-05-18 11:10:30 +02:00
List<app.fedilab.android.client.Entities.Status> statusesTemp = apiResponse.getStatuses();
2018-10-22 19:19:39 +02:00
if( statusesTemp != null){
2019-05-18 11:10:30 +02:00
for(app.fedilab.android.client.Entities.Status status: statusesTemp){
2018-10-22 19:19:39 +02:00
status.setType(action);
}
2018-10-07 10:45:34 +02:00
}
2018-12-29 12:09:02 +01:00
}else if(remoteInstanceObj != null && remoteInstanceObj.size() > 0 && remoteInstanceObj.get(0).getType().equals("MISSKEY")){
apiResponse = api.getMisskey(this.instanceName, max_id);
2019-05-18 11:10:30 +02:00
List<app.fedilab.android.client.Entities.Status> statusesTemp = apiResponse.getStatuses();
2018-12-29 12:09:02 +01:00
if( statusesTemp != null){
2019-05-18 11:10:30 +02:00
for(app.fedilab.android.client.Entities.Status status: statusesTemp){
2018-12-29 12:09:02 +01:00
status.setType(action);
}
}
} else if(remoteInstanceObj != null && remoteInstanceObj.size() > 0 && remoteInstanceObj.get(0).getType().equals("PIXELFED") ) {
apiResponse = api.getPixelfedTimeline(instanceName, max_id);
2019-05-11 19:58:41 +02:00
} else if(remoteInstanceObj != null && remoteInstanceObj.size() > 0 && remoteInstanceObj.get(0).getType().equals("GNU") ) {
apiResponse = api.getGNUTimeline(instanceName, max_id);
}else {
2018-10-22 19:19:39 +02:00
apiResponse = api.getPeertube(this.instanceName, max_id);
}
}
2018-08-20 19:00:20 +02:00
break;
2017-05-05 16:36:04 +02:00
case FAVOURITES:
2019-02-08 15:07:46 +01:00
if( MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU && MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
2019-02-05 18:05:21 +01:00
apiResponse = api.getFavourites(max_id);
}else{
GNUAPI gnuapi = new GNUAPI(this.contextReference.get());
apiResponse = gnuapi.getFavourites(max_id);
}
2017-05-05 16:36:04 +02:00
break;
case USER:
2019-01-27 10:40:09 +01:00
if(MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {
2019-01-03 17:11:53 +01:00
if (showMediaOnly)
apiResponse = api.getStatusWithMedia(targetedID, max_id);
else if (showPinned)
apiResponse = api.getPinnedStatuses(targetedID, max_id);
else
apiResponse = api.getAccountTLStatuses(targetedID, max_id, !showReply);
2019-02-08 15:07:46 +01:00
}else if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.GNU || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA){
2019-02-03 16:06:06 +01:00
GNUAPI gnuapi = new GNUAPI(this.contextReference.get());
if (showMediaOnly)
apiResponse = gnuapi.getStatusWithMedia(targetedID, max_id);
else if (showPinned)
apiResponse = gnuapi.getPinnedStatuses(targetedID, max_id);
else
apiResponse = gnuapi.getAccountTLStatuses(targetedID, max_id, !showReply);
2019-01-03 17:11:53 +01:00
}else if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE){
PeertubeAPI peertubeAPI = new PeertubeAPI(this.contextReference.get());
apiResponse = peertubeAPI.getVideos(targetedID, max_id);
}
2017-05-05 16:36:04 +02:00
break;
2019-01-09 17:30:18 +01:00
case MYVIDEOS:
2019-01-03 18:59:44 +01:00
PeertubeAPI peertubeAPI = new PeertubeAPI(this.contextReference.get());
2019-01-09 17:30:18 +01:00
apiResponse = peertubeAPI.getMyVideos(max_id);
break;
case CHANNEL:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
2019-01-03 18:59:44 +01:00
apiResponse = peertubeAPI.getVideosChannel(targetedID, max_id);
break;
2017-05-05 16:36:04 +02:00
case ONESTATUS:
2017-06-03 18:18:27 +02:00
apiResponse = api.getStatusbyId(targetedID);
2017-05-05 16:36:04 +02:00
break;
2019-03-31 15:15:46 +02:00
case SEARCH:
apiResponse = api.search2(tag, API.searchType.STATUSES, max_id);
break;
2017-05-05 16:36:04 +02:00
case TAG:
2019-02-08 15:07:46 +01:00
if( MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU && MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
2019-04-26 15:50:26 +02:00
ManageTimelines manageTimelines = new TimelinesDAO(contextReference.get(), db).getById(timelineId);
if( manageTimelines != null && manageTimelines.getTagTimeline() != null){
boolean isArt = manageTimelines.getTagTimeline().isART();
2019-02-03 19:18:04 +01:00
if (isArt)
2019-04-26 15:50:26 +02:00
apiResponse = api.getCustomArtTimeline(false, manageTimelines.getTagTimeline().getName(), max_id, manageTimelines.getTagTimeline().getAny(), manageTimelines.getTagTimeline().getAll(), manageTimelines.getTagTimeline().getNone());
2019-02-03 19:18:04 +01:00
else
2019-04-26 15:50:26 +02:00
apiResponse = api.getPublicTimelineTag(manageTimelines.getTagTimeline().getName(), false, max_id, manageTimelines.getTagTimeline().getAny(), manageTimelines.getTagTimeline().getAll(), manageTimelines.getTagTimeline().getNone());
}else{
apiResponse = api.getPublicTimelineTag(tag, false, max_id, null, null, null);
2019-02-03 19:18:04 +01:00
}
2018-12-15 12:21:03 +01:00
}else{
2019-02-03 19:18:04 +01:00
GNUAPI gnuapi = new GNUAPI(this.contextReference.get());
apiResponse = gnuapi.search(tag,max_id);
2018-12-15 12:21:03 +01:00
}
2017-05-05 16:36:04 +02:00
break;
2018-12-05 12:04:49 +01:00
case ART:
2018-12-27 18:40:18 +01:00
apiResponse = api.getArtTimeline(false, max_id, null, null, null);
2018-12-05 12:04:49 +01:00
break;
2018-02-15 16:59:52 +01:00
case CACHE_BOOKMARKS:
apiResponse = new APIResponse();
2018-10-07 10:45:34 +02:00
db = Sqlite.getInstance(contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2019-05-18 11:10:30 +02:00
List<app.fedilab.android.client.Entities.Status> statuses = new StatusCacheDAO(contextReference.get(), db).getAllStatus(StatusCacheDAO.BOOKMARK_CACHE);
2018-02-15 16:59:52 +01:00
apiResponse.setStatuses(statuses);
break;
2018-10-21 18:43:57 +02:00
case CACHE_BOOKMARKS_PEERTUBE:
apiResponse = new APIResponse();
db = Sqlite.getInstance(contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<Peertube> peertubes = new PeertubeFavoritesDAO(contextReference.get(), db).getAllPeertube();
apiResponse.setPeertubes(peertubes);
break;
2018-02-17 11:28:52 +01:00
case CACHE_STATUS:
apiResponse = new APIResponse();
db = Sqlite.getInstance(contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2018-02-17 14:43:12 +01:00
statuses = new StatusCacheDAO(contextReference.get(), db).getStatusFromID(StatusCacheDAO.ARCHIVE_CACHE, filterToots, max_id);
2018-02-17 11:28:52 +01:00
if( statuses != null && statuses.size() > 0) {
apiResponse.setStatuses(statuses);
2018-02-17 18:08:06 +01:00
apiResponse.setSince_id(statuses.get(0).getId());
apiResponse.setMax_id(statuses.get(statuses.size() - 1).getId());
2018-02-17 11:28:52 +01:00
}else{
apiResponse.setStatuses(null);
apiResponse.setMax_id(null);
apiResponse.setSince_id(null);
}
break;
2019-01-03 14:23:13 +01:00
case PSUBSCRIPTIONS:
2019-01-03 18:59:44 +01:00
peertubeAPI = new PeertubeAPI(this.contextReference.get());
2019-01-03 14:23:13 +01:00
apiResponse = peertubeAPI.getSubscriptionsTL(max_id);
break;
case POVERVIEW:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
apiResponse = peertubeAPI.getOverviewTL(max_id);
break;
case PTRENDING:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
apiResponse = peertubeAPI.getTrendingTL(max_id);
break;
case PRECENTLYADDED:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
apiResponse = peertubeAPI.getRecentlyAddedTL(max_id);
break;
case PLOCAL:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
2019-01-07 17:00:03 +01:00
apiResponse = peertubeAPI.getLocalTL(max_id);
break;
case PMYVIDEOS:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
2019-01-03 14:23:13 +01:00
apiResponse = peertubeAPI.getLocalTL(max_id);
break;
2019-01-15 15:41:30 +01:00
case PF_HOME:
api = new API(this.contextReference.get());
apiResponse = api.getHomeTimeline(max_id);
break;
case PF_LOCAL:
api = new API(this.contextReference.get());
apiResponse = api.getPublicTimeline(true,max_id);
case PF_DISCOVER:
api = new API(this.contextReference.get());
apiResponse = api.getDiscoverTimeline(true,max_id);
break;
2017-05-05 16:36:04 +02:00
case HASHTAG:
break;
2019-02-03 09:27:02 +01:00
case GNU_HOME:
GNUAPI gnuAPI = new GNUAPI(this.contextReference.get());
apiResponse = gnuAPI.getHomeTimeline(max_id);
break;
case GNU_LOCAL:
gnuAPI = new GNUAPI(this.contextReference.get());
2019-02-03 11:15:21 +01:00
apiResponse = gnuAPI.getPublicTimeline(true,max_id);
2019-02-03 09:27:02 +01:00
break;
case GNU_WHOLE:
gnuAPI = new GNUAPI(this.contextReference.get());
2019-02-03 11:15:21 +01:00
apiResponse = gnuAPI.getPublicTimeline(false,max_id);
2019-02-03 09:27:02 +01:00
break;
case GNU_DM:
gnuAPI = new GNUAPI(this.contextReference.get());
2019-02-27 09:54:38 +01:00
apiResponse = gnuAPI.getDirectTimeline(max_id);
2019-02-03 09:27:02 +01:00
break;
2017-05-05 16:36:04 +02:00
}
2019-01-27 10:40:09 +01:00
if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {
2019-01-03 17:11:53 +01:00
List<String> bookmarks = new StatusCacheDAO(contextReference.get(), db).getAllStatusId(StatusCacheDAO.BOOKMARK_CACHE);
if (apiResponse != null && apiResponse.getStatuses() != null && bookmarks != null && apiResponse.getStatuses().size() > 0) {
2019-05-18 11:10:30 +02:00
List<app.fedilab.android.client.Entities.Status> statuses = apiResponse.getStatuses();
for (app.fedilab.android.client.Entities.Status status : statuses) {
2019-01-03 17:11:53 +01:00
status.setBookmarked(bookmarks.contains(status.getId()));
}
}
2019-01-02 13:31:11 +01:00
}
2017-05-05 16:36:04 +02:00
return null;
}
@Override
protected void onPostExecute(Void result) {
listener.onRetrieveFeeds(apiResponse);
2017-05-05 16:36:04 +02:00
}
}