@@ -18,13 +18,22 @@ import android.content.Context
1818import android.content.DialogInterface
1919import android.content.Intent
2020import android.content.res.Configuration
21- import android.os.*
21+ import android.os.Build
22+ import android.os.Bundle
23+ import android.os.Environment
24+ import android.os.Handler
25+ import android.os.Looper
2226import android.util.Log
2327import android.view.Menu
2428import android.view.MenuItem
2529import android.view.View
2630import android.view.inputmethod.EditorInfo
27- import android.widget.*
31+ import android.widget.AdapterView
32+ import android.widget.ArrayAdapter
33+ import android.widget.EditText
34+ import android.widget.Spinner
35+ import android.widget.Switch
36+ import android.widget.TextView
2837import android.widget.TextView.OnEditorActionListener
2938import androidx.appcompat.app.AlertDialog
3039import androidx.appcompat.app.AppCompatActivity
@@ -33,17 +42,19 @@ import androidx.core.view.MenuItemCompat
3342import androidx.recyclerview.widget.LinearLayoutManager
3443import androidx.recyclerview.widget.RecyclerView
3544import com.google.android.material.snackbar.Snackbar
36- import com.huxq17.download.Pump
37- import com.huxq17.download.config.DownloadConfig
38- import com.huxq17.download.core.DownloadListener
3945import com.mikepenz.aboutlibraries.LibsBuilder
40- import com.suddenh4x.ratingdialog.AppRating
41- import com.suddenh4x.ratingdialog.preferences.RatingThreshold
46+ import com.tonyodev.fetch2.AbstractFetchListener
47+ import com.tonyodev.fetch2.Download
48+ import com.tonyodev.fetch2.Error
49+ import com.tonyodev.fetch2.Fetch.Impl.getInstance
50+ import com.tonyodev.fetch2.FetchConfiguration
51+ import com.tonyodev.fetch2.NetworkType
52+ import com.tonyodev.fetch2.Priority
53+ import com.tonyodev.fetch2.Request
4254import de.cketti.library.changelog.ChangeLog
43- import io.sentry.Sentry
4455import java.io.File
4556import java.io.FileOutputStream
46- import java.util.*
57+ import java.util.Locale
4758import java.util.concurrent.Executors
4859import java.util.zip.ZipFile
4960
@@ -188,7 +199,6 @@ class MainActivity : AppCompatActivity() {
188199
189200 initialize_spinner(database_name)
190201 // show_changelog()
191- show_rating()
192202 onNewIntent(intent)
193203
194204 // Request notification permission in Android 33+
@@ -351,16 +361,6 @@ class MainActivity : AppCompatActivity() {
351361 }
352362 }
353363
354- fun show_rating () {
355- AppRating .Builder (this )
356- .setMinimumLaunchTimes(10 )
357- .setMinimumDays(2 )
358- .setMinimumLaunchTimesToShowAgain(15 )
359- .setMinimumDaysToShowAgain(10 )
360- .setRatingThreshold(RatingThreshold .FIVE )
361- .showIfMeetsConditions()
362- }
363-
364364 override fun onCreateOptionsMenu (menu : Menu ? ): Boolean {
365365 val inflater = menuInflater
366366 inflater.inflate(R .menu.menu, menu)
@@ -404,16 +404,19 @@ class MainActivity : AppCompatActivity() {
404404 val about_activity = Intent (applicationContext, AboutActivity ::class .java)
405405 startActivityForResult(about_activity, 0 )
406406 }
407+
407408 R .id.license -> {
408409 LibsBuilder ()
409410 .withActivityTitle(" Open Source Licenses" )
410411 .withLicenseShown(true )
411412 .start(this )
412413 }
414+
413415 R .id.history -> {
414416 val history_activity = Intent (applicationContext, HistoryActivity ::class .java)
415417 startActivityForResult(history_activity, 0 )
416418 }
419+
417420 R .id.favourite -> {
418421 val favourite_activity = Intent (applicationContext, FavouriteActivity ::class .java)
419422 startActivityForResult(favourite_activity, 0 )
@@ -438,7 +441,8 @@ class MainActivity : AppCompatActivity() {
438441 // But we don't want the user to cancel this. It's one time and takes a couple of seconds
439442
440443 // TODO: Make this configurable based on environment?
441- val url = " https://xtreak.sfo3.cdn.digitaloceanspaces.com/dictionaries/v2/$database_name .zip"
444+ val url =
445+ " https://xtreak.sfo3.cdn.digitaloceanspaces.com/dictionaries/v2/$database_name .zip"
442446 // val url = "http://192.168.0.105:8000/$database_name.zip" // for local mobile testing
443447 // val url = "http://10.0.2.2:8000/$database_name.zip" // for local emulator testing
444448
@@ -447,65 +451,69 @@ class MainActivity : AppCompatActivity() {
447451 Environment .getDataDirectory().absolutePath + " /data/" + packageName
448452 val zip_path = File (" $package_data_directory /$database_name .zip" ).absolutePath
449453
450- // https://github.com/huxq17/Pump/blob/master/kotlin_app/src/main/java/com/huxq17/download/demo/MainActivity.kt
451- DownloadConfig .newBuilder()
452- .setMaxRunningTaskNum(1 )
453- .setMinUsableStorageSpace(140 * 1024L * 1024 ) // 140MB as per database size
454+ val fetchConfiguration: FetchConfiguration = FetchConfiguration .Builder (this )
455+ .setDownloadConcurrentLimit(3 )
454456 .build()
457+
458+ val fetch = getInstance(fetchConfiguration)
459+ val request: Request = Request (url, zip_path)
460+ request.priority = Priority .HIGH
461+ request.networkType = NetworkType .ALL
455462 progressDialog.progress = 0
456463 progressDialog.show()
457- Pump .newRequest(url, zip_path)
458- .listener(object : DownloadListener () {
464+ fetch.addListener(object : AbstractFetchListener () {
465+
466+ fun copy_and_unzip (source : String , destination : String ) {
467+ val zipfile = ZipFile (source)
468+ val entry = zipfile.entries().toList().first()
469+
470+ // The zip file only has one entry which is the database. So use it as an
471+ // input stream and copy the unzipped file to output stream. Delete the source
472+ // zip file to save space.
473+ val input_stream = zipfile.getInputStream(entry)
474+ val output_stream = FileOutputStream (destination)
475+ input_stream.copyTo(output_stream, 1024 * 1024 * 2 )
476+ File (zip_path).delete()
477+ }
459478
460- override fun onProgress (progress : Int ) {
461- progressDialog.progress = progress
462- }
479+ override fun onCompleted (download : Download ) {
480+ val destination_folder = File (" $package_data_directory /databases" )
481+ val destination_path =
482+ File (" $package_data_directory /databases/$database_name " ).absolutePath
483+ val source_path = download.file
463484
464- fun copy_and_unzip (source : String , destination : String ) {
465- val zipfile = ZipFile (source)
466- val entry = zipfile.entries().toList().first()
467-
468- // The zip file only has one entry which is the database. So use it as an
469- // input stream and copy the unzipped file to output stream. Delete the source
470- // zip file to save space.
471- val input_stream = zipfile.getInputStream(entry)
472- val output_stream = FileOutputStream (destination)
473- input_stream.copyTo(output_stream, 1024 * 1024 * 2 )
474- File (zip_path).delete()
485+ if (! destination_folder.exists()) {
486+ destination_folder.mkdirs()
475487 }
476488
477- override fun onSuccess () {
478- val destination_folder = File (" $package_data_directory /databases" )
479- val destination_path =
480- File (" $package_data_directory /databases/$database_name " ).absolutePath
481- val source_path = downloadInfo.filePath
482-
483- if (! destination_folder.exists()) {
484- destination_folder.mkdirs()
485- }
486-
487- copy_and_unzip(source_path, destination_path)
488- progressDialog.dismiss()
489- Snackbar .make(
490- findViewById(R .id.mainLayout),
491- " Download finished" ,
492- Snackbar .LENGTH_SHORT
493- ).show()
494- }
489+ copy_and_unzip(source_path, destination_path)
490+ progressDialog.dismiss()
491+ Snackbar .make(
492+ findViewById(R .id.mainLayout),
493+ " Download finished" ,
494+ Snackbar .LENGTH_SHORT
495+ ).show()
496+ }
495497
496- override fun onFailed () {
497- progressDialog.dismiss()
498- Snackbar .make(
499- findViewById(R .id.mainLayout),
500- " Download failed. Please check your internet connection and relaunch the app." ,
501- Snackbar .LENGTH_INDEFINITE
502- ).show()
503- }
504- })
505- .forceReDownload(false )
506- .threadNum(3 )
507- .setRetry(3 , 200 )
508- .submit()
498+ override fun onError (download : Download , error : Error , throwable : Throwable ? ) {
499+ progressDialog.dismiss()
500+ Snackbar .make(
501+ findViewById(R .id.mainLayout),
502+ " Download failed. Please check your internet connection and relaunch the app." ,
503+ Snackbar .LENGTH_INDEFINITE
504+ ).show()
505+ }
506+
507+ override fun onProgress (
508+ download : Download ,
509+ etaInMilliseconds : Long ,
510+ downloadedBytesPerSecond : Long
511+ ) {
512+ progressDialog.progress = download.progress
513+ }
514+ })
515+
516+ fetch.enqueue(request)
509517 }
510518
511519 override fun onDestroy () {
@@ -571,7 +579,8 @@ class MainActivity : AppCompatActivity() {
571579 // https://stackoverflow.com/questions/18414804/android-edittext-remove-focus-after-clicking-a-button
572580 wordEdit.clearFocus()
573581
574- val word = wordEdit.text.toString().trim().lowercase()
582+ var word = wordEdit.text.toString().trim().lowercase()
583+ word = removePunctuation(word).toString()
575584
576585 val executor = Executors .newSingleThreadExecutor()
577586 val handler = Handler (Looper .getMainLooper())
@@ -588,7 +597,6 @@ class MainActivity : AppCompatActivity() {
588597 addHistoryEntry(historyDao, word)
589598 }
590599 } catch (e: Exception ) {
591- Sentry .captureException(e)
592600 Log .d(" ndict:" , e.toString())
593601 meanings = listOf (
594602 Word (
@@ -603,7 +611,7 @@ class MainActivity : AppCompatActivity() {
603611 try {
604612 resolveRedirectMeaning(meanings, dao)
605613 } catch (e: Exception ) {
606- Sentry .captureException(e )
614+ Log .d( " ndict " , e.toString() )
607615 }
608616
609617 handler.post {
0 commit comments