Example usage for android.content ContentValues ContentValues

List of usage examples for android.content ContentValues ContentValues

Introduction

In this page you can find the example usage for android.content ContentValues ContentValues.

Prototype

public ContentValues() 

Source Link

Document

Creates an empty set of values using the default initial size

Usage

From source file:com.home883.ali.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.
 */
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 locationCursor = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI, null,
            WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?", new String[] { locationSetting },
            null);

    long locationRow;

    if (locationCursor.moveToFirst()) {
        locationRow = locationCursor.getLong(locationCursor.getColumnIndex(WeatherContract.LocationEntry._ID));
    } 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 locationUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                values);
        locationRow = ContentUris.parseId(locationUri);
    }

    return locationRow;
}

From source file:com.zegoggles.smssync.mail.MessageConverter.java

public @NotNull ContentValues messageToContentValues(final Message message)
        throws IOException, MessagingException {
    if (message == null)
        throw new MessagingException("message is null");

    final ContentValues values = new ContentValues();
    switch (getDataType(message)) {
    case SMS:/* w  ww. jav a2s .  c  o  m*/
        if (message.getBody() == null)
            throw new MessagingException("body is null");

        InputStream is = MimeUtility.decodeBody(message.getBody());
        if (is == null) {
            throw new MessagingException("body.getInputStream() is null for " + message.getBody());
        }
        final String body = IOUtils.toString(is);
        final String address = Headers.get(message, Headers.ADDRESS);
        values.put(SmsConsts.BODY, body);
        values.put(SmsConsts.ADDRESS, address);
        values.put(SmsConsts.TYPE, Headers.get(message, Headers.TYPE));
        values.put(SmsConsts.PROTOCOL, Headers.get(message, Headers.PROTOCOL));
        values.put(SmsConsts.SERVICE_CENTER, Headers.get(message, Headers.SERVICE_CENTER));
        values.put(SmsConsts.DATE, Headers.get(message, Headers.DATE));
        values.put(SmsConsts.STATUS, Headers.get(message, Headers.STATUS));
        values.put(SmsConsts.THREAD_ID, threadHelper.getThreadId(mContext, address));
        values.put(SmsConsts.READ, mMarkAsReadOnRestore ? "1" : Headers.get(message, Headers.READ));
        break;
    case CALLLOG:
        values.put(CallLog.Calls.NUMBER, Headers.get(message, Headers.ADDRESS));
        values.put(CallLog.Calls.TYPE, Integer.valueOf(Headers.get(message, Headers.TYPE)));
        values.put(CallLog.Calls.DATE, Headers.get(message, Headers.DATE));
        values.put(CallLog.Calls.DURATION, Long.valueOf(Headers.get(message, Headers.DURATION)));
        values.put(CallLog.Calls.NEW, 0);

        PersonRecord record = mPersonLookup.lookupPerson(Headers.get(message, Headers.ADDRESS));
        if (!record.isUnknown()) {
            values.put(CallLog.Calls.CACHED_NAME, record.getName());
            values.put(CallLog.Calls.CACHED_NUMBER_TYPE, -2);
        }

        break;
    default:
        throw new MessagingException("don't know how to restore " + getDataType(message));
    }

    return values;
}

From source file:com.example.debra.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  a2 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
    long locationId;
    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()) {
        locationId = cursor.getLong(cursor.getColumnIndex(WeatherContract.LocationEntry._ID));
    } else {
        ContentValues value = new ContentValues();
        value.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
        value.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
        value.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
        value.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);

        Uri insertedUri = mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI,
                value);
        locationId = ContentUris.parseId(insertedUri);
    }
    cursor.close();
    return locationId;
}

From source file:com.goliathonline.android.kegbot.io.RemoteDrinksHandler.java

private static ContentValues queryDrinkDetails(Uri uri, ContentResolver resolver) {
    final ContentValues values = new ContentValues();
    final Cursor cursor = resolver.query(uri, SessionsQuery.PROJECTION, null, null, null);
    try {/*from   w w  w . j av  a  2  s .co m*/
        if (cursor.moveToFirst()) {
            values.put(SyncColumns.UPDATED, cursor.getLong(SessionsQuery.UPDATED));
            values.put(Drinks.DRINK_STARRED, cursor.getInt(SessionsQuery.STARRED));
        } else {
            values.put(SyncColumns.UPDATED, KegbotContract.UPDATED_NEVER);
        }
    } finally {
        cursor.close();
    }
    return values;
}

From source file:app.com.example.kiran.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.
 *//*ww  w  .  j  av  a 2 s  .c  o  m*/
public 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.iyedb.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.
 *///  www.  j av  a2  s.co  m
private long insertLocationInDatabase(String locationSetting, String cityName, double lat, double lon) {

    Log.v(LOG_TAG, "inserting " + cityName + ", with coord: " + lat + ", " + 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()) {
        Log.v(LOG_TAG, "Found it in the database!");
        int locationIdIndex = cursor.getColumnIndex(LocationEntry._ID);
        return cursor.getLong(locationIdIndex);
    } else {
        Log.v(LOG_TAG, "Didn't find it in the database, inserting now!");
        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.kwang.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.
 */
private long addLocation(String locationSetting, String cityName, double lat, double lon) {

    Log.v(LOG_TAG, "inserting " + cityName + ", with coord: " + lat + ", " + 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()) {
        Log.v(LOG_TAG, "Found it in the database!");
        int locationIdIndex = cursor.getColumnIndex(LocationEntry._ID);
        return cursor.getLong(locationIdIndex);
    } else {
        Log.v(LOG_TAG, "Didn't find it in the database, inserting now!");
        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.acrutiapps.browser.tasks.HistoryBookmarksImportTask.java

private String readAsJSON(File file) {
    List<ContentValues> insertValues = null;

    try {//www . j  av a 2s.co  m
        insertValues = new ArrayList<ContentValues>();

        publishProgress(1, 0, 0);

        FileInputStream fis = new FileInputStream(file);

        StringBuilder sb = new StringBuilder();
        String line;

        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(fis, "UTF-8"));

            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
                return e.getMessage();
            }
        }

        JSONObject data = new JSONObject(sb.toString());

        Map<Long, Folder> folders = new HashMap<Long, Folder>();

        if (data.has("folders")) {
            JSONArray foldersArray = data.getJSONArray("folders");

            int progress = 0;
            int total = foldersArray.length();

            for (int i = 0; i < foldersArray.length(); i++) {

                publishProgress(3, progress, total);

                JSONObject folder = foldersArray.getJSONObject(i);

                long id = folder.getLong("id");
                long parentId = folder.getLong("parentId");
                String title = URLDecoder.decode(folder.getString("title"), "UTF-8");

                ContentValues values = new ContentValues();
                values.put(BookmarksProvider.Columns.TITLE, title);
                values.put(BookmarksProvider.Columns.BOOKMARK, 0);
                values.put(BookmarksProvider.Columns.IS_FOLDER, 1);
                values.put(BookmarksProvider.Columns.PARENT_FOLDER_ID, -1);

                Uri insertionUri = mContext.getContentResolver().insert(BookmarksProvider.BOOKMARKS_URI,
                        values);
                String insertionString = insertionUri.toString();

                // Get the new id for the current folder.
                long insertionId = -1;
                try {
                    insertionId = Long
                            .parseLong(insertionString.substring(insertionString.lastIndexOf('/') + 1));
                } catch (NumberFormatException e) {
                    insertionId = -1;
                }

                // Keep a relation between the id of the folder in the export file, its parent id (in the export file), and its new id.
                folders.put(id, new Folder(insertionId, parentId));

                progress++;
            }

            publishProgress(4, 0, 0);

            // Correct folders parent ids.
            if (!folders.isEmpty()) {
                for (Folder folder : folders.values()) {
                    // For each folder previously inserted, check if it had a parent folder in the export file.
                    long oldParentId = folder.getOldParentId();

                    if (oldParentId != -1) {
                        // Get the parent folder by its old Id, key of folders map.
                        Folder parentFolder = folders.get(oldParentId);
                        if (parentFolder != null) {

                            ContentValues values = new ContentValues();
                            values.put(BookmarksProvider.Columns.PARENT_FOLDER_ID, parentFolder.getNewId());

                            String whereClause = BookmarksProvider.Columns._ID + " = " + folder.getNewId();

                            mContext.getContentResolver().update(BookmarksProvider.BOOKMARKS_URI, values,
                                    whereClause, null);
                        }
                    }
                }
            }
        }

        if (data.has("bookmarks")) {
            JSONArray bookmarksArray = data.getJSONArray("bookmarks");

            int progress = 0;
            int total = bookmarksArray.length();

            for (int i = 0; i < bookmarksArray.length(); i++) {

                publishProgress(5, progress, total);

                JSONObject bookmark = bookmarksArray.getJSONObject(i);

                long folderId = bookmark.getLong("folderId");
                Folder parentFolder = null;
                if (folderId != -1) {
                    parentFolder = folders.get(folderId);
                }

                String title = URLDecoder.decode(bookmark.getString("title"), "UTF-8");
                String url = URLDecoder.decode(bookmark.getString("url"), "UTF-8");

                ContentValues values = createContentValues(title, url, bookmark.getInt("visits"),
                        bookmark.getLong("visitedDate"), bookmark.getLong("creationDate"), 1);

                if (parentFolder != null) {
                    values.put(BookmarksProvider.Columns.PARENT_FOLDER_ID, parentFolder.getNewId());
                }

                insertValues.add(values);

                progress++;
            }
        }

        if (data.has("history")) {
            JSONArray historyArray = data.getJSONArray("history");

            int progress = 0;
            int total = historyArray.length();

            for (int i = 0; i < historyArray.length(); i++) {

                publishProgress(6, progress, total);

                JSONObject history = historyArray.getJSONObject(i);

                String title = URLDecoder.decode(history.getString("title"), "UTF-8");
                String url = URLDecoder.decode(history.getString("url"), "UTF-8");

                ContentValues values = createContentValues(title, url, history.getInt("visits"),
                        history.getLong("visitedDate"), 0, 0);

                insertValues.add(values);

                progress++;
            }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return e.getMessage();
    } catch (JSONException e) {
        e.printStackTrace();
        return e.getMessage();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return e.getMessage();
    }

    if (insertValues != null) {
        publishProgress(7, 0, 0);
        mContext.getContentResolver().bulkInsert(BookmarksProvider.BOOKMARKS_URI,
                insertValues.toArray(new ContentValues[insertValues.size()]));
    }

    return null;
}

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/*from  ww w. ja  va  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;

    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.example.sunshine.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  ava2 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

    try {
        Cursor c = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI,
                new String[] { WeatherContract.LocationEntry._ID },
                WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
                new String[] { locationSetting }, null);
        if (!c.moveToFirst()) {
            ContentValues testValues = new ContentValues();
            testValues.put(WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING, locationSetting);
            testValues.put(WeatherContract.LocationEntry.COLUMN_CITY_NAME, cityName);
            testValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LAT, lat);
            testValues.put(WeatherContract.LocationEntry.COLUMN_COORD_LONG, lon);
            mContext.getContentResolver().insert(WeatherContract.LocationEntry.CONTENT_URI, testValues);
            c = mContext.getContentResolver().query(WeatherContract.LocationEntry.CONTENT_URI,
                    new String[] { WeatherContract.LocationEntry._ID },
                    WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + " = ?",
                    new String[] { locationSetting }, null);
            c.moveToFirst();
        }

        return c.getLong(c.getColumnIndex(WeatherContract.LocationEntry._ID));

    } catch (Exception E) {
        return -1;
    }
}