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.isoron.uhabits.HabitBroadcastReceiver.java

private void checkHabit(Context context, Intent intent) {
    Uri data = intent.getData();//from  w  ww. j a v a 2s  . c om
    Long timestamp = intent.getLongExtra("timestamp", DateHelper.getStartOfToday());

    long habitId = ContentUris.parseId(data);
    Habit habit = Habit.get(habitId);
    if (habit != null)
        habit.repetitions.toggle(timestamp);
    dismissNotification(context, habitId);

    sendRefreshBroadcast(context);
}

From source file:fr.eoidb.activity.MarketGroupItemListActivity.java

/** Called when the activity is first created. */
@Override//from  www .  ja  va2  s.c  o m
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.market_group_list);

    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        // handles a search query
        query = intent.getStringExtra(SearchManager.QUERY);
        showResults();
    } else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        // handles a click on a search suggestion; launches activity to show word
        Intent itemIntent = new Intent(this, ItemInfoActivity.class);
        itemIntent.setData(intent.getData());
        startActivity(itemIntent);
        finish();
    } else {
        // If there is no data associated with the Intent, sets the data to the default URI, which
        // accesses a list of notes.
        if (intent.getData() == null) {
            intent.setData(MarketGroups.CONTENT_URI);
        }

        if (getIntent().getData().equals(MarketGroups.CONTENT_URI)) {
            marketGroupId = -1;
        } else {
            marketGroupId = ContentUris.parseId(getIntent().getData());
        }

        // Creates the backing adapter for the ListView.
        adapter = new SimpleCursorAdapter(this, R.layout.market_group_row, null, dataColumns, viewIDs,
                SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        adapter.setViewBinder(new MarketGroupListViewBinder(true));

        ListView itemListView = (ListView) findViewById(R.id.market_group_list);
        itemListView.setOnItemClickListener(new ItemOnItemListClickListener());
        // Sets the ListView's adapter to be the cursor adapter that was just created.
        itemListView.setAdapter(adapter);

        getSupportLoaderManager().initLoader(LOADER_ID, null, this);
    }
}

From source file:com.prashantpal.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//  w ww  . j a  v  a  2s.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
    long locationId;
    ContentResolver resolver = mContext.getContentResolver();
    Cursor cursor = resolver.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);
        locationId = cursor.getLong(locationIdIndex);
    } else {
        ContentValues contentValues = new ContentValues();
        contentValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        Uri uri = resolver.insert(WeatherContract.LocationEntry.CONTENT_URI, contentValues);
        locationId = ContentUris.parseId(uri);
    }
    cursor.close();
    // Otherwise, insert it using the content resolver and the base URI
    return locationId;
}

From source file:com.example.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// www.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) {
    // 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 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 columnIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = locationCursor.getLong(columnIndex);
    } 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 insertedUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                values);

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

    return locationId;
}

From source file:com.bangz.smartmute.services.LocationMuteService.java

public static void addGeofence(Context context, Uri uri) {

    Intent intent = new Intent(ACTION_ADD_GEOFENCE, uri, context, LocationMuteService.class);
    LogUtils.LOGD(TAG, " Pending add one geofence. id = " + ContentUris.parseId(uri));
    context.startService(intent);//w  ww .  j a v a2  s .  co m
}

From source file:com.example.riteden.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 ww  w.  jav a2s  .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
    //WeatherProvider.query()
    Cursor cur = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI,
            new String[] { WeatherContract.LocationEntry._ID },
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?", new String[] { locationSetting },
            null);
    if (cur.moveToFirst()) {
        return cur.getLong(cur.getColumnIndex(WeatherContract.LocationEntry._ID));
    }

    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 return_rowID = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, values);
    // If it exists, return the current ID
    // Otherwise, insert it using the content resolver and the base URI
    return ContentUris.parseId(return_rowID);
}

From source file:com.example.android.miniweather.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  .jav 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;
    long locationID;

    cursor = contentResolver.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);
        locationID = cursor.getLong(locationIDIndex);
    } else {
        ContentValues newLocation = new ContentValues();
        Uri insertedUri;

        newLocation.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        newLocation.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        newLocation.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        newLocation.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
        insertedUri = contentResolver.insert(WeatherContract.LocationEntry.CONTENT_URI, newLocation);

        locationID = ContentUris.parseId(insertedUri);

    }

    cursor.close();
    return locationID;
}

From source file:com.example.fcp.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.
 *//*  w  w w  . j a  v a 2  s .co m*/
long addLocation(String locationSetting, String cityName, double lat, double lon) {
    long locationId;
    // Students: First, check if the location with this city name exists in the db
    Cursor weaCursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI, null,
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + "= ?", new String[] { locationSetting },
            null);

    // If it exists, return the current ID
    if (weaCursor.moveToFirst()) {
        int locationIdIndex = weaCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = weaCursor.getLong(locationIdIndex);
    } else {
        // Otherwise, insert it using the content resolver and the base URI
        ContentValues locValues = new ContentValues();
        locValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        locValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        locValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        locValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
        //long rowId = weaProvider.insert(WeatherContract.LocationEntry.CONTENT_URI,locValues);
        Uri retUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, locValues);
        //locationId = Long.parseLong(retUri.getLastPathSegment());
        locationId = ContentUris.parseId(retUri);
    }
    return locationId;
}

From source file:org.krackedeggs.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 ww  .j a v a  2s  . 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
    String[] projection = new String[] { WeatherContract.LocationEntry._ID,
            WeatherContract.LocationEntry.COLUMN_CITY_NAME };
    String whereclause = WeatherContract.LocationEntry.TABLE_NAME + "."
            + WeatherContract.LocationEntry.COLUMN_CITY_NAME + " = ? ";
    String[] whereclauseArgs = new String[] { cityName };
    //        WeatherProvider weatherProvider = new WeatherProvider();
    //        weatherProvider.getWritePermission();
    //        if (weatherProvider.onCreate()) {
    Cursor cursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI, projection,
            whereclause, whereclauseArgs, null);
    if (cursor.moveToFirst()) // check if city exists
    {
        return cursor.getLong(cursor.getColumnIndex(WeatherContract.LocationEntry._ID));
    }
    //        }
    ContentValues row = new ContentValues();
    row.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
    row.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
    row.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
    row.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
    Uri locationUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, row);
    return ContentUris.parseId(locationUri);
}

From source file:com.jll.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/* w ww  .  ja  va 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

    String selection = WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + "= ?" + " AND "
            + WeatherContract.LocationEntry.COLUMN_CITY_NAME + "= ?" + " AND "
            + WeatherContract.LocationEntry.COLUMN_COORD_LAT + "= ?" + " AND "
            + WeatherContract.LocationEntry.COLUMN_COORD_LONG + "= ?";
    String[] selectionArgs = new String[] { locationSetting, cityName, String.valueOf(lat),
            String.valueOf(lon) };

    Cursor cursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI,
            new String[] { WeatherContract.LocationEntry._ID }, selection, selectionArgs, null);

    long id = -1;
    if (cursor.moveToFirst()) {
        id = cursor.getLong(0);
    } 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 newRow = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, values);
        id = ContentUris.parseId(newRow);
    }

    // If it exists, return the current ID
    // Otherwise, insert it using the content resolver and the base URI
    return id;
}