Java Code Examples for android.net.Uri#getEncodedPath()

The following examples show how to use android.net.Uri#getEncodedPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileProvider.java    From V.FlyoutTest with MIT License 6 votes vote down vote up
@Override
public File getFileForUri(Uri uri) {
    String path = uri.getEncodedPath();

    final int splitIndex = path.indexOf('/', 1);
    final String tag = Uri.decode(path.substring(1, splitIndex));
    path = Uri.decode(path.substring(splitIndex + 1));

    final File root = mRoots.get(tag);
    if (root == null) {
        throw new IllegalArgumentException("Unable to find configured root for " + uri);
    }

    File file = new File(root, path);
    try {
        file = file.getCanonicalFile();
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to resolve canonical path for " + file);
    }

    if (!file.getPath().startsWith(root.getPath())) {
        throw new SecurityException("Resolved path jumped beyond configured root");
    }

    return file;
}
 
Example 2
Source File: FileProvider.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Override
public File getFileForUri(Uri uri) {
    String path = uri.getEncodedPath();

    final int splitIndex = path.indexOf('/', 1);
    final String tag = Uri.decode(path.substring(1, splitIndex));
    path = Uri.decode(path.substring(splitIndex + 1));

    final File root = mRoots.get(tag);
    if (root == null) {
        throw new IllegalArgumentException("Unable to find configured root for " + uri);
    }

    File file = new File(root, path);
    try {
        file = file.getCanonicalFile();
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to resolve canonical path for " + file);
    }

    if (!file.getPath().startsWith(root.getPath())) {
        throw new SecurityException("Resolved path jumped beyond configured root");
    }

    return file;
}
 
Example 3
Source File: FileProvider.java    From guideshow with MIT License 6 votes vote down vote up
@Override
public File getFileForUri(Uri uri) {
    String path = uri.getEncodedPath();

    final int splitIndex = path.indexOf('/', 1);
    final String tag = Uri.decode(path.substring(1, splitIndex));
    path = Uri.decode(path.substring(splitIndex + 1));

    final File root = mRoots.get(tag);
    if (root == null) {
        throw new IllegalArgumentException("Unable to find configured root for " + uri);
    }

    File file = new File(root, path);
    try {
        file = file.getCanonicalFile();
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to resolve canonical path for " + file);
    }

    if (!file.getPath().startsWith(root.getPath())) {
        throw new SecurityException("Resolved path jumped beyond configured root");
    }

    return file;
}
 
Example 4
Source File: FileProvider.java    From letv with Apache License 2.0 6 votes vote down vote up
public File getFileForUri(Uri uri) {
    String path = uri.getEncodedPath();
    int splitIndex = path.indexOf(47, 1);
    String tag = Uri.decode(path.substring(1, splitIndex));
    path = Uri.decode(path.substring(splitIndex + 1));
    File root = (File) this.mRoots.get(tag);
    if (root == null) {
        throw new IllegalArgumentException("Unable to find configured root for " + uri);
    }
    File file = new File(root, path);
    try {
        file = file.getCanonicalFile();
        if (file.getPath().startsWith(root.getPath())) {
            return file;
        }
        throw new SecurityException("Resolved path jumped beyond configured root");
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to resolve canonical path for " + file);
    }
}
 
Example 5
Source File: FileProvider.java    From android-recipes-app with Apache License 2.0 6 votes vote down vote up
@Override
public File getFileForUri(Uri uri) {
    String path = uri.getEncodedPath();

    final int splitIndex = path.indexOf('/', 1);
    final String tag = Uri.decode(path.substring(1, splitIndex));
    path = Uri.decode(path.substring(splitIndex + 1));

    final File root = mRoots.get(tag);
    if (root == null) {
        throw new IllegalArgumentException("Unable to find configured root for " + uri);
    }

    File file = new File(root, path);
    try {
        file = file.getCanonicalFile();
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to resolve canonical path for " + file);
    }

    if (!file.getPath().startsWith(root.getPath())) {
        throw new SecurityException("Resolved path jumped beyond configured root");
    }

    return file;
}
 
Example 6
Source File: FileProvider.java    From AndPermission with Apache License 2.0 6 votes vote down vote up
@Override
public File getFileForUri(Uri uri) {
    String path = uri.getEncodedPath();

    final int splitIndex = path.indexOf('/', 1);
    final String tag = Uri.decode(path.substring(1, splitIndex));
    path = Uri.decode(path.substring(splitIndex + 1));

    final File root = mRoots.get(tag);
    if (root == null) {
        throw new IllegalArgumentException("Unable to find configured root for " + uri);
    }

    File file = new File(root, path);
    try {
        file = file.getCanonicalFile();
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to resolve canonical path for " + file);
    }

    if (!file.getPath().startsWith(root.getPath())) {
        throw new SecurityException("Resolved path jumped beyond configured root");
    }
    return file;
}
 
Example 7
Source File: FileProvider.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
@Override
public File getFileForUri(Uri uri) {
    String path = uri.getEncodedPath();

    final int splitIndex = path.indexOf('/', 1);
    final String tag = Uri.decode(path.substring(1, splitIndex));
    path = Uri.decode(path.substring(splitIndex + 1));

    final File root = mRoots.get(tag);
    if (root == null) {
        throw new IllegalArgumentException("Unable to find configured root for " + uri);
    }

    File file = new File(root, path);
    try {
        file = file.getCanonicalFile();
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to resolve canonical path for " + file);
    }

    if (!file.getPath().startsWith(root.getPath())) {
        throw new SecurityException("Resolved path jumped beyond configured root");
    }

    return file;
}
 
Example 8
Source File: ContentFilesystem.java    From keemob with MIT License 6 votes vote down vote up
@Override
public LocalFilesystemURL toLocalUri(Uri inputURL) {
    if (!"content".equals(inputURL.getScheme())) {
        return null;
    }
    String subPath = inputURL.getEncodedPath();
    if (subPath.length() > 0) {
        subPath = subPath.substring(1);
    }
    Uri.Builder b = new Uri.Builder()
        .scheme(LocalFilesystemURL.FILESYSTEM_PROTOCOL)
        .authority("localhost")
        .path(name)
        .appendPath(inputURL.getAuthority());
    if (subPath.length() > 0) {
        b.appendEncodedPath(subPath);
    }
    Uri localUri = b.encodedQuery(inputURL.getEncodedQuery())
        .encodedFragment(inputURL.getEncodedFragment())
        .build();
    return LocalFilesystemURL.parse(localUri);
}
 
Example 9
Source File: DeepLinkClient.java    From OkDeepLink with Apache License 2.0 6 votes vote down vote up
public Address matchUrl(String url) {
    if (DeepLinkClient.isEmpty()) {
        init();
    }
    String path = url;
    try {
        Uri uri = Uri.parse(url);
        path = uri.getEncodedPath();
    } catch (Exception e) {
        e.printStackTrace();
    }
    for (Address entry : addresses.values()) {
        if (entry.getPath().equals(path)) {
            return entry;
        }
    }
    return null;
}
 
Example 10
Source File: FileProvider.java    From editor with GNU General Public License v3.0 6 votes vote down vote up
@Override
public File getFileForUri(Uri uri) {
    String path = uri.getEncodedPath();

    final int splitIndex = path.indexOf('/', 1);
    final String tag = Uri.decode(path.substring(1, splitIndex));
    path = Uri.decode(path.substring(splitIndex + 1));

    final File root = mRoots.get(tag);
    if (root == null) {
        throw new IllegalArgumentException("Unable to find configured root for " + uri);
    }

    File file = new File(root, path);
    try {
        file = file.getCanonicalFile();
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to resolve canonical path for " + file);
    }

    if (!file.getPath().startsWith(root.getPath())) {
        throw new SecurityException("Resolved path jumped beyond configured root");
    }

    return file;
}
 
Example 11
Source File: FileProvider.java    From telescope with Apache License 2.0 6 votes vote down vote up
@Override public File getFileForUri(Uri uri) {
  String path = uri.getEncodedPath();

  final int splitIndex = path.indexOf('/', 1);
  final String tag = Uri.decode(path.substring(1, splitIndex));
  path = Uri.decode(path.substring(splitIndex + 1));

  final File root = mRoots.get(tag);
  if (root == null) {
    throw new IllegalArgumentException("Unable to find configured root for " + uri);
  }

  File file = new File(root, path);
  try {
    file = file.getCanonicalFile();
  } catch (IOException e) {
    throw new IllegalArgumentException("Failed to resolve canonical path for " + file);
  }

  if (!file.getPath().startsWith(root.getPath())) {
    throw new SecurityException("Resolved path jumped beyond configured root");
  }

  return file;
}
 
Example 12
Source File: OurFileProvider.java    From secrecy with Apache License 2.0 6 votes vote down vote up
@Override
public File getFileForUri(Uri uri) {
    String path = uri.getEncodedPath();

    final int splitIndex = path.indexOf('/', 1);
    final String tag = Uri.decode(path.substring(1, splitIndex));
    path = Uri.decode(path.substring(splitIndex + 1));

    final File root = mRoots.get(tag);
    if (root == null) {
        throw new IllegalArgumentException("Unable to find configured root for " + uri);
    }

    File file = new File(root, path);
    try {
        file = file.getCanonicalFile();
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to resolve canonical path for " + file);
    }

    if (!file.getPath().startsWith(root.getPath())) {
        throw new SecurityException("Resolved path jumped beyond configured root");
    }

    return file;
}
 
Example 13
Source File: IOUtil.java    From droidddle with Apache License 2.0 5 votes vote down vote up
public static String getCacheFilenameForUri(Uri uri) {
        StringBuilder filename = new StringBuilder();
//        filename.append(uri.getScheme()).append("_")
//                .append(uri.getHost()).append("_");
        String encodedPath = uri.getEncodedPath();
        if (!TextUtils.isEmpty(encodedPath)) {
            int length = encodedPath.length();
            if (length > 60) {
                encodedPath = encodedPath.substring(length - 60);
            }
            encodedPath = encodedPath.replace('/', '_');
//            filename.append(encodedPath).append("_");
        }
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(uri.toString().getBytes("UTF-8"));
            byte[] digest = md.digest();
            for (byte b : digest) {
                if ((0xff & b) < 0x10) {
                    filename.append("0").append(Integer.toHexString((0xFF & b)));
                } else {
                    filename.append(Integer.toHexString(0xFF & b));
                }
            }
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            filename.append(uri.toString().hashCode());
        }

        return filename.toString();
    }
 
Example 14
Source File: APEZProvider.java    From glide-support with The Unlicense 5 votes vote down vote up
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode)
		throws FileNotFoundException {
       initIfNecessary();
	String path = uri.getEncodedPath();
	if ( path.startsWith("/") ) {
		path = path.substring(1);
	}
	return mAPKExtensionFile.getAssetFileDescriptor(path);		
}
 
Example 15
Source File: APEZProvider.java    From play-apk-expansion with Apache License 2.0 5 votes vote down vote up
@Override
public AssetFileDescriptor openAssetFile(Uri uri, String mode)
        throws FileNotFoundException {
    initIfNecessary();
    String path = uri.getEncodedPath();
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    return mAPKExtensionFile.getAssetFileDescriptor(path);
}
 
Example 16
Source File: StickerProvider.java    From quickstart-android with Apache License 2.0 5 votes vote down vote up
private File uriToFile(@NonNull Uri uri) {
    if (mRootDir == null) {
        throw new IllegalStateException("Root directory is null");
    }
    File file = new File(mRootDir, uri.getEncodedPath());
    try {
        file = file.getCanonicalFile();
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to get canonical file: " + file);
    }
    return file;
}
 
Example 17
Source File: GeopaparazziCoreActivity.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
private void checkIncomingProject() {
    Uri data = getIntent().getData();
    if (data != null) {
        String path = data.getEncodedPath();
        if (path.endsWith(LibraryConstants.GEOPAPARAZZI_DB_EXTENSION)) {
            final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(PREFS_KEY_DATABASE_TO_LOAD, path);
            editor.apply();
        }
    }
}
 
Example 18
Source File: FontProvider.java    From FontProvider with MIT License 5 votes vote down vote up
@Nullable
@Override
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
    String path = uri.getEncodedPath();
    if (!path.startsWith("/file/")) {
        return null;
    }

    return FontManager.getParcelFileDescriptor(getContext(), path.substring("/file/".length()));
}
 
Example 19
Source File: ContentProvider.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/** @hide */
public Uri validateIncomingUri(Uri uri) throws SecurityException {
    String auth = uri.getAuthority();
    if (!mSingleUser) {
        int userId = getUserIdFromAuthority(auth, UserHandle.USER_CURRENT);
        if (userId != UserHandle.USER_CURRENT && userId != mContext.getUserId()) {
            throw new SecurityException("trying to query a ContentProvider in user "
                    + mContext.getUserId() + " with a uri belonging to user " + userId);
        }
    }
    if (!matchesOurAuthorities(getAuthorityWithoutUserId(auth))) {
        String message = "The authority of the uri " + uri + " does not match the one of the "
                + "contentProvider: ";
        if (mAuthority != null) {
            message += mAuthority;
        } else {
            message += Arrays.toString(mAuthorities);
        }
        throw new SecurityException(message);
    }

    // Normalize the path by removing any empty path segments, which can be
    // a source of security issues.
    final String encodedPath = uri.getEncodedPath();
    if (encodedPath != null && encodedPath.indexOf("//") != -1) {
        final Uri normalized = uri.buildUpon()
                .encodedPath(encodedPath.replaceAll("//+", "/")).build();
        Log.w(TAG, "Normalized " + uri + " to " + normalized
                + " to avoid possible security issues");
        return normalized;
    } else {
        return uri;
    }
}
 
Example 20
Source File: FileUtils.java    From microMathematics with GNU General Public License v3.0 4 votes vote down vote up
public static String convertToRelativePath(final Uri absoluteUri, final Uri relativeToUri)
{
    if (!absoluteUri.getScheme().equals(relativeToUri.getScheme()))
    {
        return null;
    }

    String absolutePath = null, relativeTo = null;
    if (isContentUri(absoluteUri))
    {
        absolutePath = AdapterDocuments.getPath(absoluteUri, true);
        relativeTo = AdapterDocuments.getPath(relativeToUri, true);
    }
    else
    {
        absolutePath = absoluteUri.getEncodedPath();
        relativeTo = relativeToUri.getEncodedPath();
    }
    if (absolutePath == null || relativeTo == null)
    {
        return null;
    }

    // Thanks to:
    // http://mrpmorris.blogspot.com/2007/05/convert-absolute-path-to-relative-path.html
    absolutePath = absolutePath.replaceAll("\\\\", "/");
    relativeTo = relativeTo.replaceAll("\\\\", "/");
    StringBuilder relativePath = null;

    if (!absolutePath.equals(relativeTo))
    {
        String[] absoluteDirectories = absolutePath.split("/");
        String[] relativeDirectories = relativeTo.split("/");

        //Get the shortest of the two paths
        int length = absoluteDirectories.length < relativeDirectories.length ? absoluteDirectories.length
                : relativeDirectories.length;

        //Use to determine where in the loop we exited
        int lastCommonRoot = -1;
        int index;

        //Find common root
        for (index = 0; index < length; index++)
        {
            if (absoluteDirectories[index].equals(relativeDirectories[index]))
            {
                lastCommonRoot = index;
            }
            else
            {
                break;
                //If we didn't find a common prefix then throw
            }
        }
        if (lastCommonRoot != -1)
        {
            //Build up the relative path
            relativePath = new StringBuilder();
            //Add on the ..
            for (index = lastCommonRoot + 1; index < absoluteDirectories.length; index++)
            {
                if (absoluteDirectories[index].length() > 0)
                {
                    relativePath.append("../");
                }
            }
            for (index = lastCommonRoot + 1; index < relativeDirectories.length - 1; index++)
            {
                relativePath.append(relativeDirectories[index] + "/");
            }
            relativePath.append(relativeDirectories[relativeDirectories.length - 1]);
        }
    }
    return relativePath == null ? null : relativePath.toString();
}