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 D = Constants.DEBUG;
665cc617943765df27844e459362c4bc1821305216Lixin Yue
675cc617943765df27844e459362c4bc1821305216Lixin Yue    private static final boolean V = Constants.VERBOSE;
685cc617943765df27844e459362c4bc1821305216Lixin Yue
695cc617943765df27844e459362c4bc1821305216Lixin Yue    private ListView mListView;
705cc617943765df27844e459362c4bc1821305216Lixin Yue
715cc617943765df27844e459362c4bc1821305216Lixin Yue    private Cursor mTransferCursor;
725cc617943765df27844e459362c4bc1821305216Lixin Yue
735cc617943765df27844e459362c4bc1821305216Lixin Yue    private BluetoothOppTransferAdapter mTransferAdapter;
745cc617943765df27844e459362c4bc1821305216Lixin Yue
755cc617943765df27844e459362c4bc1821305216Lixin Yue    private int mIdColumnId;
765cc617943765df27844e459362c4bc1821305216Lixin Yue
775cc617943765df27844e459362c4bc1821305216Lixin Yue    private int mContextMenuPosition;
785cc617943765df27844e459362c4bc1821305216Lixin Yue
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
895cc617943765df27844e459362c4bc1821305216Lixin Yue        String direction;
905cc617943765df27844e459362c4bc1821305216Lixin Yue        int dir = getIntent().getIntExtra("direction", 0);
915cc617943765df27844e459362c4bc1821305216Lixin Yue        if (dir == BluetoothShare.DIRECTION_OUTBOUND) {
925cc617943765df27844e459362c4bc1821305216Lixin Yue            setTitle(getText(R.string.outbound_history_title));
935cc617943765df27844e459362c4bc1821305216Lixin Yue            direction = "(" + BluetoothShare.DIRECTION + " == " + BluetoothShare.DIRECTION_OUTBOUND
945cc617943765df27844e459362c4bc1821305216Lixin Yue                    + ")";
955cc617943765df27844e459362c4bc1821305216Lixin Yue        } else {
965cc617943765df27844e459362c4bc1821305216Lixin Yue            setTitle(getText(R.string.inbound_history_title));
975cc617943765df27844e459362c4bc1821305216Lixin Yue            direction = "(" + BluetoothShare.DIRECTION + " == " + BluetoothShare.DIRECTION_INBOUND
985cc617943765df27844e459362c4bc1821305216Lixin Yue                    + ")";
995cc617943765df27844e459362c4bc1821305216Lixin Yue        }
1005cc617943765df27844e459362c4bc1821305216Lixin Yue
1015cc617943765df27844e459362c4bc1821305216Lixin Yue        final String selection = BluetoothShare.STATUS + " >= '200' AND " + "("
1025cc617943765df27844e459362c4bc1821305216Lixin Yue                + BluetoothShare.VISIBILITY + " IS NULL OR " + BluetoothShare.VISIBILITY + " == '"
1035cc617943765df27844e459362c4bc1821305216Lixin Yue                + BluetoothShare.VISIBILITY_VISIBLE + "'" + ")" + " AND " + direction;
1045cc617943765df27844e459362c4bc1821305216Lixin Yue        final String sortOrder = BluetoothShare.TIMESTAMP + " DESC";
1055cc617943765df27844e459362c4bc1821305216Lixin Yue
1065cc617943765df27844e459362c4bc1821305216Lixin Yue        mTransferCursor = managedQuery(BluetoothShare.CONTENT_URI, new String[] {
1075cc617943765df27844e459362c4bc1821305216Lixin Yue                "_id", BluetoothShare.FILENAME_HINT, BluetoothShare.STATUS,
1085cc617943765df27844e459362c4bc1821305216Lixin Yue                BluetoothShare.TOTAL_BYTES, BluetoothShare._DATA, BluetoothShare.TIMESTAMP,
1095cc617943765df27844e459362c4bc1821305216Lixin Yue                BluetoothShare.VISIBILITY, BluetoothShare.DESTINATION, BluetoothShare.DIRECTION
1105cc617943765df27844e459362c4bc1821305216Lixin Yue        }, selection, sortOrder);
1115cc617943765df27844e459362c4bc1821305216Lixin Yue
1125cc617943765df27844e459362c4bc1821305216Lixin Yue        // only attach everything to the listbox if we can access
1135cc617943765df27844e459362c4bc1821305216Lixin Yue        // the transfer database. Otherwise, just show it empty
1145cc617943765df27844e459362c4bc1821305216Lixin Yue        if (mTransferCursor != null) {
1155cc617943765df27844e459362c4bc1821305216Lixin Yue            mIdColumnId = mTransferCursor.getColumnIndexOrThrow(BluetoothShare._ID);
1165cc617943765df27844e459362c4bc1821305216Lixin Yue            // Create a list "controller" for the data
1175cc617943765df27844e459362c4bc1821305216Lixin Yue            mTransferAdapter = new BluetoothOppTransferAdapter(this,
1185cc617943765df27844e459362c4bc1821305216Lixin Yue                    R.layout.bluetooth_transfer_item, mTransferCursor);
1195cc617943765df27844e459362c4bc1821305216Lixin Yue            mListView.setAdapter(mTransferAdapter);
1205cc617943765df27844e459362c4bc1821305216Lixin Yue            mListView.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
1215cc617943765df27844e459362c4bc1821305216Lixin Yue            mListView.setOnCreateContextMenuListener(this);
1225cc617943765df27844e459362c4bc1821305216Lixin Yue            mListView.setOnItemClickListener(this);
1235cc617943765df27844e459362c4bc1821305216Lixin Yue        }
124de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue
125de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue        mNotifier = new BluetoothOppNotification(this);
1265cc617943765df27844e459362c4bc1821305216Lixin Yue    }
1275cc617943765df27844e459362c4bc1821305216Lixin Yue
1285cc617943765df27844e459362c4bc1821305216Lixin Yue    @Override
1295cc617943765df27844e459362c4bc1821305216Lixin Yue    public boolean onCreateOptionsMenu(Menu menu) {
1305cc617943765df27844e459362c4bc1821305216Lixin Yue        if (mTransferCursor != null) {
1315cc617943765df27844e459362c4bc1821305216Lixin Yue            MenuInflater inflater = getMenuInflater();
1325cc617943765df27844e459362c4bc1821305216Lixin Yue            inflater.inflate(R.menu.transferhistory, menu);
1335cc617943765df27844e459362c4bc1821305216Lixin Yue        }
1345cc617943765df27844e459362c4bc1821305216Lixin Yue        return true;
1355cc617943765df27844e459362c4bc1821305216Lixin Yue    }
1365cc617943765df27844e459362c4bc1821305216Lixin Yue
1375cc617943765df27844e459362c4bc1821305216Lixin Yue    @Override
1385cc617943765df27844e459362c4bc1821305216Lixin Yue    public boolean onPrepareOptionsMenu(Menu menu) {
1395cc617943765df27844e459362c4bc1821305216Lixin Yue        boolean showClear = getClearableCount() > 0;
1405cc617943765df27844e459362c4bc1821305216Lixin Yue        menu.findItem(R.id.transfer_menu_clear_all).setEnabled(showClear);
1415cc617943765df27844e459362c4bc1821305216Lixin Yue        return super.onPrepareOptionsMenu(menu);
1425cc617943765df27844e459362c4bc1821305216Lixin Yue    }
1435cc617943765df27844e459362c4bc1821305216Lixin Yue
1445cc617943765df27844e459362c4bc1821305216Lixin Yue    @Override
1455cc617943765df27844e459362c4bc1821305216Lixin Yue    public boolean onOptionsItemSelected(MenuItem item) {
1465cc617943765df27844e459362c4bc1821305216Lixin Yue        switch (item.getItemId()) {
1475cc617943765df27844e459362c4bc1821305216Lixin Yue            case R.id.transfer_menu_clear_all:
1485cc617943765df27844e459362c4bc1821305216Lixin Yue                promptClearList();
1495cc617943765df27844e459362c4bc1821305216Lixin Yue                return true;
1505cc617943765df27844e459362c4bc1821305216Lixin Yue        }
1515cc617943765df27844e459362c4bc1821305216Lixin Yue        return false;
1525cc617943765df27844e459362c4bc1821305216Lixin Yue    }
1535cc617943765df27844e459362c4bc1821305216Lixin Yue
1545cc617943765df27844e459362c4bc1821305216Lixin Yue    @Override
1555cc617943765df27844e459362c4bc1821305216Lixin Yue    public boolean onContextItemSelected(MenuItem item) {
1565cc617943765df27844e459362c4bc1821305216Lixin Yue        mTransferCursor.moveToPosition(mContextMenuPosition);
1575cc617943765df27844e459362c4bc1821305216Lixin Yue        switch (item.getItemId()) {
1585cc617943765df27844e459362c4bc1821305216Lixin Yue            case R.id.transfer_menu_open:
1595cc617943765df27844e459362c4bc1821305216Lixin Yue                openCompleteTransfer();
160de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue                updateNotificationWhenBtDisabled();
1615cc617943765df27844e459362c4bc1821305216Lixin Yue                return true;
1625cc617943765df27844e459362c4bc1821305216Lixin Yue
1635cc617943765df27844e459362c4bc1821305216Lixin Yue            case R.id.transfer_menu_clear:
1645cc617943765df27844e459362c4bc1821305216Lixin Yue                int sessionId = mTransferCursor.getInt(mIdColumnId);
1655cc617943765df27844e459362c4bc1821305216Lixin Yue                Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + sessionId);
1665cc617943765df27844e459362c4bc1821305216Lixin Yue                BluetoothOppUtility.updateVisibilityToHidden(this, contentUri);
167de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue                updateNotificationWhenBtDisabled();
1685cc617943765df27844e459362c4bc1821305216Lixin Yue                return true;
1695cc617943765df27844e459362c4bc1821305216Lixin Yue        }
1705cc617943765df27844e459362c4bc1821305216Lixin Yue        return false;
1715cc617943765df27844e459362c4bc1821305216Lixin Yue    }
1725cc617943765df27844e459362c4bc1821305216Lixin Yue
1735cc617943765df27844e459362c4bc1821305216Lixin Yue    @Override
1745cc617943765df27844e459362c4bc1821305216Lixin Yue    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
1755cc617943765df27844e459362c4bc1821305216Lixin Yue        if (mTransferCursor != null) {
1765cc617943765df27844e459362c4bc1821305216Lixin Yue            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
1775cc617943765df27844e459362c4bc1821305216Lixin Yue            mTransferCursor.moveToPosition(info.position);
1785cc617943765df27844e459362c4bc1821305216Lixin Yue            mContextMenuPosition = info.position;
1795cc617943765df27844e459362c4bc1821305216Lixin Yue
1805cc617943765df27844e459362c4bc1821305216Lixin Yue            String fileName = mTransferCursor.getString(mTransferCursor
1815cc617943765df27844e459362c4bc1821305216Lixin Yue                    .getColumnIndexOrThrow(BluetoothShare.FILENAME_HINT));
1825cc617943765df27844e459362c4bc1821305216Lixin Yue            if (fileName == null) {
1835cc617943765df27844e459362c4bc1821305216Lixin Yue                fileName = this.getString(R.string.unknown_file);
1845cc617943765df27844e459362c4bc1821305216Lixin Yue            }
1855cc617943765df27844e459362c4bc1821305216Lixin Yue            menu.setHeaderTitle(fileName);
1865cc617943765df27844e459362c4bc1821305216Lixin Yue
1875cc617943765df27844e459362c4bc1821305216Lixin Yue            MenuInflater inflater = getMenuInflater();
1885cc617943765df27844e459362c4bc1821305216Lixin Yue            inflater.inflate(R.menu.transferhistorycontextfinished, menu);
1895cc617943765df27844e459362c4bc1821305216Lixin Yue        }
1905cc617943765df27844e459362c4bc1821305216Lixin Yue    }
1915cc617943765df27844e459362c4bc1821305216Lixin Yue
1925cc617943765df27844e459362c4bc1821305216Lixin Yue    /**
1935cc617943765df27844e459362c4bc1821305216Lixin Yue     * Prompt the user if they would like to clear the transfer history
1945cc617943765df27844e459362c4bc1821305216Lixin Yue     */
1955cc617943765df27844e459362c4bc1821305216Lixin Yue    private void promptClearList() {
1965cc617943765df27844e459362c4bc1821305216Lixin Yue        new AlertDialog.Builder(this).setTitle(R.string.transfer_clear_dlg_title).setMessage(
1975cc617943765df27844e459362c4bc1821305216Lixin Yue                R.string.transfer_clear_dlg_msg).setPositiveButton(android.R.string.ok,
1985cc617943765df27844e459362c4bc1821305216Lixin Yue                new DialogInterface.OnClickListener() {
1995cc617943765df27844e459362c4bc1821305216Lixin Yue                    public void onClick(DialogInterface dialog, int whichButton) {
2005cc617943765df27844e459362c4bc1821305216Lixin Yue                        clearAllDownloads();
2015cc617943765df27844e459362c4bc1821305216Lixin Yue                    }
2025cc617943765df27844e459362c4bc1821305216Lixin Yue                }).setNegativeButton(android.R.string.cancel, null).show();
2035cc617943765df27844e459362c4bc1821305216Lixin Yue    }
2045cc617943765df27844e459362c4bc1821305216Lixin Yue
2055cc617943765df27844e459362c4bc1821305216Lixin Yue    /**
2065cc617943765df27844e459362c4bc1821305216Lixin Yue     * Get the number of finished transfers, including error and success.
2075cc617943765df27844e459362c4bc1821305216Lixin Yue     */
2085cc617943765df27844e459362c4bc1821305216Lixin Yue    private int getClearableCount() {
2095cc617943765df27844e459362c4bc1821305216Lixin Yue        int count = 0;
2105cc617943765df27844e459362c4bc1821305216Lixin Yue        if (mTransferCursor.moveToFirst()) {
2115cc617943765df27844e459362c4bc1821305216Lixin Yue            while (!mTransferCursor.isAfterLast()) {
2125cc617943765df27844e459362c4bc1821305216Lixin Yue                int statusColumnId = mTransferCursor.getColumnIndexOrThrow(BluetoothShare.STATUS);
2135cc617943765df27844e459362c4bc1821305216Lixin Yue                int status = mTransferCursor.getInt(statusColumnId);
2145cc617943765df27844e459362c4bc1821305216Lixin Yue                if (BluetoothShare.isStatusCompleted(status)) {
2155cc617943765df27844e459362c4bc1821305216Lixin Yue                    count++;
2165cc617943765df27844e459362c4bc1821305216Lixin Yue                }
2175cc617943765df27844e459362c4bc1821305216Lixin Yue                mTransferCursor.moveToNext();
2185cc617943765df27844e459362c4bc1821305216Lixin Yue            }
2195cc617943765df27844e459362c4bc1821305216Lixin Yue        }
2205cc617943765df27844e459362c4bc1821305216Lixin Yue        return count;
2215cc617943765df27844e459362c4bc1821305216Lixin Yue    }
2225cc617943765df27844e459362c4bc1821305216Lixin Yue
2235cc617943765df27844e459362c4bc1821305216Lixin Yue    /**
2245cc617943765df27844e459362c4bc1821305216Lixin Yue     * Clear all finished transfers, error and success transfer items.
2255cc617943765df27844e459362c4bc1821305216Lixin Yue     */
2265cc617943765df27844e459362c4bc1821305216Lixin Yue    private void clearAllDownloads() {
2275cc617943765df27844e459362c4bc1821305216Lixin Yue        if (mTransferCursor.moveToFirst()) {
2285cc617943765df27844e459362c4bc1821305216Lixin Yue            while (!mTransferCursor.isAfterLast()) {
2295cc617943765df27844e459362c4bc1821305216Lixin Yue                int sessionId = mTransferCursor.getInt(mIdColumnId);
2305cc617943765df27844e459362c4bc1821305216Lixin Yue                Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + sessionId);
2315cc617943765df27844e459362c4bc1821305216Lixin Yue                BluetoothOppUtility.updateVisibilityToHidden(this, contentUri);
2325cc617943765df27844e459362c4bc1821305216Lixin Yue
2335cc617943765df27844e459362c4bc1821305216Lixin Yue                mTransferCursor.moveToNext();
2345cc617943765df27844e459362c4bc1821305216Lixin Yue            }
235de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue            updateNotificationWhenBtDisabled();
2365cc617943765df27844e459362c4bc1821305216Lixin Yue        }
2375cc617943765df27844e459362c4bc1821305216Lixin Yue    }
2385cc617943765df27844e459362c4bc1821305216Lixin Yue
2395cc617943765df27844e459362c4bc1821305216Lixin Yue    /*
2405cc617943765df27844e459362c4bc1821305216Lixin Yue     * (non-Javadoc)
2415cc617943765df27844e459362c4bc1821305216Lixin Yue     * @see
2425cc617943765df27844e459362c4bc1821305216Lixin Yue     * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
2435cc617943765df27844e459362c4bc1821305216Lixin Yue     * .AdapterView, android.view.View, int, long)
2445cc617943765df27844e459362c4bc1821305216Lixin Yue     */
2455cc617943765df27844e459362c4bc1821305216Lixin Yue    public void onItemClick(AdapterView parent, View view, int position, long id) {
2465cc617943765df27844e459362c4bc1821305216Lixin Yue        // Open the selected item
2475cc617943765df27844e459362c4bc1821305216Lixin Yue        mTransferCursor.moveToPosition(position);
2485cc617943765df27844e459362c4bc1821305216Lixin Yue        openCompleteTransfer();
249de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue        updateNotificationWhenBtDisabled();
2505cc617943765df27844e459362c4bc1821305216Lixin Yue    }
2515cc617943765df27844e459362c4bc1821305216Lixin Yue
2525cc617943765df27844e459362c4bc1821305216Lixin Yue    /**
2535cc617943765df27844e459362c4bc1821305216Lixin Yue     * Open the selected finished transfer. mDownloadCursor must be moved to
2545cc617943765df27844e459362c4bc1821305216Lixin Yue     * appropriate position before calling this function
2555cc617943765df27844e459362c4bc1821305216Lixin Yue     */
2565cc617943765df27844e459362c4bc1821305216Lixin Yue    private void openCompleteTransfer() {
2575cc617943765df27844e459362c4bc1821305216Lixin Yue        int sessionId = mTransferCursor.getInt(mIdColumnId);
2585cc617943765df27844e459362c4bc1821305216Lixin Yue        Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + sessionId);
2595cc617943765df27844e459362c4bc1821305216Lixin Yue        BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo();
2605cc617943765df27844e459362c4bc1821305216Lixin Yue        transInfo = BluetoothOppUtility.queryRecord(this, contentUri);
2615cc617943765df27844e459362c4bc1821305216Lixin Yue        if (transInfo == null) {
2625cc617943765df27844e459362c4bc1821305216Lixin Yue            Log.e(TAG, "Error: Can not get data from db");
2635cc617943765df27844e459362c4bc1821305216Lixin Yue            return;
2645cc617943765df27844e459362c4bc1821305216Lixin Yue        }
2655cc617943765df27844e459362c4bc1821305216Lixin Yue        if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND
2665cc617943765df27844e459362c4bc1821305216Lixin Yue                && BluetoothShare.isStatusSuccess(transInfo.mStatus)) {
2675cc617943765df27844e459362c4bc1821305216Lixin Yue            // if received file successfully, open this file
2685cc617943765df27844e459362c4bc1821305216Lixin Yue            BluetoothOppUtility.updateVisibilityToHidden(this, contentUri);
2695cc617943765df27844e459362c4bc1821305216Lixin Yue            BluetoothOppUtility.openReceivedFile(this, transInfo.mFileName, transInfo.mFileType,
2705cc617943765df27844e459362c4bc1821305216Lixin Yue                    transInfo.mTimeStamp, contentUri);
2715cc617943765df27844e459362c4bc1821305216Lixin Yue        } else {
2725cc617943765df27844e459362c4bc1821305216Lixin Yue            Intent in = new Intent(this, BluetoothOppTransferActivity.class);
2735cc617943765df27844e459362c4bc1821305216Lixin Yue            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2745cc617943765df27844e459362c4bc1821305216Lixin Yue            in.setData(contentUri);
2755cc617943765df27844e459362c4bc1821305216Lixin Yue            this.startActivity(in);
2765cc617943765df27844e459362c4bc1821305216Lixin Yue        }
2775cc617943765df27844e459362c4bc1821305216Lixin Yue    }
278de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue
279de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue    /**
280de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue     * When Bluetooth is disabled, notification can not be updated by
281de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue     * ContentObserver in OppService, so need update manually.
282de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue     */
283de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue    private void updateNotificationWhenBtDisabled() {
284de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
285de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue        if (!adapter.isEnabled()) {
286de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue            if (V) Log.v(TAG, "Bluetooth is not enabled, update notification manually.");
287de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue            mNotifier.updateNotification();
288de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue        }
289de773e60fed15a4ad13325089a5609c9bc6ede59Lixin Yue    }
2905cc617943765df27844e459362c4bc1821305216Lixin Yue}
291