Unverified Commit c448359d authored by Lucio Maciel's avatar Lucio Maciel Committed by GitHub

Merge pull request #1384 from RocketChat/feature/compress-image

[NEW] Compress image
parents 18e06ae0 590aedde
...@@ -5,26 +5,32 @@ import android.net.Uri ...@@ -5,26 +5,32 @@ import android.net.Uri
import chat.rocket.android.util.extensions.* import chat.rocket.android.util.extensions.*
import javax.inject.Inject import javax.inject.Inject
class UriInteractor @Inject constructor(private val context: Context) { class UriInteractor @Inject constructor(private val context: Context) {
/** /**
* Gets the file name from an [Uri]. * Returns the file name from the [Uri].
*/ */
fun getFileName(uri: Uri): String? = uri.getFileName(context) fun getFileName(uri: Uri): String? = uri.getFileName(context)
/** /**
* Gets the MimeType of an [Uri] * Returns the MimeType from the [Uri].
*/ */
fun getMimeType(uri: Uri): String = uri.getMimeType(context) fun getMimeType(uri: Uri): String = uri.getMimeType(context)
/** /**
* Gets the real path of an [Uri] * Returns the file size from the [Uri].
*/ */
fun getRealPath(uri: Uri): String? = uri.getRealPathFromURI(context)
fun getFileSize(uri: Uri) = uri.getFileSize(context) fun getFileSize(uri: Uri) = uri.getFileSize(context)
/**
* Returns the InputStream from the [Uri].
*/
fun getInputStream(uri: Uri) = uri.getInputStream(context) fun getInputStream(uri: Uri) = uri.getInputStream(context)
/**
* Returns the Bitmap from the [Uri].
*
* Note: It should be an image.
*/
fun getBitmap(uri: Uri) = uri.getBitmpap(context)
} }
\ No newline at end of file
...@@ -8,11 +8,11 @@ import android.content.Context ...@@ -8,11 +8,11 @@ import android.content.Context
import android.os.Build import android.os.Build
import android.os.VibrationEffect import android.os.VibrationEffect
import android.os.Vibrator import android.os.Vibrator
import androidx.fragment.app.Fragment
import android.view.View import android.view.View
import android.view.ViewAnimationUtils import android.view.ViewAnimationUtils
import android.view.animation.AccelerateInterpolator import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator import android.view.animation.DecelerateInterpolator
import androidx.fragment.app.Fragment
fun View.rotateBy(value: Float, duration: Long = 100) { fun View.rotateBy(value: Float, duration: Long = 100) {
animate() animate()
...@@ -31,12 +31,12 @@ fun View.fadeIn(startValue: Float = 0f, finishValue: Float = 1f, duration: Long ...@@ -31,12 +31,12 @@ fun View.fadeIn(startValue: Float = 0f, finishValue: Float = 1f, duration: Long
.alpha(startValue) .alpha(startValue)
.setDuration(duration / 2) .setDuration(duration / 2)
.setInterpolator(DecelerateInterpolator()) .setInterpolator(DecelerateInterpolator())
.withEndAction({ .withEndAction {
animate() animate()
.alpha(finishValue) .alpha(finishValue)
.setDuration(duration / 2) .setDuration(duration / 2)
.setInterpolator(AccelerateInterpolator()).start() .setInterpolator(AccelerateInterpolator()).start()
}).start() }.start()
setVisible(true) setVisible(true)
} }
...@@ -51,18 +51,25 @@ fun View.fadeOut(startValue: Float = 1f, finishValue: Float = 0f, duration: Long ...@@ -51,18 +51,25 @@ fun View.fadeOut(startValue: Float = 1f, finishValue: Float = 0f, duration: Long
.alpha(startValue) .alpha(startValue)
.setDuration(duration) .setDuration(duration)
.setInterpolator(DecelerateInterpolator()) .setInterpolator(DecelerateInterpolator())
.withEndAction({ .withEndAction {
animate() animate()
.alpha(finishValue) .alpha(finishValue)
.setDuration(duration) .setDuration(duration)
.setInterpolator(AccelerateInterpolator()).start() .setInterpolator(AccelerateInterpolator()).start()
}).start() }.start()
setVisible(false) setVisible(false)
} }
fun View.circularRevealOrUnreveal(centerX: Int, centerY: Int, startRadius: Float, endRadius: Float, duration: Long = 200) { fun View.circularRevealOrUnreveal(
val anim = ViewAnimationUtils.createCircularReveal(this, centerX, centerY, startRadius, endRadius) centerX: Int,
centerY: Int,
startRadius: Float,
endRadius: Float,
duration: Long = 200
) {
val anim =
ViewAnimationUtils.createCircularReveal(this, centerX, centerY, startRadius, endRadius)
anim.duration = duration anim.duration = duration
if (startRadius < endRadius) { if (startRadius < endRadius) {
...@@ -74,7 +81,7 @@ fun View.circularRevealOrUnreveal(centerX: Int, centerY: Int, startRadius: Float ...@@ -74,7 +81,7 @@ fun View.circularRevealOrUnreveal(centerX: Int, centerY: Int, startRadius: Float
anim.start() anim.start()
} }
fun View.shake(x: Float = 2F, num: Int = 0){ fun View.shake(x: Float = 2F, num: Int = 0) {
if (num == 6) { if (num == 6) {
this.translationX = 0.toFloat() this.translationX = 0.toFloat()
return return
...@@ -104,5 +111,4 @@ fun Fragment.vibrateSmartPhone() { ...@@ -104,5 +111,4 @@ fun Fragment.vibrateSmartPhone() {
} else { } else {
vibrator.vibrate(200) vibrator.vibrate(200)
} }
} }
\ No newline at end of file
package chat.rocket.android.util.extensions
import android.graphics.Bitmap
fun Bitmap.getCompressFormat(mimeType: String): Bitmap.CompressFormat {
return when {
mimeType.contains("jpeg") -> Bitmap.CompressFormat.JPEG
mimeType.contains("webp") -> Bitmap.CompressFormat.WEBP
else -> Bitmap.CompressFormat.PNG
}
}
\ No newline at end of file
...@@ -2,17 +2,15 @@ package chat.rocket.android.util.extensions ...@@ -2,17 +2,15 @@ package chat.rocket.android.util.extensions
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.Toast import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import chat.rocket.android.R import chat.rocket.android.R
// TODO: Remove. Use KTX instead. // TODO: Remove. Use KTX instead.
...@@ -88,13 +86,4 @@ fun Fragment.showToast(@StringRes resource: Int, duration: Int = Toast.LENGTH_SH ...@@ -88,13 +86,4 @@ fun Fragment.showToast(@StringRes resource: Int, duration: Int = Toast.LENGTH_SH
showToast(getString(resource), duration) showToast(getString(resource), duration)
fun Fragment.showToast(message: String, duration: Int = Toast.LENGTH_SHORT) = fun Fragment.showToast(message: String, duration: Int = Toast.LENGTH_SHORT) =
activity?.showToast(message, duration) activity?.showToast(message, duration)
\ No newline at end of file
fun RecyclerView.isAtBottom(): Boolean {
val manager: RecyclerView.LayoutManager? = layoutManager
if (manager is LinearLayoutManager) {
return manager.findFirstVisibleItemPosition() == 0
}
return false // or true??? we can't determine the first visible item.
}
\ No newline at end of file
...@@ -3,6 +3,7 @@ package chat.rocket.android.util.extensions ...@@ -3,6 +3,7 @@ package chat.rocket.android.util.extensions
import android.annotation.TargetApi import android.annotation.TargetApi
import android.content.ContentResolver import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.graphics.Bitmap
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.provider.DocumentsContract import android.provider.DocumentsContract
...@@ -57,25 +58,17 @@ fun Uri.getMimeType(context: Context): String { ...@@ -57,25 +58,17 @@ fun Uri.getMimeType(context: Context): String {
} }
} }
fun Uri.getRealPathFromURI(context: Context): String? {
val cursor = context.contentResolver.query(this, arrayOf(MediaStore.Images.Media.DATA), null, null, null)
cursor.use { cursor ->
val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
cursor.moveToFirst()
return cursor.getString(columnIndex)
}
}
@TargetApi(Build.VERSION_CODES.N) @TargetApi(Build.VERSION_CODES.N)
fun Uri.isVirtualFile(context: Context): Boolean { fun Uri.isVirtualFile(context: Context): Boolean {
if (!DocumentsContract.isDocumentUri(context, this)) { if (!DocumentsContract.isDocumentUri(context, this)) {
return false return false
} }
val cursor = context.contentResolver.query(this, val cursor = context.contentResolver.query(
arrayOf(DocumentsContract.Document.COLUMN_FLAGS), this,
null, null, null) arrayOf(DocumentsContract.Document.COLUMN_FLAGS),
null, null, null
)
var flags = 0 var flags = 0
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
...@@ -97,8 +90,8 @@ fun Uri.getInputStreamForVirtualFile(context: Context, mimeTypeFilter: String): ...@@ -97,8 +90,8 @@ fun Uri.getInputStreamForVirtualFile(context: Context, mimeTypeFilter: String):
throw FileNotFoundException() throw FileNotFoundException()
} }
return resolver.openTypedAssetFileDescriptor(this, openableMimeTypes[0], return resolver.openTypedAssetFileDescriptor(this, openableMimeTypes[0], null)
null)?.createInputStream() ?.createInputStream()
} }
fun Uri.getInputStream(context: Context): InputStream? { fun Uri.getInputStream(context: Context): InputStream? {
...@@ -107,4 +100,8 @@ fun Uri.getInputStream(context: Context): InputStream? { ...@@ -107,4 +100,8 @@ fun Uri.getInputStream(context: Context): InputStream? {
} }
return context.contentResolver.openInputStream(this) return context.contentResolver.openInputStream(this)
} }
\ No newline at end of file
fun Uri.getBitmpap(context: Context): Bitmap? {
return MediaStore.Images.Media.getBitmap(context.contentResolver, this)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment