| 7 | } |
| 8 | |
| 9 | export function createRandom(args) { |
| 10 | if (global.isAndroid) { |
| 11 | try { |
| 12 | const activity = Application.android.foregroundActivity as androidx.appcompat.app.AppCompatActivity; |
| 13 | const selection = [android.provider.MediaStore.MediaColumns.DISPLAY_NAME, android.provider.MediaStore.MediaColumns._ID]; |
| 14 | // testing with downloads as rename only works with a well know collection downloads/audio/photos/videos API 29+ |
| 15 | let cursor = activity.getContentResolver().query(android.provider.MediaStore.Downloads.getContentUri('external'), selection, null, null); |
| 16 | |
| 17 | let uri; |
| 18 | |
| 19 | while (cursor.moveToNext()) { |
| 20 | const index = cursor.getColumnIndex(selection[0]); |
| 21 | const name = cursor.getString(index); |
| 22 | if (name === 'ns_tmp.txt') { |
| 23 | const idIndex = cursor.getColumnIndex(selection[1]); |
| 24 | const id = cursor.getLong(idIndex); |
| 25 | uri = android.net.Uri.parse(`${android.provider.MediaStore.Downloads.getContentUri('external').toString()}/${id}`); |
| 26 | cursor.close(); |
| 27 | break; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | if (!uri) { |
| 32 | const values = new android.content.ContentValues(); |
| 33 | values.put(android.provider.MediaStore.MediaColumns.DISPLAY_NAME, 'ns_tmp.txt'); |
| 34 | values.put(android.provider.MediaStore.MediaColumns.MIME_TYPE, 'text/plain'); |
| 35 | uri = activity.getContentResolver().insert(android.provider.MediaStore.Downloads.getContentUri('external'), values); |
| 36 | } |
| 37 | |
| 38 | doWork(uri.toString()); |
| 39 | } catch (e) { |
| 40 | console.error(e); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | function doWork(path: string) { |
| 46 | try { |