Skip to content

Commit

Permalink
Merge pull request #27 from Claudemirovsky/master
Browse files Browse the repository at this point in the history
Speed-up a operation with GZIP and add KtLint
  • Loading branch information
VegaBobo authored Aug 10, 2022
2 parents 99a4b35 + d21237e commit 8b08132
Show file tree
Hide file tree
Showing 20 changed files with 327 additions and 250 deletions.
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Editor configuration, see https://editorconfig.org
root = true

[*.kt]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
ij_kotlin_line_comment_at_first_column = false
ij_kotlin_line_comment_add_space = true
ij_kotlin_name_count_to_use_star_import = 2147483647
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
ij_kotlin_keep_blank_lines_in_declarations = 1
ij_kotlin_keep_blank_lines_in_code = 1
ij_kotlin_keep_blank_lines_before_right_brace = 0
ij_kotlin_align_multiline_parameters = false
ij_continuation_indent_size = 4
ij_kotlin_import_nested_classes = false
ij_kotlin_imports_layout = *,^
7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'org.jmailen.kotlinter'
}

android {
Expand Down Expand Up @@ -38,4 +39,8 @@ dependencies {
implementation 'androidx.fragment:fragment-ktx:1.4.1'
implementation 'androidx.preference:preference-ktx:1.2.0'
implementation 'com.google.android.material:material:1.6.1'
}
}

// remove this line to disable ktlint
preBuild.dependsOn(lintKotlin)
lintKotlin.dependsOn(formatKotlin)
3 changes: 1 addition & 2 deletions app/src/main/java/vegabobo/dsusideloader/AboutActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,4 @@ class AboutActivity : AppCompatActivity() {
fun btnGoogle(view: View) {
launchUrlIntent("https://developer.android.com/")
}

}
}
145 changes: 83 additions & 62 deletions app/src/main/java/vegabobo/dsusideloader/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.google.android.material.checkbox.MaterialCheckBox
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textview.MaterialTextView
import kotlin.math.roundToInt
import vegabobo.dsusideloader.checks.CompatibilityCheck
import vegabobo.dsusideloader.checks.OperationMode
import vegabobo.dsusideloader.dsuhelper.GsiDsuObject
Expand All @@ -28,7 +29,6 @@ import vegabobo.dsusideloader.util.FilenameUtils
import vegabobo.dsusideloader.util.SPUtils
import vegabobo.dsusideloader.util.SetupStorageAccess
import vegabobo.dsusideloader.util.WorkspaceFilesUtils
import kotlin.math.roundToInt

class HomeFragment : Fragment() {

Expand All @@ -38,13 +38,15 @@ class HomeFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

if (isEnvCompatible(true))
if (isEnvCompatible(true)) {
SetupStorageAccess(requireContext())
}

// gsid refuses to start installation when < 40% free storage
// prevent user from using app on this circumstances
if (!hasAvailableStorage())
if (!hasAvailableStorage()) {
showNoAvaiableStorageDialog()
}

gsiDsuObject.userdataSize = SPUtils.getUserdataSize(requireActivity())

Expand All @@ -59,7 +61,7 @@ class HomeFragment : Fragment() {
val tc = requireView().findViewById<MaterialTextView>(R.id.tv_defaultuserdata)
val txDebugBuildInfo = requireView().findViewById<MaterialTextView>(R.id.text_debugbuild)

if(BuildConfig.DEBUG) {
if (BuildConfig.DEBUG) {
txDebugBuildInfo.visibility = View.VISIBLE
txDebugBuildInfo.text = getString(R.string.debug_build_info, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE)
}
Expand Down Expand Up @@ -103,12 +105,12 @@ class HomeFragment : Fragment() {
btnIncrease.setOnClickListener {
gsiDsuObject.userdataSize++
edDSsize.setText(getString(R.string.gigabyte_holder, gsiDsuObject.userdataSize))

}

btnDecrease.setOnClickListener {
if (gsiDsuObject.userdataSize >= 2)
if (gsiDsuObject.userdataSize >= 2) {
gsiDsuObject.userdataSize--
}
edDSsize.setText(getString(R.string.gigabyte_holder, gsiDsuObject.userdataSize))
}

Expand Down Expand Up @@ -150,22 +152,28 @@ class HomeFragment : Fragment() {
}

btnInstall.setOnClickListener {

if (!cbGSIsize.isChecked) {
gsiDsuObject.fileSize = if (edGSIsize.toString().isNotEmpty()) {
edGSIsize.text.toString().toLong()
} else {
Toast.makeText(
activity,
getString(R.string.invalid_gsi_size, getString(R.string.auto)),
getString(
R.string.invalid_gsi_size,
getString(R.string.auto)
),
Toast.LENGTH_SHORT
).show()
-1
}
}

if (!cbDSsize.isChecked) {
gsiDsuObject.userdataSize = edDSsize.text.toString().split("GB")[0].toInt()
gsiDsuObject.userdataSize = edDSsize.text
.toString()
.split("GB")
.first()
.toInt()
}

beginInstall(selectedGsi, gsiDsuObject)
Expand All @@ -181,24 +189,27 @@ class HomeFragment : Fragment() {
)
}

if (selectedGsi != Uri.EMPTY)
if (selectedGsi != Uri.EMPTY) {
btnInstall.isEnabled = true

}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}


private fun checkDialog(title: String, text: String, finish: Boolean) {
MaterialAlertDialogBuilder(requireActivity())
.setTitle(title)
.setMessage(text)
.setPositiveButton(if (finish) getString(R.string.close_app) else getString(R.string.got_it)) { _, _ -> if (finish) requireActivity().finish() }
.setPositiveButton(
if (finish) getString(R.string.close_app)
else getString(R.string.got_it)
) { _, _ -> if (finish) requireActivity().finish() }
.setCancelable(false)
.show()
}
Expand All @@ -208,7 +219,9 @@ class HomeFragment : Fragment() {
val blockSize = statFs.blockSizeLong
val totalSize = statFs.blockCountLong * blockSize
val availableSize = statFs.availableBlocksLong * blockSize
return ((availableSize.toFloat() / totalSize.toFloat()) * 100).roundToInt() > 40
// free storage in percentage
val freeStorage = (availableSize.toFloat() / totalSize.toFloat()) * 100
return freeStorage.roundToInt() > 40
}

private fun isEnvCompatible(showDialogs: Boolean): Boolean {
Expand Down Expand Up @@ -277,60 +290,67 @@ class HomeFragment : Fragment() {
}

private fun beginInstall(selectedGsi: Uri, gsiDsuObject: GsiDsuObject) {

val selectedFile = FilenameUtils.queryName(requireActivity().contentResolver, selectedGsi)
val selectedFile = FilenameUtils.queryName(
requireActivity().contentResolver,
selectedGsi
)

// file need to have a extension, if not, show error dialog.
if (selectedFile.contains(".")) {

when (selectedFile.substring(selectedFile.lastIndexOf("."))) {
".xz", ".gz", ".img", ".zip" -> {

MaterialAlertDialogBuilder(requireActivity())
.setTitle(R.string.info)
.setMessage(getString(R.string.warning))
.setPositiveButton(getString(R.string.proceed)) { _, _ ->

MaterialAlertDialogBuilder(requireActivity())
.setTitle(getString(R.string.installation))
.setMessage(
getString(
R.string.installation_details,
selectedFile,
gsiDsuObject.userdataSize.toString(),
if (gsiDsuObject.fileSize == -1L) getString(R.string.auto) else gsiDsuObject.fileSize
)
)
.setPositiveButton(getString(R.string.proceed)) { _, _ ->
WorkspaceFilesUtils.cleanWorkspaceFolder(
requireActivity(),
true
)
Thread(
PrepareDsu(
requireActivity(),
selectedGsi,
gsiDsuObject
)
).start()
}
.setNegativeButton(getString(R.string.cancel), null)
.setCancelable(true)
.show()

}
.setNegativeButton(getString(R.string.cancel), null)
.show()
}
else -> {
showUnsupportedDialog()
when (selectedFile.substringAfterLast(".")) {
"xz", "gz", "img", "zip" -> {
beginInstallDialog(selectedFile, selectedGsi, gsiDsuObject)
}
else -> showUnsupportedDialog()
}
} else {
showUnsupportedDialog()
}
}

private fun beginInstallDialog(
selectedFile: String,
selectedGsi: Uri,
gsiDsuObject: GsiDsuObject
) {
MaterialAlertDialogBuilder(requireActivity())
.setTitle(R.string.info)
.setMessage(getString(R.string.warning))
.setPositiveButton(getString(R.string.proceed)) { _, _ ->

MaterialAlertDialogBuilder(requireActivity())
.setTitle(getString(R.string.installation))
.setMessage(
getString(
R.string.installation_details,
selectedFile,
gsiDsuObject.userdataSize.toString(),
if (gsiDsuObject.fileSize == -1L) {
getString(R.string.auto)
} else gsiDsuObject.fileSize
)
)
.setPositiveButton(getString(R.string.proceed)) { _, _ ->
WorkspaceFilesUtils.cleanWorkspaceFolder(
requireActivity(),
true
)
Thread(
PrepareDsu(
requireActivity(),
selectedGsi,
gsiDsuObject
)
).start()
}
.setNegativeButton(getString(R.string.cancel), null)
.setCancelable(true)
.show()
}
.setNegativeButton(getString(R.string.cancel), null)
.show()
}

private fun showUnsupportedDialog() {
MaterialAlertDialogBuilder(requireActivity())
.setTitle(R.string.unsupported)
Expand All @@ -344,9 +364,10 @@ class HomeFragment : Fragment() {
MaterialAlertDialogBuilder(requireActivity())
.setTitle(R.string.error)
.setMessage(getString(R.string.storage_warning))
.setPositiveButton(getString(R.string.close_app)) { _, _ -> requireActivity().finish() }
.setPositiveButton(getString(R.string.close_app)) { _, _ ->
requireActivity().finish()
}
.setCancelable(false)
.show()
}

}
}
9 changes: 4 additions & 5 deletions app/src/main/java/vegabobo/dsusideloader/LogsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ class LogsActivity : AppCompatActivity() {
override fun onAddElement(s: String?) {
entireLogcat = entireLogcat + s + "\n"
if (logLines != -1 && logLines < 200) {
tvLog.append("\n"+s)
tvLog.append("\n" + s)
logLines++
} else {
if(logLines!=-1)
if (logLines != -1) {
tvLog.text = "Logs are too big, export to see everything.\n\n${tvLog.text}"
}
logLines = -1
}
}
Expand All @@ -68,7 +69,6 @@ class LogsActivity : AppCompatActivity() {
intent.putExtra(Intent.EXTRA_TITLE, "logs")
resultLauncher.launch(intent)
}

}

override fun onBackPressed() {
Expand All @@ -93,5 +93,4 @@ class LogsActivity : AppCompatActivity() {
Toast.makeText(this@LogsActivity, getString(R.string.error), Toast.LENGTH_SHORT).show()
}
}

}
}
5 changes: 0 additions & 5 deletions app/src/main/java/vegabobo/dsusideloader/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationView


class MainActivity : AppCompatActivity() {

private var fragment1: Fragment = HomeFragment()
Expand Down Expand Up @@ -35,7 +34,6 @@ class MainActivity : AppCompatActivity() {
}
}


bottomNav.setOnItemSelectedListener { item ->
when (item.itemId) {
R.id.tab_home -> {
Expand All @@ -51,8 +49,5 @@ class MainActivity : AppCompatActivity() {
}
true
}

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import androidx.preference.PreferenceFragmentCompat
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import vegabobo.dsusideloader.checks.OperationMode


class PreferencesFragment : PreferenceFragmentCompat() {

private var OP_MODE = ""
Expand All @@ -33,7 +32,6 @@ class PreferencesFragment : PreferenceFragmentCompat() {
true
}
}

}

private fun dialogInfo(message: String) {
Expand Down Expand Up @@ -73,5 +71,4 @@ class PreferencesFragment : PreferenceFragmentCompat() {
}
}
}

}
}
Loading

0 comments on commit 8b08132

Please sign in to comment.