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.awesomeapp.messenger.MainActivity.java

private void handleIntent() {

    Intent intent = getIntent();//from   w w  w.  j  a va  2  s. co m

    if (intent != null) {
        Uri data = intent.getData();
        String type = intent.getType();
        if (data != null && Imps.Chats.CONTENT_ITEM_TYPE.equals(type)) {

            long chatId = ContentUris.parseId(data);

            Intent intentChat = new Intent(this, ConversationDetailActivity.class);
            intentChat.putExtra("id", chatId);
            startActivity(intentChat);
        } else if (Imps.Contacts.CONTENT_ITEM_TYPE.equals(type)) {
            long providerId = intent.getLongExtra(ImServiceConstants.EXTRA_INTENT_PROVIDER_ID,
                    mApp.getDefaultProviderId());
            long accountId = intent.getLongExtra(ImServiceConstants.EXTRA_INTENT_ACCOUNT_ID,
                    mApp.getDefaultAccountId());
            String username = intent.getStringExtra(ImServiceConstants.EXTRA_INTENT_FROM_ADDRESS);
            startChat(providerId, accountId, username, true, true);
        } else if (intent.hasExtra("username")) {
            //launch a new chat based on the intent value
            startChat(mApp.getDefaultProviderId(), mApp.getDefaultAccountId(),
                    intent.getStringExtra("username"), true, true);
        }

        setIntent(null);
    }
}

From source file:cz.maresmar.sfm.utils.ActionUtils.java

/**
 * Changes {@link ProviderContract#ACTION_SYNC_STATUS_EDIT} actions to {@link ProviderContract#ACTION_SYNC_STATUS_LOCAL}
 * and starts changes sync.// w w  w. j a  v  a2s . c om
 *
 * @param context Some valid context
 * @param userUri User Uri prefix
 */
@WorkerThread
public static void saveEdits(@NonNull Context context, @NonNull Uri userUri) {
    Uri actionUri = Uri.withAppendedPath(userUri, ProviderContract.ACTION_PATH);
    long userId = ContentUris.parseId(userUri);

    // Delete conflict local rows
    int conflictRows = context.getContentResolver().delete(actionUri,
            ProviderContract.Action.SYNC_STATUS + " == " + ProviderContract.ACTION_SYNC_STATUS_LOCAL + " AND "
                    + "EXISTS ( SELECT * FROM " + DbContract.FoodAction.TABLE_NAME + " AS EditAct WHERE "
                    + "EditAct." + DbContract.FoodAction.COLUMN_NAME_CID + " == "
                    + ProviderContract.Action.CREDENTIAL_ID + " AND " + "EditAct."
                    + DbContract.FoodAction.COLUMN_NAME_ME_PID + " == " + ProviderContract.Action.ME_PORTAL_ID
                    + " AND " + "EditAct." + DbContract.FoodAction.COLUMN_NAME_ME_RELATIVE_ID + " == "
                    + ProviderContract.Action.ME_RELATIVE_ID + " AND " + "EditAct."
                    + DbContract.FoodAction.COLUMN_NAME_SYNC_STATUS + " == "
                    + ProviderContract.ACTION_SYNC_STATUS_EDIT + " )",
            null);
    Timber.d("Deleted %d rows with conflict local values", conflictRows);

    // Save rows that has different values then the synced ones
    ContentValues newValues = new ContentValues();
    newValues.put(ProviderContract.Action.SYNC_STATUS, ProviderContract.ACTION_SYNC_STATUS_LOCAL);
    newValues.put(ProviderContract.Action.LAST_CHANGE, System.currentTimeMillis());

    int updatedRows = context.getContentResolver().update(actionUri, newValues,
            ProviderContract.Action.SYNC_STATUS + " == " + ProviderContract.ACTION_SYNC_STATUS_EDIT + " AND ("
                    + "NOT EXISTS (SELECT * FROM " + DbContract.FoodAction.TABLE_NAME + " AS SyncAct WHERE "
                    + "SyncAct." + DbContract.FoodAction.COLUMN_NAME_CID + " IN (SELECT "
                    + DbContract.Credential._ID + " FROM " + DbContract.Credential.TABLE_NAME + " WHERE "
                    + DbContract.Credential.COLUMN_NAME_UID + " == " + userId + ") AND " + "SyncAct."
                    + DbContract.FoodAction.COLUMN_NAME_ME_PID + " == " + ProviderContract.Action.ME_PORTAL_ID
                    + " AND " + "SyncAct." + DbContract.FoodAction.COLUMN_NAME_ME_RELATIVE_ID + " == "
                    + ProviderContract.Action.ME_RELATIVE_ID + " AND " + "SyncAct."
                    + DbContract.FoodAction.COLUMN_NAME_SYNC_STATUS + " == "
                    + ProviderContract.ACTION_SYNC_STATUS_SYNCED + " ) " + "AND ("
                    + ProviderContract.Action.RESERVED_AMOUNT + " > 0 OR "
                    + ProviderContract.Action.OFFERED_AMOUNT + " > 0 ) " + "OR EXISTS (SELECT * FROM "
                    + DbContract.FoodAction.TABLE_NAME + " AS SyncAct WHERE " + "SyncAct."
                    + DbContract.FoodAction.COLUMN_NAME_CID + " IN (SELECT " + DbContract.Credential._ID
                    + " FROM " + DbContract.Credential.TABLE_NAME + " WHERE "
                    + DbContract.Credential.COLUMN_NAME_UID + " == " + userId + ") AND " + "SyncAct."
                    + DbContract.FoodAction.COLUMN_NAME_ME_PID + " == " + ProviderContract.Action.ME_PORTAL_ID
                    + " AND " + "SyncAct." + DbContract.FoodAction.COLUMN_NAME_ME_RELATIVE_ID + " == "
                    + ProviderContract.Action.ME_RELATIVE_ID + " AND " + "SyncAct."
                    + DbContract.FoodAction.COLUMN_NAME_SYNC_STATUS + " == "
                    + ProviderContract.ACTION_SYNC_STATUS_SYNCED + " AND (" + "SyncAct."
                    + DbContract.FoodAction.COLUMN_NAME_RESERVED_AMOUNT + " != "
                    + ProviderContract.Action.RESERVED_AMOUNT + " OR " + "SyncAct."
                    + DbContract.FoodAction.COLUMN_NAME_OFFERED_AMOUNT + " != "
                    + ProviderContract.Action.OFFERED_AMOUNT + " ) ) )",
            null);

    // Delete rest
    int unnecessaryRows = context.getContentResolver().delete(actionUri,
            ProviderContract.Action.SYNC_STATUS + " == " + ProviderContract.ACTION_SYNC_STATUS_EDIT, null);

    // Check invariants
    if (BuildConfig.DEBUG) {
        Assert.that(updatedRows + unnecessaryRows > 0, "Should change at least one rows");
        Assert.that(conflictRows <= updatedRows + unnecessaryRows, "Conflict rows overflow");
    }

    SyncHandler.planChangesSync(context);
}

From source file:com.lambdasoup.quickfit.ui.WorkoutListActivity.java

private void addNewWorkout() {
    ContentValues contentValues = new ContentValues();
    contentValues.put(WorkoutEntry.COL_ACTIVITY_TYPE, FitnessActivities.AEROBICS);
    contentValues.put(WorkoutEntry.COL_DURATION_MINUTES, 30);
    Uri newWorkoutUri = getContentResolver().insert(QuickFitContentProvider.getUriWorkoutsList(),
            contentValues);/*  ww w.  j a v  a2  s . c om*/
    idToSelect = ContentUris.parseId(newWorkoutUri);
    hideMiniFabs();
}

From source file:edu.cens.loci.ui.PlaceListActivity.java

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    Intent intent;/*from  w  w  w .jav  a  2 s .c o  m*/
    Bundle extras;

    switch (mMode) {
    case MODE_VIEW:
        //Log.i(TAG, "onListItemClick : position=" + position + ", id=" + id);
        if ((mFilterState & FILTER_STATE_SUGGESTED) != 0) {
            if (position == 0) {
                intent = new Intent(Constants.Intents.UI.ACTION_VIEW_SUGGESTED_GPS_PLACES);
                Toast.makeText(this, "Not supported yet", Toast.LENGTH_SHORT).show();
                return;
            } else {
                intent = new Intent(Intent.ACTION_VIEW, getSelectedUri(position - 1));
            }
        } else {
            intent = new Intent(Intent.ACTION_VIEW, getSelectedUri(position));
        }
        startActivity(intent);
        break;
    case MODE_INSERT:
        extras = getIntent().getExtras();
        if (position == 0) {
            intent = new Intent(Intent.ACTION_INSERT, Places.CONTENT_URI);
            if (extras.containsKey(UI.LIST_ORDER_EXTRA_WIFI_KEY)
                    && extras.containsKey(UI.LIST_ORDER_EXTRA_WIFI_TIME_KEY)) {
                String wifiJson = getIntent().getExtras().getString(UI.LIST_ORDER_EXTRA_WIFI_KEY);
                String timestamp = getIntent().getExtras().getString(UI.LIST_ORDER_EXTRA_WIFI_TIME_KEY);
                intent.putExtra(Insert.WIFI_FINTERPRINT, wifiJson);
                intent.putExtra(Insert.TIME, timestamp);
            }
            //intent = new Intent(Intent.ACTION_EDIT, getSelectedUri(position));
        } else {
            intent = new Intent(Intent.ACTION_EDIT, getSelectedUri(position - 1));
            if (extras.containsKey(UI.LIST_ORDER_EXTRA_WIFI_KEY)
                    && extras.containsKey(UI.LIST_ORDER_EXTRA_WIFI_TIME_KEY)) {
                String wifiJson = getIntent().getExtras().getString(UI.LIST_ORDER_EXTRA_WIFI_KEY);
                String timestamp = getIntent().getExtras().getString(UI.LIST_ORDER_EXTRA_WIFI_TIME_KEY);
                intent.putExtra(Insert.WIFI_FINTERPRINT, wifiJson);
                intent.putExtra(Insert.TIME, timestamp);
            }
        }
        //extras = getIntent().getExtras();
        //if (extras != null) 
        //   intent.putExtras(extras);

        startActivityForResult(intent, SUBACTIVITY_NEW_PLACE);
        break;
    case MODE_REGISTER_SUGGESTED:
        extras = getIntent().getExtras();
        if (position == 0) {
            intent = new Intent(Intents.UI.ACTION_CREATE_FROM_SUGGESTED_PLACE, getIntent().getData());
        } else {
            //intent = new Intent(Intents.UI.ACTION_ADDTO_FROM_SUGESTED_PLACE, getIntent().getData());
            //extras.putLong(Intents.UI.SELECTED_PLACE_ID_EXTRA_KEY, getSelectedPlaceId(position-1));
            intent = new Intent(Intents.UI.ACTION_ADDTO_FROM_SUGESTED_PLACE, getSelectedUri(position - 1));
            extras.putLong(Intents.UI.SELECTED_PLACE_ID_EXTRA_KEY, ContentUris.parseId(getIntent().getData()));
        }
        if (extras != null)
            intent.putExtras(extras);

        startActivityForResult(intent, SUBACTIVITY_NEW_PLACE);
        break;
    case MODE_PICK:
        final Uri uri = getSelectedUri(position);
        intent = new Intent();
        setResult(RESULT_OK, intent.setData(uri));
        finish();
        break;
    }

}

From source file:com.example.diego.sunshine.FetchWeatherTask.java

private Long addLocation(String locationSetting, String cityName, double lat, double lon) {

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

    Long existingId = getLocationIdByLocationSetting(locationSetting);
    if (existingId == null) {
        Log.v(LOG_TAG, "Didn't find it in the database, inserting now!");
        ContentValues locationValues = new ContentValues();
        locationValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_LAT, lat);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_LON, lon);

        Uri locationInsertUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                locationValues);//  w ww.  j  a  va  2s .  c o  m

        return ContentUris.parseId(locationInsertUri);
    } else {
        return existingId;
    }

}

From source file:com.jackie.sunshine.app.sync.SunshineSyncAdapter.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.
 *//*w w w.j av a 2 s.c o  m*/
private long addLocation(String locationSetting, String cityName, double lat, double lon) {
    ContentResolver locationResolver = getContext().getContentResolver();
    Cursor locationCursor = locationResolver.query(LocationEntry.CONTENT_URI,
            new String[] { LocationEntry._ID }, LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
            new String[] { locationSetting }, null);
    long locationId;
    // Students: First, check if the location with this city name exists in the db
    if (locationCursor != null && locationCursor.moveToFirst()) {
        // If it exists, return the current ID
        locationId = locationCursor.getLong(locationCursor.getColumnIndex(LocationEntry._ID));
    } else {
        // Otherwise, insert it using the content resolver and the base URI
        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 insertUri = locationResolver.insert(LocationEntry.CONTENT_URI, locationValues);
        locationId = ContentUris.parseId(insertUri);
    }

    if (locationCursor != null && !locationCursor.isClosed()) {
        locationCursor.close();
    }

    return locationId;
}

From source file:com.example.rafa.sunshine.app.service.SunshineService.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/* w  ww. j a  v a 2  s.com*/
 * @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) {
    long locationId;

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

    if (locationCursor.moveToFirst()) {
        int locationIdIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = locationCursor.getLong(locationIdIndex);
    } else {
        // Now that the content provider is set up, inserting rows of data is pretty simple.
        // First create a ContentValues object to hold the data you want to insert.
        ContentValues locationValues = new ContentValues(

        );

        // Then add the data, along with the corresponding name of the data type,
        // so the content provider knows what kind of value is being inserted.
        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);

        // Finally, insert location data into the database.
        Uri insertedUri = this.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);
    }

    locationCursor.close();
    // Wait, that worked?  Yes!
    return locationId;
}

From source file:mobisocial.socialkit.musubi.Musubi.java

/**
 * Returns the DbUser that is currently logged in to this app.
 *///from   w w  w  . j  a v  a  2s.  c om
public DbIdentity userForLocalDevice(Uri feedUri) {
    Uri uri;
    if (feedUri == null) {
        uri = uriForDir(DbThing.IDENTITY);
    } else {
        long feedId = ContentUris.parseId(feedUri);
        uri = uriForItem(DbThing.MEMBER, feedId);
    }
    String selection = DbIdentity.COL_OWNED + " = 1";
    String[] selectionArgs = null;
    String sortOrder = null;
    Cursor c = mContext.getContentResolver().query(uri, DbIdentity.COLUMNS, selection, selectionArgs,
            sortOrder);
    try {
        if (c == null || !c.moveToFirst()) {
            Log.w(TAG, "no local user for feed " + feedUri, new Throwable());
            return null;
        }
        DbIdentity id = DbIdentity.fromStandardCursor(mContext, c);
        return id;
    } finally {
        if (c != null) {
            c.close();
        }
    }
}

From source file:com.example.android.sunshine.service.SunshineService.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  ww .ja v  a 2  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) {
    long locationId;

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

    if (locationCursor.moveToFirst()) {
        int locationIdIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = locationCursor.getLong(locationIdIndex);
    } else {
        // Now that the content provider is set up, inserting rows of data is pretty simple.
        // First create a ContentValues object to hold the data you want to insert.
        ContentValues locationValues = new ContentValues();

        // Then add the data, along with the corresponding name of the data type,
        // so the content provider knows what kind of value is being inserted.
        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);

        // Finally, insert location data into the database.
        Uri insertedUri = this.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);
    }

    locationCursor.close();
    // Wait, that worked?  Yes!
    return locationId;
}

From source file:com.rowland.hashtrace.sync.TweetHashTracerSyncAdapter.java

/**
 * Helper method to handle insertion of a new hashtag in the tweet database.
 *
 * @param hashTagSetting/*from w ww  .  j  a v a 2  s .c o  m*/
 *            The hashtag string used to request updates from the server.
 * @return the row ID of the added location.
 */
private long addHashTag(String hashTagSetting, String hashTagName) {
    long hashTagId;

    Log.v(LOG_TAG, "inserting " + hashTagSetting + ", with name: " + hashTagName);

    // First, check if the location with this city name exists in the db
    Cursor hashTagCursor = getContext().getContentResolver().query(
            TweetHashTracerContract.HashTagEntry.CONTENT_URI, new String[] { HashTagEntry._ID },
            HashTagEntry.COLUMN_HASHTAG_SETTING + " = ?", new String[] { hashTagSetting }, null);

    if (hashTagCursor.moveToFirst()) {
        int hashTagIdIndex = hashTagCursor.getColumnIndex(HashTagEntry._ID);
        hashTagId = hashTagCursor.getLong(hashTagIdIndex);
    } else {
        // Now that the content provider is set up, inserting rows of data
        // is pretty simple.
        // First create a ContentValues object to hold the data you want to
        // insert.
        ContentValues hashTagValues = new ContentValues();

        // Then add the data, along with the corresponding name of the data
        // type,
        // so the content provider knows what kind of value is being
        // inserted.
        hashTagValues.put(HashTagEntry.COLUMN_HASHTAG_NAME, hashTagName);
        hashTagValues.put(HashTagEntry.COLUMN_HASHTAG_SETTING, hashTagSetting);

        // Finally, insert location data into the database.
        Uri insertedUri = getContext().getContentResolver()
                .insert(TweetHashTracerContract.HashTagEntry.CONTENT_URI, hashTagValues);

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

    // Wait, that worked? Yes!
    return hashTagId;
}