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