Skip to content

Commit df71c40

Browse files
authored
Merge pull request #13 from rMozg/disconnect_crash_fix
Disconnect crash fix
2 parents f4bd1d3 + 4f16dd7 commit df71c40

File tree

4 files changed

+44
-45
lines changed

4 files changed

+44
-45
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 22
99
targetSdkVersion 25
1010
versionCode 2
11-
versionName "0.1.0"
11+
versionName "0.1.1"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {

app/src/main/java/ru/evotor/devices/commons/DeviceServiceConnector.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,15 @@ public static void startInitConnections(final Context appContext, final boolean
6161

6262
DeviceServiceConnector.context = appContext;
6363

64-
new Thread(new Runnable() {
65-
@Override
66-
public void run() {
67-
printerService.initConnection(context, force);
68-
}
69-
}).start();
70-
64+
printerService.startInitConnection(context, force);
7165
}
7266

7367

7468
public static void startDeinitConnections() {
7569
if (context == null) {
7670
return;
7771
}
78-
79-
new Thread(new Runnable() {
80-
@Override
81-
public void run() {
82-
printerService.deInitConnection();
83-
}
84-
}).start();
72+
printerService.startDeinitConnection();
8573
}
8674

8775
public static void processException(Exception exc) throws DeviceServiceException {

app/src/main/java/ru/evotor/devices/commons/services/AbstractService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ public abstract class AbstractService {
1010

1111
protected Context context;
1212

13-
protected abstract void initConnection(Context appContext, boolean force);
13+
protected abstract void startInitConnection(Context appContext, boolean force);
14+
15+
protected abstract void startDeinitConnection();
1416

1517
protected abstract Boolean getServiceConnected();
1618

@@ -26,7 +28,7 @@ public void waitInitService(Context context) throws ServiceNotConnectedException
2628
Boolean connected = this.getServiceConnected();
2729

2830
if (connected == null || !connected) {
29-
this.initConnection(context, false);
31+
this.startInitConnection(context, false);
3032
}
3133

3234
while ((connected = this.getServiceConnected()) == null) {

app/src/main/java/ru/evotor/devices/commons/services/PrinterService.java

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void onServiceConnected(ComponentName name, IBinder binder) {
4141
public void onServiceDisconnected(ComponentName name) {
4242
service = null;
4343
serviceConnected = false;
44-
initConnection(context, false);
44+
startInitConnection(context, false);
4545
for (ConnectionWrapper connectionWrapper : DeviceServiceConnector.getConnectionWrappers()) {
4646
connectionWrapper.onPrinterServiceDisconnected();
4747
}
@@ -56,36 +56,45 @@ public Boolean getServiceConnected() {
5656
return serviceConnected;
5757
}
5858

59-
public void initConnection(Context appContext, boolean force) {
60-
DeviceServiceOperationOnMainThreadException.throwIfMainThread();
61-
62-
if (appContext == null) {
63-
return;
64-
}
65-
context = appContext;
66-
if (service == null || force) {
67-
Intent pr = new Intent(ACTION_PRINTER_SERVICE);
68-
pr.setPackage(TARGET_PACKAGE);
69-
pr.setClassName(TARGET_PACKAGE, TARGET_CLASS_NAME);
70-
serviceConnected = null;
71-
boolean serviceBound = context.bindService(pr, serviceConnection, Service.BIND_AUTO_CREATE);
72-
if (!serviceBound) {
73-
serviceConnected = false;
59+
@Override
60+
public void startInitConnection(final Context appContext, final boolean force) {
61+
new Thread() {
62+
@Override
63+
public void run() {
64+
if (appContext == null) {
65+
return;
66+
}
67+
68+
context = appContext;
69+
if (service == null || force) {
70+
Intent pr = new Intent(ACTION_PRINTER_SERVICE);
71+
pr.setPackage(TARGET_PACKAGE);
72+
pr.setClassName(TARGET_PACKAGE, TARGET_CLASS_NAME);
73+
serviceConnected = null;
74+
boolean serviceBound = context.bindService(pr, serviceConnection, Service.BIND_AUTO_CREATE);
75+
if (!serviceBound) {
76+
serviceConnected = false;
77+
}
78+
}
7479
}
75-
}
80+
}.start();
7681
}
7782

78-
public void deInitConnection() {
79-
DeviceServiceOperationOnMainThreadException.throwIfMainThread();
80-
81-
if (context == null) {
82-
return;
83-
}
84-
context.unbindService(serviceConnection);
85-
service = null;
86-
for (ConnectionWrapper connectionWrapper : DeviceServiceConnector.getConnectionWrappers()) {
87-
connectionWrapper.onPrinterServiceDisconnected();
88-
}
83+
@Override
84+
public void startDeinitConnection() {
85+
new Thread() {
86+
@Override
87+
public void run() {
88+
if (context == null) {
89+
return;
90+
}
91+
context.unbindService(serviceConnection);
92+
service = null;
93+
for (ConnectionWrapper connectionWrapper : DeviceServiceConnector.getConnectionWrappers()) {
94+
connectionWrapper.onPrinterServiceDisconnected();
95+
}
96+
}
97+
}.start();
8998
}
9099

91100
public int getAllowableSymbolsLineLength(int deviceId) throws DeviceServiceException {

0 commit comments

Comments
 (0)