comparison android/DWindows.kt @ 2521:5f92284e2b08

Android: Implement bitmap buttons, implement dw_listbox_selected_multi(). Added a number of safety checks to prevent java exceptions.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 09 May 2021 09:31:14 +0000
parents 167af4b0004b
children 66c490aa719d
comparison
equal deleted inserted replaced
2520:167af4b0004b 2521:5f92284e2b08
1 package org.dbsoft.dwindows 1 package org.dbsoft.dwindows
2 2
3 import android.app.Activity 3 import android.R.attr
4 import android.content.ClipData 4 import android.content.ClipData
5 import android.content.ClipboardManager 5 import android.content.ClipboardManager
6 import android.content.Context 6 import android.content.Context
7 import android.content.DialogInterface 7 import android.content.DialogInterface
8 import android.content.pm.ActivityInfo 8 import android.content.pm.ActivityInfo
9 import android.content.res.Configuration 9 import android.content.res.Configuration
10 import android.graphics.Bitmap 10 import android.graphics.Bitmap
11 import android.graphics.BitmapFactory
11 import android.graphics.drawable.GradientDrawable 12 import android.graphics.drawable.GradientDrawable
12 import android.media.AudioManager 13 import android.media.AudioManager
13 import android.media.ToneGenerator 14 import android.media.ToneGenerator
14 import android.os.Bundle 15 import android.os.Bundle
15 import android.os.Handler 16 import android.os.Handler
19 import android.text.InputFilter.LengthFilter 20 import android.text.InputFilter.LengthFilter
20 import android.text.InputType 21 import android.text.InputType
21 import android.text.method.PasswordTransformationMethod 22 import android.text.method.PasswordTransformationMethod
22 import android.util.Base64 23 import android.util.Base64
23 import android.util.Log 24 import android.util.Log
25 import android.util.SparseBooleanArray
24 import android.view.Gravity 26 import android.view.Gravity
25 import android.view.MotionEvent 27 import android.view.MotionEvent
26 import android.view.View 28 import android.view.View
27 import android.view.View.OnTouchListener 29 import android.view.View.OnTouchListener
28 import android.view.ViewGroup 30 import android.view.ViewGroup
39 import androidx.recyclerview.widget.RecyclerView 41 import androidx.recyclerview.widget.RecyclerView
40 import androidx.viewpager2.widget.ViewPager2 42 import androidx.viewpager2.widget.ViewPager2
41 import com.google.android.material.tabs.TabLayout 43 import com.google.android.material.tabs.TabLayout
42 import com.google.android.material.tabs.TabLayout.OnTabSelectedListener 44 import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
43 import com.google.android.material.tabs.TabLayoutMediator 45 import com.google.android.material.tabs.TabLayoutMediator
46 import java.io.File
47 import java.io.FileInputStream
48 import java.io.FileNotFoundException
44 import java.util.* 49 import java.util.*
45 import java.util.concurrent.locks.ReentrantLock 50 import java.util.concurrent.locks.ReentrantLock
46 51
47 52
48 class DWTabViewPagerAdapter : RecyclerView.Adapter<DWTabViewPagerAdapter.DWEventViewHolder>() { 53 class DWTabViewPagerAdapter : RecyclerView.Adapter<DWTabViewPagerAdapter.DWEventViewHolder>() {
100 105
101 if (event.action == MotionEvent.ACTION_UP) { 106 if (event.action == MotionEvent.ACTION_UP) {
102 if (event.x >= v.width - (v as EditText) 107 if (event.x >= v.width - (v as EditText)
103 .compoundDrawables[DRAWABLE_RIGHT].bounds.width() 108 .compoundDrawables[DRAWABLE_RIGHT].bounds.width()
104 ) { 109 ) {
105 value += 1 110 val newvalue = this.text.toString().toLong()
111
112 if(newvalue != null) {
113 value = newvalue + 1
114 } else {
115 value += 1
116 }
106 if(value > maximum) { 117 if(value > maximum) {
107 value = maximum 118 value = maximum
119 }
120 if(value < minimum) {
121 value = minimum
108 } 122 }
109 setText(value.toString()) 123 setText(value.toString())
110 eventHandlerInt(14, value.toInt(), 0, 0, 0) 124 eventHandlerInt(14, value.toInt(), 0, 0, 0)
111 return true 125 return true
112 } else if (event.x <= (v as EditText) 126 } else if (event.x <= (v as EditText)
113 .compoundDrawables[DRAWABLE_LEFT].bounds.width() 127 .compoundDrawables[DRAWABLE_LEFT].bounds.width()
114 ) { 128 ) {
115 value -= 1 129 val newvalue = this.text.toString().toLong()
130
131 if(newvalue != null) {
132 value = newvalue - 1
133 } else {
134 value -= 1
135 }
136 if(value > maximum) {
137 value = maximum
138 }
116 if(value < minimum) { 139 if(value < minimum) {
117 value = minimum 140 value = minimum
118 } 141 }
119 setText(value.toString()) 142 setText(value.toString())
120 eventHandlerInt(14, value.toInt(), 0, 0, 0) 143 eventHandlerInt(14, value.toInt(), 0, 0, 0)
557 } 580 }
558 } 581 }
559 return button 582 return button
560 } 583 }
561 584
585 fun bitmapButtonNew(text: String, resid: Int): ImageButton? {
586 var button: ImageButton? = null
587 waitOnUiThread {
588 button = ImageButton(this)
589 var dataArrayMap = SimpleArrayMap<String, Long>()
590
591 button!!.tag = dataArrayMap
592 button!!.id = resid
593 button!!.setImageResource(resid)
594 button!!.setOnClickListener {
595 eventHandlerSimple(button!!, 8)
596 }
597 }
598 return button
599 }
600
601 fun bitmapButtonNewFromFile(text: String, cid: Int, filename: String): ImageButton? {
602 var button: ImageButton? = null
603 waitOnUiThread {
604 button = ImageButton(this)
605 var dataArrayMap = SimpleArrayMap<String, Long>()
606
607 button!!.tag = dataArrayMap
608 button!!.id = cid
609 button!!.setOnClickListener {
610 eventHandlerSimple(button!!, 8)
611 }
612 // Try to load the image, and protect against exceptions
613 try {
614 val f = File(filename)
615 val b = BitmapFactory.decodeStream(FileInputStream(f))
616 button!!.setImageBitmap(b)
617 }
618 catch (e: FileNotFoundException)
619 {
620 }
621 }
622 return button
623 }
624
625 fun bitmapButtonNewFromData(text: String, cid: Int, data: ByteArray, length: Int): ImageButton? {
626 var button: ImageButton? = null
627 waitOnUiThread {
628 button = ImageButton(this)
629 var dataArrayMap = SimpleArrayMap<String, Long>()
630 val b = BitmapFactory.decodeByteArray(data,0, length)
631
632 button!!.tag = dataArrayMap
633 button!!.id = cid
634 button!!.setOnClickListener {
635 eventHandlerSimple(button!!, 8)
636 }
637 button!!.setImageBitmap(b)
638 }
639 return button
640 }
641
562 fun entryfieldNew(text: String, cid: Int, password: Int): EditText? { 642 fun entryfieldNew(text: String, cid: Int, password: Int): EditText? {
563 var entryfield: EditText? = null 643 var entryfield: EditText? = null
564 644
565 waitOnUiThread { 645 waitOnUiThread {
566 var dataArrayMap = SimpleArrayMap<String, Long>() 646 var dataArrayMap = SimpleArrayMap<String, Long>()
1267 { 1347 {
1268 waitOnUiThread { 1348 waitOnUiThread {
1269 if(window is DWComboBox) { 1349 if(window is DWComboBox) {
1270 val combobox = window 1350 val combobox = window
1271 1351
1272 if(index < combobox.list.count()) 1352 if(index > -1 && index < combobox.list.count())
1273 combobox.list[index] = text 1353 combobox.list[index] = text
1274 } else if(window is DWListBox) { 1354 } else if(window is DWListBox) {
1275 val listbox = window 1355 val listbox = window
1276 1356
1277 if(index < listbox.list.count()) 1357 if(index > -1 && index < listbox.list.count())
1278 listbox.list[index] = text 1358 listbox.list[index] = text
1279 } 1359 }
1280 } 1360 }
1281 } 1361 }
1282 1362
1286 1366
1287 waitOnUiThread { 1367 waitOnUiThread {
1288 if(window is DWComboBox) { 1368 if(window is DWComboBox) {
1289 val combobox = window 1369 val combobox = window
1290 1370
1291 if(index < combobox.list.count()) 1371 if(index > -1 && index < combobox.list.count())
1292 retval = combobox.list[index] 1372 retval = combobox.list[index]
1293 } else if(window is DWListBox) { 1373 } else if(window is DWListBox) {
1294 val listbox = window 1374 val listbox = window
1295 1375
1296 if(index < listbox.list.count()) 1376 if(index > -1 && index < listbox.list.count())
1297 retval = listbox.list[index] 1377 retval = listbox.list[index]
1298 } 1378 }
1299 } 1379 }
1300 return retval 1380 return retval
1301 } 1381 }
1360 } 1440 }
1361 } 1441 }
1362 } 1442 }
1363 } 1443 }
1364 1444
1365 fun listSetTop(window: View, top: Int) 1445 fun listBoxSetTop(window: View, top: Int)
1366 { 1446 {
1367 waitOnUiThread { 1447 waitOnUiThread {
1368 if(window is DWListBox) { 1448 if(window is DWListBox) {
1369 val listbox = window 1449 val listbox = window
1370 1450
1371 if(top < listbox.list.count()) { 1451 if(top < listbox.list.count()) {
1372 listbox.smoothScrollToPosition(top) 1452 listbox.smoothScrollToPosition(top)
1373 } 1453 }
1374 } 1454 }
1375 } 1455 }
1456 }
1457
1458 fun listBoxSelectedMulti(window: View, where: Int): Int
1459 {
1460 var retval: Int = -1
1461
1462 waitOnUiThread {
1463 if(window is DWListBox) {
1464 val listbox = window
1465 val checked: SparseBooleanArray = listbox.getCheckedItemPositions()
1466
1467 // If we are starting over....
1468 if(where == -1 && checked.size() > 0) {
1469 retval = checked.keyAt(0)
1470 } else {
1471 // Otherwise loop until we find our current place
1472 for (i in 0 until checked.size()) {
1473 // Item position in adapter
1474 val position: Int = checked.keyAt(i)
1475 // If we are at our current point... check to see
1476 // if there is another one, and return it...
1477 // otherwise we will return -1 to indicated we are done.
1478 if (where == position && (i+1) < checked.size()) {
1479 retval = checked.keyAt(i+1)
1480 }
1481 }
1482 }
1483 }
1484 }
1485 return retval
1376 } 1486 }
1377 1487
1378 fun timerConnect(interval: Long, sigfunc: Long, data: Long): Timer 1488 fun timerConnect(interval: Long, sigfunc: Long, data: Long): Timer
1379 { 1489 {
1380 // creating timer task, timer 1490 // creating timer task, timer