aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/services
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/pixart/messenger/services')
-rw-r--r--src/main/java/de/pixart/messenger/services/AvatarService.java35
-rw-r--r--src/main/java/de/pixart/messenger/services/ShortcutService.java42
2 files changed, 69 insertions, 8 deletions
diff --git a/src/main/java/de/pixart/messenger/services/AvatarService.java b/src/main/java/de/pixart/messenger/services/AvatarService.java
index e7096a558..766c2e240 100644
--- a/src/main/java/de/pixart/messenger/services/AvatarService.java
+++ b/src/main/java/de/pixart/messenger/services/AvatarService.java
@@ -1,6 +1,8 @@
package de.pixart.messenger.services;
+import android.content.res.Resources;
import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
@@ -21,6 +23,7 @@ import java.util.Locale;
import java.util.Set;
import de.pixart.messenger.Config;
+import de.pixart.messenger.R;
import de.pixart.messenger.entities.Account;
import de.pixart.messenger.entities.Bookmark;
import de.pixart.messenger.entities.Contact;
@@ -80,21 +83,47 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded {
}
public Bitmap getRoundedShortcut(final Contact contact) {
+ return getRoundedShortcut(contact, false);
+ }
+
+ public Bitmap getRoundedShortcutWithIcon(final Contact contact) {
+ return getRoundedShortcut(contact, true);
+ }
+
+ private Bitmap getRoundedShortcut(final Contact contact, boolean withIcon) {
DisplayMetrics metrics = mXmppConnectionService.getResources().getDisplayMetrics();
int size = Math.round(metrics.density * 48);
Bitmap bitmap = get(contact, size);
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
-
final Paint paint = new Paint();
- final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
+ drawAvatar(bitmap, canvas, paint);
+ if (withIcon) {
+ drawIcon(canvas, paint);
+ }
+ return output;
+ }
+ private void drawAvatar(Bitmap bitmap, Canvas canvas, Paint paint) {
+ final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
- return output;
+ }
+
+ private void drawIcon(Canvas canvas, Paint paint) {
+ BitmapFactory.Options opts = new BitmapFactory.Options();
+ opts.inSampleSize = 3;
+ Resources resources = mXmppConnectionService.getResources();
+ Bitmap icon = BitmapFactory.decodeResource(resources, R.drawable.ic_launcher, opts);
+ paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
+
+ int left = canvas.getWidth() - icon.getWidth();
+ int top = canvas.getHeight() - icon.getHeight();
+ final Rect rect = new Rect(left, top, left + icon.getWidth(), top + icon.getHeight());
+ canvas.drawBitmap(icon, null, rect, paint);
}
public Bitmap get(final MucOptions.User user, final int size, boolean cachedOnly) {
diff --git a/src/main/java/de/pixart/messenger/services/ShortcutService.java b/src/main/java/de/pixart/messenger/services/ShortcutService.java
index c5d73d5b1..0578a8ce5 100644
--- a/src/main/java/de/pixart/messenger/services/ShortcutService.java
+++ b/src/main/java/de/pixart/messenger/services/ShortcutService.java
@@ -4,9 +4,11 @@ import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
+import android.graphics.Bitmap;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
+import android.support.annotation.NonNull;
import android.util.Log;
import java.util.ArrayList;
@@ -74,11 +76,7 @@ public class ShortcutService {
}
List<ShortcutInfo> newDynamicShortCuts = new ArrayList<>();
for (Contact contact : contacts) {
- ShortcutInfo shortcut = new ShortcutInfo.Builder(xmppConnectionService, getShortcutId(contact))
- .setShortLabel(contact.getDisplayName())
- .setIntent(getShortcutIntent(contact))
- .setIcon(Icon.createWithBitmap(xmppConnectionService.getAvatarService().getRoundedShortcut(contact)))
- .build();
+ ShortcutInfo shortcut = getShortcutInfo(contact);
newDynamicShortCuts.add(shortcut);
}
if (shortcutManager.setDynamicShortcuts(newDynamicShortCuts)) {
@@ -88,6 +86,15 @@ public class ShortcutService {
}
}
+ @TargetApi(Build.VERSION_CODES.N_MR1)
+ private ShortcutInfo getShortcutInfo(Contact contact) {
+ return new ShortcutInfo.Builder(xmppConnectionService, getShortcutId(contact))
+ .setShortLabel(contact.getDisplayName())
+ .setIntent(getShortcutIntent(contact))
+ .setIcon(Icon.createWithBitmap(xmppConnectionService.getAvatarService().getRoundedShortcut(contact)))
+ .build();
+ }
+
private static boolean contactsChanged(List<Contact> needles, List<ShortcutInfo> haystack) {
for (Contact needle : needles) {
if (!contactExists(needle, haystack)) {
@@ -119,6 +126,31 @@ public class ShortcutService {
return intent;
}
+ @NonNull
+ public Intent createShortcut(Contact contact) {
+ Intent intent;
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ ShortcutInfo shortcut = getShortcutInfo(contact);
+ ShortcutManager shortcutManager = xmppConnectionService.getSystemService(ShortcutManager.class);
+ intent = shortcutManager.createShortcutResultIntent(shortcut);
+ } else {
+ intent = createShortcutResultIntent(contact);
+ }
+ return intent;
+ }
+
+ @NonNull
+ private Intent createShortcutResultIntent(Contact contact) {
+ Intent intent;
+ AvatarService avatarService = xmppConnectionService.getAvatarService();
+ Bitmap icon = avatarService.getRoundedShortcutWithIcon(contact);
+ intent = new Intent();
+ intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, contact.getDisplayName());
+ intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
+ intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, getShortcutIntent(contact));
+ return intent;
+ }
+
public static class FrequentContact {
private final String account;
private final Jid contact;