Search in sources :

Example 81 with Cursor

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

the class ListManagedCursor method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get a cursor with all people
    Cursor c = getContentResolver().query(Settings.System.CONTENT_URI, null, null, null, null);
    startManagingCursor(c);
    ListAdapter adapter = new SimpleCursorAdapter(this, // Use a template that displays a text view
    android.R.layout.simple_list_item_1, // Give the cursor to the list adatper
    c, // Map the NAME column in the people database to...
    new String[] { People.NAME }, // The "text1" view defined in the XML template
    new int[] { android.R.id.text1 });
    setListAdapter(adapter);
    getListView().setOnItemClickListener(this);
}
Also used : SimpleCursorAdapter(android.widget.SimpleCursorAdapter) Cursor(android.database.Cursor) ListAdapter(android.widget.ListAdapter)

Example 82 with Cursor

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

the class MtpDatabase method getObjectList.

private int[] getObjectList(int storageID, int format, int parent) {
    Cursor c = null;
    try {
        c = createObjectQuery(storageID, format, parent);
        if (c == null) {
            return null;
        }
        int count = c.getCount();
        if (count > 0) {
            int[] result = new int[count];
            for (int i = 0; i < count; i++) {
                c.moveToNext();
                result[i] = c.getInt(0);
            }
            return result;
        }
    } catch (RemoteException e) {
        Log.e(TAG, "RemoteException in getObjectList", e);
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return null;
}
Also used : Cursor(android.database.Cursor) RemoteException(android.os.RemoteException)

Example 83 with Cursor

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

the class MtpPropertyGroup method getPropertyList.

MtpPropertyList getPropertyList(int handle, int format, int depth) {
    //Log.d(TAG, "getPropertyList handle: " + handle + " format: " + format + " depth: " + depth);
    if (depth > 1) {
        // depth 0: single object, depth 1: immediate children
        return new MtpPropertyList(0, MtpConstants.RESPONSE_SPECIFICATION_BY_DEPTH_UNSUPPORTED);
    }
    String where;
    String[] whereArgs;
    if (format == 0) {
        if (handle == 0xFFFFFFFF) {
            // select all objects
            where = null;
            whereArgs = null;
        } else {
            whereArgs = new String[] { Integer.toString(handle) };
            if (depth == 1) {
                where = PARENT_WHERE;
            } else {
                where = ID_WHERE;
            }
        }
    } else {
        if (handle == 0xFFFFFFFF) {
            // select all objects with given format
            where = FORMAT_WHERE;
            whereArgs = new String[] { Integer.toString(format) };
        } else {
            whereArgs = new String[] { Integer.toString(handle), Integer.toString(format) };
            if (depth == 1) {
                where = PARENT_FORMAT_WHERE;
            } else {
                where = ID_FORMAT_WHERE;
            }
        }
    }
    Cursor c = null;
    try {
        // don't query if not necessary
        if (depth > 0 || handle == 0xFFFFFFFF || mColumns.length > 1) {
            c = mProvider.query(mUri, mColumns, where, whereArgs, null, null);
            if (c == null) {
                return new MtpPropertyList(0, MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE);
            }
        }
        int count = (c == null ? 1 : c.getCount());
        MtpPropertyList result = new MtpPropertyList(count * mProperties.length, MtpConstants.RESPONSE_OK);
        // iterate over all objects in the query
        for (int objectIndex = 0; objectIndex < count; objectIndex++) {
            if (c != null) {
                c.moveToNext();
                handle = (int) c.getLong(0);
            }
            // iterate over all properties in the query for the given object
            for (int propertyIndex = 0; propertyIndex < mProperties.length; propertyIndex++) {
                Property property = mProperties[propertyIndex];
                int propertyCode = property.code;
                int column = property.column;
                // handle some special cases
                switch(propertyCode) {
                    case MtpConstants.PROPERTY_PROTECTION_STATUS:
                        // protection status is always 0
                        result.append(handle, propertyCode, MtpConstants.TYPE_UINT16, 0);
                        break;
                    case MtpConstants.PROPERTY_OBJECT_FILE_NAME:
                        // special case - need to extract file name from full path
                        String value = c.getString(column);
                        if (value != null) {
                            result.append(handle, propertyCode, nameFromPath(value));
                        } else {
                            result.setResult(MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE);
                        }
                        break;
                    case MtpConstants.PROPERTY_NAME:
                        // first try title
                        String name = c.getString(column);
                        // then try name
                        if (name == null) {
                            name = queryString(handle, Audio.PlaylistsColumns.NAME);
                        }
                        // if title and name fail, extract name from full path
                        if (name == null) {
                            name = queryString(handle, Files.FileColumns.DATA);
                            if (name != null) {
                                name = nameFromPath(name);
                            }
                        }
                        if (name != null) {
                            result.append(handle, propertyCode, name);
                        } else {
                            result.setResult(MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE);
                        }
                        break;
                    case MtpConstants.PROPERTY_DATE_MODIFIED:
                    case MtpConstants.PROPERTY_DATE_ADDED:
                        // convert from seconds to DateTime
                        result.append(handle, propertyCode, format_date_time(c.getInt(column)));
                        break;
                    case MtpConstants.PROPERTY_ORIGINAL_RELEASE_DATE:
                        // release date is stored internally as just the year
                        int year = c.getInt(column);
                        String dateTime = Integer.toString(year) + "0101T000000";
                        result.append(handle, propertyCode, dateTime);
                        break;
                    case MtpConstants.PROPERTY_PERSISTENT_UID:
                        // PUID is concatenation of storageID and object handle
                        long puid = c.getLong(column);
                        puid <<= 32;
                        puid += handle;
                        result.append(handle, propertyCode, MtpConstants.TYPE_UINT128, puid);
                        break;
                    case MtpConstants.PROPERTY_TRACK:
                        result.append(handle, propertyCode, MtpConstants.TYPE_UINT16, c.getInt(column) % 1000);
                        break;
                    case MtpConstants.PROPERTY_ARTIST:
                        result.append(handle, propertyCode, queryAudio(handle, Audio.AudioColumns.ARTIST));
                        break;
                    case MtpConstants.PROPERTY_ALBUM_NAME:
                        result.append(handle, propertyCode, queryAudio(handle, Audio.AudioColumns.ALBUM));
                        break;
                    case MtpConstants.PROPERTY_GENRE:
                        String genre = queryGenre(handle);
                        if (genre != null) {
                            result.append(handle, propertyCode, genre);
                        } else {
                            result.setResult(MtpConstants.RESPONSE_INVALID_OBJECT_HANDLE);
                        }
                        break;
                    case MtpConstants.PROPERTY_AUDIO_WAVE_CODEC:
                    case MtpConstants.PROPERTY_AUDIO_BITRATE:
                    case MtpConstants.PROPERTY_SAMPLE_RATE:
                        // we don't have these in our database, so return 0
                        result.append(handle, propertyCode, MtpConstants.TYPE_UINT32, 0);
                        break;
                    case MtpConstants.PROPERTY_BITRATE_TYPE:
                    case MtpConstants.PROPERTY_NUMBER_OF_CHANNELS:
                        // we don't have these in our database, so return 0
                        result.append(handle, propertyCode, MtpConstants.TYPE_UINT16, 0);
                        break;
                    default:
                        if (property.type == MtpConstants.TYPE_STR) {
                            result.append(handle, propertyCode, c.getString(column));
                        } else if (property.type == MtpConstants.TYPE_UNDEFINED) {
                            result.append(handle, propertyCode, property.type, 0);
                        } else {
                            result.append(handle, propertyCode, property.type, c.getLong(column));
                        }
                        break;
                }
            }
        }
        return result;
    } catch (RemoteException e) {
        return new MtpPropertyList(0, MtpConstants.RESPONSE_GENERAL_ERROR);
    } finally {
        if (c != null) {
            c.close();
        }
    }
// impossible to get here, so no return statement
}
Also used : Cursor(android.database.Cursor) RemoteException(android.os.RemoteException)

Example 84 with Cursor

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

the class MtpPropertyGroup method queryGenre.

private String queryGenre(int id) {
    Cursor c = null;
    try {
        Uri uri = Audio.Genres.getContentUriForAudioId(mVolumeName, id);
        c = mProvider.query(uri, new String[] { Files.FileColumns._ID, Audio.GenresColumns.NAME }, null, null, null, null);
        if (c != null && c.moveToNext()) {
            return c.getString(1);
        } else {
            return "";
        }
    } catch (Exception e) {
        Log.e(TAG, "queryGenre exception", e);
        return null;
    } finally {
        if (c != null) {
            c.close();
        }
    }
}
Also used : Cursor(android.database.Cursor) Uri(android.net.Uri) RemoteException(android.os.RemoteException)

Example 85 with Cursor

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

the class MtpDatabase method getObjectReferences.

private int[] getObjectReferences(int handle) {
    Uri uri = Files.getMtpReferencesUri(mVolumeName, handle);
    Cursor c = null;
    try {
        c = mMediaProvider.query(uri, ID_PROJECTION, null, null, null, null);
        if (c == null) {
            return null;
        }
        int count = c.getCount();
        if (count > 0) {
            int[] result = new int[count];
            for (int i = 0; i < count; i++) {
                c.moveToNext();
                result[i] = c.getInt(0);
            }
            return result;
        }
    } catch (RemoteException e) {
        Log.e(TAG, "RemoteException in getObjectList", e);
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return null;
}
Also used : Cursor(android.database.Cursor) RemoteException(android.os.RemoteException) Uri(android.net.Uri)

Aggregations

Cursor (android.database.Cursor)4002 ArrayList (java.util.ArrayList)547 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)527 Uri (android.net.Uri)467 ContentValues (android.content.ContentValues)334 ContentResolver (android.content.ContentResolver)193 Test (org.junit.Test)183 RemoteException (android.os.RemoteException)182 File (java.io.File)170 IOException (java.io.IOException)159 MatrixCursor (android.database.MatrixCursor)154 Intent (android.content.Intent)140 SQLException (android.database.SQLException)126 MediumTest (android.test.suitebuilder.annotation.MediumTest)116 HashMap (java.util.HashMap)108 SQLiteException (android.database.sqlite.SQLiteException)94 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)93 SQLiteCursor (android.database.sqlite.SQLiteCursor)88 Query (android.app.DownloadManager.Query)76 MergeCursor (android.database.MergeCursor)75