diff --git a/imagepicker/build.gradle b/imagepicker/build.gradle index 1e554b74..a9fd313f 100644 --- a/imagepicker/build.gradle +++ b/imagepicker/build.gradle @@ -4,6 +4,8 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' +apply from: "../ktlint.gradle" + android { compileSdkVersion 28 diff --git a/imagepicker/src/androidTest/java/com/github/dhaval2404/imagepicker/ExampleInstrumentedTest.kt b/imagepicker/src/androidTest/java/com/github/dhaval2404/imagepicker/ExampleInstrumentedTest.kt index a0f0be36..c06a218b 100644 --- a/imagepicker/src/androidTest/java/com/github/dhaval2404/imagepicker/ExampleInstrumentedTest.kt +++ b/imagepicker/src/androidTest/java/com/github/dhaval2404/imagepicker/ExampleInstrumentedTest.kt @@ -17,5 +17,4 @@ class ExampleInstrumentedTest { assertEquals("com.github.dhaval2404.imagepicker.test", appContext.packageName) } - } \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePicker.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePicker.kt index 26c8c013..1cda0494 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePicker.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePicker.kt @@ -22,7 +22,7 @@ import java.io.File open class ImagePicker { companion object { - //Default Request Code to Pick Image + // Default Request Code to Pick Image const val REQUEST_CODE = 2404 const val RESULT_ERROR = 64 @@ -78,19 +78,18 @@ open class ImagePicker { */ fun getFile(data: Intent?): File? { val path = getFilePath(data) - if(path!=null){ + if (path != null) { return File(path) } return null } - } class Builder(private val activity: Activity) { private var fragment: Fragment? = null - //Image Provider + // Image Provider private var imageProvider = ImageProvider.BOTH /* @@ -128,7 +127,7 @@ open class ImagePicker { /** * Only Capture image using Camera. */ - //@Deprecated("Please use provider(ImageProvider.CAMERA) instead") + // @Deprecated("Please use provider(ImageProvider.CAMERA) instead") fun cameraOnly(): Builder { this.imageProvider = ImageProvider.CAMERA return this @@ -137,7 +136,7 @@ open class ImagePicker { /** * Only Pick image from gallery. */ - //@Deprecated("Please use provider(ImageProvider.GALLERY) instead") + // @Deprecated("Please use provider(ImageProvider.GALLERY) instead") fun galleryOnly(): Builder { this.imageProvider = ImageProvider.GALLERY return this @@ -195,7 +194,7 @@ open class ImagePicker { */ fun start(reqCode: Int) { if (imageProvider == ImageProvider.BOTH) { - //Pick Image Provider if not specified + // Pick Image Provider if not specified showImageProviderDialog(reqCode) } else { startActivity(reqCode) @@ -207,7 +206,7 @@ open class ImagePicker { */ fun start(completionHandler: ((resultCode: Int, data: Intent?) -> Unit)? = null) { if (imageProvider == ImageProvider.BOTH) { - //Pick Image Provider if not specified + // Pick Image Provider if not specified showImageProviderDialog(completionHandler) } else { startActivity(completionHandler) @@ -285,14 +284,13 @@ open class ImagePicker { completionHandler?.invoke(result.resultCode, result.data) } } - } catch (e : Exception){ + } catch (e: Exception) { if (e is ClassNotFoundException) { - Toast.makeText(if (fragment!= null) fragment!!.context else activity, "InlineActivityResult library not installed falling back to default method, please install " + + Toast.makeText(if (fragment != null) fragment!!.context else activity, "InlineActivityResult library not installed falling back to default method, please install " + "it from https://github.com/florent37/InlineActivityResult if you want to get inline activity results.", Toast.LENGTH_LONG).show() startActivity(REQUEST_CODE) } } - } /** @@ -307,9 +305,5 @@ open class ImagePicker { activity.startActivityForResult(intent, reqCode) } } - - } - - } diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePickerActivity.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePickerActivity.kt index 8adf8b85..731934b8 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePickerActivity.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/ImagePickerActivity.kt @@ -31,7 +31,6 @@ class ImagePickerActivity : FragmentActivity() { intent.putExtra(ImagePicker.EXTRA_ERROR, message) return intent } - } private var mGalleryProvider: GalleryProvider? = null @@ -57,7 +56,7 @@ class ImagePickerActivity : FragmentActivity() { val bundle = intent.extras!! val provider = bundle.getSerializable(ImagePicker.EXTRA_IMAGE_PROVIDER) as ImageProvider - //Create provider object and start process + // Create provider object and start process when (provider) { ImageProvider.GALLERY -> { mGalleryProvider = GalleryProvider(this) @@ -73,9 +72,11 @@ class ImagePickerActivity : FragmentActivity() { /** * Dispatch incoming result to the correct provider. */ - override fun onRequestPermissionsResult(requestCode: Int, - permissions: Array, - grantResults: IntArray) { + override fun onRequestPermissionsResult( + requestCode: Int, + permissions: Array, + grantResults: IntArray + ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) mCameraProvider?.onRequestPermissionsResult(requestCode) mGalleryProvider?.onRequestPermissionsResult(requestCode) @@ -123,15 +124,15 @@ class ImagePickerActivity : FragmentActivity() { mCropFile = file mCameraProvider?.let { - //Delete Camera file after crop. Else there will be two image for the same action. - //In case of Gallery Provider, we will get original image path, so we will not delete that. + // Delete Camera file after crop. Else there will be two image for the same action. + // In case of Gallery Provider, we will get original image path, so we will not delete that. mOriginalFile?.delete() mOriginalFile = null } - if(mCompressionProvider.isCompressionRequired(file)){ + if (mCompressionProvider.isCompressionRequired(file)) { mCompressionProvider.compress(file) - }else{ + } else { setResult(file) } } @@ -142,14 +143,14 @@ class ImagePickerActivity : FragmentActivity() { * @param file Compressed image file */ fun setCompressedImage(file: File) { - //This is the case when Crop is not enabled + // This is the case when Crop is not enabled mCameraProvider?.let { - //Delete Camera file after Compress. Else there will be two image for the same action. - //In case of Gallery Provider, we will get original image path, so we will not delete that. + // Delete Camera file after Compress. Else there will be two image for the same action. + // In case of Gallery Provider, we will get original image path, so we will not delete that. mOriginalFile?.delete() } - //If crop file is not null, Delete it after crop + // If crop file is not null, Delete it after crop mCropFile?.delete() mCropFile = null @@ -188,5 +189,4 @@ class ImagePickerActivity : FragmentActivity() { setResult(ImagePicker.RESULT_ERROR, intent) finish() } - } \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/listener/ResultListener.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/listener/ResultListener.kt index b2dcaf3d..44eb7972 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/listener/ResultListener.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/listener/ResultListener.kt @@ -11,5 +11,4 @@ package com.github.dhaval2404.imagepicker.listener internal interface ResultListener { fun onResult(t: T?) - } \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/BaseProvider.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/BaseProvider.kt index 9fc7dff7..4247ebdb 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/BaseProvider.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/BaseProvider.kt @@ -28,7 +28,7 @@ abstract class BaseProvider(protected val activity: ImagePickerActivity) : Conte * * @param errorRes Error Message */ - protected fun setError(errorRes:Int){ + protected fun setError(errorRes: Int) { setError(getString(errorRes)) } @@ -37,7 +37,7 @@ abstract class BaseProvider(protected val activity: ImagePickerActivity) : Conte * * @param messageRes String message resource */ - protected fun showToast(messageRes:Int){ + protected fun showToast(messageRes: Int) { Toast.makeText(this, messageRes, Toast.LENGTH_SHORT).show() } @@ -45,7 +45,7 @@ abstract class BaseProvider(protected val activity: ImagePickerActivity) : Conte * Call this method when task is cancel in between the operation. * E.g. user hit back-press */ - protected fun setResultCancel(){ + protected fun setResultCancel() { onFailure() activity.setResultCancel() } @@ -53,7 +53,6 @@ abstract class BaseProvider(protected val activity: ImagePickerActivity) : Conte /** * This method will be Call on Error, It can be used for clean up Tasks */ - protected open fun onFailure(){ + protected open fun onFailure() { } - } \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/CameraProvider.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/CameraProvider.kt index 2d6cc373..f01a762d 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/CameraProvider.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/CameraProvider.kt @@ -66,10 +66,10 @@ class CameraProvider(activity: ImagePickerActivity) : BaseProvider(activity) { * Create Temporary File object and Pass it to Camera Intent */ private fun startCameraIntent() { - //Create and get empty file to store capture image content + // Create and get empty file to store capture image content mCameraFile = FileUtil.getCameraFile() - //Check if file exists + // Check if file exists if (mCameraFile != null && mCameraFile!!.exists()) { val cameraIntent = IntentUtils.getCameraIntent(this, mCameraFile!!) activity.startActivityForResult(cameraIntent, CAMERA_INTENT_REQ_CODE) @@ -82,13 +82,13 @@ class CameraProvider(activity: ImagePickerActivity) : BaseProvider(activity) { * Handle Requested Permission Result */ fun onRequestPermissionsResult(requestCode: Int) { - if(requestCode==PERMISSION_INTENT_REQ_CODE) { - //Check again if permission is granted + if (requestCode == PERMISSION_INTENT_REQ_CODE) { + // Check again if permission is granted if (isPermissionGranted(this, REQUIRED_PERMISSIONS)) { - //Permission is granted, Start Camera Intent + // Permission is granted, Start Camera Intent startCameraIntent() } else { - //Exit with error message + // Exit with error message val error = getString(R.string.permission_camera_denied) setError(error) } @@ -98,8 +98,8 @@ class CameraProvider(activity: ImagePickerActivity) : BaseProvider(activity) { /** * Handle Camera Intent Activity Result * - * @param requestCode It must be {@link CameraProvider#CAMERA_INTENT_REQ_CODE} - * @param resultCode For success it should be {@link Activity#RESULT_OK} + * @param requestCode It must be {@link CameraProvider#CAMERA_INTENT_REQ_CODE} + * @param resultCode For success it should be {@link Activity#RESULT_OK} * @param data Result Intent */ fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { @@ -122,8 +122,7 @@ class CameraProvider(activity: ImagePickerActivity) : BaseProvider(activity) { /** * Delete Camera file is exists */ - override fun onFailure(){ + override fun onFailure() { mCameraFile?.delete() } - } \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/CompressionProvider.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/CompressionProvider.kt index 6c0054c0..b3e3f869 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/CompressionProvider.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/CompressionProvider.kt @@ -4,7 +4,6 @@ import android.annotation.SuppressLint import android.graphics.Bitmap import android.graphics.BitmapFactory import android.os.AsyncTask -import android.util.Log import com.github.dhaval2404.imagepicker.ImagePicker import com.github.dhaval2404.imagepicker.ImagePickerActivity import com.github.dhaval2404.imagepicker.util.FileUtil @@ -33,11 +32,11 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity init { val bundle = activity.intent.extras!! - //Get Max Width/Height parameter from Intent + // Get Max Width/Height parameter from Intent mMaxWidth = bundle.getInt(ImagePicker.EXTRA_MAX_WIDTH, 0) mMaxHeight = bundle.getInt(ImagePicker.EXTRA_MAX_HEIGHT, 0) - //Get Maximum Allowed file size + // Get Maximum Allowed file size mMaxFileSize = bundle.getLong(ImagePicker.EXTRA_IMAGE_MAX_SIZE, 0) } @@ -57,7 +56,7 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity fun isCompressionRequired(file: File): Boolean { val status = isCompressEnabled() && getSizeDiff(file) > 0L if (!status && mMaxWidth > 0 && mMaxHeight > 0) { - //Check image resolution + // Check image resolution val sizes = getImageSize(file) return sizes[0] > mMaxWidth || sizes[1] > mMaxHeight } @@ -85,17 +84,17 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity mOriginalFile = file object : AsyncTask() { override fun doInBackground(vararg params: File): File? { - //Perform operation in background + // Perform operation in background return startCompression(params[0]) } override fun onPostExecute(file: File?) { super.onPostExecute(file) if (file != null) { - //Post Result + // Post Result handleResult(file) } else { - //Post Error + // Post Error setError(com.github.dhaval2404.imagepicker.R.string.error_failed_to_compress_image) } } @@ -110,7 +109,7 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity var attempt = 0 var lastAttempt = 0 do { - //Delete file if exist, fill will be exist in second loop. + // Delete file if exist, fill will be exist in second loop. newFile?.delete() newFile = applyCompression(file, attempt) @@ -125,7 +124,7 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity if (mMaxFileSize > 0) { val diff = getSizeDiff(newFile) - //Log.i(TAG, "Size Diff:$diff") + // Log.i(TAG, "Size Diff:$diff") attempt += when { diff > 1024 * 1024 -> 3 diff > 500 * 1024 -> 2 @@ -148,7 +147,7 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity return null } - //Apply logic to get scaled bitmap resolution. + // Apply logic to get scaled bitmap resolution. val resolution = resList[attempt] var maxWidth = resolution[0] var maxHeight = resolution[1] @@ -159,9 +158,9 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity maxWidth = mMaxWidth } } - //Log.d(TAG, "maxWidth:$maxWidth, maxHeight:$maxHeight") + // Log.d(TAG, "maxWidth:$maxWidth, maxHeight:$maxHeight") - //Check file format + // Check file format var format = Bitmap.CompressFormat.JPEG var quality = 90 if (file.absolutePath.endsWith(".png")) { @@ -186,21 +185,21 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity */ private fun resolutionList(): List> { return listOf>( - arrayOf(2448, 3264), //8.0 Megapixel - arrayOf(2008, 3032), //6.0 Megapixel - arrayOf(1944, 2580), //5.0 Megapixel - arrayOf(1680, 2240), //4.0 Megapixel - arrayOf(1536, 2048), //3.0 Megapixel - arrayOf(1200, 1600), //2.0 Megapixel - arrayOf(1024, 1392), //1.3 Megapixel - arrayOf(960, 1280), //1.0 Megapixel - arrayOf(768, 1024), //0.7 Megapixel - arrayOf(600, 800), //0.4 Megapixel - arrayOf(480, 640), //0.3 Megapixel - arrayOf(240, 320), //0.15 Megapixel - arrayOf(120, 160), //0.08 Megapixel - arrayOf(60, 80), //0.04 Megapixel - arrayOf(30, 40) //0.02 Megapixel + arrayOf(2448, 3264), // 8.0 Megapixel + arrayOf(2008, 3032), // 6.0 Megapixel + arrayOf(1944, 2580), // 5.0 Megapixel + arrayOf(1680, 2240), // 4.0 Megapixel + arrayOf(1536, 2048), // 3.0 Megapixel + arrayOf(1200, 1600), // 2.0 Megapixel + arrayOf(1024, 1392), // 1.3 Megapixel + arrayOf(960, 1280), // 1.0 Megapixel + arrayOf(768, 1024), // 0.7 Megapixel + arrayOf(600, 800), // 0.4 Megapixel + arrayOf(480, 640), // 0.3 Megapixel + arrayOf(240, 320), // 0.15 Megapixel + arrayOf(120, 160), // 0.08 Megapixel + arrayOf(60, 80), // 0.04 Megapixel + arrayOf(30, 40) // 0.02 Megapixel ) } @@ -222,5 +221,4 @@ class CompressionProvider(activity: ImagePickerActivity) : BaseProvider(activity BitmapFactory.decodeFile(file.absolutePath, options) return arrayOf(options.outWidth, options.outHeight) } - } \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/CropProvider.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/CropProvider.kt index 014fcf4e..0113d4c8 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/CropProvider.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/CropProvider.kt @@ -24,7 +24,6 @@ class CropProvider(activity: ImagePickerActivity) : BaseProvider(activity) { companion object { private val TAG = CropProvider::class.java.simpleName - } private val mMaxWidth: Int @@ -37,11 +36,11 @@ class CropProvider(activity: ImagePickerActivity) : BaseProvider(activity) { init { val bundle = activity.intent.extras!! - //Get Max Width/Height parameter from Intent + // Get Max Width/Height parameter from Intent mMaxWidth = bundle.getInt(ImagePicker.EXTRA_MAX_WIDTH, 0) mMaxHeight = bundle.getInt(ImagePicker.EXTRA_MAX_HEIGHT, 0) - //Get Crop Aspect Ratio parameter from Intent + // Get Crop Aspect Ratio parameter from Intent mCropAspectX = bundle.getFloat(ImagePicker.EXTRA_CROP_X, 0f) mCropAspectY = bundle.getFloat(ImagePicker.EXTRA_CROP_Y, 0f) } @@ -106,8 +105,8 @@ class CropProvider(activity: ImagePickerActivity) : BaseProvider(activity) { /** * Handle Crop Intent Activity Result * - * @param requestCode It must be {@link UCrop#REQUEST_CROP} - * @param resultCode For success it should be {@link Activity#RESULT_OK} + * @param requestCode It must be {@link UCrop#REQUEST_CROP} + * @param resultCode For success it should be {@link Activity#RESULT_OK} * @param data Result Intent */ fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { @@ -125,7 +124,7 @@ class CropProvider(activity: ImagePickerActivity) : BaseProvider(activity) { * * @param file cropped file */ - private fun handleResult(file:File?) { + private fun handleResult(file: File?) { if (file != null) { activity.setCropImage(file) } else { @@ -139,5 +138,4 @@ class CropProvider(activity: ImagePickerActivity) : BaseProvider(activity) { override fun onFailure() { mCropImageFile?.delete() } - } \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/GalleryProvider.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/GalleryProvider.kt index b338f692..71ea2e62 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/GalleryProvider.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/provider/GalleryProvider.kt @@ -35,7 +35,7 @@ class GalleryProvider(activity: ImagePickerActivity) : BaseProvider(activity) { /** * Start Gallery Capture Intent */ - fun startIntent(){ + fun startIntent() { checkPermission() } @@ -64,13 +64,13 @@ class GalleryProvider(activity: ImagePickerActivity) : BaseProvider(activity) { * Handle Requested Permission Result */ fun onRequestPermissionsResult(requestCode: Int) { - if(requestCode==PERMISSION_INTENT_REQ_CODE) { - //Check again if permission is granted + if (requestCode == PERMISSION_INTENT_REQ_CODE) { + // Check again if permission is granted if (PermissionUtil.isPermissionGranted(this, REQUIRED_PERMISSIONS)) { - //Permission is granted, Start Camera Intent + // Permission is granted, Start Camera Intent startGalleryIntent() } else { - //Exit with error message + // Exit with error message setError(getString(R.string.permission_gallery_denied)) } } @@ -79,8 +79,8 @@ class GalleryProvider(activity: ImagePickerActivity) : BaseProvider(activity) { /** * Handle Camera Intent Activity Result * - * @param requestCode It must be {@link CameraProvider#GALLERY_INTENT_REQ_CODE} - * @param resultCode For success it should be {@link Activity#RESULT_OK} + * @param requestCode It must be {@link CameraProvider#GALLERY_INTENT_REQ_CODE} + * @param resultCode For success it should be {@link Activity#RESULT_OK} * @param data Result Intent */ fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { @@ -98,16 +98,15 @@ class GalleryProvider(activity: ImagePickerActivity) : BaseProvider(activity) { */ private fun handleResult(data: Intent?) { val uri = data?.data - if(uri!=null) { - val filePath:String? = FileUtils.getPath(activity, uri) - if(!filePath.isNullOrEmpty()) { + if (uri != null) { + val filePath: String? = FileUtils.getPath(activity, uri) + if (!filePath.isNullOrEmpty()) { activity.setImage(File(filePath)) - }else{ + } else { setError(R.string.error_failed_pick_gallery_image) } - }else{ + } else { setError(R.string.error_failed_pick_gallery_image) } } - } \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/DialogHelper.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/DialogHelper.kt index 17674eb1..007c5ce3 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/DialogHelper.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/DialogHelper.kt @@ -21,7 +21,7 @@ internal object DialogHelper { * Show Image Provide Picker Dialog. This will streamline the code to pick/capture image * */ - fun showChooseAppDialog(context: Context, listener: ResultListener){ + fun showChooseAppDialog(context: Context, listener: ResultListener) { val layoutInflater = LayoutInflater.from(context) val customView = layoutInflater.inflate(R.layout.dialog_choose_app, null) @@ -36,17 +36,16 @@ internal object DialogHelper { } .show() - //Handle Camera option click + // Handle Camera option click customView.lytCameraPick.setOnClickListener { listener.onResult(ImageProvider.CAMERA) dialog.dismiss() } - //Handle Gallery option click + // Handle Gallery option click customView.lytGalleryPick.setOnClickListener { listener.onResult(ImageProvider.GALLERY) dialog.dismiss() } } - } \ No newline at end of file diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/FileUtil.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/FileUtil.kt index 3cdb3e2f..fb40730a 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/FileUtil.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/FileUtil.kt @@ -5,7 +5,8 @@ import android.os.StatFs import java.io.File import java.io.IOException import java.text.SimpleDateFormat -import java.util.* +import java.util.Locale +import java.util.Date /** * File Utility Methods @@ -25,19 +26,19 @@ object FileUtil { // Create an image file name val imageFileName = "IMG_${getTimestamp()}.jpg" - //Create File Directory Object + // Create File Directory Object val storageDir = File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera" ) - //Create Directory If not exist + // Create Directory If not exist if (!storageDir.exists()) storageDir.mkdirs() - //Create File Object + // Create File Object val file = File(storageDir, imageFileName) - //Create empty file + // Create empty file file.createNewFile() return file @@ -68,5 +69,4 @@ object FileUtil { val blockSize = stat.blockSizeLong return availBlocks * blockSize } - } diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/ImageUtil.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/ImageUtil.kt index d9046758..36396776 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/ImageUtil.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/ImageUtil.kt @@ -15,7 +15,11 @@ */ package com.github.dhaval2404.imagepicker.util -import android.graphics.* +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.Matrix +import android.graphics.Canvas +import android.graphics.Paint import androidx.exifinterface.media.ExifInterface import java.io.File import java.io.FileOutputStream @@ -82,13 +86,12 @@ object ImageUtil { val maxRatio = reqWidth / reqHeight if (actualHeight > reqHeight || actualWidth > reqWidth) { - //If Height is greater + // If Height is greater if (imgRatio < maxRatio) { imgRatio = reqHeight / actualHeight actualWidth = (imgRatio * actualWidth).toInt() actualHeight = reqHeight.toInt() - - } //If Width is greater + } // If Width is greater else if (imgRatio > maxRatio) { imgRatio = reqWidth / actualWidth actualHeight = (imgRatio * actualHeight).toInt() @@ -111,7 +114,6 @@ object ImageUtil { bmp = BitmapFactory.decodeFile(imageFile.absolutePath, options) } catch (exception: OutOfMemoryError) { exception.printStackTrace() - } var scaledBitmap: Bitmap? = null @@ -157,8 +159,6 @@ object ImageUtil { } return scaledBitmap - - } private fun calculateInSampleSize( diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/IntentUtils.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/IntentUtils.kt index a0baa18c..7b7c606c 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/IntentUtils.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/IntentUtils.kt @@ -38,12 +38,12 @@ object IntentUtils { * @return Intent Gallery Document Intent */ private fun getGalleryDocumentIntent(): Intent { - //Show Document Intent + // Show Document Intent val intent = Intent(Intent.ACTION_OPEN_DOCUMENT) intent.addCategory(Intent.CATEGORY_OPENABLE) - //Apply filter to show image only in intent + // Apply filter to show image only in intent intent.type = "image/*" return intent @@ -53,10 +53,10 @@ object IntentUtils { * @return Intent Gallery Pick Intent */ private fun getGalleryPickIntent(): Intent { - //Show Gallery Intent, Will open google photos + // Show Gallery Intent, Will open google photos val intent = Intent(Intent.ACTION_PICK) - //Apply filter to show image only in intent + // Apply filter to show image only in intent intent.type = "image/*" return intent @@ -69,7 +69,7 @@ object IntentUtils { val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - //authority = com.github.dhaval2404.imagepicker.provider + // authority = com.github.dhaval2404.imagepicker.provider val authority = context.packageName + context.getString(R.string.image_picker_provider_authority_suffix) val photoURI = FileProvider.getUriForFile(context, authority, file) intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI) diff --git a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/PermissionUtil.kt b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/PermissionUtil.kt index 41f5e2ee..c510af03 100644 --- a/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/PermissionUtil.kt +++ b/imagepicker/src/main/kotlin/com/github/dhaval2404/imagepicker/util/PermissionUtil.kt @@ -32,10 +32,9 @@ object PermissionUtil { * * @return true if all specified permission is granted */ - fun isPermissionGranted(context: Context, permissions:Array):Boolean{ + fun isPermissionGranted(context: Context, permissions: Array): Boolean { return permissions.filter { hasPermission(context, it) }.size == permissions.size } - } \ No newline at end of file diff --git a/sample/build.gradle b/sample/build.gradle index f37565c0..f9acf986 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -4,6 +4,8 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' +apply from: "../ktlint.gradle" + android { compileSdkVersion 28 defaultConfig { diff --git a/sample/src/main/kotlin/com.github.dhaval2404.imagepicker/sample/MainActivity.kt b/sample/src/main/kotlin/com.github.dhaval2404.imagepicker/sample/MainActivity.kt index 998aa16c..f529f7c7 100644 --- a/sample/src/main/kotlin/com.github.dhaval2404.imagepicker/sample/MainActivity.kt +++ b/sample/src/main/kotlin/com.github.dhaval2404.imagepicker/sample/MainActivity.kt @@ -31,22 +31,22 @@ class MainActivity : AppCompatActivity() { fab_add_photo.setOnClickListener { ImagePicker.with(this) - .cropSquare() //Crop Square image(Optional) - .maxResultSize(620, 620) //Final image resolution will be less than 620 x 620(Optional) + .cropSquare() // Crop Square image(Optional) + .maxResultSize(620, 620) // Final image resolution will be less than 620 x 620(Optional) .start(PROFILE_IMAGE_REQ_CODE) } fab_add_gallery_photo.setOnClickListener { ImagePicker.with(this) - .galleryOnly() //User can only select image from Gallery(Optional) - .maxResultSize(1080, 1920) //Final image resolution will be less than 1080 x 1920(Optional) + .galleryOnly() // User can only select image from Gallery(Optional) + .maxResultSize(1080, 1920) // Final image resolution will be less than 1080 x 1920(Optional) .start(GALLERY_IMAGE_REQ_CODE) } fab_add_camera_photo.setOnClickListener { ImagePicker.with(this) - .provider(ImageProvider.CAMERA) //Default will be ImageProvider.BOTH - .compress(1024) //Final image size will be less than 1 MB(Optional) + .provider(ImageProvider.CAMERA) // Default will be ImageProvider.BOTH + .compress(1024) // Final image size will be less than 1 MB(Optional) .start(CAMERA_IMAGE_REQ_CODE) } @@ -56,7 +56,7 @@ class MainActivity : AppCompatActivity() { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (resultCode == Activity.RESULT_OK) { - //File object will not be null for RESULT_OK + // File object will not be null for RESULT_OK val file = ImagePicker.getFile(data) Log.e("TAG", "Path:${file?.absolutePath}") @@ -71,5 +71,4 @@ class MainActivity : AppCompatActivity() { Toast.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT).show() } } - }