Skip to content

Commit e71fb45

Browse files
Kotlin v 0.4
1 parent dc37211 commit e71fb45

File tree

6 files changed

+56
-83
lines changed

6 files changed

+56
-83
lines changed

androidexternalfilewriter-kotlin/build.gradle

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88

99
defaultConfig {
1010
minSdkVersion 17
11-
targetSdkVersion 25
11+
targetSdkVersion 26
1212
versionCode 1
1313
versionName "1.0"
1414
}
@@ -28,24 +28,5 @@ dependencies {
2828
testCompile 'junit:junit:4.12'
2929
compile "com.android.support:appcompat-v7:$rootProject.ext.supportLibVersion"
3030
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
31-
compile 'com.creativeelites:kutils:0.6@aar'
32-
}
33-
buildscript {
34-
ext.kotlin_version = '1.1.2-4'
35-
repositories {
36-
mavenCentral()
37-
}
38-
dependencies {
39-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
40-
}
41-
}
42-
repositories {
43-
mavenCentral()
44-
maven { url "https://jitpack.io" }
45-
maven {
46-
url "https://github.com/CreativeElites/MvnRepo/raw/master"
47-
}
48-
maven {
49-
url "https://dl.bintray.com/prashamtrivedi/maven"
50-
}
31+
compile 'com.creativeelites:kutils:0.8@aar'
5132
}

androidexternalfilewriter-kotlin/gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
POM_NAME=androidexternalfilewriter-kotlin
22
POM_ARTIFACT_ID=androidexternalfilewriter-kotlin
33
POM_PACKAGING=aar
4-
VERSION_NAME=0.3
5-
VERSION_CODE=3
4+
VERSION_NAME=0.4
5+
VERSION_CODE=4
66
GROUP=com.creativeelites
77
POM_DESCRIPTION=Android External FileWriter
88
POM_URL=https://github.com/PrashamTrivedi/AndroidExternalFileWriter

androidexternalfilewriter-kotlin/src/main/java/com/celites/androidexternalfilewriter_kotlin/KotlinExternalFileWriter.kt

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class KotlinExternalFileWriter {
2020
* Created by Prasham on 4/6/2016.
2121
*/
2222
public fun Context.createAppDirectory() {
23-
val directoryName = this.getString(this.getApplicationInfo().labelRes)
23+
val directoryName = this.getString(this.applicationInfo.labelRes)
2424

2525
if (isExternalStorageAvailable(false)) {
2626

@@ -47,21 +47,21 @@ public class KotlinExternalFileWriter {
4747
val detailFile = File(parent, fileName)
4848
if (!detailFile.exists()) detailFile.createNewFile()
4949
else {
50-
val messege = "File already there "
51-
throwException(messege)
50+
val message = "File already there "
51+
throwException(message)
5252
}
5353
return detailFile
5454
} else {
5555
throwException("$parent should be a directory")
5656
}
5757
} catch (e: IOException) {
5858
e.printStackTrace()
59-
val errorMessege = "IOException " + e
60-
throwException(errorMessege)
59+
val errorMessage = "IOException $e"
60+
throwException(errorMessage)
6161
} catch (e: Exception) {
6262
e.printStackTrace()
63-
val errorMessege = "Exception " + e
64-
throwException(errorMessege)
63+
val errorMessage = "Exception $e"
64+
throwException(errorMessage)
6565
}
6666

6767
}
@@ -86,16 +86,16 @@ public class KotlinExternalFileWriter {
8686
*/
8787
@Throws(ExternalFileWriterException::class)
8888
fun createSubDirectory(parent: File = appDirectory, directoryName: String): File? {
89-
if (isExternalStorageAvailable(false)) {
89+
return if (isExternalStorageAvailable(false)) {
9090

9191
getAppDirectory()
9292

9393
if (!parent.isDirectory) throwException("$parent.name Must be a directory ")
9494

9595
val subDirectory = File(parent, directoryName)
9696

97-
return createDirectory(subDirectory)
98-
} else return null
97+
createDirectory(subDirectory)
98+
} else null
9999
}
100100

101101
/**
@@ -450,25 +450,17 @@ public class KotlinExternalFileWriter {
450450

451451
val storageState = Environment.getExternalStorageState()
452452

453-
if (storageState == Environment.MEDIA_MOUNTED) {
454-
return true
455-
} else if (storageState == Environment.MEDIA_BAD_REMOVAL) {
456-
throwException("${errorStarter}Media was removed before it was unmounted.")
457-
} else if (storageState == Environment.MEDIA_CHECKING) {
458-
throwException(
453+
when (storageState) {
454+
Environment.MEDIA_MOUNTED -> return true
455+
Environment.MEDIA_BAD_REMOVAL -> throwException("${errorStarter}Media was removed before it was unmounted.")
456+
Environment.MEDIA_CHECKING -> throwException(
459457
errorStarter + "Media is present and being disk-checked, " + "Please wait and try after some time")
460-
} else if (storageState == Environment.MEDIA_MOUNTED_READ_ONLY) {
461-
throwException(errorStarter + "Presented Media is read only")
462-
} else if (storageState == Environment.MEDIA_NOFS) {
463-
throwException(errorStarter + "Blank or unsupported file media")
464-
} else if (storageState == Environment.MEDIA_SHARED) {
465-
throwException(errorStarter + "Media is shared with USB mass storage")
466-
} else if (storageState == Environment.MEDIA_REMOVED) {
467-
throwException(errorStarter + "Media is not present")
468-
} else if (storageState == Environment.MEDIA_UNMOUNTABLE) {
469-
throwException(errorStarter + "Media is present but cannot be mounted")
470-
} else if (storageState == Environment.MEDIA_UNMOUNTED) {
471-
throwException(errorStarter + "Media is present but not mounted")
458+
Environment.MEDIA_MOUNTED_READ_ONLY -> throwException(errorStarter + "Presented Media is read only")
459+
Environment.MEDIA_NOFS -> throwException(errorStarter + "Blank or unsupported file media")
460+
Environment.MEDIA_SHARED -> throwException(errorStarter + "Media is shared with USB mass storage")
461+
Environment.MEDIA_REMOVED -> throwException(errorStarter + "Media is not present")
462+
Environment.MEDIA_UNMOUNTABLE -> throwException(errorStarter + "Media is present but cannot be mounted")
463+
Environment.MEDIA_UNMOUNTED -> throwException(errorStarter + "Media is present but not mounted")
472464
}
473465

474466
return false
@@ -483,5 +475,5 @@ public class KotlinExternalFileWriter {
483475
* writing is not
484476
* possible
485477
*/
486-
class ExternalFileWriterException(messege: String) : Exception(messege)
478+
class ExternalFileWriterException(message: String) : Exception(message)
487479
}

androidexternalfilewriter-kotlin/src/main/java/com/celites/androidexternalfilewriter_kotlin/KotlinStorageAccessFileWriter.kt

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class KotlinStorageAccessFileWriter(private val requestCode: Int) {
4949

5050
public fun startWithContext(context: Context) {
5151
this.context = context
52-
val isExternaDirAvailable = isExternalDirAvailable()
53-
if (isExternaDirAvailable) {
52+
val isExternalDirAvailable = isExternalDirAvailable()
53+
if (isExternalDirAvailable) {
5454
createAppDirectory()
5555
}
5656
}
@@ -69,8 +69,8 @@ class KotlinStorageAccessFileWriter(private val requestCode: Int) {
6969
activity: Activity) {
7070
initCacheDirs()
7171
preferences = PreferenceManager.getDefaultSharedPreferences(context)
72-
val isExternaDirAvailable = isExternalDirAvailable()
73-
if (!isExternaDirAvailable) {
72+
val isExternalDirAvailable = isExternalDirAvailable()
73+
if (!isExternalDirAvailable) {
7474

7575
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
7676
activity.startActivityForResult(intent, requestCode)
@@ -81,8 +81,8 @@ class KotlinStorageAccessFileWriter(private val requestCode: Int) {
8181
fragment: Fragment) {
8282
initCacheDirs()
8383
preferences = PreferenceManager.getDefaultSharedPreferences(context)
84-
val isExternaDirAvailable = isExternalDirAvailable()
85-
if (!isExternaDirAvailable) {
84+
val isExternalDirAvailable = isExternalDirAvailable()
85+
if (!isExternalDirAvailable) {
8686
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
8787
fragment.startActivityForResult(intent, requestCode)
8888
}
@@ -168,23 +168,23 @@ class KotlinStorageAccessFileWriter(private val requestCode: Int) {
168168

169169
@RequiresApi(Build.VERSION_CODES.KITKAT)
170170
fun hasPermissions(file: DocumentFile): Boolean {
171-
val persistedUriPermissions = context.getContentResolver().persistedUriPermissions
171+
val persistedUriPermissions = context.contentResolver.persistedUriPermissions
172172
val filterForPermission = persistedUriPermissions.filter { it.uri == file.uri && it.isReadPermission && it.isWritePermission }
173173
return filterForPermission.isNotEmpty()
174174
}
175175

176176
/** Creates app directory */
177177
private fun createAppDirectory(context: Context = this.context) {
178178
val directoryName = context.getString(context.applicationInfo.labelRes)
179-
if (isDirectoryExists(directoryName, externalParentFile)) {
180-
appDirectory = externalParentFile.findFile(directoryName)
179+
appDirectory = if (isDirectoryExists(directoryName, externalParentFile)) {
180+
externalParentFile.findFile(directoryName)
181181
} else {
182-
appDirectory = externalParentFile.createDirectory(directoryName)
182+
externalParentFile.createDirectory(directoryName)
183183
}
184-
if (isDirectoryExists(directoryName, externalCacheDirectory)) {
185-
appCacheDirectory = externalCacheDirectory.findFile(directoryName)
184+
appCacheDirectory = if (isDirectoryExists(directoryName, externalCacheDirectory)) {
185+
externalCacheDirectory.findFile(directoryName)
186186
} else {
187-
appCacheDirectory = externalCacheDirectory.createDirectory(directoryName)
187+
externalCacheDirectory.createDirectory(directoryName)
188188
}
189189

190190
}
@@ -249,8 +249,7 @@ class KotlinStorageAccessFileWriter(private val requestCode: Int) {
249249
context.contentResolver.takePersistableUriPermission(treeUri, takeFlags)
250250
}
251251
externalParentFile = DocumentFile.fromTreeUri(context, treeUri)
252-
preferences.edit().putString(PARENT_URI_KEY,
253-
externalParentFile.getUri().toString()).apply()
252+
preferences.edit().putString(PARENT_URI_KEY, externalParentFile.uri.toString()).apply()
254253
createAppDirectory()
255254
handlingFinished()
256255
}
@@ -280,7 +279,6 @@ class KotlinStorageAccessFileWriter(private val requestCode: Int) {
280279
@Throws(FileNotFoundException::class)
281280
fun writeDataToFile(fileName: String, mimeType: String, data: ByteArray,
282281
inCache: Boolean = false) {
283-
getAppDirectory()
284282
val appDir = getAppDirectory(inCache)
285283
writeDataToFile(appDir, fileName, data, mimeType)
286284
}
@@ -424,21 +422,19 @@ class KotlinStorageAccessFileWriter(private val requestCode: Int) {
424422
}
425423

426424
@Throws(FileNotFoundException::class)
427-
fun writeDataToTimeStampedFile(mimeType: String, data: String, extension: String,
428-
filePrefix: String = "", inCache: Boolean, parent: DocumentFile,
429-
onFileWritten: (DocumentFile) -> Unit = {}) {
425+
fun writeDataToTimeStampedFile(mimeType: String, data: String, extension: String, filePrefix: String = "", parent: DocumentFile,
426+
onFileWritten: (DocumentFile) -> Unit = {}) {
430427
val fileExtension = if (TextUtils.isEmpty(extension)) "" else "." + extension
431428
val fileName = "$filePrefix${System.currentTimeMillis()}$fileExtension"
432429
writeDataToFile(parent = parent, fileName = fileName, data = data, mimeType = mimeType,
433430
onFileWritten = onFileWritten)
434431
}
435432

436433
@Throws(FileNotFoundException::class)
437-
fun writeDataToTimeStampedFile(mimeType: String, data: ByteArray, extension: String,
438-
filePrefix: String = "", inCache: Boolean, parent: DocumentFile,
439-
onFileWritten: (DocumentFile) -> Unit = {}) {
434+
fun writeDataToTimeStampedFile(mimeType: String, data: ByteArray, extension: String, filePrefix: String = "", parent: DocumentFile,
435+
onFileWritten: (DocumentFile) -> Unit = {}) {
440436
val fileExtension = if (TextUtils.isEmpty(extension)) "" else "." + extension
441-
val fileName = "${System.currentTimeMillis()}$fileExtension"
437+
val fileName = "$filePrefix${System.currentTimeMillis()}$fileExtension"
442438
writeDataToFile(parent = parent, fileName = fileName, data = data, mimeType = mimeType,
443439
onFileWritten = onFileWritten)
444440
}

androidexternalfilewriter/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
buildToolsVersion rootProject.ext.buildToolsVersion
77

88
defaultConfig {
9-
minSdkVersion 10
10-
targetSdkVersion 25
9+
minSdkVersion 14
10+
targetSdkVersion 26
1111
versionCode 1
1212
versionName "1.0"
1313
}

build.gradle

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4+
ext.kotlin_version = '1.1.4'
45
repositories {
56
jcenter()
6-
maven {
7-
url 'https://maven.google.com'
8-
}
7+
google()
98
}
109
dependencies {
1110
classpath 'com.android.tools.build:gradle:3.0.0-beta2'
1211
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
1312
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
13+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1414
// NOTE: Do not place your application dependencies here; they belong
1515
// in the individual module build.gradle files
1616
}
1717
}
1818

1919

2020
ext {
21-
buildToolsVersion = "25.0.3"
22-
compileSdkVersion = 25
23-
supportLibVersion = "25.3.1"
21+
buildToolsVersion = "26.0.0"
22+
compileSdkVersion = 26
23+
supportLibVersion = "26.0.1"
2424
}
2525

2626
allprojects {
@@ -31,6 +31,10 @@ allprojects {
3131
maven {
3232
url "https://dl.bintray.com/prashamtrivedi/maven"
3333
}
34+
google()
3435
jcenter()
36+
tasks.withType(Javadoc).all {
37+
enabled = false
38+
}
3539
}
3640
}

0 commit comments

Comments
 (0)