avoid binding multiple times from BarcodeService
This commit is contained in:
parent
c62f3f99be
commit
96a992353b
|
@ -1,6 +1,5 @@
|
|||
package eu.siacs.conversations.services;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.ComponentName;
|
||||
import android.content.ContentProvider;
|
||||
import android.content.ContentValues;
|
||||
|
@ -11,7 +10,6 @@ import android.database.Cursor;
|
|||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.CancellationSignal;
|
||||
import android.os.IBinder;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
|
@ -23,19 +21,15 @@ import com.google.zxing.EncodeHintType;
|
|||
import com.google.zxing.aztec.AztecWriter;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import eu.siacs.conversations.Config;
|
||||
import eu.siacs.conversations.entities.Account;
|
||||
import eu.siacs.conversations.utils.CryptoHelper;
|
||||
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
|
||||
import eu.siacs.conversations.xmpp.jid.Jid;
|
||||
|
||||
public class BarcodeProvider extends ContentProvider implements ServiceConnection {
|
||||
|
@ -45,6 +39,7 @@ public class BarcodeProvider extends ContentProvider implements ServiceConnectio
|
|||
private final Object lock = new Object();
|
||||
|
||||
private XmppConnectionService mXmppConnectionService;
|
||||
private boolean mBindingInProcess = false;
|
||||
|
||||
@Override
|
||||
public boolean onCreate() {
|
||||
|
@ -129,14 +124,19 @@ public class BarcodeProvider extends ContentProvider implements ServiceConnectio
|
|||
|
||||
private boolean connectAndWait() {
|
||||
Intent intent = new Intent(getContext(), XmppConnectionService.class);
|
||||
intent.setAction("contact_chooser");
|
||||
intent.setAction(this.getClass().getSimpleName());
|
||||
Context context = getContext();
|
||||
if (context != null) {
|
||||
synchronized (this) {
|
||||
if (mXmppConnectionService == null && !mBindingInProcess) {
|
||||
Log.d(Config.LOGTAG,"calling to bind service");
|
||||
context.startService(intent);
|
||||
context.bindService(intent, this, Context.BIND_AUTO_CREATE);
|
||||
this.mBindingInProcess = true;
|
||||
}
|
||||
}
|
||||
try {
|
||||
waitForService();
|
||||
Log.d(Config.LOGTAG, "service initialized");
|
||||
return true;
|
||||
} catch (InterruptedException e) {
|
||||
return false;
|
||||
|
@ -149,23 +149,30 @@ public class BarcodeProvider extends ContentProvider implements ServiceConnectio
|
|||
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||
synchronized (this) {
|
||||
XmppConnectionService.XmppConnectionBinder binder = (XmppConnectionService.XmppConnectionBinder) service;
|
||||
mXmppConnectionService = binder.getService();
|
||||
mBindingInProcess = false;
|
||||
synchronized (this.lock) {
|
||||
lock.notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
synchronized (this) {
|
||||
mXmppConnectionService = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void waitForService() throws InterruptedException {
|
||||
if (mXmppConnectionService == null) {
|
||||
synchronized (this.lock) {
|
||||
lock.wait();
|
||||
}
|
||||
} else {
|
||||
Log.d(Config.LOGTAG,"not waiting for service because already initialized");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue