Skip to content

Commit

Permalink
Fixed (again) notifications with attachments and added a test
Browse files Browse the repository at this point in the history
  • Loading branch information
federicoiosue committed Nov 11, 2019
1 parent 6e18b2f commit 263cde2
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (C) 2013-2019 Federico Iosue ([email protected])
*
* 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 <http://www.gnu.org/licenses/>.
*/

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);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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<Bitmap> 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());
Expand Down
6 changes: 6 additions & 0 deletions omniNotes/src/main/res/raw/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
-->
<changelog bulletedList="true">

<changelogversion
changeDate="Nov 11, 2019"
versionName="6.0.4">
<changelogtext>[u]Fix[/u] Notification on notes with attachments</changelogtext>
</changelogversion>

<changelogversion
changeDate="Nov 3, 2019"
versionName="6.0.3">
Expand Down

0 comments on commit 263cde2

Please sign in to comment.