BluetoothOppReceiver.java revision a930b6831d0c70b6c5d34e548e6b1dceaa6529a0
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 V = Constants.VERBOSE;
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(BluetoothAdapter.ACTION_STATE_CHANGED)) {
67            if (BluetoothAdapter.STATE_ON == intent.getIntExtra(
68                    BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)) {
69                if (V) Log.v(TAG, "Received BLUETOOTH_STATE_CHANGED_ACTION, BLUETOOTH_STATE_ON");
70                context.startService(new Intent(context, BluetoothOppService.class));
71
72                // If this is within a sending process, continue the handle
73                // logic to display device picker dialog.
74                synchronized (this) {
75                    if (BluetoothOppManager.getInstance(context).mSendingFlag) {
76                        // reset the flags
77                        BluetoothOppManager.getInstance(context).mSendingFlag = false;
78
79                        Intent in1 = new Intent(BluetoothDevicePicker.ACTION_LAUNCH);
80                        in1.putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
81                        in1.putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
82                                BluetoothDevicePicker.FILTER_TYPE_TRANSFER);
83                        in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_PACKAGE,
84                                Constants.THIS_PACKAGE_NAME);
85                        in1.putExtra(BluetoothDevicePicker.EXTRA_LAUNCH_CLASS,
86                                BluetoothOppReceiver.class.getName());
87
88                        in1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
89                        context.startActivity(in1);
90                    }
91                }
92            }
93        } else if (action.equals(BluetoothDevicePicker.ACTION_DEVICE_SELECTED)) {
94            BluetoothOppManager mOppManager = BluetoothOppManager.getInstance(context);
95
96            BluetoothDevice remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
97
98            if (V) Log.v(TAG, "Received BT device selected intent, bt device: " + remoteDevice);
99
100            // Insert transfer session record to database
101            mOppManager.startTransfer(remoteDevice);
102
103            // Display toast message
104            String deviceName = mOppManager.getDeviceName(remoteDevice);
105            String toastMsg;
106            int batchSize = mOppManager.getBatchSize();
107            if (mOppManager.mMultipleFlag) {
108                toastMsg = context.getString(R.string.bt_toast_5, Integer.toString(batchSize),
109                        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_OPEN_OUTBOUND_TRANSFER)) {
172            if (V) Log.v(TAG, "Received ACTION_OPEN_OUTBOUND_TRANSFER.");
173
174            Intent in = new Intent(context, BluetoothOppTransferHistory.class);
175            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
176            in.putExtra("direction", BluetoothShare.DIRECTION_OUTBOUND);
177            context.startActivity(in);
178        } else if (action.equals(Constants.ACTION_OPEN_INBOUND_TRANSFER)) {
179            if (V) Log.v(TAG, "Received ACTION_OPEN_INBOUND_TRANSFER.");
180
181            Intent in = new Intent(context, BluetoothOppTransferHistory.class);
182            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
183            in.putExtra("direction", BluetoothShare.DIRECTION_INBOUND);
184            context.startActivity(in);
185        } else if (action.equals(Constants.ACTION_HIDE)) {
186            if (V) Log.v(TAG, "Receiver hide for " + intent.getData());
187            Cursor cursor = context.getContentResolver().query(intent.getData(), null, null, null,
188                    null);
189            if (cursor != null) {
190                if (cursor.moveToFirst()) {
191                    int statusColumn = cursor.getColumnIndexOrThrow(BluetoothShare.STATUS);
192                    int status = cursor.getInt(statusColumn);
193                    int visibilityColumn = cursor.getColumnIndexOrThrow(BluetoothShare.VISIBILITY);
194                    int visibility = cursor.getInt(visibilityColumn);
195                    int userConfirmationColumn = cursor
196                            .getColumnIndexOrThrow(BluetoothShare.USER_CONFIRMATION);
197                    int userConfirmation = cursor.getInt(userConfirmationColumn);
198                    if (((userConfirmation == BluetoothShare.USER_CONFIRMATION_PENDING))
199                            && visibility == BluetoothShare.VISIBILITY_VISIBLE) {
200                        ContentValues values = new ContentValues();
201                        values.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
202                        context.getContentResolver().update(intent.getData(), values, null, null);
203                        if (V) Log.v(TAG, "Action_hide received and db updated");
204                        }
205                }
206                cursor.close();
207            }
208        } else if (action.equals(Constants.ACTION_COMPLETE_HIDE)) {
209            if (V) Log.v(TAG, "Receiver ACTION_COMPLETE_HIDE");
210            ContentValues updateValues = new ContentValues();
211            updateValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
212            context.getContentResolver().update(BluetoothShare.CONTENT_URI, updateValues,
213                    BluetoothOppNotification.WHERE_COMPLETED, null);
214        } else if (action.equals(BluetoothShare.TRANSFER_COMPLETED_ACTION)) {
215            if (V) Log.v(TAG, "Receiver Transfer Complete Intent for " + intent.getData());
216
217            String toastMsg = null;
218            BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo();
219            transInfo = BluetoothOppUtility.queryRecord(context, intent.getData());
220            if (transInfo == null) {
221                Log.e(TAG, "Error: Can not get data from db");
222                return;
223            }
224
225            if (BluetoothShare.isStatusSuccess(transInfo.mStatus)) {
226                if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) {
227                    toastMsg = context.getString(R.string.notification_sent, transInfo.mFileName);
228                } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) {
229                    toastMsg = context.getString(R.string.notification_received,
230                            transInfo.mFileName);
231                }
232
233            } else if (BluetoothShare.isStatusError(transInfo.mStatus)) {
234                if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) {
235                    toastMsg = context.getString(R.string.notification_sent_fail,
236                            transInfo.mFileName);
237                } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) {
238                    toastMsg = context.getString(R.string.download_fail_line1);
239                }
240            }
241            if (V) Log.v(TAG, "Toast msg == " + toastMsg);
242            if (toastMsg != null) {
243                Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
244            }
245        }
246    }
247}
248