diff --git a/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java index 1846875..b034e83 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/LoginActivity.java @@ -12,7 +12,7 @@ import android.widget.Toast; import org.communiquons.android.comunic.client.data.Account.Account; import org.communiquons.android.comunic.client.data.Account.AccountUtils; -import org.communiquons.android.comunic.client.data.Utilities; +import org.communiquons.android.comunic.client.data.utils.Utilities; import org.json.JSONException; import org.json.JSONObject; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java index 86f53b6..ffbd98e 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java @@ -3,11 +3,9 @@ package org.communiquons.android.comunic.client.api; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; -import android.util.Log; import org.communiquons.android.comunic.client.BuildConfig; import org.communiquons.android.comunic.client.data.Account.Account; -import org.communiquons.android.comunic.client.data.Utilities; import java.io.BufferedOutputStream; import java.io.BufferedReader; @@ -17,7 +15,6 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.io.Reader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/Account/Account.java b/app/src/main/java/org/communiquons/android/comunic/client/data/Account/Account.java index c9c559d..59f9156 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/Account/Account.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/Account/Account.java @@ -3,7 +3,7 @@ package org.communiquons.android.comunic.client.data.Account; import android.content.Context; import android.util.Log; -import org.communiquons.android.comunic.client.data.Utilities; +import org.communiquons.android.comunic.client.data.utils.Utilities; import org.json.JSONArray; import org.json.JSONException; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java index 7a1f3d0..4ed04f5 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/Account/AccountUtils.java @@ -5,8 +5,7 @@ import android.content.Context; import org.communiquons.android.comunic.client.api.APIRequestParameters; import org.communiquons.android.comunic.client.api.APIRequestTask; import org.communiquons.android.comunic.client.api.APIResponse; -import org.communiquons.android.comunic.client.data.Utilities; -import org.json.JSONException; +import org.communiquons.android.comunic.client.data.utils.Utilities; import org.json.JSONObject; /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageDownloadRunnable.java b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageDownloadRunnable.java index 7352447..bb4f38f 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageDownloadRunnable.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageDownloadRunnable.java @@ -3,8 +3,6 @@ package org.communiquons.android.comunic.client.data.ImageLoad; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import org.communiquons.android.comunic.client.data.Utilities; - import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadTask.java b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadTask.java index e49c61e..b7b42f6 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadTask.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadTask.java @@ -3,13 +3,11 @@ package org.communiquons.android.comunic.client.data.ImageLoad; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.graphics.drawable.BitmapDrawable; import android.os.AsyncTask; -import android.support.v4.content.ContextCompat; import android.util.Log; import android.widget.ImageView; -import org.communiquons.android.comunic.client.data.Utilities; +import org.communiquons.android.comunic.client.data.utils.Utilities; import java.io.File; import java.io.FileInputStream; @@ -19,7 +17,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; -import java.net.MalformedURLException; import java.net.URL; /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadUtils.java index 4e4ed83..40227a3 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadUtils.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/ImageLoad/ImageLoadUtils.java @@ -3,7 +3,7 @@ package org.communiquons.android.comunic.client.data.ImageLoad; import android.content.Context; import android.util.Log; -import org.communiquons.android.comunic.client.data.Utilities; +import org.communiquons.android.comunic.client.data.utils.Utilities; import java.io.File; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListAdapter.java index 8d63062..4b70e35 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListAdapter.java @@ -1,7 +1,6 @@ package org.communiquons.android.comunic.client.data.conversations; import android.content.Context; -import android.graphics.Typeface; import android.os.Build; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -12,12 +11,9 @@ import android.widget.ArrayAdapter; import android.widget.TextView; import org.communiquons.android.comunic.client.R; -import org.communiquons.android.comunic.client.data.Utilities; +import org.communiquons.android.comunic.client.data.utils.Utilities; import java.util.ArrayList; -import java.util.zip.Inflater; - -import static android.R.id.list; /** * Conversations adapter diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/Friend.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/Friend.java index 2bf1e67..24c7f47 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/Friend.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/Friend.java @@ -1,6 +1,6 @@ package org.communiquons.android.comunic.client.data.friendsList; -import org.communiquons.android.comunic.client.data.Utilities; +import org.communiquons.android.comunic.client.data.utils.Utilities; /** * Friend object diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java index c765a24..27f3b02 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsAdapter.java @@ -11,10 +11,9 @@ import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; -import org.communiquons.android.comunic.client.BuildConfig; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.ImageLoad.ImageLoadManager; -import org.communiquons.android.comunic.client.data.Utilities; +import org.communiquons.android.comunic.client.data.utils.Utilities; import org.communiquons.android.comunic.client.fragments.FriendsListFragment; import java.util.ArrayList; diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/utils/BitmapUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/BitmapUtils.java new file mode 100644 index 0000000..dbdd5de --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/BitmapUtils.java @@ -0,0 +1,61 @@ +package org.communiquons.android.comunic.client.data.utils; + +import android.graphics.Bitmap; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.Base64; + +import java.io.ByteArrayOutputStream; + +/** + * Bitmap utilities + * + * @author Pierre HUBERT + * Created by pierre on 12/25/17. + */ + +public class BitmapUtils { + + /** + * Convert a Bitmap into a base64-encoded string + * + * @param bitmap The bitmap to convert + * @return Encoded string + */ + public static String bitmapToBase64(@NonNull Bitmap bitmap){ + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream); + byte[] byteArray = byteArrayOutputStream.toByteArray(); + + return Base64.encodeToString(byteArray, Base64.DEFAULT); + } + + /** + * Create a bitmap based on another Bitmap by proportionally reducing its size + * + * Note : If the size of the Bitmap is smaller than the maximum width and height specified, + * The original bitmap is returned with no reduction + * + * @param bitmap The Bitmap to reduce + * @param maxW The maximum width of the new bitmap + * @param maxH The maximum height of the new bitmap + * @return The new bitmap + */ + public static Bitmap reduceBitmap(@NonNull Bitmap bitmap, int maxW, int maxH){ + + //Get the current dimensions + int currW = bitmap.getWidth(); + int currH = bitmap.getHeight(); + + //Check if the source bitmap is small enough + if(currW <= maxW && currH <= maxH) + return bitmap; + + //Compute new sizes for the bitmap + int newW = maxW; + int newH = (int) Math.floor((currH*maxW)/currW); + + //Scale and return bitmap + return Bitmap.createScaledBitmap(bitmap, newW, newH, true); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/Utilities.java similarity index 93% rename from app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java rename to app/src/main/java/org/communiquons/android/comunic/client/data/utils/Utilities.java index 0e09321..f31e426 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/utils/Utilities.java @@ -1,4 +1,4 @@ -package org.communiquons.android.comunic.client.data; +package org.communiquons.android.comunic.client.data.utils; import android.content.Context; import android.content.res.Resources; @@ -224,20 +224,6 @@ public class Utilities { } } - /** - * Convert a Bitmap into a base64-encoded string - * - * @param bitmap The bitmap to convert - * @return Encoded string - */ - public static String bitmapToBase64(@NonNull Bitmap bitmap){ - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream); - byte[] byteArray = byteArrayOutputStream.toByteArray(); - - return Base64.encodeToString(byteArray, Base64.DEFAULT); - } - /** * Prepare a string sent through the API to be shown in a TextView element diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java index cffbc8b..c53220f 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java @@ -26,11 +26,11 @@ import org.communiquons.android.comunic.client.data.Account.AccountUtils; import org.communiquons.android.comunic.client.data.DatabaseHelper; import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper; import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; -import org.communiquons.android.comunic.client.data.Utilities; import org.communiquons.android.comunic.client.data.conversations.ConversationMessage; import org.communiquons.android.comunic.client.data.conversations.ConversationMessageAdapter; import org.communiquons.android.comunic.client.data.conversations.ConversationMessagesHelper; import org.communiquons.android.comunic.client.data.conversations.ConversationRefreshRunnable; +import org.communiquons.android.comunic.client.data.utils.BitmapUtils; import java.io.FileNotFoundException; import java.io.InputStream; @@ -411,8 +411,13 @@ public class ConversationFragment extends Fragment @Override protected Boolean doInBackground(Void... params) { - String message_image = new_message_bitmap == null ? - null : Utilities.bitmapToBase64(new_message_bitmap); + String message_image = null; + + //Reduce Bitmap and convert it to a base64-encoded string + if(new_message_bitmap != null) + message_image = BitmapUtils.bitmapToBase64( + BitmapUtils.reduceBitmap(new_message_bitmap, 1199, 1199)); + return convMessHelper.sendMessage(conversation_id, message_content, message_image); }