Example usage for android.net Uri getPathSegments

List of usage examples for android.net Uri getPathSegments

Introduction

In this page you can find the example usage for android.net Uri getPathSegments.

Prototype

public abstract List<String> getPathSegments();

Source Link

Document

Gets the decoded path segments.

Usage

From source file:ca.mudar.parkcatcher.ui.activities.MainActivity.java

private void updateParkingTimeFromUri(Uri uri) {
    Log.v(TAG, "updateParkingTimeFromUri");
    List<String> pathSegments = uri.getPathSegments();

    // http://www.capteurdestationnement.com/map/search/2/15.5/12
    // http://www.capteurdestationnement.com/map/search/2/15.5/12/h2w2e7

    if ((pathSegments.size() >= 5) && (pathSegments.get(0).equals(Const.INTENT_EXTRA_URL_PATH_MAP))
            && (pathSegments.get(1).equals(Const.INTENT_EXTRA_URL_PATH_SEARCH))) {

        try {//  w  ww  . j a  v a 2s .co m
            final int day = Integer.valueOf(pathSegments.get(2));
            final double time = Double.valueOf(pathSegments.get(3));
            final int duration = Integer.valueOf(pathSegments.get(4));

            final int hourOfDay = (int) time;
            final int minute = (int) ((time - hourOfDay) * 60);

            GregorianCalendar calendar = new GregorianCalendar();

            calendar.set(Calendar.DAY_OF_WEEK, day == 7 ? Calendar.SUNDAY : day + 1);
            calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            calendar.set(Calendar.MINUTE, minute);

            parkingApp.setParkingCalendar(calendar);
            parkingApp.setParkingDuration(duration);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.opendatakit.common.android.provider.impl.InstanceProviderImpl.java

/**
 * This method removes the entry from the content provider, and also removes
 * any associated files. files: form.xml, [formmd5].formdef, formname
 * {directory}//from w ww . j a v a  2s. c o  m
 */
@Override
public int delete(Uri uri, String where, String[] whereArgs) {
    List<String> segments = uri.getPathSegments();

    if (segments.size() < 2 || segments.size() > 3) {
        throw new SQLException("Unknown URI (too many segments!) " + uri);
    }

    String appName = segments.get(0);
    ODKFileUtils.verifyExternalStorageAvailability();
    ODKFileUtils.assertDirectoryStructure(appName);
    String tableId = segments.get(1);
    // _ID in UPLOADS_TABLE_NAME
    String instanceId = (segments.size() == 3 ? segments.get(2) : null);

    SQLiteDatabase db = null;
    List<IdStruct> idStructs = new ArrayList<IdStruct>();
    try {
        db = DatabaseFactory.get().getDatabase(getContext(), appName);
        db.beginTransaction();

        boolean success = false;
        try {
            success = ODKDatabaseUtils.get().hasTableId(db, tableId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SQLException("Unknown URI (exception testing for tableId) " + uri);
        }
        if (!success) {
            throw new SQLException("Unknown URI (missing data table for tableId) " + uri);
        }

        String dbTableName = "\"" + tableId + "\"";

        if (segments.size() == 2) {
            where = "(" + where + ") AND (" + InstanceColumns.DATA_INSTANCE_ID + "=? )";
            if (whereArgs != null) {
                String[] args = new String[whereArgs.length + 1];
                for (int i = 0; i < whereArgs.length; ++i) {
                    args[i] = whereArgs[i];
                }
                args[whereArgs.length] = instanceId;
                whereArgs = args;
            } else {
                whereArgs = new String[] { instanceId };
            }
        }

        Cursor del = null;
        try {
            del = this.query(uri, null, where, whereArgs, null);
            del.moveToPosition(-1);
            while (del.moveToNext()) {
                String iId = ODKDatabaseUtils.get().getIndexAsString(del,
                        del.getColumnIndex(InstanceColumns._ID));
                String iIdDataTable = ODKDatabaseUtils.get().getIndexAsString(del,
                        del.getColumnIndex(InstanceColumns.DATA_INSTANCE_ID));
                idStructs.add(new IdStruct(iId, iIdDataTable));
                String path = ODKFileUtils.getInstanceFolder(appName, tableId, iIdDataTable);
                File f = new File(path);
                if (f.exists()) {
                    if (f.isDirectory()) {
                        FileUtils.deleteDirectory(f);
                    } else {
                        f.delete();
                    }
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Unable to delete instance directory: " + e.toString());
        } finally {
            if (del != null) {
                del.close();
            }
        }

        for (IdStruct idStruct : idStructs) {
            db.delete(DatabaseConstants.UPLOADS_TABLE_NAME, InstanceColumns.DATA_INSTANCE_ID + "=?",
                    new String[] { idStruct.idUploadsTable });
            db.delete(dbTableName, DATA_TABLE_ID_COLUMN + "=?", new String[] { idStruct.idDataTable });
        }
        db.setTransactionSuccessful();
    } finally {
        if (db != null) {
            db.endTransaction();
            db.close();
        }
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return idStructs.size();
}

From source file:ca.mudar.parkcatcher.ui.fragments.DetailsFragment.java

private int getIdFromUri(Uri uri) {
    int postId = -1;

    List<String> pathSegments = uri.getPathSegments();

    if ((pathSegments.size() == 5) && (pathSegments.get(0).equals(Const.INTENT_EXTRA_URL_PATH_POST_ID))) {

        try {/*from   ww w.  j  av  a2s  .  c  o m*/
            postId = Integer.parseInt(pathSegments.get(1));
            final int day = Integer.valueOf(pathSegments.get(2));
            final double time = Double.valueOf(pathSegments.get(3));
            final int duration = Integer.valueOf(pathSegments.get(4));

            final int hourOfDay = (int) time;
            final int minute = (int) ((time - hourOfDay) * 60);

            GregorianCalendar calendar = new GregorianCalendar();

            calendar.set(Calendar.DAY_OF_WEEK, day == 7 ? Calendar.SUNDAY : day + 1);
            calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            calendar.set(Calendar.MINUTE, minute);

            parkingApp.setParkingCalendar(calendar);
            // parkingApp.setParkingTime(hourOfDay, minute);
            parkingApp.setParkingDuration(duration);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }

    return postId;
}

From source file:org.opendatakit.common.android.provider.impl.InstanceProviderImpl.java

@Override
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
    List<String> segments = uri.getPathSegments();

    if (segments.size() != 3) {
        throw new SQLException("Unknown URI (does not specify instance!) " + uri);
    }/*from  www.  j  a v a  2  s  . c o m*/

    String appName = segments.get(0);
    ODKFileUtils.verifyExternalStorageAvailability();
    ODKFileUtils.assertDirectoryStructure(appName);

    String tableId = segments.get(1);
    // _ID in UPLOADS_TABLE_NAME
    String instanceId = segments.get(2);

    SQLiteDatabase db = null;
    int count = 0;
    try {
        db = DatabaseFactory.get().getDatabase(getContext(), appName);

        boolean success = false;
        try {
            success = ODKDatabaseUtils.get().hasTableId(db, tableId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SQLException("Unknown URI (exception testing for tableId) " + uri);
        }
        if (!success) {
            throw new SQLException("Unknown URI (missing data table for tableId) " + uri);
        }

        String dbTableName = "\"" + tableId + "\"";

        // run the query to get all the ids...
        List<IdStruct> idStructs = new ArrayList<IdStruct>();
        Cursor ref = null;
        try {
            // use this provider's query interface to get the set of ids that
            // match (if any)
            ref = this.query(uri, null, where, whereArgs, null);
            if (ref.getCount() != 0) {
                ref.moveToFirst();
                do {
                    String iId = ODKDatabaseUtils.get().getIndexAsString(ref,
                            ref.getColumnIndex(InstanceColumns._ID));
                    String iIdDataTable = ODKDatabaseUtils.get().getIndexAsString(ref,
                            ref.getColumnIndex(InstanceColumns.DATA_INSTANCE_ID));
                    idStructs.add(new IdStruct(iId, iIdDataTable));
                } while (ref.moveToNext());
            }
        } finally {
            if (ref != null) {
                ref.close();
            }
        }

        // update the values string...
        if (values.containsKey(InstanceColumns.XML_PUBLISH_STATUS)) {
            Date xmlPublishDate = new Date();
            values.put(InstanceColumns.XML_PUBLISH_TIMESTAMP,
                    TableConstants.nanoSecondsFromMillis(xmlPublishDate.getTime()));
            String xmlPublishStatus = values.getAsString(InstanceColumns.XML_PUBLISH_STATUS);
            if (values.containsKey(InstanceColumns.DISPLAY_SUBTEXT) == false) {
                String text = getDisplaySubtext(xmlPublishStatus, xmlPublishDate);
                values.put(InstanceColumns.DISPLAY_SUBTEXT, text);
            }
        }

        db.beginTransaction();
        String[] args = new String[1];
        for (IdStruct idStruct : idStructs) {
            args[0] = idStruct.idUploadsTable;
            count += db.update(DatabaseConstants.UPLOADS_TABLE_NAME, values, InstanceColumns._ID + "=?", args);
        }
        db.setTransactionSuccessful();
    } finally {
        if (db != null) {
            db.endTransaction();
            db.close();
        }
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}

From source file:com.android.messaging.datamodel.BugleNotifications.java

/**
 * Returns the thumbnailUri from the avatar URI, or null if avatar URI does not have thumbnail.
 *//*from   w  w  w . j a v  a 2 s  .c om*/
private static Uri getThumbnailUri(final Uri avatarUri) {
    Uri localUri = null;
    final String avatarType = AvatarUriUtil.getAvatarType(avatarUri);
    if (TextUtils.equals(avatarType, AvatarUriUtil.TYPE_LOCAL_RESOURCE_URI)) {
        localUri = AvatarUriUtil.getPrimaryUri(avatarUri);
    } else if (UriUtil.isLocalResourceUri(avatarUri)) {
        localUri = avatarUri;
    }
    if (localUri != null && localUri.getAuthority().equals(ContactsContract.AUTHORITY)) {
        // Contact photos are of the form: content://com.android.contacts/contacts/123/photo
        final List<String> pathParts = localUri.getPathSegments();
        if (pathParts.size() == 3 && pathParts.get(2).equals(Contacts.Photo.CONTENT_DIRECTORY)) {
            return localUri;
        }
    }
    return null;
}

From source file:com.example.android.notepad.CMNotesProvider.java

@Override
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
    //        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int count;//ww w .  j  a  v a  2  s . c om
    CMAdapter cmAdapter = new CMAdapter();

    switch (sUriMatcher.match(uri)) {
    case NOTES:
        //count = db.update(NOTES_TABLE_NAME, values, where, whereArgs);
        count = 0;
        break;

    case NOTE_ID:
        String noteId = uri.getPathSegments().get(1);
        //count = db.update(NOTES_TABLE_NAME, values, Notes._ID + "=" + noteId
        //        + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
        cmAdapter.updateValue(noteId, values);
        count = 1;
        break;

    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}

From source file:org.mozilla.labs.Soup.provider.AppsProvider.java

@Override
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int count;/*from  www.j  ava2  s  .co  m*/

    Log.d(TAG, "Update " + values.toString());

    switch (sUriMatcher.match(uri)) {
    case APPS:
        count = db.update(APPS_TABLE_NAME, values, where, whereArgs);
        break;

    case APP_ID:
        String appId = uri.getPathSegments().get(1);

        // values.put(AppsContract.Apps.MODIFIED_DATE,
        // Long.valueOf(System.currentTimeMillis()));

        count = db.update(APPS_TABLE_NAME, values,
                Apps._ID + "=" + appId + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
        break;

    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}

From source file:edu.stanford.mobisocial.dungbeetle.DungBeetleContentProvider.java

private boolean match(Uri uri, String... regexes) {
    List<String> segs = uri.getPathSegments();
    if (segs.size() == regexes.length) {
        for (int i = 0; i < regexes.length; i++) {
            if (!segs.get(i).matches(regexes[i])) {
                return false;
            }//from  w  ww .  j  a  va  2  s .  c om
        }
        return true;
    }
    return false;
}

From source file:com.example.android.notepad.CMNotesProvider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    String[] keys = null;// w  w  w.ja v  a  2  s .co m

    switch (sUriMatcher.match(uri)) {
    case NOTES:
        //qb.setProjectionMap(sNotesProjectionMap);
        break;

    case NOTE_ID:
        //qb.setProjectionMap(sNotesProjectionMap);
        //qb.appendWhere(Notes._ID + "=" + uri.getPathSegments().get(1));
        keys = new String[] { uri.getPathSegments().get(1) };
        break;

    case LIVE_FOLDER_NOTES:
        //qb.setProjectionMap(sLiveFolderProjectionMap);
        break;

    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }

    // Get the database and run the query
    //SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    //Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);

    CMAdapter cmadapter = new CMAdapter();
    JSONObject objects = cmadapter.getValues(keys);
    MatrixCursor c = new MatrixCursor(projection);

    // objects is a map of key => value mappings, where key is the _id
    Iterator<String> note_ids = objects.keys();
    while (note_ids.hasNext()) {
        String id = note_ids.next();
        RowBuilder row = c.newRow();
        for (String field : projection) {
            if (field.equals(Notes._ID)) {
                row.add(id);
            } else {
                String val = "bad entry";
                try {
                    val = objects.getJSONObject(id).getString(field);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                row.add(val);
            }
        }
    }

    // Tell the cursor what uri to watch, so it knows when its source data changes
    c.setNotificationUri(getContext().getContentResolver(), uri);
    return c;
}

From source file:org.mariotaku.twidere.provider.TweetStoreProvider.java

@Override
public int delete(final Uri uri, final String selection, final String[] selectionArgs) {
    final String table = getTableNameForContentUri(uri);
    if (getTableId(uri) == URI_NOTIFICATIONS) {
        final List<String> segments = uri.getPathSegments();
        if (segments.size() != 2)
            return 0;
        clearNotification(parseInt(segments.get(1)));
    }/*from  ww  w  .j  a  va 2 s  . c o  m*/
    if (table == null)
        return 0;
    final int result = mDatabase.delete(table, selection, selectionArgs);
    if (result > 0) {
        onDatabaseUpdated(uri);
    }
    return result;
}