use AndroidX WorkManager instead of Evernote Android Job (#1783)
* use AndroidX WorkManager instead of Evernote Android Job * move notification related classes to their own package * fix missing importmain
parent
d280616d01
commit
e4aee072e4
@ -0,0 +1,98 @@ |
||||
/* Copyright 2020 Tusky Contributors |
||||
* |
||||
* This file is part of Tusky. |
||||
* |
||||
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU |
||||
* Lesser General Public License as published by the Free Software Foundation, either version 3 of |
||||
* the License, or (at your option) any later version. |
||||
* |
||||
* Tusky 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 Lesser |
||||
* General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Lesser General Public License along with Tusky. If |
||||
* not, see <http://www.gnu.org/licenses/>. */ |
||||
|
||||
package com.keylesspalace.tusky.components.notifications |
||||
|
||||
import android.content.Context |
||||
import android.util.Log |
||||
import androidx.work.ListenableWorker |
||||
import androidx.work.Worker |
||||
import androidx.work.WorkerFactory |
||||
import androidx.work.WorkerParameters |
||||
import com.keylesspalace.tusky.db.AccountEntity |
||||
import com.keylesspalace.tusky.db.AccountManager |
||||
import com.keylesspalace.tusky.entity.Notification |
||||
import com.keylesspalace.tusky.network.MastodonApi |
||||
import com.keylesspalace.tusky.util.isLessThan |
||||
import java.io.IOException |
||||
import javax.inject.Inject |
||||
|
||||
class NotificationWorker( |
||||
private val context: Context, |
||||
params: WorkerParameters, |
||||
private val mastodonApi: MastodonApi, |
||||
private val accountManager: AccountManager |
||||
) : Worker(context, params) { |
||||
|
||||
override fun doWork(): Result { |
||||
val accountList = accountManager.getAllAccountsOrderedByActive() |
||||
for (account in accountList) { |
||||
if (account.notificationsEnabled) { |
||||
try { |
||||
Log.d(TAG, "getting Notifications for " + account.fullName) |
||||
val notificationsResponse = mastodonApi.notificationsWithAuth( |
||||
String.format("Bearer %s", account.accessToken), |
||||
account.domain |
||||
).execute() |
||||
val notifications = notificationsResponse.body() |
||||
if (notificationsResponse.isSuccessful && notifications != null) { |
||||
onNotificationsReceived(account, notifications) |
||||
} else { |
||||
Log.w(TAG, "error receiving notifications") |
||||
} |
||||
} catch (e: IOException) { |
||||
Log.w(TAG, "error receiving notifications", e) |
||||
} |
||||
} |
||||
} |
||||
return Result.success() |
||||
} |
||||
|
||||
private fun onNotificationsReceived(account: AccountEntity, notificationList: List<Notification>) { |
||||
val newId = account.lastNotificationId |
||||
var newestId = "" |
||||
var isFirstOfBatch = true |
||||
notificationList.reversed().forEach { notification -> |
||||
val currentId = notification.id |
||||
if (newestId.isLessThan(currentId)) { |
||||
newestId = currentId |
||||
} |
||||
if (newId.isLessThan(currentId)) { |
||||
NotificationHelper.make(context, notification, account, isFirstOfBatch) |
||||
isFirstOfBatch = false |
||||
} |
||||
} |
||||
account.lastNotificationId = newestId |
||||
accountManager.saveAccount(account) |
||||
} |
||||
|
||||
companion object { |
||||
private const val TAG = "NotificationWorker" |
||||
} |
||||
|
||||
} |
||||
|
||||
class NotificationWorkerFactory @Inject constructor( |
||||
val api: MastodonApi, |
||||
val accountManager: AccountManager |
||||
): WorkerFactory() { |
||||
|
||||
override fun createWorker(appContext: Context, workerClassName: String, workerParameters: WorkerParameters): ListenableWorker? { |
||||
if(workerClassName == NotificationWorker::class.java.name) { |
||||
return NotificationWorker(appContext, workerParameters, api, accountManager) |
||||
} |
||||
return null |
||||
} |
||||
} |
@ -1,137 +0,0 @@ |
||||
/* Copyright 2017 Andrew Dawson |
||||
* |
||||
* This file is part of Tusky. |
||||
* |
||||
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU |
||||
* Lesser General Public License as published by the Free Software Foundation, either version 3 of |
||||
* the License, or (at your option) any later version. |
||||
* |
||||
* Tusky 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 Lesser |
||||
* General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Lesser General Public License along with Tusky. If |
||||
* not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
package com.keylesspalace.tusky.util; |
||||
|
||||
import android.content.Context; |
||||
import android.util.Log; |
||||
|
||||
import com.evernote.android.job.Job; |
||||
import com.evernote.android.job.JobCreator; |
||||
import com.keylesspalace.tusky.db.AccountEntity; |
||||
import com.keylesspalace.tusky.db.AccountManager; |
||||
import com.keylesspalace.tusky.entity.Notification; |
||||
import com.keylesspalace.tusky.network.MastodonApi; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
import javax.inject.Inject; |
||||
|
||||
import androidx.annotation.NonNull; |
||||
import androidx.annotation.Nullable; |
||||
import retrofit2.Response; |
||||
|
||||
import static com.keylesspalace.tusky.util.StringUtils.isLessThan; |
||||
|
||||
/** |
||||
* Created by charlag on 31/10/17. |
||||
*/ |
||||
|
||||
public final class NotificationPullJobCreator implements JobCreator { |
||||
|
||||
private static final String TAG = "NotificationPJC"; |
||||
|
||||
static final String NOTIFICATIONS_JOB_TAG = "notifications_job_tag"; |
||||
|
||||
private final MastodonApi api; |
||||
private final Context context; |
||||
private final AccountManager accountManager; |
||||
|
||||
@Inject NotificationPullJobCreator(MastodonApi api, Context context, |
||||
AccountManager accountManager) { |
||||
this.api = api; |
||||
this.context = context; |
||||
this.accountManager = accountManager; |
||||
} |
||||
|
||||
@Nullable |
||||
@Override |
||||
public Job create(@NonNull String tag) { |
||||
if (tag.equals(NOTIFICATIONS_JOB_TAG)) { |
||||
return new NotificationPullJob(context, accountManager, api); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private final static class NotificationPullJob extends Job { |
||||
|
||||
private final Context context; |
||||
private final AccountManager accountManager; |
||||
private final MastodonApi mastodonApi; |
||||
|
||||
NotificationPullJob(Context context, AccountManager accountManager, |
||||
MastodonApi mastodonApi) { |
||||
this.context = context; |
||||
this.accountManager = accountManager; |
||||
this.mastodonApi = mastodonApi; |
||||
} |
||||
|
||||
@NonNull |
||||
@Override |
||||
protected Result onRunJob(@NonNull Params params) { |
||||
List<AccountEntity> accountList = new ArrayList<>(accountManager.getAllAccountsOrderedByActive()); |
||||
boolean withMuted = true; // TODO: configurable
|
||||
for (AccountEntity account : accountList) { |
||||
if (account.getNotificationsEnabled()) { |
||||
try { |
||||
Log.d(TAG, "getting Notifications for " + account.getFullName()); |
||||
Response<List<Notification>> notifications = |
||||
mastodonApi.notificationsWithAuth( |
||||
String.format("Bearer %s", account.getAccessToken()), |
||||
account.getDomain(), |
||||
withMuted |
||||
) |
||||
.execute(); |
||||
if (notifications.isSuccessful()) { |
||||
onNotificationsReceived(account, notifications.body()); |
||||
} else { |
||||
Log.w(TAG, "error receiving notifications"); |
||||
} |
||||
} catch (IOException e) { |
||||
Log.w(TAG, "error receiving notifications", e); |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
return Result.SUCCESS; |
||||
} |
||||
|
||||
private void onNotificationsReceived(AccountEntity account, List<Notification> notificationList) { |
||||
Collections.reverse(notificationList); |
||||
String newId = account.getLastNotificationId(); |
||||
String newestId = ""; |
||||
boolean isFirstOfBatch = true; |
||||
|
||||
for (Notification notification : notificationList) { |
||||
String currentId = notification.getId(); |
||||
if (isLessThan(newestId, currentId)) { |
||||
newestId = currentId; |
||||
} |
||||
if (isLessThan(newId, currentId)) { |
||||
NotificationHelper.make(context, notification, account, isFirstOfBatch); |
||||
isFirstOfBatch = false; |
||||
} |
||||
} |
||||
|
||||
account.setLastNotificationId(newestId); |
||||
accountManager.saveAccount(account); |
||||
} |
||||
|
||||
} |
||||
} |
Loading…
Reference in new issue