PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/activities/UserInfoActivity.kt

268 lines
10 KiB
Kotlin
Raw Normal View History

2019-06-03 16:23:37 +00:00
package code.name.monkey.retromusic.activities
import android.app.Activity
2019-12-01 15:27:01 +00:00
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
2019-06-03 16:23:37 +00:00
import android.content.res.ColorStateList
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore.Images.Media
2019-06-03 16:23:37 +00:00
import android.provider.MediaStore.Images.Media.getBitmap
2019-06-04 04:00:11 +00:00
import android.text.TextUtils
2019-06-03 16:23:37 +00:00
import android.view.MenuItem
import android.widget.Toast
import code.name.monkey.appthemehelper.ThemeStore
2020-02-22 12:10:12 +00:00
import code.name.monkey.appthemehelper.util.ATHUtil
2019-12-01 15:27:01 +00:00
import code.name.monkey.appthemehelper.util.ColorUtil
import code.name.monkey.appthemehelper.util.MaterialUtil
import code.name.monkey.appthemehelper.util.MaterialValueHelper
import code.name.monkey.retromusic.App
2019-06-03 16:23:37 +00:00
import code.name.monkey.retromusic.Constants.USER_BANNER
import code.name.monkey.retromusic.Constants.USER_PROFILE
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.activities.base.AbsBaseActivity
2019-12-01 15:27:01 +00:00
import code.name.monkey.retromusic.util.Compressor
2019-06-03 16:23:37 +00:00
import code.name.monkey.retromusic.util.ImageUtil.getResizedBitmap
2019-12-01 15:27:01 +00:00
import code.name.monkey.retromusic.util.PreferenceUtil
import com.afollestad.materialdialogs.LayoutMode
import com.afollestad.materialdialogs.MaterialDialog
2019-06-03 16:23:37 +00:00
import com.afollestad.materialdialogs.bottomsheets.BottomSheet
import com.afollestad.materialdialogs.list.listItems
2020-02-25 06:44:46 +00:00
import kotlinx.android.synthetic.main.activity_user_info.*
2019-12-26 19:53:11 +00:00
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
2019-12-01 15:27:01 +00:00
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
2019-06-03 16:23:37 +00:00
class UserInfoActivity : AbsBaseActivity() {
2019-12-01 15:27:01 +00:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_user_info)
setStatusbarColorAuto()
2019-12-05 18:18:54 +00:00
setNavigationbarColorAuto()
2019-12-01 15:27:01 +00:00
setTaskDescriptionColorAuto()
setLightNavigationBar(true)
2019-06-03 16:23:37 +00:00
2020-02-22 12:10:12 +00:00
toolbar.setBackgroundColor(ATHUtil.resolveColor(this, R.attr.colorSurface))
setSupportActionBar(toolbar)
2019-06-03 16:23:37 +00:00
2019-12-01 15:27:01 +00:00
MaterialUtil.setTint(nameContainer, false)
name.setText(PreferenceUtil.getInstance(this).userName)
2019-06-03 16:23:37 +00:00
2019-12-01 15:27:01 +00:00
if (PreferenceUtil.getInstance(this).profileImage.isNotEmpty()) {
loadImageFromStorage(PreferenceUtil.getInstance(this).profileImage)
}
if (PreferenceUtil.getInstance(this).bannerImage.isNotEmpty()) {
loadBannerFromStorage(PreferenceUtil.getInstance(this).bannerImage)
}
userImage.setOnClickListener {
MaterialDialog(this, BottomSheet(LayoutMode.WRAP_CONTENT)).show {
cornerRadius(PreferenceUtil.getInstance(this@UserInfoActivity).dialogCorner)
title(text = getString(R.string.set_photo))
2020-02-22 12:10:12 +00:00
listItems(
items = listOf(
getString(R.string.new_profile_photo),
getString(R.string.remove_profile_photo)
)
) { _, position, _ ->
2019-12-01 15:27:01 +00:00
when (position) {
0 -> pickNewPhoto()
1 -> PreferenceUtil.getInstance(this@UserInfoActivity).saveProfileImage("")
}
}
}
}
bannerSelect.setOnClickListener {
MaterialDialog(this, BottomSheet(LayoutMode.WRAP_CONTENT)).show {
cornerRadius(PreferenceUtil.getInstance(this@UserInfoActivity).dialogCorner)
title(R.string.select_banner_photo)
2020-02-22 12:10:12 +00:00
listItems(
items = listOf(
getString(R.string.new_banner_photo),
getString(R.string.remove_banner_photo)
)
)
2019-12-01 15:27:01 +00:00
{ _, position, _ ->
when (position) {
0 -> selectBannerImage()
1 -> PreferenceUtil.getInstance(this@UserInfoActivity).setBannerImagePath("")
}
}
}
}
next.setOnClickListener {
val nameString = name.text.toString().trim { it <= ' ' }
if (TextUtils.isEmpty(nameString)) {
Toast.makeText(this, "Umm name is empty", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
PreferenceUtil.getInstance(this).userName = nameString
setResult(Activity.RESULT_OK)
finish()
}
next.backgroundTintList = ColorStateList.valueOf(ThemeStore.accentColor(this))
2020-02-22 12:10:12 +00:00
ColorStateList.valueOf(
MaterialValueHelper.getPrimaryTextColor(
this,
ColorUtil.isColorLight(ThemeStore.accentColor(this))
)
)
.apply {
next.setTextColor(this)
next.iconTint = this
}
2019-12-01 15:27:01 +00:00
}
2019-06-03 16:23:37 +00:00
2019-12-01 15:27:01 +00:00
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == android.R.id.home) {
onBackPressed()
}
return super.onOptionsItemSelected(item)
}
2019-06-03 16:23:37 +00:00
2019-12-01 15:27:01 +00:00
private fun selectBannerImage() {
val pickImageIntent = Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI)
pickImageIntent.type = "image/*"
//pickImageIntent.putExtra("crop", "true")
pickImageIntent.putExtra("outputX", 1290)
pickImageIntent.putExtra("outputY", 720)
pickImageIntent.putExtra("aspectX", 16)
pickImageIntent.putExtra("aspectY", 9)
pickImageIntent.putExtra("scale", true)
//intent.setAction(Intent.ACTION_GET_CONTENT);
2020-02-25 06:44:46 +00:00
startActivityForResult(
Intent.createChooser(pickImageIntent, "Select Picture"),
PICK_BANNER_REQUEST
)
2019-12-01 15:27:01 +00:00
}
2019-06-03 16:23:37 +00:00
2019-12-01 15:27:01 +00:00
private fun pickNewPhoto() {
val pickImageIntent = Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI)
pickImageIntent.type = "image/*"
pickImageIntent.putExtra("crop", "true")
pickImageIntent.putExtra("outputX", 512)
pickImageIntent.putExtra("outputY", 512)
pickImageIntent.putExtra("aspectX", 1)
pickImageIntent.putExtra("aspectY", 1)
pickImageIntent.putExtra("scale", true)
2020-02-25 06:44:46 +00:00
startActivityForResult(
Intent.createChooser(pickImageIntent, "Select Picture"),
PICK_IMAGE_REQUEST
)
2019-12-01 15:27:01 +00:00
}
2019-06-03 16:23:37 +00:00
2019-12-01 15:27:01 +00:00
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && data != null) {
when (requestCode) {
PICK_IMAGE_REQUEST -> {
try {
data.data?.let {
val bitmap = getResizedBitmap(
2020-02-22 12:10:12 +00:00
getBitmap(contentResolver, it),
PROFILE_ICON_SIZE
2019-12-01 15:27:01 +00:00
)
val profileImagePath = saveToInternalStorage(bitmap, USER_PROFILE)
PreferenceUtil.getInstance(this).saveProfileImage(profileImagePath)
loadImageFromStorage(profileImagePath)
}
} catch (e: IOException) {
e.printStackTrace()
}
}
PICK_BANNER_REQUEST -> {
try {
data.data?.let {
val bitmap = getBitmap(contentResolver, it)
val profileImagePath = saveToInternalStorage(bitmap, USER_BANNER)
PreferenceUtil.getInstance(this).setBannerImagePath(profileImagePath)
loadBannerFromStorage(profileImagePath)
}
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}
}
2019-06-03 16:23:37 +00:00
2019-12-01 15:27:01 +00:00
private fun getImagePath(aUri: Uri, aSelection: String?): String? {
var path: String? = null
val cursor = App.getContext().contentResolver.query(aUri, null, aSelection, null, null)
if (cursor != null) {
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(Media.DATA))
}
cursor.close()
}
return path
}
2019-06-03 16:23:37 +00:00
2019-12-01 15:27:01 +00:00
private fun loadBannerFromStorage(profileImagePath: String) {
2019-12-26 19:53:11 +00:00
CoroutineScope(Dispatchers.IO).launch {
withContext(Dispatchers.IO) {
val bitmap = Compressor(this@UserInfoActivity).setQuality(100)
2020-02-22 12:10:12 +00:00
.setCompressFormat(Bitmap.CompressFormat.WEBP)
.compressToBitmap(File(profileImagePath, USER_BANNER))
2019-12-26 19:53:11 +00:00
withContext(Dispatchers.Main) { bannerImage.setImageBitmap(bitmap) }
}
}
2019-12-01 15:27:01 +00:00
}
2019-06-03 16:23:37 +00:00
2019-12-01 15:27:01 +00:00
private fun loadImageFromStorage(path: String) {
2019-12-26 19:53:11 +00:00
CoroutineScope(Dispatchers.IO).launch {
withContext(Dispatchers.IO) {
val bitmap = Compressor(this@UserInfoActivity)
2020-02-22 12:10:12 +00:00
.setMaxHeight(300)
.setMaxWidth(300)
.setQuality(75)
.setCompressFormat(Bitmap.CompressFormat.WEBP)
.compressToBitmap(File(path, USER_PROFILE))
2019-12-26 19:53:11 +00:00
withContext(Dispatchers.Main) { userImage.setImageBitmap(bitmap) }
}
}
2019-12-01 15:27:01 +00:00
}
2019-12-01 15:27:01 +00:00
private fun saveToInternalStorage(bitmapImage: Bitmap, userBanner: String): String {
val cw = ContextWrapper(this)
val directory = cw.getDir("imageDir", Context.MODE_PRIVATE)
val myPath = File(directory, userBanner)
var fos: FileOutputStream? = null
try {
fos = FileOutputStream(myPath)
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.WEBP, 100, fos)
} catch (e: Exception) {
e.printStackTrace()
} finally {
try {
fos?.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
return directory.absolutePath
}
2019-06-03 16:23:37 +00:00
2019-12-01 15:27:01 +00:00
companion object {
2019-06-03 16:23:37 +00:00
2019-12-01 15:27:01 +00:00
private const val PICK_IMAGE_REQUEST = 9002
private const val PICK_BANNER_REQUEST = 9004
private const val PROFILE_ICON_SIZE = 400
}
2019-06-03 16:23:37 +00:00
}
fun Activity.pickImage(requestCode: Int) {
2019-12-01 15:27:01 +00:00
Intent(Intent.ACTION_GET_CONTENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "image/*"
startActivityForResult(this, requestCode)
}
2019-06-03 16:23:37 +00:00
}