Skip to content

Commit

Permalink
Remove redundant helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiascode committed Mar 23, 2024
1 parent b1499a5 commit 94dc143
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 293 deletions.
24 changes: 7 additions & 17 deletions app/src/main/kotlin/org/cuberite/android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package org.cuberite.android

import android.Manifest
import android.content.DialogInterface
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.view.MenuItem
import androidx.activity.result.contract.ActivityResultContracts
Expand All @@ -27,9 +25,6 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
// Logging tag
private val log = "Cuberite/MainActivity"
private var permissionPopup: AlertDialog? = null
private lateinit var preferences: SharedPreferences
private lateinit var privateDir: String
private lateinit var publicDir: String

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -49,11 +44,6 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
// Set navigation bar listener
val navigation: BottomNavigationView = findViewById(R.id.bottom_navigation)
navigation.setOnItemSelectedListener(this)

// Initialize settings
preferences = getSharedPreferences(this.packageName, MODE_PRIVATE)
privateDir = this.filesDir.absolutePath
publicDir = Environment.getExternalStorageDirectory().absolutePath
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
Expand Down Expand Up @@ -86,10 +76,10 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
private val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
if (isGranted) {
Log.i(log, "Got permissions, using public directory")
preferences.edit().putString("cuberiteLocation", "$publicDir/cuberite-server").apply()
MainApplication.preferences.edit().putString("cuberiteLocation", "$MainApplication.publicDir/cuberite-server").apply()
} else {
Log.i(log, "Permissions denied, boo, using private directory")
preferences.edit().putString("cuberiteLocation", "$privateDir/cuberite-server").apply()
MainApplication.preferences.edit().putString("cuberiteLocation", "$MainApplication.privateDir/cuberite-server").apply()
}
}

Expand All @@ -108,16 +98,16 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
}

private fun checkPermissions() {
val location = preferences.getString("cuberiteLocation", "")
val location = MainApplication.preferences.getString("cuberiteLocation", "")
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// User is running Android 6 or above, show permission popup on first run
// or if user granted permission and later denied it
if (location!!.isEmpty() || location.startsWith(publicDir)) {
if (location!!.isEmpty() || location.startsWith(MainApplication.publicDir)) {
showPermissionPopup()
}
} else if (location!!.isEmpty() || location.startsWith(privateDir)) {
val editor = preferences.edit()
editor.putString("cuberiteLocation", "$publicDir/cuberite-server")
} else if (location!!.isEmpty() || location.startsWith(MainApplication.privateDir)) {
val editor = MainApplication.preferences.edit()
editor.putString("cuberiteLocation", "$MainApplication.publicDir/cuberite-server")
editor.apply()
}
}
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/kotlin/org/cuberite/android/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Intent
import android.content.SharedPreferences
import android.os.Build
import android.os.Build.VERSION_CODES.TIRAMISU
import android.os.Environment
import android.os.Parcelable
import androidx.appcompat.app.AppCompatDelegate
import com.google.android.material.color.DynamicColors
Expand All @@ -15,8 +17,12 @@ class MainApplication : Application() {
override fun onCreate() {
super.onCreate()

// Initialize settings
preferences = getSharedPreferences(packageName, MODE_PRIVATE)
privateDir = filesDir.absolutePath
publicDir = Environment.getExternalStorageDirectory().absolutePath

// Application theme
val preferences = getSharedPreferences(this.packageName, MODE_PRIVATE)
AppCompatDelegate.setDefaultNightMode(preferences.getInt("defaultTheme", AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM))
DynamicColors.applyToActivitiesIfAvailable(this)

Expand All @@ -41,6 +47,12 @@ class MainApplication : Application() {
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}

companion object {
lateinit var preferences: SharedPreferences
lateinit var privateDir: String
lateinit var publicDir: String
}
}

inline fun <reified T : Parcelable> Intent.parcelable(key: String): T? = when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.google.android.material.color.MaterialColors
import com.google.android.material.textfield.TextInputLayout
import org.cuberite.android.MainActivity
import org.cuberite.android.R
import org.cuberite.android.helpers.CuberiteHelper
import org.cuberite.android.services.CuberiteService

class ConsoleFragment : Fragment() {
Expand Down Expand Up @@ -58,8 +57,7 @@ class ConsoleFragment : Fragment() {
}

private fun sendExecuteCommand(command: String) {
if (command.isNotEmpty()
&& CuberiteHelper.isCuberiteRunning(requireActivity())) {
if (command.isNotEmpty() && CuberiteService.isRunning) {
// Logging tag
val log = "Cuberite/Console"
Log.d(log, "Executing $command")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import androidx.fragment.app.Fragment
import com.google.android.material.color.MaterialColors
import com.google.android.material.snackbar.Snackbar
import org.cuberite.android.R
import org.cuberite.android.helpers.CuberiteHelper
import org.cuberite.android.helpers.InstallHelper
import org.cuberite.android.helpers.StateHelper
import org.cuberite.android.services.CuberiteService
import org.cuberite.android.services.InstallService

Expand Down Expand Up @@ -43,7 +40,7 @@ class ControlFragment : Fragment() {
Log.d(log, "Cuberite exited on process")
val message = String.format(
getString(R.string.status_failed_start),
CuberiteHelper.preferredABI
CuberiteService.preferredABI
)
Snackbar.make(
requireActivity().findViewById(R.id.fragment_container),
Expand Down Expand Up @@ -78,12 +75,12 @@ class ControlFragment : Fragment() {
mainButtonColor = colorTo
}

private fun setInstallButton(state: StateHelper.State?) {
private fun setInstallButton() {
val colorTo = MaterialColors.getColor(mainButton, com.google.android.material.R.attr.colorPrimary)
animateColorChange(mainButton, mainButtonColor, colorTo)
mainButton.text = getText(R.string.do_install_cuberite)
mainButton.setOnClickListener {
InstallHelper.installCuberiteDownload(requireActivity(), state)
InstallService.download(requireActivity())
}
}

Expand All @@ -92,7 +89,7 @@ class ControlFragment : Fragment() {
animateColorChange(mainButton, mainButtonColor, colorTo)
mainButton.text = getText(R.string.do_start_cuberite)
mainButton.setOnClickListener {
CuberiteHelper.startCuberite(requireContext())
CuberiteService.start(requireActivity())
setStopButton()
}
}
Expand All @@ -102,7 +99,7 @@ class ControlFragment : Fragment() {
animateColorChange(mainButton, mainButtonColor, colorTo)
mainButton.text = getText(R.string.do_stop_cuberite)
mainButton.setOnClickListener {
CuberiteHelper.stopCuberite()
CuberiteService.stop()
setKillButton()
}
}
Expand All @@ -111,20 +108,16 @@ class ControlFragment : Fragment() {
val colorTo = MaterialColors.getColor(mainButton, com.google.android.material.R.attr.colorError)
animateColorChange(mainButton, mainButtonColor, colorTo)
mainButton.text = getText(R.string.do_kill_cuberite)
mainButton.setOnClickListener { CuberiteHelper.killCuberite() }
mainButton.setOnClickListener { CuberiteService.kill() }
}

private fun updateControlButton() {
when (val state = StateHelper.getState(requireContext())) {
StateHelper.State.RUNNING -> {
setStopButton()
}
StateHelper.State.READY -> {
setStartButton()
}
else -> {
setInstallButton(state)
}
if (CuberiteService.isRunning) {
setStopButton()
} else if (InstallService.isInstalled) {
setStartButton()
} else {
setInstallButton()
}
}
}
Loading

0 comments on commit 94dc143

Please sign in to comment.