Adds instance to sharedpref for notifications
This commit is contained in:
parent
0707fb7570
commit
842afbbe8a
|
@ -21,6 +21,7 @@ import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
import fr.gouv.etalab.mastodon.client.API;
|
import fr.gouv.etalab.mastodon.client.API;
|
||||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||||
|
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveNotificationsInterface;
|
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveNotificationsInterface;
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,22 +36,17 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
|
||||||
|
|
||||||
private APIResponse apiResponse;
|
private APIResponse apiResponse;
|
||||||
private String max_id;
|
private String max_id;
|
||||||
private String acct, userId;
|
private Account account;
|
||||||
private OnRetrieveNotificationsInterface listener;
|
private OnRetrieveNotificationsInterface listener;
|
||||||
private String instance;
|
|
||||||
private String token;
|
|
||||||
private boolean refreshData;
|
private boolean refreshData;
|
||||||
private WeakReference<Context> contextReference;
|
private WeakReference<Context> contextReference;
|
||||||
private boolean display;
|
private boolean display;
|
||||||
|
|
||||||
public RetrieveNotificationsAsyncTask(Context context, boolean display, String instance, String token, String max_id, String acct, String userId, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){
|
public RetrieveNotificationsAsyncTask(Context context, boolean display, Account account, String max_id, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){
|
||||||
this.contextReference = new WeakReference<>(context);
|
this.contextReference = new WeakReference<>(context);
|
||||||
this.max_id = max_id;
|
this.max_id = max_id;
|
||||||
this.listener = onRetrieveNotificationsInterface;
|
this.listener = onRetrieveNotificationsInterface;
|
||||||
this.acct = acct;
|
this.account = account;
|
||||||
this.instance = instance;
|
|
||||||
this.userId = userId;
|
|
||||||
this.token = token;
|
|
||||||
this.refreshData = true;
|
this.refreshData = true;
|
||||||
this.display = display;
|
this.display = display;
|
||||||
}
|
}
|
||||||
|
@ -58,8 +54,8 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params) {
|
protected Void doInBackground(Void... params) {
|
||||||
API api = new API(this.contextReference.get(), instance, token);
|
API api = new API(this.contextReference.get(), account.getInstance(), account.getToken());
|
||||||
if( acct == null)
|
if( account == null)
|
||||||
apiResponse = api.getNotifications(max_id, display);
|
apiResponse = api.getNotifications(max_id, display);
|
||||||
else
|
else
|
||||||
apiResponse = api.getNotificationsSince(max_id, display);
|
apiResponse = api.getNotificationsSince(max_id, display);
|
||||||
|
@ -68,7 +64,7 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask<Void, Void, Void>
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Void result) {
|
protected void onPostExecute(Void result) {
|
||||||
listener.onRetrieveNotifications(apiResponse, acct, userId, refreshData);
|
listener.onRetrieveNotifications(apiResponse, account, refreshData);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,7 @@ import fr.gouv.etalab.mastodon.R;
|
||||||
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
||||||
import fr.gouv.etalab.mastodon.asynctasks.RetrieveMissingNotificationsAsyncTask;
|
import fr.gouv.etalab.mastodon.asynctasks.RetrieveMissingNotificationsAsyncTask;
|
||||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||||
|
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||||
import fr.gouv.etalab.mastodon.drawers.NotificationsListAdapter;
|
import fr.gouv.etalab.mastodon.drawers.NotificationsListAdapter;
|
||||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMissingNotificationsInterface;
|
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMissingNotificationsInterface;
|
||||||
|
@ -112,7 +113,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
|
||||||
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
|
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
|
||||||
if (!flag_loading) {
|
if (!flag_loading) {
|
||||||
flag_loading = true;
|
flag_loading = true;
|
||||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
nextElementLoader.setVisibility(View.VISIBLE);
|
nextElementLoader.setVisibility(View.VISIBLE);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -131,20 +132,20 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
|
||||||
swiped = true;
|
swiped = true;
|
||||||
MainActivity.countNewNotifications = 0;
|
MainActivity.countNewNotifications = 0;
|
||||||
if( context != null)
|
if( context != null)
|
||||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, null, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
|
swipeRefreshLayout.setColorSchemeResources(R.color.mastodonC4,
|
||||||
R.color.mastodonC2,
|
R.color.mastodonC2,
|
||||||
R.color.mastodonC3);
|
R.color.mastodonC3);
|
||||||
if( context != null)
|
if( context != null)
|
||||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
else
|
else
|
||||||
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
if( context != null)
|
if( context != null)
|
||||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
}
|
}
|
||||||
}, 500);
|
}, 500);
|
||||||
return rootView;
|
return rootView;
|
||||||
|
@ -173,7 +174,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRetrieveNotifications(APIResponse apiResponse, String acct, String userId, boolean refreshData) {
|
public void onRetrieveNotifications(APIResponse apiResponse, Account account, boolean refreshData) {
|
||||||
mainLoader.setVisibility(View.GONE);
|
mainLoader.setVisibility(View.GONE);
|
||||||
nextElementLoader.setVisibility(View.GONE);
|
nextElementLoader.setVisibility(View.GONE);
|
||||||
if( apiResponse.getError() != null){
|
if( apiResponse.getError() != null){
|
||||||
|
@ -266,7 +267,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
|
||||||
flag_loading = true;
|
flag_loading = true;
|
||||||
swiped = true;
|
swiped = true;
|
||||||
MainActivity.countNewNotifications = 0;
|
MainActivity.countNewNotifications = 0;
|
||||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, null, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -15,11 +15,12 @@
|
||||||
package fr.gouv.etalab.mastodon.interfaces;
|
package fr.gouv.etalab.mastodon.interfaces;
|
||||||
|
|
||||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||||
|
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Thomas on 28/04/2017.
|
* Created by Thomas on 28/04/2017.
|
||||||
* Interface when notifications have been retrieved
|
* Interface when notifications have been retrieved
|
||||||
*/
|
*/
|
||||||
public interface OnRetrieveNotificationsInterface {
|
public interface OnRetrieveNotificationsInterface {
|
||||||
void onRetrieveNotifications(APIResponse apiResponse, String acct, String userId, boolean refreshData);
|
void onRetrieveNotifications(APIResponse apiResponse, Account account, boolean refreshData);
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,6 +52,7 @@ import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||||
|
|
||||||
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
|
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
|
||||||
import static fr.gouv.etalab.mastodon.helper.Helper.NOTIFICATION_INTENT;
|
import static fr.gouv.etalab.mastodon.helper.Helper.NOTIFICATION_INTENT;
|
||||||
|
import static fr.gouv.etalab.mastodon.helper.Helper.PREF_INSTANCE;
|
||||||
import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_ID;
|
import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_ID;
|
||||||
import static fr.gouv.etalab.mastodon.helper.Helper.canNotify;
|
import static fr.gouv.etalab.mastodon.helper.Helper.canNotify;
|
||||||
import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
|
import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
|
||||||
|
@ -124,7 +125,7 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
|
||||||
return;
|
return;
|
||||||
//Retrieve users in db that owner has.
|
//Retrieve users in db that owner has.
|
||||||
for (Account account: accounts) {
|
for (Account account: accounts) {
|
||||||
new RetrieveNotificationsAsyncTask(getContext(), false, account.getInstance(), account.getToken(), null, account.getAcct(), account.getId(), NotificationsSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
new RetrieveNotificationsAsyncTask(getContext(), false, account, null, NotificationsSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,7 +133,7 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRetrieveNotifications(APIResponse apiResponse, String acct, final String userId, boolean refreshData) {
|
public void onRetrieveNotifications(APIResponse apiResponse, final Account account, boolean refreshData) {
|
||||||
List<Notification> notificationsReceived = apiResponse.getNotifications();
|
List<Notification> notificationsReceived = apiResponse.getNotifications();
|
||||||
if( apiResponse.getError() != null || notificationsReceived == null || notificationsReceived.size() == 0)
|
if( apiResponse.getError() != null || notificationsReceived == null || notificationsReceived.size() == 0)
|
||||||
return;
|
return;
|
||||||
|
@ -141,7 +142,7 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
|
||||||
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
|
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
|
||||||
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
|
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
|
||||||
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
|
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
|
||||||
final String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, null);
|
final String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
|
||||||
final List<Notification> notifications = new ArrayList<>();
|
final List<Notification> notifications = new ArrayList<>();
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
for(Notification notif: notificationsReceived){
|
for(Notification notif: notificationsReceived){
|
||||||
|
@ -226,8 +227,9 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
|
||||||
final Intent intent = new Intent(getContext(), MainActivity.class);
|
final Intent intent = new Intent(getContext(), MainActivity.class);
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
|
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
|
||||||
intent.putExtra(INTENT_ACTION, NOTIFICATION_INTENT);
|
intent.putExtra(INTENT_ACTION, NOTIFICATION_INTENT);
|
||||||
intent.putExtra(PREF_KEY_ID, userId);
|
intent.putExtra(PREF_KEY_ID, account.getId());
|
||||||
long notif_id = Long.parseLong(userId);
|
intent.putExtra(PREF_INSTANCE, account.getInstance());
|
||||||
|
long notif_id = Long.parseLong(account.getId());
|
||||||
final int notificationId = ((notif_id + 1) > 2147483647) ? (int) (2147483647 - notif_id - 1) : (int) (notif_id + 1);
|
final int notificationId = ((notif_id + 1) > 2147483647) ? (int) (2147483647 - notif_id - 1) : (int) (notif_id + 1);
|
||||||
if( notificationUrl != null ){
|
if( notificationUrl != null ){
|
||||||
|
|
||||||
|
@ -247,10 +249,10 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
|
||||||
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
|
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
|
||||||
notify_user(getContext(), intent, notificationId, BitmapFactory.decodeResource(getContext().getResources(),
|
notify_user(getContext(), intent, notificationId, BitmapFactory.decodeResource(getContext().getResources(),
|
||||||
R.drawable.mastodonlogo), finalTitle, message);
|
R.drawable.mastodonlogo), finalTitle, message);
|
||||||
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, null);
|
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
|
||||||
if( lastNotif == null || Long.parseLong(notifications.get(0).getId()) > Long.parseLong(lastNotif)){
|
if( lastNotif == null || Long.parseLong(notifications.get(0).getId()) > Long.parseLong(lastNotif)){
|
||||||
SharedPreferences.Editor editor = sharedpreferences.edit();
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
||||||
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notifications.get(0).getId());
|
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), notifications.get(0).getId());
|
||||||
editor.apply();
|
editor.apply();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -260,10 +262,10 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications
|
||||||
@Override
|
@Override
|
||||||
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
|
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
|
||||||
notify_user(getContext(), intent, notificationId, resource, finalTitle, message);
|
notify_user(getContext(), intent, notificationId, resource, finalTitle, message);
|
||||||
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, null);
|
String lastNotif = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), null);
|
||||||
if( lastNotif == null || Long.parseLong(notifications.get(0).getId()) > Long.parseLong(lastNotif)){
|
if( lastNotif == null || Long.parseLong(notifications.get(0).getId()) > Long.parseLong(lastNotif)){
|
||||||
SharedPreferences.Editor editor = sharedpreferences.edit();
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
||||||
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notifications.get(0).getId());
|
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId() + account.getInstance(), notifications.get(0).getId());
|
||||||
editor.apply();
|
editor.apply();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue