• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.content;
18 
19 import android.net.Uri;
20 
21 /**
22  * Utility methods useful for working with content {@link android.net.Uri}s,
23  * those with a "content" scheme.
24  */
25 public class ContentUris {
26 
27     /**
28      * Converts the last path segment to a long.
29      *
30      * <p>This supports a common convention for content URIs where an ID is
31      * stored in the last segment.
32      *
33      * @throws UnsupportedOperationException if this isn't a hierarchical URI
34      * @throws NumberFormatException if the last segment isn't a number
35      *
36      * @return the long conversion of the last segment or -1 if the path is
37      *  empty
38      */
parseId(Uri contentUri)39     public static long parseId(Uri contentUri) {
40         String last = contentUri.getLastPathSegment();
41         return last == null ? -1 : Long.parseLong(last);
42     }
43 
44     /**
45      * Appends the given ID to the end of the path.
46      *
47      * @param builder to append the ID to
48      * @param id to append
49      *
50      * @return the given builder
51      */
appendId(Uri.Builder builder, long id)52     public static Uri.Builder appendId(Uri.Builder builder, long id) {
53         return builder.appendEncodedPath(String.valueOf(id));
54     }
55 
56     /**
57      * Appends the given ID to the end of the path.
58      *
59      * @param contentUri to start with
60      * @param id to append
61      *
62      * @return a new URI with the given ID appended to the end of the path
63      */
withAppendedId(Uri contentUri, long id)64     public static Uri withAppendedId(Uri contentUri, long id) {
65         return appendId(contentUri.buildUpon(), id).build();
66     }
67 }
68