List of usage examples for android.content ContentValues getAsByteArray
public byte[] getAsByteArray(String key)
From source file:at.bitfire.davdroid.resource.LocalAddressBook.java
protected void populatePhoto(Contact c, ContentValues row) throws RemoteException { if (row.containsKey(Photo.PHOTO_FILE_ID)) { Uri photoUri = Uri.withAppendedPath(ContentUris.withAppendedId(RawContacts.CONTENT_URI, c.getLocalID()), RawContacts.DisplayPhoto.CONTENT_DIRECTORY); try {/* w w w . ja va2 s . c o m*/ @Cleanup AssetFileDescriptor fd = providerClient.openAssetFile(photoUri, "r"); @Cleanup InputStream is = fd.createInputStream(); c.photo = IOUtils.toByteArray(is); } catch (IOException ex) { Log.w(TAG, "Couldn't read high-res contact photo", ex); } } else c.photo = row.getAsByteArray(Photo.PHOTO); }
From source file:com.granita.icloudcalsync.resource.LocalAddressBook.java
protected void populatePhoto(Contact c, ContentValues row) throws RemoteException { if (row.containsKey(Photo.PHOTO_FILE_ID)) { Uri photoUri = Uri.withAppendedPath(ContentUris.withAppendedId(RawContacts.CONTENT_URI, c.getLocalID()), RawContacts.DisplayPhoto.CONTENT_DIRECTORY); try {//from w w w . ja v a 2 s . c o m @Cleanup AssetFileDescriptor fd = providerClient.openAssetFile(photoUri, "r"); @Cleanup InputStream is = fd.createInputStream(); c.setPhoto(IOUtils.toByteArray(is)); } catch (IOException ex) { Log.w(TAG, "Couldn't read high-res contact photo", ex); } } else c.setPhoto(row.getAsByteArray(Photo.PHOTO)); }
From source file:android.pim.vcard.VCardBuilder.java
public VCardBuilder appendPhotos(final List<ContentValues> contentValuesList) { if (contentValuesList != null) { for (ContentValues contentValues : contentValuesList) { if (contentValues == null) { continue; }//from w ww. ja v a2s . c om byte[] data = contentValues.getAsByteArray(Photo.PHOTO); if (data == null) { continue; } final String photoType = VCardUtils.guessImageType(data); if (photoType == null) { Log.d(LOG_TAG, "Unknown photo type. Ignored."); continue; } final String photoString = new String(Base64.encodeBase64(data)); if (!TextUtils.isEmpty(photoString)) { appendPhotoLine(photoString, photoType); } } } return this; }
From source file:at.bitfire.vcard4android.AndroidContact.java
protected void populatePhoto(ContentValues row) throws RemoteException { if (row.containsKey(Photo.PHOTO_FILE_ID)) { Uri photoUri = Uri.withAppendedPath(rawContactSyncURI(), RawContacts.DisplayPhoto.CONTENT_DIRECTORY); try {//from w w w .j a v a 2 s. c o m @Cleanup AssetFileDescriptor fd = addressBook.provider.openAssetFile(photoUri, "r"); @Cleanup InputStream stream = fd.createInputStream(); if (stream != null) contact.photo = IOUtils.toByteArray(stream); else Constants.log.warn("Ignoring inaccessible local contact photo file"); } catch (IOException e) { Constants.log.warn("Couldn't read local contact photo file", e); } } else contact.photo = row.getAsByteArray(Photo.PHOTO); }
From source file:edu.stanford.mobisocial.dungbeetle.DBHelper.java
/** * Inserts an object into the database and flags it to be sent by * the transport layer.//from w ww . j a va2 s .c om */ long addToFeed(String appId, String feedName, ContentValues values) { try { JSONObject json = new JSONObject(values.getAsString(DbObject.JSON)); String type = values.getAsString(DbObject.TYPE); long nextSeqId = getFeedMaxSequenceId(Contact.MY_ID, feedName) + 1; long timestamp = new Date().getTime(); json.put(DbObjects.TYPE, type); json.put(DbObjects.FEED_NAME, feedName); json.put(DbObjects.SEQUENCE_ID, nextSeqId); json.put(DbObjects.TIMESTAMP, timestamp); json.put(DbObjects.APP_ID, appId); // Explicit column referencing avoids database errors. ContentValues cv = new ContentValues(); cv.put(DbObject._ID, getNextId()); cv.put(DbObject.APP_ID, appId); cv.put(DbObject.FEED_NAME, feedName); cv.put(DbObject.CONTACT_ID, Contact.MY_ID); cv.put(DbObject.TYPE, type); cv.put(DbObject.SEQUENCE_ID, nextSeqId); cv.put(DbObject.JSON, json.toString()); cv.put(DbObject.TIMESTAMP, timestamp); cv.put(DbObject.LAST_MODIFIED_TIMESTAMP, new Date().getTime()); if (values.containsKey(DbObject.RAW)) { cv.put(DbObject.RAW, values.getAsByteArray(DbObject.RAW)); } if (values.containsKey(DbObject.KEY_INT)) { cv.put(DbObject.KEY_INT, values.getAsInteger(DbObject.KEY_INT)); } if (json.has(DbObject.CHILD_FEED_NAME)) { cv.put(DbObject.CHILD_FEED_NAME, json.optString(DbObject.CHILD_FEED_NAME)); } if (cv.getAsString(DbObject.JSON).length() > SIZE_LIMIT) throw new RuntimeException("Messasge size is too large for sending"); Long objId = getWritableDatabase().insertOrThrow(DbObject.TABLE, null, cv); if (json.has(DbObjects.TARGET_HASH)) { long hashA = json.optLong(DbObjects.TARGET_HASH); long idA = objIdForHash(hashA); String relation; if (json.has(DbObjects.TARGET_RELATION)) { relation = json.optString(DbObjects.TARGET_RELATION); } else { relation = DbRelation.RELATION_PARENT; } if (idA == -1) { Log.e(TAG, "No objId found for hash " + hashA); } else { addObjRelation(idA, objId, relation); } } Uri objUri = DbObject.uriForObj(objId); mContext.getContentResolver().registerContentObserver(objUri, false, new ModificationObserver(mContext, objId)); return objId; } catch (Exception e) { // TODO, too spammy //e.printStackTrace(System.err); return -1; } }
From source file:edu.stanford.mobisocial.dungbeetle.DungBeetleContentProvider.java
/** * Inserts a message locally that has been received from some agent, * typically from a remote device./* w ww.j a v a2s. co m*/ */ @Override public Uri insert(Uri uri, ContentValues values) { ContentResolver resolver = getContext().getContentResolver(); if (DBG) Log.i(TAG, "Inserting at uri: " + uri + ", " + values); final String appId = getCallingActivityId(); if (appId == null) { Log.d(TAG, "No AppId for calling activity. Ignoring query."); return null; } List<String> segs = uri.getPathSegments(); if (match(uri, "feeds", "me")) { if (!appId.equals(SUPER_APP_ID)) { return null; } long objId = mHelper.addToFeed(appId, "friend", values); Uri objUri = DbObject.uriForObj(objId); resolver.notifyChange(Feed.uriForName("me"), null); resolver.notifyChange(Feed.uriForName("friend"), null); resolver.notifyChange(objUri, null); return objUri; } else if (match(uri, "feeds", ".+")) { String feedName = segs.get(1); String type = values.getAsString(DbObject.TYPE); try { JSONObject json = new JSONObject(values.getAsString(DbObject.JSON)); String objHash = null; if (feedName.contains(":")) { String[] parts = feedName.split(":"); feedName = parts[0]; objHash = parts[1]; } if (objHash != null) { json.put(DbObjects.TARGET_HASH, Long.parseLong(objHash)); json.put(DbObjects.TARGET_RELATION, DbRelation.RELATION_PARENT); values.put(DbObject.JSON, json.toString()); } String appAuthority = appId; if (SUPER_APP_ID.equals(appId)) { if (AppObj.TYPE.equals(type)) { if (json.has(AppObj.ANDROID_PACKAGE_NAME)) { appAuthority = json.getString(AppObj.ANDROID_PACKAGE_NAME); } } } long objId = mHelper.addToFeed(appAuthority, feedName, values); Uri objUri = DbObject.uriForObj(objId); resolver.notifyChange(objUri, null); notifyDependencies(mHelper, resolver, segs.get(1)); if (DBG) Log.d(TAG, "just inserted " + values.getAsString(DbObject.JSON)); return objUri; } catch (JSONException e) { return null; } } else if (match(uri, "out")) { try { JSONObject obj = new JSONObject(values.getAsString("json")); long objId = mHelper.addToOutgoing(appId, values.getAsString(DbObject.DESTINATION), values.getAsString(DbObject.TYPE), obj); resolver.notifyChange(Uri.parse(CONTENT_URI + "/out"), null); return DbObject.uriForObj(objId); } catch (JSONException e) { return null; } } else if (match(uri, "contacts")) { if (!appId.equals(SUPER_APP_ID)) { return null; } long id = mHelper.insertContact(values); resolver.notifyChange(Uri.parse(CONTENT_URI + "/contacts"), null); return uriWithId(uri, id); } else if (match(uri, "subscribers")) { // Question: Should this be restricted? // if(!appId.equals(SUPER_APP_ID)) return null; long id = mHelper.insertSubscriber(values); resolver.notifyChange(Uri.parse(CONTENT_URI + "/subscribers"), null); return uriWithId(uri, id); } else if (match(uri, "groups")) { if (!appId.equals(SUPER_APP_ID)) return null; long id = mHelper.insertGroup(values); getContext().getContentResolver().notifyChange(Uri.parse(CONTENT_URI + "/groups"), null); return uriWithId(uri, id); } else if (match(uri, "group_members")) { if (!appId.equals(SUPER_APP_ID)) { return null; } long id = mHelper.insertGroupMember(values); getContext().getContentResolver().notifyChange(Uri.parse(CONTENT_URI + "/group_members"), null); getContext().getContentResolver().notifyChange(Uri.parse(CONTENT_URI + "/group_contacts"), null); return uriWithId(uri, id); } else if (match(uri, "group_invitations")) { if (!appId.equals(SUPER_APP_ID)) { return null; } String groupName = values.getAsString(InviteToGroupObj.GROUP_NAME); Uri dynUpdateUri = Uri.parse(values.getAsString(InviteToGroupObj.DYN_UPDATE_URI)); long gid = values.getAsLong("groupId"); SQLiteDatabase db = mHelper.getWritableDatabase(); mHelper.addToOutgoing(db, appId, values.getAsString(InviteToGroupObj.PARTICIPANTS), InviteToGroupObj.TYPE, InviteToGroupObj.json(groupName, dynUpdateUri)); getContext().getContentResolver().notifyChange(Uri.parse(CONTENT_URI + "/out"), null); return uriWithId(uri, gid); } else if (match(uri, "dynamic_groups")) { if (!appId.equals(SUPER_APP_ID)) { return null; } Uri gUri = Uri.parse(values.getAsString("uri")); GroupProviders.GroupProvider gp = GroupProviders.forUri(gUri); String feedName = gp.feedName(gUri); Maybe<Group> mg = mHelper.groupByFeedName(feedName); long id = -1; try { Group g = mg.get(); id = g.id; } catch (Maybe.NoValError e) { ContentValues cv = new ContentValues(); cv.put(Group.NAME, gp.groupName(gUri)); cv.put(Group.FEED_NAME, feedName); cv.put(Group.DYN_UPDATE_URI, gUri.toString()); String table = DbObject.TABLE; String[] columns = new String[] { DbObject.FEED_NAME }; String selection = DbObject.CHILD_FEED_NAME + " = ?"; String[] selectionArgs = new String[] { feedName }; Cursor parent = mHelper.getReadableDatabase().query(table, columns, selection, selectionArgs, null, null, null); try { if (parent.moveToFirst()) { String parentName = parent.getString(0); table = Group.TABLE; columns = new String[] { Group._ID }; selection = Group.FEED_NAME + " = ?"; selectionArgs = new String[] { parentName }; Cursor parent2 = mHelper.getReadableDatabase().query(table, columns, selection, selectionArgs, null, null, null); try { if (parent2.moveToFirst()) { cv.put(Group.PARENT_FEED_ID, parent2.getLong(0)); } else { Log.e(TAG, "Parent feed found but no id for " + parentName); } } finally { parent2.close(); } } else { Log.w(TAG, "No parent feed for " + feedName); } } finally { parent.close(); } id = mHelper.insertGroup(cv); getContext().getContentResolver().notifyChange(Uri.parse(CONTENT_URI + "/dynamic_groups"), null); getContext().getContentResolver().notifyChange(Uri.parse(CONTENT_URI + "/groups"), null); } return uriWithId(uri, id); } else if (match(uri, "dynamic_group_member")) { if (!appId.equals(SUPER_APP_ID)) { return null; } SQLiteDatabase db = mHelper.getWritableDatabase(); db.beginTransaction(); try { ContentValues cv = new ContentValues(); String pubKeyStr = values.getAsString(Contact.PUBLIC_KEY); RSAPublicKey k = RSACrypto.publicKeyFromString(pubKeyStr); String personId = mIdent.personIdForPublicKey(k); if (!personId.equals(mIdent.userPersonId())) { cv.put(Contact.PUBLIC_KEY, values.getAsString(Contact.PUBLIC_KEY)); cv.put(Contact.NAME, values.getAsString(Contact.NAME)); cv.put(Contact.EMAIL, values.getAsString(Contact.EMAIL)); if (values.getAsString(Contact.PICTURE) != null) { cv.put(Contact.PICTURE, values.getAsByteArray(Contact.PICTURE)); } long cid = -1; Contact contact = mHelper.contactForPersonId(personId).otherwise(Contact.NA()); if (contact.id > -1) { cid = contact.id; } else { cid = mHelper.insertContact(db, cv); } if (cid > -1) { ContentValues gv = new ContentValues(); gv.put(GroupMember.GLOBAL_CONTACT_ID, values.getAsString(GroupMember.GLOBAL_CONTACT_ID)); gv.put(GroupMember.GROUP_ID, values.getAsLong(GroupMember.GROUP_ID)); gv.put(GroupMember.CONTACT_ID, cid); mHelper.insertGroupMember(db, gv); getContext().getContentResolver().notifyChange(Uri.parse(CONTENT_URI + "/group_members"), null); getContext().getContentResolver().notifyChange(Uri.parse(CONTENT_URI + "/contacts"), null); getContext().getContentResolver().notifyChange(Uri.parse(CONTENT_URI + "/group_contacts"), null); // Add subscription to this private group feed ContentValues sv = new ContentValues(); sv = new ContentValues(); sv.put(Subscriber.CONTACT_ID, cid); sv.put(Subscriber.FEED_NAME, values.getAsString(Group.FEED_NAME)); mHelper.insertSubscriber(db, sv); ContentValues xv = new ContentValues(); xv.put(Subscriber.CONTACT_ID, cid); xv.put(Subscriber.FEED_NAME, "friend"); mHelper.insertSubscriber(db, xv); getContext().getContentResolver().notifyChange(Uri.parse(CONTENT_URI + "/subscribers"), null); db.setTransactionSuccessful(); } return uriWithId(uri, cid); } else { Log.i(TAG, "Omitting self."); return uriWithId(uri, Contact.MY_ID); } } finally { db.endTransaction(); } } else { Log.e(TAG, "Failed to insert into " + uri); return null; } }