conversations-classic/src/de/gultsch/chat/utils/UIHelper.java

157 lines
5.1 KiB
Java
Raw Normal View History

2014-02-03 17:38:47 +00:00
package de.gultsch.chat.utils;
2014-02-10 02:34:00 +00:00
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
2014-02-03 17:38:47 +00:00
import java.text.SimpleDateFormat;
import java.util.Date;
2014-02-10 21:45:59 +00:00
import java.util.List;
2014-02-03 17:38:47 +00:00
import de.gultsch.chat.R;
2014-02-10 02:34:00 +00:00
import de.gultsch.chat.entities.Contact;
2014-02-03 17:38:47 +00:00
import de.gultsch.chat.entities.Conversation;
2014-02-10 21:45:59 +00:00
import de.gultsch.chat.entities.Message;
2014-02-03 17:38:47 +00:00
import de.gultsch.chat.ui.ConversationActivity;
2014-02-10 02:34:00 +00:00
import android.app.Activity;
2014-02-03 17:38:47 +00:00
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.net.Uri;
import android.preference.PreferenceManager;
2014-02-10 02:34:00 +00:00
import android.provider.ContactsContract.Contacts;
2014-02-03 17:38:47 +00:00
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
2014-02-10 02:34:00 +00:00
import android.widget.QuickContactBadge;
2014-02-03 17:38:47 +00:00
public class UIHelper {
public static String readableTimeDifference(long time) {
if (time == 0) {
return "just now";
}
Date date = new Date(time);
long difference = (System.currentTimeMillis() - time) / 1000;
if (difference < 60) {
return "just now";
} else if (difference < 60 * 10) {
return difference / 60 + " min ago";
} else if (difference < 60 * 60 * 24) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
return sdf.format(date);
} else {
SimpleDateFormat sdf = new SimpleDateFormat("M/D");
return sdf.format(date);
}
}
public static Bitmap getUnknownContactPicture(String name, int size) {
String firstLetter = name.substring(0, 1).toUpperCase();
int holoColors[] = { 0xFF1da9da, 0xFFb368d9, 0xFF83b600, 0xFFffa713,
0xFFe92727 };
int color = holoColors[Math.abs(name.hashCode()) % holoColors.length];
2014-02-03 17:38:47 +00:00
Bitmap bitmap = Bitmap
.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
bitmap.eraseColor(color);
Paint paint = new Paint();
paint.setColor(0xffe5e5e5);
paint.setTextSize((float) (size * 0.9));
paint.setAntiAlias(true);
Rect rect = new Rect();
paint.getTextBounds(firstLetter, 0, 1, rect);
float width = paint.measureText(firstLetter);
canvas.drawText(firstLetter, (size / 2) - (width / 2), (size / 2)
+ (rect.height() / 2), paint);
return bitmap;
}
2014-02-10 02:34:00 +00:00
public static Notification getUnreadMessageNotification(Context context,
Conversation conversation) {
SharedPreferences sharedPref = PreferenceManager
.getDefaultSharedPreferences(context);
String ringtone = sharedPref.getString("notification_ringtone", null);
2014-02-03 17:38:47 +00:00
Resources res = context.getResources();
2014-02-10 02:34:00 +00:00
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
mBuilder.setLargeIcon(UIHelper.getUnknownContactPicture(conversation
.getName(), (int) res
.getDimension(android.R.dimen.notification_large_icon_width)));
2014-02-03 17:38:47 +00:00
mBuilder.setContentTitle(conversation.getName());
2014-02-13 22:40:08 +00:00
mBuilder.setTicker(conversation.getLatestMessage().getBody().trim());
2014-02-10 21:45:59 +00:00
StringBuilder bigText = new StringBuilder();
List<Message> messages = conversation.getMessages();
String firstLine = "";
for(int i = messages.size() -1; i >= 0; --i) {
if (!messages.get(i).isRead()) {
if (i == messages.size() -1 ) {
firstLine = messages.get(i).getBody().trim();
bigText.append(firstLine);
} else {
firstLine = messages.get(i).getBody().trim();
bigText.insert(0, firstLine+"\n");
}
} else {
break;
}
}
mBuilder.setContentText(firstLine);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText.toString()));
2014-02-03 17:38:47 +00:00
mBuilder.setSmallIcon(R.drawable.notification);
mBuilder.setLights(0xffffffff, 2000, 4000);
2014-02-10 02:34:00 +00:00
if (ringtone != null) {
2014-02-03 17:38:47 +00:00
mBuilder.setSound(Uri.parse(ringtone));
}
2014-02-10 02:34:00 +00:00
2014-02-03 17:38:47 +00:00
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(ConversationActivity.class);
2014-02-10 02:34:00 +00:00
Intent viewConversationIntent = new Intent(context,
ConversationActivity.class);
2014-02-03 17:38:47 +00:00
viewConversationIntent.setAction(Intent.ACTION_VIEW);
2014-02-10 02:34:00 +00:00
viewConversationIntent.putExtra(ConversationActivity.CONVERSATION,
2014-02-03 17:38:47 +00:00
conversation.getUuid());
2014-02-10 02:34:00 +00:00
viewConversationIntent.setType(ConversationActivity.VIEW_CONVERSATION);
2014-02-03 17:38:47 +00:00
stackBuilder.addNextIntent(viewConversationIntent);
2014-02-10 02:34:00 +00:00
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
2014-02-03 17:38:47 +00:00
mBuilder.setContentIntent(resultPendingIntent);
return mBuilder.build();
}
2014-02-10 02:34:00 +00:00
public static void prepareContactBadge(final Activity activity,
QuickContactBadge badge, final Contact contact) {
if (contact.getSystemAccount()!=null) {
String[] systemAccount = contact.getSystemAccount().split("#");
long id = Long.parseLong(systemAccount[0]);
badge.assignContactUri(Contacts.getLookupUri(id, systemAccount[1]));
if (contact.getProfilePhoto() != null) {
badge.setImageURI(Uri.parse(contact.getProfilePhoto()));
} else {
badge.setImageBitmap(UIHelper.getUnknownContactPicture(contact.getDisplayName(), 400));
}
} else {
badge.setImageBitmap(UIHelper.getUnknownContactPicture(contact.getDisplayName(), 400));
}
}
2014-02-03 17:38:47 +00:00
}