15cc617943765df27844e459362c4bc1821305216Lixin Yue/*
25cc617943765df27844e459362c4bc1821305216Lixin Yue * Copyright (c) 2008-2009, Motorola, Inc.
35cc617943765df27844e459362c4bc1821305216Lixin Yue *
45cc617943765df27844e459362c4bc1821305216Lixin Yue * All rights reserved.
55cc617943765df27844e459362c4bc1821305216Lixin Yue *
65cc617943765df27844e459362c4bc1821305216Lixin Yue * Redistribution and use in source and binary forms, with or without
75cc617943765df27844e459362c4bc1821305216Lixin Yue * modification, are permitted provided that the following conditions are met:
85cc617943765df27844e459362c4bc1821305216Lixin Yue *
95cc617943765df27844e459362c4bc1821305216Lixin Yue * - Redistributions of source code must retain the above copyright notice,
105cc617943765df27844e459362c4bc1821305216Lixin Yue * this list of conditions and the following disclaimer.
115cc617943765df27844e459362c4bc1821305216Lixin Yue *
125cc617943765df27844e459362c4bc1821305216Lixin Yue * - Redistributions in binary form must reproduce the above copyright notice,
135cc617943765df27844e459362c4bc1821305216Lixin Yue * this list of conditions and the following disclaimer in the documentation
145cc617943765df27844e459362c4bc1821305216Lixin Yue * and/or other materials provided with the distribution.
155cc617943765df27844e459362c4bc1821305216Lixin Yue *
165cc617943765df27844e459362c4bc1821305216Lixin Yue * - Neither the name of the Motorola, Inc. nor the names of its contributors
175cc617943765df27844e459362c4bc1821305216Lixin Yue * may be used to endorse or promote products derived from this software
185cc617943765df27844e459362c4bc1821305216Lixin Yue * without specific prior written permission.
195cc617943765df27844e459362c4bc1821305216Lixin Yue *
205cc617943765df27844e459362c4bc1821305216Lixin Yue * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
215cc617943765df27844e459362c4bc1821305216Lixin Yue * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
225cc617943765df27844e459362c4bc1821305216Lixin Yue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
235cc617943765df27844e459362c4bc1821305216Lixin Yue * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
245cc617943765df27844e459362c4bc1821305216Lixin Yue * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
255cc617943765df27844e459362c4bc1821305216Lixin Yue * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
265cc617943765df27844e459362c4bc1821305216Lixin Yue * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
275cc617943765df27844e459362c4bc1821305216Lixin Yue * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
285cc617943765df27844e459362c4bc1821305216Lixin Yue * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
295cc617943765df27844e459362c4bc1821305216Lixin Yue * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
305cc617943765df27844e459362c4bc1821305216Lixin Yue * POSSIBILITY OF SUCH DAMAGE.
315cc617943765df27844e459362c4bc1821305216Lixin Yue */
325cc617943765df27844e459362c4bc1821305216Lixin Yue
335cc617943765df27844e459362c4bc1821305216Lixin Yuepackage com.android.bluetooth.opp;
345cc617943765df27844e459362c4bc1821305216Lixin Yue
355cc617943765df27844e459362c4bc1821305216Lixin Yueimport com.android.bluetooth.R;
365cc617943765df27844e459362c4bc1821305216Lixin Yue
375cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.app.Activity;
385cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.app.AlertDialog;
39de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yueimport android.bluetooth.BluetoothAdapter;
405cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.content.DialogInterface;
415cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.content.Intent;
425cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.database.Cursor;
435cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.net.Uri;
445cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.os.Bundle;
455cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.util.Log;
465cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.view.ContextMenu;
475cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.view.Menu;
485cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.view.MenuInflater;
495cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.view.MenuItem;
505cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.view.View;
515cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.view.ContextMenu.ContextMenuInfo;
525cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.widget.AdapterView;
535cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.widget.ListView;
545cc617943765df27844e459362c4bc1821305216Lixin Yueimport android.widget.AdapterView.OnItemClickListener;
555cc617943765df27844e459362c4bc1821305216Lixin Yue
565cc617943765df27844e459362c4bc1821305216Lixin Yue/**
575cc617943765df27844e459362c4bc1821305216Lixin Yue * View showing the user's finished bluetooth opp transfers that the user does
585cc617943765df27844e459362c4bc1821305216Lixin Yue * not confirm. Including outbound and inbound transfers, both successful and
595cc617943765df27844e459362c4bc1821305216Lixin Yue * failed. *
605cc617943765df27844e459362c4bc1821305216Lixin Yue */
615cc617943765df27844e459362c4bc1821305216Lixin Yuepublic class BluetoothOppTransferHistory extends Activity implements
625cc617943765df27844e459362c4bc1821305216Lixin Yue        View.OnCreateContextMenuListener, OnItemClickListener {
635cc617943765df27844e459362c4bc1821305216Lixin Yue    private static final String TAG = "BluetoothOppTransferHistory";
645cc617943765df27844e459362c4bc1821305216Lixin Yue
655cc617943765df27844e459362c4bc1821305216Lixin Yue    private static final boolean V = Constants.VERBOSE;
665cc617943765df27844e459362c4bc1821305216Lixin Yue
675cc617943765df27844e459362c4bc1821305216Lixin Yue    private ListView mListView;
685cc617943765df27844e459362c4bc1821305216Lixin Yue
695cc617943765df27844e459362c4bc1821305216Lixin Yue    private Cursor mTransferCursor;
705cc617943765df27844e459362c4bc1821305216Lixin Yue
715cc617943765df27844e459362c4bc1821305216Lixin Yue    private BluetoothOppTransferAdapter mTransferAdapter;
725cc617943765df27844e459362c4bc1821305216Lixin Yue
735cc617943765df27844e459362c4bc1821305216Lixin Yue    private int mIdColumnId;
745cc617943765df27844e459362c4bc1821305216Lixin Yue
755cc617943765df27844e459362c4bc1821305216Lixin Yue    private int mContextMenuPosition;
765cc617943765df27844e459362c4bc1821305216Lixin Yue
771ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby    private boolean mShowAllIncoming;
781ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby
79de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue    /** Class to handle Notification Manager updates */
80de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue    private BluetoothOppNotification mNotifier;
81de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue
825cc617943765df27844e459362c4bc1821305216Lixin Yue    @Override
835cc617943765df27844e459362c4bc1821305216Lixin Yue    public void onCreate(Bundle icicle) {
845cc617943765df27844e459362c4bc1821305216Lixin Yue        super.onCreate(icicle);
855cc617943765df27844e459362c4bc1821305216Lixin Yue        setContentView(R.layout.bluetooth_transfers_page);
865cc617943765df27844e459362c4bc1821305216Lixin Yue        mListView = (ListView)findViewById(R.id.list);
875cc617943765df27844e459362c4bc1821305216Lixin Yue        mListView.setEmptyView(findViewById(R.id.empty));
885cc617943765df27844e459362c4bc1821305216Lixin Yue
891ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby        mShowAllIncoming = getIntent().getBooleanExtra(
901ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby                Constants.EXTRA_SHOW_ALL_FILES, false);
911ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby
925cc617943765df27844e459362c4bc1821305216Lixin Yue        String direction;
935cc617943765df27844e459362c4bc1821305216Lixin Yue        int dir = getIntent().getIntExtra("direction", 0);
945cc617943765df27844e459362c4bc1821305216Lixin Yue        if (dir == BluetoothShare.DIRECTION_OUTBOUND) {
955cc617943765df27844e459362c4bc1821305216Lixin Yue            setTitle(getText(R.string.outbound_history_title));
965cc617943765df27844e459362c4bc1821305216Lixin Yue            direction = "(" + BluetoothShare.DIRECTION + " == " + BluetoothShare.DIRECTION_OUTBOUND
975cc617943765df27844e459362c4bc1821305216Lixin Yue                    + ")";
985cc617943765df27844e459362c4bc1821305216Lixin Yue        } else {
991ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby            if (mShowAllIncoming) {
1001ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby                setTitle(getText(R.string.btopp_live_folder));
1011ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby            } else {
1021ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby                setTitle(getText(R.string.inbound_history_title));
1031ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby            }
1045cc617943765df27844e459362c4bc1821305216Lixin Yue            direction = "(" + BluetoothShare.DIRECTION + " == " + BluetoothShare.DIRECTION_INBOUND
1055cc617943765df27844e459362c4bc1821305216Lixin Yue                    + ")";
1065cc617943765df27844e459362c4bc1821305216Lixin Yue        }
1075cc617943765df27844e459362c4bc1821305216Lixin Yue
1081ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby        String selection = BluetoothShare.STATUS + " >= '200' AND " + direction;
1091ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby
1101ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby        if (!mShowAllIncoming) {
1111ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby            selection = selection + " AND ("
1121ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby                    + BluetoothShare.VISIBILITY + " IS NULL OR "
1131ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby                    + BluetoothShare.VISIBILITY + " == '"
1141ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby                    + BluetoothShare.VISIBILITY_VISIBLE + "')";
1151ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby        }
1161ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby
1175cc617943765df27844e459362c4bc1821305216Lixin Yue        final String sortOrder = BluetoothShare.TIMESTAMP + " DESC";
1185cc617943765df27844e459362c4bc1821305216Lixin Yue
1195cc617943765df27844e459362c4bc1821305216Lixin Yue        mTransferCursor = managedQuery(BluetoothShare.CONTENT_URI, new String[] {
1205cc617943765df27844e459362c4bc1821305216Lixin Yue                "_id", BluetoothShare.FILENAME_HINT, BluetoothShare.STATUS,
1215cc617943765df27844e459362c4bc1821305216Lixin Yue                BluetoothShare.TOTAL_BYTES, BluetoothShare._DATA, BluetoothShare.TIMESTAMP,
1225cc617943765df27844e459362c4bc1821305216Lixin Yue                BluetoothShare.VISIBILITY, BluetoothShare.DESTINATION, BluetoothShare.DIRECTION
1235cc617943765df27844e459362c4bc1821305216Lixin Yue        }, selection, sortOrder);
1245cc617943765df27844e459362c4bc1821305216Lixin Yue
1255cc617943765df27844e459362c4bc1821305216Lixin Yue        // only attach everything to the listbox if we can access
1265cc617943765df27844e459362c4bc1821305216Lixin Yue        // the transfer database. Otherwise, just show it empty
1275cc617943765df27844e459362c4bc1821305216Lixin Yue        if (mTransferCursor != null) {
1285cc617943765df27844e459362c4bc1821305216Lixin Yue            mIdColumnId = mTransferCursor.getColumnIndexOrThrow(BluetoothShare._ID);
1295cc617943765df27844e459362c4bc1821305216Lixin Yue            // Create a list "controller" for the data
1305cc617943765df27844e459362c4bc1821305216Lixin Yue            mTransferAdapter = new BluetoothOppTransferAdapter(this,
1315cc617943765df27844e459362c4bc1821305216Lixin Yue                    R.layout.bluetooth_transfer_item, mTransferCursor);
1325cc617943765df27844e459362c4bc1821305216Lixin Yue            mListView.setAdapter(mTransferAdapter);
1335cc617943765df27844e459362c4bc1821305216Lixin Yue            mListView.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
1345cc617943765df27844e459362c4bc1821305216Lixin Yue            mListView.setOnCreateContextMenuListener(this);
1355cc617943765df27844e459362c4bc1821305216Lixin Yue            mListView.setOnItemClickListener(this);
1365cc617943765df27844e459362c4bc1821305216Lixin Yue        }
137de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue
138de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue        mNotifier = new BluetoothOppNotification(this);
1395cc617943765df27844e459362c4bc1821305216Lixin Yue    }
1405cc617943765df27844e459362c4bc1821305216Lixin Yue
1415cc617943765df27844e459362c4bc1821305216Lixin Yue    @Override
1425cc617943765df27844e459362c4bc1821305216Lixin Yue    public boolean onCreateOptionsMenu(Menu menu) {
1431ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby        if (mTransferCursor != null && !mShowAllIncoming) {
1445cc617943765df27844e459362c4bc1821305216Lixin Yue            MenuInflater inflater = getMenuInflater();
1455cc617943765df27844e459362c4bc1821305216Lixin Yue            inflater.inflate(R.menu.transferhistory, menu);
1465cc617943765df27844e459362c4bc1821305216Lixin Yue        }
1475cc617943765df27844e459362c4bc1821305216Lixin Yue        return true;
1485cc617943765df27844e459362c4bc1821305216Lixin Yue    }
1495cc617943765df27844e459362c4bc1821305216Lixin Yue
1505cc617943765df27844e459362c4bc1821305216Lixin Yue    @Override
1515cc617943765df27844e459362c4bc1821305216Lixin Yue    public boolean onPrepareOptionsMenu(Menu menu) {
1521ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby        if (!mShowAllIncoming) {
1531ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby            boolean showClear = getClearableCount() > 0;
1541ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby            menu.findItem(R.id.transfer_menu_clear_all).setEnabled(showClear);
1551ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby        }
1565cc617943765df27844e459362c4bc1821305216Lixin Yue        return super.onPrepareOptionsMenu(menu);
1575cc617943765df27844e459362c4bc1821305216Lixin Yue    }
1585cc617943765df27844e459362c4bc1821305216Lixin Yue
1595cc617943765df27844e459362c4bc1821305216Lixin Yue    @Override
1605cc617943765df27844e459362c4bc1821305216Lixin Yue    public boolean onOptionsItemSelected(MenuItem item) {
1615cc617943765df27844e459362c4bc1821305216Lixin Yue        switch (item.getItemId()) {
1625cc617943765df27844e459362c4bc1821305216Lixin Yue            case R.id.transfer_menu_clear_all:
1635cc617943765df27844e459362c4bc1821305216Lixin Yue                promptClearList();
1645cc617943765df27844e459362c4bc1821305216Lixin Yue                return true;
1655cc617943765df27844e459362c4bc1821305216Lixin Yue        }
1665cc617943765df27844e459362c4bc1821305216Lixin Yue        return false;
1675cc617943765df27844e459362c4bc1821305216Lixin Yue    }
1685cc617943765df27844e459362c4bc1821305216Lixin Yue
1695cc617943765df27844e459362c4bc1821305216Lixin Yue    @Override
1705cc617943765df27844e459362c4bc1821305216Lixin Yue    public boolean onContextItemSelected(MenuItem item) {
1715cc617943765df27844e459362c4bc1821305216Lixin Yue        mTransferCursor.moveToPosition(mContextMenuPosition);
1725cc617943765df27844e459362c4bc1821305216Lixin Yue        switch (item.getItemId()) {
1735cc617943765df27844e459362c4bc1821305216Lixin Yue            case R.id.transfer_menu_open:
1745cc617943765df27844e459362c4bc1821305216Lixin Yue                openCompleteTransfer();
175de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue                updateNotificationWhenBtDisabled();
1765cc617943765df27844e459362c4bc1821305216Lixin Yue                return true;
1775cc617943765df27844e459362c4bc1821305216Lixin Yue
1785cc617943765df27844e459362c4bc1821305216Lixin Yue            case R.id.transfer_menu_clear:
1795cc617943765df27844e459362c4bc1821305216Lixin Yue                int sessionId = mTransferCursor.getInt(mIdColumnId);
1805cc617943765df27844e459362c4bc1821305216Lixin Yue                Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + sessionId);
1815cc617943765df27844e459362c4bc1821305216Lixin Yue                BluetoothOppUtility.updateVisibilityToHidden(this, contentUri);
182de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue                updateNotificationWhenBtDisabled();
1835cc617943765df27844e459362c4bc1821305216Lixin Yue                return true;
1845cc617943765df27844e459362c4bc1821305216Lixin Yue        }
1855cc617943765df27844e459362c4bc1821305216Lixin Yue        return false;
1865cc617943765df27844e459362c4bc1821305216Lixin Yue    }
1875cc617943765df27844e459362c4bc1821305216Lixin Yue
1885cc617943765df27844e459362c4bc1821305216Lixin Yue    @Override
1895cc617943765df27844e459362c4bc1821305216Lixin Yue    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
1905cc617943765df27844e459362c4bc1821305216Lixin Yue        if (mTransferCursor != null) {
1915cc617943765df27844e459362c4bc1821305216Lixin Yue            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
1925cc617943765df27844e459362c4bc1821305216Lixin Yue            mTransferCursor.moveToPosition(info.position);
1935cc617943765df27844e459362c4bc1821305216Lixin Yue            mContextMenuPosition = info.position;
1945cc617943765df27844e459362c4bc1821305216Lixin Yue
1955cc617943765df27844e459362c4bc1821305216Lixin Yue            String fileName = mTransferCursor.getString(mTransferCursor
1965cc617943765df27844e459362c4bc1821305216Lixin Yue                    .getColumnIndexOrThrow(BluetoothShare.FILENAME_HINT));
1975cc617943765df27844e459362c4bc1821305216Lixin Yue            if (fileName == null) {
1985cc617943765df27844e459362c4bc1821305216Lixin Yue                fileName = this.getString(R.string.unknown_file);
1995cc617943765df27844e459362c4bc1821305216Lixin Yue            }
2005cc617943765df27844e459362c4bc1821305216Lixin Yue            menu.setHeaderTitle(fileName);
2015cc617943765df27844e459362c4bc1821305216Lixin Yue
2025cc617943765df27844e459362c4bc1821305216Lixin Yue            MenuInflater inflater = getMenuInflater();
2031ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby            if (mShowAllIncoming) {
2041ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby                inflater.inflate(R.menu.receivedfilescontextfinished, menu);
2051ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby            } else {
2061ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby                inflater.inflate(R.menu.transferhistorycontextfinished, menu);
2071ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby            }
2085cc617943765df27844e459362c4bc1821305216Lixin Yue        }
2095cc617943765df27844e459362c4bc1821305216Lixin Yue    }
2105cc617943765df27844e459362c4bc1821305216Lixin Yue
2115cc617943765df27844e459362c4bc1821305216Lixin Yue    /**
2125cc617943765df27844e459362c4bc1821305216Lixin Yue     * Prompt the user if they would like to clear the transfer history
2135cc617943765df27844e459362c4bc1821305216Lixin Yue     */
2145cc617943765df27844e459362c4bc1821305216Lixin Yue    private void promptClearList() {
2155cc617943765df27844e459362c4bc1821305216Lixin Yue        new AlertDialog.Builder(this).setTitle(R.string.transfer_clear_dlg_title).setMessage(
2165cc617943765df27844e459362c4bc1821305216Lixin Yue                R.string.transfer_clear_dlg_msg).setPositiveButton(android.R.string.ok,
2175cc617943765df27844e459362c4bc1821305216Lixin Yue                new DialogInterface.OnClickListener() {
2185cc617943765df27844e459362c4bc1821305216Lixin Yue                    public void onClick(DialogInterface dialog, int whichButton) {
2195cc617943765df27844e459362c4bc1821305216Lixin Yue                        clearAllDownloads();
2205cc617943765df27844e459362c4bc1821305216Lixin Yue                    }
2215cc617943765df27844e459362c4bc1821305216Lixin Yue                }).setNegativeButton(android.R.string.cancel, null).show();
2225cc617943765df27844e459362c4bc1821305216Lixin Yue    }
2235cc617943765df27844e459362c4bc1821305216Lixin Yue
2245cc617943765df27844e459362c4bc1821305216Lixin Yue    /**
2255cc617943765df27844e459362c4bc1821305216Lixin Yue     * Get the number of finished transfers, including error and success.
2265cc617943765df27844e459362c4bc1821305216Lixin Yue     */
2275cc617943765df27844e459362c4bc1821305216Lixin Yue    private int getClearableCount() {
2285cc617943765df27844e459362c4bc1821305216Lixin Yue        int count = 0;
2295cc617943765df27844e459362c4bc1821305216Lixin Yue        if (mTransferCursor.moveToFirst()) {
2305cc617943765df27844e459362c4bc1821305216Lixin Yue            while (!mTransferCursor.isAfterLast()) {
2315cc617943765df27844e459362c4bc1821305216Lixin Yue                int statusColumnId = mTransferCursor.getColumnIndexOrThrow(BluetoothShare.STATUS);
2325cc617943765df27844e459362c4bc1821305216Lixin Yue                int status = mTransferCursor.getInt(statusColumnId);
2335cc617943765df27844e459362c4bc1821305216Lixin Yue                if (BluetoothShare.isStatusCompleted(status)) {
2345cc617943765df27844e459362c4bc1821305216Lixin Yue                    count++;
2355cc617943765df27844e459362c4bc1821305216Lixin Yue                }
2365cc617943765df27844e459362c4bc1821305216Lixin Yue                mTransferCursor.moveToNext();
2375cc617943765df27844e459362c4bc1821305216Lixin Yue            }
2385cc617943765df27844e459362c4bc1821305216Lixin Yue        }
2395cc617943765df27844e459362c4bc1821305216Lixin Yue        return count;
2405cc617943765df27844e459362c4bc1821305216Lixin Yue    }
2415cc617943765df27844e459362c4bc1821305216Lixin Yue
2425cc617943765df27844e459362c4bc1821305216Lixin Yue    /**
2435cc617943765df27844e459362c4bc1821305216Lixin Yue     * Clear all finished transfers, error and success transfer items.
2445cc617943765df27844e459362c4bc1821305216Lixin Yue     */
2455cc617943765df27844e459362c4bc1821305216Lixin Yue    private void clearAllDownloads() {
2465cc617943765df27844e459362c4bc1821305216Lixin Yue        if (mTransferCursor.moveToFirst()) {
2475cc617943765df27844e459362c4bc1821305216Lixin Yue            while (!mTransferCursor.isAfterLast()) {
2485cc617943765df27844e459362c4bc1821305216Lixin Yue                int sessionId = mTransferCursor.getInt(mIdColumnId);
2495cc617943765df27844e459362c4bc1821305216Lixin Yue                Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + sessionId);
2505cc617943765df27844e459362c4bc1821305216Lixin Yue                BluetoothOppUtility.updateVisibilityToHidden(this, contentUri);
2515cc617943765df27844e459362c4bc1821305216Lixin Yue
2525cc617943765df27844e459362c4bc1821305216Lixin Yue                mTransferCursor.moveToNext();
2535cc617943765df27844e459362c4bc1821305216Lixin Yue            }
254de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue            updateNotificationWhenBtDisabled();
2555cc617943765df27844e459362c4bc1821305216Lixin Yue        }
2565cc617943765df27844e459362c4bc1821305216Lixin Yue    }
2575cc617943765df27844e459362c4bc1821305216Lixin Yue
2585cc617943765df27844e459362c4bc1821305216Lixin Yue    /*
2595cc617943765df27844e459362c4bc1821305216Lixin Yue     * (non-Javadoc)
2605cc617943765df27844e459362c4bc1821305216Lixin Yue     * @see
2615cc617943765df27844e459362c4bc1821305216Lixin Yue     * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
2625cc617943765df27844e459362c4bc1821305216Lixin Yue     * .AdapterView, android.view.View, int, long)
2635cc617943765df27844e459362c4bc1821305216Lixin Yue     */
264a930b6831d0c70b6c5d34e548e6b1dceaa6529a0Mohammad Shamsi    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
2655cc617943765df27844e459362c4bc1821305216Lixin Yue        // Open the selected item
2665cc617943765df27844e459362c4bc1821305216Lixin Yue        mTransferCursor.moveToPosition(position);
2675cc617943765df27844e459362c4bc1821305216Lixin Yue        openCompleteTransfer();
268de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue        updateNotificationWhenBtDisabled();
2695cc617943765df27844e459362c4bc1821305216Lixin Yue    }
2705cc617943765df27844e459362c4bc1821305216Lixin Yue
2715cc617943765df27844e459362c4bc1821305216Lixin Yue    /**
2725cc617943765df27844e459362c4bc1821305216Lixin Yue     * Open the selected finished transfer. mDownloadCursor must be moved to
2735cc617943765df27844e459362c4bc1821305216Lixin Yue     * appropriate position before calling this function
2745cc617943765df27844e459362c4bc1821305216Lixin Yue     */
2755cc617943765df27844e459362c4bc1821305216Lixin Yue    private void openCompleteTransfer() {
2765cc617943765df27844e459362c4bc1821305216Lixin Yue        int sessionId = mTransferCursor.getInt(mIdColumnId);
2775cc617943765df27844e459362c4bc1821305216Lixin Yue        Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + sessionId);
2781ed96d522298e8f48690a4f4cbf2e0c6b402e3a0Jake Hamby        BluetoothOppTransferInfo transInfo = BluetoothOppUtility.queryRecord(this, contentUri);
2795cc617943765df27844e459362c4bc1821305216Lixin Yue        if (transInfo == null) {
2805cc617943765df27844e459362c4bc1821305216Lixin Yue            Log.e(TAG, "Error: Can not get data from db");
2815cc617943765df27844e459362c4bc1821305216Lixin Yue            return;
2825cc617943765df27844e459362c4bc1821305216Lixin Yue        }
2835cc617943765df27844e459362c4bc1821305216Lixin Yue        if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND
2845cc617943765df27844e459362c4bc1821305216Lixin Yue                && BluetoothShare.isStatusSuccess(transInfo.mStatus)) {
2855cc617943765df27844e459362c4bc1821305216Lixin Yue            // if received file successfully, open this file
2865cc617943765df27844e459362c4bc1821305216Lixin Yue            BluetoothOppUtility.updateVisibilityToHidden(this, contentUri);
2875cc617943765df27844e459362c4bc1821305216Lixin Yue            BluetoothOppUtility.openReceivedFile(this, transInfo.mFileName, transInfo.mFileType,
2885cc617943765df27844e459362c4bc1821305216Lixin Yue                    transInfo.mTimeStamp, contentUri);
2895cc617943765df27844e459362c4bc1821305216Lixin Yue        } else {
2905cc617943765df27844e459362c4bc1821305216Lixin Yue            Intent in = new Intent(this, BluetoothOppTransferActivity.class);
2915cc617943765df27844e459362c4bc1821305216Lixin Yue            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2925cc617943765df27844e459362c4bc1821305216Lixin Yue            in.setData(contentUri);
2935cc617943765df27844e459362c4bc1821305216Lixin Yue            this.startActivity(in);
2945cc617943765df27844e459362c4bc1821305216Lixin Yue        }
2955cc617943765df27844e459362c4bc1821305216Lixin Yue    }
296de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue
297de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue    /**
298de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue     * When Bluetooth is disabled, notification can not be updated by
299de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue     * ContentObserver in OppService, so need update manually.
300de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue     */
301de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue    private void updateNotificationWhenBtDisabled() {
302de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
303de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue        if (!adapter.isEnabled()) {
304de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue            if (V) Log.v(TAG, "Bluetooth is not enabled, update notification manually.");
305de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue            mNotifier.updateNotification();
306de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue        }
307de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue    }
3085cc617943765df27844e459362c4bc1821305216Lixin Yue}
309