blob: b8006b2467be292feac006deb72f7b682d2667f7 [file] [log] [blame]
Nick Pelly09e9cba2009-07-10 18:45:13 -07001/*
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
Tao Liejun6769b592009-07-14 15:55:04 -070035import com.android.bluetooth.R;
36
Nick Pelly41ef8d42009-08-06 18:28:28 -070037import android.bluetooth.BluetoothAdapter;
38import android.bluetooth.BluetoothDevice;
Nick Pelly09e9cba2009-07-10 18:45:13 -070039import android.content.ContentResolver;
40import android.content.ContentValues;
41import android.content.Context;
Lixin Yuec6f1bac2009-12-23 15:37:27 +080042import android.content.Intent;
Nick Pelly09e9cba2009-07-10 18:45:13 -070043import android.content.SharedPreferences;
Nick Pelly41ef8d42009-08-06 18:28:28 -070044import android.net.Uri;
Lixin Yuec6f1bac2009-12-23 15:37:27 +080045import android.os.Process;
Martijn Coenen8099f5e2012-04-06 13:47:52 -070046import android.os.SystemClock;
Lixin Yuec6f1bac2009-12-23 15:37:27 +080047import android.text.TextUtils;
Nick Pelly41ef8d42009-08-06 18:28:28 -070048import android.util.Log;
Martijn Coenen8099f5e2012-04-06 13:47:52 -070049import android.util.Pair;
Nick Pelly09e9cba2009-07-10 18:45:13 -070050
Nick Pelly41ef8d42009-08-06 18:28:28 -070051import java.util.ArrayList;
Martijn Coenen8099f5e2012-04-06 13:47:52 -070052import java.util.Iterator;
Nick Pellyd6eaf192012-04-03 18:10:12 -070053import java.util.List;
Nick Pelly09e9cba2009-07-10 18:45:13 -070054
55/**
56 * This class provides a simplified interface on top of other Bluetooth service
57 * layer components; Also it handles some Opp application level variables. It's
58 * a singleton got from BluetoothOppManager.getInstance(context);
59 */
60public class BluetoothOppManager {
61 private static final String TAG = "BluetoothOppManager";
Ashwini Munigalab98a64f2015-08-14 16:39:32 +053062 private static final boolean V = Log.isLoggable(Constants.TAG, Log.VERBOSE);
Nick Pelly09e9cba2009-07-10 18:45:13 -070063
64 private static BluetoothOppManager INSTANCE;
65
66 /** Used when obtaining a reference to the singleton instance. */
67 private static Object INSTANCE_LOCK = new Object();
68
69 private boolean mInitialized;
70
71 private Context mContext;
72
Nick Pelly41ef8d42009-08-06 18:28:28 -070073 private BluetoothAdapter mAdapter;
Nick Pelly09e9cba2009-07-10 18:45:13 -070074
Brad Fitzpatrick55470712010-09-03 13:57:15 -070075 private String mMimeTypeOfSendingFile;
Nick Pelly09e9cba2009-07-10 18:45:13 -070076
77 private String mUriOfSendingFile;
78
Brad Fitzpatrick55470712010-09-03 13:57:15 -070079 private String mMimeTypeOfSendingFiles;
Nick Pelly09e9cba2009-07-10 18:45:13 -070080
Tao Liejun1ac55072009-08-07 15:01:24 +080081 private ArrayList<Uri> mUrisOfSendingFiles;
Nick Pelly09e9cba2009-07-10 18:45:13 -070082
Martijn Coenen8099f5e2012-04-06 13:47:52 -070083 private boolean mIsHandoverInitiated;
84
Nick Pelly09e9cba2009-07-10 18:45:13 -070085 private static final String OPP_PREFERENCE_FILE = "OPPMGR";
86
87 private static final String SENDING_FLAG = "SENDINGFLAG";
88
89 private static final String MIME_TYPE = "MIMETYPE";
90
91 private static final String FILE_URI = "FILE_URI";
92
93 private static final String MIME_TYPE_MULTIPLE = "MIMETYPE_MULTIPLE";
94
95 private static final String FILE_URIS = "FILE_URIS";
96
97 private static final String MULTIPLE_FLAG = "MULTIPLE_FLAG";
98
Lixin Yuec6f1bac2009-12-23 15:37:27 +080099 private static final String ARRAYLIST_ITEM_SEPERATOR = ";";
100
101 private static final int ALLOWED_INSERT_SHARE_THREAD_NUMBER = 3;
Nick Pelly09e9cba2009-07-10 18:45:13 -0700102
103 // used to judge if need continue sending process after received a
104 // ENABLED_ACTION
105 public boolean mSendingFlag;
106
107 public boolean mMultipleFlag;
108
Pradeep Panigrahi4c316de2013-08-29 09:28:05 +0530109 public boolean zero_length_file = false;
110
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800111 private int mfileNumInBatch;
112
113 private int mInsertShareThreadNum = 0;
Nick Pelly09e9cba2009-07-10 18:45:13 -0700114
Martijn Coenen8099f5e2012-04-06 13:47:52 -0700115 // A list of devices that may send files over OPP to this device
116 // without user confirmation. Used for connection handover from forex NFC.
117 private List<Pair<String,Long> > mWhitelist = new ArrayList<Pair<String, Long> >();
118
119 // The time for which the whitelist entries remain valid.
120 private static final int WHITELIST_DURATION_MS = 15000;
121
Nick Pelly09e9cba2009-07-10 18:45:13 -0700122 /**
123 * Get singleton instance.
124 */
125 public static BluetoothOppManager getInstance(Context context) {
126 synchronized (INSTANCE_LOCK) {
127 if (INSTANCE == null) {
128 INSTANCE = new BluetoothOppManager();
129 }
130 INSTANCE.init(context);
131
132 return INSTANCE;
133 }
134 }
135
136 /**
137 * init
138 */
139 private boolean init(Context context) {
140 if (mInitialized)
141 return true;
142 mInitialized = true;
143
Jaikumar Ganesh0bd5f7b2010-03-24 10:36:06 -0700144 mContext = context;
Nick Pelly09e9cba2009-07-10 18:45:13 -0700145
Nick Pelly3a88b202009-10-08 00:57:45 +0200146 mAdapter = BluetoothAdapter.getDefaultAdapter();
Nick Pelly41ef8d42009-08-06 18:28:28 -0700147 if (mAdapter == null) {
Nick Pellyce4d9362009-08-27 18:42:51 -0700148 if (V) Log.v(TAG, "BLUETOOTH_SERVICE is not started! ");
Nick Pelly09e9cba2009-07-10 18:45:13 -0700149 }
150
151 // Restore data from preference
152 restoreApplicationData();
153
154 return true;
155 }
156
Martijn Coenen8099f5e2012-04-06 13:47:52 -0700157
158 private void cleanupWhitelist() {
159 // Removes expired entries
160 long curTime = SystemClock.elapsedRealtime();
161 for (Iterator<Pair<String,Long>> iter = mWhitelist.iterator(); iter.hasNext(); ) {
162 Pair<String,Long> entry = iter.next();
163 if (curTime - entry.second > WHITELIST_DURATION_MS) {
164 if (V) Log.v(TAG, "Cleaning out whitelist entry " + entry.first);
165 iter.remove();
166 }
167 }
168 }
Nick Pellyd6eaf192012-04-03 18:10:12 -0700169
Martijn Coenen7a4cc5c2012-05-14 10:09:01 -0700170 public synchronized void addToWhitelist(String address) {
Martijn Coenen8099f5e2012-04-06 13:47:52 -0700171 if (address == null) return;
Martijn Coenen7a4cc5c2012-05-14 10:09:01 -0700172 // Remove any existing entries
173 for (Iterator<Pair<String,Long>> iter = mWhitelist.iterator(); iter.hasNext(); ) {
174 Pair<String,Long> entry = iter.next();
175 if (entry.first.equals(address)) {
176 iter.remove();
177 }
178 }
Martijn Coenen8099f5e2012-04-06 13:47:52 -0700179 mWhitelist.add(new Pair<String, Long>(address, SystemClock.elapsedRealtime()));
Nick Pellyd6eaf192012-04-03 18:10:12 -0700180 }
181
Martijn Coenen7a4cc5c2012-05-14 10:09:01 -0700182 public synchronized boolean isWhitelisted(String address) {
Martijn Coenen8099f5e2012-04-06 13:47:52 -0700183 cleanupWhitelist();
184 for (Pair<String,Long> entry : mWhitelist) {
185 if (entry.first.equals(address)) return true;
186 }
187 return false;
Nick Pellyd6eaf192012-04-03 18:10:12 -0700188 }
189
Nick Pelly09e9cba2009-07-10 18:45:13 -0700190 /**
191 * Restore data from preference
192 */
193 private void restoreApplicationData() {
194 SharedPreferences settings = mContext.getSharedPreferences(OPP_PREFERENCE_FILE, 0);
195
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800196 // All member vars are not initialized till now
Nick Pelly09e9cba2009-07-10 18:45:13 -0700197 mSendingFlag = settings.getBoolean(SENDING_FLAG, false);
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700198 mMimeTypeOfSendingFile = settings.getString(MIME_TYPE, null);
Nick Pelly09e9cba2009-07-10 18:45:13 -0700199 mUriOfSendingFile = settings.getString(FILE_URI, null);
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700200 mMimeTypeOfSendingFiles = settings.getString(MIME_TYPE_MULTIPLE, null);
Nick Pelly09e9cba2009-07-10 18:45:13 -0700201 mMultipleFlag = settings.getBoolean(MULTIPLE_FLAG, false);
202
Nick Pellyce4d9362009-08-27 18:42:51 -0700203 if (V) Log.v(TAG, "restoreApplicationData! " + mSendingFlag + mMultipleFlag
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700204 + mMimeTypeOfSendingFile + mUriOfSendingFile);
Nick Pelly09e9cba2009-07-10 18:45:13 -0700205
206 String strUris = settings.getString(FILE_URIS, null);
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800207 mUrisOfSendingFiles = new ArrayList<Uri>();
208 if (strUris != null) {
209 String[] splitUri = strUris.split(ARRAYLIST_ITEM_SEPERATOR);
210 for (int i = 0; i < splitUri.length; i++) {
211 mUrisOfSendingFiles.add(Uri.parse(splitUri[i]));
212 if (V) Log.v(TAG, "Uri in batch: " + Uri.parse(splitUri[i]));
213 }
214 }
215
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700216 mContext.getSharedPreferences(OPP_PREFERENCE_FILE, 0).edit().clear().apply();
Nick Pelly09e9cba2009-07-10 18:45:13 -0700217 }
218
219 /**
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800220 * Save application data to preference, need restore these data when service restart
Nick Pelly09e9cba2009-07-10 18:45:13 -0700221 */
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800222 private void storeApplicationData() {
Nick Pelly09e9cba2009-07-10 18:45:13 -0700223 SharedPreferences.Editor editor = mContext.getSharedPreferences(OPP_PREFERENCE_FILE, 0)
224 .edit();
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700225 editor.putBoolean(SENDING_FLAG, mSendingFlag);
226 editor.putBoolean(MULTIPLE_FLAG, mMultipleFlag);
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800227 if (mMultipleFlag) {
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700228 editor.putString(MIME_TYPE_MULTIPLE, mMimeTypeOfSendingFiles);
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800229 StringBuilder sb = new StringBuilder();
230 for (int i = 0, count = mUrisOfSendingFiles.size(); i < count; i++) {
231 Uri uriContent = mUrisOfSendingFiles.get(i);
232 sb.append(uriContent);
233 sb.append(ARRAYLIST_ITEM_SEPERATOR);
234 }
235 String strUris = sb.toString();
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700236 editor.putString(FILE_URIS, strUris);
Nick Pelly09e9cba2009-07-10 18:45:13 -0700237
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700238 editor.remove(MIME_TYPE);
239 editor.remove(FILE_URI);
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800240 } else {
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700241 editor.putString(MIME_TYPE, mMimeTypeOfSendingFile);
242 editor.putString(FILE_URI, mUriOfSendingFile);
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800243
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700244 editor.remove(MIME_TYPE_MULTIPLE);
245 editor.remove(FILE_URIS);
Nick Pelly09e9cba2009-07-10 18:45:13 -0700246 }
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700247 editor.apply();
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800248 if (V) Log.v(TAG, "Application data stored to SharedPreference! ");
Nick Pelly09e9cba2009-07-10 18:45:13 -0700249 }
250
Jack He0ac79062017-03-13 10:37:06 -0700251 public void saveSendingFileInfo(String mimeType, String uriString, boolean isHandover, boolean fromExternal) {
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800252 synchronized (BluetoothOppManager.this) {
253 mMultipleFlag = false;
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700254 mMimeTypeOfSendingFile = mimeType;
Pavlin Radoslavovfdb62832015-08-07 18:13:07 -0700255 mUriOfSendingFile = uriString;
Martijn Coenen8099f5e2012-04-06 13:47:52 -0700256 mIsHandoverInitiated = isHandover;
Jake Hambyee52ddf2012-07-27 14:38:32 -0700257 Uri uri = Uri.parse(uriString);
Pavlin Radoslavovfdb62832015-08-07 18:13:07 -0700258 BluetoothOppUtility.putSendFileInfo(uri,
Jack He0ac79062017-03-13 10:37:06 -0700259 BluetoothOppSendFileInfo.generateFileInfo(mContext, uri, mimeType, fromExternal));
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800260 storeApplicationData();
261 }
Nick Pelly09e9cba2009-07-10 18:45:13 -0700262 }
263
Jack He0ac79062017-03-13 10:37:06 -0700264 public void saveSendingFileInfo(String mimeType, ArrayList<Uri> uris, boolean isHandover, boolean fromExternal) {
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800265 synchronized (BluetoothOppManager.this) {
266 mMultipleFlag = true;
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700267 mMimeTypeOfSendingFiles = mimeType;
Pavlin Radoslavovfdb62832015-08-07 18:13:07 -0700268 mUrisOfSendingFiles = uris;
Martijn Coenen8099f5e2012-04-06 13:47:52 -0700269 mIsHandoverInitiated = isHandover;
Jake Hambyee52ddf2012-07-27 14:38:32 -0700270 for (Uri uri : uris) {
Pavlin Radoslavovfdb62832015-08-07 18:13:07 -0700271 BluetoothOppUtility.putSendFileInfo(uri,
Jack He0ac79062017-03-13 10:37:06 -0700272 BluetoothOppSendFileInfo.generateFileInfo(mContext, uri, mimeType, fromExternal));
Jake Hambyee52ddf2012-07-27 14:38:32 -0700273 }
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800274 storeApplicationData();
275 }
Nick Pelly09e9cba2009-07-10 18:45:13 -0700276 }
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800277
Hemant Guptaeed02cb2014-05-01 17:02:40 +0530278 public void cleanUpSendingFileInfo() {
279 synchronized (BluetoothOppManager.this) {
280 Uri uri;
281 if (V) Log.v(TAG, "cleanUpSendingFileInfo: mMultipleFlag = " +
282 mMultipleFlag);
283 if (!mMultipleFlag) {
284 uri = Uri.parse(mUriOfSendingFile);
285 if (V) Log.v(TAG, "cleanUpSendingFileInfo: " +
286 "closeSendFileInfo for uri = " + uri);
287 BluetoothOppUtility.closeSendFileInfo(uri);
288 } else {
289 for (int i = 0, count = mUrisOfSendingFiles.size(); i < count; i++) {
290 uri = mUrisOfSendingFiles.get(i);
291 if (V) Log.v(TAG, "cleanUpSendingFileInfo: " +
292 "closeSendFileInfo for uri = " + uri);
293 BluetoothOppUtility.closeSendFileInfo(uri);
294 }
295 }
296 }
297 }
298
Nick Pelly09e9cba2009-07-10 18:45:13 -0700299 /**
300 * Get the current status of Bluetooth hardware.
Nick Pelly09e9cba2009-07-10 18:45:13 -0700301 * @return true if Bluetooth enabled, false otherwise.
302 */
303 public boolean isEnabled() {
Nick Pelly41ef8d42009-08-06 18:28:28 -0700304 if (mAdapter != null) {
305 return mAdapter.isEnabled();
Nick Pelly09e9cba2009-07-10 18:45:13 -0700306 } else {
Nick Pellyce4d9362009-08-27 18:42:51 -0700307 if (V) Log.v(TAG, "BLUETOOTH_SERVICE is not available! ");
Nick Pelly09e9cba2009-07-10 18:45:13 -0700308 return false;
309 }
310 }
311
312 /**
313 * Enable Bluetooth hardware.
314 */
315 public void enableBluetooth() {
Nick Pelly41ef8d42009-08-06 18:28:28 -0700316 if (mAdapter != null) {
317 mAdapter.enable();
Nick Pelly09e9cba2009-07-10 18:45:13 -0700318 }
319 }
320
321 /**
322 * Disable Bluetooth hardware.
323 */
324 public void disableBluetooth() {
Nick Pelly41ef8d42009-08-06 18:28:28 -0700325 if (mAdapter != null) {
326 mAdapter.disable();
Nick Pelly09e9cba2009-07-10 18:45:13 -0700327 }
328 }
329
330 /**
331 * Get device name per bluetooth address.
332 */
Nick Pelly41ef8d42009-08-06 18:28:28 -0700333 public String getDeviceName(BluetoothDevice device) {
Nick Pelly09e9cba2009-07-10 18:45:13 -0700334 String deviceName;
335
Nick Pelly41ef8d42009-08-06 18:28:28 -0700336 deviceName = BluetoothOppPreference.getInstance(mContext).getName(device);
Nick Pelly09e9cba2009-07-10 18:45:13 -0700337
Nick Pelly41ef8d42009-08-06 18:28:28 -0700338 if (deviceName == null && mAdapter != null) {
339 deviceName = device.getName();
Nick Pelly09e9cba2009-07-10 18:45:13 -0700340 }
341
342 if (deviceName == null) {
343 deviceName = mContext.getString(R.string.unknown_device);
344 }
345
346 return deviceName;
347 }
348
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800349 public int getBatchSize() {
350 synchronized (BluetoothOppManager.this) {
351 return mfileNumInBatch;
352 }
353 }
354
Nick Pelly09e9cba2009-07-10 18:45:13 -0700355 /**
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800356 * Fork a thread to insert share info to db.
Nick Pelly09e9cba2009-07-10 18:45:13 -0700357 */
Nick Pelly41ef8d42009-08-06 18:28:28 -0700358 public void startTransfer(BluetoothDevice device) {
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800359 if (V) Log.v(TAG, "Active InsertShareThread number is : " + mInsertShareThreadNum);
360 InsertShareInfoThread insertThread;
361 synchronized (BluetoothOppManager.this) {
362 if (mInsertShareThreadNum > ALLOWED_INSERT_SHARE_THREAD_NUMBER) {
363 Log.e(TAG, "Too many shares user triggered concurrently!");
364
365 // Notice user
366 Intent in = new Intent(mContext, BluetoothOppBtErrorActivity.class);
367 in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
368 in.putExtra("title", mContext.getString(R.string.enabling_progress_title));
369 in.putExtra("content", mContext.getString(R.string.ErrorTooManyRequests));
370 mContext.startActivity(in);
371
372 return;
373 }
Brad Fitzpatrick55470712010-09-03 13:57:15 -0700374 insertThread = new InsertShareInfoThread(device, mMultipleFlag, mMimeTypeOfSendingFile,
Martijn Coenen8099f5e2012-04-06 13:47:52 -0700375 mUriOfSendingFile, mMimeTypeOfSendingFiles, mUrisOfSendingFiles,
376 mIsHandoverInitiated);
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800377 if (mMultipleFlag) {
378 mfileNumInBatch = mUrisOfSendingFiles.size();
379 }
Nick Pelly09e9cba2009-07-10 18:45:13 -0700380 }
381
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800382 insertThread.start();
383 }
384
385 /**
386 * Thread to insert share info to db. In multiple files (say 100 files)
387 * share case, the inserting share info to db operation would be a time
388 * consuming operation, so need a thread to handle it. This thread allows
389 * multiple instances to support below case: User select multiple files to
390 * share to one device (say device 1), and then right away share to second
391 * device (device 2), we need insert all these share info to db.
392 */
393 private class InsertShareInfoThread extends Thread {
394 private final BluetoothDevice mRemoteDevice;
395
396 private final String mTypeOfSingleFile;
397
398 private final String mUri;
399
400 private final String mTypeOfMultipleFiles;
401
402 private final ArrayList<Uri> mUris;
403
404 private final boolean mIsMultiple;
405
Martijn Coenen8099f5e2012-04-06 13:47:52 -0700406 private final boolean mIsHandoverInitiated;
407
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800408 public InsertShareInfoThread(BluetoothDevice device, boolean multiple,
Martijn Coenen8099f5e2012-04-06 13:47:52 -0700409 String typeOfSingleFile, String uri, String typeOfMultipleFiles,
410 ArrayList<Uri> uris, boolean handoverInitiated) {
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800411 super("Insert ShareInfo Thread");
412 this.mRemoteDevice = device;
413 this.mIsMultiple = multiple;
414 this.mTypeOfSingleFile = typeOfSingleFile;
415 this.mUri = uri;
416 this.mTypeOfMultipleFiles = typeOfMultipleFiles;
417 this.mUris = uris;
Martijn Coenen8099f5e2012-04-06 13:47:52 -0700418 this.mIsHandoverInitiated = handoverInitiated;
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800419
420 synchronized (BluetoothOppManager.this) {
421 mInsertShareThreadNum++;
422 }
423
424 if (V) Log.v(TAG, "Thread id is: " + this.getId());
Nick Pelly09e9cba2009-07-10 18:45:13 -0700425 }
426
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800427 @Override
428 public void run() {
429 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
Sravan Kumar Va8b88a32016-02-01 16:33:40 +0530430 if (mRemoteDevice == null) {
431 Log.e(TAG, "Target bt device is null!");
432 return;
433 }
434 if (mIsMultiple) {
435 insertMultipleShare();
436 } else {
437 insertSingleShare();
438 }
439 synchronized (BluetoothOppManager.this) {
440 mInsertShareThreadNum--;
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800441 }
442 }
Nick Pelly09e9cba2009-07-10 18:45:13 -0700443
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800444 /**
445 * Insert multiple sending sessions to db, only used by Opp application.
446 */
447 private void insertMultipleShare() {
448 int count = mUris.size();
Nick Pelly09e9cba2009-07-10 18:45:13 -0700449 Long ts = System.currentTimeMillis();
450 for (int i = 0; i < count; i++) {
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800451 Uri fileUri = mUris.get(i);
Nick Pelly09e9cba2009-07-10 18:45:13 -0700452 ContentResolver contentResolver = mContext.getContentResolver();
Tao Liejun1ac55072009-08-07 15:01:24 +0800453 String contentType = contentResolver.getType(fileUri);
Nick Pellyce4d9362009-08-27 18:42:51 -0700454 if (V) Log.v(TAG, "Got mimetype: " + contentType + " Got uri: " + fileUri);
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800455 if (TextUtils.isEmpty(contentType)) {
456 contentType = mTypeOfMultipleFiles;
457 }
Nick Pelly09e9cba2009-07-10 18:45:13 -0700458
Pavlin Radoslavovfdb62832015-08-07 18:13:07 -0700459 ContentValues values = new ContentValues();
460 values.put(BluetoothShare.URI, fileUri.toString());
Nick Pelly09e9cba2009-07-10 18:45:13 -0700461 values.put(BluetoothShare.MIMETYPE, contentType);
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800462 values.put(BluetoothShare.DESTINATION, mRemoteDevice.getAddress());
Nick Pelly09e9cba2009-07-10 18:45:13 -0700463 values.put(BluetoothShare.TIMESTAMP, ts);
Martijn Coenen8099f5e2012-04-06 13:47:52 -0700464 if (mIsHandoverInitiated) {
465 values.put(BluetoothShare.USER_CONFIRMATION,
466 BluetoothShare.USER_CONFIRMATION_HANDOVER_CONFIRMED);
467 }
Nick Pelly09e9cba2009-07-10 18:45:13 -0700468 final Uri contentUri = mContext.getContentResolver().insert(
469 BluetoothShare.CONTENT_URI, values);
Nick Pellyce4d9362009-08-27 18:42:51 -0700470 if (V) Log.v(TAG, "Insert contentUri: " + contentUri + " to device: "
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800471 + getDeviceName(mRemoteDevice));
Nick Pelly09e9cba2009-07-10 18:45:13 -0700472 }
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800473 }
474
475 /**
476 * Insert single sending session to db, only used by Opp application.
477 */
478 private void insertSingleShare() {
Nick Pelly09e9cba2009-07-10 18:45:13 -0700479 ContentValues values = new ContentValues();
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800480 values.put(BluetoothShare.URI, mUri);
481 values.put(BluetoothShare.MIMETYPE, mTypeOfSingleFile);
482 values.put(BluetoothShare.DESTINATION, mRemoteDevice.getAddress());
Martijn Coenen8099f5e2012-04-06 13:47:52 -0700483 if (mIsHandoverInitiated) {
484 values.put(BluetoothShare.USER_CONFIRMATION,
485 BluetoothShare.USER_CONFIRMATION_HANDOVER_CONFIRMED);
486 }
Nick Pelly09e9cba2009-07-10 18:45:13 -0700487 final Uri contentUri = mContext.getContentResolver().insert(BluetoothShare.CONTENT_URI,
488 values);
Nick Pellyce4d9362009-08-27 18:42:51 -0700489 if (V) Log.v(TAG, "Insert contentUri: " + contentUri + " to device: "
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800490 + getDeviceName(mRemoteDevice));
Nick Pelly09e9cba2009-07-10 18:45:13 -0700491 }
Nick Pelly09e9cba2009-07-10 18:45:13 -0700492 }
Lixin Yuec6f1bac2009-12-23 15:37:27 +0800493
Nick Pelly09e9cba2009-07-10 18:45:13 -0700494}