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.calgen.prodek.sunshine.Data.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  ww  .  j a  v a 2 s. com
public 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;

    //Checking if the location already exists
    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 uri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                locationValues);

        locationId = ContentUris.parseId(uri);

    }
    locationCursor.close();

    return locationId;
}

From source file:app.com.ferchofpz.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  a  v  a2 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 itemId = -1;
    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 columnIndex = cursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        itemId = cursor.getLong(columnIndex);
    } else {
        ContentValues insertValues = new ContentValues();

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

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

        itemId = ContentUris.parseId(insertedUri);
    }

    cursor.close();
    return itemId;
}

From source file:io.github.the_dagger.sunshine.data.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 a 2s. c  om*/
 * @param lon the longitude of the city
 * @return the row ID of the added location.
 */
public 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 locationIdIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = locationCursor.getLong(locationIdIndex);
    } else {
        ContentValues contentValues = new ContentValues();
        contentValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
        Uri insertLocationUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                contentValues);
        locationId = ContentUris.parseId(insertLocationUri);
    }
    locationCursor.close();
    return locationId;
}

From source file:com.android.dialer.calllog.DefaultVoicemailNotifier.java

/**
 * Updates the notification and notifies of the call with the given URI.
 *
 * Clears the notification if there are no new voicemails, and notifies if the given URI
 * corresponds to a new voicemail./*from  ww w  .ja  va  2s . c o  m*/
 *
 * It is not safe to call this method from the main thread.
 */
public void updateNotification(Uri newCallUri) {
    // Lookup the list of new voicemails to include in the notification.
    // TODO: Move this into a service, to avoid holding the receiver up.
    final List<NewCall> newCalls = CallLogNotificationsHelper.getInstance(mContext).getNewVoicemails();

    if (newCalls == null) {
        // Query failed, just return.
        return;
    }

    if (newCalls.isEmpty()) {
        // No voicemails to notify about: clear the notification.
        getNotificationManager().cancel(NOTIFICATION_TAG, NOTIFICATION_ID);
        return;
    }

    Resources resources = mContext.getResources();

    // This represents a list of names to include in the notification.
    String callers = null;

    // Maps each number into a name: if a number is in the map, it has already left a more
    // recent voicemail.
    final Map<String, String> names = Maps.newHashMap();

    // Determine the call corresponding to the new voicemail we have to notify about.
    NewCall callToNotify = null;

    // Iterate over the new voicemails to determine all the information above.
    Iterator<NewCall> itr = newCalls.iterator();
    while (itr.hasNext()) {
        NewCall newCall = itr.next();

        // Skip notifying for numbers which are blocked.
        if (FilteredNumbersUtil.shouldBlockVoicemail(mContext, newCall.number, newCall.countryIso,
                newCall.dateMs)) {
            itr.remove();

            // Delete the voicemail.
            mContext.getContentResolver().delete(newCall.voicemailUri, null, null);
            continue;
        }

        // Check if we already know the name associated with this number.
        String name = names.get(newCall.number);
        if (name == null) {
            name = CallLogNotificationsHelper.getInstance(mContext).getName(newCall.number,
                    newCall.numberPresentation, newCall.countryIso);
            names.put(newCall.number, name);
            // This is a new caller. Add it to the back of the list of callers.
            if (TextUtils.isEmpty(callers)) {
                callers = name;
            } else {
                callers = resources.getString(R.string.notification_voicemail_callers_list, callers, name);
            }
        }
        // Check if this is the new call we need to notify about.
        if (newCallUri != null && newCall.voicemailUri != null
                && ContentUris.parseId(newCallUri) == ContentUris.parseId(newCall.voicemailUri)) {
            callToNotify = newCall;
        }
    }

    // All the potential new voicemails have been removed, e.g. if they were spam.
    if (newCalls.isEmpty()) {
        return;
    }

    // If there is only one voicemail, set its transcription as the "long text".
    String transcription = null;
    if (newCalls.size() == 1) {
        transcription = newCalls.get(0).transcription;
    }

    if (newCallUri != null && callToNotify == null) {
        Log.e(TAG, "The new call could not be found in the call log: " + newCallUri);
    }

    // Determine the title of the notification and the icon for it.
    final String title = resources.getQuantityString(R.plurals.notification_voicemail_title, newCalls.size(),
            newCalls.size());
    // TODO: Use the photo of contact if all calls are from the same person.
    final int icon = android.R.drawable.stat_notify_voicemail;

    Pair<Uri, Integer> info = getNotificationInfo(callToNotify);

    Notification.Builder notificationBuilder = new Notification.Builder(mContext).setSmallIcon(icon)
            .setContentTitle(title).setContentText(callers)
            .setStyle(new Notification.BigTextStyle().bigText(transcription))
            .setColor(resources.getColor(R.color.dialer_theme_color)).setSound(info.first)
            .setDefaults(info.second).setDeleteIntent(createMarkNewVoicemailsAsOldIntent()).setAutoCancel(true);

    // Determine the intent to fire when the notification is clicked on.
    final Intent contentIntent;
    // Open the call log.
    contentIntent = new Intent(mContext, DialtactsActivity.class);
    contentIntent.putExtra(DialtactsActivity.EXTRA_SHOW_TAB, ListsFragment.TAB_INDEX_VOICEMAIL);
    notificationBuilder.setContentIntent(
            PendingIntent.getActivity(mContext, 0, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT));

    // The text to show in the ticker, describing the new event.
    if (callToNotify != null) {
        CharSequence msg = ContactDisplayUtils.getTtsSpannedPhoneNumber(resources,
                R.string.notification_new_voicemail_ticker, names.get(callToNotify.number));
        notificationBuilder.setTicker(msg);
    }
    Log.i(TAG, "Creating voicemail notification");
    getNotificationManager().notify(NOTIFICATION_TAG, NOTIFICATION_ID, notificationBuilder.build());
}

From source file:com.amirhashemei.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/*ww  w .j a va2s.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
    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);

    }

    locationCursor.close();
    // Otherwise, insert it using the content resolver and the base URI
    return locationId;
}

From source file:com.ryansmertz.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  w  w. 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;

    //Check if lcoatoin with this city name is in the db
    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 locationIdIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = locationCursor.getLong(locationIdIndex);
    } 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);

        locationId = ContentUris.parseId(insertedUri);
    }

    return locationId;
}

From source file:com.example.diokey.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 w  w.j a  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
    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 it exists, return the current ID
    if (locationCursor.moveToFirst()) {
        int locationIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = locationCursor.getLong(locationIndex);
    } else {
        // Otherwise, insert it using the content resolver and the base URI
        ContentValues contentValues = new ContentValues();
        contentValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        contentValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);

        Uri locationUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                contentValues);

        locationId = ContentUris.parseId(locationUri);
    }

    return locationId;
}

From source file:com.example.len.sunshinelessons1.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  a va  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;

    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 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 = 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);
    }

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

From source file:com.ahmed.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 a v  a 2 s .c  o m*/
 * @param lon the longitude of the city
 * @return the row ID of the added location.
 */
public 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 query = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI,
            new String[] { WeatherContract.LocationEntry._ID },
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?", new String[] { locationSetting },
            null);

    if (query != null && query.moveToFirst()) {
        return query.getLong(query.getColumnIndex(WeatherContract.LocationEntry._ID));
    }

    ContentValues values = new ContentValues();

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

    Uri inserted = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, values);

    return ContentUris.parseId(inserted);
}

From source file:com.example.avs.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   www .j a  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;

    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 locationIdIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);
        locationId = locationCursor.getLong(locationIdIndex);
    } 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);

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

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