@@ -37,127 +37,179 @@ internal class PluginLoaderBinder(private val mDynamicPluginLoader: DynamicPlugi
37
37
reply : android.os.Parcel ? ,
38
38
flags : Int
39
39
): Boolean {
40
+ if (reply == null ) {
41
+ throw NullPointerException (" reply == null" )
42
+ }
40
43
when (code) {
41
44
IBinder .INTERFACE_TRANSACTION -> {
42
- reply!! .writeString(PluginLoader .DESCRIPTOR )
45
+ reply.writeString(PluginLoader .DESCRIPTOR )
43
46
return true
44
47
}
45
48
PluginLoader .TRANSACTION_loadPlugin -> {
46
49
data.enforceInterface(PluginLoader .DESCRIPTOR )
47
50
val _arg0 : String
48
51
_arg0 = data.readString()!!
49
- mDynamicPluginLoader.loadPlugin(_arg0 )
50
- reply!! .writeNoException()
52
+ try {
53
+ mDynamicPluginLoader.loadPlugin(_arg0 )
54
+ reply.writeNoException()
55
+ } catch (e: Exception ) {
56
+ reply.writeException(wrapExceptionForBinder(e))
57
+ }
51
58
return true
52
59
}
53
60
PluginLoader .TRANSACTION_getLoadedPlugin -> {
54
61
data.enforceInterface(PluginLoader .DESCRIPTOR )
55
- val _result = mDynamicPluginLoader.getLoadedPlugin()
56
- reply!! .writeNoException()
57
- reply.writeMap(_result as Map <* , * >? )
62
+ try {
63
+ val _result = mDynamicPluginLoader.getLoadedPlugin()
64
+ reply.writeNoException()
65
+ reply.writeMap(_result as Map <* , * >? )
66
+ } catch (e: Exception ) {
67
+ reply.writeException(wrapExceptionForBinder(e))
68
+ }
69
+
58
70
return true
59
71
}
60
72
PluginLoader .TRANSACTION_callApplicationOnCreate -> {
61
73
data.enforceInterface(PluginLoader .DESCRIPTOR )
62
74
val _arg0 : String
63
75
_arg0 = data.readString()!!
64
- mDynamicPluginLoader.callApplicationOnCreate(_arg0 )
65
- reply!! .writeNoException()
76
+ try {
77
+ mDynamicPluginLoader.callApplicationOnCreate(_arg0 )
78
+ reply.writeNoException()
79
+ } catch (e: Exception ) {
80
+ reply.writeException(wrapExceptionForBinder(e))
81
+ }
82
+
66
83
return true
67
84
}
68
85
PluginLoader .TRANSACTION_convertActivityIntent -> {
69
86
data.enforceInterface(PluginLoader .DESCRIPTOR )
70
- val _arg0 : android.content.Intent ?
71
- if (0 != data.readInt()) {
72
- _arg0 = android.content.Intent .CREATOR .createFromParcel(data)
87
+ val intent = if (0 != data.readInt()) {
88
+ Intent .CREATOR .createFromParcel(data)
73
89
} else {
74
- _arg0 = null
90
+ reply.writeException(NullPointerException (" intent==null" ))
91
+ return true
75
92
}
76
- val _result =
77
- mDynamicPluginLoader.convertActivityIntent(_arg0 !! )// todo #32 去掉这个不安全的!!
78
- reply!! .writeNoException()
79
- if (_result != null ) {
80
- reply.writeInt(1 )
81
- _result .writeToParcel(
82
- reply,
83
- android.os.Parcelable .PARCELABLE_WRITE_RETURN_VALUE
84
- )
85
- } else {
86
- reply.writeInt(0 )
93
+
94
+ try {
95
+ val _result =
96
+ mDynamicPluginLoader.convertActivityIntent(intent)
97
+ reply.writeNoException()
98
+ if (_result != null ) {
99
+ reply.writeInt(1 )
100
+ _result .writeToParcel(
101
+ reply,
102
+ android.os.Parcelable .PARCELABLE_WRITE_RETURN_VALUE
103
+ )
104
+ } else {
105
+ reply.writeInt(0 )
106
+ }
107
+ } catch (e: Exception ) {
108
+ reply.writeException(wrapExceptionForBinder(e))
87
109
}
88
110
return true
89
111
}
90
112
PluginLoader .TRANSACTION_startPluginService -> {
91
113
data.enforceInterface(PluginLoader .DESCRIPTOR )
92
- val _arg0 : android.content.Intent ?
93
- if (0 != data.readInt()) {
94
- _arg0 = android.content.Intent .CREATOR .createFromParcel(data)
114
+ val intent = if (0 != data.readInt()) {
115
+ Intent .CREATOR .createFromParcel(data)
95
116
} else {
96
- _arg0 = null
117
+ reply.writeException(NullPointerException (" intent==null" ))
118
+ return true
97
119
}
98
- val _result = mDynamicPluginLoader.startPluginService(_arg0 !! )// todo #32 去掉这个不安全的!!
99
- reply!! .writeNoException()
100
- if (_result != null ) {
101
- reply.writeInt(1 )
102
- _result .writeToParcel(
103
- reply,
104
- android.os.Parcelable .PARCELABLE_WRITE_RETURN_VALUE
105
- )
106
- } else {
107
- reply.writeInt(0 )
120
+
121
+ try {
122
+ val _result = mDynamicPluginLoader.startPluginService(intent)
123
+ reply.writeNoException()
124
+ if (_result != null ) {
125
+ reply.writeInt(1 )
126
+ _result .writeToParcel(
127
+ reply,
128
+ android.os.Parcelable .PARCELABLE_WRITE_RETURN_VALUE
129
+ )
130
+ } else {
131
+ reply.writeInt(0 )
132
+ }
133
+ } catch (e: Exception ) {
134
+ reply.writeException(wrapExceptionForBinder(e))
108
135
}
109
136
return true
110
137
}
111
138
PluginLoader .TRANSACTION_stopPluginService -> {
112
139
data.enforceInterface(PluginLoader .DESCRIPTOR )
113
- val _arg0 : android.content.Intent ?
114
- if (0 != data.readInt()) {
115
- _arg0 = android.content.Intent .CREATOR .createFromParcel(data)
140
+ val intent = if (0 != data.readInt()) {
141
+ Intent .CREATOR .createFromParcel(data)
116
142
} else {
117
- _arg0 = null
143
+ reply.writeException(NullPointerException (" intent==null" ))
144
+ return true
145
+ }
146
+ try {
147
+ val _result = mDynamicPluginLoader.stopPluginService(intent)
148
+ reply.writeNoException()
149
+ reply.writeInt(if (_result ) 1 else 0 )
150
+ } catch (e: Exception ) {
151
+ reply.writeException(wrapExceptionForBinder(e))
118
152
}
119
- val _result = mDynamicPluginLoader.stopPluginService(_arg0 !! )// todo #32 去掉这个不安全的!!
120
- reply!! .writeNoException()
121
- reply.writeInt(if (_result ) 1 else 0 )
122
153
return true
123
154
}
124
155
PluginLoader .TRANSACTION_bindPluginService -> {
125
156
data.enforceInterface(PluginLoader .DESCRIPTOR )
126
- val _arg0 : android.content.Intent ?
127
- if (0 != data.readInt()) {
128
- _arg0 = android.content.Intent .CREATOR .createFromParcel(data)
157
+ val intent = if (0 != data.readInt()) {
158
+ Intent .CREATOR .createFromParcel(data)
129
159
} else {
130
- _arg0 = null
160
+ reply.writeException(NullPointerException (" intent==null" ))
161
+ return true
131
162
}
132
163
val _arg1 = BinderPluginServiceConnection (data.readStrongBinder())
133
164
val _arg2 : Int
134
165
_arg2 = data.readInt()
135
- val _result = mDynamicPluginLoader.bindPluginService(
136
- _arg0 !! ,
137
- _arg1 ,
138
- _arg2
139
- )// todo #32 去掉这个不安全的!!
140
- reply!! .writeNoException()
141
- reply.writeInt(if (_result ) 1 else 0 )
166
+ try {
167
+ val _result = mDynamicPluginLoader.bindPluginService(
168
+ intent,
169
+ _arg1 ,
170
+ _arg2
171
+ )
172
+ reply.writeNoException()
173
+ reply.writeInt(if (_result ) 1 else 0 )
174
+ } catch (e: Exception ) {
175
+ reply.writeException(wrapExceptionForBinder(e))
176
+ }
142
177
return true
143
178
}
144
179
PluginLoader .TRANSACTION_unbindService -> {
145
180
data.enforceInterface(PluginLoader .DESCRIPTOR )
146
- mDynamicPluginLoader.unbindService(data.readStrongBinder())
147
- reply!! .writeNoException()
181
+ try {
182
+ mDynamicPluginLoader.unbindService(data.readStrongBinder())
183
+ reply.writeNoException()
184
+ } catch (e: Exception ) {
185
+ reply.writeException(wrapExceptionForBinder(e))
186
+ }
148
187
return true
149
188
}
150
189
PluginLoader .TRANSACTION_startActivityInPluginProcess -> {
151
190
data.enforceInterface(PluginLoader .DESCRIPTOR )
152
- mDynamicPluginLoader.startActivityInPluginProcess(
153
- Intent .CREATOR .createFromParcel(
154
- data
191
+ try {
192
+ mDynamicPluginLoader.startActivityInPluginProcess(
193
+ Intent .CREATOR .createFromParcel(
194
+ data
195
+ )
155
196
)
156
- )
157
- reply!! .writeNoException()
197
+ reply.writeNoException()
198
+ } catch (e: Exception ) {
199
+ reply.writeException(wrapExceptionForBinder(e))
200
+ }
158
201
return true
159
202
}
160
203
}
161
204
return super .onTransact(code, data, reply, flags)
162
205
}
206
+
207
+ /* *
208
+ * Binder的内置writeException方法只支持特定几种Exception
209
+ * https://developer.android.com/reference/android/os/Parcel.html#writeException(java.lang.Exception)
210
+ */
211
+ private fun wrapExceptionForBinder (e : Exception ): Exception {
212
+ return IllegalStateException (e.message, e.cause)
213
+ }
214
+
163
215
}
0 commit comments