Search in sources :

Example 16 with NotFoundException

use of android.content.res.Resources.NotFoundException in project android_frameworks_base by DirtyUnicorns.

the class ResourcesImpl method loadComplexColor.

@Nullable
ComplexColor loadComplexColor(Resources wrapper, @NonNull TypedValue value, int id, Resources.Theme theme) {
    if (TRACE_FOR_PRELOAD) {
        // Log only framework resources
        if ((id >>> 24) == 0x1) {
            final String name = getResourceName(id);
            if (name != null)
                android.util.Log.d("loadComplexColor", name);
        }
    }
    final long key = (((long) value.assetCookie) << 32) | value.data;
    // Handle inline color definitions.
    if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        return getColorStateListFromInt(value, key);
    }
    final String file = value.string.toString();
    ComplexColor complexColor;
    if (file.endsWith(".xml")) {
        try {
            complexColor = loadComplexColorFromName(wrapper, theme, value, id);
        } catch (Exception e) {
            final NotFoundException rnf = new NotFoundException("File " + file + " from complex color resource ID #0x" + Integer.toHexString(id));
            rnf.initCause(e);
            throw rnf;
        }
    } else {
        throw new NotFoundException("File " + file + " from drawable resource ID #0x" + Integer.toHexString(id) + ": .xml extension required");
    }
    return complexColor;
}
Also used : NotFoundException(android.content.res.Resources.NotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NotFoundException(android.content.res.Resources.NotFoundException) Nullable(android.annotation.Nullable)

Example 17 with NotFoundException

use of android.content.res.Resources.NotFoundException in project android_frameworks_base by DirtyUnicorns.

the class ResourcesImpl method loadXmlResourceParser.

/**
     * Loads an XML parser for the specified file.
     *
     * @param file the path for the XML file to parse
     * @param id the resource identifier for the file
     * @param assetCookie the asset cookie for the file
     * @param type the type of resource (used for logging)
     * @return a parser for the specified XML file
     * @throws NotFoundException if the file could not be loaded
     */
@NonNull
XmlResourceParser loadXmlResourceParser(@NonNull String file, @AnyRes int id, int assetCookie, @NonNull String type) throws NotFoundException {
    if (id != 0) {
        try {
            synchronized (mCachedXmlBlocks) {
                final int[] cachedXmlBlockCookies = mCachedXmlBlockCookies;
                final String[] cachedXmlBlockFiles = mCachedXmlBlockFiles;
                final XmlBlock[] cachedXmlBlocks = mCachedXmlBlocks;
                // First see if this block is in our cache.
                final int num = cachedXmlBlockFiles.length;
                for (int i = 0; i < num; i++) {
                    if (cachedXmlBlockCookies[i] == assetCookie && cachedXmlBlockFiles[i] != null && cachedXmlBlockFiles[i].equals(file)) {
                        return cachedXmlBlocks[i].newParser();
                    }
                }
                // Not in the cache, create a new block and put it at
                // the next slot in the cache.
                final XmlBlock block = mAssets.openXmlBlockAsset(assetCookie, file);
                if (block != null) {
                    final int pos = (mLastCachedXmlBlockIndex + 1) % num;
                    mLastCachedXmlBlockIndex = pos;
                    final XmlBlock oldBlock = cachedXmlBlocks[pos];
                    if (oldBlock != null) {
                        oldBlock.close();
                    }
                    cachedXmlBlockCookies[pos] = assetCookie;
                    cachedXmlBlockFiles[pos] = file;
                    cachedXmlBlocks[pos] = block;
                    return block.newParser();
                }
            }
        } catch (Exception e) {
            final NotFoundException rnf = new NotFoundException("File " + file + " from xml type " + type + " resource ID #0x" + Integer.toHexString(id));
            rnf.initCause(e);
            throw rnf;
        }
    }
    throw new NotFoundException("File " + file + " from xml type " + type + " resource ID #0x" + Integer.toHexString(id));
}
Also used : NotFoundException(android.content.res.Resources.NotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NotFoundException(android.content.res.Resources.NotFoundException) NonNull(android.annotation.NonNull)

Example 18 with NotFoundException

use of android.content.res.Resources.NotFoundException in project android_frameworks_base by DirtyUnicorns.

the class ResourcesImpl method loadDrawableForCookie.

/**
     * Loads a drawable from XML or resources stream.
     */
private Drawable loadDrawableForCookie(Resources wrapper, TypedValue value, int id, Resources.Theme theme) {
    if (value.string == null) {
        throw new NotFoundException("Resource \"" + getResourceName(id) + "\" (" + Integer.toHexString(id) + ") is not a Drawable (color or path): " + value);
    }
    final String file = value.string.toString();
    if (TRACE_FOR_MISS_PRELOAD) {
        // Log only framework resources
        if ((id >>> 24) == 0x1) {
            final String name = getResourceName(id);
            if (name != null) {
                Log.d(TAG, "Loading framework drawable #" + Integer.toHexString(id) + ": " + name + " at " + file);
            }
        }
    }
    if (DEBUG_LOAD) {
        Log.v(TAG, "Loading drawable for cookie " + value.assetCookie + ": " + file);
    }
    final Drawable dr;
    Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
    try {
        if (file.endsWith(".xml")) {
            final XmlResourceParser rp = loadXmlResourceParser(file, id, value.assetCookie, "drawable");
            dr = Drawable.createFromXml(wrapper, rp, theme);
            rp.close();
        } else {
            final InputStream is = mAssets.openNonAsset(value.assetCookie, file, AssetManager.ACCESS_STREAMING);
            dr = Drawable.createFromResourceStream(wrapper, value, is, file, null);
            is.close();
        }
    } catch (Exception e) {
        Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
        final NotFoundException rnf = new NotFoundException("File " + file + " from drawable resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(e);
        throw rnf;
    }
    Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
    return dr;
}
Also used : InputStream(java.io.InputStream) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) NotFoundException(android.content.res.Resources.NotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NotFoundException(android.content.res.Resources.NotFoundException)

Example 19 with NotFoundException

use of android.content.res.Resources.NotFoundException in project android_frameworks_base by DirtyUnicorns.

the class ResourcesImpl method loadDrawable.

@Nullable
Drawable loadDrawable(Resources wrapper, TypedValue value, int id, Resources.Theme theme, boolean useCache) throws NotFoundException {
    try {
        if (TRACE_FOR_PRELOAD) {
            // Log only framework resources
            if ((id >>> 24) == 0x1) {
                final String name = getResourceName(id);
                if (name != null) {
                    Log.d("PreloadDrawable", name);
                }
            }
        }
        final boolean isColorDrawable;
        final DrawableCache caches;
        final long key;
        if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
            isColorDrawable = true;
            caches = mColorDrawableCache;
            key = value.data;
        } else {
            isColorDrawable = false;
            caches = mDrawableCache;
            key = (((long) value.assetCookie) << 32) | value.data;
        }
        // we're currently preloading or we're not using the cache.
        if (!mPreloading && useCache) {
            final Drawable cachedDrawable = caches.getInstance(key, wrapper, theme);
            if (cachedDrawable != null) {
                return cachedDrawable;
            }
        }
        // Next, check preloaded drawables. Preloaded drawables may contain
        // unresolved theme attributes.
        final Drawable.ConstantState cs;
        if (isColorDrawable) {
            cs = sPreloadedColorDrawables.get(key);
        } else {
            cs = sPreloadedDrawables[mConfiguration.getLayoutDirection()].get(key);
        }
        Drawable dr;
        if (cs != null) {
            dr = cs.newDrawable(wrapper);
        } else if (isColorDrawable) {
            dr = new ColorDrawable(value.data);
        } else {
            dr = loadDrawableForCookie(wrapper, value, id, null);
        }
        // Determine if the drawable has unresolved theme attributes. If it
        // does, we'll need to apply a theme and store it in a theme-specific
        // cache.
        final boolean canApplyTheme = dr != null && dr.canApplyTheme();
        if (canApplyTheme && theme != null) {
            dr = dr.mutate();
            dr.applyTheme(theme);
            dr.clearMutated();
        }
        // pollute the cache with drawables loaded from a foreign density.
        if (dr != null && useCache) {
            dr.setChangingConfigurations(value.changingConfigurations);
            cacheDrawable(value, isColorDrawable, caches, theme, canApplyTheme, key, dr);
        }
        return dr;
    } catch (Exception e) {
        String name;
        try {
            name = getResourceName(id);
        } catch (NotFoundException e2) {
            name = "(missing name)";
        }
        // The target drawable might fail to load for any number of
        // reasons, but we always want to include the resource name.
        // Since the client already expects this method to throw a
        // NotFoundException, just throw one of those.
        final NotFoundException nfe = new NotFoundException("Drawable " + name + " with resource ID #0x" + Integer.toHexString(id), e);
        nfe.setStackTrace(new StackTraceElement[0]);
        throw nfe;
    }
}
Also used : ColorDrawable(android.graphics.drawable.ColorDrawable) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) NotFoundException(android.content.res.Resources.NotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NotFoundException(android.content.res.Resources.NotFoundException) Nullable(android.annotation.Nullable)

Example 20 with NotFoundException

use of android.content.res.Resources.NotFoundException in project android_frameworks_base by DirtyUnicorns.

the class Ringtone method playFallbackRingtone.

private boolean playFallbackRingtone() {
    if (mAudioManager.getStreamVolume(AudioAttributes.toLegacyStreamType(mAudioAttributes)) != 0) {
        int ringtoneType = RingtoneManager.getDefaultType(mUri);
        if (ringtoneType == -1 || RingtoneManager.getActualDefaultRingtoneUri(mContext, ringtoneType) != null) {
            // Default ringtone, try fallback ringtone.
            try {
                AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(com.android.internal.R.raw.fallbackring);
                if (afd != null) {
                    mLocalPlayer = new MediaPlayer();
                    if (afd.getDeclaredLength() < 0) {
                        mLocalPlayer.setDataSource(afd.getFileDescriptor());
                    } else {
                        mLocalPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
                    }
                    mLocalPlayer.setAudioAttributes(mAudioAttributes);
                    synchronized (mPlaybackSettingsLock) {
                        applyPlaybackProperties_sync();
                    }
                    mLocalPlayer.prepare();
                    startLocalPlayer();
                    afd.close();
                    return true;
                } else {
                    Log.e(TAG, "Could not load fallback ringtone");
                }
            } catch (IOException ioe) {
                destroyLocalPlayer();
                Log.e(TAG, "Failed to open fallback ringtone");
            } catch (NotFoundException nfe) {
                Log.e(TAG, "Fallback ringtone does not exist");
            }
        } else {
            Log.w(TAG, "not playing fallback for " + mUri);
        }
    }
    return false;
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) NotFoundException(android.content.res.Resources.NotFoundException) IOException(java.io.IOException)

Aggregations

NotFoundException (android.content.res.Resources.NotFoundException)165 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)49 Resources (android.content.res.Resources)47 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)45 FileNotFoundException (java.io.FileNotFoundException)34 IOException (java.io.IOException)34 ApplicationInfo (android.content.pm.ApplicationInfo)31 File (java.io.File)30 InputStream (java.io.InputStream)28 FileInputStream (java.io.FileInputStream)20 Drawable (android.graphics.drawable.Drawable)19 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)16 Nullable (android.annotation.Nullable)15 XmlResourceParser (android.content.res.XmlResourceParser)12 SettingNotFoundException (android.provider.Settings.SettingNotFoundException)12 ArrayResourceValue (com.android.ide.common.rendering.api.ArrayResourceValue)12 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)12 NonNull (android.annotation.NonNull)10 ColorDrawable (android.graphics.drawable.ColorDrawable)10 DensityBasedResourceValue (com.android.ide.common.rendering.api.DensityBasedResourceValue)8