Example usage for android.content ServiceConnection ServiceConnection

List of usage examples for android.content ServiceConnection ServiceConnection

Introduction

In this page you can find the example usage for android.content ServiceConnection ServiceConnection.

Prototype

ServiceConnection

Source Link

Usage

From source file:com.soomla.store.billing.nokia.NokiaIabHelper.java

/**
 * See parent//w  w  w . j  a  v  a 2  s .  c om
 */
protected void startSetupInner() {
    SoomlaUtils.LogDebug(TAG, "startSetupInner launched");
    mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            SoomlaUtils.LogDebug(TAG, "Billing service disconnected.");
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            SoomlaUtils.LogDebug(TAG, "Billing service connected.");
            mService = INokiaIAPService.Stub.asInterface(service);
            String packageName = SoomlaApp.getAppContext().getPackageName();
            try {
                SoomlaUtils.LogDebug(TAG, "Checking for in-app billing 3 support.");

                // check for in-app billing v3 support
                int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
                if (response != IabResult.BILLING_RESPONSE_RESULT_OK) {
                    setupFailed(new IabResult(response, "Error checking for billing v3 support."));
                    return;
                }
                SoomlaUtils.LogDebug(TAG, "In-app billing version 3 supported for " + packageName);

                setupSuccess();
            } catch (RemoteException e) {
                setupFailed(new IabResult(IabResult.IABHELPER_REMOTE_EXCEPTION,
                        "RemoteException while setting up in-app billing."));
                e.printStackTrace();
            }
        }
    };

    SoomlaApp.getAppContext().bindService(new Intent("com.nokia.payment.iapenabler.InAppBillingService.BIND"),
            mServiceConn, Context.BIND_AUTO_CREATE);
}

From source file:com.onesignal.TrackGooglePurchase.java

void trackIAP() {
    if (mServiceConn == null) {
        mServiceConn = new ServiceConnection() {
            @Override// w  w w  . ja v  a  2 s . c  o m
            public void onServiceDisconnected(ComponentName name) {
                iapEnabled = -99;
                mIInAppBillingService = null;
            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                try {
                    Class<?> stubClass = Class.forName("com.android.vending.billing.IInAppBillingService$Stub");
                    Method asInterfaceMethod = getAsInterfaceMethod(stubClass);

                    asInterfaceMethod.setAccessible(true);
                    mIInAppBillingService = asInterfaceMethod.invoke(null, service);

                    QueryBoughtItems();
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        };

        Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
        serviceIntent.setPackage("com.android.vending");

        appContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
    } else if (mIInAppBillingService != null)
        QueryBoughtItems();
}

From source file:com.soomla.store.billing.google.GoogleIabHelper.java

/**
 * See parent//from ww w. ja  v a  2s  .  co  m
 */
protected void startSetupInner() {
    mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            SoomlaUtils.LogDebug(TAG, "Billing service disconnected.");
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            SoomlaUtils.LogDebug(TAG, "Billing service connected.");
            mService = IInAppBillingService.Stub.asInterface(service);
            String packageName = SoomlaApp.getAppContext().getPackageName();
            try {
                SoomlaUtils.LogDebug(TAG, "Checking for in-app billing 3 support.");

                // check for in-app billing v3 support
                int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
                if (response != IabResult.BILLING_RESPONSE_RESULT_OK) {
                    setupFailed(new IabResult(response, "Error checking for billing v3 support."));
                    return;
                }
                SoomlaUtils.LogDebug(TAG, "In-app billing version 3 supported for " + packageName);

                setupSuccess();
            } catch (RemoteException e) {
                setupFailed(new IabResult(IabResult.IABHELPER_REMOTE_EXCEPTION,
                        "RemoteException while setting up in-app billing."));
                e.printStackTrace();
            }
        }
    };

    Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.android.vending");
    List<ResolveInfo> intentServices = SoomlaApp.getAppContext().getPackageManager()
            .queryIntentServices(serviceIntent, 0);
    if (intentServices != null && !intentServices.isEmpty()) {
        // service available to handle that Intent
        SoomlaApp.getAppContext().bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
    } else {
        // no service available to handle that Intent
        setupFailed(new IabResult(IabResult.BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                "Billing service unavailable on device."));
    }
}

From source file:org.jitsi.service.osgi.OSGiActivity.java

/**
 * Called when the activity is starting. Initializes the corresponding
 * call interface.//from   w  w  w .  j a  va  2s  .c  o m
 *
 * @param savedInstanceState If the activity is being re-initialized after
 * previously being shut down then this Bundle contains the data it most
 * recently supplied in onSaveInstanceState(Bundle).
 * Note: Otherwise it is null.
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {
    // Hooks the exception handler to the UI thread
    ExceptionHandler.checkAndAttachExceptionHandler();

    if (AndroidUtils.hasAPI(11)) {
        ActionBar actionBar = getActionBar();
        if (actionBar != null) {

            // Disable up arrow on home activity
            Class<?> homeActivity = JitsiApplication.getHomeScreenActivityClass();
            if (this.getClass().equals(homeActivity)) {
                actionBar.setDisplayHomeAsUpEnabled(false);

                if (AndroidUtils.hasAPI(14)) {
                    actionBar.setHomeButtonEnabled(false);
                }
            }

            ActionBarUtil.setTitle(this, getTitle());
        }
    }

    super.onCreate(savedInstanceState);

    ServiceConnection serviceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (this == OSGiActivity.this.serviceConnection)
                setService((BundleContextHolder) service);
        }

        public void onServiceDisconnected(ComponentName name) {
            if (this == OSGiActivity.this.serviceConnection)
                setService(null);
        }
    };

    this.serviceConnection = serviceConnection;

    boolean bindService = false;

    try {
        bindService = bindService(new Intent(this, OSGiService.class), serviceConnection, BIND_AUTO_CREATE);
    } finally {
        if (!bindService)
            this.serviceConnection = null;
    }

    // Registers exit action listener
    this.registerReceiver(exitListener, new IntentFilter(JitsiApplication.ACTION_EXIT));
}

From source file:com.soomla.store.billing.tapclash.TapClashIabHelper.java

/**
 * See parent/*from   w ww . j a  va 2 s . c o  m*/
 */
protected void startSetupInner() {
    mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            SoomlaUtils.LogDebug(TAG, "Billing service disconnected.");
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            SoomlaUtils.LogDebug(TAG, "Billing service connected.");
            mService = IInAppBillingService.Stub.asInterface(service);
            String packageName = SoomlaApp.getAppContext().getPackageName();
            try {
                SoomlaUtils.LogDebug(TAG, "Checking for in-app billing 3 support.");

                // check for in-app billing v3 support
                int response = mService.isBillingSupported(3, packageName, ITEM_TYPE_INAPP);
                if (response != IabResult.BILLING_RESPONSE_RESULT_OK) {
                    setupFailed(new IabResult(response, "Error checking for billing v3 support."));
                    return;
                }
                SoomlaUtils.LogDebug(TAG, "In-app billing version 3 supported for " + packageName);

                setupSuccess();
            } catch (RemoteException e) {
                setupFailed(new IabResult(IabResult.IABHELPER_REMOTE_EXCEPTION,
                        "RemoteException while setting up in-app billing."));
                e.printStackTrace();
            }
        }
    };

    Intent serviceIntent = new Intent("com.gametree.vending.billing.InAppBillingService.BIND");
    serviceIntent.setPackage("com.gametree.estore");
    List<ResolveInfo> intentServices = SoomlaApp.getAppContext().getPackageManager()
            .queryIntentServices(serviceIntent, 0);
    if (intentServices != null && !intentServices.isEmpty()) {
        // service available to handle that Intent
        SoomlaApp.getAppContext().bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
    } else {
        // no service available to handle that Intent
        setupFailed(new IabResult(IabResult.BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
                "Billing service unavailable on device."));
    }
}

From source file:com.gamethrive.TrackGooglePurchase.java

void trackIAP() {
    if (mServiceConn == null) {
        mServiceConn = new ServiceConnection() {
            @Override//from  w ww . j  a va2  s.  c  o  m
            public void onServiceDisconnected(ComponentName name) {
                iapEnabled = -99;
                mIInAppBillingService = null;
            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                try {
                    Class<?> stubClass = Class.forName("com.android.vending.billing.IInAppBillingService$Stub");
                    Method asInterfaceMethod = stubClass.getMethod("asInterface", android.os.IBinder.class);
                    mIInAppBillingService = asInterfaceMethod.invoke(null, service);

                    QueryBoughtItems();
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        };

        Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
        serviceIntent.setPackage("com.android.vending");
        appContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
    } else if (mIInAppBillingService != null)
        QueryBoughtItems();
}

From source file:com.omniwearhaptics.api.OmniWearHelper.java

public OmniWearHelper(final Context context, OnOmniWearEventListener eventListener,
        OnOmniWearLogListener logListener) {

    mParent = context;/* w ww.  j  a v  a 2s.co  m*/
    mOnOmniWearEventListener = eventListener;
    mOnOmniWearLogListener = logListener;
    Intent intent = new Intent();
    intent.setClassName("com.omniwearhaptics.omniwearbtbridge",
            "com.omniwearhaptics.omniwearbtbridge.OmniWearService");
    permissionsCheck();

    // Connect to the OmniWear service.
    mServiceConnection = new ServiceConnection() {

        // When the service is bound, try to connect to the device.
        public void onServiceConnected(ComponentName name, IBinder service) {

            // Create the interface.
            mOmniWearInterface = IOmniWear.Stub.asInterface(service);

            // Register the callback.
            if (mCallback != null) {
                try {
                    if (mOmniWearInterface != null) {
                        mOmniWearInterface.registerCallback(mCallback);
                        mCallback.onOmniWearEvent(EVENT_SERVICE_BOUND);
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            } else {
                Log.w(TAG, "mCallback is null in searchForOmniWearDevice");
            }
        }

        public void onServiceDisconnected(ComponentName name) {
            try {
                mOmniWearInterface.unregisterCallback();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    };
    context.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}

From source file:com.nus.cs4222.isbtracker.MainActivity.java

@Override
public void onStart() {
    super.onStart();

    mConnection = new ServiceConnection() {
        @Override/*from  w  w  w . j a va2 s .  c o m*/
        public void onServiceConnected(ComponentName name, IBinder service) {
            ScannerService.ScannerBinder binder = (ScannerService.ScannerBinder) service;
            mService = binder.getService();

            // Set state machine listener (before binding to service)
            mService.setStateMachineListener(mStateMachineListener);

            mIsBound = true;
            Log.d(LOGTAG, "Service connected");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
            mIsBound = false;
            Log.d(LOGTAG, "Service disconnected");
        }
    };

    // Start and bind to service
    Intent intent = new Intent(this, ScannerService.class);
    startService(intent);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

From source file:com.nick.scalpel.core.AutoBindWirer.java

@Override
public void wire(final Context context, final Object object, final Field field) {
    ReflectionUtils.makeAccessible(field);
    Object fieldObject = ReflectionUtils.getField(field, object);
    if (fieldObject != null)
        return;//from   w  w  w .ja v  a2  s .  c o m

    // FIXME: 21/03/16 Ensure it is an AIDL service.
    boolean isIInterface = field.getType().isInterface();
    Preconditions.checkState(isIInterface,
            "Field:" + field.getName() + " is not an AIDL interface, is:" + field.getType());

    AutoBind autoBind = field.getAnnotation(AutoBind.class);
    String action = autoBind.action();
    String pkg = autoBind.pkg();
    int flags = autoBind.flags();
    String callback = autoBind.callback();
    boolean startService = autoBind.startService();
    boolean isExplicit = !TextUtils.isEmpty(action) && !TextUtils.isEmpty(pkg);
    Preconditions.checkState(isExplicit, "Action and PackageName should be specified");

    boolean autoUnbind = autoBind.autoUnbind();
    boolean isActivity = object instanceof Activity;
    Preconditions.checkState(!autoUnbind || isActivity, "Auto unbind only work for activities.");

    AutoBind.Callback callbackInstance = null;
    if (!TextUtils.isEmpty(callback)) {
        Object callbackObject = null;
        switch (callback) {
        case "this":
            callbackObject = object;
            break;
        default:
            Field callbackField = ReflectionUtils.findField(object, callback);
            if (callbackField != null) {
                ReflectionUtils.makeAccessible(callbackField);
                callbackObject = ReflectionUtils.getField(callbackField, object);
            }
        }
        boolean isCallback = callbackObject instanceof AutoBind.Callback;
        Preconditions.checkState(isCallback, "Field:" + callback + " is not an instance of Callback.");
        callbackInstance = (AutoBind.Callback) callbackObject;
    }

    final Intent intent = new Intent(action);
    intent.setPackage(pkg);

    if (startService)
        context.startService(intent);

    final AutoBind.Callback finalCallbackInstance = callbackInstance;
    final ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Class serviceClass = field.getType();
            String stubClassName = serviceClass.getName() + "$Stub";
            try {
                Class stubClass = Class.forName(stubClassName);
                Method asInterface = ReflectionUtils.findMethod(stubClass, "asInterface", IBinder.class);
                Object result = ReflectionUtils.invokeMethod(asInterface, null, service);
                ReflectionUtils.setField(field, object, result);
                // Callback result.
                if (finalCallbackInstance != null)
                    finalCallbackInstance.onServiceBound(name, this, intent);
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException(e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            if (finalCallbackInstance != null)
                finalCallbackInstance.onServiceDisconnected(name);
        }
    };
    //noinspection ResourceType
    context.bindService(intent, connection, flags);

    if (autoUnbind) {
        final String fieldName = field.getName();
        boolean registered = mLifeCycleManager
                .registerActivityLifecycleCallbacks(new LifeCycleCallbackAdapter() {
                    @Override
                    public void onActivityDestroyed(Activity activity) {
                        super.onActivityDestroyed(activity);
                        if (activity == object) {
                            logV("unBind service for: " + fieldName);
                            context.unbindService(connection);
                            mLifeCycleManager.unRegisterActivityLifecycleCallbacks(this);
                        }
                    }
                });
        if (!registered) {
            logE("Failed to register life cycle callback!");
        }
    }
}

From source file:com.jogden.spunkycharts.MainApplication.java

public static void connectToDataService() {
    thisApp.bindService(new Intent(thisApp, DataContentService.class),
            ourDataServiceConnection = new ServiceConnection() {
                public void onServiceConnected(ComponentName name, IBinder binder) {
                    ourDataService = ((DataContentService.OurBinder) binder).getService();
                    DataContentService.connect();
                }/*from w  w w  .j  a v a  2  s.c om*/

                public void onServiceDisconnected(ComponentName name) {
                    ourDataService = null;
                }
            }, Context.BIND_AUTO_CREATE);
    thisApp.startService(new Intent(thisApp, DataContentService.class));
}