7
7
8
8
import Foundation
9
9
import java_swift
10
+ import JNI
10
11
11
12
public extension Android . Widget {
12
13
13
14
public typealias Adapter = AndroidWidgetAdapter
14
15
}
15
16
16
- open class AndroidWidgetAdapter : JavaProtocol {
17
+ public protocol AndroidWidgetAdapter : JavaProtocol {
17
18
18
- // MARK: - Properties
19
-
20
- fileprivate lazy var listener : AndroidWidgetAdapterListenerLocal = {
21
-
22
- let proxy = AndroidWidgetAdapterListenerProxy ( )
23
- proxy. swiftObject = self
24
-
25
- return AndroidWidgetAdapterListenerLocal ( owned: proxy, proto: proxy)
26
- } ( )
27
-
28
- // MARK: - Initialization
29
-
30
- public func localJavaObject( _ locals: UnsafeMutablePointer < [ jobject ] > ) -> jobject ? {
31
-
32
- return listener. localJavaObject ( locals )
33
- }
34
-
35
- public final func notifyDataSetChanged( ) {
36
-
37
- listener. notifyDataSetChanged ( )
38
- }
39
-
40
- // MARK: - Listener
41
-
42
- open func getCount( ) -> Int { return 0 }
19
+ func getCount( ) -> Int
43
20
44
- open func getView( position: Int , convertView: Android . View . View ? , parent: Android . View . ViewGroup ) -> Android . View . View {
45
-
46
- fatalError ( " \( #function) must be implemented in subclass " )
47
- }
21
+ func getView( position: Int , convertView: Android . View . View ? , parent: Android . View . ViewGroup ) -> Android . View . View
48
22
}
49
23
50
24
// MARK: - Private
51
25
52
- fileprivate extension Android . Widget . Adapter {
53
-
54
- /// JNI Cache
55
- struct JNICache {
56
-
57
- static let classSignature = Android . SwiftSupport. className ( [ " SwiftAdapter " ] )
58
-
59
- /// JNI Java class name
60
- static let className = classSignature. rawValue
61
-
62
- /// JNI Java class
63
- static var jniClass : jclass ? = AndroidWidgetAdapterListenerLocal . _proxyClass
64
-
65
- /// JNI Method ID cache
66
- struct MethodID {
67
-
68
- static var notifyDataSetChanged : jmethodID ?
69
- }
70
- }
71
- }
72
-
73
26
private typealias AndroidWidgetAdapter_getCount_type = @convention ( c) ( _: UnsafeMutablePointer < JNIEnv ? > , _: jobject ? , _: jlong ) -> ( jint )
74
27
75
28
private func AndroidWidgetAdapter_getCount( _ __env: UnsafeMutablePointer < JNIEnv ? > ,
@@ -102,61 +55,18 @@ private func AndroidWidgetAdapter_getView( _ __env: UnsafeMutablePointer<JNIEnv?
102
55
103
56
var __locals = [ jobject] ( )
104
57
105
- return result? . localJavaObject ( & __locals)
58
+ return result. localJavaObject ( & __locals)
106
59
}
107
60
108
- fileprivate final class AndroidWidgetAdapterListenerProxy : AndroidWidgetAdapterListenerProtocol {
109
-
110
- weak var swiftObject : Android . Widget . Adapter ?
111
-
112
- func getCount( ) -> Int {
113
-
114
- return swiftObject? . getCount ( ) ?? 0
115
- }
116
-
117
- func getView( position: Int , convertView: Android . View . View ? , parent: Android . View . ViewGroup ) -> Android . View . View ? {
118
-
119
- return swiftObject? . getView ( position: position, convertView: convertView, parent: parent)
120
- }
121
- }
122
-
123
- fileprivate protocol AndroidWidgetAdapterListenerProtocol : JavaProtocol {
124
-
125
- func getCount( ) -> Int
126
-
127
- func getView( position: Int , convertView: Android . View . View ? , parent: Android . View . ViewGroup ) -> Android . View . View ?
128
- }
129
-
130
- extension AndroidWidgetAdapterListenerProtocol {
131
-
132
- public func localJavaObject( _ locals: UnsafeMutablePointer < [ jobject ] > ) -> jobject ? {
133
-
134
- return AndroidWidgetAdapterListenerLocal ( owned: self , proto: self ) . localJavaObject ( locals )
135
- }
136
-
137
- }
138
-
139
- fileprivate class AndroidWidgetAdapterListenerLocal : JNILocalProxy < AndroidWidgetAdapterListenerProtocol , Any > {
61
+ internal class AndroidWidgetAdapterListenerLocal : JNILocalProxy < AndroidWidgetAdapter , Any > {
140
62
141
63
fileprivate static let _proxyClass : jclass = {
142
64
143
- var natives = [ JNINativeMethod] ( )
144
-
145
- let getCountThunk : AndroidWidgetAdapter_getCount_type = AndroidWidgetAdapter_getCount
146
-
147
- natives. append ( JNINativeMethod ( name: strdup ( " __getCount " ) ,
148
- signature: strdup ( " (J)I " ) ,
149
- fnPtr: unsafeBitCast ( getCountThunk, to: UnsafeMutableRawPointer . self ) ) )
150
-
151
- let getViewThunk : AndroidWidgetAdapter_getView_type = AndroidWidgetAdapter_getView
152
-
153
- natives. append ( JNINativeMethod ( name: strdup ( " __getView " ) ,
154
- signature: strdup ( " (JILandroid/view/View;Landroid/view/ViewGroup;)Landroid/view/View; " ) ,
155
- fnPtr: unsafeBitCast ( getViewThunk, to: UnsafeMutableRawPointer . self ) ) )
156
-
157
- natives. append ( JNINativeMethod ( name: strdup ( " __finalize " ) ,
158
- signature: strdup ( " (J)V " ) ,
159
- fnPtr: unsafeBitCast ( JNIReleasableProxy__finalize_thunk, to: UnsafeMutableRawPointer . self ) ) )
65
+ var natives : [ JNINativeMethod ] = [
66
+ JNICache . Method. getCount. method,
67
+ JNICache . Method. getView. method,
68
+ . finalize
69
+ ]
160
70
161
71
let clazz = JNI . FindClass ( proxyClassName ( ) )
162
72
@@ -172,24 +82,59 @@ fileprivate class AndroidWidgetAdapterListenerLocal: JNILocalProxy<AndroidWidget
172
82
return JNI . api. NewGlobalRef ( JNI . env, clazz ) !
173
83
} ( )
174
84
175
- override open class func proxyClassName( ) -> String { return Android . Widget . Adapter . JNICache. className }
85
+ override open class func proxyClassName( ) -> String { return JNICache . className }
176
86
177
87
override open class func proxyClass( ) -> jclass ? { return _proxyClass }
88
+ }
89
+
90
+ extension AndroidWidgetAdapter {
178
91
179
- func notifyDataSetChanged ( ) {
92
+ public func localJavaObject ( _ locals : UnsafeMutablePointer < [ jobject ] > ) -> jobject ? {
180
93
181
- var __locals = [ jobject] ( )
94
+ return AndroidWidgetAdapterListenerLocal ( owned: self , proto: self ) . localJavaObject ( locals )
95
+ }
96
+ }
97
+
98
+ internal extension AndroidWidgetAdapterListenerLocal {
99
+
100
+ /// JNI Cache
101
+ struct JNICache {
102
+
103
+ static let classSignature = Android . SwiftSupport. className ( [ " SwiftAdapter " ] )
182
104
183
- var __args = [ jvalue] ( repeating: jvalue ( ) , count: 1 )
105
+ /// JNI Java class name
106
+ static let className = classSignature. rawValue
184
107
185
- withJavaObject {
108
+ /// JNI Method cache
109
+ fileprivate enum Method {
110
+
111
+ enum getCount : JNINativeMethodEntry {
112
+
113
+ static let name = " __getCount "
114
+
115
+ /// "(J)I"
116
+ static let signature = JNIMethodSignature ( argumentTypes: [ . long] , returnType: . int)
117
+
118
+ static let thunk : AndroidWidgetAdapter_getCount_type = AndroidWidgetAdapter_getCount
119
+ }
120
+
121
+ enum getView : JNINativeMethodEntry {
122
+
123
+ static let name = " __getView "
124
+
125
+ /// "(JILandroid/view/View;Landroid/view/ViewGroup;)Landroid/view/View;"
126
+ static let signature = JNIMethodSignature (
127
+ argumentTypes: [
128
+ . long,
129
+ . int,
130
+ . object( Android . View. ViewGroup. ViewGroupJNICache. classSignature)
131
+ ] ,
132
+ returnType: . object( Android . View. View. JNICache. classSignature)
133
+ )
134
+
135
+ static let thunk : AndroidWidgetAdapter_getView_type = AndroidWidgetAdapter_getView
136
+ }
186
137
187
- JNIMethod . CallVoidMethod ( object: $0,
188
- methodName: " notifyDataSetChanged " ,
189
- methodSig: " ()V " ,
190
- methodCache: & Android. Widget. Adapter. JNICache. MethodID. notifyDataSetChanged,
191
- args: & __args,
192
- locals: & __locals)
193
138
}
194
139
}
195
140
}
0 commit comments