From a104ffe6aad2ba67870c799cda633976a2d276f6 Mon Sep 17 00:00:00 2001 From: Jacques Foucry Date: Fri, 31 Mar 2023 18:34:04 +0200 Subject: [PATCH] Add WRITE_EXTERNAL_STORAGE permission --- app/src/main/AndroidManifest.xml | 1 + .../main/java/net/foucry/pilldroid/Utils.java | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3efe77c..a8a0d6e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,7 @@ + ActivityCompat.requestPermissions(this, new String[] { + Manifest.permission.WRITE_EXTERNAL_STORAGE + }, 0)).show(); + } + } + + public void restoreDB () + { + int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE); + if(permission == PackageManager.PERMISSION_GRANTED) { + //AppDatabase.getInstance(this).getDatabase().close(); + PrescriptionDatabase.getInstanceDatabase(this).getDatabase().close(); + + File db = new File("/sdcard/", "PrescriptionDatabaseb"); + File dbShm = new File(db.getParent(), "PrescriptionDatabase-shm"); + File dbWal = new File(db.getParent(), "PrescriptionDatabase-wal"); + + File db2 = getDatabasePath("PrescriptionDatabase"); + File dbShm2 = new File(db2.getParent(), "PrescriptionDatabase-shm"); + File dbWal2 = new File(db2.getParent(), "PrescriptionDatabase-wal"); + + try { + FileUtils.copyFile(db, db2); + FileUtils.copyFile(dbShm, dbShm2); + FileUtils.copyFile(dbWal, dbWal2); + } catch (Exception e) { + Loge("RESTOREDB", e.toString()); + } + } else { + Snackbar.make(mDrawer, "Please allow access to your storage", Snackbar.LENGTH_LONG) + .setAction("Allow", view -> ActivityCompat.requestPermissions(this, new String[] { + Manifest.permission.READ_EXTERNAL_STORAGE + }, 0)).show(); + } + } + }