BluetoothOppHandoverReceiver.java revision 03f25a2055e51bcc5e8870642763b502956b6830
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            if (action.equals(Constants.ACTION_HANDOVER_SEND)) {
47                String type = intent.getType();
48                Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
49                if (stream != null && type != null) {
50                    // Save type/stream, will be used when adding transfer
51                    // session to DB.
52                    BluetoothOppManager.getInstance(context).saveSendingFileInfo(type,
53                            stream.toString(), true);
54                } else {
55                    if (D) Log.d(TAG, "No mimeType or stream attached to handover request");
56                }
57            } else if (action.equals(Constants.ACTION_HANDOVER_SEND_MULTIPLE)) {
58                ArrayList<Uri> uris = new ArrayList<Uri>();
59                String mimeType = intent.getType();
60                uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
61                if (mimeType != null && uris != null) {
62                    BluetoothOppManager.getInstance(context).saveSendingFileInfo(mimeType,
63                            uris, true);
64                } else {
65                    if (D) Log.d(TAG, "No mimeType or stream attached to handover request");
66                    return;
67                }
68            }
69            // we already know where to send to
70            BluetoothOppManager.getInstance(context).startTransfer(device);
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