private static boolean isVideoUri(Uri uri) { return uri.getPathSegments().contains("video"); }
@Override public boolean handles(@NonNull Uri model) { return ContentResolver.SCHEME_FILE.equals(model.getScheme()) && !model.getPathSegments() .isEmpty() && ASSET_PATH_SEGMENT.equals(model.getPathSegments().get(0)); }
@DrawableRes private int findResourceIdFromResourceIdUri(Uri source) { List<String> segments = source.getPathSegments(); try { return Integer.parseInt(segments.get(RESOURCE_ID_SEGMENT_INDEX)); } catch (NumberFormatException e) { throw new IllegalArgumentException("Unrecognized Uri format: " + source, e); } } }
@Override public boolean canHandleRequest(@NonNull Request data) { Uri uri = data.uri; return uri != null && SCHEME_FILE.equals(uri.getScheme()) && !uri.getPathSegments().isEmpty() && ANDROID_ASSET.equals(uri.getPathSegments().get(0)); }
Uri data = getIntent().getData(); String scheme = data.getScheme(); // "http" String host = data.getHost(); // "twitter.com" List<String> params = data.getPathSegments(); String first = params.get(0); // "status" String second = params.get(1); // "1234"
@DrawableRes private int findResourceIdFromTypeAndNameResourceUri(Context context, Uri source) { List<String> segments = source.getPathSegments(); String packageName = source.getAuthority(); String typeName = segments.get(TYPE_PATH_SEGMENT_INDEX); String resourceName = segments.get(NAME_PATH_SEGMENT_INDEX); int result = context.getResources().getIdentifier(resourceName, typeName, packageName); if (result == MISSING_RESOURCE_ID) { result = Resources.getSystem().getIdentifier(resourceName, typeName, ANDROID_PACKAGE_NAME); } if (result == MISSING_RESOURCE_ID) { throw new IllegalArgumentException("Failed to find resource id for: " + source); } return result; }
@DrawableRes private int findResourceIdFromUri(Context context, Uri source) { List<String> segments = source.getPathSegments(); if (segments.size() == NAME_URI_PATH_SEGMENTS) { return findResourceIdFromTypeAndNameResourceUri(context, source); } else if (segments.size() == ID_PATH_SEGMENTS) { return findResourceIdFromResourceIdUri(source); } else { throw new IllegalArgumentException("Unrecognized Uri format: " + source); } }
private Card getCardFromUri(Uri uri, Collection col) { long noteId; int ord; noteId = Long.parseLong(uri.getPathSegments().get(1)); ord = Integer.parseInt(uri.getPathSegments().get(3)); return getCard(noteId, ord, col); }
private static Uri getOriginUri(Uri uri) { Uri.Builder b = uri.buildUpon(); b.encodedPath(null); List<String> segments = uri.getPathSegments(); for (int i = 0; i < segments.size() - 1; i++) { b.appendEncodedPath(segments.get(i)); } return b.build(); }
@Override public String getType(@NonNull Uri uri) { List<String> segments = uri.getPathSegments(); String accountUuid = segments.get(0); String id = segments.get(1); String mimeType = (segments.size() < 3) ? null : segments.get(2); return getType(accountUuid, id, mimeType); }
static int getResourceId(Resources resources, Request data) throws FileNotFoundException { if (data.resourceId != 0 || data.uri == null) { return data.resourceId; } String pkg = data.uri.getAuthority(); if (pkg == null) throw new FileNotFoundException("No package provided: " + data.uri); int id; List<String> segments = data.uri.getPathSegments(); if (segments == null || segments.isEmpty()) { throw new FileNotFoundException("No path segments: " + data.uri); } else if (segments.size() == 1) { try { id = Integer.parseInt(segments.get(0)); } catch (NumberFormatException e) { throw new FileNotFoundException("Last path segment is not a resource ID: " + data.uri); } } else if (segments.size() == 2) { String type = segments.get(0); String name = segments.get(1); id = resources.getIdentifier(name, type, pkg); } else { throw new FileNotFoundException("More than two path segments: " + data.uri); } return id; }
@Override public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException { List<String> segments = uri.getPathSegments(); String accountUuid = segments.get(0); String attachmentId = segments.get(1); ParcelFileDescriptor parcelFileDescriptor = openAttachment(accountUuid, attachmentId); if (parcelFileDescriptor == null) { throw new FileNotFoundException("Attachment missing or cannot be opened!"); } return parcelFileDescriptor; }
private Note getNoteFromUri(Uri uri, Collection col) { long noteId; noteId = Long.parseLong(uri.getPathSegments().get(1)); return col.getNote(noteId); }
@Override public void onClick(View v) { // 添加数据 Uri uri = Uri.parse("content://com.example.databasetest.provider/book"); ContentValues values = new ContentValues(); values.put("name", "A Clash of Kings"); values.put("author", "George Martin"); values.put("pages", 1040); values.put("price", 55.55); Uri newUri = getContentResolver().insert(uri, values); newId = newUri.getPathSegments().get(1); } });
@Override public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException { List<String> segments = uri.getPathSegments(); String messageReferenceString = segments.get(0); MessageReference messageReference = MessageReference.parse(messageReferenceString); ParcelFileDescriptor parcelFileDescriptor = openMessage(messageReference); if (parcelFileDescriptor == null) { throw new FileNotFoundException("Message missing or cannot be opened!"); } return parcelFileDescriptor; }
private long getModelIdFromUri(Uri uri, Collection col) { String modelIdSegment = uri.getPathSegments().get(1); long id; if (modelIdSegment.equals(FlashCardsContract.Model.CURRENT_MODEL_ID)) { id = col.getModels().current().optLong("id", -1); } else { try { id = Long.parseLong(uri.getPathSegments().get(1)); } catch (NumberFormatException e) { throw new IllegalArgumentException("Model ID must be either numeric or the String CURRENT_MODEL_ID"); } } return id; }
/** * Attempt to parse a {@link Repository} from the given {@link Uri} * * @param uri * @return {@link Repository} or null if unparseable */ public static Repository getRepository(Uri uri) { List<String> segments = uri.getPathSegments(); if (segments.size() < 2) { return null; } String repoOwner = segments.get(0); if (!RepositoryUtils.isValidOwner(repoOwner)) { return null; } String repoName = segments.get(1); if (!RepositoryUtils.isValidRepo(repoName)) { return null; } return InfoUtils.createRepoFromData(repoOwner, repoName); } }
@Override public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) { SQLiteDatabase db = databaseHelper.getWritableDatabase(); int result = 0; switch (matcher.match(uri)) { case TRANSACTIONS: result = db.delete(LocalCupboard.getInstance().getTable(HttpTransaction.class), selection, selectionArgs); break; case TRANSACTION: result = db.delete(LocalCupboard.getInstance().getTable(HttpTransaction.class), "_id = ?", new String[]{ uri.getPathSegments().get(1) }); break; } if (result > 0) { getContext().getContentResolver().notifyChange(uri, null); } return result; }
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) throws Exception { List<String> segments = uri.getPathSegments(); int accountId = Integer.parseInt(segments.get(1)); /* * This method below calls Account.getStats() which uses EmailProvider to do its work. * For this to work we need to clear the calling identity. Otherwise accessing * EmailProvider will fail because it's not exported so third-party apps can't access it * directly. */ long identityToken = Binder.clearCallingIdentity(); try { return getAccountUnread(accountId); } finally { Binder.restoreCallingIdentity(identityToken); } }
/** * Attempt to parse a {@link User} from the given {@link Uri} * * @param uri * @return {@link User} or null if unparseable */ public static User getUser(Uri uri) { List<String> segments = uri.getPathSegments(); if (segments.size() < 1) { return null; } String login = segments.get(0); if (!RepositoryUtils.isValidOwner(login)) { return null; } return User.builder() .login(login) .build(); } }