-
Notifications
You must be signed in to change notification settings - Fork 0
/
MposPlugin.java
305 lines (266 loc) · 11 KB
/
MposPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package com.gurpster.cordova.pagarme.mpos;
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http:https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
import android.app.Activity;
import android.app.ActivityManager;
import android.bluetooth.BluetoothAdapter;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.gurpster.cordova.pagarme.mpos.withinterface.App;
import com.gurpster.cordova.pagarme.mpos.withinterface.ChargeActivity;
import com.gurpster.cordova.pagarme.mpos.withinterface.MposService;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PermissionHelper;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.reflect.Method;
import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;
import static android.content.Context.BIND_AUTO_CREATE;
import static org.apache.cordova.media.AudioHandler.permissions;
public class MposPlugin extends CordovaPlugin {
private static final String TAG = MposPlugin.class.getSimpleName();
private MposPluginService mposPluginService;
private JSONArray requestArgs;
private CallbackContext callbackContext;
private String firedAction;
private PluginResult pluginResult;
private final ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MposPluginService.MposPluginServiceBinder binder = (MposPluginService.MposPluginServiceBinder) service;
MposPlugin.this.mposPluginService = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
private final ServiceConnection connectionWithInterface = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MposService.ServiceBinder binder = (MposService.ServiceBinder) service;
MposService mposService = binder.getService();
App.getInstance().setMposService(mposService);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void pluginInitialize() {
if (!isMyServiceRunning(MposPluginService.class) || mposPluginService == null) {
startService();
startServiceInterface();
}
super.pluginInitialize();
}
public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;
this.requestArgs = args;
this.firedAction = action;
if (action.equals("payWithInterface")) {
Intent chargeIntent = new Intent(
cordova.getActivity(),
ChargeActivity.class
);
// Charge charge = JSON.parseObject(
// getIntent().getStringExtra("data"),
// Charge.class
// );
chargeIntent.putExtra("data", args.getJSONObject(0).toString());
cordova.startActivityForResult(this, chargeIntent, 847);
return true;
}
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
callbackContext.error("error.toString()");
return false;
}
if (args != null) {
if (!bluetoothAdapter.isEnabled()) {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("status", 41);
jsonObject.put("message", "Error bluetooth not enabled");
callbackContext.error(jsonObject);
} catch (Exception e) {
}
// this.pluginResult = PluginResultHelper.makePluginResult(PluginResult.Status.OK, jsonObject);
// pluginResult.setKeepCallback(true);
// callbackContext.sendPluginResult(pluginResult);
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
cordova.getActivity().startActivityForResult(enableBtIntent, Constants.REQUEST_ENABLE_BT);
} else {
if (!isMyServiceRunning(MposPluginService.class) || mposPluginService == null) {
startService();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
invokeMethod(action, args, callbackContext);
}
}, 500);
} else {
invokeMethod(action, args, callbackContext);
}
}
} else {
callbackContext.error("Invalid parameters");
}
return true;
}
/**
* check application's permissions
*/
public boolean hasPermission() {
for (String p : permissions) {
if (!PermissionHelper.hasPermission(this, p)) {
return false;
}
}
return true;
}
/**
* We override this so that we can access the permissions variable, which no longer exists in
* the parent class, since we can't initialize it reliably in the constructor!
*
* @param requestCode The code to get request action
*/
public void requestPermissions(int requestCode) {
PermissionHelper.requestPermissions(this, requestCode, permissions);
}
/**
* processes the result of permission request
*
* @param requestCode The code to get request action
* @param permissions The collection of permissions
* @param grantResults The result of grant
*/
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
PluginResult result;
for (int r : grantResults) {
if (r == PackageManager.PERMISSION_DENIED) {
Log.d(TAG, "Permission Denied!");
result = new PluginResult(PluginResult.Status.ILLEGAL_ACCESS_EXCEPTION);
this.callbackContext.sendPluginResult(result);
return;
}
}
if (requestCode == 0) {
initialize(this.requestArgs);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK && requestCode == Constants.REQUEST_ENABLE_BT) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
invokeMethod(firedAction, requestArgs, callbackContext);
}
}, 500);
} else if (resultCode == RESULT_OK && requestCode == 847) {
this.callbackContext.success();
} else if (resultCode == RESULT_CANCELED && requestCode == 847) {
this.callbackContext.error(Constants.PAYMENT_ERROR);
} else {
this.callbackContext.error("Bluetooth not enabled");
}
}
private void initialize(final JSONArray jsonArray) {
invokeMethod("initialize", jsonArray, callbackContext);
}
private void invokeMethod(String methodName, JSONArray jsonArray, CallbackContext callbackContext) {
if (isMyServiceRunning(MposPluginService.class) && mposPluginService != null) {
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
try {
Method method = mposPluginService.getClass().getDeclaredMethod(
methodName,
jsonArray.getClass(),
callbackContext.getClass()
);
method.setAccessible(true);
method.invoke(
mposPluginService,
jsonArray,
callbackContext
);
} catch (Exception e) {
callbackContext.error(e.getMessage());
}
}
});
}
}
private void startService() {
Activity context = cordova.getActivity();
Intent intent = new Intent(context, MposPluginService.class);
context.bindService(intent, connection, BIND_AUTO_CREATE);
context.startService(intent);
}
private void stopService() {
Activity context = cordova.getActivity();
Intent intent = new Intent(context, MposPluginService.class);
context.stopService(intent);
}
private void startServiceInterface() {
Activity context = cordova.getActivity();
Intent intent = new Intent(context, MposService.class);
context.bindService(intent, connection, BIND_AUTO_CREATE);
context.startService(intent);
}
private void stopServiceInterface() {
Activity context = cordova.getActivity();
Intent intent = new Intent(context, MposService.class);
context.stopService(intent);
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
if (mposPluginService == null) {
return false;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return mposPluginService.isRunning;
} else {
ActivityManager manager = (ActivityManager) cordova.getActivity().getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
}
return false;
}
@Override
public void onStop() {
stopService();
stopServiceInterface();
super.onStop();
}
}