@@ -22,8 +22,8 @@ import java.io.IOException
22
22
* Created by Prasham on 4/11/2016.
23
23
*/
24
24
25
- @RequiresApi(Build .VERSION_CODES .LOLLIPOP ) public class KotlinStorageAccessFileWriter (
26
- val requestCode : Int ) {
25
+ @RequiresApi(Build .VERSION_CODES .LOLLIPOP )
26
+ class KotlinStorageAccessFileWriter ( private val requestCode : Int ) {
27
27
28
28
29
29
public val PARENT_URI_KEY = " APP_EXTERNAL_PARENT_FILE_URI"
@@ -88,22 +88,16 @@ import java.io.IOException
88
88
}
89
89
}
90
90
91
- public fun checkIfExternalDirAvailable (context : Context ): Boolean {
92
- initCacheDirs()
93
- preferences = PreferenceManager .getDefaultSharedPreferences(context)
94
- val isExternaDirAvailable = isExternalDirAvailable()
95
- return isExternaDirAvailable
96
- }
97
91
98
- fun isExternalDirAvailable (): Boolean {
99
- initCacheDirs()
92
+ fun isExternalDirAvailable (context : Context = this.context ): Boolean {
93
+ initCacheDirs(context )
100
94
preferences = PreferenceManager .getDefaultSharedPreferences(context)
101
95
val externalDirUrl = preferences.getString(PARENT_URI_KEY , " " )
102
96
val isExternalDirEmpty = externalDirUrl.isEmptyString()
103
97
if (! isExternalDirEmpty) {
104
98
externalParentFile = DocumentFile .fromTreeUri(context, Uri .parse(externalDirUrl))
105
99
try {
106
- createAppDirectory()
100
+ createAppDirectory(context )
107
101
} catch (e: Exception ) {
108
102
preferences.remove(PARENT_URI_KEY )
109
103
return false
@@ -113,7 +107,7 @@ import java.io.IOException
113
107
}
114
108
115
109
116
- private fun initCacheDirs () {
110
+ private fun initCacheDirs (context : Context = this.context ) {
117
111
val dirs = ContextCompat .getExternalCacheDirs(context)
118
112
externalCacheDirectory = if (dirs.size > 1 ) {
119
113
val dir = dirs[1 ]
@@ -180,7 +174,7 @@ import java.io.IOException
180
174
}
181
175
182
176
/* * Creates app directory */
183
- private fun createAppDirectory () {
177
+ private fun createAppDirectory (context : Context = this.context ) {
184
178
val directoryName = context.getString(context.applicationInfo.labelRes)
185
179
if (isDirectoryExists(directoryName, externalParentFile)) {
186
180
appDirectory = externalParentFile.findFile(directoryName)
@@ -409,37 +403,39 @@ import java.io.IOException
409
403
410
404
@Throws(FileNotFoundException ::class )
411
405
fun writeDataToTimeStampedFile (mimeType : String , data : String , extension : String ,
412
- inCache : Boolean , onFileWritten : (DocumentFile ) -> Unit = {}) {
406
+ filePrefix : String = "", inCache : Boolean ,
407
+ onFileWritten : (DocumentFile ) -> Unit = {}) {
413
408
val appDir = getAppDirectory(inCache)
414
409
val fileExtension = if (TextUtils .isEmpty(extension)) " " else " ." + extension
415
- val fileName = " ${System .currentTimeMillis()}$fileExtension "
410
+ val fileName = " $filePrefix$ {System .currentTimeMillis()}$fileExtension "
416
411
writeDataToFile(parent = appDir, fileName = fileName, data = data, mimeType = mimeType,
417
412
onFileWritten = onFileWritten)
418
413
}
419
414
420
415
@Throws(FileNotFoundException ::class )
421
416
fun writeDataToTimeStampedFile (mimeType : String , data : ByteArray , extension : String ,
422
- inCache : Boolean , onFileWritten : (DocumentFile ) -> Unit ) {
417
+ filePrefix : String = "", inCache : Boolean ,
418
+ onFileWritten : (DocumentFile ) -> Unit ) {
423
419
val appDir = getAppDirectory(inCache)
424
420
val fileExtension = if (TextUtils .isEmpty(extension)) " " else " ." + extension
425
- val fileName = " ${System .currentTimeMillis()}$fileExtension "
421
+ val fileName = " $filePrefix$ {System .currentTimeMillis()}$fileExtension "
426
422
writeDataToFile(parent = appDir, fileName = fileName, data = data, mimeType = mimeType,
427
423
onFileWritten = onFileWritten)
428
424
}
429
425
430
426
@Throws(FileNotFoundException ::class )
431
427
fun writeDataToTimeStampedFile (mimeType : String , data : String , extension : String ,
432
- inCache : Boolean , parent : DocumentFile ,
428
+ filePrefix : String = "", inCache : Boolean , parent : DocumentFile ,
433
429
onFileWritten : (DocumentFile ) -> Unit = {}) {
434
430
val fileExtension = if (TextUtils .isEmpty(extension)) " " else " ." + extension
435
- val fileName = " ${System .currentTimeMillis()}$fileExtension "
431
+ val fileName = " $filePrefix$ {System .currentTimeMillis()}$fileExtension "
436
432
writeDataToFile(parent = parent, fileName = fileName, data = data, mimeType = mimeType,
437
433
onFileWritten = onFileWritten)
438
434
}
439
435
440
436
@Throws(FileNotFoundException ::class )
441
437
fun writeDataToTimeStampedFile (mimeType : String , data : ByteArray , extension : String ,
442
- inCache : Boolean , parent : DocumentFile ,
438
+ filePrefix : String = "", inCache : Boolean , parent : DocumentFile ,
443
439
onFileWritten : (DocumentFile ) -> Unit = {}) {
444
440
val fileExtension = if (TextUtils .isEmpty(extension)) " " else " ." + extension
445
441
val fileName = " ${System .currentTimeMillis()}$fileExtension "
0 commit comments