|
@@ -2,14 +2,22 @@ package im.angry.openeuicc.ui
|
|
|
|
|
|
|
|
import android.app.Dialog
|
|
import android.app.Dialog
|
|
|
import android.os.Bundle
|
|
import android.os.Bundle
|
|
|
|
|
+import android.util.Log
|
|
|
import android.view.*
|
|
import android.view.*
|
|
|
|
|
+import android.widget.Toast
|
|
|
import androidx.appcompat.widget.Toolbar
|
|
import androidx.appcompat.widget.Toolbar
|
|
|
import androidx.fragment.app.DialogFragment
|
|
import androidx.fragment.app.DialogFragment
|
|
|
|
|
+import androidx.lifecycle.lifecycleScope
|
|
|
import com.journeyapps.barcodescanner.ScanContract
|
|
import com.journeyapps.barcodescanner.ScanContract
|
|
|
import com.journeyapps.barcodescanner.ScanOptions
|
|
import com.journeyapps.barcodescanner.ScanOptions
|
|
|
|
|
+import com.truphone.lpa.progress.DownloadProgress
|
|
|
import im.angry.openeuicc.R
|
|
import im.angry.openeuicc.R
|
|
|
import im.angry.openeuicc.databinding.FragmentProfileDownloadBinding
|
|
import im.angry.openeuicc.databinding.FragmentProfileDownloadBinding
|
|
|
import im.angry.openeuicc.util.setWidthPercent
|
|
import im.angry.openeuicc.util.setWidthPercent
|
|
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
|
|
+import kotlinx.coroutines.launch
|
|
|
|
|
+import kotlinx.coroutines.withContext
|
|
|
|
|
+import java.lang.Exception
|
|
|
|
|
|
|
|
class ProfileDownloadFragment : DialogFragment(), EuiccFragmentMarker, Toolbar.OnMenuItemClickListener {
|
|
class ProfileDownloadFragment : DialogFragment(), EuiccFragmentMarker, Toolbar.OnMenuItemClickListener {
|
|
|
companion object {
|
|
companion object {
|
|
@@ -22,6 +30,8 @@ class ProfileDownloadFragment : DialogFragment(), EuiccFragmentMarker, Toolbar.O
|
|
|
private var _binding: FragmentProfileDownloadBinding? = null
|
|
private var _binding: FragmentProfileDownloadBinding? = null
|
|
|
private val binding get() = _binding!!
|
|
private val binding get() = _binding!!
|
|
|
|
|
|
|
|
|
|
+ private var downloading = false
|
|
|
|
|
+
|
|
|
private val barcodeScannerLauncher = registerForActivityResult(ScanContract()) { result ->
|
|
private val barcodeScannerLauncher = registerForActivityResult(ScanContract()) { result ->
|
|
|
result.contents?.let { content ->
|
|
result.contents?.let { content ->
|
|
|
val components = content.split("$")
|
|
val components = content.split("$")
|
|
@@ -46,13 +56,13 @@ class ProfileDownloadFragment : DialogFragment(), EuiccFragmentMarker, Toolbar.O
|
|
|
binding.toolbar.apply {
|
|
binding.toolbar.apply {
|
|
|
setTitle(R.string.profile_download)
|
|
setTitle(R.string.profile_download)
|
|
|
setNavigationOnClickListener {
|
|
setNavigationOnClickListener {
|
|
|
- dismiss()
|
|
|
|
|
|
|
+ if (!downloading) dismiss()
|
|
|
}
|
|
}
|
|
|
setOnMenuItemClickListener(this@ProfileDownloadFragment)
|
|
setOnMenuItemClickListener(this@ProfileDownloadFragment)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- override fun onMenuItemClick(item: MenuItem): Boolean =
|
|
|
|
|
|
|
+ override fun onMenuItemClick(item: MenuItem): Boolean = downloading ||
|
|
|
when (item.itemId) {
|
|
when (item.itemId) {
|
|
|
R.id.scan -> {
|
|
R.id.scan -> {
|
|
|
barcodeScannerLauncher.launch(ScanOptions().apply {
|
|
barcodeScannerLauncher.launch(ScanOptions().apply {
|
|
@@ -61,6 +71,10 @@ class ProfileDownloadFragment : DialogFragment(), EuiccFragmentMarker, Toolbar.O
|
|
|
})
|
|
})
|
|
|
true
|
|
true
|
|
|
}
|
|
}
|
|
|
|
|
+ R.id.ok -> {
|
|
|
|
|
+ startDownloadProfile()
|
|
|
|
|
+ true
|
|
|
|
|
+ }
|
|
|
else -> false
|
|
else -> false
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -75,4 +89,56 @@ class ProfileDownloadFragment : DialogFragment(), EuiccFragmentMarker, Toolbar.O
|
|
|
it.setCanceledOnTouchOutside(false)
|
|
it.setCanceledOnTouchOutside(false)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private fun startDownloadProfile() {
|
|
|
|
|
+ val server = binding.profileDownloadServer.editText!!.let {
|
|
|
|
|
+ it.text.toString().trim().apply {
|
|
|
|
|
+ if (isEmpty()) {
|
|
|
|
|
+ it.requestFocus()
|
|
|
|
|
+ return@startDownloadProfile
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ val code = binding.profileDownloadCode.editText!!.let {
|
|
|
|
|
+ it.text.toString().trim().apply {
|
|
|
|
|
+ if (isEmpty()) {
|
|
|
|
|
+ it.requestFocus()
|
|
|
|
|
+ return@startDownloadProfile
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ downloading = true
|
|
|
|
|
+
|
|
|
|
|
+ binding.profileDownloadServer.editText!!.isEnabled = false
|
|
|
|
|
+ binding.profileDownloadCode.editText!!.isEnabled = false
|
|
|
|
|
+
|
|
|
|
|
+ binding.progress.isIndeterminate = true
|
|
|
|
|
+ binding.progress.visibility = View.VISIBLE
|
|
|
|
|
+
|
|
|
|
|
+ lifecycleScope.launch {
|
|
|
|
|
+ try {
|
|
|
|
|
+ doDownloadProfile(server, code)
|
|
|
|
|
+ } catch (e: Exception) {
|
|
|
|
|
+ Log.d(TAG, "Error downloading profile")
|
|
|
|
|
+ Log.d(TAG, Log.getStackTraceString(e))
|
|
|
|
|
+ Toast.makeText(context, R.string.profile_download_failed, Toast.LENGTH_LONG).show()
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ if (parentFragment is EuiccProfilesChangedListener) {
|
|
|
|
|
+ (parentFragment as EuiccProfilesChangedListener).onEuiccProfilesChanged()
|
|
|
|
|
+ }
|
|
|
|
|
+ dismiss()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private suspend fun doDownloadProfile(server: String, code: String) = withContext(Dispatchers.IO) {
|
|
|
|
|
+ channel.lpa.downloadProfile("1\$${server}\$${code}", DownloadProgress().apply {
|
|
|
|
|
+ setProgressListener { _, _, percentage, _ ->
|
|
|
|
|
+ binding.progress.isIndeterminate = false
|
|
|
|
|
+ binding.progress.progress = (percentage * 100).toInt()
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|