Code Cleanup
This commit is contained in:
parent
8d81424365
commit
5f5bddf5e9
7 changed files with 52 additions and 71 deletions
|
@ -31,7 +31,6 @@ import code.name.monkey.retromusic.R
|
||||||
import code.name.monkey.retromusic.activities.base.AbsMusicServiceActivity
|
import code.name.monkey.retromusic.activities.base.AbsMusicServiceActivity
|
||||||
import code.name.monkey.retromusic.databinding.ActivityPermissionBinding
|
import code.name.monkey.retromusic.databinding.ActivityPermissionBinding
|
||||||
import code.name.monkey.retromusic.extensions.*
|
import code.name.monkey.retromusic.extensions.*
|
||||||
import code.name.monkey.retromusic.util.RingtoneManager
|
|
||||||
|
|
||||||
class PermissionActivity : AbsMusicServiceActivity() {
|
class PermissionActivity : AbsMusicServiceActivity() {
|
||||||
private lateinit var binding: ActivityPermissionBinding
|
private lateinit var binding: ActivityPermissionBinding
|
||||||
|
@ -50,7 +49,7 @@ class PermissionActivity : AbsMusicServiceActivity() {
|
||||||
if (VersionUtils.hasMarshmallow()) {
|
if (VersionUtils.hasMarshmallow()) {
|
||||||
binding.audioPermission.show()
|
binding.audioPermission.show()
|
||||||
binding.audioPermission.setButtonClick {
|
binding.audioPermission.setButtonClick {
|
||||||
if (RingtoneManager.requiresDialog(this@PermissionActivity)) {
|
if (hasAudioPermission()) {
|
||||||
val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
|
val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
|
||||||
intent.data = ("package:" + applicationContext.packageName).toUri()
|
intent.data = ("package:" + applicationContext.packageName).toUri()
|
||||||
startActivity(intent)
|
startActivity(intent)
|
||||||
|
@ -99,7 +98,8 @@ class PermissionActivity : AbsMusicServiceActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun hasStoragePermission(): Boolean {
|
private fun hasStoragePermission(): Boolean {
|
||||||
return ActivityCompat.checkSelfPermission(this , Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
|
return ActivityCompat.checkSelfPermission(this,
|
||||||
|
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.M)
|
@RequiresApi(Build.VERSION_CODES.M)
|
||||||
|
|
|
@ -31,6 +31,7 @@ import code.name.monkey.retromusic.extensions.*
|
||||||
import code.name.monkey.retromusic.util.PreferenceUtil
|
import code.name.monkey.retromusic.util.PreferenceUtil
|
||||||
import code.name.monkey.retromusic.util.theme.getNightMode
|
import code.name.monkey.retromusic.util.theme.getNightMode
|
||||||
import code.name.monkey.retromusic.util.theme.getThemeResValue
|
import code.name.monkey.retromusic.util.theme.getThemeResValue
|
||||||
|
import com.google.android.play.core.splitcompat.SplitCompat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
abstract class AbsThemeActivity : ATHToolbarActivity(), Runnable {
|
abstract class AbsThemeActivity : ATHToolbarActivity(), Runnable {
|
||||||
|
@ -122,5 +123,6 @@ abstract class AbsThemeActivity : ATHToolbarActivity(), Runnable {
|
||||||
Locale.forLanguageTag(code)
|
Locale.forLanguageTag(code)
|
||||||
}
|
}
|
||||||
super.attachBaseContext(LanguageContextWrapper.wrap(newBase, locale))
|
super.attachBaseContext(LanguageContextWrapper.wrap(newBase, locale))
|
||||||
|
SplitCompat.install(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -180,11 +180,14 @@ abstract class AbsPlayerFragment(@LayoutRes layout: Int) : AbsMusicServiceFragme
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
R.id.action_set_as_ringtone -> {
|
R.id.action_set_as_ringtone -> {
|
||||||
if (RingtoneManager.requiresDialog(requireActivity())) {
|
requireContext().run {
|
||||||
RingtoneManager.getDialog(requireActivity())
|
if (RingtoneManager.requiresDialog(this)) {
|
||||||
|
RingtoneManager.showDialog(this)
|
||||||
|
} else {
|
||||||
|
RingtoneManager.setRingtone(this, song)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
val ringtoneManager = RingtoneManager(requireActivity())
|
|
||||||
ringtoneManager.setRingtone(song)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
R.id.action_go_to_genre -> {
|
R.id.action_go_to_genre -> {
|
||||||
|
@ -265,7 +268,7 @@ abstract class AbsPlayerFragment(@LayoutRes layout: Int) : AbsMusicServiceFragme
|
||||||
} else {
|
} else {
|
||||||
if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border
|
if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border
|
||||||
}
|
}
|
||||||
val drawable = requireContext().getTintedDrawable(
|
val drawable = requireContext().getTintedDrawable(
|
||||||
icon,
|
icon,
|
||||||
toolbarIconColor()
|
toolbarIconColor()
|
||||||
)
|
)
|
||||||
|
@ -379,9 +382,7 @@ fun goToArtist(activity: Activity) {
|
||||||
findNavController(R.id.fragment_container).navigate(
|
findNavController(R.id.fragment_container).navigate(
|
||||||
R.id.artistDetailsFragment,
|
R.id.artistDetailsFragment,
|
||||||
bundleOf(EXTRA_ARTIST_ID to song.artistId),
|
bundleOf(EXTRA_ARTIST_ID to song.artistId),
|
||||||
navOptions {
|
null,
|
||||||
launchSingleTop = true
|
|
||||||
},
|
|
||||||
null
|
null
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -402,9 +403,7 @@ fun goToAlbum(activity: Activity) {
|
||||||
findNavController(R.id.fragment_container).navigate(
|
findNavController(R.id.fragment_container).navigate(
|
||||||
R.id.albumDetailsFragment,
|
R.id.albumDetailsFragment,
|
||||||
bundleOf(EXTRA_ALBUM_ID to song.albumId),
|
bundleOf(EXTRA_ALBUM_ID to song.albumId),
|
||||||
navOptions {
|
null,
|
||||||
launchSingleTop = true
|
|
||||||
},
|
|
||||||
null
|
null
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,7 +66,7 @@ class UserInfoFragment : Fragment() {
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater,
|
inflater: LayoutInflater,
|
||||||
container: ViewGroup?,
|
container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?,
|
||||||
): View {
|
): View {
|
||||||
sharedElementEnterTransition = MaterialContainerTransform().apply {
|
sharedElementEnterTransition = MaterialContainerTransform().apply {
|
||||||
drawingViewId = R.id.fragment_container
|
drawingViewId = R.id.fragment_container
|
||||||
|
@ -229,7 +229,7 @@ class UserInfoFragment : Fragment() {
|
||||||
model: Any?,
|
model: Any?,
|
||||||
target: Target<Bitmap>?,
|
target: Target<Bitmap>?,
|
||||||
dataSource: DataSource?,
|
dataSource: DataSource?,
|
||||||
isFirstResource: Boolean
|
isFirstResource: Boolean,
|
||||||
): Boolean {
|
): Boolean {
|
||||||
resource?.let { saveImage(it, USER_BANNER) }
|
resource?.let { saveImage(it, USER_BANNER) }
|
||||||
return false
|
return false
|
||||||
|
@ -239,7 +239,7 @@ class UserInfoFragment : Fragment() {
|
||||||
e: GlideException?,
|
e: GlideException?,
|
||||||
model: Any?,
|
model: Any?,
|
||||||
target: Target<Bitmap>?,
|
target: Target<Bitmap>?,
|
||||||
isFirstResource: Boolean
|
isFirstResource: Boolean,
|
||||||
): Boolean {
|
): Boolean {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -251,14 +251,10 @@ class UserInfoFragment : Fragment() {
|
||||||
lifecycleScope.launch(Dispatchers.IO) {
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
val appDir = requireContext().filesDir
|
val appDir = requireContext().filesDir
|
||||||
val file = File(appDir, fileName)
|
val file = File(appDir, fileName)
|
||||||
var successful = false
|
var successful: Boolean
|
||||||
runCatching {
|
file.outputStream().buffered().use {
|
||||||
file.outputStream().buffered().use {
|
successful = ImageUtil.resizeBitmap(bitmap, 2048)
|
||||||
successful = ImageUtil.resizeBitmap(bitmap, 2048)
|
.compress(Bitmap.CompressFormat.WEBP, 100, it)
|
||||||
.compress(Bitmap.CompressFormat.WEBP, 100, it)
|
|
||||||
}
|
|
||||||
}.onFailure {
|
|
||||||
it.printStackTrace()
|
|
||||||
}
|
}
|
||||||
if (successful) {
|
if (successful) {
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
|
@ -279,7 +275,7 @@ class UserInfoFragment : Fragment() {
|
||||||
model: Any?,
|
model: Any?,
|
||||||
target: Target<Bitmap>?,
|
target: Target<Bitmap>?,
|
||||||
dataSource: DataSource?,
|
dataSource: DataSource?,
|
||||||
isFirstResource: Boolean
|
isFirstResource: Boolean,
|
||||||
): Boolean {
|
): Boolean {
|
||||||
resource?.let { saveImage(it, USER_PROFILE) }
|
resource?.let { saveImage(it, USER_PROFILE) }
|
||||||
return false
|
return false
|
||||||
|
@ -289,7 +285,7 @@ class UserInfoFragment : Fragment() {
|
||||||
e: GlideException?,
|
e: GlideException?,
|
||||||
model: Any?,
|
model: Any?,
|
||||||
target: Target<Bitmap>?,
|
target: Target<Bitmap>?,
|
||||||
isFirstResource: Boolean
|
isFirstResource: Boolean,
|
||||||
): Boolean {
|
): Boolean {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,9 +139,7 @@ class SongsFragment : AbsRecyclerViewCustomGridSizeFragment<SongAdapter, GridLay
|
||||||
CastButtonFactory.setUpMediaRouteButton(requireContext(), menu, R.id.action_cast)
|
CastButtonFactory.setUpMediaRouteButton(requireContext(), menu, R.id.action_cast)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setUpSortOrderMenu(
|
private fun setUpSortOrderMenu(sortOrderMenu: SubMenu) {
|
||||||
sortOrderMenu: SubMenu
|
|
||||||
) {
|
|
||||||
val currentSortOrder: String? = getSortOrder()
|
val currentSortOrder: String? = getSortOrder()
|
||||||
sortOrderMenu.clear()
|
sortOrderMenu.clear()
|
||||||
sortOrderMenu.add(
|
sortOrderMenu.add(
|
||||||
|
@ -211,9 +209,7 @@ class SongsFragment : AbsRecyclerViewCustomGridSizeFragment<SongAdapter, GridLay
|
||||||
sortOrderMenu.setGroupCheckable(0, true, true)
|
sortOrderMenu.setGroupCheckable(0, true, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupLayoutMenu(
|
private fun setupLayoutMenu(subMenu: SubMenu) {
|
||||||
subMenu: SubMenu
|
|
||||||
) {
|
|
||||||
when (itemLayoutRes()) {
|
when (itemLayoutRes()) {
|
||||||
R.layout.item_card -> subMenu.findItem(R.id.action_layout_card).isChecked = true
|
R.layout.item_card -> subMenu.findItem(R.id.action_layout_card).isChecked = true
|
||||||
R.layout.item_grid -> subMenu.findItem(R.id.action_layout_normal).isChecked = true
|
R.layout.item_grid -> subMenu.findItem(R.id.action_layout_normal).isChecked = true
|
||||||
|
@ -227,9 +223,7 @@ class SongsFragment : AbsRecyclerViewCustomGridSizeFragment<SongAdapter, GridLay
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setUpGridSizeMenu(
|
private fun setUpGridSizeMenu(gridSizeMenu: SubMenu) {
|
||||||
gridSizeMenu: SubMenu
|
|
||||||
) {
|
|
||||||
when (getGridSize()) {
|
when (getGridSize()) {
|
||||||
1 -> gridSizeMenu.findItem(R.id.action_grid_size_1).isChecked = true
|
1 -> gridSizeMenu.findItem(R.id.action_grid_size_1).isChecked = true
|
||||||
2 -> gridSizeMenu.findItem(R.id.action_grid_size_2).isChecked = true
|
2 -> gridSizeMenu.findItem(R.id.action_grid_size_2).isChecked = true
|
||||||
|
@ -274,9 +268,7 @@ class SongsFragment : AbsRecyclerViewCustomGridSizeFragment<SongAdapter, GridLay
|
||||||
return super.onMenuItemSelected(item)
|
return super.onMenuItemSelected(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleSortOrderMenuItem(
|
private fun handleSortOrderMenuItem(item: MenuItem): Boolean {
|
||||||
item: MenuItem
|
|
||||||
): Boolean {
|
|
||||||
val sortOrder: String = when (item.itemId) {
|
val sortOrder: String = when (item.itemId) {
|
||||||
R.id.action_song_sort_order_asc -> SongSortOrder.SONG_A_Z
|
R.id.action_song_sort_order_asc -> SongSortOrder.SONG_A_Z
|
||||||
R.id.action_song_sort_order_desc -> SongSortOrder.SONG_Z_A
|
R.id.action_song_sort_order_desc -> SongSortOrder.SONG_Z_A
|
||||||
|
@ -297,9 +289,7 @@ class SongsFragment : AbsRecyclerViewCustomGridSizeFragment<SongAdapter, GridLay
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleLayoutResType(
|
private fun handleLayoutResType(item: MenuItem): Boolean {
|
||||||
item: MenuItem
|
|
||||||
): Boolean {
|
|
||||||
val layoutRes = when (item.itemId) {
|
val layoutRes = when (item.itemId) {
|
||||||
R.id.action_layout_normal -> R.layout.item_grid
|
R.id.action_layout_normal -> R.layout.item_grid
|
||||||
R.id.action_layout_card -> R.layout.item_card
|
R.id.action_layout_card -> R.layout.item_card
|
||||||
|
@ -317,9 +307,7 @@ class SongsFragment : AbsRecyclerViewCustomGridSizeFragment<SongAdapter, GridLay
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleGridSizeMenuItem(
|
private fun handleGridSizeMenuItem(item: MenuItem): Boolean {
|
||||||
item: MenuItem
|
|
||||||
): Boolean {
|
|
||||||
val gridSize = when (item.itemId) {
|
val gridSize = when (item.itemId) {
|
||||||
R.id.action_grid_size_1 -> 1
|
R.id.action_grid_size_1 -> 1
|
||||||
R.id.action_grid_size_2 -> 2
|
R.id.action_grid_size_2 -> 2
|
||||||
|
|
|
@ -55,10 +55,9 @@ object SongMenuHelper : KoinComponent {
|
||||||
when (menuItemId) {
|
when (menuItemId) {
|
||||||
R.id.action_set_as_ringtone -> {
|
R.id.action_set_as_ringtone -> {
|
||||||
if (RingtoneManager.requiresDialog(activity)) {
|
if (RingtoneManager.requiresDialog(activity)) {
|
||||||
RingtoneManager.getDialog(activity)
|
RingtoneManager.showDialog(activity)
|
||||||
} else {
|
} else {
|
||||||
val ringtoneManager = RingtoneManager(activity)
|
RingtoneManager.setRingtone(activity, song)
|
||||||
ringtoneManager.setRingtone(song)
|
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,10 +27,10 @@ import code.name.monkey.retromusic.model.Song
|
||||||
import code.name.monkey.retromusic.util.MusicUtil.getSongFileUri
|
import code.name.monkey.retromusic.util.MusicUtil.getSongFileUri
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
|
|
||||||
class RingtoneManager(val context: Context) {
|
object RingtoneManager {
|
||||||
fun setRingtone(song: Song) {
|
fun setRingtone(context: Context, song: Song) {
|
||||||
val resolver = context.contentResolver
|
|
||||||
val uri = getSongFileUri(song.id)
|
val uri = getSongFileUri(song.id)
|
||||||
|
val resolver = context.contentResolver
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val cursor = resolver.query(
|
val cursor = resolver.query(
|
||||||
|
@ -52,28 +52,25 @@ class RingtoneManager(val context: Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
fun requiresDialog(context: Context): Boolean {
|
||||||
|
if (VersionUtils.hasMarshmallow()) {
|
||||||
fun requiresDialog(context: Context): Boolean {
|
if (!Settings.System.canWrite(context)) {
|
||||||
if (VersionUtils.hasMarshmallow()) {
|
return true
|
||||||
if (!Settings.System.canWrite(context)) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
fun getDialog(context: Context) {
|
fun showDialog(context: Context) {
|
||||||
return MaterialAlertDialogBuilder(context, R.style.MaterialAlertDialogTheme)
|
return MaterialAlertDialogBuilder(context, R.style.MaterialAlertDialogTheme)
|
||||||
.setTitle(R.string.dialog_title_set_ringtone)
|
.setTitle(R.string.dialog_title_set_ringtone)
|
||||||
.setMessage(R.string.dialog_message_set_ringtone)
|
.setMessage(R.string.dialog_message_set_ringtone)
|
||||||
.setPositiveButton(android.R.string.ok) { _, _ ->
|
.setPositiveButton(android.R.string.ok) { _, _ ->
|
||||||
val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
|
val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
|
||||||
intent.data = ("package:" + context.applicationContext.packageName).toUri()
|
intent.data = ("package:" + context.applicationContext.packageName).toUri()
|
||||||
context.startActivity(intent)
|
context.startActivity(intent)
|
||||||
}
|
}
|
||||||
.setNegativeButton(android.R.string.cancel, null)
|
.setNegativeButton(android.R.string.cancel, null)
|
||||||
.create().show()
|
.create().show()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue