Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port download library and remove F-Droid incompatible libs. #76

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@ dependencies {
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'io.github.medyo:android-about-page:2.0.0'
implementation 'com.suddenh4x.ratingdialog:awesome-app-rating:2.4.0'
implementation 'com.huxq17.pump:download:1.3.10'
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'de.cketti.library.changelog:ckchangelog:1.2.2'
implementation 'io.sentry:sentry-android:4.3.0'
implementation 'com.mikepenz:aboutlibraries:8.9.2'
implementation "com.github.tonyofrancis.Fetch:fetch2:3.3.0"

testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
android:supportsRtl="true"
android:theme="@style/Theme.NotificationDictionary">

<meta-data
android:name="io.sentry.dsn"
android:value="https://[email protected]/5955342" />

<activity
android:name=".AboutActivity"
android:exported="true"
Expand Down
158 changes: 83 additions & 75 deletions app/src/main/java/com/xtreak/notificationdictionary/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@ import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.res.Configuration
import android.os.*
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.*
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.Spinner
import android.widget.Switch
import android.widget.TextView
import android.widget.TextView.OnEditorActionListener
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -33,17 +42,19 @@ import androidx.core.view.MenuItemCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import com.huxq17.download.Pump
import com.huxq17.download.config.DownloadConfig
import com.huxq17.download.core.DownloadListener
import com.mikepenz.aboutlibraries.LibsBuilder
import com.suddenh4x.ratingdialog.AppRating
import com.suddenh4x.ratingdialog.preferences.RatingThreshold
import com.tonyodev.fetch2.AbstractFetchListener
import com.tonyodev.fetch2.Download
import com.tonyodev.fetch2.Error
import com.tonyodev.fetch2.Fetch.Impl.getInstance
import com.tonyodev.fetch2.FetchConfiguration
import com.tonyodev.fetch2.NetworkType
import com.tonyodev.fetch2.Priority
import com.tonyodev.fetch2.Request
import de.cketti.library.changelog.ChangeLog
import io.sentry.Sentry
import java.io.File
import java.io.FileOutputStream
import java.util.*
import java.util.Locale
import java.util.concurrent.Executors
import java.util.zip.ZipFile

Expand Down Expand Up @@ -188,7 +199,6 @@ class MainActivity : AppCompatActivity() {

initialize_spinner(database_name)
// show_changelog()
show_rating()
onNewIntent(intent)

// Request notification permission in Android 33+
Expand Down Expand Up @@ -351,16 +361,6 @@ class MainActivity : AppCompatActivity() {
}
}

fun show_rating() {
AppRating.Builder(this)
.setMinimumLaunchTimes(10)
.setMinimumDays(2)
.setMinimumLaunchTimesToShowAgain(15)
.setMinimumDaysToShowAgain(10)
.setRatingThreshold(RatingThreshold.FIVE)
.showIfMeetsConditions()
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.menu, menu)
Expand Down Expand Up @@ -404,16 +404,19 @@ class MainActivity : AppCompatActivity() {
val about_activity = Intent(applicationContext, AboutActivity::class.java)
startActivityForResult(about_activity, 0)
}

R.id.license -> {
LibsBuilder()
.withActivityTitle("Open Source Licenses")
.withLicenseShown(true)
.start(this)
}

R.id.history -> {
val history_activity = Intent(applicationContext, HistoryActivity::class.java)
startActivityForResult(history_activity, 0)
}

R.id.favourite -> {
val favourite_activity = Intent(applicationContext, FavouriteActivity::class.java)
startActivityForResult(favourite_activity, 0)
Expand All @@ -438,7 +441,8 @@ class MainActivity : AppCompatActivity() {
// But we don't want the user to cancel this. It's one time and takes a couple of seconds

// TODO: Make this configurable based on environment?
val url = "https://xtreak.sfo3.cdn.digitaloceanspaces.com/dictionaries/v2/$database_name.zip"
val url =
"https://xtreak.sfo3.cdn.digitaloceanspaces.com/dictionaries/v2/$database_name.zip"
// val url = "http://192.168.0.105:8000/$database_name.zip" // for local mobile testing
// val url = "http://10.0.2.2:8000/$database_name.zip" // for local emulator testing

Expand All @@ -447,65 +451,69 @@ class MainActivity : AppCompatActivity() {
Environment.getDataDirectory().absolutePath + "/data/" + packageName
val zip_path = File("$package_data_directory/$database_name.zip").absolutePath

// https://github.com/huxq17/Pump/blob/master/kotlin_app/src/main/java/com/huxq17/download/demo/MainActivity.kt
DownloadConfig.newBuilder()
.setMaxRunningTaskNum(1)
.setMinUsableStorageSpace(140 * 1024L * 1024) // 140MB as per database size
val fetchConfiguration: FetchConfiguration = FetchConfiguration.Builder(this)
.setDownloadConcurrentLimit(3)
.build()

val fetch = getInstance(fetchConfiguration)
val request: Request = Request(url, zip_path)
request.priority = Priority.HIGH
request.networkType = NetworkType.ALL
progressDialog.progress = 0
progressDialog.show()
Pump.newRequest(url, zip_path)
.listener(object : DownloadListener() {
fetch.addListener(object : AbstractFetchListener() {

fun copy_and_unzip(source: String, destination: String) {
val zipfile = ZipFile(source)
val entry = zipfile.entries().toList().first()

// The zip file only has one entry which is the database. So use it as an
// input stream and copy the unzipped file to output stream. Delete the source
// zip file to save space.
val input_stream = zipfile.getInputStream(entry)
val output_stream = FileOutputStream(destination)
input_stream.copyTo(output_stream, 1024 * 1024 * 2)
File(zip_path).delete()
}

override fun onProgress(progress: Int) {
progressDialog.progress = progress
}
override fun onCompleted(download: Download) {
val destination_folder = File("$package_data_directory/databases")
val destination_path =
File("$package_data_directory/databases/$database_name").absolutePath
val source_path = download.file

fun copy_and_unzip(source: String, destination: String) {
val zipfile = ZipFile(source)
val entry = zipfile.entries().toList().first()

// The zip file only has one entry which is the database. So use it as an
// input stream and copy the unzipped file to output stream. Delete the source
// zip file to save space.
val input_stream = zipfile.getInputStream(entry)
val output_stream = FileOutputStream(destination)
input_stream.copyTo(output_stream, 1024 * 1024 * 2)
File(zip_path).delete()
if (!destination_folder.exists()) {
destination_folder.mkdirs()
}

override fun onSuccess() {
val destination_folder = File("$package_data_directory/databases")
val destination_path =
File("$package_data_directory/databases/$database_name").absolutePath
val source_path = downloadInfo.filePath

if (!destination_folder.exists()) {
destination_folder.mkdirs()
}

copy_and_unzip(source_path, destination_path)
progressDialog.dismiss()
Snackbar.make(
findViewById(R.id.mainLayout),
"Download finished",
Snackbar.LENGTH_SHORT
).show()
}
copy_and_unzip(source_path, destination_path)
progressDialog.dismiss()
Snackbar.make(
findViewById(R.id.mainLayout),
"Download finished",
Snackbar.LENGTH_SHORT
).show()
}

override fun onFailed() {
progressDialog.dismiss()
Snackbar.make(
findViewById(R.id.mainLayout),
"Download failed. Please check your internet connection and relaunch the app.",
Snackbar.LENGTH_INDEFINITE
).show()
}
})
.forceReDownload(false)
.threadNum(3)
.setRetry(3, 200)
.submit()
override fun onError(download: Download, error: Error, throwable: Throwable?) {
progressDialog.dismiss()
Snackbar.make(
findViewById(R.id.mainLayout),
"Download failed. Please check your internet connection and relaunch the app.",
Snackbar.LENGTH_INDEFINITE
).show()
}

override fun onProgress(
download: Download,
etaInMilliseconds: Long,
downloadedBytesPerSecond: Long
) {
progressDialog.progress = download.progress
}
})

fetch.enqueue(request)
}

override fun onDestroy() {
Expand Down Expand Up @@ -571,7 +579,8 @@ class MainActivity : AppCompatActivity() {
// https://stackoverflow.com/questions/18414804/android-edittext-remove-focus-after-clicking-a-button
wordEdit.clearFocus()

val word = wordEdit.text.toString().trim().lowercase()
var word = wordEdit.text.toString().trim().lowercase()
word = removePunctuation(word).toString()

val executor = Executors.newSingleThreadExecutor()
val handler = Handler(Looper.getMainLooper())
Expand All @@ -588,7 +597,6 @@ class MainActivity : AppCompatActivity() {
addHistoryEntry(historyDao, word)
}
} catch (e: Exception) {
Sentry.captureException(e)
Log.d("ndict:", e.toString())
meanings = listOf(
Word(
Expand All @@ -603,7 +611,7 @@ class MainActivity : AppCompatActivity() {
try {
resolveRedirectMeaning(meanings, dao)
} catch (e: Exception) {
Sentry.captureException(e)
Log.d("ndict", e.toString())
}

handler.post {
Expand Down
Loading