Search in sources :

Example 31 with MatrixCursor

use of android.database.MatrixCursor in project robolectric by robolectric.

the class ShadowCursorWindowTest method shouldFillWindowWithCursor.

@Test
public void shouldFillWindowWithCursor() throws Exception {
    CursorWindow window = new CursorWindow("name");
    MatrixCursor testCursor = new MatrixCursor(new String[] { "a", "b", "c", "d" });
    testCursor.addRow(new Object[] { 12, "hello", null, new byte[] { (byte) 0xba, (byte) 0xdc, (byte) 0xaf, (byte) 0xfe } });
    testCursor.addRow(new Object[] { 34, "baz", 1.2, null });
    testCursor.addRow(new Object[] { 46, "foo", 2.4, new byte[] {} });
    DatabaseUtils.cursorFillWindow(testCursor, 0, window);
    assertThat(window.getNumRows()).isEqualTo(3);
    assertThat(window.getInt(0, 0)).isEqualTo(12);
    assertThat(window.getString(0, 1)).isEqualTo("hello");
    assertThat(window.getString(0, 2)).isNull();
    assertThat(window.getBlob(0, 3)).isEqualTo(new byte[] { (byte) 0xba, (byte) 0xdc, (byte) 0xaf, (byte) 0xfe });
    assertThat(window.getInt(1, 0)).isEqualTo(34);
    assertThat(window.getString(1, 1)).isEqualTo("baz");
    assertThat(window.getFloat(1, 2)).isEqualTo(1.2f);
    assertThat(window.getBlob(1, 3)).isEqualTo(null);
    assertThat(window.getBlob(2, 3)).isEqualTo(new byte[] {});
}
Also used : CursorWindow(android.database.CursorWindow) MatrixCursor(android.database.MatrixCursor) Test(org.junit.Test)

Example 32 with MatrixCursor

use of android.database.MatrixCursor in project robolectric by robolectric.

the class ShadowContentResolverTest method applyBatchForRegisteredProvider.

@SuppressWarnings("serial")
@Test
public void applyBatchForRegisteredProvider() throws RemoteException, OperationApplicationException {
    final ArrayList<String> operations = new ArrayList<>();
    ShadowContentResolver.registerProviderInternal("registeredProvider", new ContentProvider() {

        @Override
        public boolean onCreate() {
            return true;
        }

        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
            operations.add("query");
            MatrixCursor cursor = new MatrixCursor(new String[] { "a" });
            cursor.addRow(new Object[] { "b" });
            return cursor;
        }

        @Override
        public String getType(Uri uri) {
            return null;
        }

        @Override
        public Uri insert(Uri uri, ContentValues values) {
            operations.add("insert");
            return ContentUris.withAppendedId(uri, 1);
        }

        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            operations.add("delete");
            return 0;
        }

        @Override
        public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            operations.add("update");
            return 0;
        }
    });
    final Uri uri = Uri.parse("content://registeredProvider/path");
    contentResolver.applyBatch("registeredProvider", new ArrayList<ContentProviderOperation>() {

        {
            add(ContentProviderOperation.newInsert(uri).withValue("a", "b").build());
            add(ContentProviderOperation.newUpdate(uri).withValue("a", "b").build());
            add(ContentProviderOperation.newDelete(uri).build());
            add(ContentProviderOperation.newAssertQuery(uri).withValue("a", "b").build());
        }
    });
    assertThat(operations).containsExactly("insert", "update", "delete", "query");
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) ContentProvider(android.content.ContentProvider) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor) BaseCursor(org.robolectric.fakes.BaseCursor) Uri(android.net.Uri) MatrixCursor(android.database.MatrixCursor) Test(org.junit.Test)

Example 33 with MatrixCursor

use of android.database.MatrixCursor in project android by owncloud.

the class UsersAndGroupsSearchProvider method searchForUsersOrGroups.

private Cursor searchForUsersOrGroups(Uri uri) {
    MatrixCursor response = null;
    String userQuery = uri.getLastPathSegment().toLowerCase();
    /// need to trust on the AccountUtils to get the current account since the query in the client side is not
    /// directly started by our code, but from SearchView implementation
    Account account = AccountUtils.getCurrentOwnCloudAccount(getContext());
    /// request to the OC server about users and groups matching userQuery
    GetRemoteShareesOperation searchRequest = new GetRemoteShareesOperation(userQuery, REQUESTED_PAGE, RESULTS_PER_PAGE);
    RemoteOperationResult result = searchRequest.execute(account, getContext());
    List<JSONObject> names = new ArrayList<JSONObject>();
    if (result.isSuccess()) {
        for (Object o : result.getData()) {
            // Get JSonObjects from response
            names.add((JSONObject) o);
        }
    } else {
        showErrorMessage(result);
    }
    /// convert the responses from the OC server to the expected format
    if (names.size() > 0) {
        response = new MatrixCursor(COLUMNS);
        Iterator<JSONObject> namesIt = names.iterator();
        JSONObject item;
        String displayName = null;
        int icon = 0;
        Uri dataUri = null;
        int count = 0;
        MainApp app = (MainApp) getContext().getApplicationContext();
        Uri userBaseUri = new Uri.Builder().scheme(CONTENT).authority(sSuggestAuthority + DATA_USER_SUFFIX).build();
        Uri groupBaseUri = new Uri.Builder().scheme(CONTENT).authority(sSuggestAuthority + DATA_GROUP_SUFFIX).build();
        Uri remoteBaseUri = new Uri.Builder().scheme(CONTENT).authority(sSuggestAuthority + DATA_REMOTE_SUFFIX).build();
        FileDataStorageManager manager = new FileDataStorageManager(account, getContext().getContentResolver());
        boolean federatedShareAllowed = manager.getCapability(account.name).getFilesSharingFederationOutgoing().isTrue();
        try {
            while (namesIt.hasNext()) {
                item = namesIt.next();
                String userName = item.getString(GetRemoteShareesOperation.PROPERTY_LABEL);
                JSONObject value = item.getJSONObject(GetRemoteShareesOperation.NODE_VALUE);
                int type = value.getInt(GetRemoteShareesOperation.PROPERTY_SHARE_TYPE);
                String shareWith = value.getString(GetRemoteShareesOperation.PROPERTY_SHARE_WITH);
                if (ShareType.GROUP.getValue() == type) {
                    displayName = getContext().getString(R.string.share_group_clarification, userName);
                    icon = R.drawable.ic_group;
                    dataUri = Uri.withAppendedPath(groupBaseUri, shareWith);
                } else if (ShareType.FEDERATED.getValue() == type && federatedShareAllowed) {
                    icon = R.drawable.ic_user;
                    if (userName.equals(shareWith)) {
                        displayName = getContext().getString(R.string.share_remote_clarification, userName);
                    } else {
                        String[] uriSplitted = shareWith.split("@");
                        displayName = getContext().getString(R.string.share_known_remote_clarification, userName, uriSplitted[uriSplitted.length - 1]);
                    }
                    dataUri = Uri.withAppendedPath(remoteBaseUri, shareWith);
                } else if (ShareType.USER.getValue() == type) {
                    displayName = userName;
                    icon = R.drawable.ic_user;
                    dataUri = Uri.withAppendedPath(userBaseUri, shareWith);
                }
                if (displayName != null && dataUri != null) {
                    response.newRow().add(// BaseColumns._ID
                    count++).add(// SearchManager.SUGGEST_COLUMN_TEXT_1
                    displayName).add(// SearchManager.SUGGEST_COLUMN_ICON_1
                    icon).add(dataUri);
                }
            }
        } catch (JSONException e) {
            Log_OC.e(TAG, "Exception while parsing data of users/groups", e);
        }
    }
    return response;
}
Also used : Account(android.accounts.Account) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) GetRemoteShareesOperation(com.owncloud.android.lib.resources.shares.GetRemoteShareesOperation) ArrayList(java.util.ArrayList) MainApp(com.owncloud.android.MainApp) JSONException(org.json.JSONException) Uri(android.net.Uri) MatrixCursor(android.database.MatrixCursor) JSONObject(org.json.JSONObject) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) JSONObject(org.json.JSONObject)

Example 34 with MatrixCursor

use of android.database.MatrixCursor in project aFileChooser by iPaulPro.

the class LocalStorageProvider method queryRoots.

@Override
public Cursor queryRoots(final String[] projection) throws FileNotFoundException {
    // Create a cursor with either the requested fields, or the default
    // projection if "projection" is null.
    final MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_ROOT_PROJECTION);
    // Add Home directory
    File homeDir = Environment.getExternalStorageDirectory();
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Root.COLUMN_ROOT_ID, homeDir.getAbsolutePath());
    row.add(Root.COLUMN_DOCUMENT_ID, homeDir.getAbsolutePath());
    row.add(Root.COLUMN_TITLE, getContext().getString(R.string.internal_storage));
    row.add(Root.COLUMN_FLAGS, Root.FLAG_LOCAL_ONLY | Root.FLAG_SUPPORTS_CREATE);
    row.add(Root.COLUMN_ICON, R.drawable.ic_provider);
    // These columns are optional
    row.add(Root.COLUMN_AVAILABLE_BYTES, homeDir.getFreeSpace());
    // are automatically hidden)
    return result;
}
Also used : File(java.io.File) MatrixCursor(android.database.MatrixCursor)

Example 35 with MatrixCursor

use of android.database.MatrixCursor in project aFileChooser by iPaulPro.

the class LocalStorageProvider method queryDocument.

@Override
public Cursor queryDocument(final String documentId, final String[] projection) throws FileNotFoundException {
    // Create a cursor with either the requested fields, or the default
    // projection if "projection" is null.
    final MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION);
    includeFile(result, new File(documentId));
    return result;
}
Also used : File(java.io.File) MatrixCursor(android.database.MatrixCursor)

Aggregations

MatrixCursor (android.database.MatrixCursor)259 Cursor (android.database.Cursor)37 DirectoryResult (com.android.documentsui.DirectoryResult)35 RowBuilder (android.database.MatrixCursor.RowBuilder)30 File (java.io.File)30 MergeCursor (android.database.MergeCursor)18 Uri (android.net.Uri)18 Test (org.junit.Test)17 Setting (com.android.providers.settings.SettingsState.Setting)15 FileNotFoundException (java.io.FileNotFoundException)15 ArrayList (java.util.ArrayList)13 Bundle (android.os.Bundle)10 BitSet (java.util.BitSet)10 Random (java.util.Random)10 IOException (java.io.IOException)7 LinkedList (java.util.LinkedList)7 ContentValues (android.content.ContentValues)6 HashSet (java.util.HashSet)6 Map (java.util.Map)6 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)5