2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter
|
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
import android.content.Intent
|
|
|
|
import android.media.RingtoneManager
|
|
|
|
import android.os.Build
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.text.Editable
|
|
|
|
import android.text.TextWatcher
|
|
|
|
import android.view.View
|
|
|
|
import android.widget.Button
|
|
|
|
import android.widget.EditText
|
|
|
|
import android.widget.TextView
|
2021-05-22 00:03:16 +02:00
|
|
|
import androidx.annotation.ColorInt
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2018-01-04 19:52:25 +01:00
|
|
|
import com.jrummyapps.android.colorpicker.ColorPickerDialog
|
|
|
|
import com.jrummyapps.android.colorpicker.ColorPickerDialogListener
|
2020-02-02 15:19:57 +01:00
|
|
|
import jp.juggler.subwaytooter.api.entity.Acct
|
2018-01-04 19:52:25 +01:00
|
|
|
import jp.juggler.subwaytooter.table.AcctColor
|
2019-01-21 00:00:59 +01:00
|
|
|
import jp.juggler.util.*
|
|
|
|
import org.jetbrains.anko.backgroundColor
|
|
|
|
import org.jetbrains.anko.textColor
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
class ActNickname : AppCompatActivity(), View.OnClickListener, ColorPickerDialogListener {
|
2021-05-22 00:03:16 +02:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
private val log = LogCategory("ActNickname")
|
|
|
|
|
|
|
|
internal const val EXTRA_ACCT_ASCII = "acctAscii"
|
|
|
|
internal const val EXTRA_ACCT_PRETTY = "acctPretty"
|
|
|
|
internal const val EXTRA_SHOW_NOTIFICATION_SOUND = "show_notification_sound"
|
|
|
|
|
|
|
|
fun createIntent(
|
|
|
|
activity: Activity,
|
|
|
|
fullAcct: Acct,
|
2021-06-20 15:12:25 +02:00
|
|
|
bShowNotificationSound: Boolean,
|
2021-05-22 00:03:16 +02:00
|
|
|
) = Intent(activity, ActNickname::class.java).apply {
|
|
|
|
putExtra(EXTRA_ACCT_ASCII, fullAcct.ascii)
|
|
|
|
putExtra(EXTRA_ACCT_PRETTY, fullAcct.pretty)
|
|
|
|
putExtra(EXTRA_SHOW_NOTIFICATION_SOUND, bShowNotificationSound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private lateinit var tvPreview: TextView
|
|
|
|
private lateinit var tvAcct: TextView
|
|
|
|
private lateinit var etNickname: EditText
|
|
|
|
private lateinit var btnTextColorEdit: View
|
|
|
|
private lateinit var btnTextColorReset: View
|
|
|
|
private lateinit var btnBackgroundColorEdit: View
|
|
|
|
private lateinit var btnBackgroundColorReset: View
|
|
|
|
private lateinit var btnSave: View
|
|
|
|
private lateinit var btnDiscard: View
|
|
|
|
private lateinit var btnNotificationSoundEdit: Button
|
|
|
|
private lateinit var btnNotificationSoundReset: Button
|
|
|
|
|
2021-06-20 15:12:25 +02:00
|
|
|
private var showNotificationSound = false
|
|
|
|
private lateinit var acctAscii: String
|
|
|
|
private lateinit var acctPretty: String
|
|
|
|
private var colorFg = 0
|
|
|
|
private var colorBg = 0
|
|
|
|
private var notificationSoundUri: String? = null
|
|
|
|
private var loadingBusy = false
|
2021-05-22 00:03:16 +02:00
|
|
|
|
2022-08-06 00:52:31 +02:00
|
|
|
private val arNotificationSound = ActivityResultHandler(log) { r ->
|
2022-08-08 03:42:01 +02:00
|
|
|
r.decodeRingtonePickerResult()?.let { uri ->
|
2022-08-06 00:52:31 +02:00
|
|
|
notificationSoundUri = uri.toString()
|
2021-05-22 00:03:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBackPressed() {
|
|
|
|
setResult(RESULT_OK)
|
|
|
|
super.onBackPressed()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2022-08-06 00:52:31 +02:00
|
|
|
arNotificationSound.register(this)
|
2021-05-22 00:03:16 +02:00
|
|
|
App1.setActivityTheme(this)
|
|
|
|
|
|
|
|
val intent = intent
|
|
|
|
this.acctAscii = intent.getStringExtra(EXTRA_ACCT_ASCII)!!
|
|
|
|
this.acctPretty = intent.getStringExtra(EXTRA_ACCT_PRETTY)!!
|
2021-06-20 15:12:25 +02:00
|
|
|
this.showNotificationSound = intent.getBooleanExtra(EXTRA_SHOW_NOTIFICATION_SOUND, false)
|
2021-05-22 00:03:16 +02:00
|
|
|
|
|
|
|
initUI()
|
|
|
|
|
|
|
|
load()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun initUI() {
|
|
|
|
|
|
|
|
title = getString(
|
2021-06-20 15:12:25 +02:00
|
|
|
when {
|
|
|
|
showNotificationSound -> R.string.nickname_and_color_and_notification_sound
|
|
|
|
else -> R.string.nickname_and_color
|
|
|
|
}
|
2021-05-22 00:03:16 +02:00
|
|
|
)
|
|
|
|
setContentView(R.layout.act_nickname)
|
|
|
|
App1.initEdgeToEdge(this)
|
|
|
|
|
|
|
|
Styler.fixHorizontalPadding(findViewById(R.id.llContent))
|
|
|
|
|
|
|
|
tvPreview = findViewById(R.id.tvPreview)
|
|
|
|
tvAcct = findViewById(R.id.tvAcct)
|
|
|
|
|
|
|
|
etNickname = findViewById(R.id.etNickname)
|
|
|
|
btnTextColorEdit = findViewById(R.id.btnTextColorEdit)
|
|
|
|
btnTextColorReset = findViewById(R.id.btnTextColorReset)
|
|
|
|
btnBackgroundColorEdit = findViewById(R.id.btnBackgroundColorEdit)
|
|
|
|
btnBackgroundColorReset = findViewById(R.id.btnBackgroundColorReset)
|
|
|
|
btnSave = findViewById(R.id.btnSave)
|
|
|
|
btnDiscard = findViewById(R.id.btnDiscard)
|
|
|
|
|
|
|
|
etNickname = findViewById(R.id.etNickname)
|
|
|
|
btnTextColorEdit.setOnClickListener(this)
|
|
|
|
btnTextColorReset.setOnClickListener(this)
|
|
|
|
btnBackgroundColorEdit.setOnClickListener(this)
|
|
|
|
btnBackgroundColorReset.setOnClickListener(this)
|
|
|
|
btnSave.setOnClickListener(this)
|
|
|
|
btnDiscard.setOnClickListener(this)
|
|
|
|
|
|
|
|
btnNotificationSoundEdit = findViewById(R.id.btnNotificationSoundEdit)
|
|
|
|
btnNotificationSoundReset = findViewById(R.id.btnNotificationSoundReset)
|
|
|
|
btnNotificationSoundEdit.setOnClickListener(this)
|
|
|
|
btnNotificationSoundReset.setOnClickListener(this)
|
|
|
|
|
|
|
|
val bBefore8 = Build.VERSION.SDK_INT < 26
|
2021-05-22 11:07:23 +02:00
|
|
|
btnNotificationSoundEdit.isEnabledAlpha = bBefore8
|
|
|
|
btnNotificationSoundReset.isEnabledAlpha = bBefore8
|
2021-05-22 00:03:16 +02:00
|
|
|
|
|
|
|
etNickname.addTextChangedListener(object : TextWatcher {
|
|
|
|
override fun beforeTextChanged(
|
|
|
|
s: CharSequence,
|
|
|
|
start: Int,
|
|
|
|
count: Int,
|
2021-06-20 15:12:25 +02:00
|
|
|
after: Int,
|
2021-05-22 00:03:16 +02:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun afterTextChanged(s: Editable) {
|
|
|
|
show()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun load() {
|
2021-06-20 15:12:25 +02:00
|
|
|
loadingBusy = true
|
2021-05-22 00:03:16 +02:00
|
|
|
|
|
|
|
findViewById<View>(R.id.llNotificationSound).visibility =
|
2021-06-20 15:12:25 +02:00
|
|
|
if (showNotificationSound) View.VISIBLE else View.GONE
|
2021-05-22 00:03:16 +02:00
|
|
|
|
|
|
|
tvAcct.text = acctPretty
|
|
|
|
|
|
|
|
val ac = AcctColor.load(acctAscii, acctPretty)
|
2021-06-20 15:12:25 +02:00
|
|
|
colorBg = ac.color_bg
|
|
|
|
colorFg = ac.color_fg
|
2021-05-22 00:03:16 +02:00
|
|
|
etNickname.setText(ac.nickname)
|
2021-06-20 15:12:25 +02:00
|
|
|
notificationSoundUri = ac.notification_sound
|
2021-05-22 00:03:16 +02:00
|
|
|
|
2021-06-20 15:12:25 +02:00
|
|
|
loadingBusy = false
|
2021-05-22 00:03:16 +02:00
|
|
|
show()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun save() {
|
2021-06-20 15:12:25 +02:00
|
|
|
if (loadingBusy) return
|
2021-05-22 00:03:16 +02:00
|
|
|
AcctColor(
|
|
|
|
acctAscii,
|
|
|
|
acctPretty,
|
|
|
|
etNickname.text.toString().trim { it <= ' ' },
|
2021-06-20 15:12:25 +02:00
|
|
|
colorFg,
|
|
|
|
colorBg,
|
|
|
|
notificationSoundUri
|
2021-05-22 00:03:16 +02:00
|
|
|
).save(System.currentTimeMillis())
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun show() {
|
|
|
|
val s = etNickname.text.toString().trim { it <= ' ' }
|
|
|
|
tvPreview.text = s.notEmpty() ?: acctPretty
|
2021-06-20 15:12:25 +02:00
|
|
|
tvPreview.textColor = colorFg.notZero() ?: attrColor(R.attr.colorTimeSmall)
|
|
|
|
tvPreview.backgroundColor = colorBg
|
2021-05-22 00:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onClick(v: View) {
|
|
|
|
val builder: ColorPickerDialog.Builder
|
|
|
|
when (v.id) {
|
|
|
|
R.id.btnTextColorEdit -> {
|
|
|
|
etNickname.hideKeyboard()
|
|
|
|
builder = ColorPickerDialog.newBuilder()
|
|
|
|
.setDialogType(ColorPickerDialog.TYPE_CUSTOM)
|
|
|
|
.setAllowPresets(true)
|
|
|
|
.setShowAlphaSlider(false)
|
|
|
|
.setDialogId(1)
|
2021-06-20 15:12:25 +02:00
|
|
|
if (colorFg != 0) builder.setColor(colorFg)
|
2021-05-22 00:03:16 +02:00
|
|
|
builder.show(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
R.id.btnTextColorReset -> {
|
2021-06-20 15:12:25 +02:00
|
|
|
colorFg = 0
|
2021-05-22 00:03:16 +02:00
|
|
|
show()
|
|
|
|
}
|
|
|
|
|
|
|
|
R.id.btnBackgroundColorEdit -> {
|
|
|
|
etNickname.hideKeyboard()
|
|
|
|
builder = ColorPickerDialog.newBuilder()
|
|
|
|
.setDialogType(ColorPickerDialog.TYPE_CUSTOM)
|
|
|
|
.setAllowPresets(true)
|
|
|
|
.setShowAlphaSlider(false)
|
|
|
|
.setDialogId(2)
|
2021-06-20 15:12:25 +02:00
|
|
|
if (colorBg != 0) builder.setColor(colorBg)
|
2021-05-22 00:03:16 +02:00
|
|
|
builder.show(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
R.id.btnBackgroundColorReset -> {
|
2021-06-20 15:12:25 +02:00
|
|
|
colorBg = 0
|
2021-05-22 00:03:16 +02:00
|
|
|
show()
|
|
|
|
}
|
|
|
|
|
|
|
|
R.id.btnSave -> {
|
|
|
|
save()
|
|
|
|
setResult(Activity.RESULT_OK)
|
|
|
|
finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
R.id.btnDiscard -> {
|
|
|
|
setResult(Activity.RESULT_CANCELED)
|
|
|
|
finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
R.id.btnNotificationSoundEdit -> openNotificationSoundPicker()
|
|
|
|
|
2021-06-20 15:12:25 +02:00
|
|
|
R.id.btnNotificationSoundReset -> notificationSoundUri = ""
|
2021-05-22 00:03:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 13:16:56 +01:00
|
|
|
override fun onColorSelected(dialogId: Int, @ColorInt newColor: Int) {
|
2021-05-22 00:03:16 +02:00
|
|
|
when (dialogId) {
|
2021-11-20 13:16:56 +01:00
|
|
|
1 -> colorFg = -0x1000000 or newColor
|
|
|
|
2 -> colorBg = -0x1000000 or newColor
|
2021-05-22 00:03:16 +02:00
|
|
|
}
|
|
|
|
show()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDialogDismissed(dialogId: Int) {}
|
|
|
|
|
|
|
|
private fun openNotificationSoundPicker() {
|
|
|
|
val intent = Intent(RingtoneManager.ACTION_RINGTONE_PICKER)
|
|
|
|
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION)
|
|
|
|
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, R.string.notification_sound)
|
|
|
|
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false)
|
|
|
|
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false)
|
2021-06-20 15:12:25 +02:00
|
|
|
notificationSoundUri.mayUri()?.let {
|
2021-05-22 00:03:16 +02:00
|
|
|
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, it)
|
|
|
|
}
|
|
|
|
val chooser = Intent.createChooser(intent, getString(R.string.notification_sound))
|
|
|
|
arNotificationSound.launch(chooser)
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|