@@ -22,8 +22,8 @@ import java.io.IOException
2222 * Created by Prasham on 4/11/2016.
2323 */
2424
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 ) {
2727
2828
2929 public val PARENT_URI_KEY = " APP_EXTERNAL_PARENT_FILE_URI"
@@ -88,22 +88,16 @@ import java.io.IOException
8888 }
8989 }
9090
91- public fun checkIfExternalDirAvailable (context : Context ): Boolean {
92- initCacheDirs()
93- preferences = PreferenceManager .getDefaultSharedPreferences(context)
94- val isExternaDirAvailable = isExternalDirAvailable()
95- return isExternaDirAvailable
96- }
9791
98- fun isExternalDirAvailable (): Boolean {
99- initCacheDirs()
92+ fun isExternalDirAvailable (context : Context = this.context ): Boolean {
93+ initCacheDirs(context )
10094 preferences = PreferenceManager .getDefaultSharedPreferences(context)
10195 val externalDirUrl = preferences.getString(PARENT_URI_KEY , " " )
10296 val isExternalDirEmpty = externalDirUrl.isEmptyString()
10397 if (! isExternalDirEmpty) {
10498 externalParentFile = DocumentFile .fromTreeUri(context, Uri .parse(externalDirUrl))
10599 try {
106- createAppDirectory()
100+ createAppDirectory(context )
107101 } catch (e: Exception ) {
108102 preferences.remove(PARENT_URI_KEY )
109103 return false
@@ -113,7 +107,7 @@ import java.io.IOException
113107 }
114108
115109
116- private fun initCacheDirs () {
110+ private fun initCacheDirs (context : Context = this.context ) {
117111 val dirs = ContextCompat .getExternalCacheDirs(context)
118112 externalCacheDirectory = if (dirs.size > 1 ) {
119113 val dir = dirs[1 ]
@@ -180,7 +174,7 @@ import java.io.IOException
180174 }
181175
182176 /* * Creates app directory */
183- private fun createAppDirectory () {
177+ private fun createAppDirectory (context : Context = this.context ) {
184178 val directoryName = context.getString(context.applicationInfo.labelRes)
185179 if (isDirectoryExists(directoryName, externalParentFile)) {
186180 appDirectory = externalParentFile.findFile(directoryName)
@@ -409,37 +403,39 @@ import java.io.IOException
409403
410404 @Throws(FileNotFoundException ::class )
411405 fun writeDataToTimeStampedFile (mimeType : String , data : String , extension : String ,
412- inCache : Boolean , onFileWritten : (DocumentFile ) -> Unit = {}) {
406+ filePrefix : String = "", inCache : Boolean ,
407+ onFileWritten : (DocumentFile ) -> Unit = {}) {
413408 val appDir = getAppDirectory(inCache)
414409 val fileExtension = if (TextUtils .isEmpty(extension)) " " else " ." + extension
415- val fileName = " ${System .currentTimeMillis()}$fileExtension "
410+ val fileName = " $filePrefix$ {System .currentTimeMillis()}$fileExtension "
416411 writeDataToFile(parent = appDir, fileName = fileName, data = data, mimeType = mimeType,
417412 onFileWritten = onFileWritten)
418413 }
419414
420415 @Throws(FileNotFoundException ::class )
421416 fun writeDataToTimeStampedFile (mimeType : String , data : ByteArray , extension : String ,
422- inCache : Boolean , onFileWritten : (DocumentFile ) -> Unit ) {
417+ filePrefix : String = "", inCache : Boolean ,
418+ onFileWritten : (DocumentFile ) -> Unit ) {
423419 val appDir = getAppDirectory(inCache)
424420 val fileExtension = if (TextUtils .isEmpty(extension)) " " else " ." + extension
425- val fileName = " ${System .currentTimeMillis()}$fileExtension "
421+ val fileName = " $filePrefix$ {System .currentTimeMillis()}$fileExtension "
426422 writeDataToFile(parent = appDir, fileName = fileName, data = data, mimeType = mimeType,
427423 onFileWritten = onFileWritten)
428424 }
429425
430426 @Throws(FileNotFoundException ::class )
431427 fun writeDataToTimeStampedFile (mimeType : String , data : String , extension : String ,
432- inCache : Boolean , parent : DocumentFile ,
428+ filePrefix : String = "", inCache : Boolean , parent : DocumentFile ,
433429 onFileWritten : (DocumentFile ) -> Unit = {}) {
434430 val fileExtension = if (TextUtils .isEmpty(extension)) " " else " ." + extension
435- val fileName = " ${System .currentTimeMillis()}$fileExtension "
431+ val fileName = " $filePrefix$ {System .currentTimeMillis()}$fileExtension "
436432 writeDataToFile(parent = parent, fileName = fileName, data = data, mimeType = mimeType,
437433 onFileWritten = onFileWritten)
438434 }
439435
440436 @Throws(FileNotFoundException ::class )
441437 fun writeDataToTimeStampedFile (mimeType : String , data : ByteArray , extension : String ,
442- inCache : Boolean , parent : DocumentFile ,
438+ filePrefix : String = "", inCache : Boolean , parent : DocumentFile ,
443439 onFileWritten : (DocumentFile ) -> Unit = {}) {
444440 val fileExtension = if (TextUtils .isEmpty(extension)) " " else " ." + extension
445441 val fileName = " ${System .currentTimeMillis()}$fileExtension "
0 commit comments