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 android.bluetooth.BluetoothAdapter; 36 import android.bluetooth.BluetoothDevice; 37 import android.content.Context; 38 import android.util.Log; 39 40 import com.android.bluetooth.BluetoothMethodProxy; 41 42 import java.util.ArrayList; 43 44 /** 45 * This class stores information about a batch of OPP shares that should be 46 * transferred in one session. 47 */ 48 /*There are a few cases: 1. create a batch for a single file to send 49 * 2. create a batch for multiple files to send 50 * 3. add additional file(s) to existing batch to send 51 * 4. create a batch for receive single file 52 * 5. add additional file to existing batch to receive (this only happens as the server 53 * session notify more files to receive) 54 * 6. Cancel sending a single file 55 * 7. Cancel sending a file from multiple files (implies cancel the transfer, rest of 56 * the unsent files are also canceled) 57 * 8. Cancel receiving a single file 58 * 9. Cancel receiving a file (implies cancel the transfer, no additional files will be received) 59 */ 60 61 public class BluetoothOppBatch { 62 private static final String TAG = "BtOppBatch"; 63 private static final boolean V = Constants.VERBOSE; 64 65 public int mId; 66 public int mStatus; 67 68 public final long mTimestamp; 69 public final int mDirection; 70 public final BluetoothDevice mDestination; 71 72 private BluetoothOppBatchListener mListener; 73 74 private final ArrayList<BluetoothOppShareInfo> mShares; 75 private final Context mContext; 76 77 /** 78 * An interface for notifying when BluetoothOppTransferBatch is changed 79 */ 80 public interface BluetoothOppBatchListener { 81 /** 82 * Called to notify when a share is added into the batch 83 * @param id , BluetoothOppShareInfo.id 84 */ onShareAdded(int id)85 void onShareAdded(int id); 86 87 /** 88 * Called to notify when a share is deleted from the batch 89 * @param id , BluetoothOppShareInfo.id 90 */ onShareDeleted(int id)91 void onShareDeleted(int id); 92 93 /** 94 * Called to notify when the batch is canceled 95 */ onBatchCanceled()96 void onBatchCanceled(); 97 } 98 99 /** 100 * A batch is always created with at least one ShareInfo 101 * @param context, Context 102 * @param info, BluetoothOppShareInfo 103 */ BluetoothOppBatch(Context context, BluetoothOppShareInfo info)104 public BluetoothOppBatch(Context context, BluetoothOppShareInfo info) { 105 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 106 mContext = context; 107 mShares = new ArrayList(); 108 mTimestamp = info.mTimestamp; 109 mDirection = info.mDirection; 110 mDestination = adapter.getRemoteDevice(info.mDestination); 111 mStatus = Constants.BATCH_STATUS_PENDING; 112 mShares.add(info); 113 114 if (V) { 115 Log.v(TAG, "New Batch created for info " + info.mId); 116 } 117 } 118 119 /** 120 * Add one share into the batch. 121 */ 122 /* There are 2 cases: Service scans the databases and it's multiple send 123 * Service receives database update and know additional file should be received 124 */ addShare(BluetoothOppShareInfo info)125 public void addShare(BluetoothOppShareInfo info) { 126 mShares.add(info); 127 if (mListener != null) { 128 mListener.onShareAdded(info.mId); 129 } 130 } 131 132 /** 133 * Cancel the whole batch. 134 */ 135 /* 1) If the batch is running, stop the transfer 136 * 2) Go through mShares list and mark all incomplete share as CANCELED status 137 * 3) update ContentProvider for these canceled transfer 138 */ cancelBatch()139 public void cancelBatch() { 140 if (V) { 141 Log.v(TAG, "batch " + this.mId + " is canceled"); 142 } 143 144 if (mListener != null) { 145 mListener.onBatchCanceled(); 146 } 147 //TODO investigate if below code is redundant 148 for (int i = mShares.size() - 1; i >= 0; i--) { 149 BluetoothOppShareInfo info = mShares.get(i); 150 151 if (info.mStatus < 200) { 152 if (info.mDirection == BluetoothShare.DIRECTION_INBOUND && info.mUri != null) { 153 BluetoothMethodProxy.getInstance().contentResolverDelete( 154 mContext.getContentResolver(), info.mUri, null, null 155 ); 156 } 157 if (V) { 158 Log.v(TAG, "Cancel batch for info " + info.mId); 159 } 160 161 Constants.updateShareStatus(mContext, info.mId, BluetoothShare.STATUS_CANCELED); 162 } 163 } 164 mShares.clear(); 165 } 166 167 /** check if a specific share is in this batch */ hasShare(BluetoothOppShareInfo info)168 public boolean hasShare(BluetoothOppShareInfo info) { 169 return mShares.contains(info); 170 } 171 172 /** if this batch is empty */ isEmpty()173 public boolean isEmpty() { 174 return (mShares.size() == 0); 175 } 176 getNumShares()177 public int getNumShares() { 178 return mShares.size(); 179 } 180 181 /** 182 * Get the running status of the batch 183 * @return 184 */ 185 186 /** register a listener for the batch change */ registerListener(BluetoothOppBatchListener listener)187 public void registerListener(BluetoothOppBatchListener listener) { 188 mListener = listener; 189 } 190 191 /** 192 * Get the first pending ShareInfo of the batch 193 * @return BluetoothOppShareInfo, for the first pending share, or null if 194 * none exists 195 */ getPendingShare()196 public BluetoothOppShareInfo getPendingShare() { 197 for (int i = 0; i < mShares.size(); i++) { 198 BluetoothOppShareInfo share = mShares.get(i); 199 if (share.mStatus == BluetoothShare.STATUS_PENDING) { 200 return share; 201 } 202 } 203 return null; 204 } 205 } 206