2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.media.Ringtone
|
|
|
|
import android.media.RingtoneManager
|
|
|
|
import android.net.Uri
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2019-11-15 07:13:59 +01:00
|
|
|
import android.widget.ImageButton
|
2018-01-04 19:52:25 +01:00
|
|
|
import android.widget.TextView
|
2021-05-22 00:03:16 +02:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
2018-01-04 19:52:25 +01:00
|
|
|
import com.woxthebox.draglistview.DragItem
|
|
|
|
import com.woxthebox.draglistview.DragItemAdapter
|
|
|
|
import com.woxthebox.draglistview.DragListView
|
|
|
|
import com.woxthebox.draglistview.swipe.ListSwipeHelper
|
|
|
|
import com.woxthebox.draglistview.swipe.ListSwipeItem
|
|
|
|
import jp.juggler.subwaytooter.table.HighlightWord
|
2018-12-01 00:02:18 +01:00
|
|
|
import jp.juggler.util.*
|
|
|
|
import java.lang.ref.WeakReference
|
|
|
|
import java.util.*
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
class ActHighlightWordList : AppCompatActivity(), View.OnClickListener {
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
private val log = LogCategory("ActHighlightWordList")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private lateinit var listView : DragListView
|
|
|
|
private lateinit var listAdapter : MyListAdapter
|
|
|
|
|
|
|
|
private var last_ringtone : WeakReference<Ringtone>? = null
|
2021-05-22 00:03:16 +02:00
|
|
|
|
2021-05-22 11:07:23 +02:00
|
|
|
private val arEdit = activityResultHandler { ar->
|
|
|
|
try {
|
|
|
|
if( ar?.resultCode == RESULT_OK )
|
|
|
|
loadData()
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
throw RuntimeException("can't load data", ex)
|
2021-05-22 00:03:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
// @Override public void onBackPressed(){
|
|
|
|
// setResult( RESULT_OK );
|
|
|
|
// super.onBackPressed();
|
|
|
|
// }
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState : Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2021-05-22 00:03:16 +02:00
|
|
|
arEdit .register(this,log)
|
2019-10-07 21:39:56 +02:00
|
|
|
App1.setActivityTheme(this)
|
2018-01-04 19:52:25 +01:00
|
|
|
initUI()
|
|
|
|
loadData()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
stopLastRingtone()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun initUI() {
|
|
|
|
setContentView(R.layout.act_highlight_list)
|
2019-08-31 16:33:13 +02:00
|
|
|
App1.initEdgeToEdge(this)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2019-12-15 20:12:57 +01:00
|
|
|
Styler.fixHorizontalPadding0(findViewById(R.id.llContent))
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
// リストのアダプター
|
|
|
|
listAdapter = MyListAdapter()
|
|
|
|
|
|
|
|
// ハンドル部分をドラッグで並べ替えできるRecyclerView
|
|
|
|
listView = findViewById(R.id.drag_list_view)
|
2019-09-12 15:43:11 +02:00
|
|
|
listView.setLayoutManager(LinearLayoutManager(this))
|
2018-01-04 19:52:25 +01:00
|
|
|
listView.setAdapter(listAdapter, false)
|
|
|
|
|
|
|
|
listView.setCanDragHorizontally(true)
|
|
|
|
listView.isDragEnabled = false
|
|
|
|
listView.setCustomDragItem(MyDragItem(this, R.layout.lv_highlight_word))
|
|
|
|
|
|
|
|
listView.recyclerView.isVerticalScrollBarEnabled = true
|
|
|
|
|
|
|
|
// リストを左右スワイプした
|
|
|
|
listView.setSwipeListener(object : ListSwipeHelper.OnSwipeListenerAdapter() {
|
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
override fun onItemSwipeStarted(
|
|
|
|
item : ListSwipeItem
|
|
|
|
) {
|
2018-01-04 19:52:25 +01:00
|
|
|
// 操作中はリフレッシュ禁止
|
|
|
|
// mRefreshLayout.setEnabled( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onItemSwipeEnded(
|
2018-01-10 16:47:35 +01:00
|
|
|
item : ListSwipeItem,
|
|
|
|
swipedDirection : ListSwipeItem.SwipeDirection?
|
2018-01-04 19:52:25 +01:00
|
|
|
) {
|
|
|
|
// 操作完了でリフレッシュ許可
|
|
|
|
// mRefreshLayout.setEnabled( USE_SWIPE_REFRESH );
|
|
|
|
|
|
|
|
// 左にスワイプした(右端にBGが見えた) なら要素を削除する
|
|
|
|
if(swipedDirection == ListSwipeItem.SwipeDirection.LEFT) {
|
2018-01-10 16:47:35 +01:00
|
|
|
val o = item.tag
|
2018-01-04 19:52:25 +01:00
|
|
|
if(o is HighlightWord) {
|
2019-11-15 07:13:59 +01:00
|
|
|
o.delete(this@ActHighlightWordList)
|
2018-01-04 19:52:25 +01:00
|
|
|
listAdapter.removeItem(listAdapter.getPositionForItem(o))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
findViewById<View>(R.id.btnAdd).setOnClickListener(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun loadData() {
|
|
|
|
|
|
|
|
val tmp_list = ArrayList<HighlightWord>()
|
|
|
|
try {
|
|
|
|
|
|
|
|
HighlightWord.createCursor().use { cursor ->
|
|
|
|
while(cursor.moveToNext()) {
|
|
|
|
val item = HighlightWord(cursor)
|
|
|
|
tmp_list.add(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
|
|
|
}
|
|
|
|
|
|
|
|
listAdapter.itemList = tmp_list
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onClick(v : View) {
|
|
|
|
when(v.id) {
|
|
|
|
R.id.btnAdd -> create()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// リスト要素のViewHolder
|
2018-01-21 17:47:13 +01:00
|
|
|
internal inner class MyViewHolder(viewRoot : View) :
|
|
|
|
DragItemAdapter.ViewHolder(viewRoot, R.id.ivDragHandle, false), View.OnClickListener {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
val tvName : TextView
|
|
|
|
private val btnSound : View
|
2020-09-29 19:44:56 +02:00
|
|
|
private val ivSpeech : ImageButton
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
init {
|
|
|
|
|
|
|
|
tvName = viewRoot.findViewById(R.id.tvName)
|
|
|
|
btnSound = viewRoot.findViewById(R.id.btnSound)
|
2019-11-15 07:13:59 +01:00
|
|
|
ivSpeech = viewRoot.findViewById(R.id.ivSpeech)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
// リスト要素のビューが ListSwipeItem だった場合、Swipe操作を制御できる
|
|
|
|
if(viewRoot is ListSwipeItem) {
|
|
|
|
viewRoot.setSwipeInStyle(ListSwipeItem.SwipeInStyle.SLIDE)
|
|
|
|
viewRoot.supportedSwipeDirection = ListSwipeItem.SwipeDirection.LEFT
|
|
|
|
}
|
|
|
|
|
2018-01-21 17:47:13 +01:00
|
|
|
} // View ID。 ここを押すとドラッグ操作をすぐに開始する
|
2018-01-04 19:52:25 +01:00
|
|
|
// 長押しでドラッグ開始するなら真
|
|
|
|
|
|
|
|
fun bind(item : HighlightWord) {
|
|
|
|
itemView.tag = item // itemView は親クラスのメンバ変数
|
|
|
|
tvName.text = item.name
|
|
|
|
|
2019-01-18 16:33:53 +01:00
|
|
|
tvName.setBackgroundColor(item.color_bg)
|
|
|
|
tvName.setTextColor(
|
|
|
|
item.color_fg.notZero()
|
2021-01-04 02:11:45 +01:00
|
|
|
?: attrColor(android.R.attr.textColorPrimary)
|
2019-01-18 16:33:53 +01:00
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
btnSound.vg(item.sound_type != HighlightWord.SOUND_TYPE_NONE)?.apply {
|
2019-12-15 16:34:46 +01:00
|
|
|
setOnClickListener(this@MyViewHolder)
|
|
|
|
tag = item
|
|
|
|
}
|
2019-11-15 07:13:59 +01:00
|
|
|
|
2020-09-29 19:44:56 +02:00
|
|
|
ivSpeech.vg(item.speech != 0)?.apply {
|
2019-12-15 16:34:46 +01:00
|
|
|
setOnClickListener(this@MyViewHolder)
|
|
|
|
tag = item
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// @Override
|
|
|
|
// public boolean onItemLongClicked( View view ){
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
override fun onItemClicked(view : View) {
|
2018-01-21 17:47:13 +01:00
|
|
|
val o = view.tag
|
2018-01-04 19:52:25 +01:00
|
|
|
if(o is HighlightWord) {
|
|
|
|
edit(o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onClick(v : View) {
|
|
|
|
val o = v.tag
|
|
|
|
if(o is HighlightWord) {
|
2020-09-29 19:44:56 +02:00
|
|
|
when(v.id) {
|
|
|
|
R.id.btnSound -> {
|
2019-11-15 07:13:59 +01:00
|
|
|
sound(o)
|
|
|
|
}
|
2020-09-29 19:44:56 +02:00
|
|
|
|
|
|
|
R.id.ivSpeech -> {
|
|
|
|
App1.getAppState(this@ActHighlightWordList)
|
|
|
|
.addSpeech(o.name, dedupMode = DedupMode.None)
|
2019-11-15 07:13:59 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ドラッグ操作中のデータ
|
2020-09-09 21:46:50 +02:00
|
|
|
private inner class MyDragItem(context : Context, layoutId : Int) :
|
2018-01-21 17:47:13 +01:00
|
|
|
DragItem(context, layoutId) {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
override fun onBindDragView(clickedView : View, dragView : View) {
|
2020-09-29 19:44:56 +02:00
|
|
|
|
2018-01-21 17:47:13 +01:00
|
|
|
dragView.findViewById<TextView>(R.id.tvName).text =
|
|
|
|
clickedView.findViewById<TextView>(R.id.tvName).text
|
2020-09-29 19:44:56 +02:00
|
|
|
|
2018-01-21 17:47:13 +01:00
|
|
|
dragView.findViewById<View>(R.id.btnSound).visibility =
|
|
|
|
clickedView.findViewById<View>(R.id.btnSound).visibility
|
2020-09-29 19:44:56 +02:00
|
|
|
|
|
|
|
dragView.findViewById<View>(R.id.ivSpeech).visibility =
|
2019-11-15 07:13:59 +01:00
|
|
|
clickedView.findViewById<View>(R.id.ivSpeech).visibility
|
2020-09-29 19:44:56 +02:00
|
|
|
|
|
|
|
dragView.findViewById<View>(R.id.item_layout)
|
2021-01-04 02:11:45 +01:00
|
|
|
.setBackgroundColor(attrColor(R.attr.list_item_bg_pressed_dragged))
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 21:46:50 +02:00
|
|
|
private inner class MyListAdapter :
|
2018-01-21 17:47:13 +01:00
|
|
|
DragItemAdapter<HighlightWord, MyViewHolder>() {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
init {
|
|
|
|
setHasStableIds(true)
|
|
|
|
itemList = ArrayList()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent : ViewGroup, viewType : Int) : MyViewHolder {
|
|
|
|
val view = layoutInflater.inflate(R.layout.lv_highlight_word, parent, false)
|
|
|
|
return MyViewHolder(view)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder : MyViewHolder, position : Int) {
|
|
|
|
super.onBindViewHolder(holder, position)
|
|
|
|
holder.bind(itemList[position])
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getUniqueItemId(position : Int) : Long {
|
|
|
|
val item = mItemList[position] // mItemList は親クラスのメンバ変数
|
|
|
|
return item.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun create() {
|
2021-05-22 11:07:23 +02:00
|
|
|
arEdit.launch(
|
|
|
|
ActHighlightWordEdit.createIntent(this, "")
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
2021-05-22 00:03:16 +02:00
|
|
|
private fun edit(oldItem : HighlightWord) {
|
2021-05-22 11:07:23 +02:00
|
|
|
arEdit.launch(
|
|
|
|
ActHighlightWordEdit.createIntent(this, oldItem.id)
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun stopLastRingtone() {
|
2018-01-10 16:47:35 +01:00
|
|
|
val r = last_ringtone?.get()
|
2018-01-04 19:52:25 +01:00
|
|
|
if(r != null) {
|
|
|
|
try {
|
|
|
|
r.stop()
|
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
|
|
|
} finally {
|
|
|
|
last_ringtone = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun sound(item : HighlightWord) {
|
|
|
|
|
2018-01-10 16:47:35 +01:00
|
|
|
val sound_type = item.sound_type
|
|
|
|
if(sound_type == HighlightWord.SOUND_TYPE_NONE) return
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-11-30 05:42:20 +01:00
|
|
|
fun Uri?.tryRingTone() : Boolean {
|
|
|
|
try {
|
|
|
|
if(this != null) {
|
|
|
|
val ringtone = RingtoneManager.getRingtone(this@ActHighlightWordList, this)
|
2018-01-10 16:47:35 +01:00
|
|
|
if(ringtone != null) {
|
|
|
|
stopLastRingtone()
|
|
|
|
last_ringtone = WeakReference(ringtone)
|
|
|
|
ringtone.play()
|
2018-11-30 05:42:20 +01:00
|
|
|
return true
|
2018-01-10 16:47:35 +01:00
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
2018-11-30 05:42:20 +01:00
|
|
|
} catch(ex : Throwable) {
|
|
|
|
log.trace(ex)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
2018-11-30 05:42:20 +01:00
|
|
|
|
|
|
|
return false
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
2018-11-30 05:42:20 +01:00
|
|
|
if(sound_type == HighlightWord.SOUND_TYPE_CUSTOM
|
|
|
|
&& item.sound_uri.mayUri().tryRingTone()
|
|
|
|
) return
|
|
|
|
|
|
|
|
// fall thru 失敗したら通常の音を鳴らす
|
|
|
|
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).tryRingTone()
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|