comparison android/DWindows.kt @ 2530:b9923432cb1f

Android: Implement View based render widget and icon support with Drawable. Return nullptr instead of 0 on any functions using jobject.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 10 May 2021 22:34:52 +0000
parents 060fdb2d807d
children f45ebd96ebe5
comparison
equal deleted inserted replaced
2529:060fdb2d807d 2530:b9923432cb1f
11 import android.content.DialogInterface 11 import android.content.DialogInterface
12 import android.content.pm.ActivityInfo 12 import android.content.pm.ActivityInfo
13 import android.content.res.Configuration 13 import android.content.res.Configuration
14 import android.graphics.Bitmap 14 import android.graphics.Bitmap
15 import android.graphics.BitmapFactory 15 import android.graphics.BitmapFactory
16 import android.graphics.Canvas
17 import android.graphics.drawable.BitmapDrawable
18 import android.graphics.drawable.Drawable
16 import android.graphics.drawable.GradientDrawable 19 import android.graphics.drawable.GradientDrawable
17 import android.media.AudioManager 20 import android.media.AudioManager
18 import android.media.ToneGenerator 21 import android.media.ToneGenerator
19 import android.os.* 22 import android.os.*
20 import android.text.InputFilter 23 import android.text.InputFilter
39 import androidx.appcompat.app.AppCompatActivity 42 import androidx.appcompat.app.AppCompatActivity
40 import androidx.appcompat.widget.AppCompatEditText 43 import androidx.appcompat.widget.AppCompatEditText
41 import androidx.collection.SimpleArrayMap 44 import androidx.collection.SimpleArrayMap
42 import androidx.core.app.NotificationCompat 45 import androidx.core.app.NotificationCompat
43 import androidx.core.app.NotificationManagerCompat 46 import androidx.core.app.NotificationManagerCompat
47 import androidx.core.content.res.ResourcesCompat
44 import androidx.recyclerview.widget.RecyclerView 48 import androidx.recyclerview.widget.RecyclerView
45 import androidx.viewpager2.widget.ViewPager2 49 import androidx.viewpager2.widget.ViewPager2
46 import com.google.android.material.tabs.TabLayout 50 import com.google.android.material.tabs.TabLayout
47 import com.google.android.material.tabs.TabLayout.OnTabSelectedListener 51 import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
48 import com.google.android.material.tabs.TabLayoutMediator 52 import com.google.android.material.tabs.TabLayoutMediator
49 import java.io.File 53 import java.io.File
50 import java.io.FileFilter
51 import java.io.FileInputStream 54 import java.io.FileInputStream
52 import java.io.FileNotFoundException 55 import java.io.FileNotFoundException
53 import java.util.* 56 import java.util.*
54 import java.util.concurrent.locks.ReentrantLock 57 import java.util.concurrent.locks.ReentrantLock
55 58
226 } 229 }
227 230
228 override fun onItemClick(parent: AdapterView<*>?, view: View, position: Int, id: Long) { 231 override fun onItemClick(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
229 selected = position 232 selected = position
230 eventHandlerInt(11, position, 0, 0, 0) 233 eventHandlerInt(11, position, 0, 0, 0)
234 }
235
236 external fun eventHandlerInt(
237 message: Int,
238 inta: Int,
239 intb: Int,
240 intc: Int,
241 intd: Int
242 )
243 }
244
245 class DWRender(context: Context) : View(context) {
246 var cachedCanvas: Canvas? = null
247
248 override fun onSizeChanged(width: Int, height: Int, oldWidth: Int, oldHeight: Int) {
249 super.onSizeChanged(width, height, oldWidth, oldHeight)
250 // Send DW_SIGNAL_CONFIGURE
251 eventHandlerInt(1, width, height, 0, 0)
252 }
253
254 override fun onDraw(canvas: Canvas) {
255 super.onDraw(canvas)
256 cachedCanvas = canvas
257 // Send DW_SIGNAL_EXPOSE
258 eventHandlerInt(7, 0, 0, this.width, this.height)
259 cachedCanvas = null
231 } 260 }
232 261
233 external fun eventHandlerInt( 262 external fun eventHandlerInt(
234 message: Int, 263 message: Int,
235 inta: Int, 264 inta: Int,
1735 } 1764 }
1736 } 1765 }
1737 } 1766 }
1738 } 1767 }
1739 1768
1769 fun iconNew(filename: String?, data: ByteArray?, length: Int, resID: Int): Drawable?
1770 {
1771 var icon: Drawable? = null
1772
1773 waitOnUiThread {
1774 if(resID != 0) {
1775 icon = ResourcesCompat.getDrawable(resources, resID, null);
1776 } else if(filename != null) {
1777 // Try to load the image, and protect against exceptions
1778 try {
1779 icon = Drawable.createFromPath(filename)
1780 } catch (e: FileNotFoundException) {
1781 }
1782 } else if(data != null) {
1783 icon = BitmapDrawable(resources, BitmapFactory.decodeByteArray(data, 0, length))
1784 }
1785 }
1786 return icon
1787 }
1788
1740 fun pixmapNew(width: Int, height: Int, filename: String?, data: ByteArray?, length: Int, resID: Int): Bitmap? 1789 fun pixmapNew(width: Int, height: Int, filename: String?, data: ByteArray?, length: Int, resID: Int): Bitmap?
1741 { 1790 {
1742 var pixmap: Bitmap? = null 1791 var pixmap: Bitmap? = null
1743 1792
1744 waitOnUiThread { 1793 waitOnUiThread {
1768 dimensions = pixmap.width.toLong() or (pixmap.height.toLong() shl 32) 1817 dimensions = pixmap.width.toLong() or (pixmap.height.toLong() shl 32)
1769 } 1818 }
1770 return dimensions 1819 return dimensions
1771 } 1820 }
1772 1821
1822 fun renderNew(cid: Int): DWRender?
1823 {
1824 var render: DWRender? = null
1825
1826 waitOnUiThread {
1827 var dataArrayMap = SimpleArrayMap<String, Long>()
1828
1829 render = DWRender(this)
1830 render!!.tag = dataArrayMap
1831 render!!.id = cid
1832 }
1833 return render
1834 }
1835
1836 fun renderRedraw(render: DWRender)
1837 {
1838 render.invalidate()
1839 }
1840
1773 fun timerConnect(interval: Long, sigfunc: Long, data: Long): Timer 1841 fun timerConnect(interval: Long, sigfunc: Long, data: Long): Timer
1774 { 1842 {
1775 // creating timer task, timer 1843 // creating timer task, timer
1776 val t = Timer() 1844 val t = Timer()
1777 val tt: TimerTask = object : TimerTask() { 1845 val tt: TimerTask = object : TimerTask() {
1882 Looper.loop() 1950 Looper.loop()
1883 } catch (e2: RuntimeException) { 1951 } catch (e2: RuntimeException) {
1884 } 1952 }
1885 } 1953 }
1886 return retval 1954 return retval
1955 }
1956
1957 fun isUIThread(): Boolean
1958 {
1959 if(Looper.getMainLooper() == Looper.myLooper()) {
1960 return true
1961 }
1962 return false
1887 } 1963 }
1888 1964
1889 fun mainSleep(milliseconds: Int) 1965 fun mainSleep(milliseconds: Int)
1890 { 1966 {
1891 // If we are on the main UI thread... add an idle handler 1967 // If we are on the main UI thread... add an idle handler