forked from ReVanced/revanced-patches-template
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support patching on Android 5-7 by adding Files API compatibility
- Loading branch information
Showing
5 changed files
with
79 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package app.revanced.util | ||
|
||
import app.revanced.util.Utils.printInfo | ||
import java.io.File | ||
import java.io.InputStream | ||
import java.nio.file.Files | ||
import java.nio.file.StandardCopyOption | ||
|
||
/** | ||
* Provides java.nio.file.Files compatible functions. | ||
* | ||
* This is needed for ReVanced Manager running on Android 5.0-7.1 | ||
* because before Android 8.0 does not have java.nio.file API. | ||
*/ | ||
internal object FilesCompat { | ||
private val useCompat = try { | ||
// Check existence of java.nio.file.Files class | ||
Class.forName("java.nio.file.Files") | ||
false | ||
} catch (_ : ClassNotFoundException) { | ||
// Under Android 8.0 | ||
true | ||
} | ||
|
||
/** | ||
* Copy a file to a target file. | ||
* | ||
* If the `target` file already exists, replace an existing file. | ||
*/ | ||
fun copy(source: File, target: File) { | ||
if (useCompat) { | ||
source.copyTo(target, overwrite = true) | ||
} else { | ||
Files.copy(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING) | ||
} | ||
} | ||
|
||
/** | ||
* Copies all bytes from an input stream to a file. | ||
* | ||
* If the `target` file already exists, replace an existing file. | ||
*/ | ||
fun copy(source: InputStream, target: File) { | ||
if (useCompat) { | ||
source.use { inputStream -> | ||
target.outputStream().use { outputStream -> | ||
inputStream.copyTo(outputStream) | ||
} | ||
} | ||
} else { | ||
Files.copy(source, target.toPath(), StandardCopyOption.REPLACE_EXISTING) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters