BluetoothOppHandoverReceiver.java revision fe1c54e3fcbb75af7e49aecbd27ab7327a631c64
1/*
2 * Copyright (C) 2012 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.bluetooth.opp;
18
19import android.bluetooth.BluetoothDevice;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.net.Uri;
24import android.util.Log;
25
26import java.util.ArrayList;
27
28public class BluetoothOppHandoverReceiver extends BroadcastReceiver {
29    public static final String TAG ="BluetoothOppHandoverReceiver";
30    private static final boolean D = Constants.DEBUG;
31
32    @Override
33    public void onReceive(Context context, Intent intent) {
34        String action = intent.getAction();
35
36        if (action.equals(Constants.ACTION_HANDOVER_SEND) ||
37               action.equals(Constants.ACTION_HANDOVER_SEND_MULTIPLE)) {
38            final BluetoothDevice device =
39                    (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
40            if (device == null) {
41                if (D) Log.d(TAG, "No device attached to handover intent.");
42                return;
43            }
44
45            final String mimeType = intent.getType();
46            ArrayList<Uri> uris = new ArrayList<Uri>();
47            if (action.equals(Constants.ACTION_HANDOVER_SEND)) {
48                Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
49                if (stream != null) uris.add(stream);
50            } else if (action.equals(Constants.ACTION_HANDOVER_SEND_MULTIPLE)) {
51                uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
52            }
53
54            if (mimeType != null && uris != null && !uris.isEmpty()) {
55                final Context finalContext = context;
56                final ArrayList<Uri> finalUris = uris;
57                Thread t = new Thread(new Runnable() {
58                    public void run() {
59                        BluetoothOppManager.getInstance(finalContext)
60                                .saveSendingFileInfo(mimeType, finalUris, true /* isHandover */,
61                                        true /* fromExternal */);
62                        BluetoothOppManager.getInstance(finalContext).startTransfer(device);
63                    }
64                });
65                t.start();
66            } else {
67                if (D) Log.d(TAG, "No mimeType or stream attached to handover request");
68                return;
69            }
70        } else if (action.equals(Constants.ACTION_WHITELIST_DEVICE)) {
71            BluetoothDevice device =
72                    (BluetoothDevice)intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
73            if (D) Log.d(TAG, "Adding " + device + " to whitelist");
74            if (device == null) return;
75            BluetoothOppManager.getInstance(context).addToWhitelist(device.getAddress());
76        } else if (action.equals(Constants.ACTION_STOP_HANDOVER)) {
77            int id = intent.getIntExtra(Constants.EXTRA_BT_OPP_TRANSFER_ID, -1);
78            if (id != -1) {
79                Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + id);
80
81                if (D) Log.d(TAG, "Stopping handover transfer with Uri " + contentUri);
82                context.getContentResolver().delete(contentUri, null, null);
83            }
84        } else {
85            if (D) Log.d(TAG, "Unknown action: " + action);
86        }
87    }
88
89}
90