1 /* 2 * Copyright (C) 2014 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.tv.settings.util; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.content.Intent.ShortcutIconResource; 22 import android.content.res.Resources; 23 import android.net.Uri; 24 25 /** 26 * Utilities for working with URIs. 27 */ 28 public final class UriUtils { 29 30 private static final String SCHEME_SHORTCUT_ICON_RESOURCE = "shortcut.icon.resource"; 31 private static final String SCHEME_DELIMITER = "://"; 32 private static final String URI_PATH_DELIMITER = "/"; 33 private static final String URI_PACKAGE_DELIMITER = ":"; 34 private static final String HTTP_PREFIX = "http"; 35 private static final String HTTPS_PREFIX = "https"; 36 private static final String SCHEME_ACCOUNT_IMAGE = "image.account"; 37 private static final String ACCOUNT_IMAGE_CHANGE_NOTIFY_URI = "change_notify_uri"; 38 39 /** 40 * Non instantiable. 41 */ 42 private UriUtils() {} 43 44 /** 45 * get resource uri representation for a resource of a package 46 */ 47 public static String getAndroidResourceUri(Context context, int resourceId) { 48 return getAndroidResourceUri(context.getResources(), resourceId); 49 } 50 51 /** 52 * get resource uri representation for a resource 53 */ 54 public static String getAndroidResourceUri(Resources resources, int resourceId) { 55 return ContentResolver.SCHEME_ANDROID_RESOURCE 56 + SCHEME_DELIMITER + resources.getResourceName(resourceId) 57 .replace(URI_PACKAGE_DELIMITER, URI_PATH_DELIMITER); 58 } 59 60 /** 61 * Gets a URI with short cut icon scheme. 62 */ 63 public static Uri getShortcutIconResourceUri(ShortcutIconResource iconResource) { 64 return Uri.parse(SCHEME_SHORTCUT_ICON_RESOURCE + SCHEME_DELIMITER + iconResource.packageName 65 + URI_PATH_DELIMITER 66 + iconResource.resourceName.replace(URI_PACKAGE_DELIMITER, URI_PATH_DELIMITER)); 67 } 68 69 /** 70 * Checks if the URI refers to an Android resource. 71 */ 72 public static boolean isAndroidResourceUri(Uri uri) { 73 return ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme()); 74 } 75 76 /** 77 * Gets a URI with the account image scheme, and specifying an URI to be 78 * used in notifyChange() when the image pointed to by the returned URI is 79 * updated. 80 */ 81 public static Uri getAccountImageUri(String accountName, Uri changeNotifyUri) { 82 Uri uri = Uri.parse(SCHEME_ACCOUNT_IMAGE + SCHEME_DELIMITER + accountName); 83 if (changeNotifyUri != null) { 84 uri = uri.buildUpon().appendQueryParameter(ACCOUNT_IMAGE_CHANGE_NOTIFY_URI, 85 changeNotifyUri.toString()).build(); 86 } 87 return uri; 88 } 89 90 /** 91 * Checks if the URI refers to an account image. 92 */ 93 public static boolean isAccountImageUri(Uri uri) { 94 return uri != null && SCHEME_ACCOUNT_IMAGE.equals(uri.getScheme()); 95 } 96 97 public static String getAccountName(Uri uri) { 98 if (isAccountImageUri(uri)) { 99 return uri.getAuthority() + uri.getPath(); 100 } else { 101 throw new IllegalArgumentException("Invalid account image URI. " + uri); 102 } 103 } 104 105 public static Uri getAccountImageChangeNotifyUri(Uri uri) { 106 if (isAccountImageUri(uri)) { 107 String notifyUri = uri.getQueryParameter(ACCOUNT_IMAGE_CHANGE_NOTIFY_URI); 108 if (notifyUri == null) { 109 return null; 110 } else { 111 return Uri.parse(notifyUri); 112 } 113 } else { 114 throw new IllegalArgumentException("Invalid account image URI. " + uri); 115 } 116 } 117 118 /** 119 * Returns {@code true} if the URI refers to a content URI which can be opened via 120 * {@link ContentResolver#openInputStream(Uri)}. 121 */ 122 public static boolean isContentUri(Uri uri) { 123 return ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) || 124 ContentResolver.SCHEME_FILE.equals(uri.getScheme()); 125 } 126 127 /** 128 * Checks if the URI refers to an shortcut icon resource. 129 */ 130 public static boolean isShortcutIconResourceUri(Uri uri) { 131 return SCHEME_SHORTCUT_ICON_RESOURCE.equals(uri.getScheme()); 132 } 133 134 /** 135 * Creates a shortcut icon resource object from an Android resource URI. 136 */ 137 public static ShortcutIconResource getIconResource(Uri uri) { 138 if(isAndroidResourceUri(uri)) { 139 ShortcutIconResource iconResource = new ShortcutIconResource(); 140 iconResource.packageName = uri.getAuthority(); 141 // Trim off the scheme + 3 extra for "://", then replace the first "/" with a ":" 142 iconResource.resourceName = uri.toString().substring( 143 ContentResolver.SCHEME_ANDROID_RESOURCE.length() + SCHEME_DELIMITER.length()) 144 .replaceFirst(URI_PATH_DELIMITER, URI_PACKAGE_DELIMITER); 145 return iconResource; 146 } else if(isShortcutIconResourceUri(uri)) { 147 ShortcutIconResource iconResource = new ShortcutIconResource(); 148 iconResource.packageName = uri.getAuthority(); 149 iconResource.resourceName = uri.toString().substring( 150 SCHEME_SHORTCUT_ICON_RESOURCE.length() + SCHEME_DELIMITER.length() 151 + iconResource.packageName.length() + URI_PATH_DELIMITER.length()) 152 .replaceFirst(URI_PATH_DELIMITER, URI_PACKAGE_DELIMITER); 153 return iconResource; 154 } else { 155 throw new IllegalArgumentException("Invalid resource URI. " + uri); 156 } 157 } 158 159 /** 160 * Returns {@code true} if this is a web URI. 161 */ 162 public static boolean isWebUri(Uri resourceUri) { 163 String scheme = resourceUri.getScheme() == null ? null 164 : resourceUri.getScheme().toLowerCase(); 165 return HTTP_PREFIX.equals(scheme) || HTTPS_PREFIX.equals(scheme); 166 } 167 168 } 169