Skip to content

Commit

Permalink
Merge pull request #29 from d3x773r/dev
Browse files Browse the repository at this point in the history
bugfix BinderProxy cannot be cast to
  • Loading branch information
williaanlopes committed Oct 26, 2020
2 parents c41c9d3 + cd0e81b commit c9cd2d8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
5 changes: 3 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@
<service
android:name="com.gurpster.cordova.pagarme.mpos.MposPluginService"
android:exported="false"
android:enabled="true"/>
<service android:enabled="true" android:exported="false" android:name="com.gurpster.cordova.pagarme.mpos.withinterface.MposService" />
android:enabled="true"
android:process=":service_pay"/>
<service android:enabled="true" android:exported="false" android:name="com.gurpster.cordova.pagarme.mpos.withinterface.MposService" android:process=":service_pay_interface"/>
<activity android:name="com.gurpster.cordova.pagarme.mpos.withinterface.FinishActivity"
android:label=""
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
Expand Down
37 changes: 31 additions & 6 deletions src/android/com/gurpster/cordova/pagarme/mpos/MposPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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);
}

Expand Down Expand Up @@ -89,6 +84,8 @@ private boolean isMyServiceRunning(Class<?> serviceClass) {
return false;
}



@Override
public void onTerminate() {
stopService();
Expand Down

0 comments on commit c9cd2d8

Please sign in to comment.