Use in app notification when posting

This commit is contained in:
Thomas 2022-05-15 18:19:00 +02:00
parent d66c056b45
commit 73b7662f45
5 changed files with 35 additions and 28 deletions

View file

@ -69,6 +69,7 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.google.android.material.snackbar.Snackbar;
import com.jaredrummler.cyanea.Cyanea;
import java.io.File;
@ -79,6 +80,7 @@ import java.util.regex.Pattern;
import app.fedilab.android.activities.ActionActivity;
import app.fedilab.android.activities.BaseActivity;
import app.fedilab.android.activities.ComposeActivity;
import app.fedilab.android.activities.ContextActivity;
import app.fedilab.android.activities.DraftActivity;
import app.fedilab.android.activities.FilterActivity;
import app.fedilab.android.activities.InstanceActivity;
@ -99,6 +101,7 @@ import app.fedilab.android.client.entities.app.PinnedTimeline;
import app.fedilab.android.client.mastodon.entities.Filter;
import app.fedilab.android.client.mastodon.entities.Instance;
import app.fedilab.android.client.mastodon.entities.MastodonList;
import app.fedilab.android.client.mastodon.entities.Status;
import app.fedilab.android.databinding.ActivityMainBinding;
import app.fedilab.android.databinding.NavHeaderMainBinding;
import app.fedilab.android.exception.DBException;
@ -143,6 +146,19 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
redrawPinned(mastodonLists);
} else if (b.getBoolean(Helper.RECEIVE_RECREATE_ACTIVITY, false)) {
Cyanea.getInstance().edit().apply().recreate(BaseMainActivity.this);
} else if (b.getBoolean(Helper.RECEIVE_NEW_MESSAGE, false)) {
Status statusSent = (Status) b.getSerializable(Helper.RECEIVE_STATUS_ACTION);
Snackbar.make(binding.getRoot(), getString(R.string.message_has_been_sent), Snackbar.LENGTH_LONG)
.setAction(getString(R.string.display), view -> {
Intent intentContext = new Intent(BaseMainActivity.this, ContextActivity.class);
intentContext.putExtra(Helper.ARG_STATUS, statusSent);
intentContext.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentContext);
})
.setTextColor(ThemeHelper.getAttColor(BaseMainActivity.this, R.attr.mTextColor))
.setActionTextColor(ContextCompat.getColor(BaseMainActivity.this, R.color.cyanea_accent_reference))
.setBackgroundTint(ContextCompat.getColor(BaseMainActivity.this, R.color.cyanea_primary_dark_reference))
.show();
}
}
}

View file

@ -165,6 +165,7 @@ public class Helper {
public static final String RECEIVE_STATUS_ACTION = "RECEIVE_STATUS_ACTION";
public static final String RECEIVE_RECREATE_ACTIVITY = "RECEIVE_RECREATE_ACTIVITY";
public static final String RECEIVE_NEW_MESSAGE = "RECEIVE_NEW_MESSAGE";
public static final String RECEIVE_MASTODON_LIST = "RECEIVE_MASTODON_LIST";
public static final String RECEIVE_REDRAW_PROFILE = "RECEIVE_REDRAW_PROFILE";

View file

@ -22,6 +22,7 @@ import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
@ -62,6 +63,15 @@ public class ThemeHelper {
return typedValue.data;
}
public static int fetchAccentColor(Context context) {
TypedValue typedValue = new TypedValue();
TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[]{R.attr.colorAccent});
int color = a.getColor(0, 0);
a.recycle();
return color;
}
/**
* Initialize colors in a static variable
* Currently link_color cannot be retrieved with getAttColor in ViewModel due to application and theme

View file

@ -14,9 +14,6 @@ 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;
@ -27,22 +24,20 @@ import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
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.activities.MainActivity;
import app.fedilab.android.client.entities.Account;
import app.fedilab.android.client.entities.PostState;
@ -330,28 +325,12 @@ public class PostMessageService extends IntentService {
}
if (scheduledDate == null && token != null && firstSendMessage != null) {
Account account;
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();
}
Bundle b = new Bundle();
b.putBoolean(Helper.RECEIVE_NEW_MESSAGE, true);
Intent intentBD = new Intent(Helper.BROADCAST_DATA);
b.putSerializable(Helper.RECEIVE_STATUS_ACTION, firstSendMessage);
intentBD.putExtras(b);
LocalBroadcastManager.getInstance(PostMessageService.this).sendBroadcast(intentBD);
}
notificationManager.cancel(NOTIFICATION_INT_CHANNEL_ID);
}

View file

@ -1593,6 +1593,7 @@
<string name="poll_type">Poll type:</string>
<string name="poll_duration">Poll duration:</string>
<string name="set_display_bookmark_indication">Always display bookmark button</string>
<string name="display">Display</string>
</resources>