|
| 1 | +// |
| 2 | +// ContextCompat.swift |
| 3 | +// Android |
| 4 | +// |
| 5 | +// Created by Marco Estrella on 8/29/18. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import java_swift |
| 10 | +import JNI |
| 11 | + |
| 12 | +public extension Android.Content { |
| 13 | + |
| 14 | + public typealias ContextCompat = AndroidContextCompat |
| 15 | +} |
| 16 | + |
| 17 | +open class AndroidContextCompat: JavaObject { |
| 18 | + |
| 19 | + public required init(javaObject: jobject?) { |
| 20 | + super.init(javaObject: javaObject) |
| 21 | + } |
| 22 | + |
| 23 | + public convenience init?( casting object: JavaObject, _ file: StaticString = #file, _ line: Int = #line ) { |
| 24 | + self.init( javaObject: nil ) |
| 25 | + object.withJavaObject { |
| 26 | + self.javaObject = $0 |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +public extension AndroidContextCompat { |
| 32 | + |
| 33 | + public static func getColor(context: AndroidContext, colorRes: Int) -> Int { |
| 34 | + |
| 35 | + var __locals = [jobject]() |
| 36 | + |
| 37 | + var __args = [jvalue]( repeating: jvalue(), count: 2 ) |
| 38 | + |
| 39 | + __args[0] = JNIType.toJava(value: context, locals: &__locals) |
| 40 | + __args[1] = jvalue(i: jint(colorRes)) |
| 41 | + |
| 42 | + let __return = JNIMethod.CallStaticIntMethod(className: JNICache.className, |
| 43 | + classCache: &JNICache.jniClass, |
| 44 | + methodName: "getColor", |
| 45 | + methodSig: "(Landroid/content/Context;I)I", |
| 46 | + methodCache: &JNICache.MethodID.getColor, |
| 47 | + args: &__args, |
| 48 | + locals: &__locals ) |
| 49 | + return Int(__return) |
| 50 | + } |
| 51 | + |
| 52 | + public static func getDrawable(context: AndroidContext, drawableRes: Int) -> AndroidGraphicsDrawableDrawable? { |
| 53 | + |
| 54 | + var __locals = [jobject]() |
| 55 | + |
| 56 | + var __args = [jvalue]( repeating: jvalue(), count: 2 ) |
| 57 | + |
| 58 | + __args[0] = JNIType.toJava(value: context, locals: &__locals) |
| 59 | + __args[1] = jvalue(i: jint(drawableRes)) |
| 60 | + |
| 61 | + let __return = JNIMethod.CallStaticObjectMethod(className: JNICache.className, |
| 62 | + classCache: &JNICache.jniClass, |
| 63 | + methodName: "getDrawable", |
| 64 | + methodSig: "(Landroid/content/Context;I)Landroid/graphics/drawable/Drawable;", |
| 65 | + methodCache: &JNICache.MethodID.getDrawable, |
| 66 | + args: &__args, |
| 67 | + locals: &__locals ) |
| 68 | + |
| 69 | + return __return != nil ? AndroidGraphicsDrawableDrawable(javaObject: __return) : nil |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +internal extension AndroidContextCompat { |
| 74 | + |
| 75 | + /// JNI Cache |
| 76 | + struct JNICache { |
| 77 | + |
| 78 | + static let classSignature = SupportV4.View.className(["ContextCompat"]) |
| 79 | + |
| 80 | + /// JNI Java class name |
| 81 | + static let className = classSignature.rawValue |
| 82 | + |
| 83 | + /// JNI Java class |
| 84 | + static var jniClass: jclass? |
| 85 | + |
| 86 | + /// JNI Method ID cache |
| 87 | + struct MethodID { |
| 88 | + |
| 89 | + static var getColor: jmethodID? |
| 90 | + static var getDrawable: jmethodID? |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
0 commit comments