blob: 41ef62f67b2655019fceb3e1fdd9c5c9b3221a67 [file] [log] [blame]
Chiao Cheng86618002012-10-16 13:21:10 -07001/*
2 * Copyright (C) 2011 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
17package com.android.contacts.common.util;
18
19import android.net.Uri;
Makoto Onukia65f2ad2015-03-10 12:54:34 -070020import android.provider.ContactsContract;
Chiao Cheng86618002012-10-16 13:21:10 -070021
Tyler Gunn9e0c14e2015-08-04 13:33:49 -070022import java.util.List;
23
Chiao Cheng86618002012-10-16 13:21:10 -070024/**
25 * Utility methods for dealing with URIs.
26 */
27public class UriUtils {
28 /** Static helper, not instantiable. */
29 private UriUtils() {}
30
31 /** Checks whether two URI are equal, taking care of the case where either is null. */
32 public static boolean areEqual(Uri uri1, Uri uri2) {
33 if (uri1 == null && uri2 == null) {
34 return true;
35 }
36 if (uri1 == null || uri2 == null) {
37 return false;
38 }
39 return uri1.equals(uri2);
40 }
41
42 /** Parses a string into a URI and returns null if the given string is null. */
43 public static Uri parseUriOrNull(String uriString) {
44 if (uriString == null) {
45 return null;
46 }
47 return Uri.parse(uriString);
48 }
49
50 /** Converts a URI into a string, returns null if the given URI is null. */
51 public static String uriToString(Uri uri) {
52 return uri == null ? null : uri.toString();
53 }
Yorke Lee9c85bc32013-09-09 14:42:46 -070054
55 public static boolean isEncodedContactUri(Uri uri) {
Jay Shrauner949d3fb2014-07-02 09:48:31 -070056 if (uri == null) {
57 return false;
58 }
59 final String lastPathSegment = uri.getLastPathSegment();
60 if (lastPathSegment == null) {
61 return false;
62 }
63 return lastPathSegment.equals(Constants.LOOKUP_URI_ENCODED);
Yorke Lee9c85bc32013-09-09 14:42:46 -070064 }
Makoto Onukia65f2ad2015-03-10 12:54:34 -070065
66 /**
67 * @return {@code uri} as-is if the authority is of contacts provider. Otherwise
68 * or {@code uri} is null, return null otherwise
69 */
70 public static Uri nullForNonContactsUri(Uri uri) {
71 if (uri == null) {
72 return null;
73 }
74 return ContactsContract.AUTHORITY.equals(uri.getAuthority()) ? uri : null;
75 }
Tyler Gunn9e0c14e2015-08-04 13:33:49 -070076
77 /**
78 * Parses the given URI to determine the original lookup key of the contact.
79 */
80 public static String getLookupKeyFromUri(Uri lookupUri) {
81 // Would be nice to be able to persist the lookup key somehow to avoid having to parse
82 // the uri entirely just to retrieve the lookup key, but every uri is already parsed
83 // once anyway to check if it is an encoded JSON uri, so this has negligible effect
84 // on performance.
85 if (lookupUri != null && !UriUtils.isEncodedContactUri(lookupUri)) {
86 final List<String> segments = lookupUri.getPathSegments();
87 // This returns the third path segment of the uri, where the lookup key is located.
88 // See {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
89 return (segments.size() < 3) ? null : Uri.encode(segments.get(2));
90 } else {
91 return null;
92 }
93 }
Chiao Cheng86618002012-10-16 13:21:10 -070094}