diff --git a/omniNotes/src/androidTest/java/it/feio/android/omninotes/utils/BitmapHelperTest.java b/omniNotes/src/androidTest/java/it/feio/android/omninotes/utils/BitmapHelperTest.java new file mode 100644 index 0000000000..f8fcbbb18e --- /dev/null +++ b/omniNotes/src/androidTest/java/it/feio/android/omninotes/utils/BitmapHelperTest.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2013-2019 Federico Iosue (federico@iosue.it) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package it.feio.android.omninotes.utils; + +import static it.feio.android.omninotes.utils.ConstantsBase.MIME_TYPE_IMAGE; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; + +import android.graphics.Bitmap; +import android.net.Uri; +import android.os.Looper; +import android.support.test.internal.runner.junit4.statement.UiThreadStatement; +import android.support.test.runner.AndroidJUnit4; +import it.feio.android.omninotes.BaseAndroidTestCase; +import it.feio.android.omninotes.models.Attachment; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class BitmapHelperTest extends BaseAndroidTestCase { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + private Attachment attachment; + + @Before + public void setUp () throws IOException { + File bitmapFile = tempFolder.newFile("bitmapFile.bmp"); + + Bitmap bmp = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888); + FileOutputStream out = new FileOutputStream(bitmapFile); + bmp.compress(Bitmap.CompressFormat.PNG, 100, out); + + attachment = new Attachment(Uri.fromFile(bitmapFile), MIME_TYPE_IMAGE); + } + + @Test + public void getBitmapFromAttachment_backgroundThread () { + Bitmap bmp = BitmapHelper.getBitmapFromAttachment(testContext, attachment, 100, 100); + assertNotEquals("Thread MUST be a background one", Looper.getMainLooper(), Looper.myLooper()); + assertNotNull("Bitmap should not be null", bmp); + } + + @Test + public void getBitmapFromAttachment_mainThread () throws Throwable { + UiThreadStatement.runOnUiThread(() -> { + Bitmap bmp = BitmapHelper.getBitmapFromAttachment(testContext, attachment, 100, 100); + assertEquals("Thread MUST be a the main one", Looper.getMainLooper(), Looper.myLooper()); + assertNotNull("Bitmap should not be null", bmp); + }); + } + +} \ No newline at end of file diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/utils/BitmapHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/utils/BitmapHelper.java index 83da470c4b..2ff0909e18 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/utils/BitmapHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/utils/BitmapHelper.java @@ -27,12 +27,11 @@ import android.graphics.Bitmap; import android.media.ThumbnailUtils; import android.net.Uri; +import android.os.Looper; +import android.support.annotation.Nullable; import android.text.TextUtils; -import android.widget.RemoteViews; import com.bumptech.glide.Glide; -import com.bumptech.glide.RequestBuilder; import com.bumptech.glide.request.RequestOptions; -import com.bumptech.glide.request.target.AppWidgetTarget; import it.feio.android.omninotes.OmniNotes; import it.feio.android.omninotes.R; import it.feio.android.omninotes.helpers.AttachmentsHelper; @@ -54,16 +53,7 @@ public static Bitmap getBitmapFromAttachment (Context mContext, Attachment mAtta mAttachment.getUri().getPath(); if (AttachmentsHelper.typeOf(mAttachment, MIME_TYPE_VIDEO, MIME_TYPE_IMAGE, MIME_TYPE_SKETCH)) { - try { - bmp = Glide.with(OmniNotes.getAppContext()).asBitmap() - .apply(new RequestOptions() - .centerCrop() - .error(R.drawable.attachment_broken)) - .load(mAttachment.getUri()) - .submit(width, height).get(); - } catch (NullPointerException | InterruptedException | ExecutionException e) { - bmp = null; - } + bmp = getImageBitmap(mContext, mAttachment, width, height); } else if (MIME_TYPE_AUDIO.equals(mAttachment.getMime_type())) { bmp = ThumbnailUtils.extractThumbnail( @@ -85,30 +75,24 @@ public static Bitmap getBitmapFromAttachment (Context mContext, Attachment mAtta return bmp; } - public static void loadAttachmentIntoWidget (Attachment mAttachment, AppWidgetTarget awt) { - mAttachment.getUri().getPath(); - - RequestBuilder builder = Glide.with(OmniNotes.getAppContext()).asBitmap() - .apply(new RequestOptions() - .centerCrop() - .error(R.drawable.attachment_broken)); - - if (AttachmentsHelper.typeOf(mAttachment, MIME_TYPE_VIDEO, MIME_TYPE_IMAGE, MIME_TYPE_SKETCH)) { - builder = builder.load(mAttachment.getUri()); - } else if (MIME_TYPE_AUDIO.equals(mAttachment.getMime_type())) { - builder = builder.load(R.raw.play); - } else if (MIME_TYPE_FILES.equals(mAttachment.getMime_type())) { - if (MIME_TYPE_CONTACT_EXT.equals(FilenameUtils.getExtension(mAttachment.getName()))) { - builder = builder.load(R.raw.vcard); + @Nullable + private static Bitmap getImageBitmap (Context mContext, Attachment mAttachment, int width, int height) { + try { + if (Looper.getMainLooper() == Looper.myLooper()) { + return BitmapUtils.getThumbnail(mContext, mAttachment.getUri(), width, height); } else { - builder = builder.load(R.raw.files); + return Glide.with(OmniNotes.getAppContext()).asBitmap() + .apply(new RequestOptions() + .centerCrop() + .error(R.drawable.attachment_broken)) + .load(mAttachment.getUri()) + .submit(width, height).get(); } + } catch (NullPointerException | InterruptedException | ExecutionException e) { + return null; } - - builder.into(awt); } - public static Uri getThumbnailUri (Context mContext, Attachment mAttachment) { Uri uri = mAttachment.getUri(); String mimeType = StorageHelper.getMimeType(uri.toString()); diff --git a/omniNotes/src/main/res/raw/changelog.xml b/omniNotes/src/main/res/raw/changelog.xml index eef80b30f0..6430bd57bb 100644 --- a/omniNotes/src/main/res/raw/changelog.xml +++ b/omniNotes/src/main/res/raw/changelog.xml @@ -17,6 +17,12 @@ --> + + [u]Fix[/u] Notification on notes with attachments + +