forked from topjohnwu/Magisk
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix topjohnwu#8452
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 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
32 changes: 32 additions & 0 deletions
32
app/core/src/main/java/com/topjohnwu/magisk/core/utils/Desugar.java
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,32 @@ | ||
package com.topjohnwu.magisk.core.utils; | ||
|
||
import android.os.Build; | ||
|
||
import java.nio.file.attribute.FileTime; | ||
import java.util.zip.ZipEntry; | ||
|
||
public class Desugar { | ||
public static FileTime getLastModifiedTime(ZipEntry entry) { | ||
if (Build.VERSION.SDK_INT >= 26) { | ||
return entry.getLastModifiedTime(); | ||
} else { | ||
return FileTime.fromMillis(entry.getTime()); | ||
} | ||
} | ||
|
||
public static FileTime getLastAccessTime(ZipEntry entry) { | ||
if (Build.VERSION.SDK_INT >= 26) { | ||
return entry.getLastAccessTime(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
public static FileTime getCreationTime(ZipEntry entry) { | ||
if (Build.VERSION.SDK_INT >= 26) { | ||
return entry.getCreationTime(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
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,67 @@ | ||
import com.android.build.api.instrumentation.AsmClassVisitorFactory | ||
import com.android.build.api.instrumentation.ClassContext | ||
import com.android.build.api.instrumentation.ClassData | ||
import com.android.build.api.instrumentation.InstrumentationParameters | ||
import org.objectweb.asm.ClassVisitor | ||
import org.objectweb.asm.MethodVisitor | ||
import org.objectweb.asm.Opcodes | ||
import org.objectweb.asm.Opcodes.ASM9 | ||
|
||
private const val DESUGAR_CLASS_NAME = "com/topjohnwu/magisk/core/utils/Desugar" | ||
private const val ZIP_ENTRY_GET_TIME_DESC = "()Ljava/nio/file/attribute/FileTime;" | ||
private const val DESUGAR_GET_TIME_DESC = "(Ljava/util/zip/ZipEntry;)Ljava/nio/file/attribute/FileTime;" | ||
|
||
abstract class DesugarClassVisitorFactory : AsmClassVisitorFactory<InstrumentationParameters.None> { | ||
override fun createClassVisitor( | ||
classContext: ClassContext, | ||
nextClassVisitor: ClassVisitor | ||
): ClassVisitor { | ||
return DesugarClassVisitor(nextClassVisitor) | ||
} | ||
|
||
override fun isInstrumentable(classData: ClassData) = true | ||
|
||
class DesugarClassVisitor(cv: ClassVisitor) : ClassVisitor(ASM9, cv) { | ||
override fun visitMethod( | ||
access: Int, | ||
name: String?, | ||
descriptor: String?, | ||
signature: String?, | ||
exceptions: Array<out String>? | ||
): MethodVisitor { | ||
return DesugarMethodVisitor(super.visitMethod(access, name, descriptor, signature, exceptions)) | ||
} | ||
} | ||
|
||
class DesugarMethodVisitor(mv: MethodVisitor?) : MethodVisitor(ASM9, mv) { | ||
override fun visitMethodInsn( | ||
opcode: Int, | ||
owner: String, | ||
name: String, | ||
descriptor: String, | ||
isInterface: Boolean | ||
) { | ||
if (!process(name, descriptor)) { | ||
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface) | ||
} | ||
} | ||
|
||
private fun process(name: String, descriptor: String): Boolean { | ||
if (descriptor != ZIP_ENTRY_GET_TIME_DESC) | ||
return false | ||
when (name) { | ||
"getLastModifiedTime", "getLastAccessTime", "getCreationTime" -> { | ||
mv.visitMethodInsn( | ||
Opcodes.INVOKESTATIC, | ||
DESUGAR_CLASS_NAME, | ||
name, | ||
DESUGAR_GET_TIME_DESC, | ||
false | ||
) | ||
} | ||
else -> return false | ||
} | ||
return true | ||
} | ||
} | ||
} |