From 585979c99587e47e06dd540675830e718376c327 Mon Sep 17 00:00:00 2001 From: Martin Williams Date: Fri, 1 Mar 2024 12:41:13 +0000 Subject: [PATCH 01/13] MakeCode - support for hex file import --- .../microbit/ui/activity/MakeCodeWebView.java | 90 +++++++++++++++++++ .../microbit/ui/activity/ProjectActivity.java | 36 ++++++++ .../com/samsung/microbit/utils/BLEPair.java | 2 +- 3 files changed, 127 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/samsung/microbit/ui/activity/MakeCodeWebView.java b/app/src/main/java/com/samsung/microbit/ui/activity/MakeCodeWebView.java index 124085e0..8858fc48 100644 --- a/app/src/main/java/com/samsung/microbit/ui/activity/MakeCodeWebView.java +++ b/app/src/main/java/com/samsung/microbit/ui/activity/MakeCodeWebView.java @@ -6,6 +6,8 @@ import android.net.Uri; import android.os.Bundle; import android.os.Environment; +import android.os.Handler; +import android.os.Looper; import android.util.Base64; import android.util.Log; import android.view.View; @@ -228,6 +230,12 @@ else if ( !hexName.isEmpty()) { //Check parameters Before load Intent intent = getIntent(); + + importHex = intent.getStringExtra("importHex"); + importName = intent.getStringExtra("importName"); + + importInitialise(); + webView.loadUrl(makecodeUrl); } // onCreate @@ -300,6 +308,88 @@ void openProjectActivity() { i.setData(hexToFlash); startActivity(i); } + + private static String importHex = null; + private static String importName = null; + private boolean importPosting = false; + Handler importHandler = null; + + private void importInitialise() { + if ( importHex != null) { + importHex = importHex.replaceAll("\r\n", "\\\\n"); + importHex = importHex.replaceAll("\r", "\\\\n"); + importHex = importHex.replaceAll("\n", "\\\\n"); + + //TODO - does MakeCode signal when ready? + Looper looper = Looper.getMainLooper(); + importHandler = new Handler(looper); + importHandler.postDelayed(importCallback, 1000); + } + } + private final Runnable importCallback = new Runnable() { + @Override + public void run() { + if ( importHex != null) { + importPostMessage(); + importHandler.postDelayed( importCallback, 1000); + } else { + importHandler.removeCallbacks( importCallback); + importHandler = null; + } + } + }; + + public void importPostMessage() { + if ( importHex == null) { + return; + } + if ( importPosting) { + return; + } + if ( webView == null) { + return; + } + if ( importName == null || importName.isEmpty()) { + importName = "import.hex"; + } + + importPosting = true; + + String nl = "\n"; + String script = "javascript:("; + script += nl + "function f() { "; + script += nl + "var ret = 'OK'"; + script += nl + "try {"; + script += nl + "var loading = document.getElementById('loading')"; + script += nl + "if ( loading && loading.parentElement) {"; + script += nl + "ret = 'loading'"; + script += nl + "} else {"; + script += nl + "var name = \"" + importName + "\""; + script += nl + "var hex = \"" + importHex + "\""; + script += nl + "var msg = {"; + script += nl + "type: 'importfile',"; + script += nl + "filename: name,"; + script += nl + "parts: [ hex ]"; + script += nl + "}"; + script += nl + "window.postMessage( msg, '*')"; + script += nl + "}"; + script += nl + "} catch( err) {"; + script += nl + "ret = err.message"; + script += nl + "}"; + script += nl + "return ret;"; + script += nl + "}"; + script += nl + ")()"; + webView.evaluateJavascript( script, new ValueCallback() { + @Override + public void onReceiveValue(String s) { + Log.d(TAG, s); + if ( s != "loading") { + importHex = null; + } + } + }); + importPosting = false; + } } /* Javascript Interface */ diff --git a/app/src/main/java/com/samsung/microbit/ui/activity/ProjectActivity.java b/app/src/main/java/com/samsung/microbit/ui/activity/ProjectActivity.java index 2267c5b8..b3b022c6 100644 --- a/app/src/main/java/com/samsung/microbit/ui/activity/ProjectActivity.java +++ b/app/src/main/java/com/samsung/microbit/ui/activity/ProjectActivity.java @@ -66,6 +66,7 @@ import com.samsung.microbit.utils.Utils; import com.samsung.microbit.utils.irmHexUtils; +import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; @@ -2554,4 +2555,39 @@ private int scriptsExportSave( Uri uri) { } return ok ? 0 : 1; } + + public void makecodeEditProject(final Project project) { + File file = new File( project.filePath); + makecodeEditFile( file); + } + + private void makecodeEditFile( File file) { + String hex = readHexFileToString( file); + makecodeEditHex( hex, file.getName()); + } + + private void makecodeEditHex( String hex, String name) { + Intent launchMakeCodeIntent = new Intent(this, MakeCodeWebView.class); + if ( !hex.isEmpty()) { + launchMakeCodeIntent.putExtra("importHex", hex); + launchMakeCodeIntent.putExtra("importName", name); + } + startActivity(launchMakeCodeIntent); + } + + private String readHexFileToString( File file) { + String hex = ""; + + int size = (int) file.length(); + byte[] bytes = new byte[size]; + try { + BufferedInputStream buf = new BufferedInputStream( new FileInputStream(file)); + buf.read(bytes, 0, bytes.length); + buf.close(); + hex = new String( bytes); + } catch (Exception e) { + hex = ""; + } + return hex; + } } diff --git a/app/src/main/java/com/samsung/microbit/utils/BLEPair.java b/app/src/main/java/com/samsung/microbit/utils/BLEPair.java index ffd13608..00924549 100644 --- a/app/src/main/java/com/samsung/microbit/utils/BLEPair.java +++ b/app/src/main/java/com/samsung/microbit/utils/BLEPair.java @@ -103,7 +103,7 @@ public interface BLEPairCallback { public BLEPair( BLEPairCallback callback) { super(); paramCallback = callback; - mainLooperHandler = new Handler( Looper.getMainLooper());; + mainLooperHandler = new Handler( Looper.getMainLooper()); } public void stopScanAndPair() { From b3c7ab7b498e311e0214e9e857568bd6004f0d43 Mon Sep 17 00:00:00 2001 From: Martin Williams Date: Sun, 3 Mar 2024 17:40:38 +0000 Subject: [PATCH 02/13] Projects - add edit project button --- .../microbit/ui/adapter/ProjectAdapter.java | 18 +++++++++++++ app/src/main/res/drawable/black_btn.xml | 27 +++++++++++++++++++ .../layout/custom_project_items_button.xml | 2 +- .../res/layout/custom_project_items_edit.xml | 27 +++++++++++++++++++ app/src/main/res/layout/project_items.xml | 3 +++ app/src/main/res/values/colors.xml | 3 +++ app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/values/styles.xml | 8 ++++++ 8 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 app/src/main/res/drawable/black_btn.xml create mode 100644 app/src/main/res/layout/custom_project_items_edit.xml diff --git a/app/src/main/java/com/samsung/microbit/ui/adapter/ProjectAdapter.java b/app/src/main/java/com/samsung/microbit/ui/adapter/ProjectAdapter.java index 9611e4f3..c3d44c55 100644 --- a/app/src/main/java/com/samsung/microbit/ui/adapter/ProjectAdapter.java +++ b/app/src/main/java/com/samsung/microbit/ui/adapter/ProjectAdapter.java @@ -292,6 +292,18 @@ public void onClick(View v) { } }; + /** + * Occurs when a user clicks on the Flash button on some project item. + * Sends clicked project to flash to a micro:bit board. + */ + private View.OnClickListener editBtnClickListener = new View.OnClickListener() { + + @Override + public void onClick(View v) { + logi("editBtnClickListener() :: "); + } + }; + /** * Occurs when a user clicks on the Delete button on some project item. * Shows a dialog window to confirm deletion. @@ -404,6 +416,12 @@ public View getView(int position, View convertView, ViewGroup parent) { sendBtnLayout.setTag(position); sendBtnLayout.setOnClickListener(sendBtnClickListener); + TextView editBtnText = (TextView) convertView.findViewById(R.id.project_edit_text); + editBtnText.setTypeface(MBApp.getApp().getRobotoTypeface()); + LinearLayout editBtnLayout = (LinearLayout) convertView.findViewById(R.id.editBtn); + editBtnLayout.setTag(position); + editBtnLayout.setOnClickListener(editBtnClickListener); + ImageButton deleteBtn = (ImageButton) convertView.findViewById(R.id.deleteBtn); deleteBtn.setTag(position); deleteBtn.setOnClickListener(deleteBtnClickListener); diff --git a/app/src/main/res/drawable/black_btn.xml b/app/src/main/res/drawable/black_btn.xml new file mode 100644 index 00000000..d06b8144 --- /dev/null +++ b/app/src/main/res/drawable/black_btn.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/custom_project_items_button.xml b/app/src/main/res/layout/custom_project_items_button.xml index cecca554..096aca6f 100644 --- a/app/src/main/res/layout/custom_project_items_button.xml +++ b/app/src/main/res/layout/custom_project_items_button.xml @@ -7,7 +7,7 @@ android:layout_height="@dimen/project_item_buttons_size" android:layout_marginEnd="@dimen/custom_button_end_margin" android:layout_marginStart="@dimen/custom_button_start_margin" - android:layout_weight="1" + android:layout_weight="2" android:background="@drawable/blue_btn" tools:showIn="@layout/project_items"> diff --git a/app/src/main/res/layout/custom_project_items_edit.xml b/app/src/main/res/layout/custom_project_items_edit.xml new file mode 100644 index 00000000..fbda9194 --- /dev/null +++ b/app/src/main/res/layout/custom_project_items_edit.xml @@ -0,0 +1,27 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/project_items.xml b/app/src/main/res/layout/project_items.xml index 07f4e4ea..c7cee98a 100644 --- a/app/src/main/res/layout/project_items.xml +++ b/app/src/main/res/layout/project_items.xml @@ -40,6 +40,9 @@ + + + #3bddff #1CC6FD #37abff + #000000 + #101010 + #101010 #16cffd #333333 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 83110298..63248c67 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -385,6 +385,8 @@ V2 It is no longer necessary to connect to your micro:bit before flashing + Edit project + Edit diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index bc9c15e5..ce478135 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -106,6 +106,14 @@ ?android:textAppearance + + +