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

107 lines
4.8 KiB
Java
Raw Normal View History

2017-07-16 17:09:35 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-07-16 17:09:35 +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-07-16 17:09:35 +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-07-16 17:09:35 +02:00
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.asynctasks;
2017-07-16 17:09:35 +02:00
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
2021-01-19 17:43:51 +01:00
import android.os.Handler;
import android.os.Looper;
2017-07-17 18:53:12 +02:00
import android.os.SystemClock;
2017-07-17 18:53:12 +02:00
import com.evernote.android.job.JobManager;
import com.evernote.android.job.JobRequest;
2017-10-27 15:09:26 +02:00
import java.lang.ref.WeakReference;
2017-07-16 17:09:35 +02:00
import java.util.List;
2017-07-17 18:53:12 +02:00
import java.util.Set;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.StoredStatus;
import app.fedilab.android.fragments.DisplayScheduledTootsFragment;
import app.fedilab.android.helper.Helper;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.interfaces.OnRetrieveScheduledTootsInterface;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.jobs.ScheduledBoostsSyncJob;
import app.fedilab.android.jobs.ScheduledTootsSyncJob;
import app.fedilab.android.sqlite.BoostScheduleDAO;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.StatusStoredDAO;
2017-07-16 17:09:35 +02:00
/**
* Created by Thomas on 16/07/2017.
* Retrieves scheduled toots for an account
*/
2021-01-19 17:43:51 +01:00
public class RetrieveScheduledTootsAsyncTask {
2017-07-16 17:09:35 +02:00
2017-10-27 15:09:26 +02:00
2021-01-19 17:43:51 +01:00
private final OnRetrieveScheduledTootsInterface listener;
private final WeakReference<Context> contextReference;
private final DisplayScheduledTootsFragment.typeOfSchedule type;
2017-07-16 17:09:35 +02:00
private List<StoredStatus> storedStatuses;
2019-09-06 17:55:14 +02:00
public RetrieveScheduledTootsAsyncTask(Context context, DisplayScheduledTootsFragment.typeOfSchedule type, OnRetrieveScheduledTootsInterface onRetrieveScheduledTootsInterface) {
2017-10-27 15:09:26 +02:00
this.contextReference = new WeakReference<>(context);
2017-07-16 17:09:35 +02:00
this.listener = onRetrieveScheduledTootsInterface;
this.type = type;
2021-01-19 17:43:51 +01:00
doInBackground();
2017-07-16 17:09:35 +02:00
}
2021-01-19 17:43:51 +01:00
protected void doInBackground() {
new Thread(() -> {
SQLiteDatabase db = Sqlite.getInstance(this.contextReference.get().getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
//Retrieves job asked by the user
if (type == DisplayScheduledTootsFragment.typeOfSchedule.TOOT)
storedStatuses = new StatusStoredDAO(this.contextReference.get(), db).getAllScheduled();
else if (type == DisplayScheduledTootsFragment.typeOfSchedule.BOOST)
storedStatuses = new BoostScheduleDAO(this.contextReference.get(), db).getAllScheduled();
//Retrieves real jobs still waiting
Set<JobRequest> jobRequests = null;
if (type == DisplayScheduledTootsFragment.typeOfSchedule.TOOT)
jobRequests = JobManager.instance().getAllJobRequestsForTag(ScheduledTootsSyncJob.SCHEDULED_TOOT);
else if (type == DisplayScheduledTootsFragment.typeOfSchedule.BOOST)
jobRequests = JobManager.instance().getAllJobRequestsForTag(ScheduledBoostsSyncJob.SCHEDULED_BOOST);
int[] jobIds;
if (jobRequests != null && jobRequests.size() > 0) {
int i = 0;
jobIds = new int[jobRequests.size()];
for (JobRequest jobRequest : jobRequests) {
jobIds[i] = jobRequest.getJobId();
i++;
}
} else {
jobIds = new int[]{};
2017-07-17 18:53:12 +02:00
}
2021-01-19 17:43:51 +01:00
if (storedStatuses != null && storedStatuses.size() > 0) {
for (StoredStatus ss : storedStatuses) {
if (!Helper.isJobPresent(jobIds, ss.getJobId())) {
//JobId is fixed to -1 which means an error occured (it was never sent)
if (type == DisplayScheduledTootsFragment.typeOfSchedule.TOOT)
new StatusStoredDAO(this.contextReference.get(), db).updateJobId(ss.getId(), -1);
else if (type == DisplayScheduledTootsFragment.typeOfSchedule.BOOST)
new BoostScheduleDAO(this.contextReference.get(), db).updateJobId(ss.getId(), -1);
}
2017-07-17 18:53:12 +02:00
}
2021-01-19 17:43:51 +01:00
//Lets time to update db before dispaying
SystemClock.sleep(1000);
2017-07-17 18:53:12 +02:00
}
2021-01-19 17:43:51 +01:00
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> listener.onRetrieveScheduledToots(storedStatuses);
mainHandler.post(myRunnable);
}).start();
2017-07-16 17:09:35 +02:00
}
}