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:com.google.android.demos.jamendo.app.ArtistActivity.java

/** {@inheritDoc} */
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
    switch (loaderId) {
    case LOADER_HEADER: {
        Uri uri = getIntent().getData();
        String[] projection = { Artists._ID, Artists.IDSTR, Artists.IMAGE, Artists.NAME };
        String selection = String.format("%s=?", JamendoContract.PARAM_IMAGE_SIZE);
        String[] selectionArgs = { getDimensionPixelSizeAsString(R.dimen.image_size) };
        String sortOrder = null;/*w w w. j av a  2s.  c  om*/
        return new CursorLoader(this, uri, projection, selection, selectionArgs, sortOrder);
    }
    case LOADER_LIST: {
        Uri uri = Albums.CONTENT_URI;
        String[] projection = { Albums._ID, Albums.IMAGE, Albums.NAME };
        Uri data = getIntent().getData();
        Long artistId = null;
        String artistIdStr = null;
        try {
            artistId = Long.valueOf(ContentUris.parseId(data));
        } catch (NumberFormatException e) {
            artistIdStr = data.getLastPathSegment();
        }
        String selection = String.format("%s=?&%s=?", artistId != null ? Artists.ID : Artists.IDSTR,
                JamendoContract.PARAM_IMAGE_SIZE);
        String[] selectionArgs = { artistId != null ? artistId.toString() : artistIdStr,
                getDimensionPixelSizeAsString(R.dimen.image_size) };
        String sortOrder = Albums.Order.RELEASEDATE.descending();
        return new CursorLoader(this, uri, projection, selection, selectionArgs, sortOrder);
    }
    default:
        return null;
    }
}

From source file:org.isoron.uhabits.HabitBroadcastReceiver.java

private void snoozeHabit(Context context, Intent intent) {
    Uri data = intent.getData();//from   w w  w .  j  a  va 2  s  .c o  m
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    long delayMinutes = Long.parseLong(prefs.getString("pref_snooze_interval", "15"));

    long habitId = ContentUris.parseId(data);
    Habit habit = Habit.get(habitId);
    if (habit != null)
        ReminderHelper.createReminderAlarm(context, habit, new Date().getTime() + delayMinutes * 60 * 1000);
    dismissNotification(context, habitId);
}

From source file:ro.weednet.contactssync.platform.ContactManager.java

public static long ensureGroupExists(Context context, Account account) {
    final ContentResolver resolver = context.getContentResolver();

    // Lookup the group
    long groupId = 0;
    final Cursor cursor = resolver.query(Groups.CONTENT_URI, new String[] { Groups._ID },
            Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=? AND " + Groups.TITLE + "=?",
            new String[] { account.name, account.type, GROUP_NAME }, null);
    if (cursor != null) {
        try {/*from w ww  .j  a v a 2s  .  co  m*/
            if (cursor.moveToFirst()) {
                groupId = cursor.getLong(0);
            }
        } finally {
            cursor.close();
        }
    }

    if (groupId == 0) {
        // Group doesn't exist yet, so create it
        final ContentValues contentValues = new ContentValues();
        contentValues.put(Groups.ACCOUNT_NAME, account.name);
        contentValues.put(Groups.ACCOUNT_TYPE, account.type);
        contentValues.put(Groups.TITLE, GROUP_NAME);
        //   contentValues.put(Groups.GROUP_IS_READ_ONLY, true);
        contentValues.put(Groups.GROUP_VISIBLE, true);

        final Uri newGroupUri = resolver.insert(Groups.CONTENT_URI, contentValues);
        groupId = ContentUris.parseId(newGroupUri);
    }
    return groupId;
}

From source file:idea.ruan.oksun.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  ww w  .  j a va  2s. 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) {

    ContentResolver contentResolver = mContext.getContentResolver();

    Uri locationTableUri = WeatherContract.LocationEntry.CONTENT_URI;

    long locationId;
    Cursor c = contentResolver.query(locationTableUri, new String[] { WeatherContract.LocationEntry._ID },
            WeatherContract.LocationEntry.COLUMN_CITY_NAME + " = ?", new String[] { cityName }, null);
    if (c.moveToFirst()) {

        int i = c.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = c.getLong(i);

    } else {

        ContentValues cv = new ContentValues();

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

        locationId = ContentUris.parseId(contentResolver.insert(locationTableUri, cv));

    }

    c.close();

    return locationId;
}

From source file:alberthsu.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.ja v a2s.  c  om*/
 * @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) {

    // 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()) {
        int locationIdIndex = cursor.getColumnIndex(LocationEntry._ID);
        return cursor.getLong(locationIdIndex);
    } else {
        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.google.android.demos.jamendo.app.PlaylistActivity.java

/** {@inheritDoc} */
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
    switch (loaderId) {
    case LOADER_HEADER: {
        Uri uri = getIntent().getData();
        String[] projection = { Playlists._ID, Users.IMAGE, Playlists.NAME, Users.NAME, Users.ID,
                Playlists.ID };/*from   ww  w. j  a v  a 2s.c  o m*/
        String selection = String.format("%s=?", JamendoContract.PARAM_IMAGE_SIZE);
        String[] selectionArgs = { getDimensionPixelSizeAsString(R.dimen.image_size) };
        String sortOrder = null;
        return new CursorLoader(this, uri, projection, selection, selectionArgs, sortOrder);
    }
    case LOADER_LIST: {
        Uri uri = Tracks.CONTENT_URI;
        String[] projection = TrackListAdapter.PROJECTION;
        String selection = String.format("%s=?&%s=?&%s=?", JamendoContract.PARAM_JOIN,
                JamendoContract.PARAM_JOIN, Playlists.ID);
        Uri data = getIntent().getData();
        long playlistId = ContentUris.parseId(data);
        String[] selectionArgs = { JamendoContract.JOIN_TRACK_ALBUM, JamendoContract.JOIN_ALBUM_ARTIST,
                String.valueOf(playlistId) };
        String sortOrder = Tracks.Order.NUMPLAYLIST.ascending();
        return new CursorLoader(this, uri, projection, selection, selectionArgs, sortOrder);
    }
    default:
        return null;
    }
}

From source file:com.josenaves.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 v  a2s .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) {

    // First, check if the location with this city name exists in the db
    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()) {
        int locationIdIndex = cursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        return cursor.getLong(locationIdIndex);
    } else {
        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_COORD_LAT, lat);
        locationValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);

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

        return ContentUris.parseId(locationInsertUri);
    }
}

From source file:com.example.spatidar.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 .jav  a2s  . 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) {
    long locationId = -1;

    // Query if locationSetting entry exists in the table
    Cursor cur = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI, null,
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + "= ?", new String[] { locationSetting },
            null);

    if (cur.moveToFirst()) {
        int idx = cur.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = cur.getLong(idx);
    } else {
        // Insert
        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 uri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, values);

        locationId = ContentUris.parseId(uri);
    }
    cur.close();

    return locationId;
}

From source file:fr.eoit.activity.ItemInfo2Activity.java

/** Called when the activity is first created. */
@Override//from ww w.j  av a 2s.c om
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.item_info);

    receiver = new PriceChangedBroadCastReceiver();

    setActionBarInderterminate(true);

    itemId = (int) ContentUris.parseId(getIntent().getData());

    initOrRestart();

    pricesFragment = (PricesFragment) getSupportFragmentManager().findFragmentById(R.id.PRICES_FRAGMENT);
    blueprintInfoFragment = (BlueprintInfoFragment) getSupportFragmentManager()
            .findFragmentById(R.id.BLUEPRINT_FRAGMENT);
    producesFragment = (ProducesFragment) getSupportFragmentManager().findFragmentById(R.id.PRODUCE_FRAGMENT);
    refineMaterialListFragment = (RefineMaterialListFragment) getSupportFragmentManager()
            .findFragmentById(R.id.REFINE_FRAGMENT);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(false);

    if (findViewById(R.id.ITEM_ICON_LAYOUT) != null) {
        findViewById(R.id.ITEM_ICON_LAYOUT).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isItemIconExpended) {
                    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150,
                                    getResources().getDisplayMetrics()));

                    v.setLayoutParams(layoutParams);
                    isItemIconExpended = false;
                } else {
                    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250,
                                    getResources().getDisplayMetrics()));
                    v.setLayoutParams(layoutParams);
                    isItemIconExpended = true;
                }
            }
        });

        DefaultStationTips dialog = new DefaultStationTips(this);
        if (Stations.getProductionStation().stationId == EOITConst.Stations.JITA_STATION_ID
                && Stations.getTradeStation().stationId == EOITConst.Stations.JITA_STATION_ID
                && dialog.isActive(this)) {
            dialog.show(getSupportFragmentManager(), "default_station");
        }
    }
}

From source file:com.tcm.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  w w  w . ja  v  a 2  s  .c om
 * @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

    ContentResolver contentResolver = mContext.getContentResolver();
    Cursor cursor = contentResolver.query(LocationEntry.CONTENT_URI, new String[] { LocationEntry._ID },
            LocationEntry.COLUMN_CITY_NAME + " = ?", new String[] { cityName }, null);
    try {
        if (cursor.moveToFirst()) {
            return cursor.getInt(cursor.getColumnIndex(LocationEntry._ID));
        }
    } finally {
        cursor.close();
    }

    ContentValues contentValues = new ContentValues();
    contentValues.put(LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
    contentValues.put(LocationEntry.COLUMN_CITY_NAME, cityName);
    contentValues.put(LocationEntry.COLUMN_COORD_LAT, lat);
    contentValues.put(LocationEntry.COLUMN_COORD_LONG, lon);
    Uri insertUri = contentResolver.insert(LocationEntry.CONTENT_URI, contentValues);

    return ContentUris.parseId(insertUri);
}