Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 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 | |
Tao Liejun | 6769b59 | 2009-07-14 15:55:04 -0700 | [diff] [blame] | 35 | import com.android.bluetooth.R; |
| 36 | |
Nick Pelly | 41ef8d4 | 2009-08-06 18:28:28 -0700 | [diff] [blame] | 37 | import android.bluetooth.BluetoothAdapter; |
| 38 | import android.bluetooth.BluetoothDevice; |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 39 | import android.content.ContentResolver; |
| 40 | import android.content.ContentValues; |
| 41 | import android.content.Context; |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 42 | import android.content.Intent; |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 43 | import android.content.SharedPreferences; |
Nick Pelly | 41ef8d4 | 2009-08-06 18:28:28 -0700 | [diff] [blame] | 44 | import android.net.Uri; |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 45 | import android.os.Process; |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 46 | import android.os.SystemClock; |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 47 | import android.text.TextUtils; |
Nick Pelly | 41ef8d4 | 2009-08-06 18:28:28 -0700 | [diff] [blame] | 48 | import android.util.Log; |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 49 | import android.util.Pair; |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 50 | |
Nick Pelly | 41ef8d4 | 2009-08-06 18:28:28 -0700 | [diff] [blame] | 51 | import java.util.ArrayList; |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 52 | import java.util.Iterator; |
Nick Pelly | d6eaf19 | 2012-04-03 18:10:12 -0700 | [diff] [blame] | 53 | import java.util.List; |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 54 | |
| 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 | */ |
| 60 | public class BluetoothOppManager { |
| 61 | private static final String TAG = "BluetoothOppManager"; |
Ashwini Munigala | b98a64f | 2015-08-14 16:39:32 +0530 | [diff] [blame] | 62 | private static final boolean V = Log.isLoggable(Constants.TAG, Log.VERBOSE); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 63 | |
| 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 Pelly | 41ef8d4 | 2009-08-06 18:28:28 -0700 | [diff] [blame] | 73 | private BluetoothAdapter mAdapter; |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 74 | |
Brad Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 75 | private String mMimeTypeOfSendingFile; |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 76 | |
| 77 | private String mUriOfSendingFile; |
| 78 | |
Brad Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 79 | private String mMimeTypeOfSendingFiles; |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 80 | |
Tao Liejun | 1ac5507 | 2009-08-07 15:01:24 +0800 | [diff] [blame] | 81 | private ArrayList<Uri> mUrisOfSendingFiles; |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 82 | |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 83 | private boolean mIsHandoverInitiated; |
| 84 | |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 85 | 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 Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 99 | private static final String ARRAYLIST_ITEM_SEPERATOR = ";"; |
| 100 | |
| 101 | private static final int ALLOWED_INSERT_SHARE_THREAD_NUMBER = 3; |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 102 | |
| 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 Panigrahi | 4c316de | 2013-08-29 09:28:05 +0530 | [diff] [blame] | 109 | public boolean zero_length_file = false; |
| 110 | |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 111 | private int mfileNumInBatch; |
| 112 | |
| 113 | private int mInsertShareThreadNum = 0; |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 114 | |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 115 | // 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 Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 122 | /** |
| 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 Ganesh | 0bd5f7b | 2010-03-24 10:36:06 -0700 | [diff] [blame] | 144 | mContext = context; |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 145 | |
Nick Pelly | 3a88b20 | 2009-10-08 00:57:45 +0200 | [diff] [blame] | 146 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
Nick Pelly | 41ef8d4 | 2009-08-06 18:28:28 -0700 | [diff] [blame] | 147 | if (mAdapter == null) { |
Nick Pelly | ce4d936 | 2009-08-27 18:42:51 -0700 | [diff] [blame] | 148 | if (V) Log.v(TAG, "BLUETOOTH_SERVICE is not started! "); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // Restore data from preference |
| 152 | restoreApplicationData(); |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 157 | |
| 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 Pelly | d6eaf19 | 2012-04-03 18:10:12 -0700 | [diff] [blame] | 169 | |
Martijn Coenen | 7a4cc5c | 2012-05-14 10:09:01 -0700 | [diff] [blame] | 170 | public synchronized void addToWhitelist(String address) { |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 171 | if (address == null) return; |
Martijn Coenen | 7a4cc5c | 2012-05-14 10:09:01 -0700 | [diff] [blame] | 172 | // 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 Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 179 | mWhitelist.add(new Pair<String, Long>(address, SystemClock.elapsedRealtime())); |
Nick Pelly | d6eaf19 | 2012-04-03 18:10:12 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Martijn Coenen | 7a4cc5c | 2012-05-14 10:09:01 -0700 | [diff] [blame] | 182 | public synchronized boolean isWhitelisted(String address) { |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 183 | cleanupWhitelist(); |
| 184 | for (Pair<String,Long> entry : mWhitelist) { |
| 185 | if (entry.first.equals(address)) return true; |
| 186 | } |
| 187 | return false; |
Nick Pelly | d6eaf19 | 2012-04-03 18:10:12 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Restore data from preference |
| 192 | */ |
| 193 | private void restoreApplicationData() { |
| 194 | SharedPreferences settings = mContext.getSharedPreferences(OPP_PREFERENCE_FILE, 0); |
| 195 | |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 196 | // All member vars are not initialized till now |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 197 | mSendingFlag = settings.getBoolean(SENDING_FLAG, false); |
Brad Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 198 | mMimeTypeOfSendingFile = settings.getString(MIME_TYPE, null); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 199 | mUriOfSendingFile = settings.getString(FILE_URI, null); |
Brad Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 200 | mMimeTypeOfSendingFiles = settings.getString(MIME_TYPE_MULTIPLE, null); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 201 | mMultipleFlag = settings.getBoolean(MULTIPLE_FLAG, false); |
| 202 | |
Nick Pelly | ce4d936 | 2009-08-27 18:42:51 -0700 | [diff] [blame] | 203 | if (V) Log.v(TAG, "restoreApplicationData! " + mSendingFlag + mMultipleFlag |
Brad Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 204 | + mMimeTypeOfSendingFile + mUriOfSendingFile); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 205 | |
| 206 | String strUris = settings.getString(FILE_URIS, null); |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 207 | 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 Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 216 | mContext.getSharedPreferences(OPP_PREFERENCE_FILE, 0).edit().clear().apply(); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 220 | * Save application data to preference, need restore these data when service restart |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 221 | */ |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 222 | private void storeApplicationData() { |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 223 | SharedPreferences.Editor editor = mContext.getSharedPreferences(OPP_PREFERENCE_FILE, 0) |
| 224 | .edit(); |
Brad Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 225 | editor.putBoolean(SENDING_FLAG, mSendingFlag); |
| 226 | editor.putBoolean(MULTIPLE_FLAG, mMultipleFlag); |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 227 | if (mMultipleFlag) { |
Brad Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 228 | editor.putString(MIME_TYPE_MULTIPLE, mMimeTypeOfSendingFiles); |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 229 | 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 Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 236 | editor.putString(FILE_URIS, strUris); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 237 | |
Brad Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 238 | editor.remove(MIME_TYPE); |
| 239 | editor.remove(FILE_URI); |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 240 | } else { |
Brad Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 241 | editor.putString(MIME_TYPE, mMimeTypeOfSendingFile); |
| 242 | editor.putString(FILE_URI, mUriOfSendingFile); |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 243 | |
Brad Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 244 | editor.remove(MIME_TYPE_MULTIPLE); |
| 245 | editor.remove(FILE_URIS); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 246 | } |
Brad Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 247 | editor.apply(); |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 248 | if (V) Log.v(TAG, "Application data stored to SharedPreference! "); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Jack He | 0ac7906 | 2017-03-13 10:37:06 -0700 | [diff] [blame] | 251 | public void saveSendingFileInfo(String mimeType, String uriString, boolean isHandover, boolean fromExternal) { |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 252 | synchronized (BluetoothOppManager.this) { |
| 253 | mMultipleFlag = false; |
Brad Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 254 | mMimeTypeOfSendingFile = mimeType; |
Pavlin Radoslavov | fdb6283 | 2015-08-07 18:13:07 -0700 | [diff] [blame] | 255 | mUriOfSendingFile = uriString; |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 256 | mIsHandoverInitiated = isHandover; |
Jake Hamby | ee52ddf | 2012-07-27 14:38:32 -0700 | [diff] [blame] | 257 | Uri uri = Uri.parse(uriString); |
Pavlin Radoslavov | fdb6283 | 2015-08-07 18:13:07 -0700 | [diff] [blame] | 258 | BluetoothOppUtility.putSendFileInfo(uri, |
Jack He | 0ac7906 | 2017-03-13 10:37:06 -0700 | [diff] [blame] | 259 | BluetoothOppSendFileInfo.generateFileInfo(mContext, uri, mimeType, fromExternal)); |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 260 | storeApplicationData(); |
| 261 | } |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Jack He | 0ac7906 | 2017-03-13 10:37:06 -0700 | [diff] [blame] | 264 | public void saveSendingFileInfo(String mimeType, ArrayList<Uri> uris, boolean isHandover, boolean fromExternal) { |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 265 | synchronized (BluetoothOppManager.this) { |
| 266 | mMultipleFlag = true; |
Brad Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 267 | mMimeTypeOfSendingFiles = mimeType; |
Pavlin Radoslavov | fdb6283 | 2015-08-07 18:13:07 -0700 | [diff] [blame] | 268 | mUrisOfSendingFiles = uris; |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 269 | mIsHandoverInitiated = isHandover; |
Jake Hamby | ee52ddf | 2012-07-27 14:38:32 -0700 | [diff] [blame] | 270 | for (Uri uri : uris) { |
Pavlin Radoslavov | fdb6283 | 2015-08-07 18:13:07 -0700 | [diff] [blame] | 271 | BluetoothOppUtility.putSendFileInfo(uri, |
Jack He | 0ac7906 | 2017-03-13 10:37:06 -0700 | [diff] [blame] | 272 | BluetoothOppSendFileInfo.generateFileInfo(mContext, uri, mimeType, fromExternal)); |
Jake Hamby | ee52ddf | 2012-07-27 14:38:32 -0700 | [diff] [blame] | 273 | } |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 274 | storeApplicationData(); |
| 275 | } |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 276 | } |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 277 | |
Hemant Gupta | eed02cb | 2014-05-01 17:02:40 +0530 | [diff] [blame] | 278 | 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 Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 299 | /** |
| 300 | * Get the current status of Bluetooth hardware. |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 301 | * @return true if Bluetooth enabled, false otherwise. |
| 302 | */ |
| 303 | public boolean isEnabled() { |
Nick Pelly | 41ef8d4 | 2009-08-06 18:28:28 -0700 | [diff] [blame] | 304 | if (mAdapter != null) { |
| 305 | return mAdapter.isEnabled(); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 306 | } else { |
Nick Pelly | ce4d936 | 2009-08-27 18:42:51 -0700 | [diff] [blame] | 307 | if (V) Log.v(TAG, "BLUETOOTH_SERVICE is not available! "); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Enable Bluetooth hardware. |
| 314 | */ |
| 315 | public void enableBluetooth() { |
Nick Pelly | 41ef8d4 | 2009-08-06 18:28:28 -0700 | [diff] [blame] | 316 | if (mAdapter != null) { |
| 317 | mAdapter.enable(); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Disable Bluetooth hardware. |
| 323 | */ |
| 324 | public void disableBluetooth() { |
Nick Pelly | 41ef8d4 | 2009-08-06 18:28:28 -0700 | [diff] [blame] | 325 | if (mAdapter != null) { |
| 326 | mAdapter.disable(); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Get device name per bluetooth address. |
| 332 | */ |
Nick Pelly | 41ef8d4 | 2009-08-06 18:28:28 -0700 | [diff] [blame] | 333 | public String getDeviceName(BluetoothDevice device) { |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 334 | String deviceName; |
| 335 | |
Nick Pelly | 41ef8d4 | 2009-08-06 18:28:28 -0700 | [diff] [blame] | 336 | deviceName = BluetoothOppPreference.getInstance(mContext).getName(device); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 337 | |
Nick Pelly | 41ef8d4 | 2009-08-06 18:28:28 -0700 | [diff] [blame] | 338 | if (deviceName == null && mAdapter != null) { |
| 339 | deviceName = device.getName(); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | if (deviceName == null) { |
| 343 | deviceName = mContext.getString(R.string.unknown_device); |
| 344 | } |
| 345 | |
| 346 | return deviceName; |
| 347 | } |
| 348 | |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 349 | public int getBatchSize() { |
| 350 | synchronized (BluetoothOppManager.this) { |
| 351 | return mfileNumInBatch; |
| 352 | } |
| 353 | } |
| 354 | |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 355 | /** |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 356 | * Fork a thread to insert share info to db. |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 357 | */ |
Nick Pelly | 41ef8d4 | 2009-08-06 18:28:28 -0700 | [diff] [blame] | 358 | public void startTransfer(BluetoothDevice device) { |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 359 | 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 Fitzpatrick | 5547071 | 2010-09-03 13:57:15 -0700 | [diff] [blame] | 374 | insertThread = new InsertShareInfoThread(device, mMultipleFlag, mMimeTypeOfSendingFile, |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 375 | mUriOfSendingFile, mMimeTypeOfSendingFiles, mUrisOfSendingFiles, |
| 376 | mIsHandoverInitiated); |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 377 | if (mMultipleFlag) { |
| 378 | mfileNumInBatch = mUrisOfSendingFiles.size(); |
| 379 | } |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 382 | 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 Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 406 | private final boolean mIsHandoverInitiated; |
| 407 | |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 408 | public InsertShareInfoThread(BluetoothDevice device, boolean multiple, |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 409 | String typeOfSingleFile, String uri, String typeOfMultipleFiles, |
| 410 | ArrayList<Uri> uris, boolean handoverInitiated) { |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 411 | 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 Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 418 | this.mIsHandoverInitiated = handoverInitiated; |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 419 | |
| 420 | synchronized (BluetoothOppManager.this) { |
| 421 | mInsertShareThreadNum++; |
| 422 | } |
| 423 | |
| 424 | if (V) Log.v(TAG, "Thread id is: " + this.getId()); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 427 | @Override |
| 428 | public void run() { |
| 429 | Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
Sravan Kumar V | a8b88a3 | 2016-02-01 16:33:40 +0530 | [diff] [blame] | 430 | 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 Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 441 | } |
| 442 | } |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 443 | |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 444 | /** |
| 445 | * Insert multiple sending sessions to db, only used by Opp application. |
| 446 | */ |
| 447 | private void insertMultipleShare() { |
| 448 | int count = mUris.size(); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 449 | Long ts = System.currentTimeMillis(); |
| 450 | for (int i = 0; i < count; i++) { |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 451 | Uri fileUri = mUris.get(i); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 452 | ContentResolver contentResolver = mContext.getContentResolver(); |
Tao Liejun | 1ac5507 | 2009-08-07 15:01:24 +0800 | [diff] [blame] | 453 | String contentType = contentResolver.getType(fileUri); |
Nick Pelly | ce4d936 | 2009-08-27 18:42:51 -0700 | [diff] [blame] | 454 | if (V) Log.v(TAG, "Got mimetype: " + contentType + " Got uri: " + fileUri); |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 455 | if (TextUtils.isEmpty(contentType)) { |
| 456 | contentType = mTypeOfMultipleFiles; |
| 457 | } |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 458 | |
Pavlin Radoslavov | fdb6283 | 2015-08-07 18:13:07 -0700 | [diff] [blame] | 459 | ContentValues values = new ContentValues(); |
| 460 | values.put(BluetoothShare.URI, fileUri.toString()); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 461 | values.put(BluetoothShare.MIMETYPE, contentType); |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 462 | values.put(BluetoothShare.DESTINATION, mRemoteDevice.getAddress()); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 463 | values.put(BluetoothShare.TIMESTAMP, ts); |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 464 | if (mIsHandoverInitiated) { |
| 465 | values.put(BluetoothShare.USER_CONFIRMATION, |
| 466 | BluetoothShare.USER_CONFIRMATION_HANDOVER_CONFIRMED); |
| 467 | } |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 468 | final Uri contentUri = mContext.getContentResolver().insert( |
| 469 | BluetoothShare.CONTENT_URI, values); |
Nick Pelly | ce4d936 | 2009-08-27 18:42:51 -0700 | [diff] [blame] | 470 | if (V) Log.v(TAG, "Insert contentUri: " + contentUri + " to device: " |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 471 | + getDeviceName(mRemoteDevice)); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 472 | } |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Insert single sending session to db, only used by Opp application. |
| 477 | */ |
| 478 | private void insertSingleShare() { |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 479 | ContentValues values = new ContentValues(); |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 480 | values.put(BluetoothShare.URI, mUri); |
| 481 | values.put(BluetoothShare.MIMETYPE, mTypeOfSingleFile); |
| 482 | values.put(BluetoothShare.DESTINATION, mRemoteDevice.getAddress()); |
Martijn Coenen | 8099f5e | 2012-04-06 13:47:52 -0700 | [diff] [blame] | 483 | if (mIsHandoverInitiated) { |
| 484 | values.put(BluetoothShare.USER_CONFIRMATION, |
| 485 | BluetoothShare.USER_CONFIRMATION_HANDOVER_CONFIRMED); |
| 486 | } |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 487 | final Uri contentUri = mContext.getContentResolver().insert(BluetoothShare.CONTENT_URI, |
| 488 | values); |
Nick Pelly | ce4d936 | 2009-08-27 18:42:51 -0700 | [diff] [blame] | 489 | if (V) Log.v(TAG, "Insert contentUri: " + contentUri + " to device: " |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 490 | + getDeviceName(mRemoteDevice)); |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 491 | } |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 492 | } |
Lixin Yue | c6f1bac | 2009-12-23 15:37:27 +0800 | [diff] [blame] | 493 | |
Nick Pelly | 09e9cba | 2009-07-10 18:45:13 -0700 | [diff] [blame] | 494 | } |