Home | History | Annotate | Download | only in util
      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 
     22 import java.util.List;
     23 
     24 /**
     25  * Utility methods for dealing with URIs.
     26  */
     27 public 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     }
     54 
     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(Constants.LOOKUP_URI_ENCODED);
     64     }
     65 
     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     }
     76 
     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     }
     94 }
     95