avoid binding multiple times from BarcodeService

This commit is contained in:
Daniel Gultsch 2016-12-01 11:34:04 +01:00
parent c62f3f99be
commit 96a992353b

View file

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