18099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen/*
28099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen * Copyright (C) 2012 The Android Open Source Project
38099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen *
48099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen * Licensed under the Apache License, Version 2.0 (the "License");
58099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen * you may not use this file except in compliance with the License.
68099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen * You may obtain a copy of the License at
78099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen *
88099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen *      http://www.apache.org/licenses/LICENSE-2.0
98099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen *
108099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen * Unless required by applicable law or agreed to in writing, software
118099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen * distributed under the License is distributed on an "AS IS" BASIS,
128099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen * See the License for the specific language governing permissions and
148099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen * limitations under the License.
158099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen */
168099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen
178099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenenpackage com.android.bluetooth.opp;
188099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen
198099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenenimport android.bluetooth.BluetoothDevice;
208099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenenimport android.content.BroadcastReceiver;
218099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenenimport android.content.Context;
228099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenenimport android.content.Intent;
23584dfb2c53b45080347e412b3f7ed7fd1c9b53e7Martijn Coenenimport android.net.Uri;
248099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenenimport android.util.Log;
258099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen
2603f25a2055e51bcc5e8870642763b502956b6830Martijn Coenenimport java.util.ArrayList;
2703f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen
288099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenenpublic class BluetoothOppHandoverReceiver extends BroadcastReceiver {
298099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen    public static final String TAG ="BluetoothOppHandoverReceiver";
308099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen    private static final boolean D = Constants.DEBUG;
318099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen    private static final boolean V = Constants.VERBOSE;
328099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen
338099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen    @Override
348099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen    public void onReceive(Context context, Intent intent) {
358099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen        String action = intent.getAction();
3603f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen
3703f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen        if (action.equals(Constants.ACTION_HANDOVER_SEND) ||
3803f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen               action.equals(Constants.ACTION_HANDOVER_SEND_MULTIPLE)) {
3903f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen
4003f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen            BluetoothDevice device =
4103f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                    (BluetoothDevice)intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
4203f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen            if (device == null) {
4303f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                if (D) Log.d(TAG, "No device attached to handover intent.");
4403f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                return;
4503f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen            }
4603f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen            if (action.equals(Constants.ACTION_HANDOVER_SEND)) {
4703f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                String type = intent.getType();
4803f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                Uri stream = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
4903f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                if (stream != null && type != null) {
5003f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                    // Save type/stream, will be used when adding transfer
5103f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                    // session to DB.
5203f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                    BluetoothOppManager.getInstance(context).saveSendingFileInfo(type,
5303f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                            stream.toString(), true);
5403f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                } else {
5503f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                    if (D) Log.d(TAG, "No mimeType or stream attached to handover request");
5603f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                }
5703f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen            } else if (action.equals(Constants.ACTION_HANDOVER_SEND_MULTIPLE)) {
5803f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                ArrayList<Uri> uris = new ArrayList<Uri>();
5903f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                String mimeType = intent.getType();
6003f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
6103f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                if (mimeType != null && uris != null) {
6203f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                    BluetoothOppManager.getInstance(context).saveSendingFileInfo(mimeType,
6303f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                            uris, true);
6403f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                } else {
6503f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                    if (D) Log.d(TAG, "No mimeType or stream attached to handover request");
6603f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                    return;
6703f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen                }
6803f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen            }
6903f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen            // we already know where to send to
7003f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen            BluetoothOppManager.getInstance(context).startTransfer(device);
7103f25a2055e51bcc5e8870642763b502956b6830Martijn Coenen        } else if (action.equals(Constants.ACTION_WHITELIST_DEVICE)) {
728099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen            BluetoothDevice device =
738099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen                    (BluetoothDevice)intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
748099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen            if (D) Log.d(TAG, "Adding " + device + " to whitelist");
758099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen            if (device == null) return;
768099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen            BluetoothOppManager.getInstance(context).addToWhitelist(device.getAddress());
77584dfb2c53b45080347e412b3f7ed7fd1c9b53e7Martijn Coenen        } else if (action.equals(Constants.ACTION_STOP_HANDOVER)) {
78584dfb2c53b45080347e412b3f7ed7fd1c9b53e7Martijn Coenen            int id = intent.getIntExtra(Constants.EXTRA_BT_OPP_TRANSFER_ID, -1);
79584dfb2c53b45080347e412b3f7ed7fd1c9b53e7Martijn Coenen            if (id != -1) {
80584dfb2c53b45080347e412b3f7ed7fd1c9b53e7Martijn Coenen                Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + id);
81584dfb2c53b45080347e412b3f7ed7fd1c9b53e7Martijn Coenen
82584dfb2c53b45080347e412b3f7ed7fd1c9b53e7Martijn Coenen                if (D) Log.d(TAG, "Stopping handover transfer with Uri " + contentUri);
83584dfb2c53b45080347e412b3f7ed7fd1c9b53e7Martijn Coenen                context.getContentResolver().delete(contentUri, null, null);
84584dfb2c53b45080347e412b3f7ed7fd1c9b53e7Martijn Coenen            }
858099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen        } else {
868099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen            if (D) Log.d(TAG, "Unknown action: " + action);
878099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen        }
888099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen    }
898099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen
908099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen}
91