2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.jobs;
|
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>. */
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2019-06-11 19:38:26 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2017-07-17 18:53:12 +02:00
|
|
|
|
2017-07-16 17:09:35 +02:00
|
|
|
import com.evernote.android.job.Job;
|
|
|
|
import com.evernote.android.job.JobRequest;
|
2017-12-12 18:17:01 +01:00
|
|
|
|
2017-07-16 17:09:35 +02:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.client.API;
|
|
|
|
import app.fedilab.android.client.Entities.Account;
|
|
|
|
import app.fedilab.android.client.Entities.Status;
|
|
|
|
import app.fedilab.android.client.Entities.StoredStatus;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
|
|
|
import app.fedilab.android.sqlite.AccountDAO;
|
|
|
|
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.
|
|
|
|
* Scheduled a toot a datetime
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class ScheduledTootsSyncJob extends Job {
|
|
|
|
|
2017-07-17 18:53:12 +02:00
|
|
|
public static final String SCHEDULED_TOOT = "job_scheduled_toot";
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2017-12-12 19:27:33 +01:00
|
|
|
static {
|
2017-12-14 07:26:37 +01:00
|
|
|
Helper.installProvider();
|
2017-12-12 19:27:33 +01:00
|
|
|
}
|
2017-07-16 17:09:35 +02:00
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
public static int schedule(Context context, long id, long timestampScheduling) {
|
|
|
|
|
|
|
|
long startMs = (timestampScheduling - new Date().getTime());
|
|
|
|
long endMs = startMs + TimeUnit.MINUTES.toMillis(5);
|
2020-04-09 18:57:12 +02:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
2019-11-15 16:32:25 +01:00
|
|
|
|
|
|
|
int jobId = new JobRequest.Builder(ScheduledTootsSyncJob.SCHEDULED_TOOT)
|
|
|
|
.setExecutionWindow(startMs, endMs)
|
|
|
|
.setUpdateCurrent(false)
|
|
|
|
.setRequiredNetworkType(JobRequest.NetworkType.METERED)
|
|
|
|
.setRequirementsEnforced(false)
|
|
|
|
.build()
|
|
|
|
.schedule();
|
|
|
|
new StatusStoredDAO(context, db).scheduleStatus(id, jobId, new Date(timestampScheduling));
|
|
|
|
return jobId;
|
|
|
|
}
|
|
|
|
|
2017-07-16 17:09:35 +02:00
|
|
|
@NonNull
|
|
|
|
@Override
|
2017-12-17 14:20:04 +01:00
|
|
|
protected Result onRunJob(@NonNull Params params) {
|
2017-07-16 17:09:35 +02:00
|
|
|
//Code refresh here
|
|
|
|
int jobId = params.getId();
|
2020-04-09 18:57:12 +02:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(getContext().getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
2017-07-16 17:09:35 +02:00
|
|
|
//Retrieves the stored status
|
2017-07-17 15:22:59 +02:00
|
|
|
StoredStatus storedStatus = new StatusStoredDAO(getContext(), db).getStatusScheduled(jobId);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (storedStatus != null) {
|
2017-07-17 15:22:59 +02:00
|
|
|
String userId = storedStatus.getUserId();
|
|
|
|
String instance = storedStatus.getInstance();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (instance != null && userId != null) {
|
2019-06-05 14:35:42 +02:00
|
|
|
Account account = new AccountDAO(getContext(), db).getUniqAccount(userId, instance);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (account != null) {
|
2017-07-17 15:22:59 +02:00
|
|
|
//Retrieves the linked status to toot
|
|
|
|
Status status = storedStatus.getStatus();
|
2019-09-06 17:55:14 +02:00
|
|
|
if (status != null) {
|
2017-07-17 15:22:59 +02:00
|
|
|
int statusCode = new API(getContext(), account.getInstance(), account.getToken()).statusAction(status);
|
|
|
|
//Toot was sent
|
2019-09-06 17:55:14 +02:00
|
|
|
if (statusCode == 200) {
|
2017-07-17 15:22:59 +02:00
|
|
|
new StatusStoredDAO(getContext(), db).updateScheduledDone(jobId, new Date());
|
|
|
|
}
|
|
|
|
}
|
2017-07-16 17:09:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result.SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|