Example usage for android.database Cursor getCount

List of usage examples for android.database Cursor getCount

Introduction

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

Prototype

int getCount();

Source Link

Document

Returns the numbers of rows in the cursor.

Usage

From source file:ro.weednet.contactssync.platform.ContactManager.java

public static int getLocalContactsCount(Context context, Account account) {
    Log.i(TAG, "*** Counting local contacts");

    final Uri uri = RawContacts.CONTENT_URI.buildUpon()
            .appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name)
            .appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type).build();

    final ContentResolver resolver = context.getContentResolver();
    final Cursor c = resolver.query(uri, new String[] { Contacts._ID, RawContacts.SOURCE_ID }, null, null,
            null);//from  w ww . j av a2  s . com

    int count = 0;
    try {
        count = c.getCount();
    } catch (Exception e) {
    } finally {
        if (c != null) {
            c.close();
        }
    }

    Log.i(TAG, "*** ... found " + count);
    return count;
}

From source file:Main.java

/**
 * Try to get the exif orientation of the passed image uri
 * /*from  w  ww. j  a v  a 2 s.c om*/
 * @param context
 * @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[] { Images.ImageColumns.ORIENTATION, Images.ImageColumns.DATA }, null, null,
                        null);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }

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

            int orientationColumnIndex = result.getColumnIndex(Images.ImageColumns.ORIENTATION);
            int dataColumnIndex = result.getColumnIndex(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:busradar.madison.StopDialog.java

static RouteURL[] get_time_urls(int stopid) {
    Cursor c = G.db.rawQuery("SELECT url, route " + "FROM routestops " + "WHERE stopid = ? " + "ORDER BY route",
            new String[] { stopid + "" });

    RouteURL[] list = new RouteURL[c.getCount()];
    //ArrayList<RouteURL> list = new ArrayLst<RouteURL>();
    //int last = -1;
    int i = 0;//from   w  w w  . j  av  a 2  s. c  om
    while (c.moveToNext()) {
        RouteURL r = new RouteURL();
        r.url = c.getString(0);
        r.route = c.getInt(1);
        list[i++] = r;

    }

    c.close();
    return list;
}

From source file:Main.java

/**
 * Try to get the exif orientation of the passed image uri
 *
 * @param context//from w  ww  .  j  ava 2 s  . co  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:mil.nga.giat.mage.sdk.utils.MediaUtility.java

/**
 * Get display name from the uri// w ww  .  ja v a  2  s  . co  m
 *
 * @param context
 * @param uri
 * @return
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getDisplayName(Context context, Uri uri) {

    String name = null;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        ContentResolver resolver = context.getContentResolver();
        Cursor nameCursor = resolver.query(uri, null, null, null, null);
        try {
            if (nameCursor.getCount() > 0) {
                int displayNameIndex = nameCursor
                        .getColumnIndex(DocumentsContract.Document.COLUMN_DISPLAY_NAME);
                if (displayNameIndex >= 0 && nameCursor.moveToFirst()) {
                    name = nameCursor.getString(displayNameIndex);
                }
            }
        } finally {
            nameCursor.close();
        }
    }

    return name;
}

From source file:com.amarinfingroup.net.utilities.EncryptionUtils.java

/**
 * Retrieve the encryption information for this uri.
 *
 * @param mUri either an instance URI (if previously saved) or a form URI
 * @param instanceMetadata//from w ww .jav a  2  s.c o m
 * @return
 */
public static EncryptedFormInformation getEncryptedFormInformation(Uri mUri,
        InstanceMetadata instanceMetadata) {

    ContentResolver cr = Collect.getInstance().getContentResolver();

    // fetch the form information
    String formId;
    String formVersion;
    PublicKey pk;
    Base64Wrapper wrapper;

    Cursor formCursor = null;
    try {
        if (cr.getType(mUri) == InstanceColumns.CONTENT_ITEM_TYPE) {
            // chain back to the Form record...
            String[] selectionArgs = null;
            String selection = null;
            Cursor instanceCursor = null;
            try {
                instanceCursor = cr.query(mUri, null, null, null, null);
                if (instanceCursor.getCount() != 1) {
                    Log.e(t, "Not exactly one record for this instance!");
                    return null; // save unencrypted.
                }
                instanceCursor.moveToFirst();
                String jrFormId = instanceCursor
                        .getString(instanceCursor.getColumnIndex(InstanceColumns.JR_FORM_ID));
                int idxJrVersion = instanceCursor.getColumnIndex(InstanceColumns.JR_VERSION);
                if (!instanceCursor.isNull(idxJrVersion)) {
                    selectionArgs = new String[] { jrFormId, instanceCursor.getString(idxJrVersion) };
                    selection = FormsColumns.JR_FORM_ID + " =? AND " + FormsColumns.JR_VERSION + "=?";
                } else {
                    selectionArgs = new String[] { jrFormId };
                    selection = FormsColumns.JR_FORM_ID + " =? AND " + FormsColumns.JR_VERSION + " IS NULL";
                }
            } finally {
                if (instanceCursor != null) {
                    instanceCursor.close();
                }
            }

            formCursor = cr.query(FormsColumns.CONTENT_URI, null, selection, selectionArgs, null);

            if (formCursor.getCount() != 1) {
                Log.e(t, "Not exactly one blank form matches this jr_form_id");
                return null; // save unencrypted
            }
            formCursor.moveToFirst();
        } else if (cr.getType(mUri) == FormsColumns.CONTENT_ITEM_TYPE) {
            formCursor = cr.query(mUri, null, null, null, null);
            if (formCursor.getCount() != 1) {
                Log.e(t, "Not exactly one blank form!");
                return null; // save unencrypted.
            }
            formCursor.moveToFirst();
        }

        formId = formCursor.getString(formCursor.getColumnIndex(FormsColumns.JR_FORM_ID));
        if (formId == null || formId.length() == 0) {
            Log.e(t, "No FormId specified???");
            return null;
        }
        int idxVersion = formCursor.getColumnIndex(FormsColumns.JR_VERSION);
        int idxBase64RsaPublicKey = formCursor.getColumnIndex(FormsColumns.BASE64_RSA_PUBLIC_KEY);
        formVersion = formCursor.isNull(idxVersion) ? null : formCursor.getString(idxVersion);
        String base64RsaPublicKey = formCursor.isNull(idxBase64RsaPublicKey) ? null
                : formCursor.getString(idxBase64RsaPublicKey);

        if (base64RsaPublicKey == null || base64RsaPublicKey.length() == 0) {
            return null; // this is legitimately not an encrypted form
        }

        int version = android.os.Build.VERSION.SDK_INT;
        if (version < 8) {
            Log.e(t, "Phone does not support encryption.");
            return null; // save unencrypted
        }

        // this constructor will throw an exception if we are not
        // running on version 8 or above (if Base64 is not found).
        try {
            wrapper = new Base64Wrapper();
        } catch (ClassNotFoundException e) {
            Log.e(t, "Phone does not have Base64 class but API level is " + version);
            e.printStackTrace();
            return null; // save unencrypted
        }

        // OK -- Base64 decode (requires API Version 8 or higher)
        byte[] publicKey = wrapper.decode(base64RsaPublicKey);
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKey);
        KeyFactory kf;
        try {
            kf = KeyFactory.getInstance(RSA_ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            Log.e(t, "Phone does not support RSA encryption.");
            e.printStackTrace();
            return null;
        }
        try {
            pk = kf.generatePublic(publicKeySpec);
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
            Log.e(t, "Invalid RSA public key.");
            return null;
        }
    } finally {
        if (formCursor != null) {
            formCursor.close();
        }
    }

    // submission must have an OpenRosa metadata block with a non-null
    // instanceID value.
    if (instanceMetadata.instanceId == null) {
        Log.e(t, "No OpenRosa metadata block or no instanceId defined in that block");
        return null;
    }

    // For now, prevent encryption if the BouncyCastle implementation is not present.
    // https://code.google.com/p/opendatakit/issues/detail?id=918
    try {
        Cipher.getInstance(EncryptionUtils.SYMMETRIC_ALGORITHM, ENCRYPTION_PROVIDER);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        Log.e(t, "No BouncyCastle implementation of symmetric algorithm!");
        return null;
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
        Log.e(t, "No BouncyCastle provider for implementation of symmetric algorithm!");
        return null;
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
        Log.e(t, "No BouncyCastle provider for padding implementation of symmetric algorithm!");
        return null;
    }

    return new EncryptedFormInformation(formId, formVersion, instanceMetadata, pk, wrapper);
}

From source file:cd.education.data.collector.android.utilities.EncryptionUtils.java

/**
 * Retrieve the encryption information for this uri.
 *
 * @param mUri either an instance URI (if previously saved) or a form URI
 * @param instanceMetadata//from w  w w.j  a v  a  2s  .  c om
 * @return
 */
public static EncryptedFormInformation getEncryptedFormInformation(Uri mUri,
        FormController.InstanceMetadata instanceMetadata) {

    ContentResolver cr = Collect.getInstance().getContentResolver();

    // fetch the form information
    String formId;
    String formVersion;
    PublicKey pk;
    Base64Wrapper wrapper;

    Cursor formCursor = null;
    try {
        if (cr.getType(mUri) == InstanceColumns.CONTENT_ITEM_TYPE) {
            // chain back to the Form record...
            String[] selectionArgs = null;
            String selection = null;
            Cursor instanceCursor = null;
            try {
                instanceCursor = cr.query(mUri, null, null, null, null);
                if (instanceCursor.getCount() != 1) {
                    Log.e(t, "Not exactly one record for this instance!");
                    return null; // save unencrypted.
                }
                instanceCursor.moveToFirst();
                String jrFormId = instanceCursor
                        .getString(instanceCursor.getColumnIndex(InstanceColumns.JR_FORM_ID));
                int idxJrVersion = instanceCursor.getColumnIndex(InstanceColumns.JR_VERSION);
                if (!instanceCursor.isNull(idxJrVersion)) {
                    selectionArgs = new String[] { jrFormId, instanceCursor.getString(idxJrVersion) };
                    selection = FormsColumns.JR_FORM_ID + " =? AND " + FormsColumns.JR_VERSION + "=?";
                } else {
                    selectionArgs = new String[] { jrFormId };
                    selection = FormsColumns.JR_FORM_ID + " =? AND " + FormsColumns.JR_VERSION + " IS NULL";
                }
            } finally {
                if (instanceCursor != null) {
                    instanceCursor.close();
                }
            }

            formCursor = cr.query(FormsColumns.CONTENT_URI, null, selection, selectionArgs, null);

            if (formCursor.getCount() != 1) {
                Log.e(t, "Not exactly one blank form matches this jr_form_id");
                return null; // save unencrypted
            }
            formCursor.moveToFirst();
        } else if (cr.getType(mUri) == FormsColumns.CONTENT_ITEM_TYPE) {
            formCursor = cr.query(mUri, null, null, null, null);
            if (formCursor.getCount() != 1) {
                Log.e(t, "Not exactly one blank form!");
                return null; // save unencrypted.
            }
            formCursor.moveToFirst();
        }

        formId = formCursor.getString(formCursor.getColumnIndex(FormsColumns.JR_FORM_ID));
        if (formId == null || formId.length() == 0) {
            Log.e(t, "No FormId specified???");
            return null;
        }
        int idxVersion = formCursor.getColumnIndex(FormsColumns.JR_VERSION);
        int idxBase64RsaPublicKey = formCursor.getColumnIndex(FormsColumns.BASE64_RSA_PUBLIC_KEY);
        formVersion = formCursor.isNull(idxVersion) ? null : formCursor.getString(idxVersion);
        String base64RsaPublicKey = formCursor.isNull(idxBase64RsaPublicKey) ? null
                : formCursor.getString(idxBase64RsaPublicKey);

        if (base64RsaPublicKey == null || base64RsaPublicKey.length() == 0) {
            return null; // this is legitimately not an encrypted form
        }

        int version = android.os.Build.VERSION.SDK_INT;
        if (version < 8) {
            Log.e(t, "Phone does not support encryption.");
            return null; // save unencrypted
        }

        // this constructor will throw an exception if we are not
        // running on version 8 or above (if Base64 is not found).
        try {
            wrapper = new Base64Wrapper();
        } catch (ClassNotFoundException e) {
            Log.e(t, "Phone does not have Base64 class but API level is " + version);
            e.printStackTrace();
            return null; // save unencrypted
        }

        // OK -- Base64 decode (requires API Version 8 or higher)
        byte[] publicKey = wrapper.decode(base64RsaPublicKey);
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKey);
        KeyFactory kf;
        try {
            kf = KeyFactory.getInstance(RSA_ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            Log.e(t, "Phone does not support RSA encryption.");
            e.printStackTrace();
            return null;
        }
        try {
            pk = kf.generatePublic(publicKeySpec);
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
            Log.e(t, "Invalid RSA public key.");
            return null;
        }
    } finally {
        if (formCursor != null) {
            formCursor.close();
        }
    }

    // submission must have an OpenRosa metadata block with a non-null
    // instanceID value.
    if (instanceMetadata.instanceId == null) {
        Log.e(t, "No OpenRosa metadata block or no instanceId defined in that block");
        return null;
    }

    // For now, prevent encryption if the BouncyCastle implementation is not present.
    // https://code.google.com/p/opendatakit/issues/detail?id=918
    try {
        Cipher.getInstance(EncryptionUtils.SYMMETRIC_ALGORITHM, ENCRYPTION_PROVIDER);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        Log.e(t, "No BouncyCastle implementation of symmetric algorithm!");
        return null;
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
        Log.e(t, "No BouncyCastle provider for implementation of symmetric algorithm!");
        return null;
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
        Log.e(t, "No BouncyCastle provider for padding implementation of symmetric algorithm!");
        return null;
    }

    return new EncryptedFormInformation(formId, formVersion, instanceMetadata, pk, wrapper);
}

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

public static int getCount(String resourcePath) {
    final String[] projection = new String[] { Nodes._ID };
    final String[] selectionArgs = new String[] { resourcePath };
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, sSelection, selectionArgs, null);
    int count = 0;
    try {//from w ww.  j  av  a  2 s . c om
        count = c.getCount();
    } finally {
        c.close();
    }
    return count;
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static boolean groupHasUncatchedMessages(String group, Context context) {
    int groupid = getGroupIdFromName(group, context);

    DBHelper db = new DBHelper(context);
    SQLiteDatabase dbread = db.getReadableDatabase();

    String q = "SELECT _id FROM headers WHERE subscribed_group_id=" + groupid + " AND read=0 AND catched=0";
    Cursor c = dbread.rawQuery(q, null);

    int count = c.getCount();

    c.close();//  w w  w  .j av a  2 s .  co  m
    dbread.close();
    db.close();

    return (count > 0);
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static long getGroupLastFetchedNumber(String group, Context context) {

    long lastFetched = -1;

    DBHelper dbhelper = new DBHelper(context);
    SQLiteDatabase readdb = dbhelper.getReadableDatabase();

    String fQuery = "SELECT lastFetched FROM subscribed_groups WHERE name=" + esc(group);
    Cursor cur = readdb.rawQuery(fQuery, null);

    if (cur.getCount() > 0) {
        cur.moveToFirst();/*w w w  .  j  ava2 s.  com*/
        lastFetched = cur.getInt(0);
    }

    cur.close();
    readdb.close();
    dbhelper.close();

    return lastFetched;
}