Skip to content

Commit

Permalink
Minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiascode committed Mar 23, 2024
1 parent 94dc143 commit cf456d6
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 49 deletions.
13 changes: 6 additions & 7 deletions app/src/main/kotlin/org/cuberite/android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import org.cuberite.android.fragments.ControlFragment
import org.cuberite.android.fragments.SettingsFragment

class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListener {
// Logging tag
private val log = "Cuberite/MainActivity"
private var permissionPopup: AlertDialog? = null

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -75,10 +73,10 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen

private val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
if (isGranted) {
Log.i(log, "Got permissions, using public directory")
Log.i(LOG, "Got permissions, using public directory")
MainApplication.preferences.edit().putString("cuberiteLocation", "$MainApplication.publicDir/cuberite-server").apply()
} else {
Log.i(log, "Permissions denied, boo, using private directory")
Log.i(LOG, "Permissions denied, boo, using private directory")
MainApplication.preferences.edit().putString("cuberiteLocation", "$MainApplication.privateDir/cuberite-server").apply()
}
}
Expand All @@ -89,7 +87,7 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
.setMessage(R.string.message_externalstorage_permission)
.setCancelable(false)
.setPositiveButton(R.string.ok) { _: DialogInterface?, _: Int ->
Log.d(log, "Requesting permissions for external storage")
Log.d(LOG, "Requesting permissions for external storage")
permissionPopup = null
requestPermissionLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
Expand All @@ -112,20 +110,21 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
}
}

public override fun onPause() {
override fun onPause() {
super.onPause()
permissionPopup?.let {
permissionPopup!!.dismiss()
permissionPopup = null
}
}

public override fun onResume() {
override fun onResume() {
super.onResume()
checkPermissions()
}

companion object {
private const val LOG = "Cuberite/MainActivity"
val executeCommandLiveData = MutableLiveData<String?>()
val killCuberiteLiveData = MutableLiveData<Boolean>()
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/kotlin/org/cuberite/android/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.os.Environment
import android.os.Parcelable
import androidx.appcompat.app.AppCompatDelegate
import com.google.android.material.color.DynamicColors
import org.ini4j.Config
import java.io.Serializable

class MainApplication : Application() {
Expand All @@ -22,6 +23,11 @@ class MainApplication : Application() {
privateDir = filesDir.absolutePath
publicDir = Environment.getExternalStorageDirectory().absolutePath

// Ini4j config
val config = Config.getGlobal()
config.isEscape = false
config.isStrictOperator = true

// Application theme
AppCompatDelegate.setDefaultNightMode(preferences.getInt("defaultTheme", AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM))
DynamicColors.applyToActivitiesIfAvailable(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ class ConsoleFragment : Fragment() {

private fun sendExecuteCommand(command: String) {
if (command.isNotEmpty() && CuberiteService.isRunning) {
// Logging tag
val log = "Cuberite/Console"
Log.d(log, "Executing $command")
Log.d(LOG, "Executing $command")
MainActivity.executeCommandLiveData.postValue(command)
}
}
Expand Down Expand Up @@ -108,4 +106,8 @@ class ConsoleFragment : Fragment() {
}
}
}

companion object {
private const val LOG = "Cuberite/Console"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import org.cuberite.android.services.CuberiteService
import org.cuberite.android.services.InstallService

class ControlFragment : Fragment() {
// Logging tag
private val log = "Cuberite/Control"
private var mainButtonColor = 0
private lateinit var mainButton: Button

Expand All @@ -37,7 +35,7 @@ class ControlFragment : Fragment() {
if (!show) {
return@observe
}
Log.d(log, "Cuberite exited on process")
Log.d(LOG, "Cuberite exited on process")
val message = String.format(
getString(R.string.status_failed_start),
CuberiteService.preferredABI
Expand Down Expand Up @@ -67,7 +65,7 @@ class ControlFragment : Fragment() {
}

private fun animateColorChange(button: Button, colorFrom: Int, colorTo: Int) {
Log.d(log, "Changing color from " + Integer.toHexString(colorFrom) + " to " + Integer.toHexString(colorTo))
Log.d(LOG, "Changing color from " + Integer.toHexString(colorFrom) + " to " + Integer.toHexString(colorTo))
val colorAnimation = ValueAnimator.ofObject(ArgbEvaluator(), colorFrom, colorTo)
colorAnimation.setDuration(300)
colorAnimation.addUpdateListener { animator: ValueAnimator -> button.setBackgroundColor(animator.getAnimatedValue() as Int) }
Expand Down Expand Up @@ -120,4 +118,8 @@ class ControlFragment : Fragment() {
setInstallButton()
}
}

companion object {
private const val LOG = "Cuberite/Control"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,14 @@ import org.cuberite.android.MainApplication
import org.cuberite.android.R
import org.cuberite.android.services.CuberiteService
import org.cuberite.android.services.InstallService
import org.ini4j.Config
import org.ini4j.Ini
import java.io.File
import java.io.IOException

class SettingsFragment : PreferenceFragmentCompat() {
// Logging tag
private val log = "Cuberite/Settings"

override fun onCreatePreferences(bundle: Bundle?, s: String?) {
addPreferencesFromResource(R.xml.preferences)

// Ini4j config
val config = Config.getGlobal()
config.isEscape = false
config.isStrictOperator = true

// Initialize
initializeThemeSettings()
initializeStartupSettings()
Expand Down Expand Up @@ -119,7 +110,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
if (!(isSDAvailable || isSDEnabled)) {
return
}
Log.d(log, "SD Card found or location set, showing preference")
Log.d(LOG, "SD Card found or location set, showing preference")
toggleSD!!.isVisible = true
toggleSD.setChecked(isSDEnabled)
toggleSD.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any ->
Expand Down Expand Up @@ -173,7 +164,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
val webadminFileInner = getWebadminFile()
val urlInner = getWebadminUrl(webadminFileInner)
urlInner?.let {
Log.d(log, "Opening Webadmin on $urlInner")
Log.d(LOG, "Opening Webadmin on $urlInner")
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(urlInner))
startActivity(browserIntent)
}
Expand All @@ -187,7 +178,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
ini.put("WebAdmin", "Enabled", 1)
showWebadminCredentialPopup(webadminFileInner, ini)
} catch (e: IOException) {
Log.e(log, "Something went wrong while opening the ini file", e)
Log.e(LOG, "Something went wrong while opening the ini file", e)
val message = getString(R.string.settings_webadmin_error)
Snackbar.make(requireActivity().findViewById(R.id.fragment_container), message, Snackbar.LENGTH_LONG)
.setAnchorView(requireActivity().findViewById(R.id.bottom_navigation))
Expand Down Expand Up @@ -230,7 +221,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
url = "http://$ip:$port"
} catch (e: IOException) {
Log.e(log, "Something went wrong while opening the ini file", e)
Log.e(LOG, "Something went wrong while opening the ini file", e)
}
return url
}
Expand Down Expand Up @@ -263,7 +254,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
.setAnchorView(requireActivity().findViewById(R.id.bottom_navigation))
.show()
} catch (e: IOException) {
Log.e(log, "Something went wrong while saving the ini file", e)
Log.e(LOG, "Something went wrong while saving the ini file", e)
val message = getString(R.string.settings_webadmin_error)
Snackbar.make(requireActivity().findViewById(R.id.fragment_container), message, Snackbar.LENGTH_LONG)
.setAnchorView(requireActivity().findViewById(R.id.bottom_navigation))
Expand Down Expand Up @@ -364,4 +355,8 @@ Download URL: ${InstallService.DOWNLOAD_HOST}"""
.setPositiveButton(R.string.ok) { dialog1: DialogInterface?, _: Int -> dialog1?.dismiss() }
.show()
}

companion object {
private const val LOG = "Cuberite/Settings"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ProgressReceiver(private val cont: Context, handler: Handler?) : ResultRec
.create()
}

public override fun onReceiveResult(resultCode: Int, resultData: Bundle?) {
override fun onReceiveResult(resultCode: Int, resultData: Bundle?) {
super.onReceiveResult(resultCode, resultData)
when (resultCode) {
PROGRESS_START -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,12 @@ class CuberiteService : IntentService("CuberiteService") {
}

companion object {
private const val LOG = "Cuberite/ServerService"
const val EXECUTABLE_NAME = "Cuberite"
var isRunning = false
val endedLiveData = MutableLiveData<Boolean>()
val startupErrorLiveData = MutableLiveData<Boolean>()
val updateLogLiveData = MutableLiveData<StringBuilder>()
const val EXECUTABLE_NAME = "Cuberite"

// Logging tag
private const val LOG = "Cuberite/ServerService"

val ipAddress: String
get() {
Expand Down
27 changes: 11 additions & 16 deletions app/src/main/kotlin/org/cuberite/android/services/InstallService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -282,29 +282,24 @@ class InstallService : IntentService("InstallService") {
}

companion object {
val endedLiveData = MutableLiveData<String?>()
const val DOWNLOAD_HOST = "https://download.cuberite.org/androidbinaries/"

// Logging tag
private const val LOG = "Cuberite/InstallService"
const val DOWNLOAD_HOST = "https://download.cuberite.org/androidbinaries/"
val endedLiveData = MutableLiveData<String?>()

private val state: State
get() {
var state: State = State.DONE

if (CuberiteService.isRunning) {
return state
}

val hasBinary = File(MainApplication.privateDir + "/" + CuberiteService.EXECUTABLE_NAME).exists()
val hasServer = File(MainApplication.preferences.getString("cuberiteLocation", "")!!).exists()
var state: State = State.DONE

if (!hasBinary && !hasServer) {
state = State.NEED_DOWNLOAD_BOTH
} else if (!hasBinary) {
state = State.NEED_DOWNLOAD_BINARY
} else if (!hasServer) {
state = State.NEED_DOWNLOAD_SERVER
if (!CuberiteService.isRunning) {
if (!hasBinary && !hasServer) {
state = State.NEED_DOWNLOAD_BOTH
} else if (!hasBinary) {
state = State.NEED_DOWNLOAD_BINARY
} else if (!hasServer) {
state = State.NEED_DOWNLOAD_SERVER
}
}
Log.d(LOG, "Getting State: $state")
return state
Expand Down

0 comments on commit cf456d6

Please sign in to comment.