Skip to content

Commit 73f556f

Browse files
committed
uPy support
1 parent 7d58035 commit 73f556f

File tree

7 files changed

+96
-32
lines changed

7 files changed

+96
-32
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 29
5-
buildToolsVersion '28.0.3'
5+
buildToolsVersion '29.0.2'
66
defaultConfig {
77
applicationId "com.samsung.microbit"
88
minSdkVersion 21
@@ -29,7 +29,7 @@ dependencies {
2929
implementation 'androidx.cardview:cardview:1.0.0'
3030
implementation 'androidx.recyclerview:recyclerview:1.0.0'
3131
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.19'
32-
implementation 'no.nordicsemi.android:dfu:1.10.4'
32+
implementation 'no.nordicsemi.android:dfu:1.11.1'
3333
implementation project(':pfLibrary')
3434
implementation 'com.google.android.gms:play-services-analytics:9.2.0'
3535
implementation 'com.google.code.gson:gson:2.8.2'

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
-->
1616
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
1717
package="com.samsung.microbit"
18-
android:versionCode="33"
19-
android:versionName="2.8.2">
18+
android:versionCode="38"
19+
android:versionName="2.8.8">
2020

2121
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2222
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

app/src/main/java/com/samsung/microbit/ui/activity/MakeCodeWebView.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.samsung.microbit.ui.activity;
22

33
import android.app.Activity;
4+
import android.content.Context;
45
import android.content.Intent;
56
import android.net.Uri;
67
import android.os.Bundle;
78
import android.os.Environment;
89
import android.util.Base64;
10+
import android.util.Log;
911
import android.view.View;
1012
import android.webkit.DownloadListener;
13+
import android.webkit.JavascriptInterface;
14+
import android.webkit.ValueCallback;
1115
import android.webkit.WebSettings;
1216
import android.webkit.WebView;
1317
import android.webkit.WebViewClient;
@@ -19,13 +23,16 @@
1923
import java.io.FileOutputStream;
2024
import java.io.IOException;
2125

26+
import static android.content.ContentValues.TAG;
27+
2228
/**
2329
* Displays MakeCode
2430
*/
2531
public class MakeCodeWebView extends Activity implements View.OnClickListener {
2632

2733
private WebView webView;
28-
public static String makecodeUrl = "https://makecode.microbit.org/beta?androidapp=" + BuildConfig.VERSION_CODE;
34+
public static String makecodeUrl = "https://makecode.microbit.org/?androidapp=" + BuildConfig.VERSION_CODE;
35+
public static Activity activityHandle = null;
2936

3037
Uri hexToFlash;
3138

@@ -47,6 +54,8 @@ protected void onStop() {
4754
protected void onCreate(Bundle savedInstanceState) {
4855
super.onCreate(savedInstanceState);
4956

57+
activityHandle = this;
58+
5059
setContentView(R.layout.activity_help_web_view);
5160
webView = (WebView) findViewById(R.id.generalView);
5261

@@ -63,18 +72,40 @@ protected void onCreate(Bundle savedInstanceState) {
6372
webSettings.setDomStorageEnabled(true);
6473
webView.setWebContentsDebuggingEnabled(true);
6574

75+
webView.addJavascriptInterface(new JavaScriptInterface(this), "AndroidFunction");
76+
webView.evaluateJavascript("javascript:(function f() { document.getElementsByClassName(\"brand\")[0].addEventListener(\"click\", function(e) { AndroidFunction.returnToHome(); e.preventDefault(); return false; }) })()", new ValueCallback<String>() {
77+
@Override
78+
public void onReceiveValue(String s) {
79+
Log.d(TAG, s);
80+
}
81+
});
82+
6683
webView.setWebViewClient(new WebViewClient() {
6784
@Override
6885
public boolean shouldOverrideUrlLoading(WebView view, String url) {
86+
Log.v(TAG, "url: " + url);
87+
if(url.contains("https://microbit.org/")) activityHandle.finish();
6988
return false;
7089
}
90+
@Override
91+
public void onLoadResource (WebView view, String url) {
92+
super.onLoadResource(view, url);
93+
Log.v(TAG, "onLoadResource("+url+");");
94+
}
7195
});
7296

7397
webView.setDownloadListener(new DownloadListener() {
7498
public void onDownloadStart(String url, String userAgent,
7599
String contentDisposition, String mimetype,
76100
long contentLength) {
77101

102+
webView.evaluateJavascript("javascript:(function f() { document.getElementsByClassName(\"close\")[0].click(); } )()", new ValueCallback<String>() {
103+
@Override
104+
public void onReceiveValue(String s) {
105+
Log.d(TAG, s);
106+
}
107+
});
108+
78109
File hexToWrite;
79110
FileOutputStream outputStream;
80111
String hexName = "init";
@@ -92,6 +123,7 @@ public void onDownloadStart(String url, String userAgent,
92123
try {
93124
hexToWrite = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + hexName);
94125

126+
/*
95127
// Append n to file until it doesn't exist
96128
int i = 0;
97129
@@ -100,6 +132,11 @@ public void onDownloadStart(String url, String userAgent,
100132
hexToWrite = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + hexName);
101133
i++;
102134
}
135+
*/
136+
// Replace existing file rather than creating *-n.hex
137+
if(hexToWrite.exists()) {
138+
hexToWrite.delete();
139+
}
103140

104141
// Create file
105142
hexToWrite.createNewFile();
@@ -144,3 +181,21 @@ void openProjectActivity() {
144181
startActivity(i);
145182
}
146183
}
184+
185+
/* Javascript Interface */
186+
class JavaScriptInterface {
187+
Context mContext;
188+
189+
JavaScriptInterface(Context c) {
190+
mContext = c;
191+
}
192+
193+
@JavascriptInterface
194+
public void returnToHome() {
195+
try {
196+
MakeCodeWebView.activityHandle.finish();
197+
} catch(Exception e) {
198+
Log.v(TAG, e.toString());
199+
}
200+
}
201+
}

app/src/main/java/com/samsung/microbit/ui/activity/PairingActivity.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,16 +1052,22 @@ public void onClick(View v) {
10521052
* proceed with using bluetooth otherwise.
10531053
*/
10541054
private void checkBluetoothPermissions() {
1055+
Log.v(TAG, "checkBluetoothPermissions");
10551056
//TODO: shouldn't it be BLUETOOTH permission?
1056-
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
1057-
!= PermissionChecker.PERMISSION_GRANTED) {
1057+
if((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
1058+
!= PermissionChecker.PERMISSION_GRANTED)
1059+
|| (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
1060+
!= PermissionChecker.PERMISSION_GRANTED)
1061+
|| (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
1062+
!= PermissionChecker.PERMISSION_GRANTED)) {
10581063
PopUp.show(getString(R.string.location_permission_pairing),
10591064
getString(R.string.permissions_needed_title),
10601065
R.drawable.message_face, R.drawable.blue_btn, PopUp.GIFF_ANIMATION_NONE,
10611066
PopUp.TYPE_CHOICE,
10621067
bluetoothPermissionOKHandler,
10631068
bluetoothPermissionCancelHandler);
10641069
} else {
1070+
Log.v(TAG, "skipped");
10651071
proceedAfterBlePermissionGranted();
10661072
}
10671073
}
@@ -1263,6 +1269,12 @@ private void startScanning() {
12631269
private void scanLeDevice(final boolean enable) {
12641270
logi("scanLeDevice() :: enable = " + enable);
12651271

1272+
if(!BluetoothChecker.getInstance().isBluetoothON()) {
1273+
setActivityState(PairingActivityState.STATE_ENABLE_BT_FOR_CONNECT);
1274+
enableBluetooth();
1275+
return;
1276+
}
1277+
12661278
if(enable) {
12671279
//Start scanning.
12681280
if(!setupBleController()) {
@@ -1281,7 +1293,12 @@ private void scanLeDevice(final boolean enable) {
12811293
} else {
12821294
List<ScanFilter> filters = new ArrayList<>();
12831295
// TODO: play with ScanSettings further to ensure the Kit kat devices connectMaybeInit with higher success rate
1284-
ScanSettings settings = new ScanSettings.Builder().setLegacy(true).setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
1296+
ScanSettings settings;
1297+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
1298+
settings = new ScanSettings.Builder().setLegacy(true).setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
1299+
} else {
1300+
settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
1301+
}
12851302
leScanner.startScan(filters, settings, getNewScanCallback());
12861303
}
12871304
}
@@ -1495,25 +1512,11 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
14951512

14961513
BluetoothGatt gatt = device.connectGatt(this, true, bluetoothGattCallback);
14971514
synchronized (lock) { // Wait for services to be discovered
1498-
lock.wait();
1515+
lock.wait(10000); // 10 second max
14991516
}
15001517

15011518
stopScanning();
15021519

1503-
/* Rebond */
1504-
Log.v(TAG, "rebond, just in case");
1505-
try {
1506-
Method m = device.getClass().getMethod("removeBond", (Class[]) null);
1507-
m.invoke(device, (Object[]) null);
1508-
} catch (Exception e) { Log.e(TAG, e.getMessage()); }
1509-
1510-
// Sleep for 2000ms
1511-
try {
1512-
Thread.sleep(2000);
1513-
} catch (InterruptedException e) {
1514-
e.printStackTrace();
1515-
}
1516-
15171520
// Create bond
15181521
boolean newbond = device.createBond();
15191522
Log.v(TAG, "create bond: newbond");

app/src/main/java/com/samsung/microbit/ui/activity/PopUpActivity.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,19 @@ private void clearLayout() {
269269
private void setLayout(Intent intent) {
270270
String title = intent.getStringExtra(INTENT_EXTRA_TITLE);
271271

272-
if(!title.isEmpty()) {
273-
titleTxt.setText(title);
274-
titleTxt.setVisibility(View.VISIBLE);
272+
if(title != null) {
273+
if (!title.isEmpty()) {
274+
titleTxt.setText(title);
275+
titleTxt.setVisibility(View.VISIBLE);
276+
}
275277
}
276278

277279
String message = intent.getStringExtra(INTENT_EXTRA_MESSAGE);
278-
if(!message.isEmpty()) {
279-
messageTxt.setText(message);
280-
messageTxt.setVisibility(View.VISIBLE);
280+
if(message != null) {
281+
if (!message.isEmpty()) {
282+
messageTxt.setText(message);
283+
messageTxt.setVisibility(View.VISIBLE);
284+
}
281285
}
282286

283287
int imageResId = intent.getIntExtra(INTENT_EXTRA_ICON, 0);

app/src/main/java/com/samsung/microbit/ui/activity/ProjectActivity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,9 +1194,11 @@ protected void startFlashing(int flashingType) {
11941194
.setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(true)
11951195
.setDeviceName(currentMicrobit.mName)
11961196
.setPacketsReceiptNotificationsEnabled(true)
1197+
.setNumberOfRetries(2)
1198+
.setDisableNotification(true)
1199+
.setRestoreBond(true)
11971200
.setKeepBond(true)
11981201
.setForeground(true)
1199-
.setNumberOfRetries(3)
12001202
.setZip(this.getCacheDir() + "/update.zip");
12011203
final DfuServiceController controller = starter.start(this, DfuService.class);
12021204
} else {

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@
354354
<string name="storage_permission_for_programs">We need storage permission to list all the program files saved on your device.\n Proceed?</string>
355355
<string name="storage_permission_for_programs_error">Cannot display any program files.</string>
356356

357-
<string name="location_permission_pairing">We need location permission to search for and find your micro:bit.\n Proceed?</string>
357+
<string name="location_permission_pairing">We need permission to use your background location to enable BLE. This allows the app to search for and find your micro:bit.\n Proceed?</string>
358358
<string name="location_permission_error">Cannot pair new micro:bit without having location permission.</string>
359359

360360
<string name="camera_permission">micro:bit is trying to launch Camera. We need Camera permissions to complete this action. \n Proceed?</string>
@@ -381,7 +381,7 @@
381381
<string name="v1_button_label">V1</string>
382382
<string name="v2_button_label">V2</string>
383383

384-
<string name="no_longer_required_to_connect">It is not longer necessary to connect to your micro:bit before flashing</string>
384+
<string name="no_longer_required_to_connect">It is no longer necessary to connect to your micro:bit before flashing</string>
385385

386386
</resources>
387387

0 commit comments

Comments
 (0)