diff --git a/app/build.gradle b/app/build.gradle index ce919405e..4e1f0e35c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,8 +7,8 @@ android { applicationId "fr.gouv.etalab.mastodon" minSdkVersion 15 targetSdkVersion 25 - versionCode 10 - versionName "1.1.3" + versionCode 11 + versionName "1.1.4" } buildTypes { release { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveHomeTimelineServiceAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveHomeTimelineServiceAsyncTask.java index 7c942da80..11118809c 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveHomeTimelineServiceAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveHomeTimelineServiceAsyncTask.java @@ -35,19 +35,21 @@ public class RetrieveHomeTimelineServiceAsyncTask extends AsyncTask private String acct, userId; private OnRetrieveNotificationsInterface listener; private String instance; - - public RetrieveNotificationsAsyncTask(Context context, String max_id, String acct, String userId, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){ - this.context = context; - this.max_id = max_id; - this.listener = onRetrieveNotificationsInterface; - this.acct = acct; - this.userId = userId; - } + private String token; - public RetrieveNotificationsAsyncTask(Context context, String instance, String max_id, String acct, String userId, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){ + + public RetrieveNotificationsAsyncTask(Context context, String instance, String token, String max_id, String acct, String userId, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){ this.context = context; this.max_id = max_id; this.listener = onRetrieveNotificationsInterface; this.acct = acct; this.instance = instance; this.userId = userId; + this.token = token; } @Override protected Void doInBackground(Void... params) { if( acct == null) - notifications = new API(context, instance).getNotifications(max_id); + notifications = new API(context, instance, token).getNotifications(max_id); else - notifications = new API(context, instance).getNotificationsSince(max_id); + notifications = new API(context, instance, token).getNotificationsSince(max_id); return null; } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/UpdateAccountInfoAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/UpdateAccountInfoAsyncTask.java index 84057d894..d4203ebf7 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/UpdateAccountInfoAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/UpdateAccountInfoAsyncTask.java @@ -48,7 +48,7 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask { @Override protected Void doInBackground(Void... params) { - Account account = new API(context, instance).verifyCredentials(); + Account account = new API(context, instance, null).verifyCredentials(); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); if( token == null) { token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index 10b68f107..936e4807a 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -75,6 +75,7 @@ public class API { private int tootPerPage, accountPerPage, notificationPerPage; private int actionCode; private String instance; + private String prefKeyOauthTokenT; public enum StatusAction{ FAVOURITE, @@ -99,9 +100,10 @@ public class API { accountPerPage = sharedpreferences.getInt(Helper.SET_ACCOUNTS_PER_PAGE, 40); notificationPerPage = sharedpreferences.getInt(Helper.SET_NOTIFICATIONS_PER_PAGE, 40); this.instance = Helper.getLiveInstance(context); + this.prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); } - public API(Context context, String instance) { + public API(Context context, String instance, String token) { this.context = context; SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); tootPerPage = sharedpreferences.getInt(Helper.SET_TOOTS_PER_PAGE, 40); @@ -111,6 +113,12 @@ public class API { this.instance = instance; else this.instance = Helper.getLiveInstance(context); + + if( token != null) + this.prefKeyOauthTokenT = token; + else + this.prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + } @@ -787,7 +795,7 @@ public class API { } @Override public void onFailure(int statusCode, Header[] headers, Throwable error, JSONObject response){ - + error.printStackTrace(); } }); return notifications; @@ -1225,8 +1233,6 @@ public class API { try { client.setConnectTimeout(10000); //10s timeout client.setUserAgent(USER_AGENT); - SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - String prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); client.addHeader("Authorization", "Bearer "+prefKeyOauthTokenT); client.setSSLSocketFactory(new MastalabSSLSocketFactory(MastalabSSLSocketFactory.getKeystore())); client.get(getAbsoluteUrl(action), params, responseHandler); @@ -1242,8 +1248,6 @@ public class API { try { client.setConnectTimeout(10000); //10s timeout client.setUserAgent(USER_AGENT); - SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - String prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); client.addHeader("Authorization", "Bearer "+prefKeyOauthTokenT); client.setSSLSocketFactory(new MastalabSSLSocketFactory(MastalabSSLSocketFactory.getKeystore())); client.post(getAbsoluteUrl(action), params, responseHandler); @@ -1257,8 +1261,6 @@ public class API { try { client.setConnectTimeout(10000); //10s timeout client.setUserAgent(USER_AGENT); - SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - String prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); client.addHeader("Authorization", "Bearer "+prefKeyOauthTokenT); client.setSSLSocketFactory(new MastalabSSLSocketFactory(MastalabSSLSocketFactory.getKeystore())); client.delete(getAbsoluteUrl(action), params, responseHandler); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java index 73b42cab0..4a68fe380 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java @@ -92,7 +92,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve if(firstVisibleItem + visibleItemCount == totalItemCount ) { if(!flag_loading ) { flag_loading = true; - asyncTask = new RetrieveNotificationsAsyncTask(context, max_id, null, null,DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, max_id, null, null,DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); nextElementLoader.setVisibility(View.VISIBLE); } } else { @@ -108,7 +108,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve notifications = new ArrayList<>(); firstLoad = true; flag_loading = true; - asyncTask = new RetrieveNotificationsAsyncTask(context, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } }); swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, @@ -116,7 +116,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve R.color.colorPrimaryDark); - asyncTask = new RetrieveNotificationsAsyncTask(context, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + asyncTask = new RetrieveNotificationsAsyncTask(context, null, null, max_id, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); return rootView; } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/HomeTimelineSyncJob.java b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/HomeTimelineSyncJob.java index 72abd2043..d0d4d22b0 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/HomeTimelineSyncJob.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/HomeTimelineSyncJob.java @@ -107,8 +107,8 @@ public class HomeTimelineSyncJob extends Job implements OnRetrieveHomeTimelineSe //Retrieve users in db that owner has. for (Account account: accounts) { String since_id = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + account.getId(), null); - notificationId = (int) Math.round(Double.parseDouble(account.getId())/1000); - new RetrieveHomeTimelineServiceAsyncTask(getContext(), account.getInstance(), since_id, account.getAcct(), account.getId(), HomeTimelineSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + notificationId = (int) Math.round(Double.parseDouble(account.getId())/100); + new RetrieveHomeTimelineServiceAsyncTask(getContext(), account.getInstance(), account.getToken(), since_id, account.getAcct(), account.getId(), HomeTimelineSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } } @@ -124,45 +124,43 @@ public class HomeTimelineSyncJob extends Job implements OnRetrieveHomeTimelineSe String max_id = sharedpreferences.getString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, null); //No previous notifications in cache, so no notification will be sent - if( max_id != null ){ - String message; - String title = null; - for(Status status: statuses){ - //The notification associated to max_id is discarded as it is supposed to have already been sent - //Also, if the toot comes from the owner, we will avoid to warn him/her... - if( status.getId().equals(max_id) || status.getAccount().getAcct().trim().equals(acct.trim())) - continue; - String notificationUrl = status.getAccount().getAvatar(); - if( notificationUrl != null && icon_notification == null){ - try { - 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); - icon_notification = imageLoaderNoty.loadImageSync(notificationUrl); - title = getContext().getResources().getString(R.string.notif_pouet, status.getAccount().getDisplay_name()); - }catch (Exception e){ - icon_notification = BitmapFactory.decodeResource(getContext().getResources(), - R.drawable.mastodonlogo); - } + String message; + String title = null; + for(Status status: statuses){ + //The notification associated to max_id is discarded as it is supposed to have already been sent + //Also, if the toot comes from the owner, we will avoid to warn him/her... + if( (max_id != null && status.getId().equals(max_id)) || status.getAccount().getAcct().trim().equals(acct.trim())) + continue; + String notificationUrl = status.getAccount().getAvatar(); + if( notificationUrl != null && icon_notification == null){ + try { + 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); + icon_notification = imageLoaderNoty.loadImageSync(notificationUrl); + title = getContext().getResources().getString(R.string.notif_pouet, status.getAccount().getUsername()); + }catch (Exception e){ + icon_notification = BitmapFactory.decodeResource(getContext().getResources(), + R.drawable.mastodonlogo); } } - if(statuses.size() > 0 ) - message = getContext().getResources().getQuantityString(R.plurals.other_notif_hometimeline, statuses.size(), statuses.size()); - 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, HOME_TIMELINE_INTENT); - intent.putExtra(PREF_KEY_ID, userId); - notify_user(getContext(), intent, notificationId, icon_notification,title,message); } + if(statuses.size() > 0 ) + message = getContext().getResources().getQuantityString(R.plurals.other_notif_hometimeline, statuses.size(), statuses.size()); + 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, HOME_TIMELINE_INTENT); + intent.putExtra(PREF_KEY_ID, userId); + notify_user(getContext(), intent, notificationId, icon_notification,title,message); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Helper.LAST_HOMETIMELINE_MAX_ID + userId, statuses.get(0).getId()); editor.apply(); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java index e4ba10a12..2abb47697 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java @@ -22,6 +22,7 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.support.annotation.NonNull; +import android.util.Log; import com.evernote.android.job.Job; @@ -117,8 +118,8 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications //Retrieve users in db that owner has. for (Account account: accounts) { String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId(), null); - notificationId = (int) Math.round(Double.parseDouble(account.getId())/1000); - new RetrieveNotificationsAsyncTask(getContext(), account.getInstance(), max_id, account.getAcct(), account.getId(), NotificationsSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + notificationId = (int) Math.round(Double.parseDouble(account.getId())/100); + new RetrieveNotificationsAsyncTask(getContext(), account.getInstance(), account.getToken(), max_id, account.getAcct(), account.getId(), NotificationsSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } } } @@ -133,97 +134,95 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications 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_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); String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, null); //No previous notifications in cache, so no notification will be sent - if( max_id != null ){ - int newFollows = 0; - int newAdds = 0; - int newAsks = 0; - int newMentions = 0; - int newShare = 0; - String notificationUrl = null; - String title = null; - String message; - for(Notification notification: notifications){ - //The notification associated to max_id is discarded as it is supposed to have already been sent - if( notification.getId().equals(max_id)) - continue; - switch (notification.getType()){ - case "mention": - if(notif_mention){ - newMentions++; - if( notificationUrl == null){ - notificationUrl = notification.getAccount().getAvatar(); - title = String.format("@%s %s", notification.getAccount().getAcct(),getContext().getString(R.string.notif_mention)); - } + int newFollows = 0; + int newAdds = 0; + int newAsks = 0; + int newMentions = 0; + int newShare = 0; + String notificationUrl = null; + String title = null; + String message; + 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(); + title = String.format("@%s %s", notification.getAccount().getUsername(),getContext().getString(R.string.notif_mention)); } - break; - case "reblog": - if(notif_share){ - newShare++; - if( notificationUrl == null){ - notificationUrl = notification.getAccount().getAvatar(); - title = String.format("@%s %s", notification.getAccount().getAcct(),getContext().getString(R.string.notif_reblog)); - } - } - break; - case "favourite": - if(notif_add){ - newAdds++; - if( notificationUrl == null){ - notificationUrl = notification.getAccount().getAvatar(); - title = String.format("@%s %s", notification.getAccount().getAcct(),getContext().getString(R.string.notif_favourite)); - } - } - break; - case "follow": - if(notif_follow){ - newFollows++; - if( notificationUrl == null){ - notificationUrl = notification.getAccount().getAvatar(); - title = String.format("@%s %s", notification.getAccount().getAcct(),getContext().getString(R.string.notif_follow)); - } - } - break; - default: - } - if( notificationUrl != null && icon_notification == null){ - try { - 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); - icon_notification = imageLoaderNoty.loadImageSync(notificationUrl); - }catch (Exception e){ - icon_notification = BitmapFactory.decodeResource(getContext().getResources(), - R.drawable.mastodonlogo); } - } + break; + case "reblog": + if(notif_share){ + newShare++; + if( notificationUrl == null){ + notificationUrl = notification.getAccount().getAvatar(); + title = String.format("@%s %s", notification.getAccount().getUsername(),getContext().getString(R.string.notif_reblog)); + } + } + break; + case "favourite": + if(notif_add){ + newAdds++; + if( notificationUrl == null){ + notificationUrl = notification.getAccount().getAvatar(); + title = String.format("@%s %s", notification.getAccount().getUsername(),getContext().getString(R.string.notif_favourite)); + } + } + break; + case "follow": + if(notif_follow){ + newFollows++; + if( notificationUrl == null){ + notificationUrl = notification.getAccount().getAvatar(); + title = String.format("@%s %s", notification.getAccount().getUsername(),getContext().getString(R.string.notif_follow)); + } + } + break; + default: } - 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); - notify_user(getContext(), intent, notificationId, icon_notification,title,message); + if( notificationUrl != null && icon_notification == null){ + try { + 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); + icon_notification = imageLoaderNoty.loadImageSync(notificationUrl); + }catch (Exception e){ + icon_notification = BitmapFactory.decodeResource(getContext().getResources(), + R.drawable.mastodonlogo); + } } } + 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); + notify_user(getContext(), intent, notificationId, icon_notification,title,message); + } + SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notifications.get(0).getId()); editor.apply();