aboutsummaryrefslogtreecommitdiffstats
path: root/cm/lib/main/java/org/cyanogenmod/platform/internal/BrokerableCMSystemService.java
blob: 76bad9fbe34716921774a3c4750f5a1a0e935f1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
 * Copyright (C) 2016 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cyanogenmod.platform.internal;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Message;
import android.os.SystemClock;
import android.util.Slog;

import cyanogenmod.platform.Manifest;

import com.android.internal.util.Preconditions;

import org.cyanogenmod.platform.internal.common.BrokeredServiceConnection;

public abstract class BrokerableCMSystemService<T extends IInterface> extends CMSystemService {
    private static final String TAG = BrokerableCMSystemService.class.getSimpleName();

    private static final int MSG_TRY_CONNECTING = 1;
    private static final long SERVICE_CONNECTION_WAIT_TIME_MS = 4 * 1000L; // 4 seconds
    private Context mContext;

    private BrokeredServiceConnection mBrokeredServiceConnection;
    private T mImplementingBinderInterface;

    public BrokerableCMSystemService(Context context) {
        super(context);
        mContext = context;
    }

    /**
     * Called when the {@link IInterface} contract for the given {@link IBinder} is to
     * be assigned for the implementing service {@link T}
     * @param service
     * @return {@link T}
     */
    protected abstract T getIBinderAsIInterface(@NonNull IBinder service);

    /**
     * Called when necessary as the default implementation. (usually a failure implementation)
     * For when an implementing service is not found, is updating, or is failing.
     * @return {@link T}
     */
    protected abstract T getDefaultImplementation();

    /**
     * Called when attempting to connect to the a given {@link T} implementation. Defines
     * the {@link ComponentName} to be used for the binding operation.
     *
     * By default, the calling component MUST gate its implementation by the
     * {@link Manifest.permission.BIND_CORE_SERVICE} permission as well as using,
     * the permission granted to its own package.
     *
     * This permission can be overridden via {@link #getComponentFilteringPermission()}
     *
     * @return {@link ComponentName}
     */
    @Nullable
    protected abstract ComponentName getServiceComponent();

    /**
     * Override this method if your broker will provide its own permission for guarding a vertical
     * api defintion. Otherwise, the component from {@link #getServiceComponent()}
     * will be gated via the {@link Manifest.permission.BIND_CORE_SERVICE} permission.
     *
     * @return boolean
     */
    @NonNull
    protected String getComponentFilteringPermission() {
        return Manifest.permission.BIND_CORE_SERVICE;
    }

    /**
     * Set a {@link BrokeredServiceConnection} to receive callbacks when an implementation is
     * connected or disconnected.
     * @param brokeredServiceComponent
     */
    public final void setBrokeredServiceConnection(
            @NonNull BrokeredServiceConnection brokeredServiceComponent) {
        Preconditions.checkNotNull(brokeredServiceComponent);
        Slog.e(TAG, "Setting brokered service connection "
                + brokeredServiceComponent.toString());
        mBrokeredServiceConnection = brokeredServiceComponent;
    }

    /**
     * Get the implementing service for the given binder invocation. Usually called from a binder
     * thread in a subclassed service.
     * @return {@link T} that represents the implementing service
     */
    public final T getBrokeredService() {
        final T service = getOrConnectService();
        if (service != null) {
            return service;
        }
        return getDefaultImplementation();
    }

    @Override
    public void onBootPhase(int phase) {
        super.onBootPhase(phase);
        if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
            Slog.d(TAG, "Third party apps ready");
            tryConnecting();
        }
    }

    private T getOrConnectService() {
        synchronized (this) {
            if (mImplementingBinderInterface != null) {
                return mImplementingBinderInterface;
            }
            // Service is not connected. Try blocking connecting.
            mConnectionHandler.sendMessage(
                    mConnectionHandler.obtainMessage(MSG_TRY_CONNECTING));
            final long shouldEnd =
                    SystemClock.elapsedRealtime() + SERVICE_CONNECTION_WAIT_TIME_MS;
            long waitTime = SERVICE_CONNECTION_WAIT_TIME_MS;
            while (waitTime > 0) {
                try {
                    // TODO: consider using Java concurrent construct instead of raw object wait
                    this.wait(waitTime);
                } catch (InterruptedException e) {
                    Slog.w(TAG, "Connection wait interrupted", e);
                }
                if (mImplementingBinderInterface != null) {
                    // Success
                    return mImplementingBinderInterface;
                }
                // Calculate remaining waiting time to make sure we wait the full timeout period
                waitTime = shouldEnd - SystemClock.elapsedRealtime();
            }
            return null;
        }
    }

    private final Handler mConnectionHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_TRY_CONNECTING:
                    tryConnecting();
                    break;
                default:
                    Slog.e(TAG, "Unknown message");
            }
        }
    };

    /**
     * Attempt to connect to the component which is going to serve {@link T}
     * interface contract implementation.
     */
    public final void tryConnecting() {
        Slog.i(TAG, "Connecting to implementation");
        synchronized (this) {
            if (mImplementingBinderInterface != null) {
                Slog.d(TAG, "Already connected");
                return;
            }
            final Intent intent = new Intent();
            final ComponentName cn = getServiceComponent();
            if (cn == null) {
                Slog.e(TAG, "No implementation service found");
                return;
            }
            intent.setComponent(cn);
            try {
                if (mContext.getPackageManager().checkPermission(
                        getComponentFilteringPermission(),
                        cn.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
                    Slog.e(TAG, "Target component lacks " + getComponentFilteringPermission()
                            + " service permission, failing " + cn);
                    return;
                }
                if (!mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE)) {
                    Slog.e(TAG, "Failed to bind to implementation " + cn);
                }
            } catch (SecurityException e) {
                Slog.e(TAG, "Forbidden to bind to implementation " + cn, e);
            }
        }
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Slog.i(TAG, "Implementation service connected");
            synchronized (BrokerableCMSystemService.this) {
                mImplementingBinderInterface = getIBinderAsIInterface(service);
                BrokerableCMSystemService.this.notifyAll();
                if (mBrokeredServiceConnection != null) {
                    Slog.i(TAG, "Notifying service connected");
                    mBrokeredServiceConnection.onBrokeredServiceConnected();
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Slog.i(TAG, "Implementation service unexpectedly disconnected");
            synchronized (BrokerableCMSystemService.this) {
                mImplementingBinderInterface = null;
                BrokerableCMSystemService.this.notifyAll();
                if (mBrokeredServiceConnection != null) {
                    mBrokeredServiceConnection.onBrokeredServiceDisconnected();
                }
            }
        }
    };
}