Commit 79d1abb6 authored by Alexander Ivanov's avatar Alexander Ivanov

Resize avatar for shortcut icon. Close #107.

parent 086ea51d
...@@ -462,4 +462,29 @@ public class AvatarManager implements OnLoadListener, OnLowMemoryListener, ...@@ -462,4 +462,29 @@ public class AvatarManager implements OnLoadListener, OnLowMemoryListener,
setHash(bareAddress, hash); setHash(bareAddress, hash);
} }
} }
/**
* @param bitmap
* @return Scaled bitmap to be used for shortcut.
*/
public Bitmap createShortcutBitmap(Bitmap bitmap) {
int size = getLauncherLargeIconSize();
int max = Math.max(bitmap.getWidth(), bitmap.getHeight());
if (max == size)
return bitmap;
double scale = ((double) size) / max;
int width = (int) (bitmap.getWidth() * scale);
int height = (int) (bitmap.getHeight() * scale);
return Bitmap.createScaledBitmap(bitmap, width, height, true);
}
private int getLauncherLargeIconSize() {
if (Application.SDK_INT < 9)
return BaseShortcutHelper.getLauncherLargeIconSize();
else if (Application.SDK_INT < 11)
return GingerbreadShortcutHelper.getLauncherLargeIconSize();
else
return HoneycombShortcutHelper.getLauncherLargeIconSize();
}
} }
package com.xabber.android.data.extension.avatar;
import android.content.res.Resources;
import com.xabber.android.data.Application;
/**
* Helper class to create shortcuts under Android < 2.3.
*
* @author alexander.ivanov
*
*/
public class BaseShortcutHelper {
/**
* Get the preferred launcher icon size. This is used when custom drawables
* are created (e.g., for shortcuts).
*
* Based on {@link android.app.ActivityManager#getLauncherLargeIconSize()}
* for Android 3+.
*
* @return dimensions of square icons in terms of pixels
*/
static int getLauncherLargeIconSize() {
final Resources res = Application.getInstance().getResources();
return res.getDimensionPixelSize(android.R.dimen.app_icon_size);
}
}
package com.xabber.android.data.extension.avatar;
import android.annotation.TargetApi;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import com.xabber.android.data.Application;
/**
* Helper class to create shortcuts under Android >= 2.3.
*
* @author alexander.ivanov
*
*/
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class GingerbreadShortcutHelper {
/**
* Get the preferred launcher icon size. This is used when custom drawables
* are created (e.g., for shortcuts).
*
* Based on {@link android.app.ActivityManager#getLauncherLargeIconSize()}
* for Android 3+.
*
* @return dimensions of square icons in terms of pixels
*/
static int getLauncherLargeIconSize() {
final Resources res = Application.getInstance().getResources();
final int size = res
.getDimensionPixelSize(android.R.dimen.app_icon_size);
if ((res.getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) != Configuration.SCREENLAYOUT_SIZE_XLARGE) {
return size;
}
final int density = res.getDisplayMetrics().densityDpi;
switch (density) {
case DisplayMetrics.DENSITY_LOW:
return (size * DisplayMetrics.DENSITY_MEDIUM)
/ DisplayMetrics.DENSITY_LOW;
case DisplayMetrics.DENSITY_MEDIUM:
return (size * DisplayMetrics.DENSITY_HIGH)
/ DisplayMetrics.DENSITY_MEDIUM;
case DisplayMetrics.DENSITY_HIGH:
return (size * DisplayMetrics.DENSITY_XHIGH)
/ DisplayMetrics.DENSITY_HIGH;
case DisplayMetrics.DENSITY_XHIGH:
return (size * DisplayMetrics.DENSITY_MEDIUM * 2)
/ DisplayMetrics.DENSITY_XHIGH;
default:
return size;
}
}
}
package com.xabber.android.data.extension.avatar;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.content.Context;
import com.xabber.android.data.Application;
/**
* Helper class to create shortcuts under Android >= 3.
*
* @author alexander.ivanov
*
*/
public class HoneycombShortcutHelper {
/**
* Get the preferred launcher icon size. This is used when custom drawables
* are created (e.g., for shortcuts).
*
* @return dimensions of square icons in terms of pixels
*/
static int getLauncherLargeIconSize() {
android.app.ActivityManager activityManager = (android.app.ActivityManager) Application
.getInstance().getSystemService(Context.ACTIVITY_SERVICE);
Method method;
try {
method = activityManager.getClass().getMethod(
"getLauncherLargeIconSize", (Class[]) null);
} catch (SecurityException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
try {
return (Integer) method.invoke(activityManager, (Object[]) null);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
...@@ -25,6 +25,7 @@ import android.content.DialogInterface.OnCancelListener; ...@@ -25,6 +25,7 @@ import android.content.DialogInterface.OnCancelListener;
import android.content.Intent; import android.content.Intent;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
...@@ -973,18 +974,17 @@ public class ContactList extends ManagedListActivity implements ...@@ -973,18 +974,17 @@ public class ContactList extends ManagedListActivity implements
abstractContact.getUser())); abstractContact.getUser()));
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
abstractContact.getName()); abstractContact.getName());
Bitmap bitmap;
if (MUCManager.getInstance() if (MUCManager.getInstance()
.hasRoom(abstractContact.getAccount(), .hasRoom(abstractContact.getAccount(),
abstractContact.getUser())) abstractContact.getUser()))
intent.putExtra( bitmap = AvatarManager.getInstance().getRoomBitmap(
Intent.EXTRA_SHORTCUT_ICON, abstractContact.getUser());
AvatarManager.getInstance().getRoomBitmap(
abstractContact.getUser()));
else else
intent.putExtra( bitmap = AvatarManager.getInstance().getUserBitmap(
Intent.EXTRA_SHORTCUT_ICON, abstractContact.getUser());
AvatarManager.getInstance().getUserBitmap( intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, AvatarManager
abstractContact.getUser())); .getInstance().createShortcutBitmap(bitmap));
setResult(RESULT_OK, intent); setResult(RESULT_OK, intent);
finish(); finish();
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment