Example usage for android.database Cursor moveToFirst

List of usage examples for android.database Cursor moveToFirst

Introduction

In this page you can find the example usage for android.database Cursor moveToFirst.

Prototype

boolean moveToFirst();

Source Link

Document

Move the cursor to the first row.

Usage

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static String getPublicUrl(final String resourcePath) {
    final String[] projection = new String[] { Nodes.NODE_PUBLIC_URL };
    final String selection = Nodes.NODE_RESOURCE_PATH + "=?";
    final String[] selectionArgs = new String[] { resourcePath };
    Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    String url = null;//from  www.ja  v a2  s  .c om
    try {
        if (c.moveToFirst()) {
            url = c.getString(c.getColumnIndex(Nodes.NODE_PUBLIC_URL));
        }
    } finally {
        c.close();
    }
    return TextUtils.isEmpty(url) ? null : url;
}

From source file:de.escoand.readdaily.DownloadHandler.java

public static float downloadProgress(final Context context, final String name) {
    Cursor cursor = Database.getInstance(context).getDownloads();
    long id = 0;//from  ww  w . j a va  2 s .  c om
    float progress;

    // get download id
    while (cursor.moveToNext())
        if (cursor.getString(cursor.getColumnIndex(Database.COLUMN_SUBSCRIPTION)).equals(name)) {
            id = cursor.getLong(cursor.getColumnIndex(Database.COLUMN_ID));
            break;
        }
    cursor.close();
    if (id <= 0)
        return SUBSCRIPTION_DOWNLOAD_UNKNOWN;

    // get download
    cursor = ((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE))
            .query(new DownloadManager.Query().setFilterById(id));
    if (!cursor.moveToFirst()) {
        Database.getInstance(context).removeDownload(id);
        return DOWNLOAD_ID_UNKNOWN;
    }

    // get progress
    progress = cursor.getFloat(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))
            / cursor.getFloat(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

    cursor.close();
    return progress;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static boolean isCached(String resourcePath) {
    String[] projection = new String[] { Nodes.NODE_IS_CACHED };
    String selection = Nodes.NODE_RESOURCE_PATH + "=?";
    String[] selectionArgs = new String[] { resourcePath };
    Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    if (c != null) {
        try {// w  w  w. j  a  va  2s.  c om
            if (c.moveToFirst()) {
                return c.getInt(c.getColumnIndex(Nodes.NODE_IS_CACHED)) != 0;
            }
        } finally {
            c.close();
        }
    }
    return false;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static boolean isDirectory(long id) {
    String[] projection = new String[] { Nodes.NODE_KIND };
    String selection = Nodes._ID + "=?";
    String[] selectionArgs = new String[] { String.valueOf(id) };
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    String type = null;/*from  w  ww. j  a va  2 s .co  m*/
    try {
        if (c.moveToFirst()) {
            type = c.getString(c.getColumnIndex(Nodes.NODE_KIND));
        }
    } finally {
        c.close();
    }
    return (type != null) ? U1NodeKind.DIRECTORY == U1NodeKind.valueOf(type.toUpperCase(Locale.US)) : false;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static long getVolumeGeneration(String resourcePath) {
    String[] projection = new String[] { Volumes.VOLUME_GENERATION };
    String selection = Volumes.VOLUME_RESOURCE_PATH + "=?";
    String[] selectionArgs = new String[] { resourcePath };
    Cursor c = sResolver.query(Volumes.CONTENT_URI, projection, selection, selectionArgs, null);
    if (c != null) {
        try {/*from w  w  w  .j a v  a2  s. c o m*/
            if (c.moveToFirst()) {
                return c.getLong(c.getColumnIndex(Volumes.VOLUME_GENERATION));
            }
        } finally {
            c.close();
        }
    }
    return 0;
}

From source file:Main.java

/**
 * Try to get the exif orientation of the passed image uri
 *
 * @param context// w w w.ja v a  2 s  .c  o m
 * @param uri
 * @return
 */
public static int getExifOrientation(Context context, Uri uri) {

    final String scheme = uri.getScheme();

    ContentProviderClient provider = null;
    if (scheme == null || ContentResolver.SCHEME_FILE.equals(scheme)) {
        return getExifOrientation(uri.getPath());
    } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
        try {
            provider = context.getContentResolver().acquireContentProviderClient(uri);
        } catch (SecurityException e) {
            return 0;
        }

        if (provider != null) {
            Cursor result;
            try {
                result = provider.query(uri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION,
                        MediaStore.Images.ImageColumns.DATA }, null, null, null);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }

            if (result == null) {
                return 0;
            }

            int orientationColumnIndex = result.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION);
            int dataColumnIndex = result.getColumnIndex(MediaStore.Images.ImageColumns.DATA);

            try {
                if (result.getCount() > 0) {
                    result.moveToFirst();

                    int rotation = 0;

                    if (orientationColumnIndex > -1) {
                        rotation = result.getInt(orientationColumnIndex);
                    }

                    if (dataColumnIndex > -1) {
                        String path = result.getString(dataColumnIndex);
                        rotation |= getExifOrientation(path);
                    }
                    return rotation;
                }
            } finally {
                result.close();
            }
        }
    }
    return 0;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

/**
 * For a given file {@link Uri} string, we check if its hash has a
 * corresponding entry in the {@link MetaProvider}, telling thus whether the
 * file from under given {@link Uri} string has been already uploaded.
 * //w  w  w.j  a va 2  s . c  o  m
 * @param uriString
 *            the uri string which content we are checking
 * @return resourcePath if content under uri has been already uploaded,
 *         null otherwise
 */
public static String isUploaded(String uriString) {
    File file = null;
    String fileHash = null;

    if (uriString.startsWith(ContentResolver.SCHEME_CONTENT)) {
        final String[] projection = new String[] { MediaColumns.DATA };
        final Cursor c = sResolver.query(Uri.parse(uriString), projection, null, null, null);
        try {
            if (c.moveToFirst()) {
                String data = c.getString(c.getColumnIndex(MediaColumns.DATA));
                file = new File(data);
            } else {
                return null;
            }
        } finally {
            c.close();
        }
    } else if (uriString.startsWith(ContentResolver.SCHEME_FILE)) {
        final URI fileURI = URI.create(Uri.encode(uriString, ":/"));
        file = new File(fileURI);
    } else {
        Log.e(TAG, "Tried to check malformed uri string: " + uriString);
        return null;
    }

    try {
        if (file != null && file.exists()) {
            fileHash = HashUtils.getSha1(file);
            Log.d(TAG, String.format("Computed hash: '%s'", fileHash));
        } else {
            throw new FileNotFoundException("isUploaded()");
        }
    } catch (Exception e) {
        Log.e(TAG, "Can't compute file hash!", e);
        return null;
    }

    final String[] projection = new String[] { Nodes.NODE_RESOURCE_PATH };
    final String selection = Nodes.NODE_HASH + "=?";
    final String[] selectionArgs = new String[] { fileHash };
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    String resourcePath = null;
    try {
        if (c.moveToFirst()) {
            resourcePath = c.getString(c.getColumnIndex(Nodes.NODE_RESOURCE_PATH));
            Log.d(TAG, "Corresponding file hash found: " + resourcePath);
        } else {
            Log.d(TAG, "Corresponding file hash not found.");
        }
    } finally {
        c.close();
    }
    return resourcePath;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

/**
 * Calculates directory content size, recursively if necessary.
 * /*from  w  w w .  j  a v a2  s  . c om*/
 * @param resourcePath
 *            the directory resource path to calculate size of
 * @param recursive
 *            the flag indicating recursive calculation
 * @return the resorucePath defined directory size
 */
public static long getDirectorySize(final String resourcePath, final boolean recursive) {
    final String[] projection = new String[] { Nodes.NODE_RESOURCE_PATH, Nodes.NODE_KIND, Nodes.NODE_SIZE };
    final String selection = Nodes.NODE_PARENT_PATH + "=?";
    final String[] selectionArgs = new String[] { resourcePath };
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    U1NodeKind kind;
    long size = 0L;
    try {
        if (c.moveToFirst()) {
            do {
                kind = U1NodeKind
                        .valueOf(c.getString(c.getColumnIndex(Nodes.NODE_KIND)).toUpperCase(Locale.US));
                if (U1NodeKind.FILE == kind) {
                    size += c.getLong(c.getColumnIndex(Nodes.NODE_SIZE));
                } else if (U1NodeKind.DIRECTORY == kind && recursive) {
                    final String subDirResourcePath = c.getString(c.getColumnIndex(Nodes.NODE_RESOURCE_PATH));
                    size += getDirectorySize(subDirResourcePath, true);
                }
            } while (c.moveToNext());
        }
    } finally {
        c.close();
    }
    return size;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static U1Node getNodeByKey(String key) {
    String[] selectionArgs = new String[] { key };
    String selection = Nodes.NODE_KEY + "=?";
    String[] projection = Nodes.getDefaultProjection();
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    if (c != null) {
        try {//www  . j a va 2 s.com
            if (c.moveToFirst()) {
                String resourcePath;
                U1NodeKind kind;
                Boolean isLive = true;
                String path;
                String parentPath;
                String volumePath;
                Date whenCreated;
                Date whenChanged;
                Long generation;
                Long generationCreated;
                String contentPath;

                resourcePath = c.getString(c.getColumnIndex(Nodes.NODE_RESOURCE_PATH));
                kind = U1NodeKind
                        .valueOf(c.getString(c.getColumnIndex(Nodes.NODE_KIND)).toUpperCase(Locale.US));
                isLive = c.getInt(c.getColumnIndex(Nodes.NODE_IS_LIVE)) != 0;
                path = c.getString(c.getColumnIndex(Nodes.NODE_PATH));
                parentPath = c.getString(c.getColumnIndex(Nodes.NODE_PARENT_PATH));
                volumePath = c.getString(c.getColumnIndex(Nodes.NODE_VOLUME_PATH));
                whenCreated = new Date(c.getLong(c.getColumnIndex(Nodes.NODE_WHEN_CREATED)));
                whenChanged = new Date(c.getLong(c.getColumnIndex(Nodes.NODE_WHEN_CHANGED)));
                generation = c.getLong(c.getColumnIndex(Nodes.NODE_GENERATION));
                generationCreated = c.getLong(c.getColumnIndex(Nodes.NODE_GENERATION_CREATED));
                contentPath = c.getString(c.getColumnIndex(Nodes.NODE_CONTENT_PATH));

                return new U1Node(resourcePath, kind, isLive, path, parentPath, volumePath, key, whenCreated,
                        whenChanged, generation, generationCreated, contentPath);
            } else {
                return null;
            }
        } finally {
            c.close();
        }
    }
    return null;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static U1Node getNodeByResourcePath(String resourcePath) {
    String[] selectionArgs = new String[] { resourcePath };
    String selection = Nodes.NODE_RESOURCE_PATH + "=?";
    String[] projection = Nodes.getDefaultProjection();
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    if (c != null) {
        try {/*w  w  w. j ava2s .  c o m*/
            if (c.moveToFirst()) {
                String key;
                U1NodeKind kind;
                Boolean isLive = true;
                String path;
                String parentPath;
                String volumePath;
                Date whenCreated;
                Date whenChanged;
                Long generation;
                Long generationCreated;
                String contentPath;

                key = c.getString(c.getColumnIndex(Nodes.NODE_KEY));
                kind = U1NodeKind
                        .valueOf(c.getString(c.getColumnIndex(Nodes.NODE_KIND)).toUpperCase(Locale.US));
                isLive = c.getInt(c.getColumnIndex(Nodes.NODE_IS_LIVE)) != 0;
                path = c.getString(c.getColumnIndex(Nodes.NODE_PATH));
                parentPath = c.getString(c.getColumnIndex(Nodes.NODE_PARENT_PATH));
                volumePath = c.getString(c.getColumnIndex(Nodes.NODE_VOLUME_PATH));
                whenCreated = new Date(c.getLong(c.getColumnIndex(Nodes.NODE_WHEN_CREATED)));
                whenChanged = new Date(c.getLong(c.getColumnIndex(Nodes.NODE_WHEN_CHANGED)));
                generation = c.getLong(c.getColumnIndex(Nodes.NODE_GENERATION));
                generationCreated = c.getLong(c.getColumnIndex(Nodes.NODE_GENERATION_CREATED));
                contentPath = c.getString(c.getColumnIndex(Nodes.NODE_CONTENT_PATH));

                return new U1Node(resourcePath, kind, isLive, path, parentPath, volumePath, key, whenCreated,
                        whenChanged, generation, generationCreated, contentPath);
            } else {
                return null;
            }
        } finally {
            c.close();
        }
    }
    return null;
}