1
1
package com.mikhailgrigorev.quickpassword.common.manager
2
2
3
3
import android.content.Context
4
- import android.os.Environment
5
- import android.util.Log
6
- import com.mikhailgrigorev.quickpassword.R
4
+ import android.content.Intent
5
+ import androidx.core.content.FileProvider
6
+ import com.mikhailgrigorev.quickpassword.data.database.FOLDER_CARD_DB_NAME
7
+ import com.mikhailgrigorev.quickpassword.data.database.PASSWORD_CARD_DB_NAME
7
8
import java.io.File
8
- import java.io.FileInputStream
9
- import java.io.FileOutputStream
10
- import javax.crypto.Cipher
11
- import javax.crypto.CipherInputStream
12
- import javax.crypto.CipherOutputStream
13
- import javax.crypto.spec.SecretKeySpec
14
9
15
10
16
11
object BackupManager {
17
- fun importEncryptedDB (context : Context , DB_NAME : String ): Int {
18
- try {
19
- val appName = context.resources.getString(R .string.app_name)
20
- val file =
21
- File (Environment .getExternalStorageDirectory().toString() + " /" + appName + " /" )
22
- if (! file.exists()) {
23
- file.mkdirs()
24
- }
25
- val sd = Environment .getExternalStorageDirectory()
26
- if (sd.canWrite()) {
27
- val data = Environment .getDataDirectory()
28
- val currentDBPath = " //data//" + context.packageName + " //databases//" + DB_NAME
29
- val backupDBPath = " /$appName /$DB_NAME "
30
- val currentDB = File (sd, backupDBPath)
31
- val backupDB = File (data, currentDBPath)
32
- val src = FileInputStream (currentDB)
33
- val dst = FileOutputStream (backupDB)
34
-
35
- val sks = SecretKeySpec (" 1234567890123456" .toByteArray(), " AES" )
36
- val cipher = Cipher .getInstance(" AES" )
37
- cipher.init (Cipher .DECRYPT_MODE , sks)
38
- val cis = CipherInputStream (src, cipher)
39
- var b: Int
40
- val d = ByteArray (8 )
41
- while (cis.read(d).also { b = it } != - 1 ) {
42
- dst.write(d, 0 , b)
43
- }
44
- dst.flush()
45
- src.close()
46
- dst.close()
47
- cis.close()
48
- Log .e(" IMPORT_DB" , " Database has been imported." )
49
- return 1
50
- } else {
51
- Log .e(" IMPORT_DB" , " No storage permission." )
52
- return 2
53
- }
54
- } catch (e: java.lang.Exception ) {
55
- e.printStackTrace()
56
- Log .e(" IMPORT_DB" , " Error importing database!" )
57
- return 3
12
+ fun generateFile (context : Context , fileName : String ): File ? {
13
+ val csvFile = File (context.filesDir, fileName)
14
+ csvFile.createNewFile()
15
+ return if (csvFile.exists()) {
16
+ csvFile
17
+ } else {
18
+ null
58
19
}
59
20
}
60
21
61
- fun exportEncryptedDB (context : Context , DB_NAME : String ): Int {
62
- try {
63
- val appName: String = context.resources.getString(R .string.app_name)
64
- val file =
65
- File (Environment .getExternalStorageDirectory().toString() + " /" + appName + " /" )
66
- if (! file.exists()) {
67
- file.mkdirs()
68
- }
69
- val sd: File = Environment .getExternalStorageDirectory()
70
- if (sd.canWrite()) {
71
- val data: File = Environment .getDataDirectory()
72
- val currentDBPath =
73
- " //data//" + context.packageName.toString() + " //databases//" + DB_NAME
74
- val backupDBPath = " /$appName /$DB_NAME "
75
- val currentDB = File (data, currentDBPath)
76
- val backupDB = File (sd, backupDBPath)
77
- val src = FileInputStream (currentDB)
78
- val dst = FileOutputStream (backupDB)
79
-
80
- val sks = SecretKeySpec (" 1234567890123456" .toByteArray(), " AES" )
22
+ fun goToFileIntent (context : Context , file : File ): Intent {
23
+ val intent = Intent (Intent .ACTION_VIEW )
24
+ val contentUri =
25
+ FileProvider .getUriForFile(context, " com.mikhailgrigorev.quickpassword.fileprovider" , file)
26
+ val mimeType = context.contentResolver.getType(contentUri)
27
+ intent.setDataAndType(contentUri, mimeType)
28
+ intent.flags =
29
+ Intent .FLAG_GRANT_READ_URI_PERMISSION or Intent .FLAG_GRANT_WRITE_URI_PERMISSION
81
30
82
- val cipher: Cipher = Cipher .getInstance(" AES" )
83
- cipher.init (Cipher .ENCRYPT_MODE , sks)
84
- val cos = CipherOutputStream (dst, cipher)
85
- val b = ByteArray (8 )
86
- var i: Int = src.read(b)
87
- while (i != - 1 ) {
88
- cos.write(b, 0 , i)
89
- i = src.read(b)
90
- }
91
- src.close()
92
- dst.close()
93
- cos.flush()
94
- cos.close()
95
- return 1
96
- } else {
97
- Log .e(" EXPORT_DB" , " No storage permission." )
98
- return 2
99
- }
100
- } catch (e: Exception ) {
101
- e.printStackTrace()
102
- Log .e(" EXPORT_DB" , " Error exporting database!" )
103
- return 3
104
- }
31
+ return intent
105
32
}
33
+
34
+ fun getCSVFileName (folder : Boolean ): String =
35
+ if (folder) " $FOLDER_CARD_DB_NAME .csv" else " $PASSWORD_CARD_DB_NAME .csv"
106
36
}
0 commit comments