Profilbild wird bei Benachrichtigungen angezeigt.

This commit is contained in:
Lyndon
2018-06-29 10:10:32 +02:00
parent 32555eff37
commit 1724c14ad3
5 changed files with 152 additions and 108 deletions

View File

@@ -21,6 +21,7 @@ import de.beyondsoft.ownchat.utils.Constants;
import de.beyondsoft.ownchat.utils.rx.RxUtils;
import rx.Observable;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
@@ -37,6 +38,7 @@ import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
@@ -44,7 +46,9 @@ import android.support.v4.app.RemoteInput;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
@@ -77,20 +81,6 @@ public class HandleMessagesService extends FirebaseMessagingService {
customizeNotification(remoteMessage.getData());
}
private static Bitmap getBitmapFromUrl(String pictureUrl) {
try {
URL url = new URL(pictureUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
Bitmap bitmap = decodeStream(connection.getInputStream());
return Bitmap.createScaledBitmap(bitmap, 240, 240, false);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private void customizeNotification(Map<String, String> dataMap) {
String messageId = dataMap.get(Constants.MESSAGE_ID);
@@ -101,6 +91,7 @@ public class HandleMessagesService extends FirebaseMessagingService {
.subscribe(this::getMessageSuccess, this::getMessageError);
}
@SuppressLint("StaticFieldLeak")
private void getMessageSuccess(Message message) {
Bundle bundle = new Bundle();
@@ -115,101 +106,109 @@ public class HandleMessagesService extends FirebaseMessagingService {
bundle.putString(Constants.USERNAME, message.senderName);
bundle.putString(Constants.USER_AVATAR, message.senderAvatar);
final Bitmap bitmap = getBitmapFromUrl(message.senderAvatar);
Intent intent = new Intent();
intent.putExtra(Constants.MESSAGE, bundle);
intent.setAction(Constants.NOTIFICATION_BROADCAST);
try {
new DownloadAvatarTask() {
@Override
protected void onPostExecute(Bitmap bitmap) {
Intent intent = new Intent();
intent.putExtra(Constants.MESSAGE, bundle);
intent.setAction(Constants.NOTIFICATION_BROADCAST);
sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle results = getResultExtras(true);
sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle results = getResultExtras(true);
if (!results.getBoolean(CHAT_IS_ALREADY_OPEN, false)) {
int groupId = (int) bundle.getLong(Constants.GROUP_ID);
int numberOfUsers = message.numberOfGroupUsers;
intent = MainActivity.createIntent(context, bundle, numberOfUsers, groupId);
if (!results.getBoolean(CHAT_IS_ALREADY_OPEN, false)) {
int groupId = (int) bundle.getLong(Constants.GROUP_ID);
int numberOfUsers = message.numberOfGroupUsers;
intent = MainActivity.createIntent(context, bundle, numberOfUsers, groupId);
PendingIntent pendingIntent = PendingIntent.getActivity(context, groupId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent = PendingIntent.getActivity(context, groupId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap bitmapToDisplay = bitmap;
if (bitmapToDisplay == null) {
bitmapToDisplay = BitmapFactory.decodeResource(context.getResources(), R.drawable.avatar);
}
Bitmap bitmapToDisplay = bitmap;
if (bitmapToDisplay == null) {
bitmapToDisplay = BitmapFactory.decodeResource(context.getResources(), R.drawable.avatar);
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationUtils notificationUtils = BewoplanerApplication.getNotificationUtils();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationUtils notificationUtils = BewoplanerApplication.getNotificationUtils();
if(notificationUtils.getManager() != null) {
Notification.Builder nb = new Notification.Builder(context, context.getString(R.string.default_notification_channel_id))
.setSmallIcon(getNotificationIcon())
.setContentTitle(setNotificationTitle(message, message.numberOfGroupUsers))
.setContentText(getTextFromNotification(message))
.setAutoCancel(false)
.setStyle(new Notification.BigTextStyle().bigText(getTextFromNotification(message)))
.setContentIntent(pendingIntent)
.setLargeIcon(getCircleBitmap(bitmapToDisplay))
.setGroup(String.valueOf(groupId))
.setGroupSummary(true)
.setColor(ContextCompat.getColor(context, R.color.colorDefaultBright));
if(notificationUtils.getManager() != null) {
Notification.Builder nb = new Notification.Builder(context, context.getString(R.string.default_notification_channel_id))
.setSmallIcon(getNotificationIcon())
.setContentTitle(setNotificationTitle(message, message.numberOfGroupUsers))
.setContentText(getTextFromNotification(message))
.setAutoCancel(false)
.setStyle(new Notification.BigTextStyle().bigText(getTextFromNotification(message)))
.setContentIntent(pendingIntent)
.setLargeIcon(getCircleBitmap(bitmapToDisplay))
.setGroup(String.valueOf(groupId))
.setGroupSummary(true)
.setColor(ContextCompat.getColor(context, R.color.colorDefaultBright));
Notification.Action action2 = createAction2(context, numberOfUsers, groupId, bundle);
Notification.Action action2 = createAction2(context, numberOfUsers, groupId, bundle);
if(action2 != null) {
nb.addAction(action2);
if(action2 != null) {
nb.addAction(action2);
}
notificationUtils.getManager().notify(groupId, nb.build());
}
} else {
NotificationCompat.Action action = createAction(context, numberOfUsers, groupId, bundle);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, getString(R.string.default_notification_channel_id))
.setSmallIcon(getNotificationIcon())
.setContentTitle(setNotificationTitle(message, message.numberOfGroupUsers))
.setContentText(getTextFromNotification(message))
.setAutoCancel(false)
.setStyle(new NotificationCompat.BigTextStyle().bigText(getTextFromNotification(message)))
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setLargeIcon(getCircleBitmap(bitmapToDisplay))
.setGroup(String.valueOf(groupId))
.setGroupSummary(true)
.setDefaults(Notification.DEFAULT_ALL)
.setColor(ContextCompat.getColor(context, R.color.colorDefaultBright));
if (action != null) {
builder.addAction(action);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(notificationManager != null) {
notificationManager.notify(groupId, builder.build());
}
}
Message message = new Message(bundle.getString(Constants.MESSAGE_ID),
groupId,
bundle.getLong(Constants.CREATED_AT),
bundle.getString(Constants.MESSAGE_TEXT),
bundle.getString(Constants.FILE),
bundle.getString(Constants.SMALLER_IMAGE),
bundle.getString(Constants.FILENAME),
null,
bundle.getLong(Constants.USER_ID),
bundle.getString(Constants.USERNAME),
bundle.getString(Constants.USER_AVATAR),
Message.Status.DELIVERED,
false,
mUserIdReference.get(),
false);
storeMessageLocally(message);
storeGroupLocally(groupId, message);
}
notificationUtils.getManager().notify(groupId, nb.build());
}
} else {
NotificationCompat.Action action = createAction(context, numberOfUsers, groupId, bundle);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, getString(R.string.default_notification_channel_id))
.setSmallIcon(getNotificationIcon())
.setContentTitle(setNotificationTitle(message, message.numberOfGroupUsers))
.setContentText(getTextFromNotification(message))
.setAutoCancel(false)
.setStyle(new NotificationCompat.BigTextStyle().bigText(getTextFromNotification(message)))
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setLargeIcon(getCircleBitmap(bitmapToDisplay))
.setGroup(String.valueOf(groupId))
.setGroupSummary(true)
.setDefaults(Notification.DEFAULT_ALL)
.setColor(ContextCompat.getColor(context, R.color.colorDefaultBright));
if (action != null) {
builder.addAction(action);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if(notificationManager != null) {
notificationManager.notify(groupId, builder.build());
}
}
Message message = new Message(bundle.getString(Constants.MESSAGE_ID),
groupId,
bundle.getLong(Constants.CREATED_AT),
bundle.getString(Constants.MESSAGE_TEXT),
bundle.getString(Constants.FILE),
bundle.getString(Constants.SMALLER_IMAGE),
bundle.getString(Constants.FILENAME),
null,
bundle.getLong(Constants.USER_ID),
bundle.getString(Constants.USERNAME),
bundle.getString(Constants.USER_AVATAR),
Message.Status.DELIVERED,
false,
mUserIdReference.get(),
false);
storeMessageLocally(message);
storeGroupLocally(groupId, message);
}, null, Activity.RESULT_OK, null, null);
}
}
}, null, Activity.RESULT_OK, null, null);
}.execute(new URL(message.senderAvatar));
} catch(MalformedURLException e) {
e.printStackTrace();
}
}
private void getMessageError(Throwable throwable) {
@@ -326,4 +325,48 @@ public class HandleMessagesService extends FirebaseMessagingService {
return null;
}
private static class DownloadAvatarTask extends AsyncTask<URL, Void, Bitmap> {
@Override
protected Bitmap doInBackground(URL... urls) {
if(urls == null || urls.length == 0) {
return null;
}
HttpURLConnection connection;
try {
connection = (HttpURLConnection) urls[0].openConnection();
connection.setDoInput(true);
connection.connect();
Bitmap bitmap = decodeStream(connection.getInputStream());
final int minSize = 240;
int outWidth, outHeight, x, y;
int inWidth = bitmap.getWidth();
int inHeight = bitmap.getHeight();
if(inWidth > inHeight) {
outHeight = minSize;
outWidth = (minSize / inHeight) * inWidth;
x = (outWidth - minSize) / 2;
y = 0;
} else {
outWidth = minSize;
outHeight = (minSize / inWidth) * inHeight;
x = 0;
y = (outHeight - minSize) / 2;
}
return Bitmap.createBitmap(Bitmap.createScaledBitmap(bitmap, outWidth, outHeight, false), x, y, 240, 240);
} catch(IOException e) {
e.printStackTrace();
}
return null;
}
}
}

View File

@@ -51,6 +51,7 @@ import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SimpleItemAnimator;
import android.text.Editable;
import android.text.TextUtils;
@@ -110,6 +111,9 @@ abstract class BaseChatActivity extends RuntimePermissionActivity implements Cha
@BindView(R.id.chat_toolbar_extra_image)
ImageView mExtraImageView;
@BindView(R.id.chatSearchView)
SearchView mChatSearchView;
@BindView(R.id.chat_message)
EmojiconEditText mChatMessageEmojiEditText;

View File

@@ -16,6 +16,9 @@ import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
public class SingleChatActivity extends BaseChatActivity {
@@ -112,6 +115,7 @@ public class SingleChatActivity extends BaseChatActivity {
GlideApp.with(this)
.asBitmap()
.load(mGroup.avatar)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.avatar)
.into(mChatImageView);
}

View File

@@ -227,7 +227,7 @@ public class ContactsFragment extends BaseFragment implements ContactsView, Cont
@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
if(TextUtils.isEmpty(newText)) {
mCallback.loadGroupsFromDB();
} else {
mCallback.loadGroupsContainingName(newText);

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/chat_app_bar"
android:layout_width="match_parent"
@@ -17,7 +18,6 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:contentDescription="@null"
android:padding="5dp"
@@ -29,8 +29,6 @@
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_marginLeft="66dp"
android:layout_marginRight="10dp"
android:layout_marginStart="66dp"
android:contentDescription="@null"
tools:src="@drawable/avatar" />
@@ -41,7 +39,6 @@
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/chat_toolbar_image_view"
android:layout_toRightOf="@+id/chat_toolbar_image_view"
android:textColor="@color/colorBlack"
android:textSize="15sp"
android:textStyle="bold"
@@ -53,8 +50,6 @@
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/chat_toolbar_image_view"
android:layout_toLeftOf="@+id/chat_toolbar_extra_image"
android:layout_toRightOf="@+id/chat_toolbar_image_view"
android:layout_toStartOf="@+id/chat_toolbar_extra_image"
android:orientation="vertical">
@@ -88,10 +83,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="11dp"
android:layout_marginRight="11dp"
android:contentDescription="@null"
android:padding="5dp"
tools:src="@drawable/call" />