some fixes

This commit is contained in:
Thomas 2022-04-28 18:41:53 +02:00
parent 4767712fb0
commit 0e933cfd50
6 changed files with 124 additions and 55 deletions

View File

@ -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"

View File

@ -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
*

View File

@ -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<String, String> 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<List<Notification>> 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 != null ? finalNotificationUrl : R.drawable.fedilab_logo_bubbles)
.listener(new RequestListener<Bitmap>() {
Handler mainHandler = new Handler(Looper.getMainLooper());
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
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<Bitmap>() {
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> 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();
}
@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;
}
})
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> 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<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> 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) {
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
mainHandler.post(myRunnable);
}
}
});
mainHandler.post(myRunnable);
}
}

View File

@ -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 <http://www.gnu.org/licenses>. */
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<Status> 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();
}
}
}
}

View File

@ -188,7 +188,7 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
boolean capitalize = sharedpreferences.getBoolean(context.getString(R.string.SET_CAPITALIZE), true);
if (inReplyToUser != null) {
if (capitalize) {
statusDraft.text = inReplyToUser.acct + "\n\n";
statusDraft.text = inReplyToUser.acct + "\n";
} else {
statusDraft.text = inReplyToUser.acct + " ";
}
@ -196,9 +196,9 @@ public class ComposeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
holder.binding.content.setText(statusDraft.text);
statusDraft.cursorPosition = statusDraft.text.length();
if (statusDraft.mentions.size() > 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<RecyclerView.ViewHolder
pickupMedia(ComposeActivity.mediaType.ALL, position);
});
if (statusDraft.visibility == null) {
statusDraft.visibility = BaseMainActivity.accountWeakReference.get().mastodon_account.source.privacy;
if (position > 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);

View File

@ -1584,6 +1584,7 @@
<string name="more_actions">More Actions</string>
<string name="types_of_notifications_to_display">Types of notifications to display</string>
<string name="set_unfollow_validation_title">Confirm unfollows</string>
<string name="message_has_been_sent">Message has been sent!</string>
</resources>