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

116 lines
4.6 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.jobs;
2017-08-29 16:22:57 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-08-29 16:22:57 +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-08-29 16:22:57 +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-08-29 16:22:57 +02:00
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.SharedPreferences;
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-08-29 16:22:57 +02:00
import com.evernote.android.job.Job;
import com.evernote.android.job.JobManager;
import com.evernote.android.job.JobRequest;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
2019-01-13 14:31:32 +01:00
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.helper.Helper;
2021-02-28 11:46:34 +01:00
import app.fedilab.android.helper.NotificationsHelper;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.Sqlite;
2017-08-29 16:22:57 +02:00
2019-05-18 11:10:30 +02:00
import static app.fedilab.android.helper.Helper.canNotify;
2021-03-02 11:59:49 +01:00
2017-08-29 16:22:57 +02:00
/**
* Created by Thomas on 29/04/2017.
* Notifications refresh job
*/
2020-07-14 17:25:10 +02:00
public class BaseNotificationsSyncJob extends Job {
2017-08-29 16:22:57 +02:00
2019-10-22 16:59:31 +02:00
public static final String NOTIFICATION_REFRESH = "job_notification";
2019-09-06 17:55:14 +02:00
2018-08-16 15:26:39 +02:00
public static int schedule(boolean updateCurrent) {
2017-08-29 16:22:57 +02:00
Set<JobRequest> jobRequests = JobManager.instance().getAllJobRequestsForTag(NOTIFICATION_REFRESH);
if (!jobRequests.isEmpty() && !updateCurrent) {
return jobRequests.iterator().next().getJobId();
}
2018-08-16 15:26:39 +02:00
int jobRequestschedule = -1;
try {
2020-07-14 17:25:10 +02:00
jobRequestschedule = new JobRequest.Builder(BaseNotificationsSyncJob.NOTIFICATION_REFRESH)
2018-08-16 15:26:39 +02:00
.setPeriodic(TimeUnit.MINUTES.toMillis(Helper.MINUTES_BETWEEN_NOTIFICATIONS_REFRESH), TimeUnit.MINUTES.toMillis(5))
.setUpdateCurrent(updateCurrent)
.setRequiredNetworkType(JobRequest.NetworkType.METERED)
.setRequirementsEnforced(false)
.build()
.schedule();
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
2018-08-16 15:26:39 +02:00
return jobRequestschedule;
2017-08-29 16:22:57 +02:00
}
2019-11-15 16:32:25 +01:00
@NonNull
@Override
protected Result onRunJob(@NonNull Params params) {
//Code refresh here
callAsynchronousTask();
return Result.SUCCESS;
}
2017-08-29 16:22:57 +02:00
/**
* Task in background starts here.
*/
private void callAsynchronousTask() {
2019-09-06 17:55:14 +02:00
if (!canNotify(getContext()))
2017-08-29 16:22:57 +02:00
return;
int liveNotifications = Helper.liveNotifType(getContext());
2019-11-15 16:32:25 +01:00
if (liveNotifications != Helper.NOTIF_NONE) {
return;
}
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(getContext().getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2017-08-29 16:22:57 +02:00
//If an Internet connection and user agrees with notification refresh
final SharedPreferences sharedpreferences = getContext().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
//Check which notifications the user wants to see
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
boolean notif_poll = sharedpreferences.getBoolean(Helper.SET_NOTIF_POLL, true);
2017-08-29 16:22:57 +02:00
//User disagree with all notifications
2019-09-06 17:55:14 +02:00
if (!notif_follow && !notif_add && !notif_mention && !notif_share && !notif_poll)
2017-08-29 16:22:57 +02:00
return; //Nothing is done
//No account connected, the service is stopped
2019-09-06 17:55:14 +02:00
if (!Helper.isLoggedIn(getContext()))
2017-08-29 16:22:57 +02:00
return;
//If WIFI only and on WIFI OR user defined any connections to use the service.
2019-09-06 17:55:14 +02:00
if (!sharedpreferences.getBoolean(Helper.SET_WIFI_ONLY, false) || Helper.isOnWIFI(getContext())) {
List<Account> accounts = new AccountDAO(getContext(), db).getAllAccountCrossAction();
2017-08-29 16:22:57 +02:00
//It means there is no user in DB.
2019-09-06 17:55:14 +02:00
if (accounts == null)
2017-08-29 16:22:57 +02:00
return;
//Retrieve users in db that owner has.
2019-09-06 17:55:14 +02:00
for (Account account : accounts) {
2021-03-02 11:59:49 +01:00
NotificationsHelper.task(getContext(), account);
2017-08-29 16:22:57 +02:00
}
}
}
}