Skip to content

Commit 2c8c441

Browse files
pshnicolas-raoul
andauthored
Convert upload to kotlin (part 1) (commons-app#6024)
* Convert upload dagger module to kotlin * Code cleanup and convert the upload contract to kotlin * Code cleanup and convert CategoriesContract to kotlin * Code cleanup and convert MediaLicenseContract to kotlin * Code cleanup and convert UploadMediaDetailsContract to kotlin * Code cleanup, fixed nullability and converted DepictsContract to kotlin * Removed unused class * Convert FileMetadataUtils to kotlin * Convert EXIFReader to kotlin * Convert FileUtils to kotlin * Convert FileUtilsWrapper to kotlin * Convert ImageProcessingService to kotlin * Convert PageContentsCreator to kotlin * Convert PendingUploadsPresenter and contract to Kotlin with some code-cleanup * Convert ReadFBMD to kotlin * Convert SimilarImageInterface to kotlin * Removed unused classes * Fix merge/rebase issue --------- Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
1 parent cb00760 commit 2c8c441

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1595
-1835
lines changed

app/src/main/java/fr/free/nrw/commons/di/CommonsApplicationModule.kt

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import fr.free.nrw.commons.settings.Prefs
2929
import fr.free.nrw.commons.upload.UploadController
3030
import fr.free.nrw.commons.upload.depicts.DepictsDao
3131
import fr.free.nrw.commons.utils.ConfigUtils.isBetaFlavour
32+
import fr.free.nrw.commons.utils.TimeProvider
3233
import fr.free.nrw.commons.wikidata.WikidataEditListener
3334
import fr.free.nrw.commons.wikidata.WikidataEditListenerImpl
3435
import io.reactivex.Scheduler
@@ -224,6 +225,11 @@ open class CommonsApplicationModule(private val applicationContext: Context) {
224225
fun providesContentResolver(context: Context): ContentResolver =
225226
context.contentResolver
226227

228+
@Provides
229+
fun provideTimeProvider(): TimeProvider {
230+
return TimeProvider(System::currentTimeMillis)
231+
}
232+
227233
companion object {
228234
const val IO_THREAD: String = "io_thread"
229235
const val MAIN_THREAD: String = "main_thread"

app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,9 @@ public boolean onZoom(ZoomEvent event) {
528528
final Bundle bundle = new Bundle();
529529
try {
530530
bundle.putString("query",
531-
FileUtils.readFromResource("/queries/radius_query_for_upload_wizard.rq"));
531+
FileUtils.INSTANCE.readFromResource(
532+
"/queries/radius_query_for_upload_wizard.rq")
533+
);
532534
} catch (IOException e) {
533535
Timber.e(e);
534536
}

app/src/main/java/fr/free/nrw/commons/upload/EXIFReader.java

-36
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package fr.free.nrw.commons.upload
2+
3+
import androidx.exifinterface.media.ExifInterface
4+
import androidx.exifinterface.media.ExifInterface.TAG_DATETIME
5+
import androidx.exifinterface.media.ExifInterface.TAG_MAKE
6+
import fr.free.nrw.commons.utils.ImageUtils.FILE_NO_EXIF
7+
import fr.free.nrw.commons.utils.ImageUtils.IMAGE_OK
8+
import io.reactivex.Single
9+
import javax.inject.Inject
10+
import javax.inject.Singleton
11+
12+
/**
13+
* We try to minimize uploads from the Commons app that might be copyright violations.
14+
* If an image does not have any Exif metadata, then it was likely downloaded from the internet,
15+
* and is probably not an original work by the user. We detect these kinds of images by looking
16+
* for the presence of some basic Exif metadata.
17+
*/
18+
@Singleton
19+
class EXIFReader @Inject constructor() {
20+
fun processMetadata(path: String): Single<Int> = Single.just(
21+
try {
22+
if (ExifInterface(path).hasMakeOrDate) IMAGE_OK else FILE_NO_EXIF
23+
} catch (e: Exception) {
24+
FILE_NO_EXIF
25+
}
26+
)
27+
28+
private val ExifInterface.hasMakeOrDate get() =
29+
getAttribute(TAG_MAKE) != null || getAttribute(TAG_DATETIME) != null
30+
}
31+

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

+35-42
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
1010
import fr.free.nrw.commons.R
1111
import fr.free.nrw.commons.auth.SessionManager
1212
import fr.free.nrw.commons.contributions.Contribution
13+
import fr.free.nrw.commons.contributions.Contribution.Companion.STATE_FAILED
1314
import fr.free.nrw.commons.databinding.FragmentFailedUploadsBinding
1415
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment
1516
import fr.free.nrw.commons.media.MediaClient
@@ -43,7 +44,7 @@ class FailedUploadsFragment :
4344

4445
private lateinit var adapter: FailedUploadsAdapter
4546

46-
var contributionsList = ArrayList<Contribution>()
47+
var contributionsList = mutableListOf<Contribution>()
4748

4849
private lateinit var uploadProgressActivity: UploadProgressActivity
4950

@@ -71,7 +72,7 @@ class FailedUploadsFragment :
7172
inflater: LayoutInflater,
7273
container: ViewGroup?,
7374
savedInstanceState: Bundle?,
74-
): View? {
75+
): View {
7576
binding = FragmentFailedUploadsBinding.inflate(layoutInflater)
7677
pendingUploadsPresenter.onAttachView(this)
7778
initAdapter()
@@ -99,9 +100,9 @@ class FailedUploadsFragment :
99100
pendingUploadsPresenter.getFailedContributions()
100101
pendingUploadsPresenter.failedContributionList.observe(
101102
viewLifecycleOwner,
102-
) { list: PagedList<Contribution?> ->
103+
) { list: PagedList<Contribution> ->
103104
adapter.submitList(list)
104-
contributionsList = ArrayList()
105+
contributionsList = mutableListOf()
105106
list.forEach {
106107
if (it != null) {
107108
contributionsList.add(it)
@@ -124,26 +125,22 @@ class FailedUploadsFragment :
124125
* Restarts all the failed uploads.
125126
*/
126127
fun restartUploads() {
127-
if (contributionsList != null) {
128-
pendingUploadsPresenter.restartUploads(
129-
contributionsList,
130-
0,
131-
this.requireContext().applicationContext,
132-
)
133-
}
128+
pendingUploadsPresenter.restartUploads(
129+
contributionsList,
130+
0,
131+
requireContext().applicationContext,
132+
)
134133
}
135134

136135
/**
137136
* Restarts a specific upload.
138137
*/
139138
override fun restartUpload(index: Int) {
140-
if (contributionsList != null) {
141-
pendingUploadsPresenter.restartUpload(
142-
contributionsList,
143-
index,
144-
this.requireContext().applicationContext,
145-
)
146-
}
139+
pendingUploadsPresenter.restartUpload(
140+
contributionsList,
141+
index,
142+
requireContext().applicationContext,
143+
)
147144
}
148145

149146
/**
@@ -166,7 +163,7 @@ class FailedUploadsFragment :
166163
ViewUtil.showShortToast(context, R.string.cancelling_upload)
167164
pendingUploadsPresenter.deleteUpload(
168165
contribution,
169-
this.requireContext().applicationContext,
166+
requireContext().applicationContext,
170167
)
171168
},
172169
{},
@@ -177,28 +174,24 @@ class FailedUploadsFragment :
177174
* Deletes all the uploads after getting a confirmation from the user using Dialog.
178175
*/
179176
fun deleteUploads() {
180-
if (contributionsList != null) {
181-
DialogUtil.showAlertDialog(
182-
requireActivity(),
183-
String.format(
184-
Locale.getDefault(),
185-
requireActivity().getString(R.string.cancelling_all_the_uploads),
186-
),
187-
String.format(
188-
Locale.getDefault(),
189-
requireActivity().getString(R.string.are_you_sure_that_you_want_cancel_all_the_uploads),
190-
),
191-
String.format(Locale.getDefault(), requireActivity().getString(R.string.yes)),
192-
String.format(Locale.getDefault(), requireActivity().getString(R.string.no)),
193-
{
194-
ViewUtil.showShortToast(context, R.string.cancelling_upload)
195-
uploadProgressActivity.hidePendingIcons()
196-
pendingUploadsPresenter.deleteUploads(
197-
listOf(Contribution.STATE_FAILED),
198-
)
199-
},
200-
{},
201-
)
202-
}
177+
DialogUtil.showAlertDialog(
178+
requireActivity(),
179+
String.format(
180+
Locale.getDefault(),
181+
requireActivity().getString(R.string.cancelling_all_the_uploads),
182+
),
183+
String.format(
184+
Locale.getDefault(),
185+
requireActivity().getString(R.string.are_you_sure_that_you_want_cancel_all_the_uploads),
186+
),
187+
String.format(Locale.getDefault(), requireActivity().getString(R.string.yes)),
188+
String.format(Locale.getDefault(), requireActivity().getString(R.string.no)),
189+
{
190+
ViewUtil.showShortToast(context, R.string.cancelling_upload)
191+
uploadProgressActivity.hidePendingIcons()
192+
pendingUploadsPresenter.deleteUploads(listOf(STATE_FAILED))
193+
},
194+
{},
195+
)
203196
}
204197
}

app/src/main/java/fr/free/nrw/commons/upload/FileMetadataUtils.java

-59
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package fr.free.nrw.commons.upload
2+
3+
import androidx.exifinterface.media.ExifInterface
4+
import timber.log.Timber
5+
6+
/**
7+
* Support utils for EXIF metadata handling
8+
*
9+
*/
10+
object FileMetadataUtils {
11+
/**
12+
* Takes EXIF label from sharedPreferences as input and returns relevant EXIF tags
13+
*
14+
* @param pref EXIF sharedPreference label
15+
* @return EXIF tags
16+
*/
17+
fun getTagsFromPref(pref: String): Array<String> {
18+
Timber.d("Retuning tags for pref:%s", pref)
19+
return when (pref) {
20+
"Author" -> arrayOf(
21+
ExifInterface.TAG_ARTIST,
22+
ExifInterface.TAG_CAMERA_OWNER_NAME
23+
)
24+
25+
"Copyright" -> arrayOf(
26+
ExifInterface.TAG_COPYRIGHT
27+
)
28+
29+
"Location" -> arrayOf(
30+
ExifInterface.TAG_GPS_LATITUDE,
31+
ExifInterface.TAG_GPS_LATITUDE_REF,
32+
ExifInterface.TAG_GPS_LONGITUDE,
33+
ExifInterface.TAG_GPS_LONGITUDE_REF,
34+
ExifInterface.TAG_GPS_ALTITUDE,
35+
ExifInterface.TAG_GPS_ALTITUDE_REF
36+
)
37+
38+
"Camera Model" -> arrayOf(
39+
ExifInterface.TAG_MAKE,
40+
ExifInterface.TAG_MODEL
41+
)
42+
43+
"Lens Model" -> arrayOf(
44+
ExifInterface.TAG_LENS_MAKE,
45+
ExifInterface.TAG_LENS_MODEL,
46+
ExifInterface.TAG_LENS_SPECIFICATION
47+
)
48+
49+
"Serial Numbers" -> arrayOf(
50+
ExifInterface.TAG_BODY_SERIAL_NUMBER,
51+
ExifInterface.TAG_LENS_SERIAL_NUMBER
52+
)
53+
54+
"Software" -> arrayOf(
55+
ExifInterface.TAG_SOFTWARE
56+
)
57+
58+
else -> arrayOf()
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)