diff options
author | stom79 <tschneider.ac@gmail.com> | 2018-12-09 13:59:40 +0100 |
---|---|---|
committer | stom79 <tschneider.ac@gmail.com> | 2018-12-09 13:59:40 +0100 |
commit | d4fcb62c53a0cb27edf8ae5453db2d60c292a540 (patch) | |
tree | 8ebee345ced38ec6d940c13cf112edb78ae3efe3 /app/src/main/java/fr/gouv/etalab/mastodon/jobs/ScheduledBoostsSyncJob.java | |
parent | c6524812220384dc119fb27a007480132867e3a3 (diff) |
Issue #622 - Prepares backend for scheduling boosts
Diffstat (limited to 'app/src/main/java/fr/gouv/etalab/mastodon/jobs/ScheduledBoostsSyncJob.java')
-rw-r--r-- | app/src/main/java/fr/gouv/etalab/mastodon/jobs/ScheduledBoostsSyncJob.java | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/ScheduledBoostsSyncJob.java b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/ScheduledBoostsSyncJob.java new file mode 100644 index 000000000..83c193a6d --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/ScheduledBoostsSyncJob.java @@ -0,0 +1,97 @@ +package fr.gouv.etalab.mastodon.jobs; +/* Copyright 2018 Thomas Schneider + * + * This file is a part of Mastalab + * + * 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. + * + * Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Mastalab; if not, + * see <http://www.gnu.org/licenses>. */ + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.support.annotation.NonNull; + +import com.evernote.android.job.Job; +import com.evernote.android.job.JobRequest; + +import java.util.Date; +import java.util.concurrent.TimeUnit; + +import fr.gouv.etalab.mastodon.client.API; +import fr.gouv.etalab.mastodon.client.Entities.Account; +import fr.gouv.etalab.mastodon.client.Entities.Status; +import fr.gouv.etalab.mastodon.client.Entities.StoredStatus; +import fr.gouv.etalab.mastodon.helper.Helper; +import fr.gouv.etalab.mastodon.sqlite.AccountDAO; +import fr.gouv.etalab.mastodon.sqlite.BoostScheduleDAO; +import fr.gouv.etalab.mastodon.sqlite.Sqlite; + + +/** + * Created by Thomas on 09/12/2018. + * Scheduled a boost a datetime + */ + +public class ScheduledBoostsSyncJob extends Job { + + public static final String SCHEDULED_BOOST = "job_scheduled_boost"; + static { + Helper.installProvider(); + } + + @NonNull + @Override + protected Result onRunJob(@NonNull Params params) { + //Code refresh here + int jobId = params.getId(); + SQLiteDatabase db = Sqlite.getInstance(getContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + //Retrieves the stored status + StoredStatus storedStatus = new BoostScheduleDAO(getContext(), db).getStatusScheduled(jobId); + if( storedStatus != null){ + String userId = storedStatus.getUserId(); + String instance = storedStatus.getInstance(); + if( instance != null && userId != null){ + Account account = new AccountDAO(getContext(), db).getAccountByUserIDInstance(userId, instance); + 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 BoostScheduleDAO(getContext(), db).updateScheduledDone(jobId, new Date()); + } + } + } + } + } + return Result.SUCCESS; + } + + + public static int schedule(Context context, Status status, long id, long timestampScheduling){ + + 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(ScheduledBoostsSyncJob.SCHEDULED_BOOST) + .setExecutionWindow(startMs, endMs) + .setUpdateCurrent(false) + .setRequiredNetworkType(JobRequest.NetworkType.METERED) + .setRequirementsEnforced(false) + .build() + .schedule(); + new BoostScheduleDAO(context, db).insert(status, id, jobId, new Date(timestampScheduling)); + return jobId; + } + + +} |