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.net.Uri
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.support.v7.app.AppCompatActivity
|
|
|
|
import android.view.View
|
|
|
|
import android.widget.CompoundButton
|
|
|
|
import android.widget.Switch
|
|
|
|
import android.widget.TextView
|
|
|
|
|
|
|
|
import com.jrummyapps.android.colorpicker.ColorPickerDialog
|
|
|
|
import com.jrummyapps.android.colorpicker.ColorPickerDialogListener
|
|
|
|
|
|
|
|
import org.json.JSONException
|
|
|
|
|
|
|
|
import jp.juggler.subwaytooter.table.HighlightWord
|
|
|
|
import jp.juggler.subwaytooter.util.LogCategory
|
2018-01-21 13:46:36 +01:00
|
|
|
import jp.juggler.subwaytooter.util.toJsonObject
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
class ActHighlightWordEdit
|
|
|
|
: AppCompatActivity(),
|
|
|
|
View.OnClickListener,
|
|
|
|
ColorPickerDialogListener,
|
|
|
|
CompoundButton.OnCheckedChangeListener {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
internal val log = LogCategory("ActHighlightWordEdit")
|
|
|
|
|
|
|
|
const val EXTRA_ITEM = "item"
|
|
|
|
|
|
|
|
internal const val REQUEST_CODE_NOTIFICATION_SOUND = 2
|
|
|
|
|
|
|
|
private const val COLOR_DIALOG_ID_TEXT = 1
|
|
|
|
private const val COLOR_DIALOG_ID_BACKGROUND = 2
|
|
|
|
|
|
|
|
fun open(activity : Activity, request_code : Int, item : HighlightWord) {
|
|
|
|
try {
|
|
|
|
val intent = Intent(activity, ActHighlightWordEdit::class.java)
|
|
|
|
intent.putExtra(EXTRA_ITEM, item.encodeJson().toString())
|
|
|
|
activity.startActivityForResult(intent, request_code)
|
|
|
|
} catch(ex : JSONException) {
|
|
|
|
throw RuntimeException(ex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
internal lateinit var item : HighlightWord
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
private lateinit var tvName : TextView
|
|
|
|
private lateinit var swSound : Switch
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
private var bBusy = false
|
|
|
|
|
|
|
|
private fun makeResult() {
|
|
|
|
try {
|
|
|
|
val data = Intent()
|
|
|
|
data.putExtra(EXTRA_ITEM, item.encodeJson().toString())
|
|
|
|
setResult(Activity.RESULT_OK, data)
|
|
|
|
} catch(ex : JSONException) {
|
|
|
|
throw RuntimeException(ex)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBackPressed() {
|
|
|
|
makeResult()
|
|
|
|
super.onBackPressed()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState : Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
App1.setActivityTheme(this, false)
|
|
|
|
initUI()
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
item = HighlightWord(
|
|
|
|
(savedInstanceState?.getString(EXTRA_ITEM)
|
|
|
|
?: intent.getStringExtra(EXTRA_ITEM)
|
|
|
|
).toJsonObject()
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
showSampleText()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
override fun onActivityResult(requestCode : Int, resultCode : Int, data : Intent?) {
|
2018-01-04 19:52:25 +01:00
|
|
|
when(requestCode) {
|
|
|
|
|
|
|
|
REQUEST_CODE_NOTIFICATION_SOUND -> {
|
|
|
|
if(resultCode == Activity.RESULT_OK) {
|
|
|
|
// RINGTONE_PICKERからの選択されたデータを取得する
|
2018-01-21 13:46:36 +01:00
|
|
|
val uri = data?.extras?.get(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)
|
2018-01-04 19:52:25 +01:00
|
|
|
if(uri is Uri) {
|
|
|
|
item.sound_uri = uri.toString()
|
|
|
|
item.sound_type = HighlightWord.SOUND_TYPE_CUSTOM
|
|
|
|
swSound.isChecked = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-21 13:46:36 +01:00
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
else -> super.onActivityResult(requestCode, resultCode, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun initUI() {
|
|
|
|
setContentView(R.layout.act_highlight_edit)
|
|
|
|
|
|
|
|
tvName = findViewById(R.id.tvName)
|
|
|
|
swSound = findViewById(R.id.swSound)
|
|
|
|
swSound.setOnCheckedChangeListener(this)
|
|
|
|
|
|
|
|
findViewById<View>(R.id.btnTextColorEdit).setOnClickListener(this)
|
|
|
|
findViewById<View>(R.id.btnTextColorReset).setOnClickListener(this)
|
|
|
|
findViewById<View>(R.id.btnBackgroundColorEdit).setOnClickListener(this)
|
|
|
|
findViewById<View>(R.id.btnBackgroundColorReset).setOnClickListener(this)
|
|
|
|
findViewById<View>(R.id.btnNotificationSoundEdit).setOnClickListener(this)
|
|
|
|
findViewById<View>(R.id.btnNotificationSoundReset).setOnClickListener(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun showSampleText() {
|
|
|
|
bBusy = true
|
|
|
|
try {
|
|
|
|
|
|
|
|
swSound.isChecked = item.sound_type != HighlightWord.SOUND_TYPE_NONE
|
|
|
|
|
|
|
|
tvName.text = item.name
|
|
|
|
|
|
|
|
var c = item.color_bg
|
|
|
|
if(c == 0) {
|
|
|
|
tvName.setBackgroundColor(0)
|
|
|
|
} else {
|
|
|
|
tvName.setBackgroundColor(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
c = item.color_fg
|
|
|
|
if(c == 0) {
|
|
|
|
tvName.setTextColor(Styler.getAttributeColor(this, android.R.attr.textColorPrimary))
|
|
|
|
} else {
|
|
|
|
tvName.setTextColor(c)
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
bBusy = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onClick(v : View) {
|
|
|
|
|
|
|
|
when(v.id) {
|
|
|
|
|
|
|
|
R.id.btnTextColorEdit -> openColorPicker(COLOR_DIALOG_ID_TEXT, item.color_fg)
|
|
|
|
|
|
|
|
R.id.btnTextColorReset -> {
|
|
|
|
item.color_fg = 0
|
|
|
|
showSampleText()
|
|
|
|
}
|
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
R.id.btnBackgroundColorEdit -> openColorPicker(
|
|
|
|
COLOR_DIALOG_ID_BACKGROUND,
|
|
|
|
item.color_bg
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
R.id.btnBackgroundColorReset -> {
|
|
|
|
item.color_bg = 0
|
|
|
|
showSampleText()
|
|
|
|
}
|
|
|
|
|
|
|
|
R.id.btnNotificationSoundEdit -> openNotificationSoundPicker()
|
|
|
|
|
|
|
|
R.id.btnNotificationSoundReset -> {
|
|
|
|
item.sound_uri = null
|
2018-01-21 13:46:36 +01:00
|
|
|
item.sound_type =
|
|
|
|
if(swSound.isChecked) HighlightWord.SOUND_TYPE_DEFAULT else HighlightWord.SOUND_TYPE_NONE
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCheckedChanged(buttonView : CompoundButton, isChecked : Boolean) {
|
|
|
|
if(bBusy) return
|
|
|
|
|
|
|
|
if(! isChecked) {
|
|
|
|
item.sound_type = HighlightWord.SOUND_TYPE_NONE
|
|
|
|
} else {
|
2018-01-10 16:47:35 +01:00
|
|
|
|
2018-01-21 13:46:36 +01:00
|
|
|
item.sound_type =
|
|
|
|
if(item.sound_uri?.isEmpty() != false) HighlightWord.SOUND_TYPE_DEFAULT else HighlightWord.SOUND_TYPE_CUSTOM
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun openColorPicker(id : Int, initial_color : Int) {
|
|
|
|
val builder = ColorPickerDialog.newBuilder()
|
|
|
|
.setDialogType(ColorPickerDialog.TYPE_CUSTOM)
|
|
|
|
.setAllowPresets(true)
|
|
|
|
.setShowAlphaSlider(id == COLOR_DIALOG_ID_BACKGROUND)
|
|
|
|
.setDialogId(id)
|
|
|
|
|
|
|
|
if(initial_color != 0) builder.setColor(initial_color)
|
|
|
|
|
|
|
|
builder.show(this)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDialogDismissed(dialogId : Int) {}
|
|
|
|
|
|
|
|
override fun onColorSelected(dialogId : Int, color : Int) {
|
|
|
|
when(dialogId) {
|
|
|
|
COLOR_DIALOG_ID_TEXT -> item.color_fg = - 0x1000000 or color
|
|
|
|
COLOR_DIALOG_ID_BACKGROUND -> item.color_bg = if(color == 0) 0x01000000 else color
|
|
|
|
}
|
|
|
|
showSampleText()
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
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)
|
|
|
|
try {
|
2018-01-10 16:47:35 +01:00
|
|
|
val sound_uri = item.sound_uri
|
2018-01-21 13:46:36 +01:00
|
|
|
val uri = if(sound_uri?.isEmpty() != false) null else Uri.parse(sound_uri)
|
2018-01-04 19:52:25 +01:00
|
|
|
if(uri != null) {
|
|
|
|
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, uri)
|
|
|
|
}
|
|
|
|
} catch(ignored : Throwable) {
|
|
|
|
}
|
|
|
|
|
|
|
|
val chooser = Intent.createChooser(intent, getString(R.string.notification_sound))
|
|
|
|
startActivityForResult(chooser, REQUEST_CODE_NOTIFICATION_SOUND)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|