aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack
diff options
context:
space:
mode:
authorsteckbrief <steckbrief@chefmail.de>2015-11-06 21:41:29 +0100
committersteckbrief <steckbrief@chefmail.de>2015-11-06 21:41:29 +0100
commit6bc277152a8e1bcf5347d3e1d60c93dfc904ca23 (patch)
treef8ff8a13090f774243d2744eaba0185877245a14 /src/main/java/de/thedevstack
parent0575767152abc391fd1d0241d9cbae154236df70 (diff)
parent8838a5094812e0540ccfef3334e49e1a5c1a564b (diff)
Merge remote-tracking branch 'remotes/origin/trz/rename' into trz/rebase
Diffstat (limited to 'src/main/java/de/thedevstack')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java20
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/exceptions/FileCopyException.java14
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/utils/AvatarUtil.java164
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/utils/ImageUtil.java314
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/utils/StreamUtil.java48
5 files changed, 560 insertions, 0 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java b/src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java
index f86c509a..4a67aa3e 100644
--- a/src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java
+++ b/src/main/java/de/thedevstack/conversationsplus/ConversationsPlusApplication.java
@@ -7,6 +7,7 @@ import android.preference.PreferenceManager;
import java.io.File;
+import de.thedevstack.conversationsplus.utils.ImageUtil;
import eu.siacs.conversations.R;
/**
@@ -25,8 +26,13 @@ public class ConversationsPlusApplication extends Application {
super.onCreate();
ConversationsPlusApplication.instance = this;
ConversationsPlusPreferences.init(PreferenceManager.getDefaultSharedPreferences(getAppContext()));
+ ImageUtil.initBitmapCache();
}
+ /**
+ * Returns the instance of the application
+ * @return this application instance
+ */
public static ConversationsPlusApplication getInstance() {
return ConversationsPlusApplication.instance;
}
@@ -47,6 +53,11 @@ public class ConversationsPlusApplication extends Application {
return ConversationsPlusApplication.instance.getFilesDir();
}
+ /**
+ * Returns the version of the application.
+ * @see android.content.pm.PackageInfo#versionName
+ * @return a string representation of the version stored in packageInfo
+ */
public static String getVersion() {
final String packageName = ConversationsPlusApplication.getAppContext().getPackageName();
if (packageName != null) {
@@ -60,10 +71,19 @@ public class ConversationsPlusApplication extends Application {
}
}
+ /**
+ * Returns the application's name.
+ * @return the name as it is defined in R.string.app_name
+ */
public static String getName() {
return ConversationsPlusApplication.getAppContext().getString(R.string.app_name);
}
+ /**
+ * Returns the name and the version of this application.
+ * @see #getName() and #getVersion
+ * @return a concatination of name and version with a whitespace in between
+ */
public static String getNameAndVersion() {
return getName() + " " + getVersion();
}
diff --git a/src/main/java/de/thedevstack/conversationsplus/exceptions/FileCopyException.java b/src/main/java/de/thedevstack/conversationsplus/exceptions/FileCopyException.java
new file mode 100644
index 00000000..58363c0f
--- /dev/null
+++ b/src/main/java/de/thedevstack/conversationsplus/exceptions/FileCopyException.java
@@ -0,0 +1,14 @@
+package de.thedevstack.conversationsplus.exceptions;
+
+public class FileCopyException extends Exception {
+ private static final long serialVersionUID = -1010013599132881427L;
+ private int resId;
+
+ public FileCopyException(int resId) {
+ this.resId = resId;
+ }
+
+ public int getResId() {
+ return resId;
+ }
+} \ No newline at end of file
diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/AvatarUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/AvatarUtil.java
new file mode 100644
index 00000000..77ec345d
--- /dev/null
+++ b/src/main/java/de/thedevstack/conversationsplus/utils/AvatarUtil.java
@@ -0,0 +1,164 @@
+package de.thedevstack.conversationsplus.utils;
+
+import android.graphics.Bitmap;
+import android.net.Uri;
+import android.util.Base64;
+import android.util.Base64OutputStream;
+import android.util.Log;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.security.DigestOutputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+import de.thedevstack.conversationsplus.ConversationsPlusApplication;
+
+import eu.siacs.conversations.Config;
+import eu.siacs.conversations.utils.CryptoHelper;
+import eu.siacs.conversations.xmpp.pep.Avatar;
+
+/**
+ * This util provides access to saved avatars, creating avatars.
+ */
+public final class AvatarUtil {
+
+ /**
+ * Get the PEP Avatar.
+ * TODO: Why PEP Avatar?
+ * @param image the uri to the avatar's image
+ * @param size the image width/height to resize to
+ * @param format the format for the avatar
+ * @return the avatar
+ */
+ public static Avatar getPepAvatar(Uri image, int size, Bitmap.CompressFormat format) {
+ try {
+ Avatar avatar = new Avatar();
+ Bitmap bm = ImageUtil.cropCenterSquare(image, size);
+ if (bm == null) {
+ return null;
+ }
+ ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
+ Base64OutputStream mBase64OutputSttream = new Base64OutputStream(
+ mByteArrayOutputStream, Base64.DEFAULT);
+ MessageDigest digest = MessageDigest.getInstance("SHA-1");
+ DigestOutputStream mDigestOutputStream = new DigestOutputStream(
+ mBase64OutputSttream, digest);
+ if (!bm.compress(format, 75, mDigestOutputStream)) {
+ return null;
+ }
+ mDigestOutputStream.flush();
+ mDigestOutputStream.close();
+ avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
+ avatar.image = new String(mByteArrayOutputStream.toByteArray());
+ return avatar;
+ } catch (NoSuchAlgorithmException e) {
+ return null;
+ } catch (IOException e) {
+ return null;
+ }
+ }
+
+ /**
+ * Returns whether the avatar is cached or not.
+ * @param avatar the avatar to check the existance
+ * @return <code>true</code> if the file of the avatar exists, <code>false</code> otherwise
+ */
+ public static boolean isAvatarCached(Avatar avatar) {
+ File file = new File(getAvatarPath(avatar.getFilename()));
+ return file.exists();
+ }
+
+ /**
+ * Saves an avatar to the file system.
+ * All exceptions are silently ignored.
+ * TODO: Move real saving operation to FileBackend
+ * @param avatar the avatar to save
+ * @return <code>true</code> if the avatar was saved successfully, <code>false</code> otherwise.
+ */
+ public static boolean save(Avatar avatar) {
+ File file;
+ if (isAvatarCached(avatar)) {
+ file = new File(getAvatarPath(avatar.getFilename()));
+ } else {
+ String filename = getAvatarPath(avatar.getFilename());
+ file = new File(filename + ".tmp");
+ file.getParentFile().mkdirs();
+ OutputStream os = null;
+ try {
+ file.createNewFile();
+ os = new FileOutputStream(file);
+ MessageDigest digest = MessageDigest.getInstance("SHA-1");
+ digest.reset();
+ DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
+ mDigestOutputStream.write(avatar.getImageAsBytes());
+ mDigestOutputStream.flush();
+ mDigestOutputStream.close();
+ String sha1sum = CryptoHelper.bytesToHex(digest.digest());
+ if (sha1sum.equals(avatar.sha1sum)) {
+ file.renameTo(new File(filename));
+ } else {
+ Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
+ file.delete();
+ return false;
+ }
+ } catch (FileNotFoundException e) {
+ return false;
+ } catch (IOException e) {
+ return false;
+ } catch (NoSuchAlgorithmException e) {
+ return false;
+ } finally {
+ StreamUtil.close(os);
+ }
+ }
+ avatar.size = file.length();
+ return true;
+ }
+
+ /**
+ * Returns the avatar for an uri.
+ * @param avatar the avatar's uri
+ * @param size the height/width the avatar should have
+ * @return the bitmap of the uri
+ */
+ public static Bitmap getAvatar(String avatar, int size) {
+ if (avatar == null) {
+ return null;
+ }
+ Bitmap bm = ImageUtil.cropCenter(getAvatarUri(avatar), size, size);
+ if (bm == null) {
+ return null;
+ }
+ return bm;
+ }
+
+ /**
+ * Returns the path to an avatar
+ * @param avatar the name of the avatar.
+ * @return the path as string
+ */
+ public static String getAvatarPath(String avatar) {
+ return ConversationsPlusApplication.getInstance().getFilesDir().getAbsolutePath()+ "/avatars/" + avatar;
+ }
+
+ /**
+ * Returns the path to an avatar as an uri.
+ * @param avatar the name of the avatar
+ * @return the path as uri
+ */
+ public static Uri getAvatarUri(String avatar) {
+ return Uri.parse("file:" + getAvatarPath(avatar));
+ }
+
+ /**
+ * Avoid instantiation it's an helper class.
+ */
+ private AvatarUtil() {
+ // Static helper class
+ }
+}
diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/ImageUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/ImageUtil.java
new file mode 100644
index 00000000..07e41dcb
--- /dev/null
+++ b/src/main/java/de/thedevstack/conversationsplus/utils/ImageUtil.java
@@ -0,0 +1,314 @@
+package de.thedevstack.conversationsplus.utils;
+
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.Canvas;
+import android.graphics.Matrix;
+import android.graphics.RectF;
+import android.media.ExifInterface;
+import android.net.Uri;
+import android.util.Log;
+import android.util.LruCache;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+
+import eu.siacs.conversations.entities.Message;
+import eu.siacs.conversations.persistance.FileBackend;
+import eu.siacs.conversations.utils.ExifHelper;
+
+/**
+ * This util provides
+ */
+public final class ImageUtil {
+ private static LruCache<String, Bitmap> BITMAP_CACHE;
+
+ /**
+ * Returns a bitmap from the cache.
+ * @see LruCache#get(Object) for details
+ * @param key the key of the bitmap to get
+ * @return the bitmap
+ */
+ public static Bitmap getBitmapFromCache(String key) {
+ return BITMAP_CACHE.get(key);
+ }
+
+ /**
+ * Adds a bitmap with the given key to the cache.
+ * @see LruCache#put(Object, Object) for details
+ * @param key the key to identify this bitmap
+ * @param bitmap the bitmap to cache
+ */
+ public static void addBitmapToCache(String key, Bitmap bitmap) {
+ BITMAP_CACHE.put(key, bitmap);
+ }
+
+ /**
+ * Removes the bitmap with given key from the cache.
+ * @param key the key of the bitmap to remove
+ */
+ public static void removeBitmapFromCache(String key) {
+ BITMAP_CACHE.remove(key);
+ }
+
+ /**
+ * Clears the cache.
+ * @see LruCache#evictAll() for more details.
+ */
+ public static void evictBitmapCache() {
+ BITMAP_CACHE.evictAll();
+ }
+
+ /**
+ * Initializes the bitmap cache.
+ * This has to be executed once on application start.
+ * @see LruCache#LruCache(int) for details
+ */
+ public static void initBitmapCache() {
+ Log.i("Conversations+ImageUtil", "Initializing BitmapCache");
+ final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
+ final int cacheSize = maxMemory / 8;
+ BITMAP_CACHE = new LruCache<String, Bitmap>(cacheSize) {
+ @Override
+ protected int sizeOf(final String key, final Bitmap bitmap) {
+ return bitmap.getByteCount() / 1024;
+ }
+ };
+ }
+
+ /**
+ * Resizes a given bitmap and return a new and resized bitmap.
+ * The bitmap is only resized if either the width or the height of the original bitmap is smaller than the given size.
+ * @param originalBitmap the bitmap to resize
+ * @param size the size to scale to
+ * @return new and resized bitmap or the original bitmap if width and height are smaller than size
+ */
+ public static Bitmap resize(Bitmap originalBitmap, int size) {
+ int w = originalBitmap.getWidth();
+ int h = originalBitmap.getHeight();
+ if (Math.max(w, h) > size) {
+ int scalledW;
+ int scalledH;
+ if (w <= h) {
+ scalledW = (int) (w / ((double) h / size));
+ scalledH = size;
+ } else {
+ scalledW = size;
+ scalledH = (int) (h / ((double) w / size));
+ }
+ return Bitmap.createScaledBitmap(originalBitmap, scalledW, scalledH, true);
+ } else {
+ return originalBitmap;
+ }
+ }
+
+ /**
+ * Returns the rotation from the exif information of an image identified with the given uri.
+ * The orientation is retrieved by parsing the stream of the image.
+ * FileNotFoundException is silently ignored.
+ * @param image the uri of the image to get the rotation
+ * @return the rotation value for the image, <code>0</code> if the file cannot be found.
+ */
+ public static int getRotation(Uri image) {
+ InputStream is = null;
+ try {
+ is = StreamUtil.openInputStreamFromContentResolver(image);
+ return ExifHelper.getOrientation(is);
+ } catch (FileNotFoundException e) {
+ return 0;
+ } finally {
+ StreamUtil.close(is);
+ }
+ }
+
+ /**
+ * Returns a thumbnail for a bitmap in a message.
+ * @param message the message to get the thumbnail for
+ * @param size the size to resize the original image to
+ * @param cacheOnly whether only cached images should be returned or not
+ * @return the resized thumbail
+ * @throws FileNotFoundException if the original image does not exist anymore or an IOException occurs.
+ */
+ public static Bitmap getThumbnail(Message message, int size, boolean cacheOnly)
+ throws FileNotFoundException {
+ Bitmap thumbnail = ImageUtil.getBitmapFromCache(message.getUuid());
+ if ((thumbnail == null) && (!cacheOnly)) {
+ File file = FileBackend.getFile(message);
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inSampleSize = calcSampleSize(file, size);
+ Bitmap fullsize = BitmapFactory.decodeFile(file.getAbsolutePath(),options);
+ if (fullsize == null) {
+ throw new FileNotFoundException();
+ }
+ thumbnail = resize(fullsize, size);
+ thumbnail = rotate(thumbnail, file.getAbsolutePath());
+
+ ImageUtil.addBitmapToCache(message.getUuid(), thumbnail);
+ }
+ return thumbnail;
+ }
+
+ /**
+ * Rotates an bitmap. Only the values 90°, 180° and 270° are considered to rotate the image.
+ * The orientation information is read using the ExifInterface.
+ * @param original the original bitmap
+ * @param srcPath the path to the original bitmap (used to read the exif information)
+ * @return rotated bitmap, or original bitmap if criteria are not met
+ * @throws IOException
+ */
+ public static Bitmap rotate(Bitmap original, String srcPath) {
+ try {
+ ExifInterface exif = new ExifInterface(srcPath);
+ int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
+ int rotation = 0;
+ switch (orientation) {
+ case ExifInterface.ORIENTATION_ROTATE_90:
+ rotation = 90;
+ break;
+ case ExifInterface.ORIENTATION_ROTATE_180:
+ rotation = 180;
+ break;
+ case ExifInterface.ORIENTATION_ROTATE_270:
+ rotation = 270;
+ break;
+ }
+ if (rotation > 0) {
+ return rotate(original, rotation);
+ }
+ } catch (IOException e) {
+ Log.w("filebackend", "Error while rotating image, returning original (" + e.getMessage() + ")");
+ }
+ return original;
+ }
+
+ /**
+ * Rotates a bitmap with given degrees.
+ * @param bitmap the bitmap to be rotated
+ * @param degree the degrees to rotate the bitmap
+ * @return a newly created bitmap
+ */
+ public static Bitmap rotate(Bitmap bitmap, int degree) {
+ int w = bitmap.getWidth();
+ int h = bitmap.getHeight();
+ Matrix mtx = new Matrix();
+ mtx.postRotate(degree);
+ return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
+ }
+
+
+ public static Bitmap cropCenterSquare(Uri image, int size) {
+ if (image == null) {
+ return null;
+ }
+ InputStream is = null;
+ try {
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inSampleSize = calcSampleSize(image, size);
+ is = StreamUtil.openInputStreamFromContentResolver(image);
+ Bitmap input = BitmapFactory.decodeStream(is, null, options);
+ if (input == null) {
+ return null;
+ } else {
+ int rotation = getRotation(image);
+ if (rotation > 0) {
+ input = rotate(input, rotation);
+ }
+ return cropCenterSquare(input, size);
+ }
+ } catch (FileNotFoundException e) {
+ return null;
+ } finally {
+ StreamUtil.close(is);
+ }
+ }
+
+ public static Bitmap cropCenter(Uri image, int newHeight, int newWidth) {
+ if (image == null) {
+ return null;
+ }
+ InputStream is = null;
+ try {
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inSampleSize = calcSampleSize(image,Math.max(newHeight, newWidth));
+ is = StreamUtil.openInputStreamFromContentResolver(image);
+ Bitmap source = BitmapFactory.decodeStream(is, null, options);
+ if (source == null) {
+ return null;
+ }
+ int sourceWidth = source.getWidth();
+ int sourceHeight = source.getHeight();
+ float xScale = (float) newWidth / sourceWidth;
+ float yScale = (float) newHeight / sourceHeight;
+ float scale = Math.max(xScale, yScale);
+ float scaledWidth = scale * sourceWidth;
+ float scaledHeight = scale * sourceHeight;
+ float left = (newWidth - scaledWidth) / 2;
+ float top = (newHeight - scaledHeight) / 2;
+
+ RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
+ Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
+ Canvas canvas = new Canvas(dest);
+ canvas.drawBitmap(source, null, targetRect, null);
+ return dest;
+ } catch (FileNotFoundException e) {
+ return null;
+ } finally {
+ StreamUtil.close(is);
+ }
+ }
+
+ public static Bitmap cropCenterSquare(Bitmap input, int size) {
+ int w = input.getWidth();
+ int h = input.getHeight();
+
+ float scale = Math.max((float) size / h, (float) size / w);
+
+ float outWidth = scale * w;
+ float outHeight = scale * h;
+ float left = (size - outWidth) / 2;
+ float top = (size - outHeight) / 2;
+ RectF target = new RectF(left, top, left + outWidth, top + outHeight);
+
+ Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
+ Canvas canvas = new Canvas(output);
+ canvas.drawBitmap(input, null, target, null);
+ return output;
+ }
+
+ public static int calcSampleSize(Uri image, int size) throws FileNotFoundException {
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inJustDecodeBounds = true;
+ BitmapFactory.decodeStream(StreamUtil.openInputStreamFromContentResolver(image), null, options);
+ return calcSampleSize(options, size);
+ }
+
+ public static int calcSampleSize(File image, int size) {
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inJustDecodeBounds = true;
+ BitmapFactory.decodeFile(image.getAbsolutePath(), options);
+ return calcSampleSize(options, size);
+ }
+
+ public static int calcSampleSize(BitmapFactory.Options options, int size) {
+ int height = options.outHeight;
+ int width = options.outWidth;
+ int inSampleSize = 1;
+
+ if (height > size || width > size) {
+ int halfHeight = height / 2;
+ int halfWidth = width / 2;
+
+ while ((halfHeight / inSampleSize) > size
+ && (halfWidth / inSampleSize) > size) {
+ inSampleSize *= 2;
+ }
+ }
+ return inSampleSize;
+ }
+
+ private ImageUtil() {
+ // Static helper class
+ }
+}
diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/StreamUtil.java b/src/main/java/de/thedevstack/conversationsplus/utils/StreamUtil.java
new file mode 100644
index 00000000..64f46314
--- /dev/null
+++ b/src/main/java/de/thedevstack/conversationsplus/utils/StreamUtil.java
@@ -0,0 +1,48 @@
+package de.thedevstack.conversationsplus.utils;
+
+import android.net.Uri;
+
+import java.io.Closeable;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+
+import de.thedevstack.conversationsplus.ConversationsPlusApplication;
+
+/**
+ * Util to handle streams.
+ */
+public final class StreamUtil {
+
+ /**
+ * Opens an InputStream from Uri using the ContentResolver from application.
+ * @see android.content.ContentResolver#openInputStream(Uri)
+ * @param uri the uri to open
+ * @return the InputStream for given uri
+ * @throws FileNotFoundException if the provided URI could not be opened.
+ */
+ public static InputStream openInputStreamFromContentResolver(Uri uri) throws FileNotFoundException {
+ return ConversationsPlusApplication.getInstance().getContentResolver().openInputStream(uri);
+ }
+
+ /**
+ * Closes a stream.
+ * IOException is silently ignored.
+ * @param stream the stream to close
+ */
+ public static void close(Closeable stream) {
+ if (stream != null) {
+ try {
+ stream.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ /**
+ * Avoid instantiation of util class.
+ */
+ private StreamUtil() {
+ // Static helper class
+ }
+}