fedilab-Android-App/app/src/main/java/app/fedilab/android/jobs/ScheduledTootsSyncJob.java

98 lines
3.7 KiB
Java
Raw Normal View History

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;
import android.support.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";
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
@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();
SQLiteDatabase db = Sqlite.getInstance(getContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
//Retrieves the stored status
2017-07-17 15:22:59 +02:00
StoredStatus storedStatus = new StatusStoredDAO(getContext(), db).getStatusScheduled(jobId);
2017-07-16 17:09:35 +02:00
if( storedStatus != null){
2017-07-17 15:22:59 +02:00
String userId = storedStatus.getUserId();
String instance = storedStatus.getInstance();
if( instance != null && userId != null){
2019-06-05 14:35:42 +02:00
Account account = new AccountDAO(getContext(), db).getUniqAccount(userId, instance);
2017-07-17 15:22:59 +02:00
if( account != null){
//Retrieves the linked status to toot
Status status = storedStatus.getStatus();
if( status != null){
int statusCode = new API(getContext(), account.getInstance(), account.getToken()).statusAction(status);
//Toot was sent
if( statusCode == 200){
new StatusStoredDAO(getContext(), db).updateScheduledDone(jobId, new Date());
}
}
2017-07-16 17:09:35 +02:00
}
}
}
return Result.SUCCESS;
}
2017-07-17 18:53:12 +02:00
public static int schedule(Context context, long id, long timestampScheduling){
2017-07-16 17:09:35 +02:00
long startMs = (timestampScheduling - new Date().getTime());
long endMs = startMs + TimeUnit.MINUTES.toMillis(5);
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
int jobId = new JobRequest.Builder(ScheduledTootsSyncJob.SCHEDULED_TOOT)
.setExecutionWindow(startMs, endMs)
2017-07-17 18:53:12 +02:00
.setUpdateCurrent(false)
2017-10-18 14:50:52 +02:00
.setRequiredNetworkType(JobRequest.NetworkType.METERED)
2017-07-16 17:09:35 +02:00
.setRequirementsEnforced(false)
.build()
.schedule();
2017-07-16 19:04:53 +02:00
new StatusStoredDAO(context, db).scheduleStatus(id, jobId, new Date(timestampScheduling));
2017-07-16 17:09:35 +02:00
return jobId;
}
}