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    private static final boolean V = Constants.VERBOSE;
32
33    @Override
34    public void onReceive(Context context, Intent intent) {
35        String action = intent.getAction();
36
37        if (action.equals(Constants.ACTION_HANDOVER_SEND) ||
38               action.equals(Constants.ACTION_HANDOVER_SEND_MULTIPLE)) {
39
40            BluetoothDevice device =
41                    (BluetoothDevice)intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
42            if (device == null) {
43                if (D) Log.d(TAG, "No device attached to handover intent.");
44                return;
45            }
46
47            String mimeType = intent.getType();
48            ArrayList<Uri> uris = new ArrayList<Uri>();
49            if (action.equals(Constants.ACTION_HANDOVER_SEND)) {
50                Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
51                if (stream != null) uris.add(stream);
52            } else if (action.equals(Constants.ACTION_HANDOVER_SEND_MULTIPLE)) {
53                uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
54            }
55
56            if (mimeType != null && uris != null && !uris.isEmpty()) {
57                final String finalType = mimeType;
58                final ArrayList<Uri> finalUris = uris;
59                Thread t = new Thread(new Runnable() {
60                    public void run() {
61                        BluetoothOppManager.getInstance(context).saveSendingFileInfo(finalType,
62                            finalUris, true);
63                        BluetoothOppManager.getInstance(context).startTransfer(device);
64                    }
65                });
66                t.start();
67            } else {
68                if (D) Log.d(TAG, "No mimeType or stream attached to handover request");
69                return;
70            }
71        } else if (action.equals(Constants.ACTION_WHITELIST_DEVICE)) {
72            BluetoothDevice device =
73                    (BluetoothDevice)intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
74            if (D) Log.d(TAG, "Adding " + device + " to whitelist");
75            if (device == null) return;
76            BluetoothOppManager.getInstance(context).addToWhitelist(device.getAddress());
77        } else if (action.equals(Constants.ACTION_STOP_HANDOVER)) {
78            int id = intent.getIntExtra(Constants.EXTRA_BT_OPP_TRANSFER_ID, -1);
79            if (id != -1) {
80                Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + id);
81
82                if (D) Log.d(TAG, "Stopping handover transfer with Uri " + contentUri);
83                context.getContentResolver().delete(contentUri, null, null);
84            }
85        } else {
86            if (D) Log.d(TAG, "Unknown action: " + action);
87        }
88    }
89
90}
91