Skip to content

Commit 1acc2f2

Browse files
committed
MakeCode/Projects - import, save, download
1 parent ec50459 commit 1acc2f2

File tree

3 files changed

+150
-310
lines changed

3 files changed

+150
-310
lines changed

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

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
import android.webkit.WebSettings;
1919
import android.webkit.WebView;
2020
import android.webkit.WebViewClient;
21+
import android.widget.Toast;
2122

2223
import com.samsung.microbit.BuildConfig;
2324
import com.samsung.microbit.R;
25+
import com.samsung.microbit.utils.FileUtils;
2426
import com.samsung.microbit.utils.ProjectsHelper;
2527

2628
import java.io.File;
@@ -43,10 +45,10 @@ public class MakeCodeWebView extends Activity implements View.OnClickListener {
4345
public static Activity activityHandle = null;
4446

4547
boolean projectDownload = false;
46-
Uri hexToFlash;
4748

4849
private static final int REQUEST_CODE_SAVEDATA = 1;
4950
private static final int REQUEST_CODE_CHOOSE_FILE = 2;
51+
private static final int REQUEST_CODE_FLASH = 3;
5052
private byte[] dataToSave = null;
5153
private ValueCallback<Uri[]> onShowFileChooser_filePathCallback;
5254

@@ -198,33 +200,33 @@ public void onReceiveValue(String s) {
198200
else if ( !hexName.isEmpty()) {
199201
hexToWrite = getProjectFile(hexName);
200202

201-
/*
202-
// Append n to file until it doesn't exist
203-
int i = 0;
204-
205-
while (hexToWrite.exists()) {
206-
hexName = hexName.replaceAll("-?\\d*\\.","-" + i + ".");
207-
hexToWrite = getProjectFile( hexName);
208-
i++;
209-
}
210-
*/
211-
// Replace existing file rather than creating *-n.hex
212-
if (hexToWrite.exists()) {
213-
hexToWrite.delete();
203+
// // Replace existing file rather than creating *-n.hex
204+
// // Append n to file until it doesn't exist
205+
// int i = 0;
206+
//
207+
// while (hexToWrite.exists()) {
208+
// hexName = hexName.replaceAll("-?\\d*\\.","-" + i + ".");
209+
// hexToWrite = getProjectFile( hexName);
210+
// i++;
211+
// }
212+
213+
if ( !FileUtils.writeBytesToFile( hexToWrite, decode)) {
214+
ProjectsHelper.importToProjectsToast(
215+
ProjectsHelper.enumImportResult.WriteFailed, MakeCodeWebView.this);
216+
return;
214217
}
215218

216-
// Create file
217-
hexToWrite.createNewFile();
218-
outputStream = new FileOutputStream(hexToWrite);
219-
outputStream.write(decode);
220-
outputStream.flush();
219+
boolean download = projectDownload;
220+
projectDownload = false;
221221

222-
// Get file path
223-
hexToFlash = Uri.fromFile(hexToWrite);
224-
225-
openProjectActivity();
222+
if ( download) {
223+
openProjectActivity( hexToWrite);
224+
} else {
225+
Toast.makeText( MakeCodeWebView.this,
226+
"Saved to FLASH page", Toast.LENGTH_LONG).show();
227+
}
226228
}
227-
} catch (IOException e) {
229+
} catch ( Exception e) {
228230
e.printStackTrace();
229231
}
230232
}
@@ -233,10 +235,13 @@ else if ( !hexName.isEmpty()) {
233235
//Check parameters Before load
234236
Intent intent = getIntent();
235237

236-
importHex = intent.getStringExtra("importHex");
237-
importName = intent.getStringExtra("importName");
238-
239-
importInitialise();
238+
boolean importExtra = intent.getBooleanExtra("import", false);
239+
if ( importExtra) {
240+
importInitialise();
241+
} else {
242+
importHex = null;
243+
importName = null;
244+
}
240245

241246
webView.loadUrl(makecodeUrl);
242247
} // onCreate
@@ -254,27 +259,22 @@ private void saveData( String name, String mimetype, byte[] data) {
254259
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
255260
super.onActivityResult(requestCode, resultCode, data);
256261

257-
if ( requestCode == REQUEST_CODE_SAVEDATA) {
262+
if ( requestCode == REQUEST_CODE_FLASH) {
263+
if ( resultCode != RESULT_OK) {
264+
return;
265+
}
266+
} else if ( requestCode == REQUEST_CODE_SAVEDATA) {
258267
if ( resultCode != RESULT_OK) {
259268
dataToSave = null;
260269
return;
261270
}
262-
OutputStream os = null;
263-
try {
264-
os = getContentResolver().openOutputStream( data.getData());
265-
if ( dataToSave != null && dataToSave.length > 0) {
266-
os.write(dataToSave, 0, dataToSave.length);
271+
if ( dataToSave != null && dataToSave.length > 0) {
272+
Uri uri = data.getData();
273+
if ( !FileUtils.writeBytesToUri( uri, dataToSave, this)) {
274+
Toast.makeText(this, "Could not save file", Toast.LENGTH_LONG).show();
267275
}
268-
} catch (IOException e) {
269-
e.printStackTrace();
270-
} finally {
271-
try {
272-
dataToSave = null;
273-
if ( os != null) {
274-
os.close();
275-
}
276-
} catch (IOException e) { }
277276
}
277+
dataToSave = null;
278278
} else if (requestCode == REQUEST_CODE_CHOOSE_FILE) {
279279
if ( resultCode != RESULT_OK) {
280280
onShowFileChooser_filePathCallback.onReceiveValue( null);
@@ -305,14 +305,17 @@ public void onClick(final View v) {
305305
}
306306
}
307307

308-
void openProjectActivity() {
308+
public final static String ACTION_FLASH = "com.samsung.microbit.ACTION_FLASH";
309+
310+
void openProjectActivity( File hexToWrite) {
309311
Intent i = new Intent(this, ProjectActivity.class);
310-
i.setData(hexToFlash);
311-
startActivity(i);
312+
i.setAction( ACTION_FLASH);
313+
i.putExtra("path", hexToWrite.getAbsolutePath());
314+
startActivityForResult( i, REQUEST_CODE_FLASH);
312315
}
313316

314-
private static String importHex = null;
315-
private static String importName = null;
317+
public static String importHex = null;
318+
public static String importName = null;
316319
private boolean importPosting = false;
317320
Handler importHandler = null;
318321

0 commit comments

Comments
 (0)