Improves background jobs
This commit is contained in:
parent
eff2c8be41
commit
9700b0b4df
|
@ -1,59 +0,0 @@
|
|||
/* Copyright 2017 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>. */
|
||||
package fr.gouv.etalab.mastodon.asynctasks;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import fr.gouv.etalab.mastodon.client.API;
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveHomeTimelineServiceInterface;
|
||||
|
||||
/**
|
||||
* Created by Thomas on 20/05/2017.
|
||||
* Retrieves home timeline for the authenticated user - used in the service
|
||||
*/
|
||||
|
||||
public class RetrieveHomeTimelineServiceAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||
|
||||
private APIResponse apiResponse;
|
||||
private String since_id;
|
||||
private Account account;
|
||||
private OnRetrieveHomeTimelineServiceInterface listener;
|
||||
private WeakReference<Context> contextReference;
|
||||
|
||||
public RetrieveHomeTimelineServiceAsyncTask(Context context, Account account, String since_id, OnRetrieveHomeTimelineServiceInterface onRetrieveHomeTimelineServiceInterface){
|
||||
this.contextReference = new WeakReference<>(context);
|
||||
this.since_id = since_id;
|
||||
this.listener = onRetrieveHomeTimelineServiceInterface;
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
API api = new API(this.contextReference.get(), this.account.getInstance(), this.account.getToken());
|
||||
apiResponse = api.getHomeTimelineSinceId(since_id);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
listener.onRetrieveHomeTimelineService(apiResponse, this.account);
|
||||
}
|
||||
|
||||
}
|
|
@ -20,7 +20,6 @@ import android.content.SharedPreferences;
|
|||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.AsyncTask;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
|
@ -41,12 +40,11 @@ import java.util.concurrent.TimeUnit;
|
|||
|
||||
import fr.gouv.etalab.mastodon.R;
|
||||
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.RetrieveHomeTimelineServiceAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.client.API;
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Status;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveHomeTimelineServiceInterface;
|
||||
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
|
||||
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||
|
||||
|
@ -64,7 +62,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
|
|||
* Notifications for home timeline job
|
||||
*/
|
||||
|
||||
public class HomeTimelineSyncJob extends Job implements OnRetrieveHomeTimelineServiceInterface{
|
||||
public class HomeTimelineSyncJob extends Job {
|
||||
|
||||
static final String HOME_TIMELINE = "home_timeline";
|
||||
|
||||
|
@ -130,16 +128,13 @@ public class HomeTimelineSyncJob extends Job implements OnRetrieveHomeTimelineSe
|
|||
editor.apply();
|
||||
}
|
||||
}
|
||||
//noinspection ConstantConditions
|
||||
if( getContext() != null)
|
||||
new RetrieveHomeTimelineServiceAsyncTask(getContext(), account, max_id, HomeTimelineSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
|
||||
API api = new API(getContext(), account.getInstance(), account.getToken());
|
||||
APIResponse apiResponse = api.getHomeTimelineSinceId(max_id);
|
||||
onRetrieveHomeTimelineService(apiResponse, account);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onRetrieveHomeTimelineService(APIResponse apiResponse, final Account account) {
|
||||
final List<Status> statuses = apiResponse.getStatuses();
|
||||
if( apiResponse.getError() != null || statuses == null || statuses.size() == 0 || account == null)
|
||||
|
|
|
@ -20,7 +20,6 @@ import android.content.SharedPreferences;
|
|||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.AsyncTask;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
|
@ -40,13 +39,12 @@ import java.util.List;
|
|||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
||||
import fr.gouv.etalab.mastodon.client.API;
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.R;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.RetrieveNotificationsAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Notification;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveNotificationsInterface;
|
||||
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
|
||||
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||
|
||||
|
@ -64,7 +62,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
|
|||
* Notifications refresh job
|
||||
*/
|
||||
|
||||
public class NotificationsSyncJob extends Job implements OnRetrieveNotificationsInterface{
|
||||
public class NotificationsSyncJob extends Job {
|
||||
|
||||
static final String NOTIFICATION_REFRESH = "job_notification";
|
||||
static {
|
||||
|
@ -126,17 +124,15 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
|
|||
return;
|
||||
//Retrieve users in db that owner has.
|
||||
for (Account account: accounts) {
|
||||
//noinspection ConstantConditions
|
||||
if( getContext() != null)
|
||||
new RetrieveNotificationsAsyncTask(getContext(), false, account, null, NotificationsSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
API api = new API(getContext(), account.getInstance(), account.getToken());
|
||||
APIResponse apiResponse = api.getNotificationsSince(null, false);
|
||||
onRetrieveNotifications(apiResponse, account);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void onRetrieveNotifications(APIResponse apiResponse, final Account account, boolean refreshData) {
|
||||
private void onRetrieveNotifications(APIResponse apiResponse, final Account account) {
|
||||
List<Notification> notificationsReceived = apiResponse.getNotifications();
|
||||
if( apiResponse.getError() != null || notificationsReceived == null || notificationsReceived.size() == 0 || account == null)
|
||||
return;
|
||||
|
|
Loading…
Reference in New Issue