Skip to content

Commit

Permalink
Moved code responsible for dealing with bitmaps to androidshared
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Feb 28, 2023
1 parent 9695d0b commit 35d8f95
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 26 deletions.
4 changes: 4 additions & 0 deletions androidshared/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ dependencies {
implementation Dependencies.androidx_fragment_ktx
implementation Dependencies.androidx_preference_ktx
implementation Dependencies.timber
implementation Dependencies.androidx_exinterface

testImplementation project(':testshared')
testImplementation Dependencies.junit
Expand All @@ -70,5 +71,8 @@ dependencies {
testImplementation Dependencies.robolectric
testImplementation Dependencies.mockito_kotlin

androidTestImplementation Dependencies.androidx_test_ext_junit
androidTestImplementation Dependencies.junit

debugImplementation project(':fragmentstest')
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.odk.collect.android.instrumented.utilities
package org.odk.collect.androidshared.bitmap

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.exifinterface.media.ExifInterface
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.Test
import org.junit.runner.RunWith
import org.odk.collect.android.utilities.ImageCompressor
import org.odk.collect.android.utilities.ImageFileUtils
import java.io.File

@RunWith(AndroidJUnit4::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.odk.collect.android.instrumented.utilities
package org.odk.collect.androidshared.bitmap

import android.content.Context
import android.graphics.Bitmap
Expand All @@ -26,7 +26,6 @@ import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.odk.collect.android.utilities.ImageFileUtils
import timber.log.Timber
import java.io.File
import java.io.IOException
Expand All @@ -50,7 +49,10 @@ class ImageFileUtilsTest {
attributes[ExifInterface.TAG_ORIENTATION] = ExifInterface.ORIENTATION_ROTATE_90.toString()
saveTestBitmapToFile(sourceFile.absolutePath, attributes)
ImageFileUtils.copyImageAndApplyExifRotation(sourceFile, destinationFile)
val image = ImageFileUtils.getBitmap(destinationFile.absolutePath, BitmapFactory.Options())!!
val image = ImageFileUtils.getBitmap(
destinationFile.absolutePath,
BitmapFactory.Options()
)!!

assertEquals(2, image.width)
assertEquals(1, image.height)
Expand All @@ -64,7 +66,10 @@ class ImageFileUtilsTest {
attributes[ExifInterface.TAG_ORIENTATION] = ExifInterface.ORIENTATION_ROTATE_270.toString()
saveTestBitmapToFile(sourceFile.absolutePath, attributes)
ImageFileUtils.copyImageAndApplyExifRotation(sourceFile, destinationFile)
val image = ImageFileUtils.getBitmap(destinationFile.absolutePath, BitmapFactory.Options())!!
val image = ImageFileUtils.getBitmap(
destinationFile.absolutePath,
BitmapFactory.Options()
)!!

assertEquals(2, image.width)
assertEquals(1, image.height)
Expand All @@ -78,7 +83,10 @@ class ImageFileUtilsTest {
attributes[ExifInterface.TAG_ORIENTATION] = ExifInterface.ORIENTATION_ROTATE_180.toString()
saveTestBitmapToFile(sourceFile.absolutePath, attributes)
ImageFileUtils.copyImageAndApplyExifRotation(sourceFile, destinationFile)
val image = ImageFileUtils.getBitmap(destinationFile.absolutePath, BitmapFactory.Options())!!
val image = ImageFileUtils.getBitmap(
destinationFile.absolutePath,
BitmapFactory.Options()
)!!

assertEquals(1, image.width)
assertEquals(2, image.height)
Expand All @@ -92,7 +100,10 @@ class ImageFileUtilsTest {
attributes[ExifInterface.TAG_ORIENTATION] = ExifInterface.ORIENTATION_UNDEFINED.toString()
saveTestBitmapToFile(sourceFile.absolutePath, attributes)
ImageFileUtils.copyImageAndApplyExifRotation(sourceFile, destinationFile)
val image = ImageFileUtils.getBitmap(destinationFile.absolutePath, BitmapFactory.Options())!!
val image = ImageFileUtils.getBitmap(
destinationFile.absolutePath,
BitmapFactory.Options()
)!!

assertEquals(1, image.width)
assertEquals(2, image.height)
Expand All @@ -105,7 +116,10 @@ class ImageFileUtilsTest {
fun copyAndRotateImageNoExif() {
saveTestBitmapToFile(sourceFile.absolutePath, null)
ImageFileUtils.copyImageAndApplyExifRotation(sourceFile, destinationFile)
val image = ImageFileUtils.getBitmap(destinationFile.absolutePath, BitmapFactory.Options())!!
val image = ImageFileUtils.getBitmap(
destinationFile.absolutePath,
BitmapFactory.Options()
)!!

assertEquals(1, image.width)
assertEquals(2, image.height)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.odk.collect.android.utilities
package org.odk.collect.androidshared.bitmap

import android.graphics.Bitmap
import android.graphics.BitmapFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.odk.collect.android.utilities
package org.odk.collect.androidshared.bitmap

import android.graphics.Bitmap
import android.graphics.Bitmap.CompressFormat
Expand Down Expand Up @@ -150,7 +150,7 @@ object ImageFileUtils {
)
) {
// Source Image doesn't have any EXIF Rotations, so a normal file copy will suffice
FileUtils.copyFile(sourceFile, destFile)
sourceFile.copyTo(destFile, true)
} else {
val sourceImage = getBitmap(sourceFile.absolutePath, BitmapFactory.Options())
val orientation = sourceFileExif.getAttributeInt(
Expand Down
1 change: 0 additions & 1 deletion collect_app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ dependencies {
implementation Dependencies.androidx_work_runtime

implementation Dependencies.androidx_cardview
implementation Dependencies.androidx_exinterface
implementation Dependencies.androidx_multidex
implementation Dependencies.androidx_preference_ktx
implementation Dependencies.androidx_fragment_ktx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.json.JSONException
import org.odk.collect.android.storage.StoragePathProvider
import org.odk.collect.android.storage.StorageSubdirectory
import org.odk.collect.android.utilities.FileUtils
import org.odk.collect.android.utilities.ImageFileUtils.saveBitmapToFile
import org.odk.collect.androidshared.bitmap.ImageFileUtils
import org.odk.collect.qrcode.QRCodeDecoder
import org.odk.collect.qrcode.QRCodeEncoder
import timber.log.Timber
Expand Down Expand Up @@ -62,7 +62,7 @@ class CachingQRCodeGenerator(private val qrCodeEncoder: QRCodeEncoder) : QRCodeG
val bmp = qrCodeEncoder.encode(preferencesString)
Timber.i("QR Code generation took : %d ms", System.currentTimeMillis() - time)
Timber.i("Saving QR Code to disk... : %s", qRCodeFilepath)
saveBitmapToFile(bmp, qRCodeFilepath)
ImageFileUtils.saveBitmapToFile(bmp, qRCodeFilepath)
FileUtils.write(mdCacheFile, messageDigest)
Timber.i("Updated %s file contents", SETTINGS_MD5_FILE)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import org.odk.collect.android.R
import org.odk.collect.android.utilities.ImageFileUtils.getBitmap
import org.odk.collect.androidshared.bitmap.ImageFileUtils
import org.odk.collect.async.Scheduler
import org.odk.collect.settings.SettingsProvider
import org.odk.collect.settings.keys.ProjectKeys
Expand Down Expand Up @@ -50,7 +50,7 @@ class QRCodeViewModel(
qrCodeGenerator.generateQRCode(includedKeys, appConfigurationGenerator)
val options = BitmapFactory.Options()
options.inPreferredConfig = Bitmap.Config.ARGB_8888
val bitmap = getBitmap(filePath, options)
val bitmap = ImageFileUtils.getBitmap(filePath, options)
return@immediate Pair(filePath, bitmap)
} catch (ignored: Exception) {
// Ignored
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import org.odk.collect.android.utilities.AnimationUtils;
import org.odk.collect.android.storage.StoragePathProvider;
import org.odk.collect.android.utilities.DialogUtils;
import org.odk.collect.android.utilities.ImageFileUtils;
import org.odk.collect.androidshared.bitmap.ImageFileUtils;
import org.odk.collect.androidshared.ui.DialogFragmentUtils;
import org.odk.collect.strings.localization.LocalizedActivity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import org.odk.collect.android.storage.StoragePathProvider
import org.odk.collect.android.utilities.ImageFileUtils
import org.odk.collect.androidshared.bitmap.ImageFileUtils
import java.io.File

class DrawView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
import org.odk.collect.android.utilities.FileProvider;
import org.odk.collect.android.utilities.FormsDirDiskFormsSynchronizer;
import org.odk.collect.android.utilities.FormsRepositoryProvider;
import org.odk.collect.android.utilities.ImageCompressor;
import org.odk.collect.androidshared.bitmap.ImageCompressor;
import org.odk.collect.android.utilities.ImageCompressionController;
import org.odk.collect.android.utilities.InstancesRepositoryProvider;
import org.odk.collect.android.utilities.MediaUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.odk.collect.android.utilities
import android.content.Context
import org.odk.collect.android.R
import org.odk.collect.android.widgets.QuestionWidget
import org.odk.collect.androidshared.bitmap.ImageCompressor
import timber.log.Timber

class ImageCompressionController(private val imageCompressor: ImageCompressor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.odk.collect.android.externaldata.ExternalSelectChoice;
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.odk.collect.android.utilities.HtmlUtils;
import org.odk.collect.android.utilities.ImageFileUtils;
import org.odk.collect.androidshared.bitmap.ImageFileUtils;
import org.odk.collect.android.widgets.QuestionWidget;
import org.odk.collect.android.widgets.interfaces.SelectChoiceLoader;
import org.odk.collect.android.widgets.warnings.SpacesInUnderlyingValuesWarning;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.odk.collect.android.externaldata.ExternalSelectChoice;
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.odk.collect.android.utilities.HtmlUtils;
import org.odk.collect.android.utilities.ImageFileUtils;
import org.odk.collect.androidshared.bitmap.ImageFileUtils;
import org.odk.collect.android.widgets.QuestionWidget;
import org.odk.collect.android.widgets.interfaces.SelectChoiceLoader;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.odk.collect.android.externaldata.ExternalSelectChoice;
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.odk.collect.android.utilities.HtmlUtils;
import org.odk.collect.android.utilities.ImageFileUtils;
import org.odk.collect.androidshared.bitmap.ImageFileUtils;
import org.odk.collect.android.widgets.QuestionWidget;
import org.odk.collect.android.widgets.interfaces.MultiChoiceWidget;
import org.odk.collect.android.widgets.interfaces.SelectChoiceLoader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import org.odk.collect.android.formentry.questions.QuestionDetails;
import org.odk.collect.android.listeners.AdvanceToNextListener;
import org.odk.collect.android.utilities.HtmlUtils;
import org.odk.collect.android.utilities.ImageFileUtils;
import org.odk.collect.androidshared.bitmap.ImageFileUtils;
import org.odk.collect.android.utilities.SelectOneWidgetUtils;
import org.odk.collect.android.widgets.QuestionWidget;
import org.odk.collect.android.widgets.interfaces.MultiChoiceWidget;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoInteractions
import org.mockito.kotlin.whenever
import org.odk.collect.android.widgets.QuestionWidget
import org.odk.collect.androidshared.bitmap.ImageCompressor

@RunWith(AndroidJUnit4::class)
class ImageCompressionControllerTest {
Expand Down

0 comments on commit 35d8f95

Please sign in to comment.