Skip to content

Commit

Permalink
2.3_04
Browse files Browse the repository at this point in the history
  • Loading branch information
queuejw committed Jan 23, 2024
1 parent 6c9b761 commit 3f8b867
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
applicationId "ru.dimon6018.neko11"
minSdk 23
targetSdk = 34
versionCode 6
versionName '2.3_03'
versionCode 7
versionName '2.3_04'
multiDexEnabled false
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Binary file added app/src/main/assets/music/music1.mp3
Binary file not shown.
20 changes: 20 additions & 0 deletions app/src/main/java/ru/dimon6018/neko11/NekoApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package ru.dimon6018.neko11
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.content.res.Resources
import android.media.AudioAttributes
import android.media.MediaPlayer
import ru.dimon6018.neko11.ui.activities.NekoSettingsActivity
import ru.dimon6018.neko11.workers.Cat
import ru.dimon6018.neko11.workers.ExceptionHandler
Expand Down Expand Up @@ -82,5 +85,22 @@ class NekoApplication : Application() {
android.R.color.white
}
}
fun playMusic(mPrefs: PrefState, resources: Resources) {
if(NekoGeneralActivity.mediaPlayer == null) {
NekoGeneralActivity.mediaPlayer = MediaPlayer()
}
val afd = resources.assets.openFd("music/music1.mp3")
NekoGeneralActivity.mediaPlayer!!.isLooping = true
NekoGeneralActivity.mediaPlayer!!.setDataSource(afd.fileDescriptor, afd.startOffset, afd.getLength())
NekoGeneralActivity.mediaPlayer!!.setAudioAttributes(
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build())
if(mPrefs.isMusicEnabled()) {
NekoGeneralActivity.mediaPlayer!!.prepare()
NekoGeneralActivity.mediaPlayer!!.start()
NekoGeneralActivity.isMusicPlaying = true
}
}
}
}
38 changes: 31 additions & 7 deletions app/src/main/java/ru/dimon6018/neko11/NekoGeneralActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.MediaPlayer
import android.os.Build
import android.os.Bundle
import android.view.ContextThemeWrapper
Expand All @@ -48,6 +51,9 @@ import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.elevation.SurfaceColors
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import ru.dimon6018.neko11.activation.NekoActivationActivity
import ru.dimon6018.neko11.controls.CatControlsFragment
import ru.dimon6018.neko11.controls.CatControlsFragment.Companion.showTipAgain
Expand All @@ -60,6 +66,7 @@ import ru.dimon6018.neko11.workers.NekoWorker
import ru.dimon6018.neko11.workers.PrefState
import ru.dimon6018.neko11.workers.PrefState.PrefsListener


class NekoGeneralActivity : AppCompatActivity(), PrefsListener {

private var nekoprefs: SharedPreferences? = null
Expand Down Expand Up @@ -101,7 +108,9 @@ class NekoGeneralActivity : AppCompatActivity(), PrefsListener {
if (supportActionBar != null) {
supportActionBar!!.setDisplayUseLogoEnabled(true)
}
Thread {
WindowCompat.setDecorFitsSystemWindows(window, false)
window!!.navigationBarColor = SurfaceColors.SURFACE_2.getColor(this)
CoroutineScope(Dispatchers.Default).launch {
if (mPrefs!!.backgroundPath != "") {
try {
val bmp = BitmapFactory.decodeFile(mPrefs!!.backgroundPath)
Expand All @@ -113,10 +122,8 @@ class NekoGeneralActivity : AppCompatActivity(), PrefsListener {
showSnackBar(ex.toString(), Snackbar.LENGTH_LONG, cord)
}
}
WindowCompat.setDecorFitsSystemWindows(window, false)
window!!.navigationBarColor = SurfaceColors.SURFACE_2.getColor(this)
}
}.start()
}
if (!nekoprefs!!.getBoolean("controlsFirst", false)) {
viewPager!!.currentItem = 0
navbar?.selectedItemId = R.id.collection
Expand All @@ -135,6 +142,20 @@ class NekoGeneralActivity : AppCompatActivity(), PrefsListener {
}
}
})
mediaPlayer = MediaPlayer()
if(mPrefs!!.isMusicEnabled()) {
val afd = getResources().assets.openFd("music/music1.mp3")
mediaPlayer!!.isLooping = true
mediaPlayer!!.setDataSource(afd.fileDescriptor, afd.startOffset, afd.getLength())
mediaPlayer!!.setAudioAttributes(
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build())
mediaPlayer!!.prepare()
mediaPlayer!!.start()
isMusicPlaying = true

}
}
private fun getAndroidV(): Boolean {
return (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1)
Expand Down Expand Up @@ -188,20 +209,21 @@ class NekoGeneralActivity : AppCompatActivity(), PrefsListener {
else -> return super.onOptionsItemSelected(item)
}
}

public override fun onDestroy() {
mPrefs!!.setListener(null)
super.onDestroy()
if(mPrefs!!.isMusicEnabled()) {
isMusicPlaying = false
mediaPlayer?.release()
}
}

public override fun onPause() {
super.onPause()
}

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

private fun setupDarkMode() {
when (nekoprefs!!.getInt("darktheme", 0)) {
1 -> {
Expand Down Expand Up @@ -415,6 +437,8 @@ class NekoGeneralActivity : AppCompatActivity(), PrefsListener {

companion object {
private const val DEBUG = false
var isMusicPlaying = false
var mediaPlayer: MediaPlayer? = null

@JvmStatic
fun showSnackBar(text: String?, time: Int, view: View?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ import com.google.android.material.textview.MaterialTextView
import org.xmlpull.v1.XmlPullParserException
import ru.dimon6018.neko11.BuildConfig
import ru.dimon6018.neko11.NekoApplication.Companion.getNekoTheme
import ru.dimon6018.neko11.NekoApplication.Companion.playMusic
import ru.dimon6018.neko11.NekoGeneralActivity
import ru.dimon6018.neko11.NekoGeneralActivity.Companion.isMusicPlaying
import ru.dimon6018.neko11.NekoGeneralActivity.Companion.mediaPlayer
import ru.dimon6018.neko11.NekoGeneralActivity.Companion.showSnackBar
import ru.dimon6018.neko11.R
import ru.dimon6018.neko11.workers.BackupParser
Expand Down Expand Up @@ -85,6 +89,7 @@ class NekoSettingsActivity : AppCompatActivity(), PrefsListener {
private var autowhiteswitch: MaterialSwitch? = null
private var controlsFirst: MaterialSwitch? = null
private var allowCatRun: MaterialSwitch? = null
private var musicSwitch: MaterialSwitch? = null
private var textMatch: MaterialSwitch? = null
private var legacyControls: MaterialSwitch? = null
private var sortGroup: MaterialButtonToggleGroup? = null
Expand Down Expand Up @@ -257,6 +262,15 @@ class NekoSettingsActivity : AppCompatActivity(), PrefsListener {
editor.putBoolean("controlsFirst", isChecked)
editor.apply()
}
musicSwitch!!.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean ->
mPrefs!!.setMusic(isChecked)
if(!isChecked) {
mediaPlayer?.release()
isMusicPlaying = false
} else {
restart()
}
}
limitSlider!!.addOnChangeListener(Slider.OnChangeListener { _: Slider?, value: Float, _: Boolean ->
val valueNew = Math.round(value)
mPrefs!!.catsInLineLimit = valueNew
Expand Down Expand Up @@ -596,7 +610,11 @@ class NekoSettingsActivity : AppCompatActivity(), PrefsListener {
}
popup.show()
}

private fun restart() {
val intent = Intent(this, NekoGeneralActivity::class.java)
this.startActivity(intent)
finishAffinity()
}
@SuppressLint("SuspiciousIndentation")
private fun setupScreen() {
opensettingsbtn = findViewById(R.id.opensettingsbtn)
Expand All @@ -623,6 +641,7 @@ class NekoSettingsActivity : AppCompatActivity(), PrefsListener {
removeBack = findViewById(R.id.remove_background)
textMatch = findViewById(R.id.text_matches_theme)
legacyControls = findViewById(R.id.legacy_controls)
musicSwitch = findViewById(R.id.musicController)
val theme = nekoprefs!!.getInt("theme", 0)
val darkenabled = nekoprefs!!.getInt("darktheme", 0)
val isControlsLinear = nekoprefs!!.getBoolean("linear_control", false)
Expand All @@ -639,6 +658,7 @@ class NekoSettingsActivity : AppCompatActivity(), PrefsListener {
dyncolor!!.isChecked = theme == 8
legacyControls!!.isChecked = legacyGameplay
controlsFirst!!.isChecked = isControlsFirst
musicSwitch!!.isChecked = mPrefs!!.isMusicEnabled()
allowCatRun!!.isChecked = isCatCanRun
textMatch!!.isChecked = coloredText
limitSlider!!.value = mPrefs!!.catsInLineLimit.toFloat()
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/ru/dimon6018/neko11/workers/PrefState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ class PrefState(private val mContext: Context) : OnSharedPreferenceChangeListene
fun isDialogEnabled(): Boolean {
return mPrefs.getBoolean(CAT_DIALOG, false)
}
fun setMusic(bool: Boolean) {
mPrefs.edit().putBoolean(MUSIC, bool).apply()
}
fun isMusicEnabled(): Boolean {
return mPrefs.getBoolean(MUSIC, true)
}

companion object {
const val FILE_NAME = "mPrefs"
Expand All @@ -309,6 +315,7 @@ class PrefState(private val mContext: Context) : OnSharedPreferenceChangeListene
const val ICON_SIZE = "size"
const val ICON_BACKGROUND = "background"
const val CAT_DIALOG = "catDialogEnabled"
const val MUSIC = "musicEnabled"
private const val NCOINS = "nCoins"
const val TOY_STATE = "toy"
const val WATER_STATE = "water"
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/neko_settings_content.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@
android:textSize="12sp"
app:fontFamily="@font/opensans_regular" />

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/musicController"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:text="Включать музыку"
android:textSize="18sp" />

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/linear_controls_enable"
android:layout_width="match_parent"
Expand Down

0 comments on commit 3f8b867

Please sign in to comment.