All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.holoeverywhere.internal.ContentResolverCompat Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version

package org.holoeverywhere.internal;

import java.io.FileNotFoundException;
import java.util.List;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.net.Uri;
import android.text.TextUtils;

public class ContentResolverCompat {

    public static class OpenResourceIdResult {
        public int id;
        public Resources r;
    }

    public static OpenResourceIdResult getResourceId(Context context, Uri uri)
            throws FileNotFoundException {
        String authority = uri.getAuthority();
        Resources r;
        if (TextUtils.isEmpty(authority)) {
            throw new FileNotFoundException("No authority: " + uri);
        } else {
            try {
                r = context.getPackageManager().getResourcesForApplication(
                        authority);
            } catch (NameNotFoundException ex) {
                throw new FileNotFoundException(
                        "No package found for authority: " + uri);
            }
        }
        List path = uri.getPathSegments();
        if (path == null) {
            throw new FileNotFoundException("No path: " + uri);
        }
        int len = path.size();
        int id;
        if (len == 1) {
            try {
                id = Integer.parseInt(path.get(0));
            } catch (NumberFormatException e) {
                throw new FileNotFoundException(
                        "Single path segment is not a resource ID: " + uri);
            }
        } else if (len == 2) {
            id = r.getIdentifier(path.get(1), path.get(0), authority);
        } else {
            throw new FileNotFoundException("More than two path segments: "
                    + uri);
        }
        if (id == 0) {
            throw new FileNotFoundException("No resource found for: " + uri);
        }
        OpenResourceIdResult res = new OpenResourceIdResult();
        res.r = r;
        res.id = id;
        return res;
    }

    private ContentResolverCompat() {

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy