• 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 import com.google.android.collect.Lists;
37 
38 
39 import android.bluetooth.BluetoothAdapter;
40 import android.bluetooth.BluetoothDevice;
41 import android.net.Uri;
42 import android.content.ContentValues;
43 import android.content.Context;
44 import android.content.ActivityNotFoundException;
45 import android.content.Intent;
46 import android.content.pm.PackageManager;
47 import android.content.pm.ResolveInfo;
48 import android.database.Cursor;
49 import android.util.Log;
50 
51 import java.io.File;
52 import java.util.ArrayList;
53 import java.util.List;
54 
55 /**
56  * This class has some utilities for Opp application;
57  */
58 public class BluetoothOppUtility {
59     private static final String TAG = "BluetoothOppUtility";
60     private static final boolean D = Constants.DEBUG;
61     private static final boolean V = Constants.VERBOSE;
62 
queryRecord(Context context, Uri uri)63     public static BluetoothOppTransferInfo queryRecord(Context context, Uri uri) {
64         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
65         BluetoothOppTransferInfo info = new BluetoothOppTransferInfo();
66         Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
67         if (cursor != null) {
68             if (cursor.moveToFirst()) {
69                 info.mID = cursor.getInt(cursor.getColumnIndexOrThrow(BluetoothShare._ID));
70                 info.mStatus = cursor.getInt(cursor.getColumnIndexOrThrow(BluetoothShare.STATUS));
71                 info.mDirection = cursor.getInt(cursor
72                         .getColumnIndexOrThrow(BluetoothShare.DIRECTION));
73                 info.mTotalBytes = cursor.getInt(cursor
74                         .getColumnIndexOrThrow(BluetoothShare.TOTAL_BYTES));
75                 info.mCurrentBytes = cursor.getInt(cursor
76                         .getColumnIndexOrThrow(BluetoothShare.CURRENT_BYTES));
77                 info.mTimeStamp = cursor.getLong(cursor
78                         .getColumnIndexOrThrow(BluetoothShare.TIMESTAMP));
79                 info.mDestAddr = cursor.getString(cursor
80                         .getColumnIndexOrThrow(BluetoothShare.DESTINATION));
81 
82                 info.mFileName = cursor.getString(cursor
83                         .getColumnIndexOrThrow(BluetoothShare._DATA));
84                 if (info.mFileName == null) {
85                     info.mFileName = cursor.getString(cursor
86                             .getColumnIndexOrThrow(BluetoothShare.FILENAME_HINT));
87                 }
88                 if (info.mFileName == null) {
89                     info.mFileName = context.getString(R.string.unknown_file);
90                 }
91 
92                 info.mFileUri = cursor.getString(cursor.getColumnIndexOrThrow(BluetoothShare.URI));
93 
94                 if (info.mFileUri != null) {
95                     Uri u = Uri.parse(info.mFileUri);
96                     info.mFileType = context.getContentResolver().getType(u);
97                 } else {
98                     Uri u = Uri.parse(info.mFileName);
99                     info.mFileType = context.getContentResolver().getType(u);
100                 }
101                 if (info.mFileType == null) {
102                     info.mFileType = cursor.getString(cursor
103                             .getColumnIndexOrThrow(BluetoothShare.MIMETYPE));
104                 }
105 
106                 BluetoothDevice remoteDevice = adapter.getRemoteDevice(info.mDestAddr);
107                 info.mDeviceName =
108                         BluetoothOppManager.getInstance(context).getDeviceName(remoteDevice);
109 
110                 if (V) Log.v(TAG, "Get data from db:" + info.mFileName + info.mFileType
111                             + info.mDestAddr);
112             }
113             cursor.close();
114         } else {
115             info = null;
116             if (V) Log.v(TAG, "BluetoothOppManager Error: not got data from db for uri:" + uri);
117         }
118         return info;
119     }
120 
121     /**
122      * Organize Array list for transfers in one batch
123      */
124     // This function is used when UI show batch transfer. Currently only show single transfer.
queryTransfersInBatch(Context context, Long timeStamp)125     public static ArrayList<String> queryTransfersInBatch(Context context, Long timeStamp) {
126         ArrayList<String> uris = Lists.newArrayList();
127         final String WHERE = BluetoothShare.TIMESTAMP + " == " + timeStamp;
128 
129         Cursor metadataCursor = context.getContentResolver().query(BluetoothShare.CONTENT_URI,
130                 new String[] {
131                     BluetoothShare._DATA
132                 }, WHERE, null, BluetoothShare._ID);
133 
134         if (metadataCursor == null) {
135             return null;
136         }
137 
138         for (metadataCursor.moveToFirst(); !metadataCursor.isAfterLast(); metadataCursor
139                 .moveToNext()) {
140             String fileName = metadataCursor.getString(0);
141             Uri path = Uri.parse(fileName);
142             // If there is no scheme, then it must be a file
143             if (path.getScheme() == null) {
144                 path = Uri.fromFile(new File(fileName));
145             }
146             uris.add(path.toString());
147             if (V) Log.d(TAG, "Uri in this batch: " + path.toString());
148         }
149         metadataCursor.close();
150         return uris;
151     }
152 
153     /**
154      * Open the received file with appropriate application, if can not find
155      * application to handle, display error dialog.
156      */
openReceivedFile(Context context, String fileName, String mimetype, Long timeStamp, Uri uri)157     public static void openReceivedFile(Context context, String fileName, String mimetype,
158             Long timeStamp, Uri uri) {
159         if (fileName == null || mimetype == null) {
160             Log.e(TAG, "ERROR: Para fileName ==null, or mimetype == null");
161             return;
162         }
163 
164         File f = new File(fileName);
165         if (!f.exists()) {
166             Intent in = new Intent(context, BluetoothOppBtErrorActivity.class);
167             in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
168             in.putExtra("title", context.getString(R.string.not_exist_file));
169             in.putExtra("content", context.getString(R.string.not_exist_file_desc));
170             context.startActivity(in);
171 
172             // Due to the file is not existing, delete related info in btopp db
173             // to prevent this file from appearing in live folder
174             if (V) Log.d(TAG, "This uri will be deleted: " + uri);
175             context.getContentResolver().delete(uri, null, null);
176             return;
177         }
178 
179         Uri path = Uri.parse(fileName);
180         // If there is no scheme, then it must be a file
181         if (path.getScheme() == null) {
182             path = Uri.fromFile(new File(fileName));
183         }
184 
185         if (isRecognizedFileType(context, path, mimetype)) {
186             Intent activityIntent = new Intent(Intent.ACTION_VIEW);
187             activityIntent.setDataAndType(path, mimetype);
188 
189             activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
190             try {
191                 if (V) Log.d(TAG, "ACTION_VIEW intent sent out: " + path + " / " + mimetype);
192                 context.startActivity(activityIntent);
193             } catch (ActivityNotFoundException ex) {
194                 if (V) Log.d(TAG, "no activity for handling ACTION_VIEW intent:  " + mimetype, ex);
195             }
196         } else {
197             Intent in = new Intent(context, BluetoothOppBtErrorActivity.class);
198             in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
199             in.putExtra("title", context.getString(R.string.unknown_file));
200             in.putExtra("content", context.getString(R.string.unknown_file_desc));
201             context.startActivity(in);
202         }
203     }
204 
205     /**
206      * To judge if the file type supported (can be handled by some app) by phone
207      * system.
208      */
isRecognizedFileType(Context context, Uri fileUri, String mimetype)209     public static boolean isRecognizedFileType(Context context, Uri fileUri, String mimetype) {
210         boolean ret = true;
211 
212         if (D) Log.d(TAG, "RecognizedFileType() fileUri: " + fileUri + " mimetype: " + mimetype);
213 
214         Intent mimetypeIntent = new Intent(Intent.ACTION_VIEW);
215         mimetypeIntent.setDataAndType(fileUri, mimetype);
216         List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(mimetypeIntent,
217                 PackageManager.MATCH_DEFAULT_ONLY);
218 
219         if (list.size() == 0) {
220             if (D) Log.d(TAG, "NO application to handle MIME type " + mimetype);
221             ret = false;
222         }
223         return ret;
224     }
225 
226     /**
227      * update visibility to Hidden
228      */
updateVisibilityToHidden(Context context, Uri uri)229     public static void updateVisibilityToHidden(Context context, Uri uri) {
230         ContentValues updateValues = new ContentValues();
231         updateValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
232         context.getContentResolver().update(uri, updateValues, null, null);
233     }
234 
235     /**
236      * Helper function to build the progress text.
237      */
formatProgressText(long totalBytes, long currentBytes)238     public static String formatProgressText(long totalBytes, long currentBytes) {
239         if (totalBytes <= 0) {
240             return "0%";
241         }
242         long progress = currentBytes * 100 / totalBytes;
243         StringBuilder sb = new StringBuilder();
244         sb.append(progress);
245         sb.append('%');
246         return sb.toString();
247     }
248 
249     /**
250      * Get status description according to status code.
251      */
getStatusDescription(Context context, int statusCode, String deviceName)252     public static String getStatusDescription(Context context, int statusCode, String deviceName) {
253         String ret;
254         if (statusCode == BluetoothShare.STATUS_PENDING) {
255             ret = context.getString(R.string.status_pending);
256         } else if (statusCode == BluetoothShare.STATUS_RUNNING) {
257             ret = context.getString(R.string.status_running);
258         } else if (statusCode == BluetoothShare.STATUS_SUCCESS) {
259             ret = context.getString(R.string.status_success);
260         } else if (statusCode == BluetoothShare.STATUS_NOT_ACCEPTABLE) {
261             ret = context.getString(R.string.status_not_accept);
262         } else if (statusCode == BluetoothShare.STATUS_FORBIDDEN) {
263             ret = context.getString(R.string.status_forbidden);
264         } else if (statusCode == BluetoothShare.STATUS_CANCELED) {
265             ret = context.getString(R.string.status_canceled);
266         } else if (statusCode == BluetoothShare.STATUS_FILE_ERROR) {
267             ret = context.getString(R.string.status_file_error);
268         } else if (statusCode == BluetoothShare.STATUS_ERROR_NO_SDCARD) {
269             ret = context.getString(R.string.status_no_sd_card);
270         } else if (statusCode == BluetoothShare.STATUS_CONNECTION_ERROR) {
271             ret = context.getString(R.string.status_connection_error);
272         } else if (statusCode == BluetoothShare.STATUS_ERROR_SDCARD_FULL) {
273             ret = context.getString(R.string.bt_sm_2_1, deviceName);
274         } else if ((statusCode == BluetoothShare.STATUS_BAD_REQUEST)
275                 || (statusCode == BluetoothShare.STATUS_LENGTH_REQUIRED)
276                 || (statusCode == BluetoothShare.STATUS_PRECONDITION_FAILED)
277                 || (statusCode == BluetoothShare.STATUS_UNHANDLED_OBEX_CODE)
278                 || (statusCode == BluetoothShare.STATUS_OBEX_DATA_ERROR)) {
279             ret = context.getString(R.string.status_protocol_error);
280         } else {
281             ret = context.getString(R.string.status_unknown_error);
282         }
283         return ret;
284     }
285 
286     /**
287      * Retry the failed transfer: Will insert a new transfer session to db
288      */
retryTransfer(Context context, BluetoothOppTransferInfo transInfo)289     public static void retryTransfer(Context context, BluetoothOppTransferInfo transInfo) {
290         ContentValues values = new ContentValues();
291         values.put(BluetoothShare.URI, transInfo.mFileUri);
292         values.put(BluetoothShare.MIMETYPE, transInfo.mFileType);
293         values.put(BluetoothShare.DESTINATION, transInfo.mDestAddr);
294 
295         final Uri contentUri = context.getContentResolver().insert(BluetoothShare.CONTENT_URI,
296                 values);
297         if (V) Log.v(TAG, "Insert contentUri: " + contentUri + "  to device: " +
298                 transInfo.mDeviceName);
299     }
300 
301 }
302