Example usage for android.database Cursor isNull

List of usage examples for android.database Cursor isNull

Introduction

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

Prototype

boolean isNull(int columnIndex);

Source Link

Document

Returns true if the value in the indicated column is null.

Usage

From source file:com.mobeelizer.mobile.android.types.FileFieldTypeHelper.java

@Override
public void setValueFromDatabaseToMap(final Cursor cursor, final Map<String, String> values,
        final MobeelizerFieldAccessor field, final Map<String, String> options) {
    int columnIndex = cursor.getColumnIndex(field.getName() + _GUID);

    if (cursor.isNull(columnIndex)) {
        values.put(field.getName(), null);
    } else {//from w  w  w  . j  a v a2s  .  c  o  m
        try {
            JSONObject json = new JSONObject();
            json.put(JSON_GUID, cursor.getString(columnIndex));
            json.put(JSON_NAME, cursor.getString(cursor.getColumnIndex(field.getName() + _NAME)));

            values.put(field.getName(), json.toString());
        } catch (JSONException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }
}

From source file:com.mobeelizer.mobile.android.types.FileFieldTypeHelper.java

@Override
public <T> void setValueFromDatabaseToEntity(final Cursor cursor, final T entity,
        final MobeelizerFieldAccessor field, final Map<String, String> options) {
    int columnIndex = cursor.getColumnIndex(field.getName() + FileFieldTypeHelper._GUID);

    if (cursor.isNull(columnIndex)) {
        return;//from w  ww  . j  ava 2  s.c om
    }

    String[] file = new String[] { cursor.getString(columnIndex),
            cursor.getString(cursor.getColumnIndex(field.getName() + FileFieldTypeHelper._NAME)) };

    MobeelizerFile mobeelizerFile = (MobeelizerFile) getType().convertFromDatabaseValueToEntityValue(field,
            file);

    setValue(field, entity, new MobeelizerFileImpl(mobeelizerFile.getName(), mobeelizerFile.getGuid()));
}

From source file:com.silentcircle.contacts.list.PhoneNumberListAdapter.java

protected void bindPhoto(final ContactListItemView view, Cursor cursor) {
    long photoId = 0;
    if (!cursor.isNull(PhoneQuery.PHONE_PHOTO_ID)) {
        photoId = cursor.getLong(PhoneQuery.PHONE_PHOTO_ID);
    }/*  ww w.  j a v a2  s  .c o  m*/
    getPhotoLoader().loadThumbnail(view.getPhotoView(), photoId, true);
}

From source file:com.silentcircle.contacts.list.PhoneNumberListAdapter.java

protected void bindPhoneNumber(ContactListItemView view, Cursor cursor) {
    CharSequence label = null;/*from  w  w w. j  a va2  s . com*/
    if (!cursor.isNull(PhoneQuery.PHONE_TYPE)) {
        final int type = cursor.getInt(PhoneQuery.PHONE_TYPE);
        final String customLabel = cursor.getString(PhoneQuery.PHONE_LABEL);

        // TODO cache
        label = PhoneLabel.getTypeLabel(getContext().getResources(), type, customLabel);
    }
    view.setLabel(label);
    view.showData(cursor, PhoneQuery.PHONE_NUMBER);
}

From source file:com.textuality.lifesaver.Columns.java

public JSONObject cursorToJSON(Cursor cursor) {
    setColumns(cursor);//from  ww w . j  a va  2 s. co m
    JSONObject json = new JSONObject();
    try {
        for (int i = 0; i < names.length; i++) {
            int col = columns[i];
            if (cursor.isNull(col))
                continue;
            switch (types[i]) {
            case STRING:
                json.put(names[i], cursor.getString(col));
                break;
            case INT:
                json.put(names[i], cursor.getInt(col));
                break;
            case LONG:
                json.put(names[i], cursor.getLong(col));
                break;
            case FLOAT:
                json.put(names[i], cursor.getFloat(col));
                break;
            case DOUBLE:
                json.put(names[i], cursor.getDouble(col));
                break;
            }
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }

    return json;
}

From source file:com.swater.meimeng.activity.oomimg.SimpleThumbnailCursorAdapter.java

@Override
public void setViewImage(ImageView v, String value) {
    final int id = v.getId();
    for (int i = 0; i < mImageIDs.length; i++) {
        if (id == mImageIDs[i]) {
            final List<String> alternates = mAlternateImages.get(id);
            if (alternates != null && alternates.size() > 1) {
                final Cursor c = getCursor();
                for (final String alternate : alternates) {
                    final int idx = c.getColumnIndex(alternate);
                    if (c.isNull(idx)) {
                        continue;
                    } else {
                        // only set the first one that isn't null
                        setViewImageAndTag(v, c.getString(idx), defaultImages[i]);
                        break;
                    }/*from  w  w w .jav  a2 s .co m*/
                }
            } else {
                setViewImageAndTag(v, value, defaultImages[i]);
            }
        }
    }
}

From source file:org.strongswan.android.data.VpnProfileDataSource.java

private Integer getInt(Cursor cursor, int columnIndex) {
    return cursor.isNull(columnIndex) ? null : cursor.getInt(columnIndex);
}

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//  w  w w  .  j  a  va2 s .co  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// w w w  .j a  va 2s  .c  o  m
 * @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.textuality.lifesaver2.Columns.java

public JSONObject cursorToJSON(Cursor cursor) {
    setColumns(cursor);//from   w ww.  ja v a 2s. co m
    JSONObject json = new JSONObject();
    try {
        for (int i = 0; i < mNames.length; i++) {
            int col = mColumns[i];
            if (cursor.isNull(col))
                continue;
            switch (mTypes[i]) {
            case STRING:
                String str = cursor.getString(col);
                if (mNames[i].equals("number")) {
                    json.put("name", mNameFinder.find(str));
                } else if (mNames[i].equals("address")) {
                    str = PhoneNumberUtils.formatNumber(str);
                    str = PhoneNumberUtils.stripSeparators(str);
                    json.put("name", mNameFinder.find(str));
                }
                json.put(mNames[i], str);
                break;
            case INT:
                json.put(mNames[i], cursor.getInt(col));
                break;
            case LONG:
                long val = cursor.getLong(col);
                json.put(mNames[i], val);
                if (mNames[i].equals("date")) {
                    json.put("tzoffset", mTZ.getOffset(val));
                }
                break;
            case FLOAT:
                json.put(mNames[i], cursor.getFloat(col));
                break;
            case DOUBLE:
                json.put(mNames[i], cursor.getDouble(col));
                break;
            }
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }

    return json;
}