• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.contacts.common.util;
18 
19 import android.net.Uri;
20 import android.provider.ContactsContract;
21 import java.util.List;
22 
23 /** Utility methods for dealing with URIs. */
24 public class UriUtils {
25 
26   /** Static helper, not instantiable. */
UriUtils()27   private UriUtils() {}
28 
29   /** Checks whether two URI are equal, taking care of the case where either is null. */
areEqual(Uri uri1, Uri uri2)30   public static boolean areEqual(Uri uri1, Uri uri2) {
31     if (uri1 == null && uri2 == null) {
32       return true;
33     }
34     if (uri1 == null || uri2 == null) {
35       return false;
36     }
37     return uri1.equals(uri2);
38   }
39 
40   /** Parses a string into a URI and returns null if the given string is null. */
parseUriOrNull(String uriString)41   public static Uri parseUriOrNull(String uriString) {
42     if (uriString == null) {
43       return null;
44     }
45     return Uri.parse(uriString);
46   }
47 
48   /** Converts a URI into a string, returns null if the given URI is null. */
uriToString(Uri uri)49   public static String uriToString(Uri uri) {
50     return uri == null ? null : uri.toString();
51   }
52 
isEncodedContactUri(Uri uri)53   public static boolean isEncodedContactUri(Uri uri) {
54     if (uri == null) {
55       return false;
56     }
57     final String lastPathSegment = uri.getLastPathSegment();
58     if (lastPathSegment == null) {
59       return false;
60     }
61     return lastPathSegment.equals(Constants.LOOKUP_URI_ENCODED);
62   }
63 
64   /**
65    * @return {@code uri} as-is if the authority is of contacts provider. Otherwise or {@code uri} is
66    *     null, return null otherwise
67    */
nullForNonContactsUri(Uri uri)68   public static Uri nullForNonContactsUri(Uri uri) {
69     if (uri == null) {
70       return null;
71     }
72     return ContactsContract.AUTHORITY.equals(uri.getAuthority()) ? uri : null;
73   }
74 
75   /** Parses the given URI to determine the original lookup key of the contact. */
getLookupKeyFromUri(Uri lookupUri)76   public static String getLookupKeyFromUri(Uri lookupUri) {
77     // Would be nice to be able to persist the lookup key somehow to avoid having to parse
78     // the uri entirely just to retrieve the lookup key, but every uri is already parsed
79     // once anyway to check if it is an encoded JSON uri, so this has negligible effect
80     // on performance.
81     if (lookupUri != null && !UriUtils.isEncodedContactUri(lookupUri)) {
82       final List<String> segments = lookupUri.getPathSegments();
83       // This returns the third path segment of the uri, where the lookup key is located.
84       // See {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
85       return (segments.size() < 3) ? null : Uri.encode(segments.get(2));
86     } else {
87       return null;
88     }
89   }
90 }
91