ManagedServices.java revision e77bb36d48b6b8b5c3bb6a1195aca469bb237919
1/**
2 * Copyright (c) 2014, The Android Open Source Project
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 com.android.server.notification;
18
19import android.app.ActivityManager;
20import android.app.PendingIntent;
21import android.content.ComponentName;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.ServiceConnection;
26import android.content.pm.ApplicationInfo;
27import android.content.pm.PackageManager;
28import android.content.pm.PackageManager.NameNotFoundException;
29import android.content.pm.ResolveInfo;
30import android.content.pm.ServiceInfo;
31import android.content.pm.UserInfo;
32import android.database.ContentObserver;
33import android.net.Uri;
34import android.os.Build;
35import android.os.Handler;
36import android.os.IBinder;
37import android.os.IInterface;
38import android.os.RemoteException;
39import android.os.UserHandle;
40import android.os.UserManager;
41import android.provider.Settings;
42import android.text.TextUtils;
43import android.util.ArraySet;
44import android.util.Slog;
45import android.util.SparseArray;
46
47import java.io.PrintWriter;
48import java.util.ArrayList;
49import java.util.Arrays;
50import java.util.List;
51import java.util.Set;
52
53/**
54 * Manages the lifecycle of application-provided services bound by system server.
55 *
56 * Services managed by this helper must have:
57 *  - An associated system settings value with a list of enabled component names.
58 *  - A well-known action for services to use in their intent-filter.
59 *  - A system permission for services to require in order to ensure system has exclusive binding.
60 *  - A settings page for user configuration of enabled services, and associated intent action.
61 *  - A remote interface definition (aidl) provided by the service used for communication.
62 */
63abstract public class ManagedServices {
64    protected final String TAG = getClass().getSimpleName();
65    protected static final boolean DEBUG = true;
66
67    private static final String ENABLED_SERVICES_SEPARATOR = ":";
68
69    private final Context mContext;
70    protected final Object mMutex;
71    private final UserProfiles mUserProfiles;
72    private final SettingsObserver mSettingsObserver;
73    private final Config mConfig;
74
75    // contains connections to all connected services, including app services
76    // and system services
77    protected final ArrayList<ManagedServiceInfo> mServices = new ArrayList<ManagedServiceInfo>();
78    // things that will be put into mServices as soon as they're ready
79    private final ArrayList<String> mServicesBinding = new ArrayList<String>();
80    // lists the component names of all enabled (and therefore connected)
81    // app services for current profiles.
82    private ArraySet<ComponentName> mEnabledServicesForCurrentProfiles
83            = new ArraySet<ComponentName>();
84    // Just the packages from mEnabledServicesForCurrentProfiles
85    private ArraySet<String> mEnabledServicesPackageNames = new ArraySet<String>();
86
87    public ManagedServices(Context context, Handler handler, Object mutex,
88            UserProfiles userProfiles) {
89        mContext = context;
90        mMutex = mutex;
91        mUserProfiles = userProfiles;
92        mConfig = getConfig();
93        mSettingsObserver = new SettingsObserver(handler);
94    }
95
96    abstract protected Config getConfig();
97
98    private String getCaption() {
99        return mConfig.caption;
100    }
101
102    abstract protected IInterface asInterface(IBinder binder);
103
104    abstract protected void onServiceAdded(IInterface service);
105
106    protected void onServiceRemovedLocked(ManagedServiceInfo removed) { }
107
108    private ManagedServiceInfo newServiceInfo(IInterface service,
109            ComponentName component, int userid, boolean isSystem, ServiceConnection connection,
110            int targetSdkVersion) {
111        return new ManagedServiceInfo(service, component, userid, isSystem, connection,
112                targetSdkVersion);
113    }
114
115    public void onBootPhaseAppsCanStart() {
116        mSettingsObserver.observe();
117    }
118
119    public void dump(PrintWriter pw) {
120        pw.println("    All " + getCaption() + "s (" + mEnabledServicesForCurrentProfiles.size()
121                + ") enabled for current profiles:");
122        for (ComponentName cmpt : mEnabledServicesForCurrentProfiles) {
123            pw.println("      " + cmpt);
124        }
125
126        pw.println("    Live " + getCaption() + "s (" + mServices.size() + "):");
127        for (ManagedServiceInfo info : mServices) {
128            pw.println("      " + info.component
129                    + " (user " + info.userid + "): " + info.service
130                    + (info.isSystem?" SYSTEM":""));
131        }
132    }
133
134    public void onPackagesChanged(boolean queryReplace, String[] pkgList) {
135        if (DEBUG) Slog.d(TAG, "onPackagesChanged queryReplace=" + queryReplace
136                + " pkgList=" + (pkgList == null ? null : Arrays.asList(pkgList))
137                + " mEnabledServicesPackageNames=" + mEnabledServicesPackageNames);
138        boolean anyServicesInvolved = false;
139        if (pkgList != null && (pkgList.length > 0)) {
140            for (String pkgName : pkgList) {
141                if (mEnabledServicesPackageNames.contains(pkgName)) {
142                    anyServicesInvolved = true;
143                }
144            }
145        }
146
147        if (anyServicesInvolved) {
148            // if we're not replacing a package, clean up orphaned bits
149            if (!queryReplace) {
150                disableNonexistentServices();
151            }
152            // make sure we're still bound to any of our services who may have just upgraded
153            rebindServices();
154        }
155    }
156
157    public ManagedServiceInfo checkServiceTokenLocked(IInterface service) {
158        checkNotNull(service);
159        final IBinder token = service.asBinder();
160        final int N = mServices.size();
161        for (int i=0; i<N; i++) {
162            final ManagedServiceInfo info = mServices.get(i);
163            if (info.service.asBinder() == token) return info;
164        }
165        throw new SecurityException("Disallowed call from unknown " + getCaption() + ": "
166                + service);
167    }
168
169    public void unregisterService(IInterface service, int userid) {
170        checkNotNull(service);
171        // no need to check permissions; if your service binder is in the list,
172        // that's proof that you had permission to add it in the first place
173        unregisterServiceImpl(service, userid);
174    }
175
176    public void registerService(IInterface service, ComponentName component, int userid) {
177        checkNotNull(service);
178        registerServiceImpl(service, component, userid);
179    }
180
181    /**
182     * Remove access for any services that no longer exist.
183     */
184    private void disableNonexistentServices() {
185        int[] userIds = mUserProfiles.getCurrentProfileIds();
186        final int N = userIds.length;
187        for (int i = 0 ; i < N; ++i) {
188            disableNonexistentServices(userIds[i]);
189        }
190    }
191
192    private void disableNonexistentServices(int userId) {
193        String flatIn = Settings.Secure.getStringForUser(
194                mContext.getContentResolver(),
195                mConfig.secureSettingName,
196                userId);
197        if (!TextUtils.isEmpty(flatIn)) {
198            if (DEBUG) Slog.v(TAG, "flat before: " + flatIn);
199            PackageManager pm = mContext.getPackageManager();
200            List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
201                    new Intent(mConfig.serviceInterface),
202                    PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
203                    userId);
204            if (DEBUG) Slog.v(TAG, mConfig.serviceInterface + " services: " + installedServices);
205            Set<ComponentName> installed = new ArraySet<ComponentName>();
206            for (int i = 0, count = installedServices.size(); i < count; i++) {
207                ResolveInfo resolveInfo = installedServices.get(i);
208                ServiceInfo info = resolveInfo.serviceInfo;
209
210                if (!mConfig.bindPermission.equals(info.permission)) {
211                    Slog.w(TAG, "Skipping " + getCaption() + " service "
212                            + info.packageName + "/" + info.name
213                            + ": it does not require the permission "
214                            + mConfig.bindPermission);
215                    continue;
216                }
217                installed.add(new ComponentName(info.packageName, info.name));
218            }
219
220            String flatOut = "";
221            if (!installed.isEmpty()) {
222                String[] enabled = flatIn.split(ENABLED_SERVICES_SEPARATOR);
223                ArrayList<String> remaining = new ArrayList<String>(enabled.length);
224                for (int i = 0; i < enabled.length; i++) {
225                    ComponentName enabledComponent = ComponentName.unflattenFromString(enabled[i]);
226                    if (installed.contains(enabledComponent)) {
227                        remaining.add(enabled[i]);
228                    }
229                }
230                flatOut = TextUtils.join(ENABLED_SERVICES_SEPARATOR, remaining);
231            }
232            if (DEBUG) Slog.v(TAG, "flat after: " + flatOut);
233            if (!flatIn.equals(flatOut)) {
234                Settings.Secure.putStringForUser(mContext.getContentResolver(),
235                        mConfig.secureSettingName,
236                        flatOut, userId);
237            }
238        }
239    }
240
241    /**
242     * Called whenever packages change, the user switches, or the secure setting
243     * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
244     */
245    private void rebindServices() {
246        if (DEBUG) Slog.d(TAG, "rebindServices");
247        final int[] userIds = mUserProfiles.getCurrentProfileIds();
248        final int nUserIds = userIds.length;
249
250        final SparseArray<String> flat = new SparseArray<String>();
251
252        for (int i = 0; i < nUserIds; ++i) {
253            flat.put(userIds[i], Settings.Secure.getStringForUser(
254                    mContext.getContentResolver(),
255                    mConfig.secureSettingName,
256                    userIds[i]));
257        }
258
259        ManagedServiceInfo[] toRemove = new ManagedServiceInfo[mServices.size()];
260        final SparseArray<ArrayList<ComponentName>> toAdd
261                = new SparseArray<ArrayList<ComponentName>>();
262
263        synchronized (mMutex) {
264            // unbind and remove all existing services
265            toRemove = mServices.toArray(toRemove);
266
267            final ArraySet<ComponentName> newEnabled = new ArraySet<ComponentName>();
268            final ArraySet<String> newPackages = new ArraySet<String>();
269
270            for (int i = 0; i < nUserIds; ++i) {
271                final ArrayList<ComponentName> add = new ArrayList<ComponentName>();
272                toAdd.put(userIds[i], add);
273
274                // decode the list of components
275                String toDecode = flat.get(userIds[i]);
276                if (toDecode != null) {
277                    String[] components = toDecode.split(ENABLED_SERVICES_SEPARATOR);
278                    for (int j = 0; j < components.length; j++) {
279                        final ComponentName component
280                                = ComponentName.unflattenFromString(components[j]);
281                        if (component != null) {
282                            newEnabled.add(component);
283                            add.add(component);
284                            newPackages.add(component.getPackageName());
285                        }
286                    }
287
288                }
289            }
290            mEnabledServicesForCurrentProfiles = newEnabled;
291            mEnabledServicesPackageNames = newPackages;
292        }
293
294        for (ManagedServiceInfo info : toRemove) {
295            final ComponentName component = info.component;
296            final int oldUser = info.userid;
297            Slog.v(TAG, "disabling " + getCaption() + " for user "
298                    + oldUser + ": " + component);
299            unregisterService(component, info.userid);
300        }
301
302        for (int i = 0; i < nUserIds; ++i) {
303            final ArrayList<ComponentName> add = toAdd.get(userIds[i]);
304            final int N = add.size();
305            for (int j = 0; j < N; j++) {
306                final ComponentName component = add.get(j);
307                Slog.v(TAG, "enabling " + getCaption() + " for user " + userIds[i] + ": "
308                        + component);
309                registerService(component, userIds[i]);
310            }
311        }
312    }
313
314    /**
315     * Version of registerService that takes the name of a service component to bind to.
316     */
317    private void registerService(final ComponentName name, final int userid) {
318        if (DEBUG) Slog.v(TAG, "registerService: " + name + " u=" + userid);
319
320        synchronized (mMutex) {
321            final String servicesBindingTag = name.toString() + "/" + userid;
322            if (mServicesBinding.contains(servicesBindingTag)) {
323                // stop registering this thing already! we're working on it
324                return;
325            }
326            mServicesBinding.add(servicesBindingTag);
327
328            final int N = mServices.size();
329            for (int i=N-1; i>=0; i--) {
330                final ManagedServiceInfo info = mServices.get(i);
331                if (name.equals(info.component)
332                        && info.userid == userid) {
333                    // cut old connections
334                    if (DEBUG) Slog.v(TAG, "    disconnecting old " + getCaption() + ": "
335                            + info.service);
336                    removeServiceLocked(i);
337                    if (info.connection != null) {
338                        mContext.unbindService(info.connection);
339                    }
340                }
341            }
342
343            Intent intent = new Intent(mConfig.serviceInterface);
344            intent.setComponent(name);
345
346            intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel);
347
348            final PendingIntent pendingIntent = PendingIntent.getActivity(
349                    mContext, 0, new Intent(mConfig.settingsAction), 0);
350            intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
351
352            ApplicationInfo appInfo = null;
353            try {
354                appInfo = mContext.getPackageManager().getApplicationInfo(
355                        name.getPackageName(), 0);
356            } catch (NameNotFoundException e) {
357                // Ignore if the package doesn't exist we won't be able to bind to the service.
358            }
359            final int targetSdkVersion =
360                    appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE;
361
362            try {
363                if (DEBUG) Slog.v(TAG, "binding: " + intent);
364                if (!mContext.bindServiceAsUser(intent,
365                        new ServiceConnection() {
366                            IInterface mService;
367
368                            @Override
369                            public void onServiceConnected(ComponentName name, IBinder binder) {
370                                boolean added = false;
371                                synchronized (mMutex) {
372                                    mServicesBinding.remove(servicesBindingTag);
373                                    try {
374                                        mService = asInterface(binder);
375                                        ManagedServiceInfo info = newServiceInfo(mService, name,
376                                                userid, false /*isSystem*/, this, targetSdkVersion);
377                                        binder.linkToDeath(info, 0);
378                                        added = mServices.add(info);
379                                    } catch (RemoteException e) {
380                                        // already dead
381                                    }
382                                }
383                                if (added) {
384                                    onServiceAdded(mService);
385                                }
386                            }
387
388                            @Override
389                            public void onServiceDisconnected(ComponentName name) {
390                                Slog.v(TAG, getCaption() + " connection lost: " + name);
391                            }
392                        },
393                        Context.BIND_AUTO_CREATE,
394                        new UserHandle(userid)))
395                {
396                    mServicesBinding.remove(servicesBindingTag);
397                    Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent);
398                    return;
399                }
400            } catch (SecurityException ex) {
401                Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex);
402                return;
403            }
404        }
405    }
406
407    /**
408     * Remove a service for the given user by ComponentName
409     */
410    private void unregisterService(ComponentName name, int userid) {
411        synchronized (mMutex) {
412            final int N = mServices.size();
413            for (int i=N-1; i>=0; i--) {
414                final ManagedServiceInfo info = mServices.get(i);
415                if (name.equals(info.component)
416                        && info.userid == userid) {
417                    removeServiceLocked(i);
418                    if (info.connection != null) {
419                        try {
420                            mContext.unbindService(info.connection);
421                        } catch (IllegalArgumentException ex) {
422                            // something happened to the service: we think we have a connection
423                            // but it's bogus.
424                            Slog.e(TAG, getCaption() + " " + name + " could not be unbound: " + ex);
425                        }
426                    }
427                }
428            }
429        }
430    }
431
432    /**
433     * Removes a service from the list but does not unbind
434     *
435     * @return the removed service.
436     */
437    private ManagedServiceInfo removeServiceImpl(IInterface service, final int userid) {
438        if (DEBUG) Slog.d(TAG, "removeServiceImpl service=" + service + " u=" + userid);
439        ManagedServiceInfo serviceInfo = null;
440        synchronized (mMutex) {
441            final int N = mServices.size();
442            for (int i=N-1; i>=0; i--) {
443                final ManagedServiceInfo info = mServices.get(i);
444                if (info.service.asBinder() == service.asBinder()
445                        && info.userid == userid) {
446                    if (DEBUG) Slog.d(TAG, "Removing active service " + info.component);
447                    serviceInfo = removeServiceLocked(i);
448                }
449            }
450        }
451        return serviceInfo;
452    }
453
454    private ManagedServiceInfo removeServiceLocked(int i) {
455        final ManagedServiceInfo info = mServices.remove(i);
456        onServiceRemovedLocked(info);
457        return info;
458    }
459
460    private void checkNotNull(IInterface service) {
461        if (service == null) {
462            throw new IllegalArgumentException(getCaption() + " must not be null");
463        }
464    }
465
466    private void registerServiceImpl(final IInterface service,
467            final ComponentName component, final int userid) {
468        synchronized (mMutex) {
469            try {
470                ManagedServiceInfo info = newServiceInfo(service, component, userid,
471                        true /*isSystem*/, null, Build.VERSION_CODES.L);
472                service.asBinder().linkToDeath(info, 0);
473                mServices.add(info);
474            } catch (RemoteException e) {
475                // already dead
476            }
477        }
478    }
479
480    /**
481     * Removes a service from the list and unbinds.
482     */
483    private void unregisterServiceImpl(IInterface service, int userid) {
484        ManagedServiceInfo info = removeServiceImpl(service, userid);
485        if (info != null && info.connection != null) {
486            mContext.unbindService(info.connection);
487        }
488    }
489
490    private class SettingsObserver extends ContentObserver {
491        private final Uri mSecureSettingsUri = Settings.Secure.getUriFor(mConfig.secureSettingName);
492
493        private SettingsObserver(Handler handler) {
494            super(handler);
495        }
496
497        private void observe() {
498            ContentResolver resolver = mContext.getContentResolver();
499            resolver.registerContentObserver(mSecureSettingsUri,
500                    false, this, UserHandle.USER_ALL);
501            update(null);
502        }
503
504        @Override
505        public void onChange(boolean selfChange, Uri uri) {
506            update(uri);
507        }
508
509        private void update(Uri uri) {
510            if (uri == null || mSecureSettingsUri.equals(uri)) {
511                rebindServices();
512            }
513        }
514    }
515
516    public class ManagedServiceInfo implements IBinder.DeathRecipient {
517        public IInterface service;
518        public ComponentName component;
519        public int userid;
520        public boolean isSystem;
521        public ServiceConnection connection;
522        public int targetSdkVersion;
523
524        public ManagedServiceInfo(IInterface service, ComponentName component,
525                int userid, boolean isSystem, ServiceConnection connection, int targetSdkVersion) {
526            this.service = service;
527            this.component = component;
528            this.userid = userid;
529            this.isSystem = isSystem;
530            this.connection = connection;
531            this.targetSdkVersion = targetSdkVersion;
532        }
533
534        @Override
535        public String toString() {
536            return new StringBuilder("ManagedServiceInfo[")
537                    .append("component=").append(component)
538                    .append(",userid=").append(userid)
539                    .append(",isSystem=").append(isSystem)
540                    .append(",targetSdkVersion=").append(targetSdkVersion)
541                    .append(",connection=").append(connection == null ? null : "<connection>")
542                    .append(",service=").append(service)
543                    .append(']').toString();
544        }
545
546        public boolean enabledAndUserMatches(int nid) {
547            if (!isEnabledForCurrentProfiles()) {
548                return false;
549            }
550            if (this.userid == UserHandle.USER_ALL) return true;
551            if (nid == UserHandle.USER_ALL || nid == this.userid) return true;
552            return supportsProfiles() && mUserProfiles.isCurrentProfile(nid);
553        }
554
555        public boolean supportsProfiles() {
556            return targetSdkVersion >= Build.VERSION_CODES.L;
557        }
558
559        @Override
560        public void binderDied() {
561            if (DEBUG) Slog.d(TAG, "binderDied");
562            // Remove the service, but don't unbind from the service. The system will bring the
563            // service back up, and the onServiceConnected handler will readd the service with the
564            // new binding. If this isn't a bound service, and is just a registered
565            // service, just removing it from the list is all we need to do anyway.
566            removeServiceImpl(this.service, this.userid);
567        }
568
569        /** convenience method for looking in mEnabledServicesForCurrentProfiles */
570        public boolean isEnabledForCurrentProfiles() {
571            if (this.isSystem) return true;
572            if (this.connection == null) return false;
573            return mEnabledServicesForCurrentProfiles.contains(this.component);
574        }
575    }
576
577    public static class UserProfiles {
578        // Profiles of the current user.
579        private final SparseArray<UserInfo> mCurrentProfiles = new SparseArray<UserInfo>();
580
581        public void updateCache(Context context) {
582            UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
583            if (userManager != null) {
584                int currentUserId = ActivityManager.getCurrentUser();
585                List<UserInfo> profiles = userManager.getProfiles(currentUserId);
586                synchronized (mCurrentProfiles) {
587                    mCurrentProfiles.clear();
588                    for (UserInfo user : profiles) {
589                        mCurrentProfiles.put(user.id, user);
590                    }
591                }
592            }
593        }
594
595        public int[] getCurrentProfileIds() {
596            synchronized (mCurrentProfiles) {
597                int[] users = new int[mCurrentProfiles.size()];
598                final int N = mCurrentProfiles.size();
599                for (int i = 0; i < N; ++i) {
600                    users[i] = mCurrentProfiles.keyAt(i);
601                }
602                return users;
603            }
604        }
605
606        public boolean isCurrentProfile(int userId) {
607            synchronized (mCurrentProfiles) {
608                return mCurrentProfiles.get(userId) != null;
609            }
610        }
611    }
612
613    protected static class Config {
614        String caption;
615        String serviceInterface;
616        String secureSettingName;
617        String bindPermission;
618        String settingsAction;
619        int clientLabel;
620    }
621}
622