From 0e933cfd501f65d11455469fc2f63db2bc20b239 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 28 Apr 2022 18:41:53 +0200 Subject: [PATCH] some fixes --- app/build.gradle | 4 +- .../android/client/entities/Account.java | 20 +++- .../android/helper/NotificationsHelper.java | 99 ++++++++++--------- .../android/services/PostMessageService.java | 42 +++++++- .../android/ui/drawer/ComposeAdapter.java | 13 ++- app/src/main/res/values/strings.xml | 1 + 6 files changed, 124 insertions(+), 55 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 302fde2a..865ac1df 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -26,13 +26,13 @@ android { } productFlavors { fdroid { - applicationId "fr.gouv.etalab.mastodon" + applicationId "fr.gouv.etalab.mastodon.test" buildConfigField "boolean", "DONATIONS", "true" buildConfigField "boolean", "push", "false" flavor = "fdroid" } playstore { - applicationId "app.fedilab.android" + applicationId "app.fedilab.android.test" buildConfigField "boolean", "DONATIONS", "false" buildConfigField "boolean", "push", "true" flavor = "playstore" diff --git a/app/src/main/java/app/fedilab/android/client/entities/Account.java b/app/src/main/java/app/fedilab/android/client/entities/Account.java index b95f17ad..47be0b1d 100644 --- a/app/src/main/java/app/fedilab/android/client/entities/Account.java +++ b/app/src/main/java/app/fedilab/android/client/entities/Account.java @@ -236,7 +236,7 @@ public class Account implements Serializable { } /** - * Returns an Account by token + * Returns an Account by userId and instance * * @param userId String * @param instance String @@ -254,6 +254,24 @@ public class Account implements Serializable { } } + /** + * Returns an Account by token + * + * @param token String + * @return Account {@link Account} + */ + public Account getAccountByToken(String token) throws DBException { + if (db == null) { + throw new DBException("db is null. Wrong initialization."); + } + try { + Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_TOKEN + " = \"" + token + "\"", null, null, null, null, "1"); + return cursorToUser(c); + } catch (Exception e) { + return null; + } + } + /** * Returns authenticated Account * diff --git a/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java b/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java index f87b2fe3..1876bc7c 100644 --- a/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/NotificationsHelper.java @@ -43,6 +43,7 @@ import com.bumptech.glide.request.transition.Transition; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.concurrent.TimeUnit; @@ -62,6 +63,7 @@ import retrofit2.converter.gson.GsonConverterFactory; public class NotificationsHelper { + public static HashMap since_ids = new HashMap<>(); public static void task(Context context, String slug) throws DBException { @@ -73,6 +75,10 @@ public class NotificationsHelper { return; } String last_notifid = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + slug, null); + if (since_ids.containsKey(slug)) { + last_notifid = since_ids.get(slug); + } + //Check which notifications the user wants to see boolean notif_follow = prefs.getBoolean(context.getString(R.string.SET_NOTIF_FOLLOW), true); boolean notif_mention = prefs.getBoolean(context.getString(R.string.SET_NOTIF_MENTION), true); @@ -84,11 +90,12 @@ public class NotificationsHelper { return; //Nothing is done MastodonNotificationsService mastodonNotificationsService = init(context, slugArray[1]); + String finalLast_notifid = last_notifid; new Thread(() -> { Notifications notifications = new Notifications(); Call> notificationsCall; - if (last_notifid != null) { - notificationsCall = mastodonNotificationsService.getNotifications(accountDb.token, null, null, null, last_notifid, null, 30); + if (finalLast_notifid != null) { + notificationsCall = mastodonNotificationsService.getNotifications(accountDb.token, null, null, null, finalLast_notifid, null, 30); } else { notificationsCall = mastodonNotificationsService.getNotifications(accountDb.token, null, null, null, null, null, 5); } @@ -98,6 +105,9 @@ public class NotificationsHelper { if (notificationsResponse.isSuccessful()) { notifications.notifications = notificationsResponse.body(); if (notifications.notifications != null) { + if (notifications.notifications.size() > 0) { + since_ids.put(slug, notifications.notifications.get(0).id); + } for (Notification notification : notifications.notifications) { if (notification != null && notification.status != null) { notification.status = SpannableHelper.convertStatus(context.getApplicationContext(), notification.status); @@ -285,57 +295,52 @@ public class NotificationsHelper { intent.putExtra(Helper.INTENT_TARGETED_ACCOUNT, targeted_account); intent.putExtra(Helper.PREF_INSTANCE, account.instance); notificationUrl = notifications.get(0).account.avatar; - if (notificationUrl != null) { - - Handler mainHandler = new Handler(Looper.getMainLooper()); - - final String finalNotificationUrl = notificationUrl; - Helper.NotifType finalNotifType = notifType; - String finalMessage = message; - String finalMessage1 = message; - Runnable myRunnable = () -> Glide.with(context) - .asBitmap() - .load(finalNotificationUrl) - .listener(new RequestListener() { + Handler mainHandler = new Handler(Looper.getMainLooper()); + final String finalNotificationUrl = notificationUrl; + Helper.NotifType finalNotifType = notifType; + String finalMessage = message; + String finalMessage1 = message; + Runnable myRunnable = () -> Glide.with(context) + .asBitmap() + .load(finalNotificationUrl != null ? finalNotificationUrl : R.drawable.fedilab_logo_bubbles) + .listener(new RequestListener() { - @Override - public boolean onResourceReady(Bitmap resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { - return false; - } + @Override + public boolean onResourceReady(Bitmap resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + return false; + } - @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { - notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(), - R.mipmap.ic_launcher), finalNotifType, context.getString(R.string.top_notification), finalMessage1); - String lastNotif = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, null); - if (lastNotif == null || notifications.get(0).id.compareTo(lastNotif) > 0) { - SharedPreferences.Editor editor = prefs.edit(); - editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id); - editor.apply(); - } - return false; + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(), + R.mipmap.ic_launcher), finalNotifType, context.getString(R.string.top_notification), finalMessage1); + String lastNotif = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, null); + if (lastNotif == null || notifications.get(0).id.compareTo(lastNotif) > 0) { + SharedPreferences.Editor editor = prefs.edit(); + editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id); + editor.apply(); } - }) - .into(new CustomTarget() { - @Override - public void onResourceReady(@NonNull Bitmap resource, Transition transition) { - notify_user(context, account, intent, resource, finalNotifType, context.getString(R.string.top_notification), finalMessage); - String lastNotif = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, null); - if (lastNotif == null || notifications.get(0).id.compareTo(lastNotif) > 0) { - SharedPreferences.Editor editor = prefs.edit(); - editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id); - editor.apply(); - } + return false; + } + }) + .into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Bitmap resource, Transition transition) { + notify_user(context, account, intent, resource, finalNotifType, context.getString(R.string.top_notification), finalMessage); + String lastNotif = prefs.getString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, null); + if (lastNotif == null || notifications.get(0).id.compareTo(lastNotif) > 0) { + SharedPreferences.Editor editor = prefs.edit(); + editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id); + editor.apply(); } + } - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }); - mainHandler.post(myRunnable); + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { - } + } + }); + mainHandler.post(myRunnable); } } diff --git a/app/src/main/java/app/fedilab/android/services/PostMessageService.java b/app/src/main/java/app/fedilab/android/services/PostMessageService.java index 430ff3ac..a80ea37e 100644 --- a/app/src/main/java/app/fedilab/android/services/PostMessageService.java +++ b/app/src/main/java/app/fedilab/android/services/PostMessageService.java @@ -14,6 +14,9 @@ package app.fedilab.android.services; * You should have received a copy of the GNU General Public License along with Fedilab; if not, * see . */ +import static app.fedilab.android.helper.Helper.NotifType.TOOT; +import static app.fedilab.android.helper.Helper.notify_user; + import android.app.IntentService; import android.app.Notification; import android.app.NotificationChannel; @@ -23,6 +26,7 @@ import android.content.Intent; import android.graphics.BitmapFactory; import android.os.Build; import android.os.Bundle; +import android.text.Html; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -31,10 +35,13 @@ import androidx.core.app.NotificationCompat; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import java.util.concurrent.TimeUnit; import app.fedilab.android.BaseMainActivity; import app.fedilab.android.R; +import app.fedilab.android.activities.ContextActivity; +import app.fedilab.android.client.entities.Account; import app.fedilab.android.client.entities.PostState; import app.fedilab.android.client.entities.StatusDraft; import app.fedilab.android.client.mastodon.MastodonStatusesService; @@ -136,6 +143,7 @@ public class PostMessageService extends IntentService { } MastodonStatusesService mastodonStatusesService = init(instance); boolean error = false; + Status firstSendMessage = null; if (statusDraft != null && statusDraft.statusDraftList != null && statusDraft.statusDraftList.size() > 0) { //If state is null, it is created (typically when submitting the status the first time) if (statusDraft.state == null) { @@ -156,8 +164,12 @@ public class PostMessageService extends IntentService { } startingPosition++; } - String in_reply_to_status = null; + List statuses = statusDraft.statusDraftList; + String in_reply_to_status = null; + if (statusDraft.statusReplyList != null && statusDraft.statusReplyList.size() > 0) { + in_reply_to_status = statusDraft.statusReplyList.get(statusDraft.statusReplyList.size() - 1).id; + } totalMediaSize = 0; totalBitRead = 0; for (int i = startingPosition; i < statuses.size(); i++) { @@ -221,6 +233,9 @@ public class PostMessageService extends IntentService { if (statusResponse.isSuccessful()) { Status statusReply = statusResponse.body(); + if (firstSendMessage == null && statusReply != null) { + firstSendMessage = statusReply; + } if (statusReply != null) { in_reply_to_status = statusReply.id; statusDraft.state.posts_successfully_sent = i; @@ -284,6 +299,31 @@ public class PostMessageService extends IntentService { } } } + + if (scheduledDate == null && token != null) { + Account account = null; + try { + account = new Account(PostMessageService.this).getAccountByToken(token); + final Intent pendingIntent = new Intent(PostMessageService.this, ContextActivity.class); + pendingIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); + pendingIntent.putExtra(Helper.ARG_STATUS, firstSendMessage); + pendingIntent.putExtra(Helper.PREF_INSTANCE, account.instance); + String text = firstSendMessage.content; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) + text = Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY).toString(); + else + text = Html.fromHtml(text).toString(); + if (text.length() > 255) { + text = text.substring(0, 254); + text = String.format(Locale.getDefault(), "%s…", text); + } + notify_user(PostMessageService.this, account, pendingIntent, BitmapFactory.decodeResource(getResources(), + R.mipmap.ic_launcher), TOOT, getString(R.string.message_has_been_sent), text); + } catch (DBException e) { + e.printStackTrace(); + } + + } } } diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/ComposeAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/ComposeAdapter.java index 4c93a79e..f3340314 100644 --- a/app/src/main/java/app/fedilab/android/ui/drawer/ComposeAdapter.java +++ b/app/src/main/java/app/fedilab/android/ui/drawer/ComposeAdapter.java @@ -188,7 +188,7 @@ public class ComposeAdapter extends RecyclerView.Adapter 1) { - statusDraft.text += "\n\n"; + statusDraft.text += "\n"; for (Mention mention : statusDraft.mentions) { - if (mention.id != null && mention.acct != null && !mention.id.equals(BaseMainActivity.client_id)) { + if (mention.id != null && mention.acct != null && !mention.id.equals(BaseMainActivity.currentUserID)) { String tootTemp = String.format("@%s ", mention.acct); statusDraft.text = String.format("%s ", (statusDraft.text + tootTemp.trim())); } @@ -1006,8 +1006,13 @@ public class ComposeAdapter extends RecyclerView.Adapter 0) { + statusDraft.visibility = statusList.get(position - 1).visibility; + } else { + statusDraft.visibility = BaseMainActivity.accountWeakReference.get().mastodon_account.source.privacy; + } } + switch (statusDraft.visibility.toLowerCase()) { case "public": holder.binding.buttonVisibility.setImageResource(R.drawable.ic_compose_visibility_public); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e092c436..9210b245 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1584,6 +1584,7 @@ More Actions Types of notifications to display Confirm unfollows + Message has been sent!