summaryrefslogtreecommitdiffstats
path: root/src/com/android/nfc/handover/HandoverService.java
blob: 261cbca4307b3a268d386784adf47f5d13ed2b49 (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
package com.android.nfc.handover;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.util.Pair;

import com.android.nfc.R;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public class HandoverService extends Service implements HandoverTransfer.Callback,
        BluetoothHeadsetHandover.Callback {

    static final String TAG = "HandoverService";

    static final int MSG_REGISTER_CLIENT = 0;
    static final int MSG_DEREGISTER_CLIENT = 1;
    static final int MSG_START_INCOMING_TRANSFER = 2;
    static final int MSG_START_OUTGOING_TRANSFER = 3;
    static final int MSG_HEADSET_HANDOVER = 4;

    static final String BUNDLE_TRANSFER = "transfer";

    static final String EXTRA_HEADSET_DEVICE = "device";
    static final String EXTRA_HEADSET_NAME = "headsetname";

    static final String ACTION_CANCEL_HANDOVER_TRANSFER =
            "com.android.nfc.handover.action.CANCEL_HANDOVER_TRANSFER";
    static final String EXTRA_SOURCE_ADDRESS =
            "com.android.nfc.handover.extra.SOURCE_ADDRESS";

    static final String ACTION_BT_OPP_TRANSFER_PROGRESS =
            "android.btopp.intent.action.BT_OPP_TRANSFER_PROGRESS";

    static final String ACTION_BT_OPP_TRANSFER_DONE =
            "android.btopp.intent.action.BT_OPP_TRANSFER_DONE";

    static final String EXTRA_BT_OPP_TRANSFER_STATUS =
            "android.btopp.intent.extra.BT_OPP_TRANSFER_STATUS";

    static final String EXTRA_BT_OPP_TRANSFER_MIMETYPE =
            "android.btopp.intent.extra.BT_OPP_TRANSFER_MIMETYPE";

    static final String EXTRA_BT_OPP_ADDRESS =
            "android.btopp.intent.extra.BT_OPP_ADDRESS";

    static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0;

    static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1;

    static final String EXTRA_BT_OPP_TRANSFER_DIRECTION =
            "android.btopp.intent.extra.BT_OPP_TRANSFER_DIRECTION";

    static final int DIRECTION_BLUETOOTH_INCOMING = 0;

    static final int DIRECTION_BLUETOOTH_OUTGOING = 1;

    static final String EXTRA_BT_OPP_TRANSFER_ID =
            "android.btopp.intent.extra.BT_OPP_TRANSFER_ID";

    static final String EXTRA_BT_OPP_TRANSFER_PROGRESS =
            "android.btopp.intent.extra.BT_OPP_TRANSFER_PROGRESS";

    static final String EXTRA_BT_OPP_TRANSFER_URI =
            "android.btopp.intent.extra.BT_OPP_TRANSFER_URI";

    // permission needed to be able to receive handover status requests
    static final String HANDOVER_STATUS_PERMISSION =
            "com.android.permission.HANDOVER_STATUS";

    // Variables below only accessed on main thread
    final Queue<BluetoothOppHandover> mPendingOutTransfers;
    final HashMap<Pair<String, Boolean>, HandoverTransfer> mTransfers;
    final Messenger mMessenger;

    SoundPool mSoundPool;
    int mSuccessSound;

    BluetoothAdapter mBluetoothAdapter;
    Messenger mClient;
    Handler mHandler;
    BluetoothHeadsetHandover mBluetoothHeadsetHandover;
    boolean mBluetoothHeadsetConnected;
    boolean mBluetoothEnabledByNfc;

    public HandoverService() {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mPendingOutTransfers = new LinkedList<BluetoothOppHandover>();
        mTransfers = new HashMap<Pair<String, Boolean>, HandoverTransfer>();
        mHandler = new MessageHandler();
        mMessenger = new Messenger(mHandler);
        mBluetoothHeadsetConnected = false;
        mBluetoothEnabledByNfc = false;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0);
        mSuccessSound = mSoundPool.load(this, R.raw.end, 1);

        IntentFilter filter = new IntentFilter(ACTION_BT_OPP_TRANSFER_DONE);
        filter.addAction(ACTION_BT_OPP_TRANSFER_PROGRESS);
        filter.addAction(ACTION_CANCEL_HANDOVER_TRANSFER);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mReceiver, filter, HANDOVER_STATUS_PERMISSION, mHandler);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mSoundPool != null) {
            mSoundPool.release();
        }
        unregisterReceiver(mReceiver);
    }

    void doOutgoingTransfer(Message msg) {
        Bundle msgData = msg.getData();

        msgData.setClassLoader(getClassLoader());
        PendingHandoverTransfer pendingTransfer = (PendingHandoverTransfer)
                msgData.getParcelable(BUNDLE_TRANSFER);
        createHandoverTransfer(pendingTransfer);

        // Create the actual transfer
        BluetoothOppHandover handover = new BluetoothOppHandover(HandoverService.this,
                pendingTransfer.remoteDevice, pendingTransfer.uris,
                pendingTransfer.remoteActivating);
        if (mBluetoothAdapter.isEnabled()) {
            // Start the transfer
            handover.start();
        } else {
            if (!enableBluetooth()) {
                Log.e(TAG, "Error enabling Bluetooth.");
                notifyClientTransferComplete(pendingTransfer.id);
                return;
            }
            mPendingOutTransfers.add(handover);
            // Queue the transfer and enable Bluetooth - when it is enabled
            // the transfer will be started.
        }
    }

    void doIncomingTransfer(Message msg) {
        Bundle msgData = msg.getData();

        msgData.setClassLoader(getClassLoader());
        PendingHandoverTransfer pendingTransfer = (PendingHandoverTransfer)
                msgData.getParcelable(BUNDLE_TRANSFER);
        if (!mBluetoothAdapter.isEnabled() && !enableBluetooth()) {
            Log.e(TAG, "Error enabling Bluetooth.");
            notifyClientTransferComplete(pendingTransfer.id);
            return;
        }
        createHandoverTransfer(pendingTransfer);
        // Remote device will connect and finish the transfer
    }

    void doHeadsetHandover(Message msg) {
        Bundle msgData = msg.getData();
        BluetoothDevice device = (BluetoothDevice) msgData.getParcelable(EXTRA_HEADSET_DEVICE);
        String name = (String) msgData.getString(EXTRA_HEADSET_NAME);
        mBluetoothHeadsetHandover = new BluetoothHeadsetHandover(HandoverService.this,
                device, name, HandoverService.this);
        if (mBluetoothAdapter.isEnabled()) {
            mBluetoothHeadsetHandover.start();
        } else {
            // Once BT is enabled, the headset pairing will be started
            if (!enableBluetooth()) {
                Log.e(TAG, "Error enabling Bluetooth.");
                mBluetoothHeadsetHandover = null;
            }
        }
    }

    void startPendingTransfers() {
        while (!mPendingOutTransfers.isEmpty()) {
             BluetoothOppHandover handover = mPendingOutTransfers.remove();
             handover.start();
        }
    }

    class MessageHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_REGISTER_CLIENT:
                    mClient = msg.replyTo;
                    break;
                case MSG_DEREGISTER_CLIENT:
                    mClient = null;
                    break;
                case MSG_START_INCOMING_TRANSFER:
                    doIncomingTransfer(msg);
                    break;
                case MSG_START_OUTGOING_TRANSFER:
                    doOutgoingTransfer(msg);
                    break;
                case MSG_HEADSET_HANDOVER:
                    doHeadsetHandover(msg);
                    break;
            }
        }
    }

    boolean enableBluetooth() {
        if (!mBluetoothAdapter.isEnabled()) {
            mBluetoothEnabledByNfc = true;
            return mBluetoothAdapter.enableNoAutoConnect();
        }
        return true;
    }

    void disableBluetoothIfNeeded() {
        if (!mBluetoothEnabledByNfc) return;

        if (mTransfers.size() == 0 && !mBluetoothHeadsetConnected) {
            mBluetoothAdapter.disable();
            mBluetoothEnabledByNfc = false;
        }
    }

    void createHandoverTransfer(PendingHandoverTransfer pendingTransfer) {
        Pair<String, Boolean> key = new Pair<String, Boolean>(
                pendingTransfer.remoteDevice.getAddress(), pendingTransfer.incoming);
        if (mTransfers.containsKey(key)) {
            HandoverTransfer transfer = mTransfers.get(key);
            if (!transfer.isRunning()) {
                mTransfers.remove(key); // new one created below
            } else {
                // There is already a transfer running to this
                // device - it will automatically get combined
                // with the existing transfer.
                notifyClientTransferComplete(pendingTransfer.id);
                return;
            }
        }

        HandoverTransfer transfer = new HandoverTransfer(this, this, pendingTransfer);
        mTransfers.put(key, transfer);
        transfer.updateNotification();
    }

    HandoverTransfer findHandoverTransfer(String sourceAddress, boolean incoming) {
        Pair<String, Boolean> key = new Pair<String, Boolean>(sourceAddress, incoming);
        if (mTransfers.containsKey(key)) {
            HandoverTransfer transfer = mTransfers.get(key);
            if (transfer.isRunning()) {
                return transfer;
            }
        }
        return null;
    }

    @Override
    public IBinder onBind(Intent intent) {
       return mMessenger.getBinder();
    }

    final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                if (state == BluetoothAdapter.STATE_ON) {
                    // If there is a pending headset pairing, start it
                    if (mBluetoothHeadsetHandover != null &&
                            !mBluetoothHeadsetHandover.hasStarted()) {
                        mBluetoothHeadsetHandover.start();
                    }

                    // Start any pending transfers
                    startPendingTransfers();
                } else if (state == BluetoothAdapter.STATE_OFF) {
                    mBluetoothEnabledByNfc = false;
                    mBluetoothHeadsetConnected = false;
                }
            }
            else if (action.equals(ACTION_CANCEL_HANDOVER_TRANSFER)) {
                String sourceAddress = intent.getStringExtra(EXTRA_SOURCE_ADDRESS);
                HandoverTransfer transfer = findHandoverTransfer(sourceAddress, true);
                if (transfer != null) {
                    transfer.cancel();
                }
            } else if (action.equals(ACTION_BT_OPP_TRANSFER_PROGRESS) ||
                    action.equals(ACTION_BT_OPP_TRANSFER_DONE)) {
                int direction = intent.getIntExtra(EXTRA_BT_OPP_TRANSFER_DIRECTION, -1);
                int id = intent.getIntExtra(EXTRA_BT_OPP_TRANSFER_ID, -1);
                String sourceAddress = intent.getStringExtra(EXTRA_BT_OPP_ADDRESS);

                if (direction == -1 || id == -1 || sourceAddress == null) return;
                boolean incoming = (direction == DIRECTION_BLUETOOTH_INCOMING);

                HandoverTransfer transfer = findHandoverTransfer(sourceAddress, incoming);
                if (transfer == null) {
                    // There is no transfer running for this source address; most likely
                    // the transfer was cancelled. We need to tell BT OPP to stop transferring
                    // in case this was an incoming transfer
                    Intent cancelIntent = new Intent("android.btopp.intent.action.STOP_HANDOVER_TRANSFER");
                    cancelIntent.putExtra(EXTRA_BT_OPP_TRANSFER_ID, id);
                    sendBroadcast(cancelIntent);
                    return;
                }

                if (action.equals(ACTION_BT_OPP_TRANSFER_DONE)) {
                    int handoverStatus = intent.getIntExtra(EXTRA_BT_OPP_TRANSFER_STATUS,
                            HANDOVER_TRANSFER_STATUS_FAILURE);
                    if (handoverStatus == HANDOVER_TRANSFER_STATUS_SUCCESS) {
                        String uriString = intent.getStringExtra(EXTRA_BT_OPP_TRANSFER_URI);
                        String mimeType = intent.getStringExtra(EXTRA_BT_OPP_TRANSFER_MIMETYPE);
                        Uri uri = Uri.parse(uriString);
                        if (uri.getScheme() == null) {
                            uri = Uri.fromFile(new File(uri.getPath()));
                        }
                        transfer.finishTransfer(true, uri, mimeType);
                    } else {
                        transfer.finishTransfer(false, null, null);
                    }
                } else if (action.equals(ACTION_BT_OPP_TRANSFER_PROGRESS)) {
                    float progress = intent.getFloatExtra(EXTRA_BT_OPP_TRANSFER_PROGRESS, 0.0f);
                    transfer.updateFileProgress(progress);
                }
            }
        }
    };

    void notifyClientTransferComplete(int transferId) {
        if (mClient != null) {
            Message msg = Message.obtain(null, HandoverManager.MSG_HANDOVER_COMPLETE);
            msg.arg1 = transferId;
            try {
                mClient.send(msg);
            } catch (RemoteException e) {
                // Ignore
            }
        }
    }

    @Override
    public void onTransferComplete(HandoverTransfer transfer, boolean success) {
        // Called on the main thread

        // First, remove the transfer from our list
        Iterator it = mTransfers.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry hashPair = (Map.Entry)it.next();
            HandoverTransfer transferEntry = (HandoverTransfer) hashPair.getValue();
            if (transferEntry == transfer) {
                it.remove();
            }
        }

        // Notify any clients of the service
        notifyClientTransferComplete(transfer.getTransferId());

        // Play success sound
        if (success) {
            mSoundPool.play(mSuccessSound, 1.0f, 1.0f, 0, 0, 1.0f);
        }
        disableBluetoothIfNeeded();
    }

    @Override
    public void onBluetoothHeadsetHandoverComplete(boolean connected) {
        // Called on the main thread
        mBluetoothHeadsetHandover = null;
        mBluetoothHeadsetConnected = connected;
        if (mClient != null) {
            Message msg = Message.obtain(null,
                    connected ? HandoverManager.MSG_HEADSET_CONNECTED
                              : HandoverManager.MSG_HEADSET_NOT_CONNECTED);
            try {
                mClient.send(msg);
            } catch (RemoteException e) {
                // Ignore
            }
        }
        disableBluetoothIfNeeded();
    }
}