Search in sources :

Example 1 with INotificationListener

use of android.service.notification.INotificationListener 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)

Aggregations

PendingIntent (android.app.PendingIntent)1 ComponentName (android.content.ComponentName)1 Intent (android.content.Intent)1 ServiceConnection (android.content.ServiceConnection)1 IBinder (android.os.IBinder)1 RemoteException (android.os.RemoteException)1 UserHandle (android.os.UserHandle)1 INotificationListener (android.service.notification.INotificationListener)1