package eu.siacs.conversations.persistance; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.util.Log; import android.util.LruCache; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.utils.CryptoHelper; public class FileBackend { private static int IMAGE_SIZE = 1920; private Context context; private LruCache thumbnailCache; public FileBackend(Context context) { this.context = context; int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); int cacheSize = maxMemory / 8; thumbnailCache = new LruCache(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getByteCount() / 1024; } }; } public File getImageFile(Message message) { Conversation conversation = message.getConversation(); String prefix = context.getFilesDir().getAbsolutePath(); String path = prefix + "/" + conversation.getAccount().getJid() + "/" + conversation.getContactJid(); String filename = message.getUuid() + ".webp"; return new File(path + "/" + filename); } private 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)); } Bitmap scalledBitmap = Bitmap.createScaledBitmap( originalBitmap, scalledW, scalledH, true); return scalledBitmap; } else { return originalBitmap; } } public File copyImageToPrivateStorage(Message message, Uri image) { try { InputStream is = context.getContentResolver() .openInputStream(image); File file = getImageFile(message); file.getParentFile().mkdirs(); file.createNewFile(); OutputStream os = new FileOutputStream(file); Bitmap originalBitmap = BitmapFactory.decodeStream(is); is.close(); Bitmap scalledBitmap = resize(originalBitmap, IMAGE_SIZE); boolean success = scalledBitmap.compress(Bitmap.CompressFormat.WEBP,75,os); if (!success) { Log.d("xmppService", "couldnt compress"); } os.close(); message.setBody(this.createSha1(file)); return file; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public Bitmap getImageFromMessage(Message message) { return BitmapFactory .decodeFile(getImageFile(message).getAbsolutePath()); } public Bitmap getThumbnailFromMessage(Message message, int size) { Bitmap thumbnail = thumbnailCache.get(message.getUuid()); if (thumbnail==null) { Log.d("xmppService","creating new thumbnail" + message.getUuid()); Bitmap fullsize = BitmapFactory.decodeFile(getImageFile(message) .getAbsolutePath()); thumbnail = resize(fullsize, size); this.thumbnailCache.put(message.getUuid(), thumbnail); } return thumbnail; } private String createSha1(final File file) { InputStream fis = null; try { MessageDigest digest = MessageDigest.getInstance("SHA-1"); fis = new FileInputStream(file); int n = 0; byte[] buffer = new byte[8192]; while (n != -1) { n = fis.read(buffer); if (n > 0) { digest.update(buffer, 0, n); } } fis.close(); return CryptoHelper.bytesToHex(digest.digest()); } catch (NoSuchAlgorithmException e) { return null; } catch (FileNotFoundException e) { if (fis!=null) { try { fis.close(); return null; } catch (IOException e1) { return null; } } } catch (IOException e) { if (fis!=null) { try { fis.close(); return null; } catch (IOException e1) { return null; } } } return null; } }