Search in sources :

Example 1 with NameNotFoundException

use of android.content.pm.PackageManager.NameNotFoundException in project cw-omnibus by commonsguy.

the class SuggestionsAdapter method getTheDrawable.

public Drawable getTheDrawable(Uri uri) throws FileNotFoundException {
    String authority = uri.getAuthority();
    Resources r;
    if (TextUtils.isEmpty(authority)) {
        throw new FileNotFoundException("No authority: " + uri);
    } else {
        try {
            r = mContext.getPackageManager().getResourcesForApplication(authority);
        } catch (NameNotFoundException ex) {
            throw new FileNotFoundException("No package found for authority: " + uri);
        }
    }
    List<String> 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);
    }
    return r.getDrawable(id);
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) FileNotFoundException(java.io.FileNotFoundException) SpannableString(android.text.SpannableString) Resources(android.content.res.Resources)

Example 2 with NameNotFoundException

use of android.content.pm.PackageManager.NameNotFoundException in project packer-ng-plugin by mcxiaoke.

the class MainActivity method addMetaDataSection.

private void addMetaDataSection() {
    final PackageManager pm = getPackageManager();
    final String packageName = getPackageName();
    try {
        final ApplicationInfo info = pm.getApplicationInfo(packageName, PackageManager.GET_SIGNATURES | PackageManager.GET_META_DATA);
        final Bundle bundle = info.metaData;
        final StringBuilder builder = new StringBuilder();
        builder.append("[MetaData]\n");
        if (bundle != null) {
            final Set<String> keySet = bundle.keySet();
            for (final String key : keySet) {
                builder.append(key).append("=").append(bundle.get(key)).append("\n");
            }
        }
        addSection(builder.toString());
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : PackageManager(android.content.pm.PackageManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) Bundle(android.os.Bundle) ApplicationInfo(android.content.pm.ApplicationInfo)

Example 3 with NameNotFoundException

use of android.content.pm.PackageManager.NameNotFoundException in project AndroidTraining by mixi-inc.

the class SuggestionsAdapter method getTheDrawable.

public Drawable getTheDrawable(Uri uri) throws FileNotFoundException {
    String authority = uri.getAuthority();
    Resources r;
    if (TextUtils.isEmpty(authority)) {
        throw new FileNotFoundException("No authority: " + uri);
    } else {
        try {
            r = mContext.getPackageManager().getResourcesForApplication(authority);
        } catch (NameNotFoundException ex) {
            throw new FileNotFoundException("No package found for authority: " + uri);
        }
    }
    List<String> 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);
    }
    return r.getDrawable(id);
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) FileNotFoundException(java.io.FileNotFoundException) SpannableString(android.text.SpannableString) Resources(android.content.res.Resources)

Example 4 with NameNotFoundException

use of android.content.pm.PackageManager.NameNotFoundException in project AndroidTraining by mixi-inc.

the class SuggestionsAdapter method getActivityIcon.

/**
     * Gets the activity or application icon for an activity.
     *
     * @param component Name of an activity.
     * @return A drawable, or {@code null} if neither the acitivy or the application
     *         have an icon set.
     */
private Drawable getActivityIcon(ComponentName component) {
    PackageManager pm = mContext.getPackageManager();
    final ActivityInfo activityInfo;
    try {
        activityInfo = pm.getActivityInfo(component, PackageManager.GET_META_DATA);
    } catch (NameNotFoundException ex) {
        Log.w(LOG_TAG, ex.toString());
        return null;
    }
    int iconId = activityInfo.getIconResource();
    if (iconId == 0)
        return null;
    String pkg = component.getPackageName();
    Drawable drawable = pm.getDrawable(pkg, iconId, activityInfo.applicationInfo);
    if (drawable == null) {
        Log.w(LOG_TAG, "Invalid icon resource " + iconId + " for " + component.flattenToShortString());
        return null;
    }
    return drawable;
}
Also used : ActivityInfo(android.content.pm.ActivityInfo) PackageManager(android.content.pm.PackageManager) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) Drawable(android.graphics.drawable.Drawable) SpannableString(android.text.SpannableString)

Example 5 with NameNotFoundException

use of android.content.pm.PackageManager.NameNotFoundException in project Inscription by MartinvanZ.

the class ChangeLogDialog method getHTML.

//Returns change log in HTML format 
public String getHTML() {
    //TODO: Remove duplicate code with the method show()
    //Get resources
    final String packageName = mContext.getPackageName();
    final Resources resources;
    try {
        resources = mContext.getPackageManager().getResourcesForApplication(packageName);
    } catch (NameNotFoundException ignored) {
        return "";
    }
    //Create HTML change log
    return getHTMLChangelog(R.xml.changelog, resources, 0);
}
Also used : NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) Resources(android.content.res.Resources)

Aggregations

NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)1027 PackageManager (android.content.pm.PackageManager)363 PackageInfo (android.content.pm.PackageInfo)297 ApplicationInfo (android.content.pm.ApplicationInfo)242 Intent (android.content.Intent)144 ComponentName (android.content.ComponentName)133 ActivityInfo (android.content.pm.ActivityInfo)124 Resources (android.content.res.Resources)115 Context (android.content.Context)104 Drawable (android.graphics.drawable.Drawable)95 Bundle (android.os.Bundle)93 IOException (java.io.IOException)89 UserHandle (android.os.UserHandle)79 ResolveInfo (android.content.pm.ResolveInfo)73 ArrayList (java.util.ArrayList)70 RemoteException (android.os.RemoteException)63 File (java.io.File)58 TextView (android.widget.TextView)53 View (android.view.View)46 FileNotFoundException (java.io.FileNotFoundException)44