18
18
import android .webkit .WebSettings ;
19
19
import android .webkit .WebView ;
20
20
import android .webkit .WebViewClient ;
21
+ import android .widget .Toast ;
21
22
22
23
import com .samsung .microbit .BuildConfig ;
23
24
import com .samsung .microbit .R ;
25
+ import com .samsung .microbit .utils .FileUtils ;
24
26
import com .samsung .microbit .utils .ProjectsHelper ;
25
27
26
28
import java .io .File ;
@@ -43,10 +45,10 @@ public class MakeCodeWebView extends Activity implements View.OnClickListener {
43
45
public static Activity activityHandle = null ;
44
46
45
47
boolean projectDownload = false ;
46
- Uri hexToFlash ;
47
48
48
49
private static final int REQUEST_CODE_SAVEDATA = 1 ;
49
50
private static final int REQUEST_CODE_CHOOSE_FILE = 2 ;
51
+ private static final int REQUEST_CODE_FLASH = 3 ;
50
52
private byte [] dataToSave = null ;
51
53
private ValueCallback <Uri []> onShowFileChooser_filePathCallback ;
52
54
@@ -198,33 +200,33 @@ public void onReceiveValue(String s) {
198
200
else if ( !hexName .isEmpty ()) {
199
201
hexToWrite = getProjectFile (hexName );
200
202
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 ;
214
217
}
215
218
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 ;
221
221
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
+ }
226
228
}
227
- } catch (IOException e ) {
229
+ } catch ( Exception e ) {
228
230
e .printStackTrace ();
229
231
}
230
232
}
@@ -233,10 +235,13 @@ else if ( !hexName.isEmpty()) {
233
235
//Check parameters Before load
234
236
Intent intent = getIntent ();
235
237
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
+ }
240
245
241
246
webView .loadUrl (makecodeUrl );
242
247
} // onCreate
@@ -254,27 +259,22 @@ private void saveData( String name, String mimetype, byte[] data) {
254
259
protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
255
260
super .onActivityResult (requestCode , resultCode , data );
256
261
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 ) {
258
267
if ( resultCode != RESULT_OK ) {
259
268
dataToSave = null ;
260
269
return ;
261
270
}
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 ();
267
275
}
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 ) { }
277
276
}
277
+ dataToSave = null ;
278
278
} else if (requestCode == REQUEST_CODE_CHOOSE_FILE ) {
279
279
if ( resultCode != RESULT_OK ) {
280
280
onShowFileChooser_filePathCallback .onReceiveValue ( null );
@@ -305,14 +305,17 @@ public void onClick(final View v) {
305
305
}
306
306
}
307
307
308
- void openProjectActivity () {
308
+ public final static String ACTION_FLASH = "com.samsung.microbit.ACTION_FLASH" ;
309
+
310
+ void openProjectActivity ( File hexToWrite ) {
309
311
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 );
312
315
}
313
316
314
- private static String importHex = null ;
315
- private static String importName = null ;
317
+ public static String importHex = null ;
318
+ public static String importName = null ;
316
319
private boolean importPosting = false ;
317
320
Handler importHandler = null ;
318
321
0 commit comments