TelecomLoaderService.java revision 5d2c1e69ecb851121177396ac376dee1fb41d421
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.telecom;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
23import android.os.IBinder;
24import android.os.RemoteException;
25import android.os.ServiceManager;
26import android.os.UserHandle;
27import android.util.Slog;
28
29import com.android.server.SystemService;
30
31/**
32 * Starts the telecom component by binding to its ITelecomService implementation. Telecom is setup
33 * to run in the system-server process so once it is loaded into memory it will stay running.
34 * @hide
35 */
36public class TelecomLoaderService extends SystemService {
37    private static final String TAG = "TelecomLoaderService";
38
39    private class TelecomServiceConnection implements ServiceConnection {
40        @Override
41        public void onServiceConnected(ComponentName name, IBinder service) {
42            // Normally, we would listen for death here, but since telecom runs in the same process
43            // as this loader (process="system") thats redundant here.
44            try {
45                service.linkToDeath(new IBinder.DeathRecipient() {
46                    @Override
47                    public void binderDied() {
48                        connectToTelecom();
49                    }
50                }, 0);
51
52                ServiceManager.addService(Context.TELECOM_SERVICE, service);
53            } catch (RemoteException e) {
54                Slog.w(TAG, "Failed linking to death.");
55            }
56        }
57
58        @Override
59        public void onServiceDisconnected(ComponentName name) {
60            connectToTelecom();
61        }
62    }
63
64    private static final ComponentName SERVICE_COMPONENT = new ComponentName(
65            "com.android.server.telecom",
66            "com.android.server.telecom.TelecomService");
67
68    private static final String SERVICE_ACTION = "com.android.ITelecomService";
69
70    private final Context mContext;
71    private TelecomServiceConnection mServiceConnection;
72
73    public TelecomLoaderService(Context context) {
74        super(context);
75        mContext = context;
76    }
77
78    @Override
79    public void onStart() {
80    }
81
82    @Override
83    public void onBootPhase(int phase) {
84        if (phase == PHASE_ACTIVITY_MANAGER_READY) {
85            connectToTelecom();
86        }
87    }
88
89    private void connectToTelecom() {
90        if (mServiceConnection != null) {
91            // TODO: Is unbinding worth doing or wait for system to rebind?
92            mContext.unbindService(mServiceConnection);
93            mServiceConnection = null;
94        }
95
96        TelecomServiceConnection serviceConnection = new TelecomServiceConnection();
97        Intent intent = new Intent(SERVICE_ACTION);
98        intent.setComponent(SERVICE_COMPONENT);
99        int flags = Context.BIND_IMPORTANT | Context.BIND_AUTO_CREATE;
100
101        // Bind to Telecom and register the service
102        if (mContext.bindServiceAsUser(intent, serviceConnection, flags, UserHandle.OWNER)) {
103            mServiceConnection = serviceConnection;
104        }
105    }
106}
107