summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/launcher/dashclock/ExtensionHost.java
blob: 1bf2bb9e3c1a2bab44bb796f950f86b7f9c3e20a (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
 * Copyright 2013 Google Inc.
 *
 * 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.launcher.dashclock;

import com.google.android.apps.dashclock.api.DashClockExtension;
import com.google.android.apps.dashclock.api.ExtensionData;
import com.google.android.apps.dashclock.api.internal.IExtension;
import com.google.android.apps.dashclock.api.internal.IExtensionHost;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

/**
 * The primary local-process endpoint that deals with extensions. Instances of this class are in
 * charge of maintaining a {@link ServiceConnection} with connected extensions. There should
 * only be one instance of this class in the app.
 * <p>
 * This class is intended to be used as part of a containing service. Make sure to call
 * {@link #destroy()} in the service's {@link android.app.Service#onDestroy()}.
 */
public class ExtensionHost {
    // TODO: this class badly needs inline docs
    private static final String TAG = "ExtensionHost";

    private static final int CURRENT_EXTENSION_PROTOCOL_VERSION = 2;

    /**
     * The amount of time to wait after something has changed before recognizing it as an individual
     * event. Any changes within this time window will be collapsed, and will further delay the
     * handling of the event.
     */
    public static final int UPDATE_COLLAPSE_TIME_MILLIS = 500;

    private Context mContext;
    private Context mHostActivityContext;
    private Handler mClientThreadHandler = new Handler();

    private ExtensionManager mExtensionManager;

    private Map<ComponentName, Connection> mExtensionConnections
            = new HashMap<ComponentName, Connection>();

    private final Set<ComponentName> mExtensionsToUpdateWhenScreenOn = new HashSet<ComponentName>();
    private boolean mScreenOnReceiverRegistered = false;

    private volatile Looper mAsyncLooper;
    private volatile Handler mAsyncHandler;

    public ExtensionHost(Context context, Context hostActivityContext) {
        mContext = context;
        mHostActivityContext = hostActivityContext;
        init();
    }

    public void init() {
        mExtensionManager = ExtensionManager.getInstance(mContext, mHostActivityContext);
        mExtensionManager.addOnChangeListener(mChangeListener);

        HandlerThread thread = new HandlerThread("ExtensionHost");
        thread.start();
        mAsyncLooper = thread.getLooper();
        mAsyncHandler = new Handler(mAsyncLooper);

        mChangeListener.onExtensionsChanged(null);
        mExtensionManager.cleanupExtensions();

        Log.d(TAG, "ExtensionHost initialized.");
    }

    public void destroy() {
        mExtensionManager.removeOnChangeListener(mChangeListener);
        if (mScreenOnReceiverRegistered) {
            mContext.unregisterReceiver(mScreenOnReceiver);
            mScreenOnReceiverRegistered = false;
        }
        establishAndDestroyConnections(new ArrayList<ComponentName>());
        mAsyncLooper.quit();
    }

    private void establishAndDestroyConnections(List<ComponentName> newExtensionNames) {
        // Get the list of active extensions
        Set<ComponentName> activeSet = new HashSet<ComponentName>();
        activeSet.addAll(newExtensionNames);

        // Get the list of connected extensions
        Set<ComponentName> connectedSet = new HashSet<ComponentName>();
        connectedSet.addAll(mExtensionConnections.keySet());

        for (final ComponentName cn : activeSet) {
            if (connectedSet.contains(cn)) {
                continue;
            }

            // Bind anything not currently connected (this is the initial connection
            // to the now-added extension)
            Connection conn = createConnection(cn, false);
            if (conn != null) {
                mExtensionConnections.put(cn, conn);
            }
        }

        // Remove active items from the connected set, leaving only newly-inactive items
        // to be disconnected below.
        connectedSet.removeAll(activeSet);

        for (ComponentName cn : connectedSet) {
            Connection conn = mExtensionConnections.get(cn);

            // Unbind the now-disconnected extension
            destroyConnection(conn);
            mExtensionConnections.remove(cn);
        }
    }

    private Connection createConnection(final ComponentName cn, final boolean isReconnect) {
        Log.d(TAG, "createConnection for " + cn + "; isReconnect=" + isReconnect);

        final Connection conn = new Connection();
        conn.componentName = cn;
        conn.contentObserver = new ContentObserver(mClientThreadHandler) {
            @Override
            public void onChange(boolean selfChange) {
                execute(conn.componentName,
                        UPDATE_OPERATIONS.get(DashClockExtension.UPDATE_REASON_CONTENT_CHANGED),
                        UPDATE_COLLAPSE_TIME_MILLIS,
                        DashClockExtension.UPDATE_REASON_CONTENT_CHANGED);
            }
        };
        conn.hostInterface = makeHostInterface(conn);
        conn.serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(final ComponentName componentName, IBinder iBinder) {
                conn.ready = true;
                conn.binder = IExtension.Stub.asInterface(iBinder);

                // Initialize the service
                execute(conn, new Operation() {
                    @Override
                    public void run(IExtension extension) throws RemoteException {
                        // Note that this is protected from ANRs since it runs in the
                        // AsyncHandler thread. Also, since this is a 'oneway' call,
                        // when used with remote extensions, this call does not block.
                        try {
                            extension.onInitialize(conn.hostInterface, isReconnect);
                        } catch (SecurityException e) {
                            Log.e(TAG, "Error initializing extension "
                                    + componentName.toString(), e);
                        }
                    }
                }, 0, null);

                if (!isReconnect) {
                    execute(conn.componentName,
                            UPDATE_OPERATIONS.get(DashClockExtension.UPDATE_REASON_INITIAL),
                            0,
                            null);
                }

                // Execute operations that were deferred until the service was available.
                // TODO: handle service disruptions that occur here
                synchronized (conn.deferredOps) {
                    if (conn.ready) {
                        Set<Object> processedCollapsedTokens = new HashSet<Object>();
                        Iterator<Pair<Object, Operation>> it = conn.deferredOps.iterator();
                        while (it.hasNext()) {
                            Pair<Object, Operation> op = it.next();
                            if (op.first != null) {
                                if (processedCollapsedTokens.contains(op.first)) {
                                    // An operation with this collapse token has already been
                                    // processed; skip this one.
                                    continue;
                                }

                                processedCollapsedTokens.add(op.first);
                            }
                            execute(conn, op.second, 0, null);
                            it.remove();
                        }
                    }
                }
            }

            @Override
            public void onServiceDisconnected(final ComponentName componentName) {
                conn.serviceConnection = null;
                conn.binder = null;
                conn.ready = false;
                mClientThreadHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mExtensionConnections.remove(componentName);
                    }
                });
            }
        };

        try {
            if (!mContext.bindService(new Intent().setComponent(cn), conn.serviceConnection,
                    Context.BIND_AUTO_CREATE)) {
                Log.e(TAG, "Error binding to extension " + cn.flattenToShortString());
                return null;
            }
        } catch (SecurityException e) {
            Log.e(TAG, "Error binding to extension " + cn.flattenToShortString(), e);
            return null;
        }

        return conn;
    }

    private IExtensionHost makeHostInterface(final Connection conn) {
        return new IExtensionHost.Stub() {
            @Override
            public void publishUpdate(ExtensionData data) throws RemoteException {
                if (data == null) {
                    data = new ExtensionData();
                }

                // TODO: this needs to be thread-safe
                Log.d(TAG, "publishUpdate received for extension " + conn.componentName);
                mExtensionManager.updateExtensionData(conn.componentName, data);
            }

            @Override
            public void addWatchContentUris(String[] contentUris) throws RemoteException {
                if (contentUris != null && contentUris.length > 0 && conn.contentObserver != null) {
                    ContentResolver resolver = mContext.getContentResolver();
                    for (String uri : contentUris) {
                        if (TextUtils.isEmpty(uri)) {
                            continue;
                        }

                        resolver.registerContentObserver(Uri.parse(uri), true,
                                conn.contentObserver);
                    }
                }
            }

            @Override
            public void removeAllWatchContentUris() throws RemoteException {
                ContentResolver resolver = mContext.getContentResolver();
                resolver.unregisterContentObserver(conn.contentObserver);
            }

            @Override
            public void setUpdateWhenScreenOn(boolean updateWhenScreenOn) throws RemoteException {
                synchronized (mExtensionsToUpdateWhenScreenOn) {
                    if (updateWhenScreenOn) {
                        if (mExtensionsToUpdateWhenScreenOn.size() == 0) {
                            IntentFilter filter = new IntentFilter();
                            filter.addAction(Intent.ACTION_SCREEN_ON);
                            mContext.registerReceiver(mScreenOnReceiver, filter);
                            mScreenOnReceiverRegistered = true;
                        }

                        mExtensionsToUpdateWhenScreenOn.add(conn.componentName);

                    } else {
                        mExtensionsToUpdateWhenScreenOn.remove(conn.componentName);

                        if (mExtensionsToUpdateWhenScreenOn.size() == 0) {
                            mContext.unregisterReceiver(mScreenOnReceiver);
                            mScreenOnReceiverRegistered = false;
                        }
                    }
                }
            }
        };
    }

    public void requestAllManualUpdate() {
        for (ComponentName cn : mExtensionConnections.keySet()) {
            execute(cn, UPDATE_OPERATIONS.get(DashClockExtension.UPDATE_REASON_MANUAL),
                    0, null);
        }
    }

    private void destroyConnection(Connection conn) {
        if (conn.contentObserver != null) {
            mContext.getContentResolver().unregisterContentObserver(conn.contentObserver);
            conn.contentObserver = null;
        }

        conn.binder = null;
        mContext.unbindService(conn.serviceConnection);
        conn.serviceConnection = null;
    }

    private ExtensionManager.OnChangeListener mChangeListener
            = new ExtensionManager.OnChangeListener() {
        @Override
        public void onExtensionsChanged(ComponentName sourceExtension) {
            if (sourceExtension != null) {
                // If the extension change is a result of a single extension, don't do anything,
                // since we're only interested in events triggered by the system overall (e.g.
                // extensions added or removed).
                return;
            }
            Log.d(TAG, "onExtensionsChanged; calling establishAndDestroyConnections.");
            establishAndDestroyConnections(mExtensionManager.getActiveExtensionNames());
        }
    };

    private void execute(final Connection conn, final Operation operation,
            int collapseDelayMillis, final Object collapseToken) {
        final Object collapseTokenForConn;
        if (collapseDelayMillis > 0 && collapseToken != null) {
            collapseTokenForConn = new Pair<ComponentName, Object>(conn.componentName,
                    collapseToken);
        } else {
            collapseTokenForConn = null;
        }

        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    if (conn.binder == null) {
                        throw new RemoteException("Binder is unavailable.");
                    }
                    operation.run(conn.binder);
                } catch (RemoteException e) {
                    Log.e(TAG, "Couldn't execute operation; scheduling for retry upon service "
                            + "reconnection.", e);
                    // TODO: exponential backoff for retrying the same operation, or fail after
                    // n attempts (in case the remote service consistently crashes when
                    // executing this operation)
                    synchronized (conn.deferredOps) {
                        conn.deferredOps.add(new Pair<Object, Operation>(
                                collapseTokenForConn, operation));
                    }
                }
            }
        };

        if (conn.ready) {
            if (collapseTokenForConn != null) {
                mAsyncHandler.removeCallbacksAndMessages(collapseTokenForConn);
            }

            if (collapseDelayMillis > 0) {
                mAsyncHandler.postAtTime(runnable, collapseTokenForConn,
                        SystemClock.uptimeMillis() + collapseDelayMillis);
            } else {
                mAsyncHandler.post(runnable);
            }
        } else {
            mAsyncHandler.post(new Runnable() {
                @Override
                public void run() {
                    synchronized (conn.deferredOps) {
                        conn.deferredOps.add(new Pair<Object, Operation>(
                                collapseTokenForConn, operation));
                    }
                }
            });
        }
    }

    public void execute(ComponentName cn, Operation operation,
                int collapseDelayMillis, final Object collapseToken) {
        Connection conn = mExtensionConnections.get(cn);
        if (conn == null) {
            conn = createConnection(cn, true);
            if (conn != null) {
                mExtensionConnections.put(cn, conn);
            } else {
                Log.e(TAG, "Couldn't connect to extension to perform operation; operation "
                        + "canceled.");
                return;
            }
        }

        execute(conn, operation, collapseDelayMillis, collapseToken);
    }

    private final BroadcastReceiver mScreenOnReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            synchronized (mExtensionsToUpdateWhenScreenOn) {
                for (ComponentName cn : mExtensionsToUpdateWhenScreenOn) {
                    execute(cn, UPDATE_OPERATIONS.get(DashClockExtension.UPDATE_REASON_SCREEN_ON),
                            0, null);
                }
            }
        }
    };

    static final SparseArray<Operation> UPDATE_OPERATIONS = new SparseArray<Operation>();

    static {
        _createUpdateOperation(DashClockExtension.UPDATE_REASON_UNKNOWN);
        _createUpdateOperation(DashClockExtension.UPDATE_REASON_INITIAL);
        _createUpdateOperation(DashClockExtension.UPDATE_REASON_PERIODIC);
        _createUpdateOperation(DashClockExtension.UPDATE_REASON_SETTINGS_CHANGED);
        _createUpdateOperation(DashClockExtension.UPDATE_REASON_CONTENT_CHANGED);
        _createUpdateOperation(DashClockExtension.UPDATE_REASON_SCREEN_ON);
        _createUpdateOperation(DashClockExtension.UPDATE_REASON_MANUAL);
    }

    private static void _createUpdateOperation(final int reason) {
        UPDATE_OPERATIONS.put(reason, new ExtensionHost.Operation() {
            @Override
            public void run(IExtension extension) throws RemoteException {
                // Note that this is protected from ANRs since it runs in the AsyncHandler thread.
                // Also, since this is a 'oneway' call, when used with remote extensions, this call
                // does not block.
                extension.onUpdate(reason);
            }
        });
    }

    public static boolean supportsProtocolVersion(int protocolVersion) {
        return protocolVersion > 0 && protocolVersion <= CURRENT_EXTENSION_PROTOCOL_VERSION;
    }

    /**
     * Will be run on a worker thread.
     */
    public static interface Operation {
        void run(IExtension extension) throws RemoteException;
    }

    private static class Connection {
        boolean ready = false;
        ComponentName componentName;
        ServiceConnection serviceConnection;
        IExtension binder;
        IExtensionHost hostInterface;
        ContentObserver contentObserver;

        /**
         * Only access on the async thread. The pair is (collapse token, operation)
         */
        final Queue<Pair<Object, Operation>> deferredOps
                = new LinkedList<Pair<Object, Operation>>();
    }
}