BluetoothOppReceiver.java revision 41ef8d494511c040451f2f887cb31c3100746b61
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.BluetoothError;
41import android.bluetooth.BluetoothIntent;
42import android.content.BroadcastReceiver;
43import android.content.ContentUris;
44import android.content.ContentValues;
45import android.content.Context;
46import android.content.Intent;
47import android.database.Cursor;
48import android.net.Uri;
49import android.util.Log;
50import android.widget.Toast;
51
52/**
53 * Receives and handles: system broadcasts; Intents from other applications;
54 * Intents from OppService; Intents from modules in Opp application layer.
55 */
56public class BluetoothOppReceiver extends BroadcastReceiver {
57    private static final String TAG = "BluetoothOppReceiver";
58
59    @Override
60    public void onReceive(Context context, Intent intent) {
61        String action = intent.getAction();
62
63        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
64
65            context.startService(new Intent(context, BluetoothOppService.class));
66        } else if (action.equals(BluetoothIntent.BLUETOOTH_STATE_CHANGED_ACTION)) {
67            if (BluetoothAdapter.BLUETOOTH_STATE_ON == intent.getIntExtra(
68                    BluetoothIntent.BLUETOOTH_STATE, BluetoothError.ERROR)) {
69                if (Constants.LOGVV) {
70                    Log.v(TAG, "Received BLUETOOTH_STATE_CHANGED_ACTION, BLUETOOTH_STATE_ON");
71                }
72                context.startService(new Intent(context, BluetoothOppService.class));
73
74                // If this is within a sending process, continue the handle
75                // logic to display device picker dialog.
76                synchronized (this) {
77                    if (BluetoothOppManager.getInstance(context).mSendingFlag) {
78                        // reset the flags
79                        BluetoothOppManager.getInstance(context).mSendingFlag = false;
80
81                        Intent in1 = new Intent(context, BluetoothDevicePickerActivity.class);
82                        in1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
83                        context.startActivity(in1);
84                    }
85                }
86            }
87        } else if (action.equals(BluetoothShare.BLUETOOTH_DEVICE_SELECTED_ACTION)) {
88            BluetoothOppManager mOppManager = BluetoothOppManager.getInstance(context);
89
90            BluetoothDevice remoteDevice = intent.getParcelableExtra("BT_DEVICE");
91
92            if (Constants.LOGVV) {
93                Log.v(TAG, "Received BT device selected intent, bt device: " + remoteDevice);
94            }
95
96            // Insert transfer session record to database
97            mOppManager.startTransfer(remoteDevice);
98
99            // Display toast message
100            String deviceName = mOppManager.getDeviceName(remoteDevice);
101            String toastMsg;
102            if (mOppManager.mMultipleFlag) {
103                toastMsg = context.getString(R.string.bt_toast_5, Integer
104                        .toString(mOppManager.mfileNumInBatch), deviceName);
105            } else {
106                toastMsg = context.getString(R.string.bt_toast_4, deviceName);
107            }
108            Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
109        } else if (action.equals(Constants.ACTION_INCOMING_FILE_CONFIRM)) {
110            if (Constants.LOGVV) {
111                Log.v(TAG, "Receiver ACTION_INCOMING_FILE_CONFIRM");
112            }
113
114            Uri uri = intent.getData();
115            Intent in = new Intent(context, BluetoothOppIncomingFileConfirmActivity.class);
116            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
117            in.setData(uri);
118            context.startActivity(in);
119
120            NotificationManager notMgr = (NotificationManager)context
121                    .getSystemService(Context.NOTIFICATION_SERVICE);
122            if (notMgr != null) {
123                notMgr.cancel((int)ContentUris.parseId(intent.getData()));
124                Log.v(TAG, "notMgr.cancel called");
125            }
126        } else if (action.equals(BluetoothShare.INCOMING_FILE_CONFIRMATION_REQUEST_ACTION)) {
127            if (Constants.LOGVV) {
128                Log.v(TAG, "Receiver INCOMING_FILE_NOTIFICATION");
129            }
130
131            Toast.makeText(context, context.getString(R.string.incoming_file_toast_msg),
132                    Toast.LENGTH_SHORT).show();
133
134        } else if (action.equals(Constants.ACTION_OPEN) || action.equals(Constants.ACTION_LIST)) {
135            if (Constants.LOGVV) {
136                if (action.equals(Constants.ACTION_OPEN)) {
137                    Log.v(TAG, "Receiver open for " + intent.getData());
138                } else {
139                    Log.v(TAG, "Receiver list for " + intent.getData());
140                }
141            }
142
143            BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo();
144            Uri uri = intent.getData();
145            transInfo = BluetoothOppUtility.queryRecord(context, uri);
146            if (transInfo == null) {
147                if (Constants.LOGVV) {
148                    Log.e(TAG, "Error: Can not get data from db");
149                }
150                return;
151            }
152
153            if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND
154                    && BluetoothShare.isStatusSuccess(transInfo.mStatus)) {
155                // if received file successfully, open this file
156                BluetoothOppUtility.openReceivedFile(context, transInfo.mFileName,
157                        transInfo.mFileType, transInfo.mTimeStamp);
158                BluetoothOppUtility.updateVisibilityToHidden(context, uri);
159            } else {
160                Intent in = new Intent(context, BluetoothOppTransferActivity.class);
161                in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
162                in.setData(uri);
163                context.startActivity(in);
164            }
165
166            NotificationManager notMgr = (NotificationManager)context
167                    .getSystemService(Context.NOTIFICATION_SERVICE);
168            if (notMgr != null) {
169                notMgr.cancel((int)ContentUris.parseId(intent.getData()));
170                if (Constants.LOGVV) {
171                    Log.v(TAG, "notMgr.cancel called");
172                }
173            }
174        } else if (action.equals(Constants.ACTION_HIDE)) {
175            if (Constants.LOGVV) {
176                Log.v(TAG, "Receiver hide for " + intent.getData());
177            }
178            Cursor cursor = context.getContentResolver().query(intent.getData(), null, null, null,
179                    null);
180            if (cursor != null) {
181                if (cursor.moveToFirst()) {
182                    int statusColumn = cursor.getColumnIndexOrThrow(BluetoothShare.STATUS);
183                    int status = cursor.getInt(statusColumn);
184                    int visibilityColumn = cursor.getColumnIndexOrThrow(BluetoothShare.VISIBILITY);
185                    int visibility = cursor.getInt(visibilityColumn);
186                    int userConfirmationColumn = cursor
187                            .getColumnIndexOrThrow(BluetoothShare.USER_CONFIRMATION);
188                    int userConfirmation = cursor.getInt(userConfirmationColumn);
189                    if ((BluetoothShare.isStatusCompleted(status) || (userConfirmation == BluetoothShare.USER_CONFIRMATION_PENDING))
190                            && visibility == BluetoothShare.VISIBILITY_VISIBLE) {
191                        ContentValues values = new ContentValues();
192                        values.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
193                        context.getContentResolver().update(intent.getData(), values, null, null);
194                        if (Constants.LOGVV) {
195                            Log.v(TAG, "Action_hide received and db updated");
196                        }
197                    }
198                }
199                cursor.close();
200            }
201        } else if (action.equals(BluetoothShare.TRANSFER_COMPLETED_ACTION)) {
202            if (Constants.LOGVV) {
203                Log.v(TAG, "Receiver Transfer Complete Intent for " + intent.getData());
204            }
205
206            String toastMsg = null;
207            BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo();
208            transInfo = BluetoothOppUtility.queryRecord(context, intent.getData());
209            if (transInfo == null) {
210                if (Constants.LOGVV) {
211                    Log.e(TAG, "Error: Can not get data from db");
212                }
213                return;
214            }
215
216            if (BluetoothShare.isStatusSuccess(transInfo.mStatus)) {
217                if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) {
218                    toastMsg = context.getString(R.string.notification_sent, transInfo.mFileName);
219                } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) {
220                    toastMsg = context.getString(R.string.notification_received,
221                            transInfo.mFileName);
222                }
223
224            } else if (BluetoothShare.isStatusError(transInfo.mStatus)) {
225                if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) {
226                    toastMsg = context.getString(R.string.notification_sent_fail,
227                            transInfo.mFileName);
228                } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) {
229                    toastMsg = context.getString(R.string.download_fail_line1);
230                }
231            }
232            if (Constants.LOGVV) {
233                Log.v(TAG, "Toast msg == " + toastMsg);
234            }
235            if (toastMsg != null) {
236                Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
237            }
238        }
239    }
240}
241