109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly/*
209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * Copyright (c) 2008-2009, Motorola, Inc.
309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly *
409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * All rights reserved.
509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly *
609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * Redistribution and use in source and binary forms, with or without
709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * modification, are permitted provided that the following conditions are met:
809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly *
909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * - Redistributions of source code must retain the above copyright notice,
1009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * this list of conditions and the following disclaimer.
1109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly *
1209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * - Redistributions in binary form must reproduce the above copyright notice,
1309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * this list of conditions and the following disclaimer in the documentation
1409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * and/or other materials provided with the distribution.
1509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly *
1609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * - Neither the name of the Motorola, Inc. nor the names of its contributors
1709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * may be used to endorse or promote products derived from this software
1809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * without specific prior written permission.
1909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly *
2009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * POSSIBILITY OF SUCH DAMAGE.
3109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly */
3209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
3309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellypackage com.android.bluetooth.opp;
3409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
3509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.ContentProvider;
3609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.ContentValues;
3709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.Context;
3809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.Intent;
3909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.database.Cursor;
4009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.database.SQLException;
4109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.UriMatcher;
4209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.database.sqlite.SQLiteDatabase;
4309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.database.sqlite.SQLiteOpenHelper;
4409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.database.sqlite.SQLiteQueryBuilder;
4509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.net.Uri;
4609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.provider.LiveFolders;
4709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.util.Log;
4809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
4909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport java.util.HashMap;
5009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
5109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly/**
526769b59d715ea98bd72eafcfea9acd2714a887daTao Liejun * This provider allows application to interact with Bluetooth OPP manager
5309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly */
5409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
5509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellypublic final class BluetoothOppProvider extends ContentProvider {
5609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
57ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly    private static final String TAG = "BluetoothOppProvider";
58ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly    private static final boolean D = Constants.DEBUG;
59ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly    private static final boolean V = Constants.VERBOSE;
601ac5507790a87810061a19dadec36eb328a222eaTao Liejun
6109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /** Database filename */
6209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final String DB_NAME = "btopp.db";
6309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
6409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /** Current database version */
6509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final int DB_VERSION = 1;
6609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
6709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /** Database version from which upgrading is a nop */
6809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final int DB_VERSION_NOP_UPGRADE_FROM = 0;
6909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
7009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /** Database version to which upgrading is a nop */
7109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final int DB_VERSION_NOP_UPGRADE_TO = 1;
7209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
7309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /** Name of table in the database */
7409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final String DB_TABLE = "btopp";
7509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
7609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /** MIME type for the entire share list */
7709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final String SHARE_LIST_TYPE = "vnd.android.cursor.dir/vnd.android.btopp";
7809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
7909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /** MIME type for an individual share */
8009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final String SHARE_TYPE = "vnd.android.cursor.item/vnd.android.btopp";
8109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
8209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /** URI matcher used to recognize URIs sent by applications */
8309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
8409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
8509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /** URI matcher constant for the URI of the entire share list */
8609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final int SHARES = 1;
8709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
8809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /** URI matcher constant for the URI of an individual share */
8909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final int SHARES_ID = 2;
9009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
9109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /** URI matcher constant for the URI of live folder */
9209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final int LIVE_FOLDER_RECEIVED_FILES = 3;
9309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    static {
9409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        sURIMatcher.addURI("com.android.bluetooth.opp", "btopp", SHARES);
9509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        sURIMatcher.addURI("com.android.bluetooth.opp", "btopp/#", SHARES_ID);
9609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        sURIMatcher.addURI("com.android.bluetooth.opp", "live_folders/received",
9709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                LIVE_FOLDER_RECEIVED_FILES);
9809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
9909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
10009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final HashMap<String, String> LIVE_FOLDER_PROJECTION_MAP;
10109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    static {
10209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        LIVE_FOLDER_PROJECTION_MAP = new HashMap<String, String>();
10309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        LIVE_FOLDER_PROJECTION_MAP.put(LiveFolders._ID, BluetoothShare._ID + " AS "
10409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                + LiveFolders._ID);
10509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        LIVE_FOLDER_PROJECTION_MAP.put(LiveFolders.NAME, BluetoothShare.FILENAME_HINT + " AS "
10609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                + LiveFolders.NAME);
10709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
10809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
10909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /** The database that lies underneath this content provider */
11009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private SQLiteOpenHelper mOpenHelper = null;
11109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
11209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /**
11309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly     * Creates and updated database on demand when opening it. Helper class to
11409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly     * create database the first time the provider is initialized and upgrade it
11509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly     * when a new version of the provider needs an updated version of the
11609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly     * database.
11709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly     */
11809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private final class DatabaseHelper extends SQLiteOpenHelper {
11909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
12009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        public DatabaseHelper(final Context context) {
12109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            super(context, DB_NAME, null, DB_VERSION);
12209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
12309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
12409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        /**
12509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly         * Creates database the first time we try to open it.
12609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly         */
12709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        @Override
12809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        public void onCreate(final SQLiteDatabase db) {
129ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly            if (V) Log.v(TAG, "populating new database");
13009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            createTable(db);
13109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
13209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
13309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        //TODO: use this function to check garbage transfer left in db, for example,
13409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        // a crash incoming file
13509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        /*
13609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly         * (not a javadoc comment) Checks data integrity when opening the
13709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly         * database.
13809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly         */
13909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        /*
14009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly         * @Override public void onOpen(final SQLiteDatabase db) {
14109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly         * super.onOpen(db); }
14209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly         */
14309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
14409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        /**
14509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly         * Updates the database format when a content provider is used with a
14609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly         * database that was created with a different format.
14709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly         */
14809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        // Note: technically, this could also be a downgrade, so if we want
14909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        // to gracefully handle upgrades we should be careful about
15009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        // what to do on downgrades.
15109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        @Override
15209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        public void onUpgrade(final SQLiteDatabase db, int oldV, final int newV) {
15309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            if (oldV == DB_VERSION_NOP_UPGRADE_FROM) {
15409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                if (newV == DB_VERSION_NOP_UPGRADE_TO) { // that's a no-op
15509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    // upgrade.
15609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    return;
15709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                }
15809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                // NOP_FROM and NOP_TO are identical, just in different
15909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                // codelines. Upgrading
16009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                // from NOP_FROM is the same as upgrading from NOP_TO.
16109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                oldV = DB_VERSION_NOP_UPGRADE_TO;
16209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
1631ac5507790a87810061a19dadec36eb328a222eaTao Liejun            Log.i(TAG, "Upgrading downloads database from version " + oldV + " to "
16409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    + newV + ", which will destroy all old data");
16509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            dropTable(db);
16609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            createTable(db);
16709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
16809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
16909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
17009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
17109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private void createTable(SQLiteDatabase db) {
17209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        try {
17309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            db.execSQL("CREATE TABLE " + DB_TABLE + "(" + BluetoothShare._ID
17409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    + " INTEGER PRIMARY KEY AUTOINCREMENT," + BluetoothShare.URI + " TEXT, "
17509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    + BluetoothShare.FILENAME_HINT + " TEXT, " + BluetoothShare._DATA + " TEXT, "
17609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    + BluetoothShare.MIMETYPE + " TEXT, " + BluetoothShare.DIRECTION + " INTEGER, "
17709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    + BluetoothShare.DESTINATION + " TEXT, " + BluetoothShare.VISIBILITY
17809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    + " INTEGER, " + BluetoothShare.USER_CONFIRMATION + " INTEGER, "
17909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    + BluetoothShare.STATUS + " INTEGER, " + BluetoothShare.TOTAL_BYTES
18009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    + " INTEGER, " + BluetoothShare.CURRENT_BYTES + " INTEGER, "
18109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    + BluetoothShare.TIMESTAMP + " INTEGER," + Constants.MEDIA_SCANNED
18209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    + " INTEGER); ");
18309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        } catch (SQLException ex) {
1841ac5507790a87810061a19dadec36eb328a222eaTao Liejun            Log.e(TAG, "couldn't create table in downloads database");
18509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            throw ex;
18609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
18709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
18809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
18909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private void dropTable(SQLiteDatabase db) {
19009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        try {
19109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
19209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        } catch (SQLException ex) {
1931ac5507790a87810061a19dadec36eb328a222eaTao Liejun            Log.e(TAG, "couldn't drop table in downloads database");
19409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            throw ex;
19509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
19609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
19709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
19809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    @Override
19909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    public String getType(Uri uri) {
20009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        int match = sURIMatcher.match(uri);
20109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        switch (match) {
20209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            case SHARES: {
20309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                return SHARE_LIST_TYPE;
20409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
20509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            case SHARES_ID: {
20609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                return SHARE_TYPE;
20709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
20809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            default: {
209ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly                if (D) Log.d(TAG, "calling getType on an unknown URI: " + uri);
21009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                throw new IllegalArgumentException("Unknown URI: " + uri);
21109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
21209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
21309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
21409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
21509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final void copyString(String key, ContentValues from, ContentValues to) {
21609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        String s = from.getAsString(key);
21709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        if (s != null) {
21809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            to.put(key, s);
21909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
22009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
22109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
22209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final void copyInteger(String key, ContentValues from, ContentValues to) {
22309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        Integer i = from.getAsInteger(key);
22409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        if (i != null) {
22509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            to.put(key, i);
22609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
22709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
22809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
22909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    @Override
23009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    public Uri insert(Uri uri, ContentValues values) {
23109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
23209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
23309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        if (sURIMatcher.match(uri) != SHARES) {
234ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly            if (D) Log.d(TAG, "calling insert on an unknown/invalid URI: " + uri);
23509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            throw new IllegalArgumentException("Unknown/Invalid URI " + uri);
23609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
23709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
23809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        ContentValues filteredValues = new ContentValues();
23909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
24009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        copyString(BluetoothShare.URI, values, filteredValues);
24109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        copyString(BluetoothShare.FILENAME_HINT, values, filteredValues);
24209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        copyString(BluetoothShare.MIMETYPE, values, filteredValues);
24309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        copyString(BluetoothShare.DESTINATION, values, filteredValues);
24409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
24509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        copyInteger(BluetoothShare.VISIBILITY, values, filteredValues);
24609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        copyInteger(BluetoothShare.TOTAL_BYTES, values, filteredValues);
24709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
24809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        if (values.getAsInteger(BluetoothShare.VISIBILITY) == null) {
24909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            filteredValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_VISIBLE);
25009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
25109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        Integer dir = values.getAsInteger(BluetoothShare.DIRECTION);
25209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        Integer con = values.getAsInteger(BluetoothShare.USER_CONFIRMATION);
25309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
25409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        if (values.getAsInteger(BluetoothShare.DIRECTION) == null) {
25509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            dir = BluetoothShare.DIRECTION_OUTBOUND;
25609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
25709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        if (dir == BluetoothShare.DIRECTION_OUTBOUND && con == null) {
25809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            con = BluetoothShare.USER_CONFIRMATION_AUTO_CONFIRMED;
25909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
26009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        if (dir == BluetoothShare.DIRECTION_INBOUND && con == null) {
26109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            con = BluetoothShare.USER_CONFIRMATION_PENDING;
26209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
26309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        filteredValues.put(BluetoothShare.USER_CONFIRMATION, con);
26409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        filteredValues.put(BluetoothShare.DIRECTION, dir);
26509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
26609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        filteredValues.put(BluetoothShare.STATUS, BluetoothShare.STATUS_PENDING);
26709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        filteredValues.put(Constants.MEDIA_SCANNED, 0);
26809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
26909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        Long ts = values.getAsLong(BluetoothShare.TIMESTAMP);
27009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        if (ts == null) {
27109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            ts = System.currentTimeMillis();
27209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
27309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        filteredValues.put(BluetoothShare.TIMESTAMP, ts);
27409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
27509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        Context context = getContext();
27609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        context.startService(new Intent(context, BluetoothOppService.class));
27709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
27809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        long rowID = db.insert(DB_TABLE, null, filteredValues);
27909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
28009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        Uri ret = null;
28109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
28209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        if (rowID != -1) {
28309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            context.startService(new Intent(context, BluetoothOppService.class));
28409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            ret = Uri.parse(BluetoothShare.CONTENT_URI + "/" + rowID);
28509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            context.getContentResolver().notifyChange(uri, null);
28609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        } else {
287ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly            if (D) Log.d(TAG, "couldn't insert into btopp database");
28809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
28909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
29009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        return ret;
29109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
29209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
29309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    @Override
29409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    public boolean onCreate() {
29509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        mOpenHelper = new DatabaseHelper(getContext());
29609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        return true;
29709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
29809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
29909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    @Override
30009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
30109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            String sortOrder) {
30209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
30309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
30409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
30509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
30609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        int match = sURIMatcher.match(uri);
30709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        switch (match) {
30809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            case SHARES: {
30909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                qb.setTables(DB_TABLE);
31009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                break;
31109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
31209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            case SHARES_ID: {
31309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                qb.setTables(DB_TABLE);
31409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                qb.appendWhere(BluetoothShare._ID + "=");
31509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                qb.appendWhere(uri.getPathSegments().get(1));
31609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                break;
31709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
31809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            case LIVE_FOLDER_RECEIVED_FILES: {
31909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                qb.setTables(DB_TABLE);
32009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                qb.setProjectionMap(LIVE_FOLDER_PROJECTION_MAP);
32109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                qb.appendWhere(BluetoothShare.DIRECTION + "=" + BluetoothShare.DIRECTION_INBOUND
32209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                        + " AND " + BluetoothShare.STATUS + "=" + BluetoothShare.STATUS_SUCCESS);
323239bc526513429995c61c4148c105725c395b1a9Jackson Fan                sortOrder = "_id DESC, " + sortOrder;
32409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                break;
32509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
32609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            default: {
327ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly                if (D) Log.d(TAG, "querying unknown URI: " + uri);
32809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                throw new IllegalArgumentException("Unknown URI: " + uri);
32909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
33009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
33109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
332ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly        if (V) {
33309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            java.lang.StringBuilder sb = new java.lang.StringBuilder();
33409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            sb.append("starting query, database is ");
33509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            if (db != null) {
33609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                sb.append("not ");
33709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
33809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            sb.append("null; ");
33909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            if (projection == null) {
34009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                sb.append("projection is null; ");
34109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            } else if (projection.length == 0) {
34209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                sb.append("projection is empty; ");
34309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            } else {
34409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                for (int i = 0; i < projection.length; ++i) {
34509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    sb.append("projection[");
34609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    sb.append(i);
34709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    sb.append("] is ");
34809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    sb.append(projection[i]);
34909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    sb.append("; ");
35009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                }
35109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
35209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            sb.append("selection is ");
35309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            sb.append(selection);
35409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            sb.append("; ");
35509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            if (selectionArgs == null) {
35609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                sb.append("selectionArgs is null; ");
35709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            } else if (selectionArgs.length == 0) {
35809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                sb.append("selectionArgs is empty; ");
35909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            } else {
36009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                for (int i = 0; i < selectionArgs.length; ++i) {
36109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    sb.append("selectionArgs[");
36209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    sb.append(i);
36309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    sb.append("] is ");
36409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    sb.append(selectionArgs[i]);
36509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    sb.append("; ");
36609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                }
36709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
36809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            sb.append("sort is ");
36909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            sb.append(sortOrder);
37009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            sb.append(".");
3711ac5507790a87810061a19dadec36eb328a222eaTao Liejun            Log.v(TAG, sb.toString());
37209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
37309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
37409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        Cursor ret = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
37509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
37609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        if (ret != null) {
37709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            ret.setNotificationUri(getContext().getContentResolver(), uri);
378ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly            if (V) Log.v(TAG, "created cursor " + ret + " on behalf of ");// +
37909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        } else {
380ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly            if (D) Log.d(TAG, "query failed in downloads database");
38109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
38209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
38309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        return ret;
38409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
38509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
38609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    @Override
38709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
38809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
38909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
39009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        int count;
39109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        long rowId = 0;
39209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
39309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        int match = sURIMatcher.match(uri);
39409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        switch (match) {
39509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            case SHARES:
39609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            case SHARES_ID: {
39709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                String myWhere;
39809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                if (selection != null) {
39909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    if (match == SHARES) {
40009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                        myWhere = "( " + selection + " )";
40109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    } else {
40209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                        myWhere = "( " + selection + " ) AND ";
40309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    }
40409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                } else {
40509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    myWhere = "";
40609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                }
40709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                if (match == SHARES_ID) {
40809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    String segment = uri.getPathSegments().get(1);
40909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    rowId = Long.parseLong(segment);
41009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    myWhere += " ( " + BluetoothShare._ID + " = " + rowId + " ) ";
41109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                }
41209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
41309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                if (values.size() > 0) {
41409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    count = db.update(DB_TABLE, values, myWhere, selectionArgs);
41509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                } else {
41609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    count = 0;
41709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                }
41809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                break;
41909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
42009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            default: {
421ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly                if (D) Log.d(TAG, "updating unknown/invalid URI: " + uri);
42209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                throw new UnsupportedOperationException("Cannot update URI: " + uri);
42309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
42409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
42509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        getContext().getContentResolver().notifyChange(uri, null);
42609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
42709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        return count;
42809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
42909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
43009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    @Override
43109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    public int delete(Uri uri, String selection, String[] selectionArgs) {
43209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
43309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        int count;
43409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        int match = sURIMatcher.match(uri);
43509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        switch (match) {
43609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            case SHARES:
43709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            case SHARES_ID: {
43809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                String myWhere;
43909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                if (selection != null) {
44009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    if (match == SHARES) {
44109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                        myWhere = "( " + selection + " )";
44209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    } else {
44309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                        myWhere = "( " + selection + " ) AND ";
44409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    }
44509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                } else {
44609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    myWhere = "";
44709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                }
44809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                if (match == SHARES_ID) {
44909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    String segment = uri.getPathSegments().get(1);
45009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    long rowId = Long.parseLong(segment);
45109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    myWhere += " ( " + BluetoothShare._ID + " = " + rowId + " ) ";
45209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                }
45309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
45409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                count = db.delete(DB_TABLE, myWhere, selectionArgs);
45509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                break;
45609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
45709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            default: {
458ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly                if (D) Log.d(TAG, "deleting unknown/invalid URI: " + uri);
45909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                throw new UnsupportedOperationException("Cannot delete URI: " + uri);
46009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
46109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
46209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        getContext().getContentResolver().notifyChange(uri, null);
46309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        return count;
46409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
46509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly}
466