Skip to content

Commit 23e1f01

Browse files
fix/handle-shared-files-and-error-handling (commons-app#6141)
1 parent ef032b0 commit 23e1f01

File tree

1 file changed

+56
-8
lines changed

1 file changed

+56
-8
lines changed

app/src/main/java/fr/free/nrw/commons/upload/UploadActivity.kt

+56-8
Original file line numberDiff line numberDiff line change
@@ -684,17 +684,64 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
684684

685685
private fun receiveInternalSharedItems() {
686686
val intent = intent
687+
Timber.d("Intent has EXTRA_FILES: ${EXTRA_FILES}")
688+
uploadableFiles = try {
689+
// Check if intent has the extra before trying to read it
690+
if (!intent.hasExtra(EXTRA_FILES)) {
691+
Timber.w("No EXTRA_FILES found in intent")
692+
mutableListOf()
693+
} else {
694+
// Try to get the files as Parcelable array
695+
val files = if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
696+
intent.getParcelableArrayListExtra(EXTRA_FILES, UploadableFile::class.java)
697+
} else {
698+
@Suppress("DEPRECATION")
699+
intent.getParcelableArrayListExtra<UploadableFile>(EXTRA_FILES)
700+
}
701+
702+
// Convert to mutable list or return empty list if null
703+
files?.toMutableList() ?: run {
704+
Timber.w("Files array was null")
705+
mutableListOf()
706+
}
707+
}
708+
} catch (e: Exception) {
709+
Timber.e(e, "Error reading files from intent")
710+
mutableListOf()
711+
}
712+
713+
// Log the result for debugging
714+
isMultipleFilesSelected = uploadableFiles.size > 1
715+
Timber.i("Received files count: ${uploadableFiles.size}")
716+
uploadableFiles.forEachIndexed { index, file ->
717+
Timber.d("File $index path: ${file.getFilePath()}")
718+
}
687719

688-
Timber.d("Received intent %s with action %s", intent.toString(), intent.action)
720+
// Handle other extras with null safety
721+
place = try {
722+
if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
723+
intent.getParcelableExtra(PLACE_OBJECT, Place::class.java)
724+
} else {
725+
@Suppress("DEPRECATION")
726+
intent.getParcelableExtra(PLACE_OBJECT)
727+
}
728+
} catch (e: Exception) {
729+
Timber.e(e, "Error reading place")
730+
null
731+
}
689732

690-
uploadableFiles = mutableListOf<UploadableFile>().apply {
691-
addAll(intent.getParcelableArrayListExtra(EXTRA_FILES) ?: emptyList())
733+
prevLocation = try {
734+
if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
735+
intent.getParcelableExtra(LOCATION_BEFORE_IMAGE_CAPTURE, LatLng::class.java)
736+
} else {
737+
@Suppress("DEPRECATION")
738+
intent.getParcelableExtra(LOCATION_BEFORE_IMAGE_CAPTURE)
739+
}
740+
} catch (e: Exception) {
741+
Timber.e(e, "Error reading location")
742+
null
692743
}
693-
isMultipleFilesSelected = uploadableFiles!!.size > 1
694-
Timber.i("Received multiple upload %s", uploadableFiles!!.size)
695744

696-
place = intent.getParcelableExtra<Place>(PLACE_OBJECT)
697-
prevLocation = intent.getParcelableExtra(LOCATION_BEFORE_IMAGE_CAPTURE)
698745
isInAppCameraUpload = intent.getBooleanExtra(IN_APP_CAMERA_UPLOAD, false)
699746
resetDirectPrefs()
700747
}
@@ -803,6 +850,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
803850
/**
804851
* Overrides the back button to make sure the user is prepared to lose their progress
805852
*/
853+
@SuppressLint("MissingSuperCall")
806854
override fun onBackPressed() {
807855
showAlertDialog(
808856
this,
@@ -920,7 +968,7 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
920968

921969
companion object {
922970
private var uploadIsOfAPlace = false
923-
const val EXTRA_FILES: String = "commons_image_exta"
971+
const val EXTRA_FILES: String = "commons_image_extra"
924972
const val LOCATION_BEFORE_IMAGE_CAPTURE: String = "user_location_before_image_capture"
925973
const val IN_APP_CAMERA_UPLOAD: String = "in_app_camera_upload"
926974

0 commit comments

Comments
 (0)