Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6cfd0d74 authored by tibbi's avatar tibbi
Browse files

move some Activity extensions to Context extensions

parent 460c33f9
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ buildscript {
        propMinSdkVersion = 16
        propTargetSdkVersion = propCompileSdkVersion
        propVersionCode = 1
        propVersionName = '3.11.37'
        propVersionName = '3.11.38'
        kotlin_version = '1.2.21'
        support_libs = '27.0.2'
    }
+0 −73
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@ import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Looper
import android.os.TransactionTooLargeException
@@ -92,16 +91,6 @@ fun Activity.appLaunched() {
    }
}

fun Activity.updateSDCardPath() {
    Thread {
        val oldPath = baseConfig.sdCardPath
        baseConfig.sdCardPath = getSDCardPath().trimEnd('/')
        if (oldPath != baseConfig.sdCardPath) {
            baseConfig.treeUri = ""
        }
    }.start()
}

@SuppressLint("InlinedApi")
fun Activity.isShowingSAFDialog(path: String, treeUri: String, requestCode: Int): Boolean {
    return if (needsStupidWritePermissions(path) && (treeUri.isEmpty() || !hasProperStoredTreeUri())) {
@@ -259,14 +248,6 @@ fun Activity.openFile(uri: Uri, forceChooser: Boolean, applicationId: String) {
    }.start()
}

fun Activity.getUriMimeType(oldUri: Uri, newUri: Uri): String {
    var mimeType = getMimeTypeFromUri(oldUri)
    if (mimeType.isEmpty()) {
        mimeType = getMimeTypeFromUri(newUri)
    }
    return mimeType
}

fun Activity.tryGenericMimeType(intent: Intent, mimeType: String, uri: Uri): Boolean {
    var genericMimeType = mimeType.getGenericMimeType()
    if (genericMimeType.isEmpty()) {
@@ -432,37 +413,6 @@ fun BaseSimpleActivity.deleteFileBg(fileDirItem: FileDirItem, allowDeleteFolder:
    }
}

@SuppressLint("NewApi")
fun BaseSimpleActivity.trySAFFileDelete(fileDirItem: FileDirItem, allowDeleteFolder: Boolean = false, callback: ((wasSuccess: Boolean) -> Unit)? = null) {
    var fileDeleted = tryFastDocumentDelete(fileDirItem, allowDeleteFolder)
    if (!fileDeleted) {
        val document = getFileDocument(fileDirItem.path)
        if (document != null && (fileDirItem.isDirectory == document.isDirectory)) {
            fileDeleted = (document.isFile == true || allowDeleteFolder) && DocumentsContract.deleteDocument(applicationContext.contentResolver, document.uri)
        }

        if (fileDeleted) {
            rescanDeletedPath(fileDirItem.path) {
                callback?.invoke(true)
            }
        }
    }
}

fun BaseSimpleActivity.rescanDeletedPath(path: String, callback: (() -> Unit)? = null) {
    if (deleteFromMediaStore(path)) {
        callback?.invoke()
    } else {
        MediaScannerConnection.scanFile(applicationContext, arrayOf(path), null, { s, uri ->
            try {
                applicationContext.contentResolver.delete(uri, null, null)
            } catch (e: Exception) {
            }
            callback?.invoke()
        })
    }
}

private fun deleteRecursively(file: File): Boolean {
    if (file.isDirectory) {
        val files = file.listFiles() ?: return file.delete()
@@ -610,29 +560,6 @@ fun BaseSimpleActivity.getFileOutputStreamSync(targetPath: String, mimeType: Str
    }
}

@SuppressLint("NewApi")
fun BaseSimpleActivity.getFileDocument(path: String): DocumentFile? {
    if (!isLollipopPlus()) {
        return null
    }

    val isOTG = path.startsWith(OTG_PATH)
    var relativePath = path.substring(if (isOTG) OTG_PATH.length else sdCardPath.length)
    if (relativePath.startsWith(File.separator)) {
        relativePath = relativePath.substring(1)
    }

    var document = DocumentFile.fromTreeUri(this, Uri.parse(if (isOTG) baseConfig.OTGTreeUri else baseConfig.treeUri))
    val parts = relativePath.split("/")
    for (part in parts) {
        val currDocument = document.findFile(part)
        if (currDocument != null)
            document = currDocument
    }

    return document
}

fun Activity.handleHiddenFolderPasswordProtection(callback: () -> Unit) {
    if (baseConfig.isPasswordProtectionOn) {
        SecurityDialog(this, baseConfig.passwordHash, baseConfig.protectionType) { hash, type, success ->
+54 −0
Original line number Diff line number Diff line
@@ -166,6 +166,29 @@ fun Context.getFastDocument(fileDirItem: FileDirItem): DocumentFile? {
    return DocumentFile.fromSingleUri(this, Uri.parse(fullUri))
}

@SuppressLint("NewApi")
fun Context.getFileDocument(path: String): DocumentFile? {
    if (!isLollipopPlus()) {
        return null
    }

    val isOTG = path.startsWith(OTG_PATH)
    var relativePath = path.substring(if (isOTG) OTG_PATH.length else sdCardPath.length)
    if (relativePath.startsWith(File.separator)) {
        relativePath = relativePath.substring(1)
    }

    var document = DocumentFile.fromTreeUri(applicationContext, Uri.parse(if (isOTG) baseConfig.OTGTreeUri else baseConfig.treeUri))
    val parts = relativePath.split("/")
    for (part in parts) {
        val currDocument = document.findFile(part)
        if (currDocument != null)
            document = currDocument
    }

    return document
}

fun Context.scanFile(file: File, callback: (() -> Unit)? = null) {
    scanFiles(arrayListOf(file), callback)
}
@@ -302,6 +325,37 @@ fun Context.getOTGItems(path: String, callback: (ArrayList<FileDirItem>) -> Unit
    callback(items)
}

fun Context.rescanDeletedPath(path: String, callback: (() -> Unit)? = null) {
    if (deleteFromMediaStore(path)) {
        callback?.invoke()
    } else {
        MediaScannerConnection.scanFile(applicationContext, arrayOf(path), null, { s, uri ->
            try {
                applicationContext.contentResolver.delete(uri, null, null)
            } catch (e: Exception) {
            }
            callback?.invoke()
        })
    }
}

@SuppressLint("NewApi")
fun Context.trySAFFileDelete(fileDirItem: FileDirItem, allowDeleteFolder: Boolean = false, callback: ((wasSuccess: Boolean) -> Unit)? = null) {
    var fileDeleted = tryFastDocumentDelete(fileDirItem, allowDeleteFolder)
    if (!fileDeleted) {
        val document = getFileDocument(fileDirItem.path)
        if (document != null && (fileDirItem.isDirectory == document.isDirectory)) {
            fileDeleted = (document.isFile == true || allowDeleteFolder) && DocumentsContract.deleteDocument(applicationContext.contentResolver, document.uri)
        }

        if (fileDeleted) {
            rescanDeletedPath(fileDirItem.path) {
                callback?.invoke(true)
            }
        }
    }
}

// avoid these being set as SD card paths
private val physicalPaths = arrayListOf(
        "/storage/sdcard1", // Motorola Xoom
+18 −0
Original line number Diff line number Diff line
@@ -318,3 +318,21 @@ fun Context.getCurrentFormattedDateTime(): String {
    val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd_HH-mm", Locale.getDefault())
    return simpleDateFormat.format(Date(System.currentTimeMillis()))
}

fun Context.updateSDCardPath() {
    Thread {
        val oldPath = baseConfig.sdCardPath
        baseConfig.sdCardPath = getSDCardPath().trimEnd('/')
        if (oldPath != baseConfig.sdCardPath) {
            baseConfig.treeUri = ""
        }
    }.start()
}

fun Context.getUriMimeType(oldUri: Uri, newUri: Uri): String {
    var mimeType = getMimeTypeFromUri(oldUri)
    if (mimeType.isEmpty()) {
        mimeType = getMimeTypeFromUri(newUri)
    }
    return mimeType
}