diff 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
line wrap: on
line diff
--- a/android/DWindows.kt	Mon May 10 20:06:50 2021 +0000
+++ b/android/DWindows.kt	Mon May 10 22:34:52 2021 +0000
@@ -13,6 +13,9 @@
 import android.content.res.Configuration
 import android.graphics.Bitmap
 import android.graphics.BitmapFactory
+import android.graphics.Canvas
+import android.graphics.drawable.BitmapDrawable
+import android.graphics.drawable.Drawable
 import android.graphics.drawable.GradientDrawable
 import android.media.AudioManager
 import android.media.ToneGenerator
@@ -41,13 +44,13 @@
 import androidx.collection.SimpleArrayMap
 import androidx.core.app.NotificationCompat
 import androidx.core.app.NotificationManagerCompat
+import androidx.core.content.res.ResourcesCompat
 import androidx.recyclerview.widget.RecyclerView
 import androidx.viewpager2.widget.ViewPager2
 import com.google.android.material.tabs.TabLayout
 import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
 import com.google.android.material.tabs.TabLayoutMediator
 import java.io.File
-import java.io.FileFilter
 import java.io.FileInputStream
 import java.io.FileNotFoundException
 import java.util.*
@@ -239,6 +242,32 @@
     )
 }
 
+class DWRender(context: Context) : View(context) {
+    var cachedCanvas: Canvas? = null
+
+    override fun onSizeChanged(width: Int, height: Int, oldWidth: Int, oldHeight: Int) {
+        super.onSizeChanged(width, height, oldWidth, oldHeight)
+        // Send DW_SIGNAL_CONFIGURE
+        eventHandlerInt(1, width, height, 0, 0)
+    }
+
+    override fun onDraw(canvas: Canvas) {
+        super.onDraw(canvas)
+        cachedCanvas = canvas
+        // Send DW_SIGNAL_EXPOSE
+        eventHandlerInt(7, 0, 0, this.width, this.height)
+        cachedCanvas = null
+    }
+
+    external fun eventHandlerInt(
+        message: Int,
+        inta: Int,
+        intb: Int,
+        intc: Int,
+        intd: Int
+    )
+}
+
 class DWFileChooser(private val activity: Activity) {
     private val list: ListView = ListView(activity)
     private val dialog: Dialog = Dialog(activity)
@@ -1737,6 +1766,26 @@
         }
     }
 
+    fun iconNew(filename: String?, data: ByteArray?, length: Int, resID: Int): Drawable?
+    {
+        var icon: Drawable? = null
+
+        waitOnUiThread {
+            if(resID != 0) {
+                icon = ResourcesCompat.getDrawable(resources, resID, null);
+            } else if(filename != null) {
+                // Try to load the image, and protect against exceptions
+                try {
+                    icon = Drawable.createFromPath(filename)
+                } catch (e: FileNotFoundException) {
+                }
+            } else if(data != null) {
+                icon = BitmapDrawable(resources, BitmapFactory.decodeByteArray(data, 0, length))
+            }
+        }
+        return icon
+    }
+
     fun pixmapNew(width: Int, height: Int, filename: String?, data: ByteArray?, length: Int, resID: Int): Bitmap?
     {
         var pixmap: Bitmap? = null
@@ -1770,6 +1819,25 @@
         return dimensions
     }
 
+    fun renderNew(cid: Int): DWRender?
+    {
+        var render: DWRender? = null
+
+        waitOnUiThread {
+            var dataArrayMap = SimpleArrayMap<String, Long>()
+
+            render = DWRender(this)
+            render!!.tag = dataArrayMap
+            render!!.id = cid
+        }
+        return render
+    }
+
+    fun renderRedraw(render: DWRender)
+    {
+        render.invalidate()
+    }
+
     fun timerConnect(interval: Long, sigfunc: Long, data: Long): Timer
     {
         // creating timer task, timer
@@ -1886,6 +1954,14 @@
         return retval
     }
 
+    fun isUIThread(): Boolean
+    {
+        if(Looper.getMainLooper() == Looper.myLooper()) {
+            return true
+        }
+        return false
+    }
+
     fun mainSleep(milliseconds: Int)
     {
         // If we are on the main UI thread... add an idle handler