Example usage for android.content ContentUris parseId

List of usage examples for android.content ContentUris parseId

Introduction

In this page you can find the example usage for android.content ContentUris parseId.

Prototype

public static long parseId(Uri contentUri) 

Source Link

Document

Converts the last path segment to a long.

Usage

From source file:org.dmfs.webcal.fragments.PagerFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    // get the id before we call super.onCreate, because it's used to retrieve the id of the section to show first
    mId = ContentUris.parseId(mUri);
    super.onCreate(savedInstanceState);
}

From source file:org.kontalk.ui.ConversationsActivity.java

protected boolean handleIntent(Intent intent) {
    if (super.handleIntent(intent))
        return true;

    if (intent != null) {
        String action = intent.getAction();

        // this is for intents coming from the world, forwarded by ComposeMessage
        boolean actionView = Intent.ACTION_VIEW.equals(action);
        if (actionView || ComposeMessage.ACTION_VIEW_USERID.equals(action)) {
            Uri uri = null;//from   w ww. ja  v  a  2 s .  c  o  m

            if (actionView) {
                Cursor c = getContentResolver().query(intent.getData(),
                        new String[] { Syncer.DATA_COLUMN_PHONE }, null, null, null);
                if (c.moveToFirst()) {
                    String phone = c.getString(0);
                    String userJID = XMPPUtils.createLocalJID(this, MessageUtils.sha1(phone));
                    uri = Threads.getUri(userJID);
                }
                c.close();
            } else {
                uri = intent.getData();
            }

            if (uri != null)
                openConversation(uri);
        } else if (ComposeMessage.ACTION_VIEW_CONVERSATION.equals(action)) {
            Uri uri = intent.getData();
            if (uri != null) {
                long threadId = ContentUris.parseId(uri);
                if (threadId >= 0)
                    openConversation(threadId);
            }
        }
    }

    return true;
}

From source file:com.appsimobile.appsii.module.apps.AppsProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();

    // Generate the body of the query
    int match = sURLMatcher.match(uri);
    switch (match) {
    // loads all tagged-apps in a given tag.
    case TABLE_APPS_ITEM:
        long id = ContentUris.parseId(uri);
        qb.setTables(TaggedAppColumns.TABLE_NAME + "," + TagColumns.TABLE_NAME);
        qb.setProjectionMap(sAppsProjectionMap);
        qb.appendWhere(TaggedAppColumns.TAG_ID + "=" + id + " AND ");
        qb.appendWhere("taggedApps.tag_id=tags._id");
        break;//from  w  w  w. java  2s  .c om
    // loads all tagged apps
    case TABLE_APPS:
        qb.setTables(TaggedAppColumns.TABLE_NAME + "," + TagColumns.TABLE_NAME);
        qb.setProjectionMap(sAppsProjectionMap);
        qb.appendWhere("taggedApps.tag_id=tags._id");
        break;
    // loads items in the launch history table
    case TABLE_HISTORY:
        qb.setTables(LaunchHistoryColumns.TABLE_NAME);
        qb.setProjectionMap(sHistoryProjectionMap);
        break;
    // loads all tags
    case TABLE_TAGS:
        qb.setTables(TagColumns.TABLE_NAME);
        qb.setProjectionMap(sTagsProjectionMap);
        break;
    default:
        throw new IllegalArgumentException("Invalid uri: " + uri);
    }
    return qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
}

From source file:com.android.messaging.mmslib.util.PduCache.java

@Override
public synchronized PduCacheEntry purge(Uri uri) {
    int match = URI_MATCHER.match(uri);
    switch (match) {
    case MMS_ALL_ID:
        return purgeSingleEntry(uri);
    case MMS_INBOX_ID:
    case MMS_SENT_ID:
    case MMS_DRAFTS_ID:
    case MMS_OUTBOX_ID:
        String msgId = uri.getLastPathSegment();
        return purgeSingleEntry(Uri.withAppendedPath(Mms.CONTENT_URI, msgId));
    // Implicit batch of purge, return null.
    case MMS_ALL:
    case MMS_CONVERSATION:
        purgeAll();/*from w  ww. j a v a  2 s.  c om*/
        return null;
    case MMS_INBOX:
    case MMS_SENT:
    case MMS_DRAFTS:
    case MMS_OUTBOX:
        purgeByMessageBox(MATCH_TO_MSGBOX_ID_MAP.get(match));
        return null;
    case MMS_CONVERSATION_ID:
        purgeByThreadId(ContentUris.parseId(uri));
        return null;
    default:
        return null;
    }
}

From source file:com.iyedb.sunshine.FetchWeatherTask.java

/**
 * Helper method to handle insertion of a new location in the weather database.
 *
 * @param locationSetting The location string used to request updates from the server.
 * @param cityName        A human-readable city name, e.g "Mountain View"
 * @param lat             the latitude of the city
 * @param lon             the longitude of the city
 * @return the row ID of the added location.
 *///from   w  w w. j  a va2  s  . c o m
private long insertLocationInDatabase(String locationSetting, String cityName, double lat, double lon) {

    Log.v(LOG_TAG, "inserting " + cityName + ", with coord: " + lat + ", " + lon);

    // First, check if the location with this city name exists in the db
    Cursor cursor = mContext.getContentResolver().query(LocationEntry.CONTENT_URI,
            new String[] { LocationEntry._ID }, LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
            new String[] { locationSetting }, null);

    if (cursor.moveToFirst()) {
        Log.v(LOG_TAG, "Found it in the database!");
        int locationIdIndex = cursor.getColumnIndex(LocationEntry._ID);
        return cursor.getLong(locationIdIndex);
    } else {
        Log.v(LOG_TAG, "Didn't find it in the database, inserting now!");
        ContentValues locationValues = new ContentValues();
        locationValues.put(LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        locationValues.put(LocationEntry.COLUMN_CITY_NAME, cityName);
        locationValues.put(LocationEntry.COLUMN_COORD_LAT, lat);
        locationValues.put(LocationEntry.COLUMN_COORD_LONG, lon);

        Uri locationInsertUri = mContext.getContentResolver().insert(LocationEntry.CONTENT_URI, locationValues);

        return ContentUris.parseId(locationInsertUri);
    }
}

From source file:com.kwang.sunshine.FetchWeatherTask.java

/**
 * Helper method to handle insertion of a new location in the weather database.
 *
 * @param locationSetting The location string used to request updates from the server.
 * @param cityName A human-readable city name, e.g "Mountain View"
 * @param lat the latitude of the city//from  w w w. j  ava2 s  . c o  m
 * @param lon the longitude of the city
 * @return the row ID of the added location.
 */
private long addLocation(String locationSetting, String cityName, double lat, double lon) {

    Log.v(LOG_TAG, "inserting " + cityName + ", with coord: " + lat + ", " + lon);

    // First, check if the location with this city name exists in the db
    Cursor cursor = mContext.getContentResolver().query(LocationEntry.CONTENT_URI,
            new String[] { LocationEntry._ID }, LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
            new String[] { locationSetting }, null);

    if (cursor.moveToFirst()) {
        Log.v(LOG_TAG, "Found it in the database!");
        int locationIdIndex = cursor.getColumnIndex(LocationEntry._ID);
        return cursor.getLong(locationIdIndex);
    } else {
        Log.v(LOG_TAG, "Didn't find it in the database, inserting now!");
        ContentValues locationValues = new ContentValues();
        locationValues.put(LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        locationValues.put(LocationEntry.COLUMN_CITY_NAME, cityName);
        locationValues.put(LocationEntry.COLUMN_COORD_LAT, lat);
        locationValues.put(LocationEntry.COLUMN_COORD_LONG, lon);

        Uri locationInsertUri = mContext.getContentResolver().insert(LocationEntry.CONTENT_URI, locationValues);

        return ContentUris.parseId(locationInsertUri);
    }
}

From source file:com.home883.ali.sunshine.FetchWeatherTask.java

/**
 * Helper method to handle insertion of a new location in the weather database.
 *
 * @param locationSetting The location string used to request updates from the server.
 * @param cityName A human-readable city name, e.g "Mountain View"
 * @param lat the latitude of the city/* ww w .j a  v a2 s.  c o  m*/
 * @param lon the longitude of the city
 * @return the row ID of the added location.
 */
long addLocation(String locationSetting, String cityName, double lat, double lon) {
    // Students: First, check if the location with this city name exists in the db
    // If it exists, return the current ID
    // Otherwise, insert it using the content resolver and the base URI

    Cursor locationCursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI, null,
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?", new String[] { locationSetting },
            null);

    long locationRow;

    if (locationCursor.moveToFirst()) {
        locationRow = locationCursor.getLong(locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID));
    } else {
        ContentValues values = new ContentValues();
        values.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        values.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        values.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        values.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
        Uri locationUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                values);
        locationRow = ContentUris.parseId(locationUri);
    }

    return locationRow;
}

From source file:com.example.debra.sunshine.app.FetchWeatherTask.java

/**
 * Helper method to handle insertion of a new location in the weather database.
 *
 * @param locationSetting The location string used to request updates from the server.
 * @param cityName A human-readable city name, e.g "Mountain View"
 * @param lat the latitude of the city//from  www . j  a  va2  s  .co  m
 * @param lon the longitude of the city
 * @return the row ID of the added location.
 */
long addLocation(String locationSetting, String cityName, double lat, double lon) {
    // Students: First, check if the location with this city name exists in the db
    // If it exists, return the current ID
    // Otherwise, insert it using the content resolver and the base URI
    long locationId;
    Cursor cursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI,
            new String[] { WeatherContract.LocationEntry._ID },
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ? ", new String[] { locationSetting },
            null);
    if (cursor.moveToFirst()) {
        locationId = cursor.getLong(cursor.getColumnIndex(WeatherContract.LocationEntry._ID));
    } else {
        ContentValues value = new ContentValues();
        value.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        value.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        value.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        value.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);

        Uri insertedUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                value);
        locationId = ContentUris.parseId(insertedUri);
    }
    cursor.close();
    return locationId;
}

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

public static long addGroupToContacts(Context context, ContentProviderClient provider, Account account,
        JSONObject group, SyncResult result)
        throws RemoteException, OperationApplicationException, JSONException {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ContentProviderOperation.Builder op = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
            .withValue(RawContacts.ACCOUNT_TYPE, GMSApplication.ACCOUNT_TYPE)
            .withValue(RawContacts.ACCOUNT_NAME, account.name)
            .withValue(RawContacts.SOURCE_ID, group.getString(JSONKeys.KEY_ID));
    ops.add(op.build());//from  w  w w . j a  v  a  2  s .c  o  m
    op = ContentProviderOperation.newInsert(Data.CONTENT_URI).withValueBackReference(Data.RAW_CONTACT_ID, 0)
            .withValue(Data.MIMETYPE, CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
            .withValue(CommonDataKinds.StructuredName.DISPLAY_NAME, group.getString(JSONKeys.KEY_NAME));
    ops.add(op.build());
    op = ContentProviderOperation.newInsert(Data.CONTENT_URI).withValueBackReference(Data.RAW_CONTACT_ID, 0)
            .withValue(Data.MIMETYPE, CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(CommonDataKinds.Phone.NUMBER, group.get(JSONKeys.KEY_PHONE))
            .withValue(CommonDataKinds.Phone.TYPE, CommonDataKinds.Phone.TYPE_MAIN);
    ops.add(op.build());
    op = ContentProviderOperation.newInsert(Data.CONTENT_URI).withValueBackReference(Data.RAW_CONTACT_ID, 0)
            .withValue(Data.MIMETYPE, CommonDataKinds.Photo.CONTENT_ITEM_TYPE).withYieldAllowed(true);
    ops.add(op.build());
    InputStream is = null;
    ByteArrayOutputStream baos = null;
    try {
        is = context.getAssets().open("gms.png", AssetManager.ACCESS_BUFFER);
        baos = new ByteArrayOutputStream();
        int value = is.read();
        while (value != -1) {
            baos.write(value);
            value = is.read();
        }
        op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE, CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, baos.toByteArray())
                .withYieldAllowed(true);
        ops.add(op.build());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (is != null) {
                is.close();
            }
            if (baos != null) {
                baos.close();
            }
        } catch (IOException ignore) {
        }
    }
    ContentProviderResult[] results = provider.applyBatch(ops);
    return (results[0].uri != null) ? ContentUris.parseId(results[0].uri) : -1L;
}

From source file:ua.ck.geeknub.prozapas.sunshine.app.FetchWeatherTask.java

/**
 * Helper method to handle insertion of a new location in the weather database.
 *
 * @param locationSetting The location string used to request updates from the server.
 * @param cityName        A human-readable city name, e.g "Mountain View"
 * @param lat             the latitude of the city
 * @param lon             the longitude of the city
 * @return the row ID of the added location.
 */// ww w. jav a  2s  .  c  o  m
long addLocation(String locationSetting, String cityName, double lat, double lon) {
    long locationId;

    Cursor locationCursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI,
            new String[] { WeatherContract.LocationEntry._ID },
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?", new String[] { locationSetting },
            null);

    if (locationCursor.moveToFirst()) {
        int locationIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = locationCursor.getLong(locationIndex);
    } else {
        ContentValues locationValues = new ContentValues();

        locationValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);

        Uri insertedUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                locationValues);

        // The resulting URI contains the ID for the row.  Extract the locationId from the Uri.
        locationId = ContentUris.parseId(insertedUri);
    }
    return locationId;
}