mirror of
https://github.com/moparisthebest/Conversations
synced 2024-11-28 19:52:15 -05:00
Refactor OpenPgpServiceConnection with better callback
This commit is contained in:
parent
940a1d8eeb
commit
cbfeb90cec
@ -26,9 +26,11 @@ import android.os.IBinder;
|
|||||||
|
|
||||||
public class OpenPgpServiceConnection {
|
public class OpenPgpServiceConnection {
|
||||||
|
|
||||||
// interface to create callbacks for onServiceConnected
|
// callback interface
|
||||||
public interface OnBound {
|
public interface OnBound {
|
||||||
public void onBound(IOpenPgpService service);
|
public void onBound(IOpenPgpService service);
|
||||||
|
|
||||||
|
public void onError(Exception e);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Context mApplicationContext;
|
private Context mApplicationContext;
|
||||||
@ -39,19 +41,19 @@ public class OpenPgpServiceConnection {
|
|||||||
private OnBound mOnBoundListener;
|
private OnBound mOnBoundListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create new OpenPgpServiceConnection
|
* Create new connection
|
||||||
*
|
*
|
||||||
* @param context
|
* @param context
|
||||||
* @param providerPackageName specify package name of OpenPGP provider,
|
* @param providerPackageName specify package name of OpenPGP provider,
|
||||||
* e.g., "org.sufficientlysecure.keychain"
|
* e.g., "org.sufficientlysecure.keychain"
|
||||||
*/
|
*/
|
||||||
public OpenPgpServiceConnection(Context context, String providerPackageName) {
|
public OpenPgpServiceConnection(Context context, String providerPackageName) {
|
||||||
this.mApplicationContext = context.getApplicationContext();
|
this.mApplicationContext = context;
|
||||||
this.mProviderPackageName = providerPackageName;
|
this.mProviderPackageName = providerPackageName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create new OpenPgpServiceConnection
|
* Create new connection with callback
|
||||||
*
|
*
|
||||||
* @param context
|
* @param context
|
||||||
* @param providerPackageName specify package name of OpenPGP provider,
|
* @param providerPackageName specify package name of OpenPGP provider,
|
||||||
@ -60,8 +62,7 @@ public class OpenPgpServiceConnection {
|
|||||||
*/
|
*/
|
||||||
public OpenPgpServiceConnection(Context context, String providerPackageName,
|
public OpenPgpServiceConnection(Context context, String providerPackageName,
|
||||||
OnBound onBoundListener) {
|
OnBound onBoundListener) {
|
||||||
this.mApplicationContext = context.getApplicationContext();
|
this(context, providerPackageName);
|
||||||
this.mProviderPackageName = providerPackageName;
|
|
||||||
this.mOnBoundListener = onBoundListener;
|
this.mOnBoundListener = onBoundListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,23 +92,25 @@ public class OpenPgpServiceConnection {
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean bindToService() {
|
public void bindToService() {
|
||||||
// if not already bound...
|
// if not already bound...
|
||||||
if (mService == null) {
|
if (mService == null) {
|
||||||
try {
|
try {
|
||||||
Intent serviceIntent = new Intent();
|
Intent serviceIntent = new Intent(OpenPgpApi.SERVICE_INTENT);
|
||||||
serviceIntent.setAction(IOpenPgpService.class.getName());
|
|
||||||
// NOTE: setPackage is very important to restrict the intent to this provider only!
|
// NOTE: setPackage is very important to restrict the intent to this provider only!
|
||||||
serviceIntent.setPackage(mProviderPackageName);
|
serviceIntent.setPackage(mProviderPackageName);
|
||||||
mApplicationContext.bindService(serviceIntent, mServiceConnection,
|
mApplicationContext.bindService(serviceIntent, mServiceConnection,
|
||||||
Context.BIND_AUTO_CREATE);
|
Context.BIND_AUTO_CREATE);
|
||||||
|
|
||||||
return true;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return false;
|
if (mOnBoundListener != null) {
|
||||||
|
mOnBoundListener.onError(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return true;
|
// already bound, but also inform client about it with callback
|
||||||
|
if (mOnBoundListener != null) {
|
||||||
|
mOnBoundListener.onBound(mService);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user