Skip to content

Commit

Permalink
actions: refactor DialogUtils to kotlin (fixes open-learning-exchan…
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 and dogi authored Jan 3, 2024
1 parent ff748c1 commit 47056e6
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 122 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1252
versionName "0.12.52"
versionCode 1253
versionName "0.12.53"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
120 changes: 0 additions & 120 deletions app/src/main/java/org/ole/planet/myplanet/utilities/DialogUtils.java

This file was deleted.

142 changes: 142 additions & 0 deletions app/src/main/java/org/ole/planet/myplanet/utilities/DialogUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package org.ole.planet.myplanet.utilities

import android.app.Activity
import android.app.ProgressDialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.provider.Settings
import androidx.appcompat.app.AlertDialog
import android.view.View
import com.google.android.material.snackbar.Snackbar
import org.ole.planet.myplanet.MainApplication
import org.ole.planet.myplanet.R
import org.ole.planet.myplanet.datamanager.MyDownloadService
import org.ole.planet.myplanet.datamanager.Service
import org.ole.planet.myplanet.model.MyPlanet

object DialogUtils {
@JvmStatic
fun getProgressDialog(context: Context): ProgressDialog {
val prgDialog = ProgressDialog(context)
prgDialog.setTitle(context.getString(R.string.downloading_file))
prgDialog.max = 100
prgDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)
prgDialog.setCancelable(false)
prgDialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getString(R.string.finish)) { _, _ -> prgDialog.dismiss() }
prgDialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.stop_download)) { _, _ ->
context.stopService(Intent(context, MyDownloadService::class.java))
}
return prgDialog
}

@JvmStatic
fun showError(prgDialog: ProgressDialog, message: String) {
prgDialog.setTitle(message)
prgDialog.getButton(ProgressDialog.BUTTON_NEGATIVE)?.isEnabled = false
}

@JvmStatic
fun showWifiSettingDialog(context: Context) {
if (!NetworkUtils.isWifiBluetoothEnabled()) return
showDialog(context)
}

private fun showDialog(context: Context) {
if (MainApplication.syncFailedCount > 3) {
val pd = AlertDialog.Builder(context)
var message = if (NetworkUtils.isBluetoothEnabled()) "Bluetooth " else ""
message += if (NetworkUtils.isWifiEnabled()) "Wifi " else ""
message += context.getString(R.string.is_on_please_turn_of_to_save_battery)
pd.setMessage(message)
pd.setPositiveButton(context.getString(R.string.go_to_settings)) { _, _ ->
MainApplication.syncFailedCount = 0
val intent = Intent(Settings.ACTION_WIFI_SETTINGS)
context.startActivity(intent)
}.setNegativeButton(context.getString(R.string.cancel), null)
pd.setCancelable(false)
val d = pd.create()
d.show()
}
}

@JvmStatic
fun showSnack(v: View?, s: String) {
if (v != null) Snackbar.make(v, s, Snackbar.LENGTH_LONG).show()
}

@JvmStatic
fun showAlert(context: Context, title: String?, message: String?) {
if (context is Activity && !(context as Activity).isFinishing) {
AlertDialog.Builder(context)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.finish, null)
.show()
}
}

@JvmStatic
fun getAlertDialog(context: Context, message: String, pos: String, listener: DialogInterface.OnClickListener?): AlertDialog {
return AlertDialog.Builder(context)
.setMessage(message)
.setIcon(R.drawable.courses)
.setPositiveButton(pos, listener)
.setNegativeButton("Cancel", null)
.show()
}

@JvmStatic
fun showCloseAlert(context: Context, title: String, message: String) {
AlertDialog.Builder(context)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.close, null)
.show()
}

@JvmStatic
fun getAlertDialog(context: Context, title: String, v: View): AlertDialog {
return AlertDialog.Builder(context)
.setTitle(title)
.setIcon(R.drawable.ic_edit)
.setView(v)
.setPositiveButton(R.string.submit, null)
.setNegativeButton(R.string.cancel, null)
.show()
}

@JvmStatic
fun getUpdateDialog(context: Context, info: MyPlanet, progressDialog: ProgressDialog): AlertDialog.Builder {
return AlertDialog.Builder(context)
.setTitle(R.string.new_version_of_my_planet_available)
.setMessage(R.string.download_first_to_continue)
.setNeutralButton(R.string.upgrade_local) { _, _ ->
startDownloadUpdate(context, Utilities.getApkUpdateUrl(info.localapkpath), progressDialog)
}
.setPositiveButton(R.string.upgrade) { _, _ ->
startDownloadUpdate(context, info.apkpath!!, progressDialog)
}
}

@JvmStatic
fun startDownloadUpdate(context: Context, path: String, progressDialog: ProgressDialog) {
Service(MainApplication.context).checkCheckSum(object : Service.ChecksumCallback {
override fun onMatch() {
Utilities.toast(MainApplication.context, context.getString(R.string.apk_already_exists))
FileUtils.installApk(context, path)
}

override fun onFail() {
val url = ArrayList<String>()
url.add(path)
if (progressDialog != null) {
progressDialog.setMessage(context.getString(R.string.downloading_file))
progressDialog.setCancelable(false)
progressDialog.show()
}
Utilities.openDownloadService(context, url)
}
}, path)
}
}

0 comments on commit 47056e6

Please sign in to comment.