FileBackend.java

   1package eu.siacs.conversations.persistance;
   2
   3import android.annotation.TargetApi;
   4import android.content.ContentResolver;
   5import android.content.Context;
   6import android.content.Intent;
   7import android.database.Cursor;
   8import android.graphics.Bitmap;
   9import android.graphics.BitmapFactory;
  10import android.graphics.Canvas;
  11import android.graphics.Color;
  12import android.graphics.Matrix;
  13import android.graphics.Paint;
  14import android.graphics.RectF;
  15import android.media.MediaMetadataRetriever;
  16import android.net.Uri;
  17import android.os.Build;
  18import android.os.Environment;
  19import android.os.ParcelFileDescriptor;
  20import android.provider.MediaStore;
  21import android.provider.OpenableColumns;
  22import android.support.v4.content.FileProvider;
  23import android.system.Os;
  24import android.system.StructStat;
  25import android.util.Base64;
  26import android.util.Base64OutputStream;
  27import android.util.Log;
  28import android.util.LruCache;
  29import android.webkit.MimeTypeMap;
  30
  31import java.io.ByteArrayOutputStream;
  32import java.io.Closeable;
  33import java.io.File;
  34import java.io.FileDescriptor;
  35import java.io.FileInputStream;
  36import java.io.FileNotFoundException;
  37import java.io.FileOutputStream;
  38import java.io.IOException;
  39import java.io.InputStream;
  40import java.io.OutputStream;
  41import java.net.Socket;
  42import java.net.URL;
  43import java.security.DigestOutputStream;
  44import java.security.MessageDigest;
  45import java.security.NoSuchAlgorithmException;
  46import java.text.SimpleDateFormat;
  47import java.util.Arrays;
  48import java.util.Date;
  49import java.util.List;
  50import java.util.Locale;
  51import java.util.UUID;
  52
  53import eu.siacs.conversations.Config;
  54import eu.siacs.conversations.R;
  55import eu.siacs.conversations.entities.DownloadableFile;
  56import eu.siacs.conversations.entities.Message;
  57import eu.siacs.conversations.services.XmppConnectionService;
  58import eu.siacs.conversations.ui.RecordingActivity;
  59import eu.siacs.conversations.utils.CryptoHelper;
  60import eu.siacs.conversations.utils.ExifHelper;
  61import eu.siacs.conversations.utils.FileUtils;
  62import eu.siacs.conversations.utils.FileWriterException;
  63import eu.siacs.conversations.utils.MimeUtils;
  64import eu.siacs.conversations.xmpp.pep.Avatar;
  65
  66public class FileBackend {
  67
  68	private static final Object THUMBNAIL_LOCK = new Object();
  69
  70	private static final SimpleDateFormat IMAGE_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
  71
  72	private static final String FILE_PROVIDER = ".files";
  73
  74	private XmppConnectionService mXmppConnectionService;
  75
  76	public FileBackend(XmppConnectionService service) {
  77		this.mXmppConnectionService = service;
  78	}
  79
  80	private void createNoMedia(File diretory) {
  81		final File noMedia = new File(diretory,".nomedia");
  82		if (!noMedia.exists()) {
  83			try {
  84				if (!noMedia.createNewFile()) {
  85					Log.d(Config.LOGTAG,"created nomedia file "+noMedia.getAbsolutePath());
  86				}
  87			} catch (Exception e) {
  88				Log.d(Config.LOGTAG, "could not create nomedia file");
  89			}
  90		}
  91	}
  92
  93	public void updateMediaScanner(File file) {
  94		if (!isInDirectoryThatShouldNotBeScanned(mXmppConnectionService, file)) {
  95			Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  96			intent.setData(Uri.fromFile(file));
  97			mXmppConnectionService.sendBroadcast(intent);
  98		} else if (file.getAbsolutePath().startsWith(getAppMediaDirectory(mXmppConnectionService))) {
  99			createNoMedia(file.getParentFile());
 100		}
 101	}
 102
 103	private static boolean isInDirectoryThatShouldNotBeScanned(Context context, File file) {
 104		return isInDirectoryThatShouldNotBeScanned(context, file.getAbsolutePath());
 105	}
 106
 107	public static boolean isInDirectoryThatShouldNotBeScanned(Context context, String path) {
 108		for(String type : new String[]{RecordingActivity.STORAGE_DIRECTORY_TYPE_NAME, "Files"}) {
 109			if (path.startsWith(getConversationsDirectory(context, type))) {
 110				return true;
 111			}
 112		}
 113		return false;
 114	}
 115
 116	public boolean deleteFile(Message message) {
 117		File file = getFile(message);
 118		if (file.delete()) {
 119			updateMediaScanner(file);
 120			return true;
 121		} else {
 122			return false;
 123		}
 124	}
 125
 126	public DownloadableFile getFile(Message message) {
 127		return getFile(message, true);
 128	}
 129
 130	public DownloadableFile getFileForPath(String path, String mime) {
 131		final DownloadableFile file;
 132		if (path.startsWith("/")) {
 133			file = new DownloadableFile(path);
 134		} else {
 135			if (mime != null && mime.startsWith("image/")) {
 136				file = new DownloadableFile(getConversationsDirectory("Images") + path);
 137			} else if (mime != null && mime.startsWith("video/")) {
 138				file = new DownloadableFile(getConversationsDirectory("Videos") + path);
 139			} else {
 140				file = new DownloadableFile(getConversationsDirectory("Files") + path);
 141			}
 142		}
 143		return file;
 144	}
 145
 146	public DownloadableFile getFile(Message message, boolean decrypted) {
 147		final boolean encrypted = !decrypted
 148				&& (message.getEncryption() == Message.ENCRYPTION_PGP
 149				|| message.getEncryption() == Message.ENCRYPTION_DECRYPTED);
 150		String path = message.getRelativeFilePath();
 151		if (path == null) {
 152			path = message.getUuid();
 153		}
 154		final DownloadableFile file = getFileForPath(path, message.getMimeType());
 155		if (encrypted) {
 156			return new DownloadableFile(getConversationsDirectory("Files") + file.getName() + ".pgp");
 157		} else {
 158			return file;
 159		}
 160	}
 161
 162	public static long getFileSize(Context context, Uri uri) {
 163		try {
 164			final Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
 165			if (cursor != null && cursor.moveToFirst()) {
 166				long size = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE));
 167				cursor.close();
 168				return size;
 169			} else {
 170				return -1;
 171			}
 172		} catch (Exception e) {
 173			return -1;
 174		}
 175	}
 176
 177	public static boolean allFilesUnderSize(Context context, List<Uri> uris, long max) {
 178		if (max <= 0) {
 179			Log.d(Config.LOGTAG, "server did not report max file size for http upload");
 180			return true; //exception to be compatible with HTTP Upload < v0.2
 181		}
 182		for (Uri uri : uris) {
 183			String mime = context.getContentResolver().getType(uri);
 184			if (mime != null && mime.startsWith("video/")) {
 185				try {
 186					Dimensions dimensions = FileBackend.getVideoDimensions(context, uri);
 187					if (dimensions.getMin() > 720) {
 188						Log.d(Config.LOGTAG, "do not consider video file with min width larger than 720 for size check");
 189						continue;
 190					}
 191				} catch (NotAVideoFile notAVideoFile) {
 192					//ignore and fall through
 193				}
 194			}
 195			if (FileBackend.getFileSize(context, uri) > max) {
 196				Log.d(Config.LOGTAG, "not all files are under " + max + " bytes. suggesting falling back to jingle");
 197				return false;
 198			}
 199		}
 200		return true;
 201	}
 202
 203	public String getConversationsDirectory(final String type) {
 204		return getConversationsDirectory(mXmppConnectionService, type);
 205	}
 206
 207	public static String getConversationsDirectory(Context context, final String type) {
 208		if (Config.ONLY_INTERNAL_STORAGE) {
 209			return context.getFilesDir().getAbsolutePath() + "/" + type + "/";
 210		} else {
 211			return getAppMediaDirectory(context)+context.getString(R.string.app_name)+" " + type + "/";
 212		}
 213	}
 214
 215	public static String getAppMediaDirectory(Context context) {
 216		return Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+context.getString(R.string.app_name)+"/Media/";
 217	}
 218
 219	public static String getConversationsLogsDirectory() {
 220		return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Conversations/";
 221	}
 222
 223	public Bitmap resize(Bitmap originalBitmap, int size) {
 224		int w = originalBitmap.getWidth();
 225		int h = originalBitmap.getHeight();
 226		if (Math.max(w, h) > size) {
 227			int scalledW;
 228			int scalledH;
 229			if (w <= h) {
 230				scalledW = (int) (w / ((double) h / size));
 231				scalledH = size;
 232			} else {
 233				scalledW = size;
 234				scalledH = (int) (h / ((double) w / size));
 235			}
 236			Bitmap result = Bitmap.createScaledBitmap(originalBitmap, scalledW, scalledH, true);
 237			if (originalBitmap != null && !originalBitmap.isRecycled()) {
 238				originalBitmap.recycle();
 239			}
 240			return result;
 241		} else {
 242			return originalBitmap;
 243		}
 244	}
 245
 246	private static Bitmap rotate(Bitmap bitmap, int degree) {
 247		if (degree == 0) {
 248			return bitmap;
 249		}
 250		int w = bitmap.getWidth();
 251		int h = bitmap.getHeight();
 252		Matrix mtx = new Matrix();
 253		mtx.postRotate(degree);
 254		Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
 255		if (bitmap != null && !bitmap.isRecycled()) {
 256			bitmap.recycle();
 257		}
 258		return result;
 259	}
 260
 261
 262	public boolean useImageAsIs(Uri uri) {
 263		String path = getOriginalPath(uri);
 264		if (path == null || isPathBlacklisted(path)) {
 265			return false;
 266		}
 267		File file = new File(path);
 268		long size = file.length();
 269		if (size == 0 || size >= mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize)) {
 270			return false;
 271		}
 272		BitmapFactory.Options options = new BitmapFactory.Options();
 273		options.inJustDecodeBounds = true;
 274		try {
 275			BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(uri), null, options);
 276			if (options.outMimeType == null || options.outHeight <= 0 || options.outWidth <= 0) {
 277				return false;
 278			}
 279			return (options.outWidth <= Config.IMAGE_SIZE && options.outHeight <= Config.IMAGE_SIZE && options.outMimeType.contains(Config.IMAGE_FORMAT.name().toLowerCase()));
 280		} catch (FileNotFoundException e) {
 281			return false;
 282		}
 283	}
 284
 285	public static boolean isPathBlacklisted(String path) {
 286		final String androidDataPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/";
 287		return path.startsWith(androidDataPath);
 288	}
 289
 290	public String getOriginalPath(Uri uri) {
 291		return FileUtils.getPath(mXmppConnectionService, uri);
 292	}
 293
 294	private void copyFileToPrivateStorage(File file, Uri uri) throws FileCopyException {
 295		Log.d(Config.LOGTAG, "copy file (" + uri.toString() + ") to private storage " + file.getAbsolutePath());
 296		file.getParentFile().mkdirs();
 297		OutputStream os = null;
 298		InputStream is = null;
 299		try {
 300			file.createNewFile();
 301			os = new FileOutputStream(file);
 302			is = mXmppConnectionService.getContentResolver().openInputStream(uri);
 303			byte[] buffer = new byte[1024];
 304			int length;
 305			while ((length = is.read(buffer)) > 0) {
 306				try {
 307					os.write(buffer, 0, length);
 308				} catch (IOException e) {
 309					throw new FileWriterException();
 310				}
 311			}
 312			try {
 313				os.flush();
 314			} catch (IOException e) {
 315				throw new FileWriterException();
 316			}
 317		} catch (FileNotFoundException e) {
 318			throw new FileCopyException(R.string.error_file_not_found);
 319		} catch (FileWriterException e) {
 320			throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
 321		} catch (IOException e) {
 322			e.printStackTrace();
 323			throw new FileCopyException(R.string.error_io_exception);
 324		} finally {
 325			close(os);
 326			close(is);
 327		}
 328	}
 329
 330	public void copyFileToPrivateStorage(Message message, Uri uri, String type) throws FileCopyException {
 331		String mime = type != null ? type : MimeUtils.guessMimeTypeFromUri(mXmppConnectionService, uri);
 332		Log.d(Config.LOGTAG, "copy " + uri.toString() + " to private storage (mime=" + mime + ")");
 333		String extension = MimeUtils.guessExtensionFromMimeType(mime);
 334		if (extension == null) {
 335			Log.d(Config.LOGTAG,"extension from mime type was null");
 336			extension = getExtensionFromUri(uri);
 337		}
 338		if ("ogg".equals(extension) && type != null && type.startsWith("audio/")) {
 339			extension = "oga";
 340		}
 341		message.setRelativeFilePath(message.getUuid() + "." + extension);
 342		copyFileToPrivateStorage(mXmppConnectionService.getFileBackend().getFile(message), uri);
 343	}
 344
 345	private String getExtensionFromUri(Uri uri) {
 346		String[] projection = {MediaStore.MediaColumns.DATA};
 347		String filename = null;
 348		Cursor cursor = mXmppConnectionService.getContentResolver().query(uri, projection, null, null, null);
 349		if (cursor != null) {
 350			try {
 351				if (cursor.moveToFirst()) {
 352					filename = cursor.getString(0);
 353				}
 354			} catch (Exception e) {
 355				filename = null;
 356			} finally {
 357				cursor.close();
 358			}
 359		}
 360		if (filename == null) {
 361			final List<String> segments = uri.getPathSegments();
 362			if (segments.size() > 0) {
 363				filename = segments.get(segments.size() -1);
 364			}
 365		}
 366		int pos = filename == null ? -1 : filename.lastIndexOf('.');
 367		return pos > 0 ? filename.substring(pos + 1) : null;
 368	}
 369
 370	private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException {
 371		file.getParentFile().mkdirs();
 372		InputStream is = null;
 373		OutputStream os = null;
 374		try {
 375			if (!file.exists() && !file.createNewFile()) {
 376				throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
 377			}
 378			is = mXmppConnectionService.getContentResolver().openInputStream(image);
 379			if (is == null) {
 380				throw new FileCopyException(R.string.error_not_an_image_file);
 381			}
 382			Bitmap originalBitmap;
 383			BitmapFactory.Options options = new BitmapFactory.Options();
 384			int inSampleSize = (int) Math.pow(2, sampleSize);
 385			Log.d(Config.LOGTAG, "reading bitmap with sample size " + inSampleSize);
 386			options.inSampleSize = inSampleSize;
 387			originalBitmap = BitmapFactory.decodeStream(is, null, options);
 388			is.close();
 389			if (originalBitmap == null) {
 390				throw new FileCopyException(R.string.error_not_an_image_file);
 391			}
 392			Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
 393			int rotation = getRotation(image);
 394			scaledBitmap = rotate(scaledBitmap, rotation);
 395			boolean targetSizeReached = false;
 396			int quality = Config.IMAGE_QUALITY;
 397			final int imageMaxSize = mXmppConnectionService.getResources().getInteger(R.integer.auto_accept_filesize);
 398			while (!targetSizeReached) {
 399				os = new FileOutputStream(file);
 400				boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, quality, os);
 401				if (!success) {
 402					throw new FileCopyException(R.string.error_compressing_image);
 403				}
 404				os.flush();
 405				targetSizeReached = file.length() <= imageMaxSize || quality <= 50;
 406				quality -= 5;
 407			}
 408			scaledBitmap.recycle();
 409		} catch (FileNotFoundException e) {
 410			throw new FileCopyException(R.string.error_file_not_found);
 411		} catch (IOException e) {
 412			e.printStackTrace();
 413			throw new FileCopyException(R.string.error_io_exception);
 414		} catch (SecurityException e) {
 415			throw new FileCopyException(R.string.error_security_exception_during_image_copy);
 416		} catch (OutOfMemoryError e) {
 417			++sampleSize;
 418			if (sampleSize <= 3) {
 419				copyImageToPrivateStorage(file, image, sampleSize);
 420			} else {
 421				throw new FileCopyException(R.string.error_out_of_memory);
 422			}
 423		} finally {
 424			close(os);
 425			close(is);
 426		}
 427	}
 428
 429	public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException {
 430		Log.d(Config.LOGTAG, "copy image (" + image.toString() + ") to private storage " + file.getAbsolutePath());
 431		copyImageToPrivateStorage(file, image, 0);
 432	}
 433
 434	public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException {
 435		switch (Config.IMAGE_FORMAT) {
 436			case JPEG:
 437				message.setRelativeFilePath(message.getUuid() + ".jpg");
 438				break;
 439			case PNG:
 440				message.setRelativeFilePath(message.getUuid() + ".png");
 441				break;
 442			case WEBP:
 443				message.setRelativeFilePath(message.getUuid() + ".webp");
 444				break;
 445		}
 446		copyImageToPrivateStorage(getFile(message), image);
 447		updateFileParams(message);
 448	}
 449
 450	private int getRotation(File file) {
 451		return getRotation(Uri.parse("file://" + file.getAbsolutePath()));
 452	}
 453
 454	private int getRotation(Uri image) {
 455		InputStream is = null;
 456		try {
 457			is = mXmppConnectionService.getContentResolver().openInputStream(image);
 458			return ExifHelper.getOrientation(is);
 459		} catch (FileNotFoundException e) {
 460			return 0;
 461		} finally {
 462			close(is);
 463		}
 464	}
 465
 466	public Bitmap getThumbnail(Message message, int size, boolean cacheOnly) throws FileNotFoundException {
 467		final String uuid = message.getUuid();
 468		final LruCache<String, Bitmap> cache = mXmppConnectionService.getBitmapCache();
 469		Bitmap thumbnail = cache.get(uuid);
 470		if ((thumbnail == null) && (!cacheOnly)) {
 471			synchronized (THUMBNAIL_LOCK) {
 472				thumbnail = cache.get(uuid);
 473				if (thumbnail != null) {
 474					return thumbnail;
 475				}
 476				DownloadableFile file = getFile(message);
 477				final String mime = file.getMimeType();
 478				if (mime.startsWith("video/")) {
 479					thumbnail = getVideoPreview(file, size);
 480				} else {
 481					Bitmap fullsize = getFullsizeImagePreview(file, size);
 482					if (fullsize == null) {
 483						throw new FileNotFoundException();
 484					}
 485					thumbnail = resize(fullsize, size);
 486					thumbnail = rotate(thumbnail, getRotation(file));
 487					if (mime.equals("image/gif")) {
 488						Bitmap withGifOverlay = thumbnail.copy(Bitmap.Config.ARGB_8888, true);
 489						drawOverlay(withGifOverlay, R.drawable.play_gif, 1.0f);
 490						thumbnail.recycle();
 491						thumbnail = withGifOverlay;
 492					}
 493				}
 494				this.mXmppConnectionService.getBitmapCache().put(uuid, thumbnail);
 495			}
 496		}
 497		return thumbnail;
 498	}
 499
 500	private Bitmap getFullsizeImagePreview(File file, int size) {
 501		BitmapFactory.Options options = new BitmapFactory.Options();
 502		options.inSampleSize = calcSampleSize(file, size);
 503		try {
 504			return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
 505		} catch (OutOfMemoryError e) {
 506			options.inSampleSize *= 2;
 507			return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
 508		}
 509	}
 510
 511	private void drawOverlay(Bitmap bitmap, int resource, float factor) {
 512		Bitmap overlay = BitmapFactory.decodeResource(mXmppConnectionService.getResources(), resource);
 513		Canvas canvas = new Canvas(bitmap);
 514		float targetSize = Math.min(canvas.getWidth(), canvas.getHeight()) * factor;
 515		Log.d(Config.LOGTAG, "target size overlay: " + targetSize + " overlay bitmap size was " + overlay.getHeight());
 516		float left = (canvas.getWidth() - targetSize) / 2.0f;
 517		float top = (canvas.getHeight() - targetSize) / 2.0f;
 518		RectF dst = new RectF(left, top, left + targetSize - 1, top + targetSize - 1);
 519		canvas.drawBitmap(overlay, null, dst, createAntiAliasingPaint());
 520	}
 521
 522	private static Paint createAntiAliasingPaint() {
 523		Paint paint = new Paint();
 524		paint.setAntiAlias(true);
 525		paint.setFilterBitmap(true);
 526		paint.setDither(true);
 527		return paint;
 528	}
 529
 530	private Bitmap getVideoPreview(File file, int size) {
 531		MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
 532		Bitmap frame;
 533		try {
 534			metadataRetriever.setDataSource(file.getAbsolutePath());
 535			frame = metadataRetriever.getFrameAtTime(0);
 536			metadataRetriever.release();
 537			frame = resize(frame, size);
 538		} catch (RuntimeException e) {
 539			frame = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
 540			frame.eraseColor(0xff000000);
 541		}
 542		drawOverlay(frame, R.drawable.play_video, 0.75f);
 543		return frame;
 544	}
 545
 546	private static String getTakePhotoPath() {
 547		return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/";
 548	}
 549
 550	public Uri getTakePhotoUri() {
 551		File file;
 552		if (Config.ONLY_INTERNAL_STORAGE) {
 553			file = new File(mXmppConnectionService.getCacheDir().getAbsolutePath(), "Camera/IMG_" + this.IMAGE_DATE_FORMAT.format(new Date()) + ".jpg");
 554		} else {
 555			file = new File(getTakePhotoPath() + "IMG_" + this.IMAGE_DATE_FORMAT.format(new Date()) + ".jpg");
 556		}
 557		file.getParentFile().mkdirs();
 558		return getUriForFile(mXmppConnectionService, file);
 559	}
 560
 561	public static Uri getUriForFile(Context context, File file) {
 562		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) {
 563			try {
 564				return FileProvider.getUriForFile(context, getAuthority(context), file);
 565			} catch (IllegalArgumentException e) {
 566				if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
 567					throw new SecurityException(e);
 568				} else {
 569					return Uri.fromFile(file);
 570				}
 571			}
 572		} else {
 573			return Uri.fromFile(file);
 574		}
 575	}
 576
 577	public static String getAuthority(Context context) {
 578		return context.getPackageName() + FILE_PROVIDER;
 579	}
 580
 581	public static Uri getIndexableTakePhotoUri(Uri original) {
 582		if (Config.ONLY_INTERNAL_STORAGE || "file".equals(original.getScheme())) {
 583			return original;
 584		} else {
 585			List<String> segments = original.getPathSegments();
 586			return Uri.parse("file://" + getTakePhotoPath() + segments.get(segments.size() - 1));
 587		}
 588	}
 589
 590	public Avatar getPepAvatar(Uri image, int size, Bitmap.CompressFormat format) {
 591		Bitmap bm = cropCenterSquare(image, size);
 592		if (bm == null) {
 593			return null;
 594		}
 595		if (hasAlpha(bm)) {
 596			Log.d(Config.LOGTAG, "alpha in avatar detected; uploading as PNG");
 597			bm.recycle();
 598			bm = cropCenterSquare(image, 96);
 599			return getPepAvatar(bm, Bitmap.CompressFormat.PNG, 100);
 600		}
 601		return getPepAvatar(bm, format, 100);
 602	}
 603
 604	private static boolean hasAlpha(final Bitmap bitmap) {
 605		for (int x = 0; x < bitmap.getWidth(); ++x) {
 606			for (int y = 0; y < bitmap.getWidth(); ++y) {
 607				if (Color.alpha(bitmap.getPixel(x, y)) < 255) {
 608					return true;
 609				}
 610			}
 611		}
 612		return false;
 613	}
 614
 615	private Avatar getPepAvatar(Bitmap bitmap, Bitmap.CompressFormat format, int quality) {
 616		try {
 617			ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
 618			Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
 619			MessageDigest digest = MessageDigest.getInstance("SHA-1");
 620			DigestOutputStream mDigestOutputStream = new DigestOutputStream(mBase64OutputStream, digest);
 621			if (!bitmap.compress(format, quality, mDigestOutputStream)) {
 622				return null;
 623			}
 624			mDigestOutputStream.flush();
 625			mDigestOutputStream.close();
 626			long chars = mByteArrayOutputStream.size();
 627			if (format != Bitmap.CompressFormat.PNG && quality >= 50 && chars >= Config.AVATAR_CHAR_LIMIT) {
 628				int q = quality - 2;
 629				Log.d(Config.LOGTAG, "avatar char length was " + chars + " reducing quality to " + q);
 630				return getPepAvatar(bitmap, format, q);
 631			}
 632			Log.d(Config.LOGTAG, "settled on char length " + chars + " with quality=" + quality);
 633			final Avatar avatar = new Avatar();
 634			avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
 635			avatar.image = new String(mByteArrayOutputStream.toByteArray());
 636			if (format.equals(Bitmap.CompressFormat.WEBP)) {
 637				avatar.type = "image/webp";
 638			} else if (format.equals(Bitmap.CompressFormat.JPEG)) {
 639				avatar.type = "image/jpeg";
 640			} else if (format.equals(Bitmap.CompressFormat.PNG)) {
 641				avatar.type = "image/png";
 642			}
 643			avatar.width = bitmap.getWidth();
 644			avatar.height = bitmap.getHeight();
 645			return avatar;
 646		} catch (Exception e) {
 647			return null;
 648		}
 649	}
 650
 651	public Avatar getStoredPepAvatar(String hash) {
 652		if (hash == null) {
 653			return null;
 654		}
 655		Avatar avatar = new Avatar();
 656		File file = new File(getAvatarPath(hash));
 657		FileInputStream is = null;
 658		try {
 659			avatar.size = file.length();
 660			BitmapFactory.Options options = new BitmapFactory.Options();
 661			options.inJustDecodeBounds = true;
 662			BitmapFactory.decodeFile(file.getAbsolutePath(), options);
 663			is = new FileInputStream(file);
 664			ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
 665			Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT);
 666			MessageDigest digest = MessageDigest.getInstance("SHA-1");
 667			DigestOutputStream os = new DigestOutputStream(mBase64OutputStream, digest);
 668			byte[] buffer = new byte[4096];
 669			int length;
 670			while ((length = is.read(buffer)) > 0) {
 671				os.write(buffer, 0, length);
 672			}
 673			os.flush();
 674			os.close();
 675			avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest());
 676			avatar.image = new String(mByteArrayOutputStream.toByteArray());
 677			avatar.height = options.outHeight;
 678			avatar.width = options.outWidth;
 679			avatar.type = options.outMimeType;
 680			return avatar;
 681		} catch (NoSuchAlgorithmException | IOException e) {
 682			return null;
 683		} finally {
 684			close(is);
 685		}
 686	}
 687
 688	public boolean isAvatarCached(Avatar avatar) {
 689		File file = new File(getAvatarPath(avatar.getFilename()));
 690		return file.exists();
 691	}
 692
 693	public boolean save(final Avatar avatar) {
 694		File file;
 695		if (isAvatarCached(avatar)) {
 696			file = new File(getAvatarPath(avatar.getFilename()));
 697			avatar.size = file.length();
 698		} else {
 699			file = new File(mXmppConnectionService.getCacheDir().getAbsolutePath() + "/" + UUID.randomUUID().toString());
 700			if (file.getParentFile().mkdirs()) {
 701				Log.d(Config.LOGTAG,"created cache directory");
 702			}
 703			OutputStream os = null;
 704			try {
 705				if (!file.createNewFile()) {
 706					Log.d(Config.LOGTAG,"unable to create temporary file "+file.getAbsolutePath());
 707				}
 708				os = new FileOutputStream(file);
 709				MessageDigest digest = MessageDigest.getInstance("SHA-1");
 710				digest.reset();
 711				DigestOutputStream mDigestOutputStream = new DigestOutputStream(os, digest);
 712				final byte[] bytes = avatar.getImageAsBytes();
 713				mDigestOutputStream.write(bytes);
 714				mDigestOutputStream.flush();
 715				mDigestOutputStream.close();
 716				String sha1sum = CryptoHelper.bytesToHex(digest.digest());
 717				if (sha1sum.equals(avatar.sha1sum)) {
 718					File outputFile = new File(getAvatarPath(avatar.getFilename()));
 719					if (outputFile.getParentFile().mkdirs()) {
 720						Log.d(Config.LOGTAG,"created avatar directory");
 721					}
 722					String filename = getAvatarPath(avatar.getFilename());
 723					if (!file.renameTo(new File(filename))) {
 724						Log.d(Config.LOGTAG,"unable to rename "+file.getAbsolutePath()+" to "+outputFile);
 725						return false;
 726					}
 727				} else {
 728					Log.d(Config.LOGTAG, "sha1sum mismatch for " + avatar.owner);
 729					if (!file.delete()) {
 730						Log.d(Config.LOGTAG,"unable to delete temporary file");
 731					}
 732					return false;
 733				}
 734				avatar.size = bytes.length;
 735			} catch (IllegalArgumentException | IOException | NoSuchAlgorithmException e) {
 736				return false;
 737			} finally {
 738				close(os);
 739			}
 740		}
 741		return true;
 742	}
 743
 744	private String getAvatarPath(String avatar) {
 745		return mXmppConnectionService.getFilesDir().getAbsolutePath() + "/avatars/" + avatar;
 746	}
 747
 748	public Uri getAvatarUri(String avatar) {
 749		return Uri.parse("file:" + getAvatarPath(avatar));
 750	}
 751
 752	public Bitmap cropCenterSquare(Uri image, int size) {
 753		if (image == null) {
 754			return null;
 755		}
 756		InputStream is = null;
 757		try {
 758			BitmapFactory.Options options = new BitmapFactory.Options();
 759			options.inSampleSize = calcSampleSize(image, size);
 760			is = mXmppConnectionService.getContentResolver().openInputStream(image);
 761			if (is == null) {
 762				return null;
 763			}
 764			Bitmap input = BitmapFactory.decodeStream(is, null, options);
 765			if (input == null) {
 766				return null;
 767			} else {
 768				input = rotate(input, getRotation(image));
 769				return cropCenterSquare(input, size);
 770			}
 771		} catch (FileNotFoundException | SecurityException e) {
 772			return null;
 773		} finally {
 774			close(is);
 775		}
 776	}
 777
 778	public Bitmap cropCenter(Uri image, int newHeight, int newWidth) {
 779		if (image == null) {
 780			return null;
 781		}
 782		InputStream is = null;
 783		try {
 784			BitmapFactory.Options options = new BitmapFactory.Options();
 785			options.inSampleSize = calcSampleSize(image, Math.max(newHeight, newWidth));
 786			is = mXmppConnectionService.getContentResolver().openInputStream(image);
 787			if (is == null) {
 788				return null;
 789			}
 790			Bitmap source = BitmapFactory.decodeStream(is, null, options);
 791			if (source == null) {
 792				return null;
 793			}
 794			int sourceWidth = source.getWidth();
 795			int sourceHeight = source.getHeight();
 796			float xScale = (float) newWidth / sourceWidth;
 797			float yScale = (float) newHeight / sourceHeight;
 798			float scale = Math.max(xScale, yScale);
 799			float scaledWidth = scale * sourceWidth;
 800			float scaledHeight = scale * sourceHeight;
 801			float left = (newWidth - scaledWidth) / 2;
 802			float top = (newHeight - scaledHeight) / 2;
 803
 804			RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
 805			Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
 806			Canvas canvas = new Canvas(dest);
 807			canvas.drawBitmap(source, null, targetRect, createAntiAliasingPaint());
 808			if (source.isRecycled()) {
 809				source.recycle();
 810			}
 811			return dest;
 812		} catch (SecurityException e) {
 813			return null; //android 6.0 with revoked permissions for example
 814		} catch (FileNotFoundException e) {
 815			return null;
 816		} finally {
 817			close(is);
 818		}
 819	}
 820
 821	public Bitmap cropCenterSquare(Bitmap input, int size) {
 822		int w = input.getWidth();
 823		int h = input.getHeight();
 824
 825		float scale = Math.max((float) size / h, (float) size / w);
 826
 827		float outWidth = scale * w;
 828		float outHeight = scale * h;
 829		float left = (size - outWidth) / 2;
 830		float top = (size - outHeight) / 2;
 831		RectF target = new RectF(left, top, left + outWidth, top + outHeight);
 832
 833		Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
 834		Canvas canvas = new Canvas(output);
 835		canvas.drawBitmap(input, null, target, createAntiAliasingPaint());
 836		if (!input.isRecycled()) {
 837			input.recycle();
 838		}
 839		return output;
 840	}
 841
 842	private int calcSampleSize(Uri image, int size) throws FileNotFoundException, SecurityException {
 843		BitmapFactory.Options options = new BitmapFactory.Options();
 844		options.inJustDecodeBounds = true;
 845		BitmapFactory.decodeStream(mXmppConnectionService.getContentResolver().openInputStream(image), null, options);
 846		return calcSampleSize(options, size);
 847	}
 848
 849	private static int calcSampleSize(File image, int size) {
 850		BitmapFactory.Options options = new BitmapFactory.Options();
 851		options.inJustDecodeBounds = true;
 852		BitmapFactory.decodeFile(image.getAbsolutePath(), options);
 853		return calcSampleSize(options, size);
 854	}
 855
 856	public static int calcSampleSize(BitmapFactory.Options options, int size) {
 857		int height = options.outHeight;
 858		int width = options.outWidth;
 859		int inSampleSize = 1;
 860
 861		if (height > size || width > size) {
 862			int halfHeight = height / 2;
 863			int halfWidth = width / 2;
 864
 865			while ((halfHeight / inSampleSize) > size
 866					&& (halfWidth / inSampleSize) > size) {
 867				inSampleSize *= 2;
 868			}
 869		}
 870		return inSampleSize;
 871	}
 872
 873	public void updateFileParams(Message message) {
 874		updateFileParams(message, null);
 875	}
 876
 877	public void updateFileParams(Message message, URL url) {
 878		DownloadableFile file = getFile(message);
 879		final String mime = file.getMimeType();
 880		boolean image = message.getType() == Message.TYPE_IMAGE || (mime != null && mime.startsWith("image/"));
 881		boolean video = mime != null && mime.startsWith("video/");
 882		boolean audio = mime != null && mime.startsWith("audio/");
 883		final StringBuilder body = new StringBuilder();
 884		if (url != null) {
 885			body.append(url.toString());
 886		}
 887		body.append('|').append(file.getSize());
 888		if (image || video) {
 889			try {
 890				Dimensions dimensions = image ? getImageDimensions(file) : getVideoDimensions(file);
 891				if (dimensions.valid()) {
 892					body.append('|').append(dimensions.width).append('|').append(dimensions.height);
 893				}
 894			} catch (NotAVideoFile notAVideoFile) {
 895				Log.d(Config.LOGTAG, "file with mime type " + file.getMimeType() + " was not a video file");
 896				//fall threw
 897			}
 898		} else if (audio) {
 899			body.append("|0|0|").append(getMediaRuntime(file));
 900		}
 901		message.setBody(body.toString());
 902	}
 903
 904	public int getMediaRuntime(Uri uri) {
 905		try {
 906			MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
 907			mediaMetadataRetriever.setDataSource(mXmppConnectionService, uri);
 908			return Integer.parseInt(mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
 909		} catch (RuntimeException e) {
 910			return 0;
 911		}
 912	}
 913
 914	private int getMediaRuntime(File file) {
 915		try {
 916			MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
 917			mediaMetadataRetriever.setDataSource(file.toString());
 918			return Integer.parseInt(mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
 919		} catch (RuntimeException e) {
 920			return 0;
 921		}
 922	}
 923
 924	private Dimensions getImageDimensions(File file) {
 925		BitmapFactory.Options options = new BitmapFactory.Options();
 926		options.inJustDecodeBounds = true;
 927		BitmapFactory.decodeFile(file.getAbsolutePath(), options);
 928		int rotation = getRotation(file);
 929		boolean rotated = rotation == 90 || rotation == 270;
 930		int imageHeight = rotated ? options.outWidth : options.outHeight;
 931		int imageWidth = rotated ? options.outHeight : options.outWidth;
 932		return new Dimensions(imageHeight, imageWidth);
 933	}
 934
 935	private Dimensions getVideoDimensions(File file) throws NotAVideoFile {
 936		MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
 937		try {
 938			metadataRetriever.setDataSource(file.getAbsolutePath());
 939		} catch (RuntimeException e) {
 940			throw new NotAVideoFile(e);
 941		}
 942		return getVideoDimensions(metadataRetriever);
 943	}
 944
 945	private static Dimensions getVideoDimensions(Context context, Uri uri) throws NotAVideoFile {
 946		MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
 947		try {
 948			mediaMetadataRetriever.setDataSource(context, uri);
 949		} catch (RuntimeException e) {
 950			throw new NotAVideoFile(e);
 951		}
 952		return getVideoDimensions(mediaMetadataRetriever);
 953	}
 954
 955	private static Dimensions getVideoDimensionsOfFrame(MediaMetadataRetriever mediaMetadataRetriever) {
 956		Bitmap bitmap = null;
 957		try {
 958			bitmap = mediaMetadataRetriever.getFrameAtTime();
 959			return new Dimensions(bitmap.getHeight(), bitmap.getWidth());
 960		} catch (Exception e) {
 961			return null;
 962		} finally {
 963			if (bitmap != null) {
 964				bitmap.recycle();;
 965			}
 966		}
 967	}
 968
 969	private static Dimensions getVideoDimensions(MediaMetadataRetriever metadataRetriever) throws NotAVideoFile {
 970		String hasVideo = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
 971		if (hasVideo == null) {
 972			throw new NotAVideoFile();
 973		}
 974		Dimensions dimensions = getVideoDimensionsOfFrame(metadataRetriever);
 975		if (dimensions != null) {
 976			return dimensions;
 977		}
 978		int rotation = extractRotationFromMediaRetriever(metadataRetriever);
 979		boolean rotated = rotation == 90 || rotation == 270;
 980		int height;
 981		try {
 982			String h = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
 983			height = Integer.parseInt(h);
 984		} catch (Exception e) {
 985			height = -1;
 986		}
 987		int width;
 988		try {
 989			String w = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
 990			width = Integer.parseInt(w);
 991		} catch (Exception e) {
 992			width = -1;
 993		}
 994		metadataRetriever.release();
 995		Log.d(Config.LOGTAG, "extracted video dims " + width + "x" + height);
 996		return rotated ? new Dimensions(width, height) : new Dimensions(height, width);
 997	}
 998
 999	private static int extractRotationFromMediaRetriever(MediaMetadataRetriever metadataRetriever) {
1000		String r = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
1001		try {
1002			return Integer.parseInt(r);
1003		} catch (Exception e) {
1004			return 0;
1005		}
1006	}
1007
1008	private static class Dimensions {
1009		public final int width;
1010		public final int height;
1011
1012		Dimensions(int height, int width) {
1013			this.width = width;
1014			this.height = height;
1015		}
1016
1017		public int getMin() {
1018			return Math.min(width, height);
1019		}
1020
1021		public boolean valid() {
1022			return width > 0 && height > 0;
1023		}
1024	}
1025
1026	private static class NotAVideoFile extends Exception {
1027		public NotAVideoFile(Throwable t) {
1028			super(t);
1029		}
1030
1031		public NotAVideoFile() {
1032			super();
1033		}
1034	}
1035
1036	public class FileCopyException extends Exception {
1037		private static final long serialVersionUID = -1010013599132881427L;
1038		private int resId;
1039
1040		public FileCopyException(int resId) {
1041			this.resId = resId;
1042		}
1043
1044		public int getResId() {
1045			return resId;
1046		}
1047	}
1048
1049	public Bitmap getAvatar(String avatar, int size) {
1050		if (avatar == null) {
1051			return null;
1052		}
1053		Bitmap bm = cropCenter(getAvatarUri(avatar), size, size);
1054		if (bm == null) {
1055			return null;
1056		}
1057		return bm;
1058	}
1059
1060	public boolean isFileAvailable(Message message) {
1061		return getFile(message).exists();
1062	}
1063
1064	public static void close(Closeable stream) {
1065		if (stream != null) {
1066			try {
1067				stream.close();
1068			} catch (IOException e) {
1069			}
1070		}
1071	}
1072
1073	public static void close(Socket socket) {
1074		if (socket != null) {
1075			try {
1076				socket.close();
1077			} catch (IOException e) {
1078			}
1079		}
1080	}
1081
1082
1083	public static boolean weOwnFile(Context context, Uri uri) {
1084		if (uri == null || !ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
1085			return false;
1086		} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
1087			return fileIsInFilesDir(context, uri);
1088		} else {
1089			return weOwnFileLollipop(uri);
1090		}
1091	}
1092
1093
1094	/**
1095	 * This is more than hacky but probably way better than doing nothing
1096	 * Further 'optimizations' might contain to get the parents of CacheDir and NoBackupDir
1097	 * and check against those as well
1098	 */
1099	private static boolean fileIsInFilesDir(Context context, Uri uri) {
1100		try {
1101			final String haystack = context.getFilesDir().getParentFile().getCanonicalPath();
1102			final String needle = new File(uri.getPath()).getCanonicalPath();
1103			return needle.startsWith(haystack);
1104		} catch (IOException e) {
1105			return false;
1106		}
1107	}
1108
1109	@TargetApi(Build.VERSION_CODES.LOLLIPOP)
1110	private static boolean weOwnFileLollipop(Uri uri) {
1111		try {
1112			File file = new File(uri.getPath());
1113			FileDescriptor fd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY).getFileDescriptor();
1114			StructStat st = Os.fstat(fd);
1115			return st.st_uid == android.os.Process.myUid();
1116		} catch (FileNotFoundException e) {
1117			return false;
1118		} catch (Exception e) {
1119			return true;
1120		}
1121	}
1122}