changeset 2528:03f6870bcfcc

Android: Implement dw_bitmap_new(), dw_dwindow_set_bitmap() and dw_window_set_bitmap_from_data().
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 10 May 2021 09:29:51 +0000
parents eec926265888
children 060fdb2d807d
files android/DWindows.kt android/dw.cpp
diffstat 2 files changed, 134 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Mon May 10 02:24:52 2021 +0000
+++ b/android/DWindows.kt	Mon May 10 09:29:51 2021 +0000
@@ -1626,7 +1626,11 @@
         var calendar: CalendarView? = null
 
         waitOnUiThread {
+            var dataArrayMap = SimpleArrayMap<String, Long>()
+
             calendar = CalendarView(this)
+            calendar!!.tag = dataArrayMap
+            calendar!!.id = cid
             calendar!!.setOnDateChangeListener { calendar, year, month, day ->
                 val c: Calendar = Calendar.getInstance();
                 c.set(year, month, day);
@@ -1656,6 +1660,83 @@
         return date
     }
 
+    fun bitmapNew(cid: Int): ImageView?
+    {
+        var imageview: ImageView? = null
+
+        waitOnUiThread {
+            var dataArrayMap = SimpleArrayMap<String, Long>()
+
+            imageview = ImageView(this)
+            imageview!!.tag = dataArrayMap
+            imageview!!.id = cid
+        }
+
+        return imageview
+    }
+
+    fun windowSetBitmap(window: View, resID: Int, filename: String?)
+    {
+        waitOnUiThread {
+            if(resID != 0) {
+                if(window is ImageButton) {
+                    val button = window
+
+                    button.setImageResource(resID)
+                } else if(window is ImageView) {
+                    val imageview = window
+
+                    imageview.setImageResource(resID)
+                }
+            } else if(filename != null) {
+                // Try to load the image, and protect against exceptions
+                try {
+                    val f = File(filename)
+                    val b = BitmapFactory.decodeStream(FileInputStream(f))
+                    if(window is ImageButton) {
+                        val button = window
+
+                        button.setImageBitmap(b)
+                    } else if(window is ImageView) {
+                        val imageview = window
+
+                        imageview.setImageBitmap(b)
+                    }
+                } catch (e: FileNotFoundException) {
+                }
+            }
+        }
+    }
+
+    fun windowSetBitmapFromData(window: View, resID: Int, data: ByteArray?, length: Int)
+    {
+        waitOnUiThread {
+            if(resID != 0) {
+                if (window is ImageButton) {
+                    val button = window
+
+                    button.setImageResource(resID)
+                } else if (window is ImageView) {
+                    val imageview = window
+
+                    imageview.setImageResource(resID)
+                }
+            } else if(data != null) {
+                val b = BitmapFactory.decodeByteArray(data, 0, length)
+
+                if (window is ImageButton) {
+                    val button = window
+
+                    button.setImageBitmap(b)
+                } else if (window is ImageView) {
+                    val imageview = window
+
+                    imageview.setImageBitmap(b)
+                }
+            }
+        }
+    }
+
     fun timerConnect(interval: Long, sigfunc: Long, data: Long): Timer
     {
         // creating timer task, timer
--- a/android/dw.cpp	Mon May 10 02:24:52 2021 +0000
+++ b/android/dw.cpp	Mon May 10 09:29:51 2021 +0000
@@ -5,6 +5,8 @@
  * (C) 2011-2021 Brian Smith <brian@dbsoft.org>
  * (C) 2011-2021 Mark Hessling <mark@rexx.org>
  *
+ * Requires Android API 19 (KitKat) or higher.
+ *
  */
 
 #include "dw.h"
@@ -773,8 +775,7 @@
         if(ext)
             jext = env->NewStringUTF(defpath);
         // First get the class that contains the method you need to call
-        //jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
-        jclass clazz = env->FindClass(DW_CLASS_NAME);
+        jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
         // Get the method that you want to call
         jmethodID fileBrowse = env->GetMethodID(clazz, "fileBrowse",
                                                 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)Ljava/lang/String;");
@@ -3123,6 +3124,19 @@
  */
 HWND API dw_bitmap_new(ULONG cid)
 {
+    JNIEnv *env;
+
+    if((env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        // First get the class that contains the method you need to call
+        jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
+        // Get the method that you want to call
+        jmethodID mleNew = env->GetMethodID(clazz, "bitmapNew",
+                                            "(I)Landroid/widget/ImageView;");
+        // Call the method on the object
+        jobject result = env->NewWeakGlobalRef(env->CallObjectMethod(_dw_obj, mleNew, (int)cid));
+        return result;
+    }
     return 0;
 }
 
@@ -4148,6 +4162,26 @@
  */
 void API dw_window_set_bitmap_from_data(HWND handle, unsigned long cid, const char *data, int len)
 {
+    JNIEnv *env;
+
+    if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        // Construct a byte array
+        jbyteArray bytearray = NULL;
+        if(data && len > 0) {
+            bytearray = env->NewByteArray(len);
+            env->SetByteArrayRegion(bytearray, 0, len, reinterpret_cast<const jbyte *>(data));
+        }
+        // First get the class that contains the method you need to call
+        jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
+        // Get the method that you want to call
+        jmethodID windowSetBitmapFromData = env->GetMethodID(clazz, "windowSetBitmapFromData",
+                                                             "(Landroid/view/View;I[BI)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, windowSetBitmapFromData, handle, (int)cid, bytearray, len);
+        // Clean up after the array now that we are finished
+        //env->ReleaseByteArrayElements(bytearray, (jbyte *) data, 0);
+    }
 }
 
 /*
@@ -4162,6 +4196,23 @@
  */
 void API dw_window_set_bitmap(HWND handle, unsigned long resid, const char *filename)
 {
+    JNIEnv *env;
+
+    if(handle && (env = (JNIEnv *)pthread_getspecific(_dw_env_key)))
+    {
+        // Construct a string
+        jstring jstr = NULL;
+        if(filename) {
+            jstr = env->NewStringUTF(filename);
+        }
+        // First get the class that contains the method you need to call
+        jclass clazz = _dw_find_class(env, DW_CLASS_NAME);
+        // Get the method that you want to call
+        jmethodID windowSetBitmapFromData = env->GetMethodID(clazz, "windowSetBitmap",
+                                                             "(Landroid/view/View;ILjava/lang/String;)V");
+        // Call the method on the object
+        env->CallVoidMethod(_dw_obj, windowSetBitmapFromData, handle, (int)resid, jstr);
+    }
 }
 
 /*
@@ -5726,7 +5777,6 @@
         case DW_FEATURE_DARK_MODE:               /* Supports Dark Mode user interface */
         case DW_FEATURE_NOTIFICATION:            /* Supports sending system notifications */
         case DW_FEATURE_UTF8_UNICODE:            /* Supports UTF8 encoded Unicode text */
-        case DW_FEATURE_MLE_AUTO_COMPLETE:       /* Supports auto completion in Multi-line Edit boxes */
         case DW_FEATURE_MLE_WORD_WRAP:           /* Supports word wrapping in Multi-line Edit boxes */
         case DW_FEATURE_CONTAINER_STRIPE:        /* Supports striped line display in container widgets */
             return DW_FEATURE_ENABLED;
@@ -5758,7 +5808,6 @@
         case DW_FEATURE_DARK_MODE:               /* Supports Dark Mode user interface */
         case DW_FEATURE_NOTIFICATION:            /* Supports sending system notifications */
         case DW_FEATURE_UTF8_UNICODE:            /* Supports UTF8 encoded Unicode text */
-        case DW_FEATURE_MLE_AUTO_COMPLETE:       /* Supports auto completion in Multi-line Edit boxes */
         case DW_FEATURE_MLE_WORD_WRAP:           /* Supports word wrapping in Multi-line Edit boxes */
         case DW_FEATURE_CONTAINER_STRIPE:        /* Supports striped line display in container widgets */
             return DW_ERROR_GENERAL;