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 /** 23 * Utility methods for dealing with URIs. 24 */ 25 public class UriUtils { 26 /** Static helper, not instantiable. */ 27 private UriUtils() {} 28 29 /** Checks whether two URI are equal, taking care of the case where either is null. */ 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. */ 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. */ 49 public static String uriToString(Uri uri) { 50 return uri == null ? null : uri.toString(); 51 } 52 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 66 * or {@code uri} is null, return null otherwise 67 */ 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