List of usage examples for android.database Cursor isNull
boolean isNull(int columnIndex);
true
if the value in the indicated column is null. From source file:org.opensilk.video.data.VideosProviderClient.java
MediaBrowser.MediaItem buildShallowMedia(Cursor c) { String displayName = c.getString(0); Uri parentUri = Uri.parse(c.getString(1)); String serverId = c.getString(2); String title = c.getString(3); String subtitle = c.getString(4); Uri artworkUri = StringUtils.isEmpty(c.getString(5)) ? null : Uri.parse(c.getString(5)); boolean isIndexed = c.getInt(6) == 1; long lastPosition = c.isNull(7) ? -1 : c.getLong(7); long duration = c.isNull(8) ? -1 : c.getLong(8); int type = c.getInt(9); Uri mediaUri = Uri.parse(c.getString(10)); String pfx = "media:"; int flag = 0; switch (type) { case MediaMetaExtras.MEDIA_TYPE.DIRECTORY: pfx = "directory:"; flag = MediaBrowser.MediaItem.FLAG_BROWSABLE; break;/* w w w .j a v a2s . co m*/ case MediaMetaExtras.MEDIA_TYPE.TV_EPISODE: pfx = "tv_episode:"; flag = MediaBrowser.MediaItem.FLAG_PLAYABLE; break; case MediaMetaExtras.MEDIA_TYPE.MOVIE: pfx = "movie:"; flag = MediaBrowser.MediaItem.FLAG_PLAYABLE; break; case MediaMetaExtras.MEDIA_TYPE.VIDEO: pfx = "video:"; flag = MediaBrowser.MediaItem.FLAG_PLAYABLE; break; } MediaDescription.Builder builder = new MediaDescription.Builder().setMediaId(pfx + mediaUri) .setTitle(StringUtils.isEmpty(title) ? displayName : title).setSubtitle(subtitle) .setIconUri(artworkUri); MediaMetaExtras metaExtras = MediaMetaExtras.unknown().setMediaType(type).setMediaTitle(displayName) .setParentUri(parentUri).setServerId(serverId).setIndexed(isIndexed).setLastPosition(lastPosition) .setDuration(duration); MediaDescriptionUtil.setMediaUri(builder, metaExtras, mediaUri); builder.setExtras(metaExtras.getBundle()); return new MediaBrowser.MediaItem(builder.build(), flag); }
From source file:net.kourlas.voipms_sms.Database.java
/** * Gets all of the messages associated with the specified contact phone number, except for deleted messages. * * @param contact The contact phone number. * @return All of the messages associated with the specified contact phone number, except for deleted messages. *//* w w w. jav a 2s . c om*/ public synchronized Conversation getConversation(String did, String contact) { List<Message> messages = new ArrayList<>(); Cursor cursor = database.query(TABLE_MESSAGE, columns, COLUMN_CONTACT + "=" + contact + " AND " + COLUMN_DID + "=" + did + " AND " + COLUMN_DELETED + "=" + "0", null, null, null, null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { Message message = new Message(cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DATABASE_ID)), cursor.isNull(cursor.getColumnIndexOrThrow(COLUMN_VOIP_ID)) ? null : cursor.getLong(cursor.getColumnIndex(COLUMN_VOIP_ID)), cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DATE)), cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_TYPE)), cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DID)), cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTACT)), cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MESSAGE)), cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_UNREAD)), cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELETED)), cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELIVERED)), cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DELIVERY_IN_PROGRESS))); messages.add(message); cursor.moveToNext(); } cursor.close(); Message[] messageArray = new Message[messages.size()]; messages.toArray(messageArray); return new Conversation(messageArray); }
From source file:org.jamienicol.episodes.NextEpisodeFragment.java
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { if (data != null && data.moveToFirst()) { final int episodeIdColumnIndex = data.getColumnIndexOrThrow(EpisodesTable.COLUMN_ID); episodeId = data.getInt(episodeIdColumnIndex); final int seasonNumberColumnIndex = data.getColumnIndexOrThrow(EpisodesTable.COLUMN_SEASON_NUMBER); final int seasonNumber = data.getInt(seasonNumberColumnIndex); final int episodeNumberColumnIndex = data.getColumnIndexOrThrow(EpisodesTable.COLUMN_EPISODE_NUMBER); final int episodeNumber = data.getInt(episodeNumberColumnIndex); final int titleColumnIndex = data.getColumnIndexOrThrow(EpisodesTable.COLUMN_NAME); final String title = data.getString(titleColumnIndex); String titleText = ""; if (seasonNumber != 0) { titleText += getActivity().getString(R.string.season_episode_prefix, seasonNumber, episodeNumber); }//from w w w . jav a 2 s. co m titleText += title; titleView.setText(titleText); final int overviewColumnIndex = data.getColumnIndexOrThrow(EpisodesTable.COLUMN_OVERVIEW); if (data.isNull(overviewColumnIndex)) { overviewView.setText(""); } else { overviewView.setText(data.getString(overviewColumnIndex)); } final int firstAiredColumnIndex = data.getColumnIndexOrThrow(EpisodesTable.COLUMN_FIRST_AIRED); if (data.isNull(firstAiredColumnIndex)) { dateView.setText(""); } else { final Date date = new Date(data.getLong(firstAiredColumnIndex) * 1000); final String dateText = DateFormat.getDateInstance(DateFormat.LONG).format(date); dateView.setText(dateText); } final int watchedColumnIndex = data.getColumnIndexOrThrow(EpisodesTable.COLUMN_WATCHED); watched = data.getInt(watchedColumnIndex) > 0 ? true : false; watchedCheckBox.setChecked(watched); rootView.setVisibility(View.VISIBLE); } else { episodeId = -1; rootView.setVisibility(View.INVISIBLE); } }
From source file:at.bitfire.davdroid.mirakel.resource.LocalAddressBook.java
protected void populatePhoto(Contact c) throws RemoteException { @Cleanup Cursor cursor = providerClient.query(dataURI(), new String[] { Photo.PHOTO_FILE_ID, Photo.PHOTO }, Photo.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?", new String[] { String.valueOf(c.getLocalID()), Photo.CONTENT_ITEM_TYPE }, null); if (cursor != null && cursor.moveToNext()) { if (!cursor.isNull(0)) { Uri photoUri = Uri.withAppendedPath( ContentUris.withAppendedId(RawContacts.CONTENT_URI, c.getLocalID()), RawContacts.DisplayPhoto.CONTENT_DIRECTORY); try { @Cleanup//from ww w . java 2 s . com 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(cursor.getBlob(1)); } }
From source file:ch.ethz.twimight.net.opportunistic.ScanningService.java
private void sendDisasterHtmls(Cursor c) throws JSONException { JSONObject toSendXml;/*from w w w . j a v a 2 s .c om*/ // String userId = String.valueOf(c.getLong(c // .getColumnIndex(Tweets.COL_TWITTERUSER))); String substr = Html.fromHtml(c.getString(c.getColumnIndex(Tweets.COL_TEXT))).toString(); String[] strarr = substr.split(" "); // check the urls of the tweet for (String subStrarr : strarr) { if (subStrarr.indexOf("http://") >= 0 || subStrarr.indexOf("https://") >= 0) { String subUrl = null; if (subStrarr.indexOf("http://") >= 0) { subUrl = subStrarr.substring(subStrarr.indexOf("http://")); } else if (subStrarr.indexOf("https://") >= 0) { subUrl = subStrarr.substring(subStrarr.indexOf("https://")); } Cursor cursorHtml = htmlDbHelper.getPageInfo(subUrl); if (cursorHtml != null) { if (!cursorHtml.isNull(cursorHtml.getColumnIndex(HtmlPage.COL_FILENAME))) { String[] filePath = { HtmlPage.HTML_PATH + "/" + LoginActivity.getTwitterId(this) }; String filename = cursorHtml.getString(cursorHtml.getColumnIndex(HtmlPage.COL_FILENAME)); Long tweetId = cursorHtml.getLong(cursorHtml.getColumnIndex(HtmlPage.COL_DISASTERID)); if (sdCardHelper.checkSDState(filePath)) { File xmlFile = sdCardHelper.getFileFromSDCard(filePath[0], filename); if (xmlFile.exists()) { toSendXml = getJSONFromXml(xmlFile); toSendXml.put(HtmlPage.COL_URL, subUrl); toSendXml.put(HtmlPage.COL_FILENAME, filename); toSendXml.put(HtmlPage.COL_DISASTERID, tweetId); Log.d(TAG, "sending htmls"); Log.d(TAG, toSendXml.toString(5)); bluetoothHelper.write(toSendXml.toString()); } } } } } } }
From source file:org.mozilla.gecko.tests.BaseTest.java
public boolean CursorMatches(Cursor c, ContentValues cv) { for (int i = 0; i < c.getColumnCount(); i++) { String column = c.getColumnName(i); if (cv.containsKey(column)) { mAsserter.info("Comparing", "Column values for: " + column); Object value = cv.get(column); if (value == null) { if (!c.isNull(i)) { return false; }/*from w ww. j a va 2 s. c o m*/ } else { if (c.isNull(i) || !value.toString().equals(c.getString(i))) { return false; } } } } return true; }
From source file:info.staticfree.android.units.Units.java
@Override public boolean onContextItemSelected(MenuItem item) { final ContextMenuInfo ctxMenuInfo = item.getMenuInfo(); int position = history.getCount() - 1; if (ctxMenuInfo instanceof AdapterContextMenuInfo) { position = ((AdapterContextMenuInfo) ctxMenuInfo).position; }/*from w w w .j a v a 2 s .c o m*/ final Uri itemUri = ContentUris.withAppendedId(HistoryEntry.CONTENT_URI, mHistoryAdapter.getItemId(position)); switch (item.getItemId()) { case MENU_COPY: { final CharSequence itemText = getEntryAsCharSequence(itemUri); final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); clipboard.setText(itemText); Toast.makeText(this, getString(R.string.toast_copy, itemText), Toast.LENGTH_SHORT).show(); } break; case MENU_REEDIT: { setCurrentEntry(itemUri); setHistoryVisible(false); } break; case MENU_SEND: { final CharSequence itemText = getEntryAsCharSequence(itemUri); startActivity(Intent.createChooser( new Intent(Intent.ACTION_SEND).setType("text/plain").putExtra(Intent.EXTRA_TEXT, itemText), getText(R.string.ctx_menu_send_title))); } break; case MENU_USE_RESULT: { final Cursor c = getContentResolver().query(itemUri, PROJECTION_LOAD_FROM_HISTORY, null, null, null); if (c.moveToFirst()) { final int resultCol = c.getColumnIndex(HistoryEntry._RESULT); setCurrentEntry((c.isNull(resultCol) ? "" : (c.getDouble(resultCol) + " ")) + c.getString(c.getColumnIndex(HistoryEntry._WANT)), ""); setHistoryVisible(false); } c.close(); } break; } return super.onContextItemSelected(item); }
From source file:nl.privacybarometer.privacyvandaag.service.FetcherService.java
private void mobilizeAllEntries() { ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(TaskColumns.CONTENT_URI, new String[] { TaskColumns._ID, TaskColumns.ENTRY_ID, TaskColumns.NUMBER_ATTEMPT }, TaskColumns.IMG_URL_TO_DL + Constants.DB_IS_NULL, null, null); ArrayList<ContentProviderOperation> operations = new ArrayList<>(); while (cursor != null && cursor.moveToNext()) { long taskId = cursor.getLong(0); long entryId = cursor.getLong(1); int nbAttempt = 0; if (!cursor.isNull(2)) { nbAttempt = cursor.getInt(2); }/*from w ww. ja v a 2s. c om*/ boolean success = false; Uri entryUri = EntryColumns.CONTENT_URI(entryId); Cursor entryCursor = cr.query(entryUri, null, null, null, null); if (entryCursor != null && entryCursor.moveToFirst()) { if (entryCursor.isNull(entryCursor.getColumnIndex(EntryColumns.MOBILIZED_HTML))) { // If we didn't already mobilized it int linkPos = entryCursor.getColumnIndex(EntryColumns.LINK); int abstractHtmlPos = entryCursor.getColumnIndex(EntryColumns.ABSTRACT); int feedIdPos = entryCursor.getColumnIndex(EntryColumns.FEED_ID); HttpURLConnection connection = null; try { String link = entryCursor.getString(linkPos); String feedId = entryCursor.getString(feedIdPos); Cursor cursorFeed = cr.query(FeedColumns.CONTENT_URI(feedId), null, null, null, null); cursorFeed.moveToNext(); int cookieNamePosition = cursorFeed.getColumnIndex(FeedColumns.COOKIE_NAME); int cookieValuePosition = cursorFeed.getColumnIndex(FeedColumns.COOKIE_VALUE); String cookieName = cursorFeed.getString(cookieNamePosition); String cookieValue = cursorFeed.getString(cookieValuePosition); cursorFeed.close(); // Take substring from RSS content and use it // to try to find a text indicator for better content extraction String contentIndicator = null; String text = entryCursor.getString(abstractHtmlPos); if (!TextUtils.isEmpty(text)) { text = Html.fromHtml(text).toString(); if (text.length() > 60) { contentIndicator = text.substring(20, 40); } } connection = NetworkUtils.setupConnection(link, cookieName, cookieValue); String mobilizedHtml = ArticleTextExtractor.extractContent(connection.getInputStream(), contentIndicator); if (mobilizedHtml != null) { mobilizedHtml = HtmlUtils.improveHtmlContent(mobilizedHtml, NetworkUtils.getBaseUrl(link)); ContentValues values = new ContentValues(); values.put(EntryColumns.MOBILIZED_HTML, mobilizedHtml); ArrayList<String> imgUrlsToDownload = null; if (NetworkUtils.needDownloadPictures()) { imgUrlsToDownload = HtmlUtils.getImageURLs(mobilizedHtml); } String mainImgUrl; if (imgUrlsToDownload != null) { mainImgUrl = HtmlUtils.getMainImageURL(imgUrlsToDownload); } else { mainImgUrl = HtmlUtils.getMainImageURL(mobilizedHtml); } if (mainImgUrl != null) { values.put(EntryColumns.IMAGE_URL, mainImgUrl); } if (cr.update(entryUri, values, null, null) > 0) { success = true; operations.add(ContentProviderOperation.newDelete(TaskColumns.CONTENT_URI(taskId)) .build()); if (imgUrlsToDownload != null && !imgUrlsToDownload.isEmpty()) { addImagesToDownload(String.valueOf(entryId), imgUrlsToDownload); } } } } catch (Throwable ignored) { if (connection != null) { connection.disconnect(); } } finally { if (connection != null) { connection.disconnect(); } } } else { // We already mobilized it success = true; operations.add(ContentProviderOperation.newDelete(TaskColumns.CONTENT_URI(taskId)).build()); } } if (entryCursor != null) entryCursor.close(); if (!success) { if (nbAttempt + 1 > MAX_TASK_ATTEMPT) { operations.add(ContentProviderOperation.newDelete(TaskColumns.CONTENT_URI(taskId)).build()); } else { ContentValues values = new ContentValues(); values.put(TaskColumns.NUMBER_ATTEMPT, nbAttempt + 1); operations.add(ContentProviderOperation.newUpdate(TaskColumns.CONTENT_URI(taskId)) .withValues(values).build()); } } } if (cursor != null) cursor.close(); if (!operations.isEmpty()) { try { cr.applyBatch(FeedData.AUTHORITY, operations); } catch (Throwable ignored) { } } }
From source file:org.odk.collect.android.logic.FormRelationsManager.java
/** * Adds to InstanceProvider using information from the form definition. * * After an instance is created and written to disk, it must be added to * the InstanceProvider. That happens here. This is mostly copypasta from * the private method inside the SaveToDiskTask. * * @param formUri The Uri for the form template for the instance. * @param instancePath The path to disk where the instance has been saved. * @return Returns the Uri of the record that was created in the * InstanceProvider./*from w w w. j a v a 2s. c o m*/ */ private static Uri updateInstanceDatabase(Uri formUri, String instancePath) { ContentValues values = new ContentValues(); values.put(InstanceColumns.STATUS, InstanceProviderAPI.STATUS_INCOMPLETE); values.put(InstanceColumns.CAN_EDIT_WHEN_COMPLETE, Boolean.toString(true)); // Entry didn't exist, so create it. Cursor c = null; try { // retrieve the form definition... c = Collect.getInstance().getContentResolver().query(formUri, null, null, null, null); c.moveToFirst(); String jrformid = c.getString(c.getColumnIndex(FormsColumns.JR_FORM_ID)); String jrversion = c.getString(c.getColumnIndex(FormsColumns.JR_VERSION)); String formname = c.getString(c.getColumnIndex(FormsColumns.DISPLAY_NAME)); String submissionUri = null; if (!c.isNull(c.getColumnIndex(FormsColumns.SUBMISSION_URI))) { submissionUri = c.getString(c.getColumnIndex(FormsColumns.SUBMISSION_URI)); } // add missing fields into values values.put(InstanceColumns.INSTANCE_FILE_PATH, instancePath); values.put(InstanceColumns.SUBMISSION_URI, submissionUri); values.put(InstanceColumns.DISPLAY_NAME, formname); values.put(InstanceColumns.JR_FORM_ID, jrformid); values.put(InstanceColumns.JR_VERSION, jrversion); } finally { if (c != null) { c.close(); } } Uri insertedInstance = Collect.getInstance().getContentResolver().insert(InstanceColumns.CONTENT_URI, values); if (LOCAL_LOG) { Log.d(TAG, "Successfully placed instance \'" + insertedInstance.toString() + "\' into InstanceProvider"); } return insertedInstance; }
From source file:ch.ethz.twimight.net.opportunistic.ScanningService.java
/** * Creates a JSON Object from a direct message * /*from w w w .j a v a2s . c o m*/ * @param c * @return * @throws JSONException */ private JSONObject getDmJSON(Cursor c) throws JSONException { JSONObject o = new JSONObject(); if (c.getColumnIndex(DirectMessages.COL_RECEIVER) < 0 || c.getColumnIndex(DirectMessages.COL_SENDER) < 0 || c.isNull(c.getColumnIndex(DirectMessages.COL_CRYPTEXT))) { Log.i(TAG, "missing users data"); return null; } else { o.put(TYPE, MESSAGE_TYPE_DM); o.put(DirectMessages.COL_DISASTERID, c.getLong(c.getColumnIndex(DirectMessages.COL_DISASTERID))); o.put(DirectMessages.COL_CRYPTEXT, c.getString(c.getColumnIndex(DirectMessages.COL_CRYPTEXT))); o.put(DirectMessages.COL_SENDER, c.getString(c.getColumnIndex(DirectMessages.COL_SENDER))); if (c.getColumnIndex(DirectMessages.COL_CREATED) >= 0) o.put(DirectMessages.COL_CREATED, c.getLong(c.getColumnIndex(DirectMessages.COL_CREATED))); o.put(DirectMessages.COL_RECEIVER, c.getLong(c.getColumnIndex(DirectMessages.COL_RECEIVER))); o.put(DirectMessages.COL_RECEIVER_SCREENNAME, c.getString(c.getColumnIndex(DirectMessages.COL_RECEIVER_SCREENNAME))); o.put(DirectMessages.COL_DISASTERID, c.getLong(c.getColumnIndex(DirectMessages.COL_DISASTERID))); o.put(DirectMessages.COL_SIGNATURE, c.getString(c.getColumnIndex(DirectMessages.COL_SIGNATURE))); o.put(DirectMessages.COL_CERTIFICATE, c.getString(c.getColumnIndex(DirectMessages.COL_CERTIFICATE))); return o; } }