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.jcjacksonengineering.android.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.  ja  v a  2 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) {
    long locationId;

    // First, check if the location with this city name exists 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 {
        // 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.google.android.demos.jamendo.app.AlbumActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    int groupId = MENU_GROUP_INTENT_OPTIONS;
    int itemId = Menu.NONE;
    int order = Menu.NONE;
    ComponentName caller = getComponentName();
    Intent[] specifics = null;//from ww w  .ja  v  a2 s. co m
    Intent intent = new Intent();
    long id = ContentUris.parseId(getIntent().getData());
    intent.setDataAndType(JamendoContract.createPlaylistUri(JamendoContract.FORMAT_M3U, Albums.ID, id),
            JamendoContract.CONTENT_TYPE_M3U);
    intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
    int flags = 0;
    MenuItem[] outSpecificItems = null;
    menu.addIntentOptions(groupId, itemId, order, caller, specifics, intent, flags, outSpecificItems);
    return menu.hasVisibleItems();
}

From source file:com.example.conor.dotaapp.FetchMatchTask.java

/**
 * Helper method to handle insertion of a new location in the weather database.
 *
 * @param steamAccountId Steam account id of the player.
 * @param heroId id of the player's hero
 * @param playerSlot the position of the player on the team
 * @return the row ID of the added location.
 *//*from   ww w . ja va  2  s .  co  m*/
long addPlayer(String steamAccountId, int heroId, int playerSlot) {
    long steamId;

    // First, check if the location with this city name exists in the db
    Cursor playerCursor = mContext.getContentResolver().query(MatchContract.PlayerEntry.CONTENT_URI,
            new String[] { MatchContract.PlayerEntry._ID },
            MatchContract.PlayerEntry.COLUMN_ACCOUNT_ID + " = ?", new String[] { steamAccountId }, null);

    if (playerCursor.moveToFirst()) {
        int steamIdIndex = playerCursor.getColumnIndex(MatchContract.PlayerEntry._ID);
        steamId = playerCursor.getLong(steamIdIndex);
    } 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 playerValues = 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.
        playerValues.put(MatchContract.PlayerEntry.COLUMN_ACCOUNT_ID, steamAccountId);
        playerValues.put(MatchContract.PlayerEntry.COLUMN_HERO_ID, heroId);
        playerValues.put(MatchContract.PlayerEntry.COLUMN_P_SLOT, playerSlot);

        // Finally, insert location data into the database.
        Uri insertedUri = mContext.getContentResolver().insert(MatchContract.PlayerEntry.CONTENT_URI,
                playerValues);

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

    playerCursor.close();
    return steamId;
}

From source file:edu.msu.walajahi.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 ww . 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) {
    // 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;

    // First, check if the location with this city name exists 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 {
        // 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.ksk.droidbatterybooster.provider.TimeSchedule.java

/**
 * Creates a new time schedule and fills in the given schedule's id.
 *//* ww  w.j a va 2s  . co  m*/
public static long addTimeSchedule(Context context, TimeSchedule schedule) {
    ContentValues values = createContentValues(schedule);
    Uri uri = context.getContentResolver().insert(CONTENT_URI, values);
    schedule.id = (int) ContentUris.parseId(uri);
    setNextAction(context);
    return calculateTimeSchedule(schedule);
}

From source file:com.renjunzheng.vendingmachine.MyGcmListenerService.java

private void updateBalance(Bundle data) {

    ContentValues newValues = new ContentValues();
    newValues.put(DataContract.UserEntry.COLUMN_DISPLAY_NAME, data.getString("real_name"));
    newValues.put(DataContract.UserEntry.COLUMN_MONEY_LEFT, data.getString("balance"));

    String selection = DataContract.UserEntry.COLUMN_EMAIL + " =?";
    String[] selectionArgs = { data.getString("email") };
    int rowUpdated = getContentResolver().update(DataContract.UserEntry.CONTENT_URI, newValues, selection,
            selectionArgs);//from w ww  . ja v a 2s  .c o m
    if (rowUpdated == 0) {
        newValues.put(DataContract.UserEntry.COLUMN_EMAIL, data.getString("email"));
        Uri returnedUri = getContentResolver().insert(DataContract.UserEntry.CONTENT_URI, newValues);
        Log.i(TAG, "inserted row num " + ContentUris.parseId(returnedUri));
    } else {
        Log.i(TAG, "updated row num " + Integer.toString(rowUpdated));
    }
}

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

private void createNotification(Context context, Intent intent) {
    Uri data = intent.getData();/*from   w w w .  j a  v  a2 s . co m*/
    Habit habit = Habit.get(ContentUris.parseId(data));
    Long timestamp = intent.getLongExtra("timestamp", DateHelper.getStartOfToday());
    Long reminderTime = intent.getLongExtra("reminderTime", DateHelper.getStartOfToday());

    if (habit == null)
        return;
    if (habit.repetitions.hasImplicitRepToday())
        return;

    habit.highlight = 1;
    habit.save();

    if (!checkWeekday(intent, habit))
        return;

    // Check if reminder has been turned off after alarm was scheduled
    if (habit.reminderHour == null)
        return;

    Intent contentIntent = new Intent(context, MainActivity.class);
    contentIntent.setData(data);
    PendingIntent contentPendingIntent = PendingIntent.getActivity(context, 0, contentIntent, 0);

    PendingIntent dismissPendingIntent = buildDismissIntent(context);
    PendingIntent checkIntentPending = buildCheckIntent(context, habit, timestamp);
    PendingIntent snoozeIntentPending = buildSnoozeIntent(context, habit);

    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender()
            .setBackground(BitmapFactory.decodeResource(context.getResources(), R.drawable.stripe));

    Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(habit.name).setContentText(habit.description)
            .setContentIntent(contentPendingIntent).setDeleteIntent(dismissPendingIntent)
            .addAction(R.drawable.ic_action_check, context.getString(R.string.check), checkIntentPending)
            .addAction(R.drawable.ic_action_snooze, context.getString(R.string.snooze), snoozeIntentPending)
            .setSound(soundUri).extend(wearableExtender).setWhen(reminderTime).setShowWhen(true).build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Activity.NOTIFICATION_SERVICE);

    int notificationId = (int) (habit.getId() % Integer.MAX_VALUE);
    notificationManager.notify(notificationId, notification);
}

From source file:com.android.contacts.common.vcard.NotificationImportExportListener.java

@Override
public void onImportFinished(ImportRequest request, int jobId, Uri createdUri) {
    final String description = mContext.getString(R.string.importing_vcard_finished_title, request.displayName);
    final Intent intent;
    if (createdUri != null) {
        final long rawContactId = ContentUris.parseId(createdUri);
        final Uri contactUri = RawContacts.getContactLookupUri(mContext.getContentResolver(),
                ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
        intent = new Intent(Intent.ACTION_VIEW, contactUri);
    } else {//  w  w w  .  ja  va2s . c  o m
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    }
    intent.setPackage(mContext.getPackageName());
    final Notification notification = NotificationImportExportListener.constructFinishNotification(mContext,
            description, null, intent);
    mNotificationManager.notify(NotificationImportExportListener.DEFAULT_NOTIFICATION_TAG, jobId, notification);
}

From source file:com.example.rafa.sunshine.app.experiments.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 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) {
    long locationId;

    // First, check if the location with this city name exists 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);

    //Si ja existeix aquesta localitzaci obtenim el ID.
    if (locationCursor.moveToFirst()) { //ara estem al primer registre (en aquest cas l'nic)
        //1. obtenim l'ndex de la columna que ens interessa, en aquest camp el del ID.
        //2. a partir d'aquest ndex, o per a que s'entengui ms, a partir d'aquesta columna (en
        //el registre que estem tractant) agafem el seu valor, en aquest cas el ID.
        int locationIdIndex = locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID);//1
        locationId = locationCursor.getLong(locationIdIndex);//2

        //Si no crearem la localitzaci en la db.
    } 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.google.android.demos.jamendo.app.AlbumActivity.java

private void share() {
    long id = ContentUris.parseId(getIntent().getData());
    Uri uri = ContentUris.withAppendedId(BASE_URI, id);
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    Cursor cursor = mHeaderAdapter.getCursor();
    if (cursor != null && cursor.moveToFirst()) {
        String albumName = cursor.getString(cursor.getColumnIndexOrThrow(Albums.NAME));
        String artistName = cursor.getString(cursor.getColumnIndexOrThrow(Artists.NAME));
        String template = getString(R.string.jamendo_template_album_artist);
        String subject = MessageFormat.format(template, albumName, artistName);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    }//ww w  . j  av a2  s.co m
    intent.putExtra(Intent.EXTRA_TEXT, String.valueOf(uri));
    intent = Intent.createChooser(intent, null);
    startActivity(intent);
}