2020-05-12 09:03:46 +03:00
|
|
|
package com.illusionware.npsbrowser.activities
|
|
|
|
|
|
|
|
|
|
import android.Manifest
|
|
|
|
|
import android.app.Activity
|
|
|
|
|
import android.content.Intent
|
|
|
|
|
import android.content.pm.PackageManager
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.util.Log
|
2020-05-19 04:53:12 +03:00
|
|
|
import android.view.Menu
|
|
|
|
|
import android.view.MenuItem
|
2020-05-12 09:03:46 +03:00
|
|
|
import androidx.core.app.ActivityCompat
|
|
|
|
|
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader
|
2020-05-17 05:54:01 +03:00
|
|
|
import com.illusionware.npsbrowser.AppData
|
2020-05-19 04:53:12 +03:00
|
|
|
import com.illusionware.npsbrowser.R
|
2020-05-12 09:03:46 +03:00
|
|
|
import com.illusionware.npsbrowser.databinding.ActivityMainBinding
|
2020-05-19 04:53:12 +03:00
|
|
|
import kotlinx.android.synthetic.main.appbar.*
|
2020-05-12 09:03:46 +03:00
|
|
|
import java.io.InputStream
|
|
|
|
|
|
|
|
|
|
class MainActivity : AppCompatActivity() {
|
|
|
|
|
|
|
|
|
|
private val tsvReader = csvReader {
|
|
|
|
|
charset = "UTF-8"
|
2020-05-17 05:39:32 +03:00
|
|
|
quoteChar = '\\'
|
|
|
|
|
escapeChar = '\\'
|
2020-05-12 09:03:46 +03:00
|
|
|
delimiter = '\t'
|
2020-05-17 05:39:32 +03:00
|
|
|
skipEmptyLine = true
|
2020-05-12 09:03:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
super.onCreate(savedInstanceState)
|
2020-05-19 04:53:12 +03:00
|
|
|
val binding = ActivityMainBinding.inflate(layoutInflater)
|
2020-05-12 09:03:46 +03:00
|
|
|
setContentView(binding.root)
|
|
|
|
|
|
2020-05-19 04:53:12 +03:00
|
|
|
setSupportActionBar(toolbar)
|
|
|
|
|
|
2020-05-12 09:03:46 +03:00
|
|
|
binding.tsvChooserButton.setOnClickListener {
|
|
|
|
|
if (!isStoragePermissionGranted())
|
|
|
|
|
return@setOnClickListener
|
|
|
|
|
val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
|
|
|
|
|
addCategory(Intent.CATEGORY_OPENABLE)
|
|
|
|
|
type = "text/tab-separated-values"
|
|
|
|
|
}
|
|
|
|
|
startActivityForResult(intent, 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-19 04:53:12 +03:00
|
|
|
|
|
|
|
|
override fun onCreateOptionsMenu(menu : Menu) : Boolean {
|
|
|
|
|
menuInflater.inflate(R.menu.toolbar_main, menu)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
|
return when (item.itemId) {
|
|
|
|
|
R.id.action_settings -> {
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
else -> return super.onOptionsItemSelected(item)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 09:03:46 +03:00
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
|
|
|
|
|
super.onActivityResult(requestCode, resultCode, resultData)
|
|
|
|
|
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
|
|
|
|
|
resultData?.data?.also { uri ->
|
|
|
|
|
val inputStream: InputStream? = contentResolver.openInputStream(uri)
|
2020-05-17 05:54:01 +03:00
|
|
|
val entries : List<Map<String, String?>> = tsvReader.readAllWithHeader(inputStream!!)
|
|
|
|
|
val myIntent = Intent(this, AppsListActivity::class.java)
|
|
|
|
|
var apps = ArrayList<AppData>(entries.size)
|
|
|
|
|
|
|
|
|
|
entries.forEach { entry ->
|
|
|
|
|
if (entry["Name"].isNullOrEmpty()) {
|
2020-05-19 04:53:12 +03:00
|
|
|
return@forEach
|
2020-05-17 05:54:01 +03:00
|
|
|
}
|
|
|
|
|
val id = entry["Title ID"]
|
|
|
|
|
val region = entry["Region"]
|
|
|
|
|
val name = entry["Name"]
|
|
|
|
|
val link = entry["PKG direct link"]
|
|
|
|
|
val license = entry["zRIF"]
|
|
|
|
|
val contentID = entry["Content ID"]
|
|
|
|
|
val lastDateTime = entry["Last Modification Date"]
|
|
|
|
|
val fileSize = entry["File Size"]?.toLongOrNull()
|
|
|
|
|
val sha256 = entry["SHA256"]
|
|
|
|
|
val minFW = entry["Required FW"]
|
|
|
|
|
var app = AppData(id, region, name, link ?: "MISSING", license ?: "MISSING",
|
|
|
|
|
contentID ?: "MISSING", lastDateTime ?: "MISSING",
|
2020-05-19 04:53:12 +03:00
|
|
|
fileSize ?: 0, sha256 ?: "MISSING", minFW)
|
2020-05-17 05:54:01 +03:00
|
|
|
apps.add(app)
|
|
|
|
|
}
|
2020-05-19 05:04:51 +03:00
|
|
|
apps.sortBy { it.title }
|
2020-05-19 04:53:12 +03:00
|
|
|
AppsListActivity.apps = apps
|
2020-05-17 05:54:01 +03:00
|
|
|
startActivity(myIntent)
|
2020-05-12 09:03:46 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun isStoragePermissionGranted(): Boolean {
|
|
|
|
|
return if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
Log.v("perms", "Permission is granted")
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
Log.v("perms", "Permission is revoked")
|
|
|
|
|
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 1)
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|