|
5 | 5 | import android.content.Context;
|
6 | 6 | import android.content.SharedPreferences;
|
7 | 7 | import android.content.res.Resources;
|
| 8 | +import android.database.Cursor; |
| 9 | +import android.net.Uri; |
8 | 10 | import android.os.Build;
|
9 | 11 | import android.os.Environment;
|
| 12 | +import android.provider.DocumentsContract; |
10 | 13 | import android.util.Log;
|
| 14 | +import android.widget.Toast; |
11 | 15 |
|
12 | 16 | import androidx.core.app.ActivityCompat;
|
13 | 17 | import androidx.core.content.ContextCompat;
|
|
17 | 21 | import com.samsung.microbit.data.constants.Constants;
|
18 | 22 | import com.samsung.microbit.data.constants.FileConstants;
|
19 | 23 | import com.samsung.microbit.data.model.Project;
|
| 24 | +import com.samsung.microbit.ui.activity.ProjectActivity; |
20 | 25 |
|
21 | 26 | import java.io.BufferedOutputStream;
|
| 27 | +import java.io.BufferedReader; |
| 28 | +import java.io.ByteArrayInputStream; |
22 | 29 | import java.io.File;
|
23 | 30 | import java.io.FileInputStream;
|
24 | 31 | import java.io.FileOutputStream;
|
25 | 32 | import java.io.FilenameFilter;
|
26 | 33 | import java.io.IOException;
|
| 34 | +import java.io.InputStream; |
| 35 | +import java.io.InputStreamReader; |
| 36 | +import java.io.OutputStream; |
| 37 | +import java.net.URLDecoder; |
27 | 38 | import java.util.HashMap;
|
28 | 39 | import java.util.List;
|
29 | 40 | import java.util.zip.ZipEntry;
|
@@ -243,4 +254,113 @@ public static boolean copyDownloads( Context context) {
|
243 | 254 | }
|
244 | 255 | return true;
|
245 | 256 | }
|
| 257 | + |
| 258 | + public static boolean writeStringToProjectHex( String hex, String fileName, Context ctx) |
| 259 | + { |
| 260 | + boolean ok = true; |
| 261 | + |
| 262 | + File file = ProjectsHelper.projectFile( ctx, fileName); |
| 263 | + |
| 264 | + try { |
| 265 | + byte[] bytes = hex.getBytes(); |
| 266 | + |
| 267 | + if ( file.exists()) { |
| 268 | + file.delete(); |
| 269 | + } |
| 270 | + file.createNewFile(); |
| 271 | + |
| 272 | + FileOutputStream os = new FileOutputStream(file); |
| 273 | + os.write( bytes); |
| 274 | + os.flush(); |
| 275 | + os.close(); |
| 276 | + } catch ( Exception e) { |
| 277 | + ok = false; |
| 278 | + } |
| 279 | + return ok; |
| 280 | + } |
| 281 | + |
| 282 | + public static enum enumImportResult { |
| 283 | + Success, |
| 284 | + NotFound, |
| 285 | + ReadFailed, |
| 286 | + NotHex, |
| 287 | + AlreadyExists, |
| 288 | + WriteFailed |
| 289 | + } |
| 290 | + |
| 291 | + public static class ProjectsHelperImportResult { |
| 292 | + |
| 293 | + public enumImportResult result; |
| 294 | + public File file; |
| 295 | + |
| 296 | + public ProjectsHelperImportResult( enumImportResult resultIn) { |
| 297 | + super(); |
| 298 | + result = resultIn; |
| 299 | + file = null; |
| 300 | + } |
| 301 | + |
| 302 | + public ProjectsHelperImportResult( enumImportResult resultIn, File fileIn) { |
| 303 | + super(); |
| 304 | + result = resultIn; |
| 305 | + file = fileIn; |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + public static ProjectsHelperImportResult importToProjectsWork(Uri uri, Context ctx) { |
| 310 | + if ( uri == null) { |
| 311 | + return new ProjectsHelperImportResult( ProjectsHelper.enumImportResult.NotFound); |
| 312 | + } |
| 313 | + |
| 314 | + byte [] bytes = FileUtils.readBytesFromUri( uri, ctx); |
| 315 | + if ( bytes == null) { |
| 316 | + return new ProjectsHelperImportResult( ProjectsHelper.enumImportResult.ReadFailed); |
| 317 | + } |
| 318 | + |
| 319 | + if ( !FileUtils.isHex( bytes)) { |
| 320 | + return new ProjectsHelperImportResult( ProjectsHelper.enumImportResult.NotHex); |
| 321 | + } |
| 322 | + |
| 323 | + String fileName = FileUtils.fileNameFromUri( uri, ctx); |
| 324 | + if ( fileName == null) { |
| 325 | + fileName = "import.hex"; |
| 326 | + } |
| 327 | + |
| 328 | + File file = ProjectsHelper.projectFile( ctx, fileName); |
| 329 | + if ( file.exists()) { |
| 330 | + return new ProjectsHelperImportResult( ProjectsHelper.enumImportResult.AlreadyExists); |
| 331 | + } |
| 332 | + |
| 333 | + boolean saved = FileUtils.writeBytesToFile( file, bytes); |
| 334 | + if ( !saved) { |
| 335 | + if ( file.exists()) { |
| 336 | + file.delete(); |
| 337 | + } |
| 338 | + return new ProjectsHelperImportResult( ProjectsHelper.enumImportResult.WriteFailed); |
| 339 | + } |
| 340 | + |
| 341 | + return new ProjectsHelperImportResult( ProjectsHelper.enumImportResult.Success, file); |
| 342 | + } |
| 343 | + |
| 344 | + public static void importToProjectsToast(ProjectsHelper.enumImportResult result, Context ctx) { |
| 345 | + switch ( result) { |
| 346 | + case Success: |
| 347 | + Toast.makeText( ctx, "micro:bit HEX imported", Toast.LENGTH_LONG).show(); |
| 348 | + break; |
| 349 | + case NotFound: |
| 350 | + Toast.makeText( ctx, "File not found", Toast.LENGTH_LONG).show(); |
| 351 | + break; |
| 352 | + case ReadFailed: |
| 353 | + Toast.makeText( ctx, "Could not read micro:bit HEX", Toast.LENGTH_LONG).show(); |
| 354 | + break; |
| 355 | + case NotHex: |
| 356 | + Toast.makeText( ctx, "Not a micro:bit HEX", Toast.LENGTH_LONG).show(); |
| 357 | + break; |
| 358 | + case AlreadyExists: |
| 359 | + Toast.makeText( ctx, "A project with the same name already exists", Toast.LENGTH_LONG).show(); |
| 360 | + break; |
| 361 | + case WriteFailed: |
| 362 | + Toast.makeText( ctx, "Could not store micro:bit HEX", Toast.LENGTH_LONG).show(); |
| 363 | + break; |
| 364 | + } |
| 365 | + } |
246 | 366 | }
|
0 commit comments