Search in sources :

Example 81 with ServiceConnection

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

the class AppWidgetServiceImpl method notifyAppWidgetViewDataChangedInstanceLocked.

void notifyAppWidgetViewDataChangedInstanceLocked(AppWidgetId id, int viewId) {
    // drop unbound appWidgetIds (shouldn't be possible under normal circumstances)
    if (id != null && id.provider != null && !id.provider.zombie && !id.host.zombie) {
        // is anyone listening?
        if (id.host.callbacks != null) {
            try {
                // the lock is held, but this is a oneway call
                id.host.callbacks.viewDataChanged(id.appWidgetId, viewId, mUserId);
            } catch (RemoteException e) {
                // It failed; remove the callback. No need to prune because
                // we know that this host is still referenced by this instance.
                id.host.callbacks = null;
            }
        }
        // RemoteViewsFactory.onDataSetChanged() directly
        if (id.host.callbacks == null) {
            Set<FilterComparison> keys = mRemoteViewsServicesAppWidgets.keySet();
            for (FilterComparison key : keys) {
                if (mRemoteViewsServicesAppWidgets.get(key).contains(id.appWidgetId)) {
                    Intent intent = key.getIntent();
                    final ServiceConnection conn = new ServiceConnection() {

                        @Override
                        public void onServiceConnected(ComponentName name, IBinder service) {
                            IRemoteViewsFactory cb = IRemoteViewsFactory.Stub.asInterface(service);
                            try {
                                cb.onDataSetChangedAsync();
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            } catch (RuntimeException e) {
                                e.printStackTrace();
                            }
                            mContext.unbindService(this);
                        }

                        @Override
                        public void onServiceDisconnected(android.content.ComponentName name) {
                        // Do nothing
                        }
                    };
                    int userId = UserHandle.getUserId(id.provider.uid);
                    // Bind to the service and call onDataSetChanged()
                    final long token = Binder.clearCallingIdentity();
                    try {
                        mContext.bindServiceAsUser(intent, conn, Context.BIND_AUTO_CREATE, new UserHandle(userId));
                    } finally {
                        Binder.restoreCallingIdentity(token);
                    }
                }
            }
        }
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) Point(android.graphics.Point) IBinder(android.os.IBinder) UserHandle(android.os.UserHandle) IRemoteViewsFactory(com.android.internal.widget.IRemoteViewsFactory) FilterComparison(android.content.Intent.FilterComparison) ComponentName(android.content.ComponentName) RemoteException(android.os.RemoteException)

Example 82 with ServiceConnection

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

the class NotificationManagerService method registerListenerService.

/**
     * Version of registerListener that takes the name of a
     * {@link android.service.notification.NotificationListenerService} to bind to.
     *
     * This is the mechanism by which third parties may subscribe to notifications.
     */
private void registerListenerService(final ComponentName name, final int userid) {
    checkCallerIsSystem();
    if (DBG)
        Slog.v(TAG, "registerListenerService: " + name + " u=" + userid);
    synchronized (mNotificationList) {
        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 = mListeners.size();
        for (int i = N - 1; i >= 0; i--) {
            final NotificationListenerInfo info = mListeners.get(i);
            if (name.equals(info.component) && info.userid == userid) {
                // cut old connections
                if (DBG)
                    Slog.v(TAG, "    disconnecting old listener: " + info.listener);
                mListeners.remove(i);
                if (info.connection != null) {
                    mContext.unbindService(info.connection);
                }
            }
        }
        Intent intent = new Intent(NotificationListenerService.SERVICE_INTERFACE);
        intent.setComponent(name);
        intent.putExtra(Intent.EXTRA_CLIENT_LABEL, com.android.internal.R.string.notification_listener_binding_label);
        intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(mContext, 0, new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS), 0));
        try {
            if (DBG)
                Slog.v(TAG, "binding: " + intent);
            if (!mContext.bindServiceAsUser(intent, new ServiceConnection() {

                INotificationListener mListener;

                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    synchronized (mNotificationList) {
                        mServicesBinding.remove(servicesBindingTag);
                        try {
                            mListener = INotificationListener.Stub.asInterface(service);
                            NotificationListenerInfo info = new NotificationListenerInfo(mListener, name, userid, this);
                            service.linkToDeath(info, 0);
                            mListeners.add(info);
                        } catch (RemoteException e) {
                        // already dead
                        }
                    }
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Slog.v(TAG, "notification listener connection lost: " + name);
                }
            }, Context.BIND_AUTO_CREATE, new UserHandle(userid))) {
                mServicesBinding.remove(servicesBindingTag);
                Slog.w(TAG, "Unable to bind listener service: " + intent);
                return;
            }
        } catch (SecurityException ex) {
            Slog.e(TAG, "Unable to bind listener service: " + intent, ex);
            return;
        }
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) INotificationListener(android.service.notification.INotificationListener) UserHandle(android.os.UserHandle) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) ComponentName(android.content.ComponentName) RemoteException(android.os.RemoteException)

Example 83 with ServiceConnection

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

the class ActivityTestMain method onStop.

@Override
protected void onStop() {
    super.onStop();
    for (ServiceConnection conn : mConnections) {
        unbindService(conn);
    }
    mConnections.clear();
}
Also used : ServiceConnection(android.content.ServiceConnection)

Example 84 with ServiceConnection

use of android.content.ServiceConnection in project AndroidTraining by mixi-inc.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    disableActionViews();
    //アプリ内課金サービスに接続する
    mBillingServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBillingService = IInAppBillingService.Stub.asInterface(service);
            enableActionViews();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBillingService = null;
        }
    };
    bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mBillingServiceConnection, Context.BIND_AUTO_CREATE);
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) ComponentName(android.content.ComponentName) Intent(android.content.Intent)

Example 85 with ServiceConnection

use of android.content.ServiceConnection in project platform_frameworks_base by android.

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