Skip to content

Commit d9b02a3

Browse files
committed
Fix crashes
1 parent e9ce5f0 commit d9b02a3

File tree

17 files changed

+133
-73
lines changed

17 files changed

+133
-73
lines changed

app/src/main/kotlin/com/w2sv/autocrop/activities/crop/CropActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import com.w2sv.autocrop.activities.main.MainActivity
1010
class CropActivity : ApplicationActivity() {
1111

1212
companion object {
13-
const val EXTRA_N_UNCROPPED_SCREENSHOTS = "com.w2sv.autocrop.extra.N_UNCROPPED_SCREENSHOTS"
13+
const val EXTRA_N_UNCROPPED_SCREENSHOTS = "com.w2sv.autocrop.EXTRA_N_UNCROPPED_SCREENSHOTS"
14+
const val EXTRA_N_NOT_OPENABLE_URIS = "com.w2sv.autocrop.EXTRA_N_NOT_OPENABLE_URIS"
1415
}
1516

1617
override fun getRootFragment(): Fragment =

app/src/main/kotlin/com/w2sv/autocrop/activities/crop/fragments/cropping/CropFragment.kt

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ class CropFragment
6262
)
6363

6464
private val screenshotUris: List<Uri> = savedStateHandle[MainActivity.EXTRA_SELECTED_IMAGE_URIS]!!
65-
66-
val nScreenshots: Int
67-
get() = screenshotUris.size
65+
val nScreenshots = screenshotUris.size
6866

6967
fun getNUncroppableImages(): Int =
7068
nScreenshots - cropBundles.size
7169

7270
val cropBundles = mutableListOf<CropBundle>()
71+
var nNotOpenableUris = 0
72+
7373
val liveProgress: LiveData<Int> = MutableLiveData(0)
7474

7575
suspend fun launchCropCoroutine(
@@ -100,22 +100,24 @@ class CropFragment
100100
subList(liveProgress.value!!, size)
101101
}
102102

103-
private fun getCropBundle(screenshotUri: Uri, contentResolver: ContentResolver): CropBundle? {
104-
val screenshotBitmap = contentResolver.loadBitmap(screenshotUri)
105-
106-
return screenshotBitmap.cropEdgesCandidates()?.let { candidates ->
107-
CropBundle.assemble(
108-
Screenshot(
109-
screenshotUri,
110-
screenshotBitmap.height,
111-
candidates,
112-
Screenshot.MediaStoreData.query(contentResolver, screenshotUri)
113-
),
114-
screenshotBitmap,
115-
candidates.maxHeightEdges()
116-
)
103+
private fun getCropBundle(screenshotUri: Uri, contentResolver: ContentResolver): CropBundle? =
104+
contentResolver.loadBitmap(screenshotUri)?.let { screenshotBitmap ->
105+
screenshotBitmap.cropEdgesCandidates()?.let { candidates ->
106+
CropBundle.assemble(
107+
Screenshot(
108+
screenshotUri,
109+
screenshotBitmap.height,
110+
candidates,
111+
Screenshot.MediaStoreData.query(contentResolver, screenshotUri)
112+
),
113+
screenshotBitmap,
114+
candidates.maxHeightEdges()
115+
)
116+
}
117117
}
118-
}
118+
?: null.also {
119+
nNotOpenableUris += 1
120+
}
119121
}
120122

121123
private val viewModel by viewModels<ViewModel>()
@@ -169,6 +171,10 @@ class CropFragment
169171
CropActivity.EXTRA_N_UNCROPPED_SCREENSHOTS,
170172
viewModel.getNUncroppableImages()
171173
)
174+
.putExtra(
175+
CropActivity.EXTRA_N_NOT_OPENABLE_URIS,
176+
viewModel.nNotOpenableUris
177+
)
172178
)
173179
Animatoo.animateSwipeLeft(requireActivity())
174180
}

app/src/main/kotlin/com/w2sv/autocrop/activities/cropexamination/CropExaminationActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class CropExaminationActivity : ApplicationActivity() {
4646

4747
override fun getRootFragment(): Fragment =
4848
CropPagerFragment.getInstance(
49-
intent.getInt(CropActivity.EXTRA_N_UNCROPPED_SCREENSHOTS)
49+
intent.getInt(CropActivity.EXTRA_N_UNCROPPED_SCREENSHOTS),
50+
intent.getInt(CropActivity.EXTRA_N_NOT_OPENABLE_URIS)
5051
)
5152

5253
@HiltViewModel

app/src/main/kotlin/com/w2sv/autocrop/activities/cropexamination/fragments/comparison/ComparisonFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ComparisonFragment
6060
) : androidx.lifecycle.ViewModel() {
6161

6262
val cropBundle: CropBundle = savedStateHandle[CropBundle.EXTRA]!!
63-
val screenshotBitmap: Bitmap = context.contentResolver.loadBitmap(cropBundle.screenshot.uri)
63+
val screenshotBitmap: Bitmap = context.contentResolver.loadBitmap(cropBundle.screenshot.uri)!!
6464

6565
val enterTransitionCompleted by AutoSwitch(false, switchOn = false)
6666

app/src/main/kotlin/com/w2sv/autocrop/activities/cropexamination/fragments/croppager/CropPagerFragment.kt

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import com.w2sv.autocrop.ui.crossFade
4343
import com.w2sv.autocrop.ui.scrollPeriodically
4444
import com.w2sv.autocrop.utils.extensions.snackyBuilder
4545
import com.w2sv.kotlinutils.delegates.Consumable
46-
import com.w2sv.kotlinutils.extensions.numericallyInflected
4746
import dagger.hilt.android.AndroidEntryPoint
4847
import dagger.hilt.android.lifecycle.HiltViewModel
4948
import dagger.hilt.android.qualifiers.ApplicationContext
@@ -62,10 +61,13 @@ class CropPagerFragment :
6261
CropPagerInstructionsDialog.OnDismissedListener {
6362

6463
companion object {
65-
fun getInstance(nUncroppedScreenshots: Int): CropPagerFragment =
64+
fun getInstance(nUncroppedScreenshots: Int, nNotOpenableUris: Int): CropPagerFragment =
6665
CropPagerFragment()
6766
.apply {
68-
arguments = bundleOf(CropActivity.EXTRA_N_UNCROPPED_SCREENSHOTS to nUncroppedScreenshots)
67+
arguments = bundleOf(
68+
CropActivity.EXTRA_N_UNCROPPED_SCREENSHOTS to nUncroppedScreenshots,
69+
CropActivity.EXTRA_N_NOT_OPENABLE_URIS to nNotOpenableUris
70+
)
6971
}
7072
}
7173

@@ -90,20 +92,51 @@ class CropPagerFragment :
9092
/**
9193
* Inherently serves as flag, with != null meaning snackbar is to be displayed and vice-versa
9294
*/
93-
val uncroppedScreenshotsSnackbarText by Consumable<SpannableStringBuilder>(
94-
savedStateHandle.get<Int>(CropActivity.EXTRA_N_UNCROPPED_SCREENSHOTS)!!.let { nUncroppedScreenshots ->
95-
if (nUncroppedScreenshots != 0)
96-
SpannableStringBuilder()
97-
.append("Couldn't find crop bounds for")
98-
.bold {
99-
color(context.getThemedColor(R.color.highlight)) {
100-
append(" $nUncroppedScreenshots")
95+
val uncroppedScreenshotsSnackbarText by Consumable(
96+
SpannableStringBuilder()
97+
.run {
98+
savedStateHandle.get<Int>(CropActivity.EXTRA_N_UNCROPPED_SCREENSHOTS)!!
99+
.let { nUncroppedScreenshots ->
100+
if (nUncroppedScreenshots != 0) {
101+
append("Couldn't find crop bounds for")
102+
bold {
103+
color(context.getThemedColor(R.color.highlight)) {
104+
append(" $nUncroppedScreenshots")
105+
}
106+
}
107+
append(context.resources.getQuantityString(R.plurals.image, nUncroppedScreenshots))
101108
}
102109
}
103-
.append(" image".numericallyInflected(nUncroppedScreenshots))
104-
else
105-
null
106-
}
110+
111+
savedStateHandle.get<Int>(CropActivity.EXTRA_N_NOT_OPENABLE_URIS)!!
112+
.let { nNotOpenableUris ->
113+
when {
114+
nNotOpenableUris == 0 -> Unit
115+
isEmpty() -> {
116+
append(
117+
"Couldn't open $nNotOpenableUris ${
118+
context.resources.getQuantityString(
119+
R.plurals.image,
120+
nNotOpenableUris
121+
)
122+
}"
123+
)
124+
}
125+
126+
else -> {
127+
append(
128+
"& couldn't open $nNotOpenableUris ${
129+
context.resources.getQuantityString(
130+
R.plurals.image,
131+
nNotOpenableUris
132+
)
133+
}"
134+
)
135+
}
136+
}
137+
}
138+
ifEmpty { null }
139+
}
107140
)
108141

109142
//$$$$$$$$$$$$$
@@ -217,7 +250,7 @@ class CropPagerFragment :
217250
override fun onManualCropResult(cropEdges: CropEdges) {
218251
viewModel.dataSet.liveElement.let {
219252
it.crop = Crop.fromScreenshot(
220-
requireContext().contentResolver.loadBitmap(it.screenshot.uri),
253+
requireContext().contentResolver.loadBitmap(it.screenshot.uri)!!,
221254
it.screenshot.mediaStoreData.diskUsage,
222255
cropEdges
223256
)

app/src/main/kotlin/com/w2sv/autocrop/activities/cropexamination/fragments/manualcrop/ManualCropFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ManualCropFragment
5353

5454
init {
5555
with(savedStateHandle.get<CropBundle>(CropBundle.EXTRA)!!) {
56-
bitmap = context.contentResolver.loadBitmap(screenshot.uri)
56+
bitmap = context.contentResolver.loadBitmap(screenshot.uri)!!
5757
initialCropEdges = crop.edges
5858
}
5959
}

app/src/main/kotlin/com/w2sv/autocrop/activities/main/fragments/flowfield/FlowFieldFragment.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import com.w2sv.autocrop.screenshotlistening.ScreenshotListener
3333
import com.w2sv.autocrop.ui.fadeIn
3434
import com.w2sv.autocrop.utils.documentUriPathIdentifier
3535
import com.w2sv.autocrop.utils.extensions.snackyBuilder
36-
import com.w2sv.kotlinutils.extensions.numericallyInflected
3736
import com.w2sv.permissionhandler.PermissionHandler
3837
import com.w2sv.permissionhandler.requestPermissions
3938
import dagger.hilt.android.AndroidEntryPoint
@@ -151,7 +150,14 @@ class FlowFieldFragment :
151150
snackyBuilder(
152151
SpannableStringBuilder()
153152
.apply {
154-
append("Saved ${it.nSavedCrops} ${"crop".numericallyInflected(it.nSavedCrops)} to ")
153+
append(
154+
"Saved ${it.nSavedCrops} ${
155+
resources.getQuantityString(
156+
R.plurals.crop,
157+
it.nSavedCrops
158+
)
159+
} to "
160+
)
155161
color(getThemedColor(R.color.success)) {
156162
append(it.saveDirName)
157163
}
@@ -162,7 +168,7 @@ class FlowFieldFragment :
162168
"corresponding"
163169
else
164170
it.nDeletedScreenshots
165-
} ${"screenshot".numericallyInflected(it.nDeletedScreenshots)}"
171+
} ${resources.getQuantityString(R.plurals.screenshot, it.nDeletedScreenshots)}"
166172
)
167173
}
168174
)

app/src/main/kotlin/com/w2sv/autocrop/cropbundle/io/extensions/ContentResolver.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ import android.graphics.BitmapFactory
66
import android.net.Uri
77
import android.provider.MediaStore
88
import slimber.log.i
9+
import java.io.FileNotFoundException
910

10-
fun ContentResolver.loadBitmap(uri: Uri): Bitmap =
11-
BitmapFactory.decodeStream(openInputStream(uri))
11+
fun ContentResolver.loadBitmap(uri: Uri): Bitmap? =
12+
try {
13+
BitmapFactory.decodeStream(openInputStream(uri))
14+
}
15+
catch (e: FileNotFoundException) {
16+
i(e)
17+
null
18+
}
1219

1320
/**
1421
* https://stackoverflow.com/questions/10716642/android-deleting-an-image?noredirect=1&lq=1

app/src/main/kotlin/com/w2sv/autocrop/screenshotlistening/ScreenshotListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class ScreenshotListener : BoundService(),
182182
*/
183183
private fun onNewScreenshotUri(uri: Uri): Boolean =
184184
try {
185-
val screenshotBitmap = contentResolver.loadBitmap(uri)
185+
val screenshotBitmap = contentResolver.loadBitmap(uri)!!
186186
screenshotBitmap.cropEdges()?.let { cropEdges ->
187187
val screenshotMediaStoreData = Screenshot.MediaStoreData.query(contentResolver, uri)
188188
val deleteRequestUri = getDeleteRequestUri(screenshotMediaStoreData.id)

app/src/main/kotlin/com/w2sv/autocrop/screenshotlistening/services/abstrct/LoggingService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ abstract class LoggingService : Service() {
1515
}
1616
}
1717

18-
fun emitOnReceiveLog(intent: Intent?){
19-
i{
18+
fun emitOnReceiveLog(intent: Intent?) {
19+
i {
2020
"onReceive; intent: $intent"
2121
}
2222
}

0 commit comments

Comments
 (0)