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 org.openintents.openpgp.IOpenPgpService;
 20
 21import android.content.ComponentName;
 22import android.content.Context;
 23import android.content.Intent;
 24import android.content.ServiceConnection;
 25import android.os.IBinder;
 26
 27public class OpenPgpServiceConnection {
 28
 29    // interface to create callbacks for onServiceConnected
 30    public interface OnBound {
 31        public void onBound(IOpenPgpService service);
 32    }
 33
 34    private Context mApplicationContext;
 35
 36    private IOpenPgpService mService;
 37    private String mProviderPackageName;
 38
 39    private OnBound mOnBoundListener;
 40
 41    /**
 42     * Create new OpenPgpServiceConnection
 43     *
 44     * @param context
 45     * @param providerPackageName specify package name of OpenPGP provider,
 46     *                            e.g., "org.sufficientlysecure.keychain"
 47     */
 48    public OpenPgpServiceConnection(Context context, String providerPackageName) {
 49        this.mApplicationContext = context.getApplicationContext();
 50        this.mProviderPackageName = providerPackageName;
 51    }
 52
 53    /**
 54     * Create new OpenPgpServiceConnection
 55     *
 56     * @param context
 57     * @param providerPackageName specify package name of OpenPGP provider,
 58     *                            e.g., "org.sufficientlysecure.keychain"
 59     * @param onBoundListener     callback, executed when connection to service has been established
 60     */
 61    public OpenPgpServiceConnection(Context context, String providerPackageName,
 62                                    OnBound onBoundListener) {
 63        this.mApplicationContext = context.getApplicationContext();
 64        this.mProviderPackageName = providerPackageName;
 65        this.mOnBoundListener = onBoundListener;
 66    }
 67
 68    public IOpenPgpService getService() {
 69        return mService;
 70    }
 71
 72    public boolean isBound() {
 73        return (mService != null);
 74    }
 75
 76    private ServiceConnection mServiceConnection = new ServiceConnection() {
 77        public void onServiceConnected(ComponentName name, IBinder service) {
 78            mService = IOpenPgpService.Stub.asInterface(service);
 79            if (mOnBoundListener != null) {
 80                mOnBoundListener.onBound(mService);
 81            }
 82        }
 83
 84        public void onServiceDisconnected(ComponentName name) {
 85            mService = null;
 86        }
 87    };
 88
 89    /**
 90     * If not already bound, bind to service!
 91     *
 92     * @return
 93     */
 94    public boolean bindToService() {
 95        // if not already bound...
 96        if (mService == null) {
 97            try {
 98                Intent serviceIntent = new Intent();
 99                serviceIntent.setAction(IOpenPgpService.class.getName());
100                // NOTE: setPackage is very important to restrict the intent to this provider only!
101                serviceIntent.setPackage(mProviderPackageName);
102                mApplicationContext.bindService(serviceIntent, mServiceConnection,
103                        Context.BIND_AUTO_CREATE);
104
105                return true;
106            } catch (Exception e) {
107                return false;
108            }
109        } else {
110            return true;
111        }
112    }
113
114    public void unbindFromService() {
115        mApplicationContext.unbindService(mServiceConnection);
116    }
117
118}