aboutsummaryrefslogtreecommitdiffstats
path: root/apps/SdkController/src/com/android/tools/sdkcontroller/service/ControllerService.java
blob: cd35833f27554ff21417158500ddd734b19ce8d3 (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
/*
 * Copyright (C) 2012 The Android Open Source 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 com.android.tools.sdkcontroller.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.android.tools.sdkcontroller.R;
import com.android.tools.sdkcontroller.activities.MainActivity;
import com.android.tools.sdkcontroller.handlers.BaseHandler;
import com.android.tools.sdkcontroller.handlers.BaseHandler.HandlerType;
import com.android.tools.sdkcontroller.handlers.MultiTouchHandler;
import com.android.tools.sdkcontroller.handlers.SensorsHandler;
import com.android.tools.sdkcontroller.lib.EmulatorConnection;
import com.android.tools.sdkcontroller.lib.EmulatorConnection.EmulatorConnectionType;
import com.android.tools.sdkcontroller.lib.EmulatorListener;

/**
 * The background service of the SdkController.
 * There can be only one instance of this.
 * <p/>
 * The service manages a number of action "handlers" which can be seen as individual tasks
 * that the user might want to accomplish, for example "sending sensor data to the emulator"
 * or "sending multi-touch data and displaying an emulator screen".
 * <p/>
 * Each handler currently has its own emulator connection associated to it (cf class
 * {@code EmuCnxHandler} below. However our goal is to later move to a single connection channel
 * with all data multiplexed on top of it.
 * <p/>
 * All the handlers are created when the service starts, and whether the emulator connection
 * is successful or not, and whether there's any UI to control it. It's up to the handlers
 * to deal with these specific details. <br/>
 * For example the {@link SensorsHandler} initializes its sensor list as soon as created
 * and then tries to send data as soon as there's an emulator connection.
 * On the other hand the {@link MultiTouchHandler} lays dormant till there's an UI interacting
 * with it.
 */
public class ControllerService extends Service {

    /*
     * Implementation reference:
     * http://developer.android.com/reference/android/app/Service.html#LocalServiceSample
     */

    public static String TAG = ControllerService.class.getSimpleName();
    private static boolean DEBUG = true;

    /** Identifier for the notification. */
    private static int NOTIF_ID = 'S' << 24 + 'd' << 16 + 'k' << 8 + 'C' << 0;

    private final IBinder mBinder = new ControllerBinder();

    private List<ControllerListener> mListeners = new ArrayList<ControllerListener>();

    /**
     * Whether the service is running. Set to true in onCreate, false in onDestroy.
     */
    private static volatile boolean gServiceIsRunning = false;

    /** Internal error reported by the service. */
    private String mServiceError = "";

    private final Set<EmuCnxHandler> mHandlers = new HashSet<ControllerService.EmuCnxHandler>();

    /**
     * Interface that the service uses to notify binded activities.
     * <p/>
     * As a design rule, implementations of this listener should be aware that most calls
     * will NOT happen on the UI thread. Any access to the UI should be properly protected
     * by using {@link Activity#runOnUiThread(Runnable)}.
     */
    public interface ControllerListener {
        /**
         * The error string reported by the service has changed. <br/>
         * Note this may be called from a thread different than the UI thread.
         */
        void onErrorChanged();

        /**
         * The service status has changed (emulator connected/disconnected.)
         */
        void onStatusChanged();
    }

    /** Interface that callers can use to access the service. */
    public class ControllerBinder extends Binder {

        /**
         * Adds a new listener that will be notified when the service state changes.
         *
         * @param listener A non-null listener. Ignored if already listed.
         */
        public void addControllerListener(ControllerListener listener) {
            assert listener != null;
            if (listener != null) {
                synchronized(mListeners) {
                    if (!mListeners.contains(listener)) {
                        mListeners.add(listener);
                    }
                }
            }
        }

        /**
         * Removes a listener.
         *
         * @param listener A listener to remove. Can be null.
         */
        public void removeControllerListener(ControllerListener listener) {
            assert listener != null;
            synchronized(mListeners) {
                mListeners.remove(listener);
            }
        }

        /**
         * Returns the error string accumulated by the service.
         * Typically these would relate to failures to establish the communication
         * channel(s) with the emulator, or unexpected disconnections.
         */
        public String getServiceError() {
            return mServiceError;
        }

        /**
         * Indicates when <em>all</all> the communication channels for all handlers
         * are properly connected.
         *
         * @return True if all the handler's communication channels are connected.
         */
        public boolean isEmuConnected() {
            for (EmuCnxHandler handler : mHandlers) {
                if (!handler.isConnected()) {
                    return false;
                }
            }
            return true;
        }

        /**
         * Returns the handler for the given type.
         *
         * @param type One of the {@link HandlerType}s. Must not be null.
         * @return Null if the type is not found, otherwise the handler's unique instance.
         */
        public BaseHandler getHandler(HandlerType type) {
            for (EmuCnxHandler handler : mHandlers) {
                BaseHandler h = handler.getHandler();
                if (h.getType() == type) {
                    return h;
                }
            }
            return null;
        }
    }

    /**
     * Whether the service is running. Set to true in onCreate, false in onDestroy.
     */
    public static boolean isServiceIsRunning() {
        return gServiceIsRunning;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (DEBUG) Log.d(TAG, "Service onCreate");
        gServiceIsRunning = true;
        showNotification();
        onServiceStarted();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        if (DEBUG) Log.d(TAG, "Service onStartCommand");
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        if (DEBUG) Log.d(TAG, "Service onBind");
        return mBinder;
    }

    @Override
    public void onDestroy() {
        if (DEBUG) Log.d(TAG, "Service onDestroy");
        gServiceIsRunning = false;
        removeNotification();
        resetError();
        onServiceStopped();
        super.onDestroy();
    }

    // ------

    /**
     * Wrapper that associates one {@link EmulatorConnection} with
     * one {@link BaseHandler}. Ideally we would not need this if all
     * the action handlers were using the same port, so this wrapper
     * is just temporary.
     */
    private class EmuCnxHandler implements EmulatorListener {

        private EmulatorConnection mCnx;
        private boolean mConnected;
        private final BaseHandler mHandler;

        public EmuCnxHandler(BaseHandler handler) {
            mHandler = handler;
        }

        @Override
        public void onEmulatorConnected() {
            mConnected = true;
            notifyStatusChanged();
        }

        @Override
        public void onEmulatorDisconnected() {
            mConnected = false;
            notifyStatusChanged();
        }

        @Override
        public String onEmulatorQuery(String query, String param) {
            if (DEBUG) Log.d(TAG, mHandler.getType().toString() +  " Query " + query);
            return mHandler.onEmulatorQuery(query, param);
        }

        @Override
        public String onEmulatorBlobQuery(byte[] array) {
            if (DEBUG) Log.d(TAG, mHandler.getType().toString() +  " BlobQuery " + array.length);
            return mHandler.onEmulatorBlobQuery(array);
        }

        EmuCnxHandler connect() {
            assert mCnx == null;

            mCnx = new EmulatorConnection(this);

            // Apps targeting Honeycomb SDK can't do network IO on their main UI
            // thread. So just start the connection from a thread.
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    // This will call onEmulatorBindResult with the result.
                    mCnx.connect(mHandler.getPort(), EmulatorConnectionType.SYNC_CONNECTION);
                }
            }, "EmuCnxH.connect-" + mHandler.getType().toString());
            t.start();

            return this;
        }

        @Override
        public void onEmulatorBindResult(boolean success, Exception e) {
            if (success) {
                mHandler.onStart(mCnx, ControllerService.this /*context*/);
            } else {
                Log.e(TAG, "EmuCnx failed for " + mHandler.getType(), e);
                String msg = mHandler.getType().toString() + " failed: " +
                    (e == null ? "n/a" : e.toString());
                addError(msg);
            }
        }

        void disconnect() {
            if (mCnx != null) {
                mHandler.onStop();
                mCnx.disconnect();
                mCnx = null;
            }
        }

        boolean isConnected() {
            return mConnected;
        }

        public BaseHandler getHandler() {
            return mHandler;
        }
    }

    private void disconnectAll() {
        for(EmuCnxHandler handler : mHandlers) {
            handler.disconnect();
        }
        mHandlers.clear();
    }

    /**
     * Called when the service has been created.
     */
    private void onServiceStarted() {
        try {
            disconnectAll();

            assert mHandlers.isEmpty();
            mHandlers.add(new EmuCnxHandler(new MultiTouchHandler()).connect());
            mHandlers.add(new EmuCnxHandler(new SensorsHandler()).connect());
        } catch (Exception e) {
            addError("Connection failed: " + e.toString());
        }
    }

    /**
     * Called when the service is being destroyed.
     */
    private void onServiceStopped() {
        disconnectAll();
    }

    private void notifyErrorChanged() {
        synchronized(mListeners) {
            for (ControllerListener listener : mListeners) {
                listener.onErrorChanged();
            }
        }
    }

    private void notifyStatusChanged() {
        synchronized(mListeners) {
            for (ControllerListener listener : mListeners) {
                listener.onStatusChanged();
            }
        }
    }

    /**
     * Resets the error string and notify listeners.
     */
    private void resetError() {
        mServiceError = "";

        notifyErrorChanged();
    }

    /**
     * An internal utility method to add a line to the error string and notify listeners.
     * @param error A non-null non-empty error line. \n will be added automatically.
     */
    private void addError(String error) {
        Log.e(TAG, error);
        if (mServiceError.length() > 0) {
            mServiceError += "\n";
        }
        mServiceError += error;

        notifyErrorChanged();
    }

    /**
     * Displays a notification showing that the service is running.
     * When the user touches the notification, it opens the main activity
     * which allows the user to stop this service.
     */
    @SuppressWarnings("deprecated")
    private void showNotification() {
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        String text = getString(R.string.service_notif_title);

        // Note: Notification is marked as deprecated -- in API 11+ there's a new Builder class
        // but we need to have API 7 compatibility so we ignore that warning.

        Notification n = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());
        n.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pi = PendingIntent.getActivity(
                this,     //context
                0,        //requestCode
                intent,   //intent
                0         // pending intent flags
                );
        n.setLatestEventInfo(this, text, text, pi);

        nm.notify(NOTIF_ID, n);
    }

    private void removeNotification() {
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.cancel(NOTIF_ID);
    }
}