BluetoothOppReceiver.java revision 1f0fc26568c1babf0c66d2c75812b72e894eb0de
1/*
2 * Copyright (c) 2008-2009, Motorola, Inc.
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * - Neither the name of the Motorola, Inc. nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33package com.android.bluetooth.opp;
34
35import com.android.bluetooth.R;
36
37import android.app.NotificationManager;
38import android.bluetooth.BluetoothAdapter;
39import android.bluetooth.BluetoothDevice;
40import android.bluetooth.BluetoothDevicePicker;
41import android.content.BroadcastReceiver;
42import android.content.ContentUris;
43import android.content.ContentValues;
44import android.content.Context;
45import android.content.Intent;
46import android.database.Cursor;
47import android.net.Uri;
48import android.util.Log;
49import android.widget.Toast;
50
51/**
52 * Receives and handles: system broadcasts; Intents from other applications;
53 * Intents from OppService; Intents from modules in Opp application layer.
54 */
55public class BluetoothOppReceiver extends BroadcastReceiver {
56    private static final String TAG = "BluetoothOppReceiver";
57    private static final boolean D = Constants.DEBUG;
58    private static final boolean V = Constants.VERBOSE;
59
60    @Override
61    public void onReceive(Context context, Intent intent) {
62        String action = intent.getAction();
63
64        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
65
66            context.startService(new Intent(context, BluetoothOppService.class));
67        } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
68            if (BluetoothAdapter.STATE_ON == intent.getIntExtra(
69                    BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
70                if (V) Log.v(TAG, "Received BLUETOOTH_STATE_CHANGED_ACTION, BLUETOOTH_STATE_ON");
71                context.startService(new Intent(context, BluetoothOppService.class));
72
73                // If this is within a sending process, continue the handle
74                // logic to display device picker dialog.
75                synchronized (this) {
76                    if (BluetoothOppManager.getInstance(context).mSendingFlag) {
77                        // reset the flags
78                        BluetoothOppManager.getInstance(context).mSendingFlag = false;
79
80                        Intent in1 = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);
81                        in1.putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
82                        in1.putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
83                                BluetoothDevicePicker.FILTER_TYPE_TRANSFER);
84                        in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE,
85                                Constants.THIS_PACKAGE_NAME);
86                        in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS,
87                                BluetoothOppReceiver.class.getName());
88
89                        in1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
90                        context.startActivity(in1);
91                    }
92                }
93            }
94        } else if (action.equals(BluetoothDevicePicker.ACTION_DEVICE_SELECTED)) {
95            BluetoothOppManager mOppManager = BluetoothOppManager.getInstance(context);
96
97            BluetoothDevice remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
98
99            if (V) Log.v(TAG, "Received BT device selected intent, bt device: " + remoteDevice);
100
101            // Insert transfer session record to database
102            mOppManager.startTransfer(remoteDevice);
103
104            // Display toast message
105            String deviceName = mOppManager.getDeviceName(remoteDevice);
106            String toastMsg;
107            if (mOppManager.mMultipleFlag) {
108                toastMsg = context.getString(R.string.bt_toast_5, Integer
109                        .toString(mOppManager.mfileNumInBatch), deviceName);
110            } else {
111                toastMsg = context.getString(R.string.bt_toast_4, deviceName);
112            }
113            Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
114        } else if (action.equals(Constants.ACTION_INCOMING_FILE_CONFIRM)) {
115            if (V) Log.v(TAG, "Receiver ACTION_INCOMING_FILE_CONFIRM");
116
117            Uri uri = intent.getData();
118            Intent in = new Intent(context, BluetoothOppIncomingFileConfirmActivity.class);
119            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
120            in.setData(uri);
121            context.startActivity(in);
122
123            NotificationManager notMgr = (NotificationManager)context
124                    .getSystemService(Context.NOTIFICATION_SERVICE);
125            if (notMgr != null) {
126                notMgr.cancel((int)ContentUris.parseId(intent.getData()));
127                if (V) Log.v(TAG, "notMgr.cancel called");
128            }
129        } else if (action.equals(BluetoothShare.INCOMING_FILE_CONFIRMATION_REQUEST_ACTION)) {
130            if (V) Log.v(TAG, "Receiver INCOMING_FILE_NOTIFICATION");
131
132            Toast.makeText(context, context.getString(R.string.incoming_file_toast_msg),
133                    Toast.LENGTH_SHORT).show();
134
135        } else if (action.equals(Constants.ACTION_OPEN) || action.equals(Constants.ACTION_LIST)) {
136            if (V) {
137                if (action.equals(Constants.ACTION_OPEN)) {
138                    Log.v(TAG, "Receiver open for " + intent.getData());
139                } else {
140                    Log.v(TAG, "Receiver list for " + intent.getData());
141                }
142            }
143
144            BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo();
145            Uri uri = intent.getData();
146            transInfo = BluetoothOppUtility.queryRecord(context, uri);
147            if (transInfo == null) {
148                Log.e(TAG, "Error: Can not get data from db");
149                return;
150            }
151
152            if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND
153                    && BluetoothShare.isStatusSuccess(transInfo.mStatus)) {
154                // if received file successfully, open this file
155                BluetoothOppUtility.openReceivedFile(context, transInfo.mFileName,
156                        transInfo.mFileType, transInfo.mTimeStamp, uri);
157                BluetoothOppUtility.updateVisibilityToHidden(context, uri);
158            } else {
159                Intent in = new Intent(context, BluetoothOppTransferActivity.class);
160                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
161                in.setData(uri);
162                context.startActivity(in);
163            }
164
165            NotificationManager notMgr = (NotificationManager)context
166                    .getSystemService(Context.NOTIFICATION_SERVICE);
167            if (notMgr != null) {
168                notMgr.cancel((int)ContentUris.parseId(intent.getData()));
169                if (V) Log.v(TAG, "notMgr.cancel called");
170                }
171        } else if (action.equals(Constants.ACTION_HIDE)) {
172            if (V) Log.v(TAG, "Receiver hide for " + intent.getData());
173            Cursor cursor = context.getContentResolver().query(intent.getData(), null, null, null,
174                    null);
175            if (cursor != null) {
176                if (cursor.moveToFirst()) {
177                    int statusColumn = cursor.getColumnIndexOrThrow(BluetoothShare.STATUS);
178                    int status = cursor.getInt(statusColumn);
179                    int visibilityColumn = cursor.getColumnIndexOrThrow(BluetoothShare.VISIBILITY);
180                    int visibility = cursor.getInt(visibilityColumn);
181                    int userConfirmationColumn = cursor
182                            .getColumnIndexOrThrow(BluetoothShare.USER_CONFIRMATION);
183                    int userConfirmation = cursor.getInt(userConfirmationColumn);
184                    if ((BluetoothShare.isStatusCompleted(status) || (userConfirmation == BluetoothShare.USER_CONFIRMATION_PENDING))
185                            && visibility == BluetoothShare.VISIBILITY_VISIBLE) {
186                        ContentValues values = new ContentValues();
187                        values.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
188                        context.getContentResolver().update(intent.getData(), values, null, null);
189                        if (V) Log.v(TAG, "Action_hide received and db updated");
190                        }
191                }
192                cursor.close();
193            }
194        } else if (action.equals(BluetoothShare.TRANSFER_COMPLETED_ACTION)) {
195            if (V) Log.v(TAG, "Receiver Transfer Complete Intent for " + intent.getData());
196
197            String toastMsg = null;
198            BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo();
199            transInfo = BluetoothOppUtility.queryRecord(context, intent.getData());
200            if (transInfo == null) {
201                Log.e(TAG, "Error: Can not get data from db");
202                return;
203            }
204
205            if (BluetoothShare.isStatusSuccess(transInfo.mStatus)) {
206                if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) {
207                    toastMsg = context.getString(R.string.notification_sent, transInfo.mFileName);
208                } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) {
209                    toastMsg = context.getString(R.string.notification_received,
210                            transInfo.mFileName);
211                }
212
213            } else if (BluetoothShare.isStatusError(transInfo.mStatus)) {
214                if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) {
215                    toastMsg = context.getString(R.string.notification_sent_fail,
216                            transInfo.mFileName);
217                } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) {
218                    toastMsg = context.getString(R.string.download_fail_line1);
219                }
220            }
221            if (V) Log.v(TAG, "Toast msg == " + toastMsg);
222            if (toastMsg != null) {
223                Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
224            }
225        }
226    }
227}
228