Search in sources :

Example 6 with MergeCursor

use of android.database.MergeCursor in project platform_frameworks_base by android.

the class ModelTest method testModelIdIsUnique.

// Tests multiple authorities with clashing document IDs.
public void testModelIdIsUnique() {
    MatrixCursor cIn1 = new MatrixCursor(COLUMNS);
    MatrixCursor cIn2 = new MatrixCursor(COLUMNS);
    // Make two sets of items with the same IDs, under different authorities.
    final String AUTHORITY0 = "auth0";
    final String AUTHORITY1 = "auth1";
    for (int i = 0; i < ITEM_COUNT; ++i) {
        MatrixCursor.RowBuilder row0 = cIn1.newRow();
        row0.add(RootCursorWrapper.COLUMN_AUTHORITY, AUTHORITY0);
        row0.add(Document.COLUMN_DOCUMENT_ID, Integer.toString(i));
        MatrixCursor.RowBuilder row1 = cIn2.newRow();
        row1.add(RootCursorWrapper.COLUMN_AUTHORITY, AUTHORITY1);
        row1.add(Document.COLUMN_DOCUMENT_ID, Integer.toString(i));
    }
    Cursor cIn = new MergeCursor(new Cursor[] { cIn1, cIn2 });
    // Update the model, then make sure it contains all the expected items.
    DirectoryResult r = new DirectoryResult();
    r.cursor = cIn;
    model.update(r);
    assertEquals(ITEM_COUNT * 2, model.getItemCount());
    BitSet b0 = new BitSet(ITEM_COUNT);
    BitSet b1 = new BitSet(ITEM_COUNT);
    for (String id : model.getModelIds()) {
        Cursor cOut = model.getItem(id);
        String authority = DocumentInfo.getCursorString(cOut, RootCursorWrapper.COLUMN_AUTHORITY);
        String docId = DocumentInfo.getCursorString(cOut, Document.COLUMN_DOCUMENT_ID);
        switch(authority) {
            case AUTHORITY0:
                b0.set(Integer.parseInt(docId));
                break;
            case AUTHORITY1:
                b1.set(Integer.parseInt(docId));
                break;
            default:
                fail("Unrecognized authority string");
        }
    }
    assertEquals(ITEM_COUNT, b0.cardinality());
    assertEquals(ITEM_COUNT, b1.cardinality());
}
Also used : DirectoryResult(com.android.documentsui.DirectoryResult) MergeCursor(android.database.MergeCursor) BitSet(java.util.BitSet) MergeCursor(android.database.MergeCursor) MatrixCursor(android.database.MatrixCursor) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor)

Example 7 with MergeCursor

use of android.database.MergeCursor in project tray by grandcentrix.

the class TrayContentProvider method query.

@Override
public Cursor query(final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) {
    final SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    final int match = sURIMatcher.match(uri);
    switch(match) {
        case SINGLE_PREFERENCE:
        case INTERNAL_SINGLE_PREFERENCE:
            builder.appendWhere(TrayContract.Preferences.Columns.KEY + " = " + DatabaseUtils.sqlEscapeString(uri.getPathSegments().get(2)));
        // no break
        case MODULE_PREFERENCE:
        case INTERNAL_MODULE_PREFERENCE:
            if (match == SINGLE_PREFERENCE || match == INTERNAL_SINGLE_PREFERENCE) {
                builder.appendWhere(" AND ");
            }
            builder.appendWhere(TrayContract.Preferences.Columns.MODULE + " = " + DatabaseUtils.sqlEscapeString(uri.getPathSegments().get(1)));
        // no break
        case ALL_PREFERENCE:
        case INTERNAL_ALL_PREFERENCE:
            builder.setTables(getTable(uri));
            break;
        default:
            throw new IllegalArgumentException("Query is not supported for Uri: " + uri);
    }
    final Cursor cursor;
    final String backup = uri.getQueryParameter("backup");
    if (backup == null) {
        // backup not set, query both dbs
        Cursor cursor1 = builder.query(mUserDbHelper.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
        Cursor cursor2 = builder.query(mDeviceDbHelper.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
        cursor = new MergeCursor(new Cursor[] { cursor1, cursor2 });
    } else {
        // Query
        cursor = builder.query(getReadableDatabase(uri), projection, selection, selectionArgs, null, null, sortOrder);
    }
    if (cursor != null) {
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
    }
    return cursor;
}
Also used : MergeCursor(android.database.MergeCursor) MergeCursor(android.database.MergeCursor) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder)

Example 8 with MergeCursor

use of android.database.MergeCursor in project android_frameworks_base by DirtyUnicorns.

the class RecentsLoader method loadInBackgroundLocked.

private DirectoryResult loadInBackgroundLocked() {
    if (mFirstPassLatch == null) {
        // First time through we kick off all the recent tasks, and wait
        // around to see if everyone finishes quickly.
        final Collection<RootInfo> roots = mRoots.getMatchingRootsBlocking(mState);
        for (RootInfo root : roots) {
            if (root.supportsRecents()) {
                mTasks.put(root, new RecentsTask(root.authority, root.rootId));
            }
        }
        mFirstPassLatch = new CountDownLatch(mTasks.size());
        for (RecentsTask task : mTasks.values()) {
            ProviderExecutor.forAuthority(task.authority).execute(task);
        }
        try {
            mFirstPassLatch.await(MAX_FIRST_PASS_WAIT_MILLIS, TimeUnit.MILLISECONDS);
            mFirstPassDone = true;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    final long rejectBefore = System.currentTimeMillis() - REJECT_OLDER_THAN;
    // Collect all finished tasks
    boolean allDone = true;
    List<Cursor> cursors = new ArrayList<>();
    for (RecentsTask task : mTasks.values()) {
        if (task.isDone()) {
            try {
                final Cursor cursor = task.get();
                if (cursor == null)
                    continue;
                final FilteringCursorWrapper filtered = new FilteringCursorWrapper(cursor, mState.acceptMimes, RECENT_REJECT_MIMES, rejectBefore) {

                    @Override
                    public void close() {
                    // Ignored, since we manage cursor lifecycle internally
                    }
                };
                cursors.add(filtered);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
            // We already logged on other side
            } catch (Exception e) {
                Log.e(TAG, "Failed to query Recents for authority: " + task.authority + ". Skip this authority in Recents.", e);
            }
        } else {
            allDone = false;
        }
    }
    if (DEBUG) {
        Log.d(TAG, "Found " + cursors.size() + " of " + mTasks.size() + " recent queries done");
    }
    final DirectoryResult result = new DirectoryResult();
    result.sortOrder = SORT_ORDER_LAST_MODIFIED;
    final Cursor merged;
    if (cursors.size() > 0) {
        merged = new MergeCursor(cursors.toArray(new Cursor[cursors.size()]));
    } else {
        // Return something when nobody is ready
        merged = new MatrixCursor(new String[0]);
    }
    // Tell the UI if this is an in-progress result. When loading is complete, another update is
    // sent with EXTRA_LOADING set to false.
    Bundle extras = new Bundle();
    extras.putBoolean(DocumentsContract.EXTRA_LOADING, !allDone);
    merged.setExtras(extras);
    result.cursor = merged;
    return result;
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) MergeCursor(android.database.MergeCursor) CountDownLatch(java.util.concurrent.CountDownLatch) Cursor(android.database.Cursor) MergeCursor(android.database.MergeCursor) MatrixCursor(android.database.MatrixCursor) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) MatrixCursor(android.database.MatrixCursor) RootInfo(com.android.documentsui.model.RootInfo) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with MergeCursor

use of android.database.MergeCursor in project android_frameworks_base by AOSPA.

the class RecentsLoader method loadInBackgroundLocked.

private DirectoryResult loadInBackgroundLocked() {
    if (mFirstPassLatch == null) {
        // First time through we kick off all the recent tasks, and wait
        // around to see if everyone finishes quickly.
        final Collection<RootInfo> roots = mRoots.getMatchingRootsBlocking(mState);
        for (RootInfo root : roots) {
            if (root.supportsRecents()) {
                mTasks.put(root, new RecentsTask(root.authority, root.rootId));
            }
        }
        mFirstPassLatch = new CountDownLatch(mTasks.size());
        for (RecentsTask task : mTasks.values()) {
            ProviderExecutor.forAuthority(task.authority).execute(task);
        }
        try {
            mFirstPassLatch.await(MAX_FIRST_PASS_WAIT_MILLIS, TimeUnit.MILLISECONDS);
            mFirstPassDone = true;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    final long rejectBefore = System.currentTimeMillis() - REJECT_OLDER_THAN;
    // Collect all finished tasks
    boolean allDone = true;
    List<Cursor> cursors = new ArrayList<>();
    for (RecentsTask task : mTasks.values()) {
        if (task.isDone()) {
            try {
                final Cursor cursor = task.get();
                if (cursor == null)
                    continue;
                final FilteringCursorWrapper filtered = new FilteringCursorWrapper(cursor, mState.acceptMimes, RECENT_REJECT_MIMES, rejectBefore) {

                    @Override
                    public void close() {
                    // Ignored, since we manage cursor lifecycle internally
                    }
                };
                cursors.add(filtered);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
            // We already logged on other side
            } catch (Exception e) {
                Log.e(TAG, "Failed to query Recents for authority: " + task.authority + ". Skip this authority in Recents.", e);
            }
        } else {
            allDone = false;
        }
    }
    if (DEBUG) {
        Log.d(TAG, "Found " + cursors.size() + " of " + mTasks.size() + " recent queries done");
    }
    final DirectoryResult result = new DirectoryResult();
    result.sortOrder = SORT_ORDER_LAST_MODIFIED;
    final Cursor merged;
    if (cursors.size() > 0) {
        merged = new MergeCursor(cursors.toArray(new Cursor[cursors.size()]));
    } else {
        // Return something when nobody is ready
        merged = new MatrixCursor(new String[0]);
    }
    // Tell the UI if this is an in-progress result. When loading is complete, another update is
    // sent with EXTRA_LOADING set to false.
    Bundle extras = new Bundle();
    extras.putBoolean(DocumentsContract.EXTRA_LOADING, !allDone);
    merged.setExtras(extras);
    result.cursor = merged;
    return result;
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) MergeCursor(android.database.MergeCursor) CountDownLatch(java.util.concurrent.CountDownLatch) Cursor(android.database.Cursor) MergeCursor(android.database.MergeCursor) MatrixCursor(android.database.MatrixCursor) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) MatrixCursor(android.database.MatrixCursor) RootInfo(com.android.documentsui.model.RootInfo) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with MergeCursor

use of android.database.MergeCursor in project android_frameworks_base by ResurrectionRemix.

the class ModelTest method testModelIdIsUnique.

// Tests multiple authorities with clashing document IDs.
public void testModelIdIsUnique() {
    MatrixCursor cIn1 = new MatrixCursor(COLUMNS);
    MatrixCursor cIn2 = new MatrixCursor(COLUMNS);
    // Make two sets of items with the same IDs, under different authorities.
    final String AUTHORITY0 = "auth0";
    final String AUTHORITY1 = "auth1";
    for (int i = 0; i < ITEM_COUNT; ++i) {
        MatrixCursor.RowBuilder row0 = cIn1.newRow();
        row0.add(RootCursorWrapper.COLUMN_AUTHORITY, AUTHORITY0);
        row0.add(Document.COLUMN_DOCUMENT_ID, Integer.toString(i));
        MatrixCursor.RowBuilder row1 = cIn2.newRow();
        row1.add(RootCursorWrapper.COLUMN_AUTHORITY, AUTHORITY1);
        row1.add(Document.COLUMN_DOCUMENT_ID, Integer.toString(i));
    }
    Cursor cIn = new MergeCursor(new Cursor[] { cIn1, cIn2 });
    // Update the model, then make sure it contains all the expected items.
    DirectoryResult r = new DirectoryResult();
    r.cursor = cIn;
    model.update(r);
    assertEquals(ITEM_COUNT * 2, model.getItemCount());
    BitSet b0 = new BitSet(ITEM_COUNT);
    BitSet b1 = new BitSet(ITEM_COUNT);
    for (String id : model.getModelIds()) {
        Cursor cOut = model.getItem(id);
        String authority = DocumentInfo.getCursorString(cOut, RootCursorWrapper.COLUMN_AUTHORITY);
        String docId = DocumentInfo.getCursorString(cOut, Document.COLUMN_DOCUMENT_ID);
        switch(authority) {
            case AUTHORITY0:
                b0.set(Integer.parseInt(docId));
                break;
            case AUTHORITY1:
                b1.set(Integer.parseInt(docId));
                break;
            default:
                fail("Unrecognized authority string");
        }
    }
    assertEquals(ITEM_COUNT, b0.cardinality());
    assertEquals(ITEM_COUNT, b1.cardinality());
}
Also used : DirectoryResult(com.android.documentsui.DirectoryResult) MergeCursor(android.database.MergeCursor) BitSet(java.util.BitSet) MergeCursor(android.database.MergeCursor) MatrixCursor(android.database.MatrixCursor) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor)

Aggregations

MergeCursor (android.database.MergeCursor)31 Cursor (android.database.Cursor)25 MatrixCursor (android.database.MatrixCursor)13 SQLiteCursor (android.database.sqlite.SQLiteCursor)7 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Bundle (android.os.Bundle)5 DirectoryResult (com.android.documentsui.DirectoryResult)5 DocumentInfo.getCursorString (com.android.documentsui.model.DocumentInfo.getCursorString)5 RootInfo (com.android.documentsui.model.RootInfo)5 IOException (java.io.IOException)5 BitSet (java.util.BitSet)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ExecutionException (java.util.concurrent.ExecutionException)5 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 LinkedList (java.util.LinkedList)2 Intent (android.content.Intent)1 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)1 Uri (android.net.Uri)1 List (java.util.List)1