2017-05-05 16:36:04 +02:00
|
|
|
package fr.gouv.etalab.mastodon.jobs;
|
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
2017-07-10 10:33:24 +02:00
|
|
|
* This file is a part of Mastalab
|
2017-05-05 16:36:04 +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.
|
|
|
|
*
|
2017-07-10 10:33:24 +02:00
|
|
|
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-05-05 16:36:04 +02:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
2017-08-04 11:11:27 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
|
2017-05-05 16:36:04 +02:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2017-05-20 15:15:02 +02:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.content.Context;
|
2017-05-28 14:58:04 +02:00
|
|
|
import android.content.Intent;
|
2017-05-05 16:36:04 +02:00
|
|
|
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;
|
2017-06-26 18:49:39 +02:00
|
|
|
import android.view.View;
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
import com.evernote.android.job.Job;
|
|
|
|
import com.evernote.android.job.JobManager;
|
|
|
|
import com.evernote.android.job.JobRequest;
|
|
|
|
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
|
2017-06-26 18:49:39 +02:00
|
|
|
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
2017-05-05 16:36:04 +02:00
|
|
|
import com.nostra13.universalimageloader.core.ImageLoader;
|
|
|
|
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
|
2017-06-26 18:49:39 +02:00
|
|
|
import com.nostra13.universalimageloader.core.assist.FailReason;
|
|
|
|
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
|
|
|
|
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-05-28 14:58:04 +02:00
|
|
|
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
2017-06-03 18:18:27 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.APIResponse;
|
2017-05-25 13:33:33 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
|
2017-05-05 16:36:04 +02:00
|
|
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
|
|
|
import mastodon.etalab.gouv.fr.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;
|
|
|
|
|
2017-05-28 14:58:04 +02:00
|
|
|
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
|
2017-05-20 15:15:02 +02:00
|
|
|
import static fr.gouv.etalab.mastodon.helper.Helper.NOTIFICATION_INTENT;
|
2017-05-28 14:58:04 +02:00
|
|
|
import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_ID;
|
2017-07-10 18:43:36 +02:00
|
|
|
import static fr.gouv.etalab.mastodon.helper.Helper.canNotify;
|
2017-05-20 15:15:02 +02:00
|
|
|
import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 29/04/2017.
|
|
|
|
* Notifications refresh job
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class NotificationsSyncJob extends Job implements OnRetrieveNotificationsInterface{
|
|
|
|
|
2017-05-20 15:15:02 +02:00
|
|
|
static final String NOTIFICATION_REFRESH = "job_notification";
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
protected Result onRunJob(Params params) {
|
|
|
|
//Code refresh here
|
|
|
|
callAsynchronousTask();
|
|
|
|
return Result.SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-20 15:15:02 +02:00
|
|
|
public static int schedule(boolean updateCurrent){
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
Set<JobRequest> jobRequests = JobManager.instance().getAllJobRequestsForTag(NOTIFICATION_REFRESH);
|
|
|
|
if (!jobRequests.isEmpty() && !updateCurrent) {
|
|
|
|
return jobRequests.iterator().next().getJobId();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new JobRequest.Builder(NotificationsSyncJob.NOTIFICATION_REFRESH)
|
|
|
|
.setPeriodic(TimeUnit.MINUTES.toMillis(Helper.MINUTES_BETWEEN_NOTIFICATIONS_REFRESH), TimeUnit.MINUTES.toMillis(5))
|
|
|
|
.setPersisted(true)
|
|
|
|
.setUpdateCurrent(updateCurrent)
|
|
|
|
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
|
|
|
|
.setRequirementsEnforced(false)
|
|
|
|
.build()
|
|
|
|
.schedule();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Task in background starts here.
|
|
|
|
*/
|
|
|
|
private void callAsynchronousTask() {
|
2017-07-10 18:43:36 +02:00
|
|
|
if( !canNotify(getContext()))
|
|
|
|
return;
|
2017-05-05 16:36:04 +02:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(getContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
//If an Internet connection and user agrees with notification refresh
|
|
|
|
final SharedPreferences sharedpreferences = getContext().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
2017-05-20 15:15:02 +02:00
|
|
|
//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_ask = sharedpreferences.getBoolean(Helper.SET_NOTIF_ASK, true);
|
|
|
|
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
|
|
|
|
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
|
|
|
|
//User disagree with all notifications
|
|
|
|
if( !notif_follow && !notif_add && !notif_ask && !notif_mention && !notif_share)
|
|
|
|
return; //Nothing is done
|
2017-06-03 15:54:19 +02:00
|
|
|
//No account connected, the service is stopped
|
|
|
|
if(!Helper.isLoggedIn(getContext()))
|
|
|
|
return;
|
2017-05-05 16:36:04 +02:00
|
|
|
//If WIFI only and on WIFI OR user defined any connections to use the service.
|
|
|
|
if(!sharedpreferences.getBoolean(Helper.SET_WIFI_ONLY, false) || Helper.isOnWIFI(getContext())) {
|
|
|
|
List<Account> accounts = new AccountDAO(getContext(),db).getAllAccount();
|
|
|
|
//It means there is no user in DB.
|
|
|
|
if( accounts == null )
|
|
|
|
return;
|
|
|
|
//Retrieve users in db that owner has.
|
|
|
|
for (Account account: accounts) {
|
2017-05-28 14:58:04 +02:00
|
|
|
String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId(), null);
|
2017-05-28 17:16:31 +02:00
|
|
|
new RetrieveNotificationsAsyncTask(getContext(), account.getInstance(), account.getToken(), max_id, account.getAcct(), account.getId(), NotificationsSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2017-06-03 18:18:27 +02:00
|
|
|
public void onRetrieveNotifications(APIResponse apiResponse, String acct, String userId) {
|
|
|
|
|
|
|
|
List<Notification> notifications = apiResponse.getNotifications();
|
|
|
|
if( apiResponse.getError() != null || notifications == null || notifications.size() == 0)
|
2017-05-05 16:36:04 +02:00
|
|
|
return;
|
|
|
|
Bitmap icon_notification = null;
|
|
|
|
final SharedPreferences sharedpreferences = getContext().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
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);
|
2017-06-26 18:49:39 +02:00
|
|
|
final String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, null);
|
2017-05-05 16:36:04 +02:00
|
|
|
//No previous notifications in cache, so no notification will be sent
|
2017-05-28 17:16:31 +02:00
|
|
|
int newFollows = 0;
|
|
|
|
int newAdds = 0;
|
|
|
|
int newAsks = 0;
|
|
|
|
int newMentions = 0;
|
|
|
|
int newShare = 0;
|
|
|
|
String notificationUrl = null;
|
|
|
|
String title = null;
|
2017-06-26 18:49:39 +02:00
|
|
|
final String message;
|
2017-05-28 17:16:31 +02:00
|
|
|
for(Notification notification: notifications){
|
|
|
|
//The notification associated to max_id is discarded as it is supposed to have already been sent
|
|
|
|
if( max_id != null && notification.getId().equals(max_id))
|
|
|
|
continue;
|
|
|
|
switch (notification.getType()){
|
|
|
|
case "mention":
|
|
|
|
if(notif_mention){
|
|
|
|
newMentions++;
|
|
|
|
if( notificationUrl == null){
|
|
|
|
notificationUrl = notification.getAccount().getAvatar();
|
2017-07-31 15:17:19 +02:00
|
|
|
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
|
|
|
|
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getContext().getString(R.string.notif_mention));
|
|
|
|
else
|
|
|
|
title = String.format("@%s %s", notification.getAccount().getUsername(),getContext().getString(R.string.notif_mention));
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2017-05-28 17:16:31 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "reblog":
|
|
|
|
if(notif_share){
|
|
|
|
newShare++;
|
|
|
|
if( notificationUrl == null){
|
|
|
|
notificationUrl = notification.getAccount().getAvatar();
|
2017-07-31 15:17:19 +02:00
|
|
|
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
|
|
|
|
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getContext().getString(R.string.notif_reblog));
|
|
|
|
else
|
|
|
|
title = String.format("@%s %s", notification.getAccount().getUsername(),getContext().getString(R.string.notif_reblog));
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2017-05-28 17:16:31 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "favourite":
|
|
|
|
if(notif_add){
|
|
|
|
newAdds++;
|
|
|
|
if( notificationUrl == null){
|
|
|
|
notificationUrl = notification.getAccount().getAvatar();
|
2017-07-31 15:17:19 +02:00
|
|
|
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
|
|
|
|
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getContext().getString(R.string.notif_favourite));
|
|
|
|
else
|
|
|
|
title = String.format("@%s %s", notification.getAccount().getUsername(),getContext().getString(R.string.notif_favourite));
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2017-05-28 17:16:31 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "follow":
|
|
|
|
if(notif_follow){
|
|
|
|
newFollows++;
|
|
|
|
if( notificationUrl == null){
|
|
|
|
notificationUrl = notification.getAccount().getAvatar();
|
2017-07-31 15:17:19 +02:00
|
|
|
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
|
|
|
|
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getContext().getString(R.string.notif_follow));
|
|
|
|
else
|
|
|
|
title = String.format("@%s %s", notification.getAccount().getUsername(),getContext().getString(R.string.notif_follow));
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-28 17:16:31 +02:00
|
|
|
break;
|
|
|
|
default:
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-28 17:16:31 +02:00
|
|
|
int allNotifCount = newFollows + newAdds + newAsks + newMentions + newShare;
|
|
|
|
if( allNotifCount > 0){
|
|
|
|
//Some others notification
|
|
|
|
int other = allNotifCount -1;
|
|
|
|
if(other > 0 )
|
|
|
|
message = getContext().getResources().getQuantityString(R.plurals.other_notifications, other, other);
|
|
|
|
else
|
|
|
|
message = "";
|
|
|
|
final Intent intent = new Intent(getContext(), MainActivity.class);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
|
|
|
|
intent.putExtra(INTENT_ACTION, NOTIFICATION_INTENT);
|
|
|
|
intent.putExtra(PREF_KEY_ID, userId);
|
2017-06-10 16:01:10 +02:00
|
|
|
long notif_id = Long.parseLong(userId);
|
2017-06-26 18:49:39 +02:00
|
|
|
final int notificationId = ((notif_id + 1) > 2147483647) ? (int) (2147483647 - notif_id - 1) : (int) (notif_id + 1);
|
|
|
|
|
|
|
|
if( notificationUrl != null && icon_notification == null){
|
|
|
|
ImageLoader imageLoaderNoty = ImageLoader.getInstance();
|
|
|
|
File cacheDir = new File(getContext().getCacheDir(), getContext().getString(R.string.app_name));
|
|
|
|
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getContext())
|
|
|
|
.imageDownloader(new PatchBaseImageDownloader(getContext()))
|
|
|
|
.threadPoolSize(5)
|
|
|
|
.threadPriority(Thread.MIN_PRIORITY + 3)
|
|
|
|
.denyCacheImageMultipleSizesInMemory()
|
|
|
|
.diskCache(new UnlimitedDiskCache(cacheDir))
|
|
|
|
.build();
|
|
|
|
imageLoaderNoty.init(config);
|
|
|
|
DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(new SimpleBitmapDisplayer()).cacheInMemory(false)
|
|
|
|
.cacheOnDisk(true).resetViewBeforeLoading(true).build();
|
|
|
|
|
|
|
|
final String finalTitle = title;
|
|
|
|
imageLoaderNoty.loadImage(notificationUrl, options, new SimpleImageLoadingListener(){
|
|
|
|
@Override
|
|
|
|
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
|
|
|
super.onLoadingComplete(imageUri, view, loadedImage);
|
|
|
|
if( max_id != null)
|
|
|
|
notify_user(getContext(), intent, notificationId, loadedImage, finalTitle, message);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void onLoadingFailed(java.lang.String imageUri, android.view.View view, FailReason failReason){
|
|
|
|
if( max_id != null)
|
|
|
|
notify_user(getContext(), intent, notificationId, BitmapFactory.decodeResource(getContext().getResources(),
|
|
|
|
R.drawable.mastodonlogo), finalTitle, message);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2017-05-28 17:16:31 +02:00
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
2017-06-03 18:18:27 +02:00
|
|
|
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, apiResponse.getMax_id());
|
2017-05-05 16:36:04 +02:00
|
|
|
editor.apply();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|