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.dialer.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   private static final String LOOKUP_URI_ENCODED = "encoded";
27 
28   /** Static helper, not instantiable. */
UriUtils()29   private UriUtils() {}
30 
31   /** Checks whether two URI are equal, taking care of the case where either is null. */
areEqual(Uri uri1, Uri uri2)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. */
parseUriOrNull(String uriString)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. */
uriToString(Uri uri)51   public static String uriToString(Uri uri) {
52     return uri == null ? null : uri.toString();
53   }
54 
isEncodedContactUri(Uri uri)55   public static boolean isEncodedContactUri(Uri uri) {
56     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(LOOKUP_URI_ENCODED);
64   }
65 
66   /**
67    * @return {@code uri} as-is if the authority is of contacts provider. Otherwise or {@code uri} is
68    *     null, return null otherwise
69    */
nullForNonContactsUri(Uri uri)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   }
76 
77   /** Parses the given URI to determine the original lookup key of the contact. */
getLookupKeyFromUri(Uri lookupUri)78   public static String getLookupKeyFromUri(Uri lookupUri) {
79     // Would be nice to be able to persist the lookup key somehow to avoid having to parse
80     // the uri entirely just to retrieve the lookup key, but every uri is already parsed
81     // once anyway to check if it is an encoded JSON uri, so this has negligible effect
82     // on performance.
83     if (lookupUri != null && !UriUtils.isEncodedContactUri(lookupUri)) {
84       final List<String> segments = lookupUri.getPathSegments();
85       // This returns the third path segment of the uri, where the lookup key is located.
86       // See {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
87       return (segments.size() < 3) ? null : Uri.encode(segments.get(2));
88     } else {
89       return null;
90     }
91   }
92 }
93