OpenPgpServiceConnection.java

  1/*
  2 * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *      http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package org.openintents.openpgp.util;
 18
 19import android.content.ComponentName;
 20import android.content.Context;
 21import android.content.Intent;
 22import android.content.ServiceConnection;
 23import android.os.IBinder;
 24
 25import org.openintents.openpgp.IOpenPgpService;
 26
 27public class OpenPgpServiceConnection {
 28
 29    // callback interface
 30    public interface OnBound {
 31        public void onBound(IOpenPgpService service);
 32
 33        public void onError(Exception e);
 34    }
 35
 36    private Context mApplicationContext;
 37
 38    private IOpenPgpService mService;
 39    private String mProviderPackageName;
 40
 41    private OnBound mOnBoundListener;
 42
 43    /**
 44     * Create new connection
 45     *
 46     * @param context
 47     * @param providerPackageName specify package name of OpenPGP provider,
 48     *                            e.g., "org.sufficientlysecure.keychain"
 49     */
 50    public OpenPgpServiceConnection(Context context, String providerPackageName) {
 51        this.mApplicationContext = context.getApplicationContext();
 52        this.mProviderPackageName = providerPackageName;
 53    }
 54
 55    /**
 56     * Create new connection with callback
 57     *
 58     * @param context
 59     * @param providerPackageName specify package name of OpenPGP provider,
 60     *                            e.g., "org.sufficientlysecure.keychain"
 61     * @param onBoundListener     callback, executed when connection to service has been established
 62     */
 63    public OpenPgpServiceConnection(Context context, String providerPackageName,
 64                                    OnBound onBoundListener) {
 65        this(context, providerPackageName);
 66        this.mOnBoundListener = onBoundListener;
 67    }
 68
 69    public IOpenPgpService getService() {
 70        return mService;
 71    }
 72
 73    public boolean isBound() {
 74        return (mService != null);
 75    }
 76
 77    private ServiceConnection mServiceConnection = new ServiceConnection() {
 78        public void onServiceConnected(ComponentName name, IBinder service) {
 79            mService = IOpenPgpService.Stub.asInterface(service);
 80            if (mOnBoundListener != null) {
 81                mOnBoundListener.onBound(mService);
 82            }
 83        }
 84
 85        public void onServiceDisconnected(ComponentName name) {
 86            mService = null;
 87        }
 88    };
 89
 90    /**
 91     * If not already bound, bind to service!
 92     *
 93     * @return
 94     */
 95    public void bindToService() {
 96        // if not already bound...
 97        if (mService == null) {
 98            try {
 99                Intent serviceIntent = new Intent(OpenPgpApi.SERVICE_INTENT);
100                // NOTE: setPackage is very important to restrict the intent to this provider only!
101                serviceIntent.setPackage(mProviderPackageName);
102                boolean connect = mApplicationContext.bindService(serviceIntent, mServiceConnection,
103                        Context.BIND_AUTO_CREATE);
104                if (!connect) {
105                    throw new Exception("bindService() returned false!");
106                }
107            } catch (Exception e) {
108                if (mOnBoundListener != null) {
109                    mOnBoundListener.onError(e);
110                }
111            }
112        } else {
113            // already bound, but also inform client about it with callback
114            if (mOnBoundListener != null) {
115                mOnBoundListener.onBound(mService);
116            }
117        }
118    }
119
120    public void unbindFromService() {
121        mApplicationContext.unbindService(mServiceConnection);
122    }
123
124}