List of usage examples for android.database MergeCursor MergeCursor
public MergeCursor(Cursor[] cursors)
From source file:org.thoughtcrime.securesms.contacts.ContactAccessor.java
/*** * If the code below looks shitty to you, that's because it was taken * directly from the Android source, where shitty code is all you get. *//*from ww w . j a v a 2s .c o m*/ public Cursor getCursorForRecipientFilter(CharSequence constraint, ContentResolver mContentResolver) { final String SORT_ORDER = Contacts.TIMES_CONTACTED + " DESC," + Contacts.DISPLAY_NAME + "," + Phone.TYPE; final String[] PROJECTION_PHONE = { Phone._ID, // 0 Phone.CONTACT_ID, // 1 Phone.TYPE, // 2 Phone.NUMBER, // 3 Phone.LABEL, // 4 Phone.DISPLAY_NAME, // 5 }; String phone = ""; String cons = null; if (constraint != null) { cons = constraint.toString(); if (RecipientsAdapter.usefulAsDigits(cons)) { phone = PhoneNumberUtils.convertKeypadLettersToDigits(cons); if (phone.equals(cons) && !PhoneNumberUtils.isWellFormedSmsAddress(phone)) { phone = ""; } else { phone = phone.trim(); } } } Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(cons)); String selection = String.format("%s=%s OR %s=%s OR %s=%s", Phone.TYPE, Phone.TYPE_MOBILE, Phone.TYPE, Phone.TYPE_WORK_MOBILE, Phone.TYPE, Phone.TYPE_MMS); Cursor phoneCursor = mContentResolver.query(uri, PROJECTION_PHONE, null, null, SORT_ORDER); if (phone.length() > 0) { ArrayList result = new ArrayList(); result.add(Integer.valueOf(-1)); // ID result.add(Long.valueOf(-1)); // CONTACT_ID result.add(Integer.valueOf(Phone.TYPE_CUSTOM)); // TYPE result.add(phone); // NUMBER /* * The "\u00A0" keeps Phone.getDisplayLabel() from deciding * to display the default label ("Home") next to the transformation * of the letters into numbers. */ result.add("\u00A0"); // LABEL result.add(cons); // NAME ArrayList<ArrayList> wrap = new ArrayList<ArrayList>(); wrap.add(result); ArrayListCursor translated = new ArrayListCursor(PROJECTION_PHONE, wrap); return new MergeCursor(new Cursor[] { translated, phoneCursor }); } else { return phoneCursor; } }
From source file:com.securecomcode.text.contacts.ContactAccessor.java
/*** * If the code below looks shitty to you, that's because it was taken * directly from the Android source, where shitty code is all you get. *///from ww w .j a v a 2 s . c o m public Cursor getCursorForRecipientFilter(CharSequence constraint, ContentResolver mContentResolver) { final String SORT_ORDER = Contacts.TIMES_CONTACTED + " DESC," + Contacts.DISPLAY_NAME + "," + Contacts.Data.IS_SUPER_PRIMARY + " DESC," + Phone.TYPE; final String[] PROJECTION_PHONE = { Phone._ID, // 0 Phone.CONTACT_ID, // 1 Phone.TYPE, // 2 Phone.NUMBER, // 3 Phone.LABEL, // 4 Phone.DISPLAY_NAME, // 5 }; String phone = ""; String cons = null; if (constraint != null) { cons = constraint.toString(); if (RecipientsAdapter.usefulAsDigits(cons)) { phone = PhoneNumberUtils.convertKeypadLettersToDigits(cons); if (phone.equals(cons) && !PhoneNumberUtils.isWellFormedSmsAddress(phone)) { phone = ""; } else { phone = phone.trim(); } } } Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(cons)); String selection = String.format("%s=%s OR %s=%s OR %s=%s", Phone.TYPE, Phone.TYPE_MOBILE, Phone.TYPE, Phone.TYPE_WORK_MOBILE, Phone.TYPE, Phone.TYPE_MMS); Cursor phoneCursor = mContentResolver.query(uri, PROJECTION_PHONE, null, null, SORT_ORDER); if (phone.length() > 0) { ArrayList result = new ArrayList(); result.add(Integer.valueOf(-1)); // ID result.add(Long.valueOf(-1)); // CONTACT_ID result.add(Integer.valueOf(Phone.TYPE_CUSTOM)); // TYPE result.add(phone); // NUMBER /* * The "\u00A0" keeps Phone.getDisplayLabel() from deciding * to display the default label ("Home") next to the transformation * of the letters into numbers. */ result.add("\u00A0"); // LABEL result.add(cons); // NAME ArrayList<ArrayList> wrap = new ArrayList<ArrayList>(); wrap.add(result); ArrayListCursor translated = new ArrayListCursor(PROJECTION_PHONE, wrap); return new MergeCursor(new Cursor[] { translated, phoneCursor }); } else { return phoneCursor; } }
From source file:com.android.music.PlaylistBrowserFragment.java
private Cursor mergedCursor(Cursor c) { if (c == null) { return null; }/*from w w w .ja v a 2 s .co m*/ if (c instanceof MergeCursor) { // this shouldn't happen, but fail gracefully Log.d("PlaylistBrowserActivity", "Already wrapped"); return c; } MatrixCursor autoplaylistscursor = new MatrixCursor(mCols); if (mCreateShortcut) { ArrayList<Object> all = new ArrayList<Object>(2); all.add(ALL_SONGS_PLAYLIST); all.add(getString(R.string.play_all)); autoplaylistscursor.addRow(all); } ArrayList<Object> recent = new ArrayList<Object>(2); recent.add(RECENTLY_ADDED_PLAYLIST); recent.add(getString(R.string.recentlyadded)); autoplaylistscursor.addRow(recent); // check if there are any podcasts Cursor counter = MusicUtils.query(getActivity(), MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] { "count(*)" }, "is_podcast=1", null, null); if (counter != null) { counter.moveToFirst(); int numpodcasts = counter.getInt(0); counter.close(); if (numpodcasts > 0) { ArrayList<Object> podcasts = new ArrayList<Object>(2); podcasts.add(PODCASTS_PLAYLIST); podcasts.add(getString(R.string.podcasts_listitem)); autoplaylistscursor.addRow(podcasts); } } Cursor cc = new MergeCursor(new Cursor[] { autoplaylistscursor, c }); return cc; }
From source file:com.concentricsky.android.khanacademy.app.ManageDownloadsActivity.java
private Cursor getDisplayOptionsCursor(SQLiteOpenHelper helper) { SQLiteDatabase db = helper.getReadableDatabase(); String sql = "select distinct topic._id as _id, topic.title as title from topic, topicvideo, video where video.download_status>? and topicvideo.video_id=video.readable_id and topicvideo.topic_id=topic._id group by title"; String[] selectionArgs = { String.valueOf(Video.DL_STATUS_NOT_STARTED) }; Cursor mainCursor = db.rawQuery(sql, selectionArgs); sql = "select '-1' as _id, 'All Videos' as title"; Cursor headerCursor = db.rawQuery(sql, null); MergeCursor cursor = new MergeCursor(new Cursor[] { headerCursor, mainCursor }); return cursor; }
From source file:com.adityarathi.muo.utils.Common.java
/** * Download Manager implementation for pinning songs. */// w w w. ja v a2s.c o m public void queueSongsToPin(boolean getAllPinnedSongs, boolean pinPlaylist, String selection) { //If the current cursor is empty or null, retrieve the cursor using the selection parameter. if (mPinnedSongsCursor == null || mPinnedSongsCursor.getCount() <= 0) { if (getAllPinnedSongs == true) { mPinnedSongsCursor = null; mIsFetchingPinnedSongs = true; Toast.makeText(mContext, R.string.getting_pinned_songs, Toast.LENGTH_LONG).show(); } else if (pinPlaylist == true) { //Pinning from a playlist, so we'll need to use a db call that utilizes a JOIN. mPinnedSongsCursor = mDBAccessHelper.getAllSongsInPlaylistSearchable(selection); } else { //Check if we're pinning a smart playlist. if (selection.equals("TOP_25_PLAYED_SONGS")) { mPinnedSongsCursor = mDBAccessHelper.getTop25PlayedTracks(selection); } else if (selection.equals("RECENTLY_ADDED")) { mPinnedSongsCursor = mDBAccessHelper.getRecentlyAddedSongs(selection); } else if (selection.equals("TOP_RATED")) { mPinnedSongsCursor = mDBAccessHelper.getTopRatedSongs(selection); } else if (selection.equals("RECENTLY_PLAYED")) { mPinnedSongsCursor = mDBAccessHelper.getRecentlyPlayedSongs(selection); } else { //Not playing from a smart playlist. Just use a regular db query that searches songs. mPinnedSongsCursor = mDBAccessHelper.getAllSongsSearchable(selection); } } //Intent intent = new Intent(this, PinGMusicSongsService.class); //startService(intent); } else { //mPinnedSongsCursor already has songs queued, so append a new intermCursor; Cursor intermCursor = null; if (getAllPinnedSongs == true) { Toast.makeText(mContext, R.string.wait_until_pinning_complete, Toast.LENGTH_SHORT).show(); return; } else if (pinPlaylist == true) { //Pinning from a playlist, so we'll need to use a db call that utilizes a JOIN. intermCursor = mDBAccessHelper.getAllSongsInPlaylistSearchable(selection); } else { //Check if we're pinning a smart playlist. if (selection.equals("TOP_25_PLAYED_SONGS")) { intermCursor = mDBAccessHelper.getTop25PlayedTracks(selection); } else if (selection.equals("RECENTLY_ADDED")) { intermCursor = mDBAccessHelper.getRecentlyAddedSongs(selection); } else if (selection.equals("TOP_RATED")) { intermCursor = mDBAccessHelper.getTopRatedSongs(selection); } else if (selection.equals("RECENTLY_PLAYED")) { intermCursor = mDBAccessHelper.getRecentlyPlayedSongs(selection); } else { //Not playing from a smart playlist. Just use a regular db query that searches songs. intermCursor = mDBAccessHelper.getAllSongsSearchable(selection); } } Cursor[] cursorArray = { mPinnedSongsCursor, intermCursor }; MergeCursor mergeCursor = new MergeCursor(cursorArray); mPinnedSongsCursor = (Cursor) mergeCursor; } }
From source file:com.aniruddhc.acemusic.player.Utils.Common.java
/** * Download Manager implementation for pinning songs. *///ww w . ja v a 2 s. c o m public void queueSongsToPin(boolean getAllPinnedSongs, boolean pinPlaylist, String selection) { //If the current cursor is empty or null, retrieve the cursor using the selection parameter. if (mPinnedSongsCursor == null || mPinnedSongsCursor.getCount() <= 0) { if (getAllPinnedSongs == true) { mPinnedSongsCursor = null; mIsFetchingPinnedSongs = true; Toast.makeText(mContext, R.string.getting_pinned_songs, Toast.LENGTH_LONG).show(); } else if (pinPlaylist == true) { //Pinning from a playlist, so we'll need to use a db call that utilizes a JOIN. mPinnedSongsCursor = mDBAccessHelper.getAllSongsInPlaylistSearchable(selection); } else { //Check if we're pinning a smart playlist. if (selection.equals("TOP_25_PLAYED_SONGS")) { mPinnedSongsCursor = mDBAccessHelper.getTop25PlayedTracks(selection); } else if (selection.equals("RECENTLY_ADDED")) { mPinnedSongsCursor = mDBAccessHelper.getRecentlyAddedSongs(selection); } else if (selection.equals("TOP_RATED")) { mPinnedSongsCursor = mDBAccessHelper.getTopRatedSongs(selection); } else if (selection.equals("RECENTLY_PLAYED")) { mPinnedSongsCursor = mDBAccessHelper.getRecentlyPlayedSongs(selection); } else { //Not playing from a smart playlist. Just use a regular db query that searches songs. mPinnedSongsCursor = mDBAccessHelper.getAllSongsSearchable(selection); } } Intent intent = new Intent(this, PinGMusicSongsService.class); startService(intent); } else { //mPinnedSongsCursor already has songs queued, so append a new intermCursor; Cursor intermCursor = null; if (getAllPinnedSongs == true) { Toast.makeText(mContext, R.string.wait_until_pinning_complete, Toast.LENGTH_SHORT).show(); return; } else if (pinPlaylist == true) { //Pinning from a playlist, so we'll need to use a db call that utilizes a JOIN. intermCursor = mDBAccessHelper.getAllSongsInPlaylistSearchable(selection); } else { //Check if we're pinning a smart playlist. if (selection.equals("TOP_25_PLAYED_SONGS")) { intermCursor = mDBAccessHelper.getTop25PlayedTracks(selection); } else if (selection.equals("RECENTLY_ADDED")) { intermCursor = mDBAccessHelper.getRecentlyAddedSongs(selection); } else if (selection.equals("TOP_RATED")) { intermCursor = mDBAccessHelper.getTopRatedSongs(selection); } else if (selection.equals("RECENTLY_PLAYED")) { intermCursor = mDBAccessHelper.getRecentlyPlayedSongs(selection); } else { //Not playing from a smart playlist. Just use a regular db query that searches songs. intermCursor = mDBAccessHelper.getAllSongsSearchable(selection); } } Cursor[] cursorArray = { mPinnedSongsCursor, intermCursor }; MergeCursor mergeCursor = new MergeCursor(cursorArray); mPinnedSongsCursor = (Cursor) mergeCursor; } }
From source file:com.android.mtkex.chips.BaseRecipientAdapter.java
private Cursor doQuery(CharSequence constraint, int limit, Long directoryId) { final Uri.Builder builder = mQuery.getContentFilterUri().buildUpon().appendPath(constraint.toString()) .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY, String.valueOf(limit + ALLOWANCE_FOR_DUPLICATES)); if (directoryId != null) { builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(directoryId)); }/*w w w . j a v a 2s.co m*/ if (mAccount != null) { builder.appendQueryParameter(PRIMARY_ACCOUNT_NAME, mAccount.name); builder.appendQueryParameter(PRIMARY_ACCOUNT_TYPE, mAccount.type); } final long start = System.currentTimeMillis(); Cursor cursor = mContentResolver.query(/// M: Cursor might be modified later builder.build(), mQuery.getProjection(), null, null, Contacts.DISPLAY_NAME); final long end = System.currentTimeMillis(); Log.d(TAG, "[doQuery] 1st query, constraint: " + constraint + ", result count: " + (cursor != null ? cursor.getCount() : "null")); /// M: MTK debug log if (DEBUG) { Log.d(TAG, "Time for autocomplete (query: " + constraint + ", directoryId: " + directoryId + ", num_of_results: " + (cursor != null ? cursor.getCount() : "null") + "): " + (end - start) + " ms"); } /// M: Show phone and email when filtering in phoneQuery. @{ if ((mQueryType == QUERY_TYPE_PHONE) && mShowPhoneAndEmail) { /// M: Save number of current phone query results for checking whether the query result is from phone or email mQueryPhoneNum = cursor != null ? cursor.getCount() : 0; Queries.Query currentQuery = Queries.EMAIL; final Uri.Builder builder2 = currentQuery.getContentFilterUri().buildUpon() .appendPath(constraint.toString()).appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY, String.valueOf(limit + ALLOWANCE_FOR_DUPLICATES)); if (directoryId != null) { builder2.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(directoryId)); } if (mAccount != null) { builder2.appendQueryParameter(PRIMARY_ACCOUNT_NAME, mAccount.name); builder2.appendQueryParameter(PRIMARY_ACCOUNT_TYPE, mAccount.type); } Cursor cursor2 = mContentResolver.query(builder2.build(), currentQuery.getProjection(), null, null, Contacts.DISPLAY_NAME); Log.d(TAG, "[doQuery] 2nd query, constraint: " + constraint + ", result count: " + (cursor2 != null ? cursor2.getCount() : "null")); /// M: MTK debug log Cursor[] cursors = new Cursor[2]; cursors[0] = cursor; cursors[1] = cursor2; cursor = new MergeCursor(cursors); } /// @} return cursor; }
From source file:com.adityarathi.muo.services.AudioPlaybackService.java
/** * This method combines mCursor with the specified newCursor. * /* w w w. ja va2 s .c om*/ * @param newCursor The new cursor to append to mCursor. * @param playNext Pass true if newCursor should be appeneded after the current song. */ public void enqueueCursor(Cursor newCursor, boolean playNext) { Cursor[] cursorArray = { getCursor(), newCursor }; mMergeCursor = new MergeCursor(cursorArray); setCursor(mMergeCursor); getCursor().moveToPosition(mPlaybackIndecesList.get(mCurrentSongIndex)); mEnqueuePerformed = true; if (playNext) { //Check which mMediaPlayer is currently playing, and prepare the other mediaPlayer. prepareAlternateMediaPlayer(); } }
From source file:org.totschnig.myexpenses.activity.ExpenseEdit.java
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { if (data == null) { return;//from w w w. j ava2 s . co m } int id = loader.getId(); switch (id) { case METHODS_CURSOR: mMethodsCursor = data; View methodContainer = findViewById(R.id.MethodRow); if (mMethodsAdapter == null || !data.moveToFirst()) { methodContainer.setVisibility(View.GONE); } else { methodContainer.setVisibility(View.VISIBLE); MatrixCursor extras = new MatrixCursor(new String[] { KEY_ROWID, KEY_LABEL, KEY_IS_NUMBERED }); extras.addRow(new String[] { "0", "- - - -", "0" }); mMethodsAdapter.swapCursor(new MergeCursor(new Cursor[] { extras, data })); if (mSavedInstance) { mTransaction.methodId = mMethodId; } if (mTransaction.methodId != null) { while (data.isAfterLast() == false) { if (data.getLong(data.getColumnIndex(KEY_ROWID)) == mTransaction.methodId) { mMethodSpinner.setSelection(data.getPosition() + 1); break; } data.moveToNext(); } } else { mMethodSpinner.setSelection(0); } } break; case ACCOUNTS_CURSOR: mAccountsAdapter.swapCursor(data); mAccounts = new Account[data.getCount()]; if (mSavedInstance) { mTransaction.accountId = mAccountId; mTransaction.transfer_account = mTransferAccountId; } data.moveToFirst(); boolean selectionSet = false; String currencyExtra = getIntent().getStringExtra(KEY_CURRENCY); while (data.isAfterLast() == false) { int position = data.getPosition(); Account a = Account.fromCacheOrFromCursor(data); mAccounts[position] = a; if (!selectionSet && (a.currency.getCurrencyCode().equals(currencyExtra) || (currencyExtra == null && a.getId().equals(mTransaction.accountId)))) { mAccountSpinner.setSelection(position); setAccountLabel(a); selectionSet = true; } data.moveToNext(); } //if the accountId we have been passed does not exist, we select the first entry if (mAccountSpinner.getSelectedItemPosition() == android.widget.AdapterView.INVALID_POSITION) { mAccountSpinner.setSelection(0); mTransaction.accountId = mAccounts[0].getId(); setAccountLabel(mAccounts[0]); } if (mOperationType == MyExpenses.TYPE_TRANSFER) { mTransferAccountCursor = new FilterCursorWrapper(data); int selectedPosition = setTransferAccountFilterMap(); mTransferAccountsAdapter.swapCursor(mTransferAccountCursor); mTransferAccountSpinner.setSelection(selectedPosition); mTransaction.transfer_account = mTransferAccountSpinner.getSelectedItemId(); configureTransferInput(); if (!mNewInstance && !(mTransaction instanceof Template)) { isProcessingLinkedAmountInputs = true; mTransferAmountText.setAmount(mTransaction.getTransferAmount().getAmountMajor().abs()); updateExchangeRates(); isProcessingLinkedAmountInputs = false; } } else { //the methods cursor is based on the current account, //hence it is loaded only after the accounts cursor is loaded if (!(mTransaction instanceof SplitPartCategory)) { mManager.initLoader(METHODS_CURSOR, null, this); } } mTypeButton.setEnabled(true); configureType(); configureStatusSpinner(); if (mIsResumed) setupListeners(); break; case LAST_EXCHANGE_CURSOR: if (data.moveToFirst()) { final Currency currency1 = getCurrentAccount().currency; final Currency currency2 = Account .getInstanceFromDb(mTransferAccountSpinner.getSelectedItemId()).currency; if (currency1.getCurrencyCode().equals(data.getString(0)) && currency2.getCurrencyCode().equals(data.getString(1))) { BigDecimal amount = new Money(currency1, data.getLong(2)).getAmountMajor(); BigDecimal transferAmount = new Money(currency2, data.getLong(3)).getAmountMajor(); BigDecimal exchangeRate = amount.compareTo(nullValue) != 0 ? transferAmount.divide(amount, EXCHANGE_RATE_FRACTION_DIGITS, RoundingMode.DOWN) : nullValue; if (exchangeRate.compareTo(nullValue) != 0) { mExchangeRate1Text.setAmount(exchangeRate); } } } } }