Example usage for android.content Context getContentResolver

List of usage examples for android.content Context getContentResolver

Introduction

In this page you can find the example usage for android.content Context getContentResolver.

Prototype

public abstract ContentResolver getContentResolver();

Source Link

Document

Return a ContentResolver instance for your application's package.

Usage

From source file:Main.java

/**
 * Looks up a contacts display name by contact id - if not found, the
 * address (phone number) will be formatted and returned instead.
 *///from   w w  w .  ja va2s.c  o m
public static String getPersonName(Context context, String id, String address) {

    // Check for id, if null return the formatting phone number as the name
    if (id == null || "".equals(id.trim())) {
        if (address != null && !"".equals(address.trim())) {
            return PhoneNumberUtils.formatNumber(address);
        } else {
            return null;
        }
    }

    Cursor cursor = context.getContentResolver().query(Uri.withAppendedPath(Contacts.CONTENT_URI, id),
            new String[] { Contacts.DISPLAY_NAME }, null, null, null);

    if (cursor != null) {
        try {
            if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                String name = cursor.getString(0);
                return name;
            }
        } finally {
            cursor.close();
        }
    }

    if (address != null) {
        return PhoneNumberUtils.formatNumber(address);
    }

    return null;
}

From source file:com.rukman.emde.smsgroups.platform.GMSContactOperations.java

/**
 * When we first add a sync adapter to the system, the contacts from that
 * sync adapter will be hidden unless they're merged/grouped with an existing
 * contact.  But typically we want to actually show those contacts, so we
 * need to mess with the Settings table to get them to show up.
 *
 * @param context the Authenticator Activity context
 * @param account the Account who's visibility we're changing
 * @param visible true if we want the contacts visible, false for hidden
 *///from ww w .ja v  a2s. c  o  m
public static void setAccountContactsVisibility(Context context, Account account, boolean visible) {
    ContentValues values = new ContentValues();
    values.put(RawContacts.ACCOUNT_NAME, account.name);
    values.put(RawContacts.ACCOUNT_TYPE, GMSApplication.ACCOUNT_TYPE);
    values.put(Settings.UNGROUPED_VISIBLE, visible ? 1 : 0);
    context.getContentResolver().insert(Settings.CONTENT_URI, values);
}

From source file:com.csipsimple.backup.SipProfileJson.java

public static JSONArray serializeSipProfiles(Context ctxt) {

    JSONArray jsonSipProfiles = new JSONArray();
    Cursor c = ctxt.getContentResolver().query(SipProfile.ACCOUNT_URI, DBProvider.ACCOUNT_FULL_PROJECTION, null,
            null, null);//from   w  ww.ja va2s  .  c  om
    if (c != null) {
        try {
            while (c.moveToNext()) {
                SipProfile account = new SipProfile(c);
                JSONObject p = serializeSipProfile(ctxt, account);
                try {
                    jsonSipProfiles.put(jsonSipProfiles.length(), p);
                } catch (JSONException e) {
                    Log.e(THIS_FILE, "Impossible to add profile", e);
                }
            }

        } catch (Exception e) {
            Log.e(THIS_FILE, "Error on looping over sip profiles", e);
        } finally {
            c.close();
        }
    }

    // Add negative fake accounts

    Map<String, String> callHandlers = CallHandlerPlugin.getAvailableCallHandlers(ctxt);
    for (String packageName : callHandlers.keySet()) {
        final Long externalAccountId = CallHandlerPlugin.getAccountIdForCallHandler(ctxt, packageName);
        SipProfile gsmProfile = new SipProfile();
        gsmProfile.id = externalAccountId;
        JSONObject p = serializeSipProfile(ctxt, gsmProfile);
        try {
            jsonSipProfiles.put(jsonSipProfiles.length(), p);
        } catch (JSONException e) {
            Log.e(THIS_FILE, "Impossible to add profile", e);
        }
    }

    return jsonSipProfiles;
}

From source file:Main.java

/** Returns a Bitmap from the given URI that may be scaled by an integer factor to reduce its size,
 * while staying as least as large as the width and height parameters.
 *///  ww  w  . j av  a2 s.c  om
public static Bitmap scaledBitmapFromURIWithMinimumSize(Context context, Uri imageURI, int width, int height)
        throws FileNotFoundException {
    BitmapFactory.Options options = computeBitmapSizeFromURI(context, imageURI);
    options.inJustDecodeBounds = false;

    float wratio = 1.0f * options.outWidth / width;
    float hratio = 1.0f * options.outHeight / height;
    options.inSampleSize = (int) Math.min(wratio, hratio);

    return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI), null, options);
}

From source file:Main.java

/**
 * Try to return the absolute file path from the given Uri
 * /*from  w  w  w . jav a 2  s  .  c  o m*/
 * @param context
 * @param uri
 * @return the file path or null
 */
public static String getRealFilePath(final Context context, final Uri uri) {

    if (null == uri)
        return null;

    final String scheme = uri.getScheme();
    String data = null;

    if (scheme == null)
        data = uri.getPath();
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        data = uri.getPath();
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        Cursor cursor = context.getContentResolver().query(uri, new String[] { ImageColumns.DATA }, null, null,
                null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

From source file:com.adjust.sdk.Util.java

protected static String getAttributionId(final Context context) {
    try {/*w  w  w  .ja  v a 2s . c  o  m*/
        final ContentResolver contentResolver = context.getContentResolver();
        final Uri uri = Uri.parse("content://com.facebook.katana.provider.AttributionIdProvider");
        final String columnName = "aid";
        final String[] projection = { columnName };
        final Cursor cursor = contentResolver.query(uri, projection, null, null, null);

        if (null == cursor) {
            return null;
        }
        if (!cursor.moveToFirst()) {
            cursor.close();
            return null;
        }

        final String attributionId = cursor.getString(cursor.getColumnIndex(columnName));
        cursor.close();
        return attributionId;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.example.igorklimov.popularmoviesdemo.helpers.Utility.java

public static boolean isFavorite(Cursor cursor, Context context) {
    String title = getTitle(cursor);
    Cursor query = context.getContentResolver().query(FavoriteMovie.CONTENT_URI, null,
            MovieContract.COLUMN_TITLE + "=?", new String[] { title }, null);
    return query.getCount() != 0;
}

From source file:Main.java

/**
 * Try to return the absolute file path from the given Uri
 *
 * @param context//from  ww  w  .  j av  a  2  s  .  c  o m
 * @param uri
 * @return the file path or null
 */
public static String uri2FilePath(final Context context, final Uri uri) {
    if (null == uri)
        return null;
    final String scheme = uri.getScheme();
    String data = null;
    if (scheme == null)
        data = uri.getPath();
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        data = uri.getPath();
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        Cursor cursor = context.getContentResolver().query(uri,
                new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

From source file:com.example.igorklimov.popularmoviesdemo.helpers.Utility.java

public static int getId(Context context) {
    Uri uri = getContentUri(context);//from  w ww .j a  v a  2 s . com
    Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
    int id = 0;
    if (cursor != null && cursor.moveToFirst()) {
        id = cursor.getInt(0);
        cursor.close();
    }
    return id;
}

From source file:com.example.igorklimov.popularmoviesdemo.helpers.Utility.java

public static int removeFromFavorite(Cursor cursor, Context context) {
    //        setFavoritesRowCountPreference(context, -1);
    return context.getContentResolver().delete(FavoriteMovie.CONTENT_URI, MovieContract.COLUMN_TITLE + "=?",
            new String[] { getTitle(cursor) });
}