FileBackend.java

  1package eu.siacs.conversations.persistance;
  2
  3import android.content.Context;
  4import android.content.Intent;
  5import android.database.Cursor;
  6import android.graphics.Bitmap;
  7import android.graphics.BitmapFactory;
  8import android.graphics.Canvas;
  9import android.graphics.Matrix;
 10import android.graphics.RectF;
 11import android.net.Uri;
 12import android.os.Environment;
 13import android.provider.OpenableColumns;
 14import android.util.Base64;
 15import android.util.Base64OutputStream;
 16import android.util.Log;
 17import android.webkit.MimeTypeMap;
 18
 19import java.io.ByteArrayOutputStream;
 20import java.io.Closeable;
 21import java.io.File;
 22import java.io.FileNotFoundException;
 23import java.io.FileOutputStream;
 24import java.io.IOException;
 25import java.io.InputStream;
 26import java.io.OutputStream;
 27import java.net.Socket;
 28import java.net.URL;
 29import java.security.DigestOutputStream;
 30import java.security.MessageDigest;
 31import java.security.NoSuchAlgorithmException;
 32import java.text.SimpleDateFormat;
 33import java.util.Date;
 34import java.util.List;
 35import java.util.Locale;
 36
 37import eu.siacs.conversations.Config;
 38import eu.siacs.conversations.R;
 39import eu.siacs.conversations.entities.DownloadableFile;
 40import eu.siacs.conversations.entities.Message;
 41import eu.siacs.conversations.services.XmppConnectionService;
 42import eu.siacs.conversations.utils.CryptoHelper;
 43import eu.siacs.conversations.utils.ExifHelper;
 44import eu.siacs.conversations.utils.FileUtils;
 45import eu.siacs.conversations.xmpp.pep.Avatar;
 46
 47public class FileBackend {
 48	private final SimpleDateFormat imageDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US);
 49
 50	private XmppConnectionService mXmppConnectionService;
 51
 52	public FileBackend(XmppConnectionService service) {
 53		this.mXmppConnectionService = service;
 54	}
 55
 56	private void createNoMedia() {
 57		final File nomedia = new File(getConversationsFileDirectory()+".nomedia");
 58		if (!nomedia.exists()) {
 59			try {
 60				nomedia.createNewFile();
 61			} catch (Exception e) {
 62				Log.d(Config.LOGTAG, "could not create nomedia file");
 63			}
 64		}
 65	}
 66
 67	public void updateMediaScanner(File file) {
 68		if (file.getAbsolutePath().startsWith(getConversationsImageDirectory())) {
 69			Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
 70			intent.setData(Uri.fromFile(file));
 71			mXmppConnectionService.sendBroadcast(intent);
 72		} else {
 73			createNoMedia();
 74		}
 75	}
 76
 77	public boolean deleteFile(Message message) {
 78		File file = getFile(message);
 79		if (file.delete()) {
 80			updateMediaScanner(file);
 81			return true;
 82		} else {
 83			return false;
 84		}
 85	}
 86
 87	public DownloadableFile getFile(Message message) {
 88		return getFile(message, true);
 89	}
 90
 91	public DownloadableFile getFile(Message message, boolean decrypted) {
 92		final boolean encrypted = !decrypted
 93				&& (message.getEncryption() == Message.ENCRYPTION_PGP
 94				|| message.getEncryption() == Message.ENCRYPTION_DECRYPTED);
 95		final DownloadableFile file;
 96		String path = message.getRelativeFilePath();
 97		if (path == null) {
 98			path = message.getUuid();
 99		}
100		if (path.startsWith("/")) {
101			file = new DownloadableFile(path);
102		} else {
103			String mime = message.getMimeType();
104			if (mime != null && mime.startsWith("image")) {
105				file = new DownloadableFile(getConversationsImageDirectory() + path);
106			} else {
107				file = new DownloadableFile(getConversationsFileDirectory() + path);
108			}
109		}
110		if (encrypted) {
111			return new DownloadableFile(getConversationsFileDirectory() + file.getName() + ".pgp");
112		} else {
113			return file;
114		}
115	}
116
117	public static long getFileSize(Context context, Uri uri) {
118		Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
119		if (cursor != null && cursor.moveToFirst()) {
120			return cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
121		} else {
122			return -1;
123		}
124	}
125
126	public static boolean allFilesUnderSize(Context context, List<Uri> uris, long max) {
127		for(Uri uri : uris) {
128			if (FileBackend.getFileSize(context, uri) > max) {
129				return false;
130			}
131		}
132		return true;
133	}
134
135	public static String getConversationsFileDirectory() {
136		return  Environment.getExternalStorageDirectory().getAbsolutePath()+"/Conversations/";
137	}
138
139	public static String getConversationsImageDirectory() {
140		return Environment.getExternalStoragePublicDirectory(
141				Environment.DIRECTORY_PICTURES).getAbsolutePath()
142			+ "/Conversations/";
143	}
144
145	public Bitmap resize(Bitmap originalBitmap, int size) {
146		int w = originalBitmap.getWidth();
147		int h = originalBitmap.getHeight();
148		if (Math.max(w, h) > size) {
149			int scalledW;
150			int scalledH;
151			if (w <= h) {
152				scalledW = (int) (w / ((double) h / size));
153				scalledH = size;
154			} else {
155				scalledW = size;
156				scalledH = (int) (h / ((double) w / size));
157			}
158			Bitmap result = Bitmap.createScaledBitmap(originalBitmap, scalledW, scalledH, true);
159			if (originalBitmap != null && !originalBitmap.isRecycled()) {
160				originalBitmap.recycle();
161			}
162			return result;
163		} else {
164			return originalBitmap;
165		}
166	}
167
168	public static Bitmap rotate(Bitmap bitmap, int degree) {
169		if (degree == 0) {
170			return bitmap;
171		}
172		int w = bitmap.getWidth();
173		int h = bitmap.getHeight();
174		Matrix mtx = new Matrix();
175		mtx.postRotate(degree);
176		Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
177		if (bitmap != null && !bitmap.isRecycled()) {
178			bitmap.recycle();
179		}
180		return result;
181	}
182
183	public boolean useImageAsIs(Uri uri) {
184		String path = getOriginalPath(uri);
185		if (path == null) {
186			return false;
187		}
188		File file = new File(path);
189		long size = file.length();
190		if (size == 0 || size >= Config.IMAGE_MAX_SIZE ) {
191			return false;
192		}
193		BitmapFactory.Options options = new BitmapFactory.Options();
194		options.inJustDecodeBounds = true;
195		try {
196			BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(uri), null, options);
197			if (options == null || options.outMimeType == null || options.outHeight <= 0 || options.outWidth <= 0) {
198				return false;
199			}
200			return (options.outWidth <= Config.IMAGE_SIZE && options.outHeight <= Config.IMAGE_SIZE && options.outMimeType.contains(Config.IMAGE_FORMAT.name().toLowerCase()));
201		} catch (FileNotFoundException e) {
202			return false;
203		}
204	}
205
206	public String getOriginalPath(Uri uri) {
207		return FileUtils.getPath(mXmppConnectionService,uri);
208	}
209
210	public void copyFileToPrivateStorage(File file, Uri uri) throws FileCopyException {
211		file.getParentFile().mkdirs();
212		OutputStream os = null;
213		InputStream is = null;
214		try {
215			file.createNewFile();
216			os = new FileOutputStream(file);
217			is = mXmppConnectionService.getContentResolver().openInputStream(uri);
218			byte[] buffer = new byte[1024];
219			int length;
220			while ((length = is.read(buffer)) > 0) {
221				os.write(buffer, 0, length);
222			}
223			os.flush();
224		} catch(FileNotFoundException e) {
225			throw new FileCopyException(R.string.error_file_not_found);
226		} catch (IOException e) {
227			e.printStackTrace();
228			throw new FileCopyException(R.string.error_io_exception);
229		} finally {
230			close(os);
231			close(is);
232		}
233		Log.d(Config.LOGTAG, "output file name " + file.getAbsolutePath());
234	}
235
236	public void copyFileToPrivateStorage(Message message, Uri uri) throws FileCopyException {
237		Log.d(Config.LOGTAG, "copy " + uri.toString() + " to private storage");
238		String mime = mXmppConnectionService.getContentResolver().getType(uri);
239		String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mime);
240		message.setRelativeFilePath(message.getUuid() + "." + extension);
241		copyFileToPrivateStorage(mXmppConnectionService.getFileBackend().getFile(message), uri);
242	}
243
244	private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException {
245		file.getParentFile().mkdirs();
246		InputStream is = null;
247		OutputStream os = null;
248		try {
249			file.createNewFile();
250			is = mXmppConnectionService.getContentResolver().openInputStream(image);
251			Bitmap originalBitmap;
252			BitmapFactory.Options options = new BitmapFactory.Options();
253			int inSampleSize = (int) Math.pow(2, sampleSize);
254			Log.d(Config.LOGTAG, "reading bitmap with sample size " + inSampleSize);
255			options.inSampleSize = inSampleSize;
256			originalBitmap = BitmapFactory.decodeStream(is, null, options);
257			is.close();
258			if (originalBitmap == null) {
259				throw new FileCopyException(R.string.error_not_an_image_file);
260			}
261			Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
262			int rotation = getRotation(image);
263			scaledBitmap = rotate(scaledBitmap, rotation);
264			boolean targetSizeReached = false;
265			int quality = Config.IMAGE_QUALITY;
266			while(!targetSizeReached) {
267				os = new FileOutputStream(file);
268				boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, quality, os);
269				if (!success) {
270					throw new FileCopyException(R.string.error_compressing_image);
271				}
272				os.flush();
273				targetSizeReached = file.length() <= Config.IMAGE_MAX_SIZE || quality <= 50;
274				quality -= 5;
275			}
276			scaledBitmap.recycle();
277			return;
278		} catch (FileNotFoundException e) {
279			throw new FileCopyException(R.string.error_file_not_found);
280		} catch (IOException e) {
281			e.printStackTrace();
282			throw new FileCopyException(R.string.error_io_exception);
283		} catch (SecurityException e) {
284			throw new FileCopyException(R.string.error_security_exception_during_image_copy);
285		} catch (OutOfMemoryError e) {
286			++sampleSize;
287			if (sampleSize <= 3) {
288				copyImageToPrivateStorage(file, image, sampleSize);
289			} else {
290				throw new FileCopyException(R.string.error_out_of_memory);
291			}
292		} catch (NullPointerException e) {
293			throw new FileCopyException(R.string.error_io_exception);
294		} finally {
295			close(os);
296			close(is);
297		}
298	}
299
300	public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException {
301		copyImageToPrivateStorage(file, image, 0);
302	}
303
304	public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException {
305		switch(Config.IMAGE_FORMAT) {
306			case JPEG:
307				message.setRelativeFilePath(message.getUuid()+".jpg");
308				break;
309			case PNG:
310				message.setRelativeFilePath(message.getUuid()+".png");
311				break;
312			case WEBP:
313				message.setRelativeFilePath(message.getUuid()+".webp");
314				break;
315		}
316		copyImageToPrivateStorage(getFile(message), image);
317		updateFileParams(message);
318	}
319
320	private int getRotation(File file) {
321		return getRotation(Uri.parse("file://"+file.getAbsolutePath()));
322	}
323
324	private int getRotation(Uri image) {
325		InputStream is = null;
326		try {
327			is = mXmppConnectionService.getContentResolver().openInputStream(image);
328			return ExifHelper.getOrientation(is);
329		} catch (FileNotFoundException e) {
330			return 0;
331		} finally {
332			close(is);
333		}
334	}
335
336	public Bitmap getThumbnail(Message message, int size, boolean cacheOnly)
337			throws FileNotFoundException {
338		Bitmap thumbnail = mXmppConnectionService.getBitmapCache().get(message.getUuid());
339		if ((thumbnail == null) && (!cacheOnly)) {
340			File file = getFile(message);
341			BitmapFactory.Options options = new BitmapFactory.Options();
342			options.inSampleSize = calcSampleSize(file, size);
343			Bitmap fullsize = BitmapFactory.decodeFile(file.getAbsolutePath(),options);
344			if (fullsize == null) {
345				throw new FileNotFoundException();
346			}
347			thumbnail = resize(fullsize, size);
348			thumbnail = rotate(thumbnail, getRotation(file));
349			this.mXmppConnectionService.getBitmapCache().put(message.getUuid(),thumbnail);
350		}
351		return thumbnail;
352	}
353
354	public Uri getTakePhotoUri() {
355		StringBuilder pathBuilder = new StringBuilder();
356		pathBuilder.append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));
357		pathBuilder.append('/');
358		pathBuilder.append("Camera");
359		pathBuilder.append('/');
360		pathBuilder.append("IMG_" + this.imageDateFormat.format(new Date()) + ".jpg");
361		Uri uri = Uri.parse("file://" + pathBuilder.toString());
362		File file = new File(uri.toString());
363		file.getParentFile().mkdirs();
364		return uri;
365	}
366
367	public Avatar getPepAvatar(Uri image, int size, Bitmap.CompressFormat format) {
368		try {
369			Avatar avatar = new Avatar();
370			Bitmap bm = cropCenterSquare(image, size);
371			if (bm == null) {
372				return null;
373			}
374			ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
375			Base64OutputStream mBase64OutputSttream = new Base64OutputStream(
376					mByteArrayOutputStream, Base64.DEFAULT);
377			MessageDigest digest = MessageDigest.getInstance("SHA-1");
378			DigestOutputStream mDigestOutputStream = new DigestOutputStream(
379					mBase64OutputSttream, digest);
380			if (!bm.compress(format, 75, mDigestOutputStream)) {
381				return null;
382			}
383			mDigestOutputStream.flush();
384			mDigestOutputStream.close();
385			avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
386			avatar.image = new String(mByteArrayOutputStream.toByteArray());
387			return avatar;
388		} catch (NoSuchAlgorithmException e) {
389			return null;
390		} catch (IOException e) {
391			return null;
392		}
393	}
394
395	public boolean isAvatarCached(Avatar avatar) {
396		File file = new File(getAvatarPath(avatar.getFilename()));
397		return file.exists();
398	}
399
400	public boolean save(Avatar avatar) {
401		File file;
402		if (isAvatarCached(avatar)) {
403			file = new File(getAvatarPath(avatar.getFilename()));
404		} else {
405			String filename = getAvatarPath(avatar.getFilename());
406			file = new File(filename + ".tmp");
407			file.getParentFile().mkdirs();
408			OutputStream os = null;
409			try {
410				file.createNewFile();
411				os = new FileOutputStream(file);
412				MessageDigest digest = MessageDigest.getInstance("SHA-1");
413				digest.reset();
414				DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
415				mDigestOutputStream.write(avatar.getImageAsBytes());
416				mDigestOutputStream.flush();
417				mDigestOutputStream.close();
418				String sha1sum = CryptoHelper.bytesToHex(digest.digest());
419				if (sha1sum.equals(avatar.sha1sum)) {
420					file.renameTo(new File(filename));
421				} else {
422					Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
423					file.delete();
424					return false;
425				}
426			} catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
427				return false;
428			} finally {
429				close(os);
430			}
431		}
432		avatar.size = file.length();
433		return true;
434	}
435
436	public String getAvatarPath(String avatar) {
437		return mXmppConnectionService.getFilesDir().getAbsolutePath()+ "/avatars/" + avatar;
438	}
439
440	public Uri getAvatarUri(String avatar) {
441		return Uri.parse("file:" + getAvatarPath(avatar));
442	}
443
444	public Bitmap cropCenterSquare(Uri image, int size) {
445		if (image == null) {
446			return null;
447		}
448		InputStream is = null;
449		try {
450			BitmapFactory.Options options = new BitmapFactory.Options();
451			options.inSampleSize = calcSampleSize(image, size);
452			is = mXmppConnectionService.getContentResolver().openInputStream(image);
453			if (is == null) {
454				return null;
455			}
456			Bitmap input = BitmapFactory.decodeStream(is, null, options);
457			if (input == null) {
458				return null;
459			} else {
460				input = rotate(input, getRotation(image));
461				return cropCenterSquare(input, size);
462			}
463		} catch (SecurityException e) {
464			return null; // happens for example on Android 6.0 if contacts permissions get revoked
465		} catch (FileNotFoundException e) {
466			return null;
467		} finally {
468			close(is);
469		}
470	}
471
472	public Bitmap cropCenter(Uri image, int newHeight, int newWidth) {
473		if (image == null) {
474			return null;
475		}
476		InputStream is = null;
477		try {
478			BitmapFactory.Options options = new BitmapFactory.Options();
479			options.inSampleSize = calcSampleSize(image, Math.max(newHeight, newWidth));
480			is = mXmppConnectionService.getContentResolver().openInputStream(image);
481			if (is == null) {
482				return null;
483			}
484			Bitmap source = BitmapFactory.decodeStream(is, null, options);
485			if (source == null) {
486				return null;
487			}
488			int sourceWidth = source.getWidth();
489			int sourceHeight = source.getHeight();
490			float xScale = (float) newWidth / sourceWidth;
491			float yScale = (float) newHeight / sourceHeight;
492			float scale = Math.max(xScale, yScale);
493			float scaledWidth = scale * sourceWidth;
494			float scaledHeight = scale * sourceHeight;
495			float left = (newWidth - scaledWidth) / 2;
496			float top = (newHeight - scaledHeight) / 2;
497
498			RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
499			Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
500			Canvas canvas = new Canvas(dest);
501			canvas.drawBitmap(source, null, targetRect, null);
502			if (source != null && !source.isRecycled()) {
503				source.recycle();
504			}
505			return dest;
506		} catch (SecurityException e) {
507			return null; //android 6.0 with revoked permissions for example
508		} catch (FileNotFoundException e) {
509			return null;
510		} finally {
511			close(is);
512		}
513	}
514
515	public Bitmap cropCenterSquare(Bitmap input, int size) {
516		int w = input.getWidth();
517		int h = input.getHeight();
518
519		float scale = Math.max((float) size / h, (float) size / w);
520
521		float outWidth = scale * w;
522		float outHeight = scale * h;
523		float left = (size - outWidth) / 2;
524		float top = (size - outHeight) / 2;
525		RectF target = new RectF(left, top, left + outWidth, top + outHeight);
526
527		Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
528		Canvas canvas = new Canvas(output);
529		canvas.drawBitmap(input, null, target, null);
530		if (input != null && !input.isRecycled()) {
531			input.recycle();
532		}
533		return output;
534	}
535
536	private int calcSampleSize(Uri image, int size) throws FileNotFoundException, SecurityException {
537		BitmapFactory.Options options = new BitmapFactory.Options();
538		options.inJustDecodeBounds = true;
539		BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(image), null, options);
540		return calcSampleSize(options, size);
541	}
542
543	private static int calcSampleSize(File image, int size) {
544		BitmapFactory.Options options = new BitmapFactory.Options();
545		options.inJustDecodeBounds = true;
546		BitmapFactory.decodeFile(image.getAbsolutePath(), options);
547		return calcSampleSize(options, size);
548	}
549
550	public static int calcSampleSize(BitmapFactory.Options options, int size) {
551		int height = options.outHeight;
552		int width = options.outWidth;
553		int inSampleSize = 1;
554
555		if (height > size || width > size) {
556			int halfHeight = height / 2;
557			int halfWidth = width / 2;
558
559			while ((halfHeight / inSampleSize) > size
560					&& (halfWidth / inSampleSize) > size) {
561				inSampleSize *= 2;
562			}
563		}
564		return inSampleSize;
565	}
566
567	public Uri getJingleFileUri(Message message) {
568		File file = getFile(message);
569		return Uri.parse("file://" + file.getAbsolutePath());
570	}
571
572	public void updateFileParams(Message message) {
573		updateFileParams(message,null);
574	}
575
576	public void updateFileParams(Message message, URL url) {
577		DownloadableFile file = getFile(message);
578		if (message.getType() == Message.TYPE_IMAGE || file.getMimeType().startsWith("image/")) {
579			BitmapFactory.Options options = new BitmapFactory.Options();
580			options.inJustDecodeBounds = true;
581			BitmapFactory.decodeFile(file.getAbsolutePath(), options);
582			int rotation = getRotation(file);
583			boolean rotated = rotation == 90 || rotation == 270;
584			int imageHeight = rotated ? options.outWidth : options.outHeight;
585			int imageWidth = rotated ? options.outHeight : options.outWidth;
586			if (url == null) {
587				message.setBody(Long.toString(file.getSize()) + '|' + imageWidth + '|' + imageHeight);
588			} else {
589				message.setBody(url.toString()+"|"+Long.toString(file.getSize()) + '|' + imageWidth + '|' + imageHeight);
590			}
591		} else {
592			if (url != null) {
593				message.setBody(url.toString()+"|"+Long.toString(file.getSize()));
594			} else {
595				message.setBody(Long.toString(file.getSize()));
596			}
597		}
598
599	}
600
601	public class FileCopyException extends Exception {
602		private static final long serialVersionUID = -1010013599132881427L;
603		private int resId;
604
605		public FileCopyException(int resId) {
606			this.resId = resId;
607		}
608
609		public int getResId() {
610			return resId;
611		}
612	}
613
614	public Bitmap getAvatar(String avatar, int size) {
615		if (avatar == null) {
616			return null;
617		}
618		Bitmap bm = cropCenter(getAvatarUri(avatar), size, size);
619		if (bm == null) {
620			return null;
621		}
622		return bm;
623	}
624
625	public boolean isFileAvailable(Message message) {
626		return getFile(message).exists();
627	}
628
629	public static void close(Closeable stream) {
630		if (stream != null) {
631			try {
632				stream.close();
633			} catch (IOException e) {
634			}
635		}
636	}
637
638	public static void close(Socket socket) {
639		if (socket != null) {
640			try {
641				socket.close();
642			} catch (IOException e) {
643			}
644		}
645	}
646}