Example usage for android.database Cursor getColumnIndexOrThrow

List of usage examples for android.database Cursor getColumnIndexOrThrow

Introduction

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

Prototype

int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException;

Source Link

Document

Returns the zero-based index for the given column name, or throws IllegalArgumentException if the column doesn't exist.

Usage

From source file:de.vanita5.twittnuker.util.Utils.java

public static String getImagePathFromUri(final Context context, final Uri uri) {
    if (context == null || uri == null)
        return null;

    final String mediaUriStart = ParseUtils.parseString(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    if (ParseUtils.parseString(uri).startsWith(mediaUriStart)) {

        final String[] proj = { MediaStore.Images.Media.DATA };
        final Cursor cur = ContentResolverUtils.query(context.getContentResolver(), uri, proj, null, null,
                null);/*from   w  w  w.ja va 2s .com*/

        if (cur == null)
            return null;

        final int idxData = cur.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cur.moveToFirst();
        try {
            return cur.getString(idxData);
        } finally {
            cur.close();
        }
    } else {
        final String path = uri.getPath();
        if (path != null && new File(path).exists())
            return path;
    }
    return null;
}

From source file:de.vanita5.twittnuker.util.Utils.java

public static long[] getOldestStatusIdsFromDatabase(final Context context, final Uri uri,
        final long[] account_ids) {
    if (context == null || uri == null || account_ids == null)
        return null;
    final String[] cols = new String[] { Statuses.STATUS_ID };
    final ContentResolver resolver = context.getContentResolver();
    final long[] status_ids = new long[account_ids.length];
    int idx = 0;/*from   w  w  w . j a v  a 2s. com*/
    for (final long account_id : account_ids) {
        final String where = Statuses.ACCOUNT_ID + " = " + account_id;
        final Cursor cur = ContentResolverUtils.query(resolver, uri, cols, where, null, Statuses.STATUS_ID);
        if (cur == null) {
            continue;
        }

        if (cur.getCount() > 0) {
            cur.moveToFirst();
            status_ids[idx] = cur.getLong(cur.getColumnIndexOrThrow(Statuses.STATUS_ID));
        }
        cur.close();
        idx++;
    }
    return status_ids;
}

From source file:com.dwdesign.tweetings.util.Utils.java

public static Twitter getTwitterInstance(final Context context, final long account_id,
        final boolean include_entities, final boolean include_rts, final boolean use_httpclient) {
    if (context == null)
        return null;
    final SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
            Context.MODE_PRIVATE);
    final boolean enable_gzip_compressing = preferences != null
            ? preferences.getBoolean(PREFERENCE_KEY_GZIP_COMPRESSING, true)
            : true;//w  w w. j a  va2 s  .c  o  m
    final boolean ignore_ssl_error = preferences != null
            ? preferences.getBoolean(PREFERENCE_KEY_IGNORE_SSL_ERROR, false)
            : false;
    final boolean enable_proxy = preferences != null
            ? preferences.getBoolean(PREFERENCE_KEY_ENABLE_PROXY, false)
            : false;
    final String consumer_key = preferences != null
            ? preferences.getString(PREFERENCE_KEY_CONSUMER_KEY, CONSUMER_KEY)
            : CONSUMER_KEY;
    final String consumer_secret = preferences != null
            ? preferences.getString(PREFERENCE_KEY_CONSUMER_SECRET, CONSUMER_SECRET)
            : CONSUMER_SECRET;

    Twitter twitter = null;
    final StringBuilder where = new StringBuilder();
    where.append(Accounts.USER_ID + "=" + account_id);
    final Cursor cur = context.getContentResolver().query(Accounts.CONTENT_URI, Accounts.COLUMNS,
            where.toString(), null, null);
    if (cur != null) {
        if (cur.getCount() == 1) {
            cur.moveToFirst();
            final ConfigurationBuilder cb = new ConfigurationBuilder();
            setUserAgent(context, cb);
            if (use_httpclient) {
                cb.setHttpClientImplementation(HttpClientImpl.class);
            }
            cb.setGZIPEnabled(enable_gzip_compressing);
            if (enable_proxy) {
                final String proxy_host = preferences.getString(PREFERENCE_KEY_PROXY_HOST, null);
                final int proxy_port = parseInt(preferences.getString(PREFERENCE_KEY_PROXY_PORT, "-1"));
                if (!isNullOrEmpty(proxy_host) && proxy_port > 0) {
                    cb.setHttpProxyHost(proxy_host);
                    cb.setHttpProxyPort(proxy_port);
                }

            }
            final String rest_base_url = cur.getString(cur.getColumnIndexOrThrow(Accounts.REST_BASE_URL));
            final String signing_rest_base_url = cur
                    .getString(cur.getColumnIndexOrThrow(Accounts.SIGNING_REST_BASE_URL));
            final String search_base_url = cur.getString(cur.getColumnIndexOrThrow(Accounts.SEARCH_BASE_URL));
            final String upload_base_url = cur.getString(cur.getColumnIndexOrThrow(Accounts.UPLOAD_BASE_URL));
            final String oauth_base_url = cur.getString(cur.getColumnIndexOrThrow(Accounts.OAUTH_BASE_URL));
            final String signing_oauth_base_url = cur
                    .getString(cur.getColumnIndexOrThrow(Accounts.SIGNING_OAUTH_BASE_URL));
            //if (!isNullOrEmpty(rest_base_url)) {
            cb.setRestBaseURL(DEFAULT_REST_BASE_URL);
            //}
            //if (!isNullOrEmpty(search_base_url)) {
            cb.setSearchBaseURL(DEFAULT_SEARCH_BASE_URL);
            //}
            cb.setIncludeEntitiesEnabled(include_entities);
            cb.setIncludeRTsEnabled(include_rts);

            switch (cur.getInt(cur.getColumnIndexOrThrow(Accounts.AUTH_TYPE))) {
            case Accounts.AUTH_TYPE_OAUTH:
            case Accounts.AUTH_TYPE_XAUTH:
                if (isNullOrEmpty(consumer_key) || isNullOrEmpty(consumer_secret)) {
                    cb.setOAuthConsumerKey(CONSUMER_KEY);
                    cb.setOAuthConsumerSecret(CONSUMER_SECRET);
                } else {
                    cb.setOAuthConsumerKey(consumer_key);
                    cb.setOAuthConsumerSecret(consumer_secret);
                }
                twitter = new TwitterFactory(cb.build()).getInstance(
                        new AccessToken(cur.getString(cur.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN)),
                                cur.getString(cur.getColumnIndexOrThrow(Accounts.TOKEN_SECRET))));
                break;
            case Accounts.AUTH_TYPE_BASIC:
                twitter = new TwitterFactory(cb.build()).getInstance(
                        new BasicAuthorization(cur.getString(cur.getColumnIndexOrThrow(Accounts.USERNAME)),
                                cur.getString(cur.getColumnIndexOrThrow(Accounts.BASIC_AUTH_PASSWORD))));
                break;
            default:
            }
        }
        cur.close();
    }
    return twitter;
}

From source file:de.vanita5.twittnuker.util.Utils.java

public static long[] getOldestMessageIdsFromDatabase(final Context context, final Uri uri,
        final long[] account_ids) {
    if (context == null || uri == null)
        return null;
    final String[] cols = new String[] { DirectMessages.MESSAGE_ID };
    final ContentResolver resolver = context.getContentResolver();
    final long[] status_ids = new long[account_ids.length];
    int idx = 0;//from   w  ww .j  a va 2s .  com
    for (final long account_id : account_ids) {
        final String where = Statuses.ACCOUNT_ID + " = " + account_id;
        final Cursor cur = ContentResolverUtils.query(resolver, uri, cols, where, null,
                DirectMessages.MESSAGE_ID);
        if (cur == null) {
            continue;
        }

        if (cur.getCount() > 0) {
            cur.moveToFirst();
            status_ids[idx] = cur.getLong(cur.getColumnIndexOrThrow(DirectMessages.MESSAGE_ID));
        }
        cur.close();
        idx++;
    }
    return status_ids;
}

From source file:de.vanita5.twittnuker.util.Utils.java

public static long[] getNewestStatusIdsFromDatabase(final Context context, final Uri uri,
        final long[] account_ids) {
    if (context == null || uri == null || account_ids == null)
        return null;
    final String[] cols = new String[] { Statuses.STATUS_ID };
    final ContentResolver resolver = context.getContentResolver();
    final long[] status_ids = new long[account_ids.length];
    int idx = 0;/* w w  w .j  av a 2 s .c  o  m*/
    for (final long account_id : account_ids) {
        final String where = Statuses.ACCOUNT_ID + " = " + account_id;
        final Cursor cur = ContentResolverUtils.query(resolver, uri, cols, where, null,
                Statuses.DEFAULT_SORT_ORDER);
        if (cur == null) {
            continue;
        }

        if (cur.getCount() > 0) {
            cur.moveToFirst();
            status_ids[idx] = cur.getLong(cur.getColumnIndexOrThrow(Statuses.STATUS_ID));
        }
        cur.close();
        idx++;
    }
    return status_ids;
}

From source file:de.vanita5.twittnuker.util.Utils.java

public static long[] getNewestMessageIdsFromDatabase(final Context context, final Uri uri,
        final long[] account_ids) {
    if (context == null || uri == null || account_ids == null)
        return null;
    final String[] cols = new String[] { DirectMessages.MESSAGE_ID };
    final ContentResolver resolver = context.getContentResolver();
    final long[] status_ids = new long[account_ids.length];
    int idx = 0;/*from   w w w . j a v a 2s  .co  m*/
    for (final long account_id : account_ids) {
        final String where = Statuses.ACCOUNT_ID + " = " + account_id;
        final Cursor cur = ContentResolverUtils.query(resolver, uri, cols, where, null,
                DirectMessages.DEFAULT_SORT_ORDER);
        if (cur == null) {
            continue;
        }

        if (cur.getCount() > 0) {
            cur.moveToFirst();
            status_ids[idx] = cur.getLong(cur.getColumnIndexOrThrow(DirectMessages.MESSAGE_ID));
        }
        cur.close();
        idx++;
    }
    return status_ids;
}

From source file:com.av.remusic.service.MediaService.java

public String getGenreName() {
    synchronized (this) {
        if (mCursor == null || mPlayPos < 0 || mPlayPos >= mPlaylist.size()) {
            return null;
        }//from  w ww.  j av  a2s.  c o m
        String[] genreProjection = { MediaStore.Audio.Genres.NAME };
        Uri genreUri = MediaStore.Audio.Genres.getContentUriForAudioId("external",
                (int) mPlaylist.get(mPlayPos).mId);
        Cursor genreCursor = getContentResolver().query(genreUri, genreProjection, null, null, null);
        if (genreCursor != null) {
            try {
                if (genreCursor.moveToFirst()) {
                    return genreCursor
                            .getString(genreCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME));
                }
            } finally {
                genreCursor.close();
            }
        }
        return null;
    }
}

From source file:com.bluros.music.MusicService.java

private void addXTrackSelector(Notification n) {
    if (NotificationHelper.isSupported(n)) {
        StringBuilder selection = new StringBuilder();
        StringBuilder order = new StringBuilder().append("CASE _id \n");
        for (int i = 0; i < mPlaylist.size(); i++) {
            selection.append("_id=").append(mPlaylist.get(i).mId).append(" OR ");
            order.append("WHEN ").append(mPlaylist.get(i).mId).append(" THEN ").append(i).append("\n");
        }/*from ww w.  java 2 s  . co  m*/
        order.append("END");
        Cursor c = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                NOTIFICATION_PROJECTION, selection.substring(0, selection.length() - 3), null,
                order.toString());
        if (c != null && c.getCount() != 0) {
            c.moveToFirst();
            ArrayList<Bundle> list = new ArrayList<>();
            do {
                TrackItem t = new TrackItem()
                        .setArt(ImageLoader.getInstance()
                                .loadImageSync(MusicUtils
                                        .getAlbumArtUri(
                                                c.getLong(c.getColumnIndexOrThrow(AudioColumns.ALBUM_ID)))
                                        .toString()))
                        .setTitle(c.getString(c.getColumnIndexOrThrow(AudioColumns.TITLE)))
                        .setArtist(c.getString(c.getColumnIndexOrThrow(AudioColumns.ARTIST)))
                        .setDuration(MusicUtils.makeShortTimeString(this,
                                c.getInt(c.getColumnIndexOrThrow(AudioColumns.DURATION)) / 1000));
                list.add(t.get());
            } while (c.moveToNext());
            try {
                NotificationHelper.insertToNotification(n, list, this, getQueuePosition());
            } catch (ModNotInstalledException e) {
                e.printStackTrace();
            }
            c.close();
        }
    }
}

From source file:org.getlantern.firetweet.util.Utils.java

public static long[] getNewestMessageIdsFromDatabase(final Context context, final Uri uri,
        final long[] accountIds) {
    if (context == null || uri == null || accountIds == null)
        return null;
    final String[] cols = new String[] { DirectMessages.MESSAGE_ID };
    final ContentResolver resolver = context.getContentResolver();
    final long[] messageIds = new long[accountIds.length];
    int idx = 0;/*from w ww. j  a va 2  s.  com*/
    for (final long accountId : accountIds) {
        final String where = Statuses.ACCOUNT_ID + " = " + accountId;
        final Cursor cur = ContentResolverUtils.query(resolver, uri, cols, where, null,
                DirectMessages.DEFAULT_SORT_ORDER);
        if (cur == null) {
            continue;
        }

        if (cur.getCount() > 0) {
            cur.moveToFirst();
            messageIds[idx] = cur.getLong(cur.getColumnIndexOrThrow(DirectMessages.MESSAGE_ID));
        }
        cur.close();
        idx++;
    }
    return messageIds;
}

From source file:org.getlantern.firetweet.util.Utils.java

public static Authorization getTwitterAuthorization(final Context context, final long accountId) {

    final String where = Expression.equals(new Column(Accounts.ACCOUNT_ID), accountId).getSQL();
    final Cursor c = ContentResolverUtils.query(context.getContentResolver(), Accounts.CONTENT_URI,
            Accounts.COLUMNS, where, null, null);
    if (c == null)
        return null;
    try {/*from  w ww  . j av  a2  s.  com*/
        if (!c.moveToFirst())
            return null;

        switch (c.getInt(c.getColumnIndexOrThrow(Accounts.AUTH_TYPE))) {
        case Accounts.AUTH_TYPE_OAUTH:
        case Accounts.AUTH_TYPE_XAUTH: {
            final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
                    Context.MODE_PRIVATE);
            // Here I use old consumer key/secret because it's default
            // key for older
            // versions
            final String prefConsumerKey = prefs.getString(KEY_CONSUMER_KEY, TWITTER_CONSUMER_KEY);
            final String prefConsumerSecret = prefs.getString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET);
            final ConfigurationBuilder cb = new ConfigurationBuilder();
            final String apiUrlFormat = c.getString(c.getColumnIndex(Accounts.API_URL_FORMAT));
            final String consumerKey = trim(c.getString(c.getColumnIndex(Accounts.CONSUMER_KEY)));
            final String consumerSecret = trim(c.getString(c.getColumnIndex(Accounts.CONSUMER_SECRET)));
            final boolean sameOAuthSigningUrl = c
                    .getInt(c.getColumnIndex(Accounts.SAME_OAUTH_SIGNING_URL)) == 1;
            if (!isEmpty(apiUrlFormat)) {
                cb.setRestBaseURL(getApiUrl(apiUrlFormat, "api", "/1.1/"));
                cb.setOAuthBaseURL(getApiUrl(apiUrlFormat, "api", "/oauth/"));
                cb.setUploadBaseURL(getApiUrl(apiUrlFormat, "upload", "/1.1/"));
                cb.setOAuthAuthorizationURL(getApiUrl(apiUrlFormat, null, null));
                if (!sameOAuthSigningUrl) {
                    cb.setSigningRestBaseURL(DEFAULT_SIGNING_REST_BASE_URL);
                    cb.setSigningOAuthBaseURL(DEFAULT_SIGNING_OAUTH_BASE_URL);
                    cb.setSigningUploadBaseURL(DEFAULT_SIGNING_UPLOAD_BASE_URL);
                }
            }
            if (!isEmpty(consumerKey) && !isEmpty(consumerSecret)) {
                cb.setOAuthConsumerKey(consumerKey);
                cb.setOAuthConsumerSecret(consumerSecret);
            } else if (!isEmpty(prefConsumerKey) && !isEmpty(prefConsumerSecret)) {
                cb.setOAuthConsumerKey(prefConsumerKey);
                cb.setOAuthConsumerSecret(prefConsumerSecret);
            } else {
                cb.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
                cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
            }
            final OAuthAuthorization auth = new OAuthAuthorization(cb.build());
            final String token = c.getString(c.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN));
            final String tokenSecret = c.getString(c.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN_SECRET));
            auth.setOAuthAccessToken(new AccessToken(token, tokenSecret));
            return auth;
        }
        case Accounts.AUTH_TYPE_BASIC: {
            final String screenName = c.getString(c.getColumnIndexOrThrow(Accounts.SCREEN_NAME));
            final String username = c.getString(c.getColumnIndexOrThrow(Accounts.BASIC_AUTH_USERNAME));
            final String loginName = username != null ? username : screenName;
            final String password = c.getString(c.getColumnIndexOrThrow(Accounts.BASIC_AUTH_PASSWORD));
            if (isEmpty(loginName) || isEmpty(password))
                return null;
            return new BasicAuthorization(loginName, password);
        }
        default: {
            return null;
        }
        }
    } finally {
        c.close();
    }
}