Skip to content

Commit

Permalink
sounds (Tested on R)
Browse files Browse the repository at this point in the history
  • Loading branch information
AkosPaha01 committed Jul 27, 2021
1 parent 1a1baf4 commit 1deadb3
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 36 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ android {
applicationId "de.dertyp7214.rboardthememanager"
minSdkVersion 26
targetSdkVersion 31
versionCode 324001
versionCode 324002
versionName "3.2.4"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.dertyp7214.rboardthememanager.adapter

import android.app.Activity
import android.media.MediaPlayer
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -9,10 +10,16 @@ import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.topjohnwu.superuser.io.SuFile
import de.dertyp7214.rboardthememanager.Config
import de.dertyp7214.rboardthememanager.R
import de.dertyp7214.rboardthememanager.core.copyRecursively
import de.dertyp7214.rboardthememanager.core.download
import de.dertyp7214.rboardthememanager.core.openDialog
import de.dertyp7214.rboardthememanager.data.SoundPack
import de.dertyp7214.rboardthememanager.utils.getSoundsDirectory
import java.io.File
import java.io.FileInputStream

class SoundPackAdapter(
private val list: List<SoundPack>,
Expand All @@ -30,48 +37,83 @@ class SoundPackAdapter(
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = list[position]
val soundPack = list[position]

holder.title.text = item.title
holder.author.text = item.author
holder.title.text = soundPack.title
holder.author.text = soundPack.author

holder.image.setImageResource(R.drawable.ic_sounds)

holder.root.setOnClickListener {
TODO("DOWNLOAD SOUNDS")
val sounds = arrayListOf<File>()
activity.openDialog(R.layout.soundpack_dialog) { dialog ->
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
soundPack.download(activity) { sounds ->
activity.openDialog(R.layout.soundpack_dialog) { dialog ->
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)

recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(activity)
recyclerView.adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ViewHolder {
return ViewHolder(TextView(activity))
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val sound = sounds[position]
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(activity)
recyclerView.adapter = SoundAdapter(sounds, activity)

holder.itemView.let {
if (it is TextView) it.text = sound.name
findViewById<Button>(R.id.ok)?.setOnClickListener {
getSoundsDirectory()?.path?.let { path ->
SuFile("${Config.MODULE_PATH}$path/audio/ui").apply {
deleteRecursive()
SuFile(sounds.first()).parentFile
?.copyRecursively(this)
}
}
dialog.dismiss()
}
findViewById<Button>(R.id.cancel)?.setOnClickListener { dialog.dismiss() }
}
}
}
}

override fun getItemCount(): Int = sounds.size
override fun getItemCount(): Int = list.size

}
private class SoundAdapter(
private val sounds: List<String>,
private val activity: Activity
) :
RecyclerView.Adapter<SoundAdapter.ViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ViewHolder {
return ViewHolder(
LayoutInflater.from(activity)
.inflate(android.R.layout.simple_list_item_1, parent, false)
)
}

findViewById<Button>(R.id.ok)?.setOnClickListener {
dialog.dismiss()
class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
val textView = v as TextView
}

override fun onBindViewHolder(
holder: ViewHolder,
position: Int
) {
val sound = sounds[position]
val file = File(sound)

holder.textView.text = file.name
holder.textView.setOnClickListener {
MediaPlayer().apply {
try {
setDataSource(FileInputStream(file).fd)
prepare()
start()
setOnCompletionListener {
release()
}
} catch (e: Exception) {
release()
}
}
findViewById<Button>(R.id.cancel)?.setOnClickListener { dialog.dismiss() }
}
}
}

override fun getItemCount(): Int = list.size
override fun getItemCount(): Int = sounds.size
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import android.annotation.SuppressLint
import android.app.Activity
import android.content.DialogInterface
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.RenderEffect
import android.graphics.Shader
import android.net.Uri
import android.os.Build
import android.view.View
import android.widget.Button
import android.widget.EditText
Expand All @@ -15,14 +19,24 @@ import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.preference.PreferenceManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import de.dertyp7214.rboardthememanager.R

val Activity.preferences: SharedPreferences
get() = PreferenceManager.getDefaultSharedPreferences(this)

inline val Activity.content: View
get() {
return findViewById(android.R.id.content)
}

inline val Activity.enableBlur: Boolean
get() {
return preferences.getBoolean("useBlur", true)
}


operator fun <T : ViewModel> FragmentActivity.get(modelClass: Class<T>): T =
run(::ViewModelProvider)[modelClass]

Expand All @@ -39,12 +53,25 @@ fun Activity.openDialog(
negative: ((dialogInterface: DialogInterface) -> Unit)? = { it.dismiss() },
positive: (dialogInterface: DialogInterface) -> Unit
): AlertDialog {
if (enableBlur) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
content.setRenderEffect(
RenderEffect.createBlurEffect(
10F,
10F,
Shader.TileMode.REPEAT
)
)
}
return MaterialAlertDialogBuilder(this)
.setCancelable(cancelable)
.setCancelable(false)
.setMessage(message)
.setTitle(title)
.setPositiveButton(positiveText) { dialogInterface, _ -> positive(dialogInterface) }
.setOnDismissListener { if (enableBlur) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
content.setRenderEffect(null)
}
}
.apply {
if (negative != null) setNegativeButton(negativeText) { dialogInterface, _ -> negative.invoke(
dialogInterface
Expand Down Expand Up @@ -142,10 +169,23 @@ fun Activity.openDialog(
cancelable: Boolean = true,
block: View.(DialogInterface) -> Unit
): AlertDialog {
if (enableBlur) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
content.setRenderEffect(
RenderEffect.createBlurEffect(
10F,
10F,
Shader.TileMode.REPEAT
)
)
}
val view = layoutInflater.inflate(layout, null)
return MaterialAlertDialogBuilder(this)
.setCancelable(cancelable)
.setView(view)
.setOnDismissListener { if (enableBlur) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
content.setRenderEffect(null)
}
}
.create().also { dialog ->
block(view, dialog)
dialog.show()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.dertyp7214.rboardthememanager.core

import android.app.Activity
import com.downloader.Error
import com.downloader.OnDownloadListener
import com.downloader.PRDownloader
import de.dertyp7214.rboardthememanager.R
import de.dertyp7214.rboardthememanager.data.SoundPack
import de.dertyp7214.rboardthememanager.utils.ZipHelper
import java.io.File

fun SoundPack.download(activity: Activity, result: (sounds: List<String>) -> Unit) {
val dialog = activity.openLoadingDialog(R.string.downloading_pack)
val name = title.replace(" ", "_")
PRDownloader.download(url, activity.cacheDir.absolutePath, "$name.zip").build()
.setOnStartOrResumeListener { }
.setOnCancelListener { }
.setOnProgressListener { }
.start(object : OnDownloadListener {
override fun onDownloadComplete() {
val pack = File(activity.cacheDir, "$name.zip")
val destination = File(activity.cacheDir, name)
dialog.dismiss()
if (ZipHelper().unpackZip(destination.absolutePath, pack.absolutePath))
result(destination.listFiles()?.map { it.absolutePath } ?: listOf())
else result(listOf())
}

override fun onError(error: Error?) {
dialog.dismiss()
result(listOf())
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ fun ThemePack.download(activity: Activity, result: (themes: List<String>) -> Uni
override fun onDownloadComplete() {
val pack = File(activity.cacheDir, "$name.pack")
val destination = SuFile(activity.cacheDir, name)
dialog.dismiss()
if (ZipHelper().unpackZip(destination.absolutePath, pack.absolutePath)) {
SuFile(destination, "pack.meta").writeFile(
"name=$title\nauthor=$author\n"
)
result(destination.listFiles { file -> file.extension == "zip" }
?.map { it.absolutePath } ?: listOf())
} else result(listOf())
dialog.dismiss()
}

override fun onError(error: Error?) {
result(listOf())
dialog.dismiss()
result(listOf())
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ class Settings(private val activity: Activity) : AbstractPreference() {
)
}
),
USE_BLUR(
"useBlur",
R.string.use_blur,
R.string.use_blur_long,
-1,
true,
TYPE.BOOLEAN
),
UNINSTALL(
"uninstall",
R.string.uninstall,
Expand Down Expand Up @@ -173,7 +181,7 @@ class Settings(private val activity: Activity) : AbstractPreference() {

override fun preferences(builder: PreferenceScreen.Builder) {
SETTINGS.values()
.filter { !(it == SETTINGS.SHOW_SYSTEM_THEME && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) }
.filter { !(it == SETTINGS.SHOW_SYSTEM_THEME && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) && !(it == SETTINGS.USE_BLUR && Build.VERSION.SDK_INT < Build.VERSION_CODES.S) }
.forEach { item ->
val pref: Preference = when (item.type) {
TYPE.BOOLEAN -> builder.switch(item.key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.content.Intent.*
import android.graphics.RenderEffect
import android.graphics.Shader
import android.net.Uri
import android.os.Build
import android.os.Bundle
Expand Down Expand Up @@ -604,7 +606,19 @@ class MainActivity : AppCompatActivity() {
setFinishListener { path, _ ->
finished = true
manager.cancel(notificationId)
if (enableBlur) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
content.setRenderEffect(
RenderEffect.createBlurEffect(
10F,
10F,
Shader.TileMode.REPEAT
)
)
}
PackageUtils.install(this@MainActivity, File(path), downloadResultLauncher) {
if (enableBlur) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
content.setRenderEffect(null)
}
Toast.makeText(this@MainActivity, R.string.error, Toast.LENGTH_LONG).show()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package de.dertyp7214.rboardthememanager.utils

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.topjohnwu.superuser.io.SuFile
import de.dertyp7214.rboardthememanager.Config.SOUNDS_PACKS_URL
import de.dertyp7214.rboardthememanager.data.SoundPack
import java.net.URL
Expand All @@ -17,4 +18,16 @@ object SoundHelper {
listOf()
}
}

fun getSoundsDirectory(): SuFile? {
val productMedia = SuFile("/system/product/media/audio/ui/KeypressStandard.ogg")
val systemMedia = SuFile("/system/media/audio/ui/KeypressStandard.ogg")
return if (productMedia.exists() && productMedia.isFile) {
SuFile("/system/product/media")
} else if (systemMedia.exists() && systemMedia.isFile) {
SuFile("/system/media")
} else {
null
}
}
}
5 changes: 2 additions & 3 deletions app/src/main/res/layout/soundpack_dialog.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
Expand Down Expand Up @@ -47,7 +46,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@android:string/ok"
android:text="@string/install"
app:cornerRadius="@dimen/roundCorners" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,6 @@
<string name="processing_flags">Processing Flags</string>
<string name="load_flags">Load flags</string>
<string name="load_flags_long">Old flags backup found. Do you want to load it?</string>
<string name="use_blur">Use blur</string>
<string name="use_blur_long">Use blur Rendereffects</string>
</resources>
Loading

0 comments on commit 1deadb3

Please sign in to comment.