• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 package com.android.bluetooth.opp;
34 
35 import com.android.bluetooth.R;
36 
37 import android.app.NotificationManager;
38 import android.bluetooth.BluetoothAdapter;
39 import android.bluetooth.BluetoothDevice;
40 import android.bluetooth.BluetoothDevicePicker;
41 import android.content.BroadcastReceiver;
42 import android.content.ContentUris;
43 import android.content.ContentValues;
44 import android.content.Context;
45 import android.content.Intent;
46 import android.database.Cursor;
47 import android.net.Uri;
48 import android.util.Log;
49 import 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  */
55 public 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
onReceive(Context context, Intent intent)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             int batchSize = mOppManager.getBatchSize();
108             if (mOppManager.mMultipleFlag) {
109                 toastMsg = context.getString(R.string.bt_toast_5, Integer.toString(batchSize),
110                         deviceName);
111             } else {
112                 toastMsg = context.getString(R.string.bt_toast_4, deviceName);
113             }
114             Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
115         } else if (action.equals(Constants.ACTION_INCOMING_FILE_CONFIRM)) {
116             if (V) Log.v(TAG, "Receiver ACTION_INCOMING_FILE_CONFIRM");
117 
118             Uri uri = intent.getData();
119             Intent in = new Intent(context, BluetoothOppIncomingFileConfirmActivity.class);
120             in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
121             in.setData(uri);
122             context.startActivity(in);
123 
124             NotificationManager notMgr = (NotificationManager)context
125                     .getSystemService(Context.NOTIFICATION_SERVICE);
126             if (notMgr != null) {
127                 notMgr.cancel((int)ContentUris.parseId(intent.getData()));
128                 if (V) Log.v(TAG, "notMgr.cancel called");
129             }
130         } else if (action.equals(BluetoothShare.INCOMING_FILE_CONFIRMATION_REQUEST_ACTION)) {
131             if (V) Log.v(TAG, "Receiver INCOMING_FILE_NOTIFICATION");
132 
133             Toast.makeText(context, context.getString(R.string.incoming_file_toast_msg),
134                     Toast.LENGTH_SHORT).show();
135 
136         } else if (action.equals(Constants.ACTION_OPEN) || action.equals(Constants.ACTION_LIST)) {
137             if (V) {
138                 if (action.equals(Constants.ACTION_OPEN)) {
139                     Log.v(TAG, "Receiver open for " + intent.getData());
140                 } else {
141                     Log.v(TAG, "Receiver list for " + intent.getData());
142                 }
143             }
144 
145             BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo();
146             Uri uri = intent.getData();
147             transInfo = BluetoothOppUtility.queryRecord(context, uri);
148             if (transInfo == null) {
149                 Log.e(TAG, "Error: Can not get data from db");
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, uri);
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 (V) Log.v(TAG, "notMgr.cancel called");
171                 }
172         } else if (action.equals(Constants.ACTION_OPEN_OUTBOUND_TRANSFER)) {
173             if (V) Log.v(TAG, "Received ACTION_OPEN_OUTBOUND_TRANSFER.");
174 
175             Intent in = new Intent(context, BluetoothOppTransferHistory.class);
176             in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
177             in.putExtra("direction", BluetoothShare.DIRECTION_OUTBOUND);
178             context.startActivity(in);
179         } else if (action.equals(Constants.ACTION_OPEN_INBOUND_TRANSFER)) {
180             if (V) Log.v(TAG, "Received ACTION_OPEN_INBOUND_TRANSFER.");
181 
182             Intent in = new Intent(context, BluetoothOppTransferHistory.class);
183             in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
184             in.putExtra("direction", BluetoothShare.DIRECTION_INBOUND);
185             context.startActivity(in);
186         } else if (action.equals(Constants.ACTION_HIDE)) {
187             if (V) Log.v(TAG, "Receiver hide for " + intent.getData());
188             Cursor cursor = context.getContentResolver().query(intent.getData(), null, null, null,
189                     null);
190             if (cursor != null) {
191                 if (cursor.moveToFirst()) {
192                     int statusColumn = cursor.getColumnIndexOrThrow(BluetoothShare.STATUS);
193                     int status = cursor.getInt(statusColumn);
194                     int visibilityColumn = cursor.getColumnIndexOrThrow(BluetoothShare.VISIBILITY);
195                     int visibility = cursor.getInt(visibilityColumn);
196                     int userConfirmationColumn = cursor
197                             .getColumnIndexOrThrow(BluetoothShare.USER_CONFIRMATION);
198                     int userConfirmation = cursor.getInt(userConfirmationColumn);
199                     if (((userConfirmation == BluetoothShare.USER_CONFIRMATION_PENDING))
200                             && visibility == BluetoothShare.VISIBILITY_VISIBLE) {
201                         ContentValues values = new ContentValues();
202                         values.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
203                         context.getContentResolver().update(intent.getData(), values, null, null);
204                         if (V) Log.v(TAG, "Action_hide received and db updated");
205                         }
206                 }
207                 cursor.close();
208             }
209         } else if (action.equals(Constants.ACTION_COMPLETE_HIDE)) {
210             if (V) Log.v(TAG, "Receiver ACTION_COMPLETE_HIDE");
211             ContentValues updateValues = new ContentValues();
212             updateValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
213             context.getContentResolver().update(BluetoothShare.CONTENT_URI, updateValues,
214                     BluetoothOppNotification.WHERE_COMPLETED, null);
215         } else if (action.equals(BluetoothShare.TRANSFER_COMPLETED_ACTION)) {
216             if (V) Log.v(TAG, "Receiver Transfer Complete Intent for " + intent.getData());
217 
218             String toastMsg = null;
219             BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo();
220             transInfo = BluetoothOppUtility.queryRecord(context, intent.getData());
221             if (transInfo == null) {
222                 Log.e(TAG, "Error: Can not get data from db");
223                 return;
224             }
225 
226             if (BluetoothShare.isStatusSuccess(transInfo.mStatus)) {
227                 if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) {
228                     toastMsg = context.getString(R.string.notification_sent, transInfo.mFileName);
229                 } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) {
230                     toastMsg = context.getString(R.string.notification_received,
231                             transInfo.mFileName);
232                 }
233 
234             } else if (BluetoothShare.isStatusError(transInfo.mStatus)) {
235                 if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) {
236                     toastMsg = context.getString(R.string.notification_sent_fail,
237                             transInfo.mFileName);
238                 } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) {
239                     toastMsg = context.getString(R.string.download_fail_line1);
240                 }
241             }
242             if (V) Log.v(TAG, "Toast msg == " + toastMsg);
243             if (toastMsg != null) {
244                 Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
245             }
246         }
247     }
248 }
249