Search in sources :

Example 46 with ServiceConnection

use of android.content.ServiceConnection in project android_frameworks_base by DirtyUnicorns.

the class ManagedServices method registerServiceLocked.

private void registerServiceLocked(final ComponentName name, final int userid, final boolean isSystem) {
    if (DEBUG)
        Slog.v(TAG, "registerService: " + name + " u=" + userid);
    final String servicesBindingTag = name.toString() + "/" + userid;
    if (mServicesBinding.contains(servicesBindingTag)) {
        // stop registering this thing already! we're working on it
        return;
    }
    mServicesBinding.add(servicesBindingTag);
    final int N = mServices.size();
    for (int i = N - 1; i >= 0; i--) {
        final ManagedServiceInfo info = mServices.get(i);
        if (name.equals(info.component) && info.userid == userid) {
            // cut old connections
            if (DEBUG)
                Slog.v(TAG, "    disconnecting old " + getCaption() + ": " + info.service);
            removeServiceLocked(i);
            if (info.connection != null) {
                mContext.unbindService(info.connection);
            }
        }
    }
    Intent intent = new Intent(mConfig.serviceInterface);
    intent.setComponent(name);
    intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel);
    final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mConfig.settingsAction), 0);
    intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
    ApplicationInfo appInfo = null;
    try {
        appInfo = mContext.getPackageManager().getApplicationInfo(name.getPackageName(), 0);
    } catch (NameNotFoundException e) {
    // Ignore if the package doesn't exist we won't be able to bind to the service.
    }
    final int targetSdkVersion = appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE;
    try {
        if (DEBUG)
            Slog.v(TAG, "binding: " + intent);
        ServiceConnection serviceConnection = new ServiceConnection() {

            IInterface mService;

            @Override
            public void onServiceConnected(ComponentName name, IBinder binder) {
                boolean added = false;
                ManagedServiceInfo info = null;
                synchronized (mMutex) {
                    mServicesBinding.remove(servicesBindingTag);
                    try {
                        mService = asInterface(binder);
                        info = newServiceInfo(mService, name, userid, isSystem, this, targetSdkVersion);
                        binder.linkToDeath(info, 0);
                        added = mServices.add(info);
                    } catch (RemoteException e) {
                    // already dead
                    }
                }
                if (added) {
                    onServiceAdded(info);
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Slog.v(TAG, getCaption() + " connection lost: " + name);
            }
        };
        if (!mContext.bindServiceAsUser(intent, serviceConnection, BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE | BIND_ALLOW_WHITELIST_MANAGEMENT, new UserHandle(userid))) {
            mServicesBinding.remove(servicesBindingTag);
            Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent);
            return;
        }
    } catch (SecurityException ex) {
        Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex);
        return;
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ApplicationInfo(android.content.pm.ApplicationInfo) IInterface(android.os.IInterface) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IBinder(android.os.IBinder) UserHandle(android.os.UserHandle) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) RemoteException(android.os.RemoteException)

Example 47 with ServiceConnection

use of android.content.ServiceConnection in project android_frameworks_base by DirtyUnicorns.

the class PacManager method bind.

private void bind() {
    if (mContext == null) {
        Log.e(TAG, "No context for binding");
        return;
    }
    Intent intent = new Intent();
    intent.setClassName(PAC_PACKAGE, PAC_SERVICE);
    if ((mProxyConnection != null) && (mConnection != null)) {
        // Already bound no need to bind again, just download the new file.
        mNetThreadHandler.post(mPacDownloader);
        return;
    }
    mConnection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName component) {
            synchronized (mProxyLock) {
                mProxyService = null;
            }
        }

        @Override
        public void onServiceConnected(ComponentName component, IBinder binder) {
            synchronized (mProxyLock) {
                try {
                    Log.d(TAG, "Adding service " + PAC_SERVICE_NAME + " " + binder.getInterfaceDescriptor());
                } catch (RemoteException e1) {
                    Log.e(TAG, "Remote Exception", e1);
                }
                ServiceManager.addService(PAC_SERVICE_NAME, binder);
                mProxyService = IProxyService.Stub.asInterface(binder);
                if (mProxyService == null) {
                    Log.e(TAG, "No proxy service");
                } else {
                    try {
                        mProxyService.startPacSystem();
                    } catch (RemoteException e) {
                        Log.e(TAG, "Unable to reach ProxyService - PAC will not be started", e);
                    }
                    mNetThreadHandler.post(mPacDownloader);
                }
            }
        }
    };
    mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND | Context.BIND_NOT_VISIBLE);
    intent = new Intent();
    intent.setClassName(PROXY_PACKAGE, PROXY_SERVICE);
    mProxyConnection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName component) {
        }

        @Override
        public void onServiceConnected(ComponentName component, IBinder binder) {
            IProxyCallback callbackService = IProxyCallback.Stub.asInterface(binder);
            if (callbackService != null) {
                try {
                    callbackService.getProxyPort(new IProxyPortListener.Stub() {

                        @Override
                        public void setProxyPort(int port) throws RemoteException {
                            if (mLastPort != -1) {
                                // Always need to send if port changed
                                mHasSentBroadcast = false;
                            }
                            mLastPort = port;
                            if (port != -1) {
                                Log.d(TAG, "Local proxy is bound on " + port);
                                sendProxyIfNeeded();
                            } else {
                                Log.e(TAG, "Received invalid port from Local Proxy," + " PAC will not be operational");
                            }
                        }
                    });
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    mContext.bindService(intent, mProxyConnection, Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND | Context.BIND_NOT_VISIBLE);
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) ComponentName(android.content.ComponentName) IProxyCallback(com.android.net.IProxyCallback) RemoteException(android.os.RemoteException)

Example 48 with ServiceConnection

use of android.content.ServiceConnection in project android_frameworks_base by ParanoidAndroid.

the class LocalReceiver method onReceive.

public void onReceive(Context context, Intent intent) {
    String resultString = LaunchpadActivity.RECEIVER_LOCAL;
    if (BroadcastTest.BROADCAST_FAIL_REGISTER.equals(intent.getAction())) {
        resultString = "Successfully registered, but expected it to fail";
        try {
            context.registerReceiver(this, new IntentFilter("foo.bar"));
            context.unregisterReceiver(this);
        } catch (ReceiverCallNotAllowedException e) {
            //resultString = "This is the correct behavior but not yet implemented";
            resultString = LaunchpadActivity.RECEIVER_LOCAL;
        }
    } else if (BroadcastTest.BROADCAST_FAIL_BIND.equals(intent.getAction())) {
        resultString = "Successfully bound to service, but expected it to fail";
        try {
            ServiceConnection sc = new ServiceConnection() {

                public void onServiceConnected(ComponentName name, IBinder service) {
                }

                public void onServiceDisconnected(ComponentName name) {
                }
            };
            context.bindService(new Intent(context, LocalService.class), sc, 0);
            context.unbindService(sc);
        } catch (ReceiverCallNotAllowedException e) {
            //resultString = "This is the correct behavior but not yet implemented";
            resultString = LaunchpadActivity.RECEIVER_LOCAL;
        }
    } else if (LaunchpadActivity.BROADCAST_REPEAT.equals(intent.getAction())) {
        Intent newIntent = new Intent(intent);
        newIntent.setAction(LaunchpadActivity.BROADCAST_LOCAL);
        context.sendOrderedBroadcast(newIntent, null);
    }
    try {
        IBinder caller = intent.getIBinderExtra("caller");
        Parcel data = Parcel.obtain();
        data.writeInterfaceToken(LaunchpadActivity.LAUNCH);
        data.writeString(resultString);
        caller.transact(LaunchpadActivity.GOT_RECEIVE_TRANSACTION, data, null, 0);
        data.recycle();
    } catch (RemoteException ex) {
    }
}
Also used : ReceiverCallNotAllowedException(android.content.ReceiverCallNotAllowedException) IntentFilter(android.content.IntentFilter) ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) Parcel(android.os.Parcel) ComponentName(android.content.ComponentName) Intent(android.content.Intent) RemoteException(android.os.RemoteException)

Example 49 with ServiceConnection

use of android.content.ServiceConnection in project android_frameworks_base by AOSPA.

the class LocalReceiver method onReceive.

public void onReceive(Context context, Intent intent) {
    String resultString = LaunchpadActivity.RECEIVER_LOCAL;
    if (BroadcastTest.BROADCAST_FAIL_REGISTER.equals(intent.getAction())) {
        resultString = "Successfully registered, but expected it to fail";
        try {
            context.registerReceiver(this, new IntentFilter("foo.bar"));
            context.unregisterReceiver(this);
        } catch (ReceiverCallNotAllowedException e) {
            //resultString = "This is the correct behavior but not yet implemented";
            resultString = LaunchpadActivity.RECEIVER_LOCAL;
        }
    } else if (BroadcastTest.BROADCAST_FAIL_BIND.equals(intent.getAction())) {
        resultString = "Successfully bound to service, but expected it to fail";
        try {
            ServiceConnection sc = new ServiceConnection() {

                public void onServiceConnected(ComponentName name, IBinder service) {
                }

                public void onServiceDisconnected(ComponentName name) {
                }
            };
            context.bindService(new Intent(context, LocalService.class), sc, 0);
            context.unbindService(sc);
        } catch (ReceiverCallNotAllowedException e) {
            //resultString = "This is the correct behavior but not yet implemented";
            resultString = LaunchpadActivity.RECEIVER_LOCAL;
        }
    } else if (LaunchpadActivity.BROADCAST_REPEAT.equals(intent.getAction())) {
        Intent newIntent = new Intent(intent);
        newIntent.setAction(LaunchpadActivity.BROADCAST_LOCAL);
        context.sendOrderedBroadcast(newIntent, null);
    }
    try {
        IBinder caller = intent.getIBinderExtra("caller");
        Parcel data = Parcel.obtain();
        data.writeInterfaceToken(LaunchpadActivity.LAUNCH);
        data.writeString(resultString);
        caller.transact(LaunchpadActivity.GOT_RECEIVE_TRANSACTION, data, null, 0);
        data.recycle();
    } catch (RemoteException ex) {
    }
}
Also used : ReceiverCallNotAllowedException(android.content.ReceiverCallNotAllowedException) IntentFilter(android.content.IntentFilter) ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) Parcel(android.os.Parcel) ComponentName(android.content.ComponentName) Intent(android.content.Intent) RemoteException(android.os.RemoteException)

Example 50 with ServiceConnection

use of android.content.ServiceConnection in project android_frameworks_base by AOSPA.

the class MediaBrowser method connect.

/**
     * Connects to the media browse service.
     * <p>
     * The connection callback specified in the constructor will be invoked
     * when the connection completes or fails.
     * </p>
     */
public void connect() {
    if (mState != CONNECT_STATE_DISCONNECTED) {
        throw new IllegalStateException("connect() called while not disconnected (state=" + getStateLabel(mState) + ")");
    }
    // TODO: remove this extra check.
    if (DBG) {
        if (mServiceConnection != null) {
            throw new RuntimeException("mServiceConnection should be null. Instead it is " + mServiceConnection);
        }
    }
    if (mServiceBinder != null) {
        throw new RuntimeException("mServiceBinder should be null. Instead it is " + mServiceBinder);
    }
    if (mServiceCallbacks != null) {
        throw new RuntimeException("mServiceCallbacks should be null. Instead it is " + mServiceCallbacks);
    }
    mState = CONNECT_STATE_CONNECTING;
    final Intent intent = new Intent(MediaBrowserService.SERVICE_INTERFACE);
    intent.setComponent(mServiceComponent);
    final ServiceConnection thisConnection = mServiceConnection = new MediaServiceConnection();
    boolean bound = false;
    try {
        bound = mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
    } catch (Exception ex) {
        Log.e(TAG, "Failed binding to service " + mServiceComponent);
    }
    if (!bound) {
        // Tell them that it didn't work. We are already on the main thread,
        // but we don't want to do callbacks inside of connect(). So post it,
        // and then check that we are on the same ServiceConnection. We know
        // we won't also get an onServiceConnected or onServiceDisconnected,
        // so we won't be doing double callbacks.
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                // Ensure that nobody else came in or tried to connect again.
                if (thisConnection == mServiceConnection) {
                    forceCloseConnection();
                    mCallback.onConnectionFailed();
                }
            }
        });
    }
    if (DBG) {
        Log.d(TAG, "connect...");
        dump();
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) Intent(android.content.Intent) RemoteException(android.os.RemoteException)

Aggregations

ServiceConnection (android.content.ServiceConnection)122 ComponentName (android.content.ComponentName)95 Intent (android.content.Intent)95 IBinder (android.os.IBinder)94 RemoteException (android.os.RemoteException)75 PendingIntent (android.app.PendingIntent)46 UserHandle (android.os.UserHandle)23 Handler (android.os.Handler)20 Message (android.os.Message)19 Messenger (android.os.Messenger)19 IRemoteViewsFactory (com.android.internal.widget.IRemoteViewsFactory)12 IInterface (android.os.IInterface)9 ResolveInfo (android.content.pm.ResolveInfo)8 IntentFilter (android.content.IntentFilter)7 Point (android.graphics.Point)7 AndroidRuntimeException (android.util.AndroidRuntimeException)7 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)7 Test (org.junit.Test)7 FilterComparison (android.content.Intent.FilterComparison)6 ReceiverCallNotAllowedException (android.content.ReceiverCallNotAllowedException)6