BluetoothOppReceiver.java revision 239bc526513429995c61c4148c105725c395b1a9
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.BluetoothDevice;
39import android.bluetooth.BluetoothError;
40import android.bluetooth.BluetoothIntent;
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
58    @Override
59    public void onReceive(Context context, Intent intent) {
60        String action = intent.getAction();
61
62        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
63
64            context.startService(new Intent(context, BluetoothOppService.class));
65        } else if (action.equals(BluetoothIntent.BLUETOOTH_STATE_CHANGED_ACTION)) {
66            if (BluetoothDevice.BLUETOOTH_STATE_ON == intent.getIntExtra(
67                    BluetoothIntent.BLUETOOTH_STATE, BluetoothError.ERROR)) {
68                if (Constants.LOGVV) {
69                    Log.v(TAG, "Received BLUETOOTH_STATE_CHANGED_ACTION, BLUETOOTH_STATE_ON");
70                }
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(context, BluetoothDevicePickerActivity.class);
81                        in1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
82                        context.startActivity(in1);
83                    }
84                }
85            }
86        } else if (action.equals(BluetoothShare.BLUETOOTH_DEVICE_SELECTED_ACTION)) {
87            BluetoothOppManager mOppManager = BluetoothOppManager.getInstance(context);
88
89            String btAddr = (String)intent.getStringExtra("BT_ADDRESS");
90
91            if (Constants.LOGVV) {
92                Log.v(TAG, "Received BT device selected intent, bt addr: " + btAddr);
93            }
94
95            // Insert transfer session record to database
96            mOppManager.startTransfer(btAddr);
97
98            // Display toast message
99            String deviceName = mOppManager.getDeviceName(btAddr);
100            String toastMsg;
101            if (mOppManager.mMultipleFlag) {
102                toastMsg = context.getString(R.string.bt_toast_5, Integer
103                        .toString(mOppManager.mfileNumInBatch), deviceName);
104            } else {
105                toastMsg = context.getString(R.string.bt_toast_4, deviceName);
106            }
107            Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
108        } else if (action.equals(Constants.ACTION_INCOMING_FILE_CONFIRM)) {
109            if (Constants.LOGVV) {
110                Log.v(TAG, "Receiver ACTION_INCOMING_FILE_CONFIRM");
111            }
112
113            Uri uri = intent.getData();
114            Intent in = new Intent(context, BluetoothOppIncomingFileConfirmActivity.class);
115            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
116            in.setData(uri);
117            context.startActivity(in);
118
119            NotificationManager notMgr = (NotificationManager)context
120                    .getSystemService(Context.NOTIFICATION_SERVICE);
121            if (notMgr != null) {
122                notMgr.cancel((int)ContentUris.parseId(intent.getData()));
123                Log.v(TAG, "notMgr.cancel called");
124            }
125        } else if (action.equals(BluetoothShare.INCOMING_FILE_CONFIRMATION_REQUEST_ACTION)) {
126            if (Constants.LOGVV) {
127                Log.v(TAG, "Receiver INCOMING_FILE_NOTIFICATION");
128            }
129
130            Toast.makeText(context, context.getString(R.string.incoming_file_toast_msg),
131                    Toast.LENGTH_SHORT).show();
132
133        } else if (action.equals(Constants.ACTION_OPEN) || action.equals(Constants.ACTION_LIST)) {
134            if (Constants.LOGVV) {
135                if (action.equals(Constants.ACTION_OPEN)) {
136                    Log.v(TAG, "Receiver open for " + intent.getData());
137                } else {
138                    Log.v(TAG, "Receiver list for " + intent.getData());
139                }
140            }
141
142            BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo();
143            Uri uri = intent.getData();
144            transInfo = BluetoothOppUtility.queryRecord(context, uri);
145            if (transInfo == null) {
146                if (Constants.LOGVV) {
147                    Log.e(TAG, "Error: Can not get data from db");
148                }
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);
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                Log.v(TAG, "notMgr.cancel called");
170            }
171        } else if (action.equals(Constants.ACTION_HIDE)) {
172            if (Constants.LOGVV) {
173                Log.v(TAG, "Receiver hide for " + intent.getData());
174            }
175            Cursor cursor = context.getContentResolver().query(intent.getData(), null, null, null,
176                    null);
177            if (cursor != null) {
178                if (cursor.moveToFirst()) {
179                    int statusColumn = cursor.getColumnIndexOrThrow(BluetoothShare.STATUS);
180                    int status = cursor.getInt(statusColumn);
181                    int visibilityColumn = cursor.getColumnIndexOrThrow(BluetoothShare.VISIBILITY);
182                    int visibility = cursor.getInt(visibilityColumn);
183                    int userConfirmationColumn = cursor
184                            .getColumnIndexOrThrow(BluetoothShare.USER_CONFIRMATION);
185                    int userConfirmation = cursor.getInt(userConfirmationColumn);
186                    if ((BluetoothShare.isStatusCompleted(status) || (userConfirmation == BluetoothShare.USER_CONFIRMATION_PENDING))
187                            && visibility == BluetoothShare.VISIBILITY_VISIBLE) {
188                        ContentValues values = new ContentValues();
189                        values.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
190                        context.getContentResolver().update(intent.getData(), values, null, null);
191                        if (Constants.LOGVV) {
192                            Log.v(TAG, "Action_hide received and db updated");
193                        }
194                    }
195                }
196                cursor.close();
197            }
198        } else if (action.equals(BluetoothShare.TRANSFER_COMPLETED_ACTION)) {
199            if (Constants.LOGVV) {
200                Log.v(TAG, "Receiver Transfer Complete Intent for " + intent.getData());
201            }
202
203            String toastMsg = null;
204            BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo();
205            transInfo = BluetoothOppUtility.queryRecord(context, intent.getData());
206            if (transInfo == null) {
207                if (Constants.LOGVV) {
208                    Log.e(TAG, "Error: Can not get data from db");
209                }
210                return;
211            }
212
213            if (BluetoothShare.isStatusSuccess(transInfo.mStatus)) {
214                if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) {
215                    toastMsg = context.getString(R.string.notification_sent, transInfo.mFileName);
216                } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) {
217                    toastMsg = context.getString(R.string.notification_received,
218                            transInfo.mFileName);
219                }
220
221            } else if (BluetoothShare.isStatusError(transInfo.mStatus)) {
222                if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) {
223                    toastMsg = context.getString(R.string.notification_sent_fail,
224                            transInfo.mFileName);
225                } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) {
226                    toastMsg = context.getString(R.string.download_fail_line1);
227                }
228            }
229            if (Constants.LOGVV) {
230                Log.v(TAG, "Toast msg == " + toastMsg);
231            }
232            if (toastMsg != null) {
233                Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
234            }
235        }
236    }
237}
238