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.Intent.ShortcutIconResource; 21 import android.net.Uri; 22 23 /** 24 * Utilities for working with URIs. 25 */ 26 public final class UriUtils { 27 28 private static final String SCHEME_SHORTCUT_ICON_RESOURCE = "shortcut.icon.resource"; 29 private static final String SCHEME_DELIMITER = "://"; 30 private static final String URI_PATH_DELIMITER = "/"; 31 private static final String URI_PACKAGE_DELIMITER = ":"; 32 private static final String HTTP_PREFIX = "http"; 33 private static final String HTTPS_PREFIX = "https"; 34 private static final String SCHEME_ACCOUNT_IMAGE = "image.account"; 35 private static final String ACCOUNT_IMAGE_CHANGE_NOTIFY_URI = "change_notify_uri"; 36 37 /** 38 * Non instantiable. 39 */ UriUtils()40 private UriUtils() {} 41 42 /** 43 * Gets a URI with short cut icon scheme. 44 */ getShortcutIconResourceUri(ShortcutIconResource iconResource)45 public static Uri getShortcutIconResourceUri(ShortcutIconResource iconResource) { 46 return Uri.parse(SCHEME_SHORTCUT_ICON_RESOURCE + SCHEME_DELIMITER + iconResource.packageName 47 + URI_PATH_DELIMITER 48 + iconResource.resourceName.replace(URI_PACKAGE_DELIMITER, URI_PATH_DELIMITER)); 49 } 50 51 /** 52 * Checks if the URI refers to an Android resource. 53 */ isAndroidResourceUri(Uri uri)54 public static boolean isAndroidResourceUri(Uri uri) { 55 return ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme()); 56 } 57 58 /** 59 * Checks if the URI refers to an account image. 60 */ isAccountImageUri(Uri uri)61 public static boolean isAccountImageUri(Uri uri) { 62 return uri != null && SCHEME_ACCOUNT_IMAGE.equals(uri.getScheme()); 63 } 64 getAccountName(Uri uri)65 public static String getAccountName(Uri uri) { 66 if (isAccountImageUri(uri)) { 67 return uri.getAuthority() + uri.getPath(); 68 } else { 69 throw new IllegalArgumentException("Invalid account image URI. " + uri); 70 } 71 } 72 getAccountImageChangeNotifyUri(Uri uri)73 public static Uri getAccountImageChangeNotifyUri(Uri uri) { 74 if (isAccountImageUri(uri)) { 75 String notifyUri = uri.getQueryParameter(ACCOUNT_IMAGE_CHANGE_NOTIFY_URI); 76 if (notifyUri == null) { 77 return null; 78 } else { 79 return Uri.parse(notifyUri); 80 } 81 } else { 82 throw new IllegalArgumentException("Invalid account image URI. " + uri); 83 } 84 } 85 86 /** 87 * Returns {@code true} if the URI refers to a content URI which can be opened via 88 * {@link ContentResolver#openInputStream(Uri)}. 89 */ isContentUri(Uri uri)90 public static boolean isContentUri(Uri uri) { 91 return ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) || 92 ContentResolver.SCHEME_FILE.equals(uri.getScheme()); 93 } 94 95 /** 96 * Checks if the URI refers to an shortcut icon resource. 97 */ isShortcutIconResourceUri(Uri uri)98 public static boolean isShortcutIconResourceUri(Uri uri) { 99 return SCHEME_SHORTCUT_ICON_RESOURCE.equals(uri.getScheme()); 100 } 101 102 /** 103 * Creates a shortcut icon resource object from an Android resource URI. 104 */ getIconResource(Uri uri)105 public static ShortcutIconResource getIconResource(Uri uri) { 106 if(isAndroidResourceUri(uri)) { 107 ShortcutIconResource iconResource = new ShortcutIconResource(); 108 iconResource.packageName = uri.getAuthority(); 109 // Trim off the scheme + 3 extra for "://", then replace the first "/" with a ":" 110 iconResource.resourceName = uri.toString().substring( 111 ContentResolver.SCHEME_ANDROID_RESOURCE.length() + SCHEME_DELIMITER.length()) 112 .replaceFirst(URI_PATH_DELIMITER, URI_PACKAGE_DELIMITER); 113 return iconResource; 114 } else if(isShortcutIconResourceUri(uri)) { 115 ShortcutIconResource iconResource = new ShortcutIconResource(); 116 iconResource.packageName = uri.getAuthority(); 117 iconResource.resourceName = uri.toString().substring( 118 SCHEME_SHORTCUT_ICON_RESOURCE.length() + SCHEME_DELIMITER.length() 119 + iconResource.packageName.length() + URI_PATH_DELIMITER.length()) 120 .replaceFirst(URI_PATH_DELIMITER, URI_PACKAGE_DELIMITER); 121 return iconResource; 122 } else { 123 throw new IllegalArgumentException("Invalid resource URI. " + uri); 124 } 125 } 126 127 /** 128 * Returns {@code true} if this is a web URI. 129 */ isWebUri(Uri resourceUri)130 public static boolean isWebUri(Uri resourceUri) { 131 String scheme = resourceUri.getScheme() == null ? null 132 : resourceUri.getScheme().toLowerCase(); 133 return HTTP_PREFIX.equals(scheme) || HTTPS_PREFIX.equals(scheme); 134 } 135 136 } 137