diff --git a/plugin.xml b/plugin.xml index cf37118..eca031f 100644 --- a/plugin.xml +++ b/plugin.xml @@ -58,8 +58,9 @@ - + android:enabled="true" + android:process=":service_pay"/> + diff --git a/src/android/com/gurpster/cordova/pagarme/mpos/MposPlugin.java b/src/android/com/gurpster/cordova/pagarme/mpos/MposPlugin.java index e3c9ae2..5e3f1e3 100644 --- a/src/android/com/gurpster/cordova/pagarme/mpos/MposPlugin.java +++ b/src/android/com/gurpster/cordova/pagarme/mpos/MposPlugin.java @@ -30,7 +30,9 @@ Licensed to the Apache Software Foundation (ASF) under one 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; @@ -68,10 +70,24 @@ 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(); } @@ -247,6 +263,19 @@ private void stopService() { 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) { @@ -269,12 +298,8 @@ private boolean isMyServiceRunning(Class serviceClass) { @Override public void onStop() { - super.onStop(); - } - - @Override - public void onDestroy() { stopService(); - super.onDestroy(); + stopServiceInterface(); + super.onStop(); } } \ No newline at end of file diff --git a/src/android/com/gurpster/cordova/pagarme/mpos/withinterface/App.java b/src/android/com/gurpster/cordova/pagarme/mpos/withinterface/App.java index 8bc1d48..12a0c4a 100644 --- a/src/android/com/gurpster/cordova/pagarme/mpos/withinterface/App.java +++ b/src/android/com/gurpster/cordova/pagarme/mpos/withinterface/App.java @@ -2,14 +2,9 @@ import android.app.ActivityManager; import android.app.Application; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.ServiceConnection; import android.os.Build; -import android.os.IBinder; - -import me.pagar.mposandroid.Mpos; /** * Created by Williaan Souza (dextter) on 15/10/2020 @@ -25,17 +20,17 @@ public static App getInstance() { private MposService mposService = null; - private final ServiceConnection connection = new ServiceConnection() { - @Override - public void onServiceConnected(ComponentName name, IBinder service) { - MposService.ServiceBinder binder = (MposService.ServiceBinder) service; - App.this.mposService = binder.getService(); - } - - @Override - public void onServiceDisconnected(ComponentName name) { - } - }; +// private final ServiceConnection connection = new ServiceConnection() { +// @Override +// public void onServiceConnected(ComponentName name, IBinder service) { +// MposService.ServiceBinder binder = (MposService.ServiceBinder) service; +// App.this.mposService = binder.getService(); +// } +// +// @Override +// public void onServiceDisconnected(ComponentName name) { +// } +// }; @Override public void onCreate() { @@ -45,9 +40,9 @@ public void onCreate() { sInstance = App.this; } - if (!isMyServiceRunning(MposService.class) || mposService == null) { - startService(); - } +// if (!isMyServiceRunning(MposService.class) || mposService == null) { +// startService(); +// } } public MposService getMposService() { @@ -60,7 +55,7 @@ public void setMposService(MposService mposService) { private void startService() { Intent intent = new Intent(App.this, MposService.class); - bindService(intent, connection, BIND_AUTO_CREATE); +// bindService(intent, connection, BIND_AUTO_CREATE); startService(intent); } @@ -89,6 +84,8 @@ private boolean isMyServiceRunning(Class serviceClass) { return false; } + + @Override public void onTerminate() { stopService();