2017-11-10 19:26:25 +11:00
|
|
|
/* 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/>. */
|
|
|
|
|
2017-11-02 07:02:44 +11:00
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.text.Spanned;
|
2018-02-04 08:45:14 +11:00
|
|
|
import android.util.Log;
|
2017-11-02 07:02:44 +11:00
|
|
|
|
|
|
|
import com.evernote.android.job.Job;
|
|
|
|
import com.evernote.android.job.JobCreator;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
2018-02-04 08:45:14 +11:00
|
|
|
import com.keylesspalace.tusky.db.AccountEntity;
|
2017-11-02 07:02:44 +11:00
|
|
|
import com.keylesspalace.tusky.entity.Notification;
|
|
|
|
import com.keylesspalace.tusky.json.SpannedTypeAdapter;
|
|
|
|
import com.keylesspalace.tusky.network.MastodonApi;
|
2017-11-14 05:05:23 +11:00
|
|
|
import com.keylesspalace.tusky.util.NotificationManager;
|
2017-11-02 07:02:44 +11:00
|
|
|
import com.keylesspalace.tusky.util.OkHttpUtils;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-02-04 08:45:14 +11:00
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.util.ArrayList;
|
2017-11-02 07:02:44 +11:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
import retrofit2.Response;
|
|
|
|
import retrofit2.Retrofit;
|
|
|
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by charlag on 31/10/17.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public final class NotificationPullJobCreator implements JobCreator {
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
private static final String TAG = "NotificationPJC";
|
|
|
|
|
2017-11-02 07:02:44 +11:00
|
|
|
static final String NOTIFICATIONS_JOB_TAG = "notifications_job_tag";
|
|
|
|
|
|
|
|
private Context context;
|
|
|
|
|
|
|
|
NotificationPullJobCreator(Context context) {
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public Job create(@NonNull String tag) {
|
|
|
|
if (tag.equals(NOTIFICATIONS_JOB_TAG)) {
|
2018-02-04 08:45:14 +11:00
|
|
|
return new NotificationPullJob(context);
|
2017-11-02 07:02:44 +11:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-10 19:26:25 +11:00
|
|
|
private static MastodonApi createMastodonApi(String domain, Context context) {
|
2017-12-27 07:45:08 +11:00
|
|
|
SharedPreferences preferences = context.getSharedPreferences(
|
|
|
|
context.getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
2017-11-02 07:02:44 +11:00
|
|
|
|
2017-12-27 07:45:08 +11:00
|
|
|
OkHttpClient okHttpClient = OkHttpUtils.getCompatibleClientBuilder(preferences)
|
2017-11-02 07:02:44 +11:00
|
|
|
.build();
|
|
|
|
|
|
|
|
Gson gson = new GsonBuilder()
|
|
|
|
.registerTypeAdapter(Spanned.class, new SpannedTypeAdapter())
|
|
|
|
.create();
|
|
|
|
|
|
|
|
Retrofit retrofit = new Retrofit.Builder()
|
|
|
|
.baseUrl("https://" + domain)
|
|
|
|
.client(okHttpClient)
|
|
|
|
.addConverterFactory(GsonConverterFactory.create(gson))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
return retrofit.create(MastodonApi.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
private final static class NotificationPullJob extends Job {
|
|
|
|
|
|
|
|
private Context context;
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
NotificationPullJob(Context context) {
|
2017-11-02 07:02:44 +11:00
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
protected Result onRunJob(Params params) {
|
2018-02-04 08:45:14 +11:00
|
|
|
|
|
|
|
List<AccountEntity> accountList = new ArrayList<>(TuskyApplication.getAccountManager().getAllAccountsOrderedByActive());
|
|
|
|
|
|
|
|
for(AccountEntity account: accountList) {
|
|
|
|
|
|
|
|
if(account.getNotificationsEnabled()) {
|
|
|
|
MastodonApi api = createMastodonApi(account.getDomain(), context);
|
|
|
|
try {
|
|
|
|
Log.d(TAG, "getting Notifications for "+account.getFullName());
|
|
|
|
Response<List<Notification>> notifications =
|
|
|
|
api.notificationsWithAuth(String.format("Bearer %s", account.getAccessToken())).execute();
|
|
|
|
if (notifications.isSuccessful()) {
|
|
|
|
onNotificationsReceived(account, notifications.body());
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "error receiving notificationsEnabled");
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, "error receiving notificationsEnabled", e);
|
|
|
|
}
|
2017-11-02 07:02:44 +11:00
|
|
|
}
|
2018-02-04 08:45:14 +11:00
|
|
|
|
2017-11-02 07:02:44 +11:00
|
|
|
}
|
2018-02-04 08:45:14 +11:00
|
|
|
|
2017-11-02 07:02:44 +11:00
|
|
|
return Result.SUCCESS;
|
2018-02-04 08:45:14 +11:00
|
|
|
|
|
|
|
|
2017-11-02 07:02:44 +11:00
|
|
|
}
|
|
|
|
|
2018-02-04 08:45:14 +11:00
|
|
|
private void onNotificationsReceived(AccountEntity account, List<Notification> notificationList) {
|
|
|
|
|
|
|
|
BigInteger newId = new BigInteger(account.getLastNotificationId());
|
|
|
|
|
|
|
|
BigInteger newestId = BigInteger.ZERO;
|
|
|
|
|
|
|
|
for(Notification notification: notificationList){
|
|
|
|
|
|
|
|
BigInteger currentId = new BigInteger(notification.id);
|
|
|
|
|
|
|
|
if(isBiggerThan(currentId, newestId)) {
|
|
|
|
newestId = currentId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isBiggerThan(currentId, newId)) {
|
|
|
|
account.setLastNotificationId(notification.id);
|
|
|
|
|
|
|
|
NotificationManager.make(context, notification, account);
|
2017-11-02 07:02:44 +11:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 08:45:14 +11:00
|
|
|
|
|
|
|
account.setLastNotificationId(newestId.toString());
|
|
|
|
TuskyApplication.getAccountManager().saveAccount(account);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isBiggerThan(BigInteger newId, BigInteger lastShownNotificationId) {
|
|
|
|
|
|
|
|
return lastShownNotificationId.compareTo(newId) == - 1;
|
2017-11-02 07:02:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|