Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a6d7e3fb authored by Tao Bai's avatar Tao Bai
Browse files

Load app resource as shared library.

- Added aapt command line flag --app-as-shared-lib to build app resources
  that could be loaded as shared lib at runtime.
- Added new method AssetManager.addAssetPathAsSharedLibrary() to load an
  app resource as shared library.

Bug 22487604

Change-Id: Ib9b33c35f9c2b7129f3ba205de03d4564623ea39
parent 81d74743
Loading
Loading
Loading
Loading
+16 −2
Original line number Original line Diff line number Diff line
@@ -610,14 +610,28 @@ public final class AssetManager implements AutoCloseable {
     * {@hide}
     * {@hide}
     */
     */
    public final int addAssetPath(String path) {
    public final int addAssetPath(String path) {
        return  addAssetPathInternal(path, false);
    }

    /**
     * Add an application assets to the asset manager and loading it as shared library.
     * This can be either a directory or ZIP file.  Not for use by applications.  Returns
     * the cookie of the added asset, or 0 on failure.
     * {@hide}
     */
    public final int addAssetPathAsSharedLibrary(String path) {
        return addAssetPathInternal(path, true);
    }

    private final int addAssetPathInternal(String path, boolean appAsLib) {
        synchronized (this) {
        synchronized (this) {
            int res = addAssetPathNative(path);
            int res = addAssetPathNative(path, appAsLib);
            makeStringBlocks(mStringBlocks);
            makeStringBlocks(mStringBlocks);
            return res;
            return res;
        }
        }
    }
    }


    private native final int addAssetPathNative(String path);
    private native final int addAssetPathNative(String path, boolean appAsLib);


     /**
     /**
     * Add a set of assets to overlay an already added set of assets.
     * Add a set of assets to overlay an already added set of assets.
+3 −3
Original line number Original line Diff line number Diff line
@@ -510,7 +510,7 @@ static jlong android_content_AssetManager_getAssetRemainingLength(JNIEnv* env, j
}
}


static jint android_content_AssetManager_addAssetPath(JNIEnv* env, jobject clazz,
static jint android_content_AssetManager_addAssetPath(JNIEnv* env, jobject clazz,
                                                       jstring path)
                                                       jstring path, jboolean appAsLib)
{
{
    ScopedUtfChars path8(env, path);
    ScopedUtfChars path8(env, path);
    if (path8.c_str() == NULL) {
    if (path8.c_str() == NULL) {
@@ -523,7 +523,7 @@ static jint android_content_AssetManager_addAssetPath(JNIEnv* env, jobject clazz
    }
    }


    int32_t cookie;
    int32_t cookie;
    bool res = am->addAssetPath(String8(path8.c_str()), &cookie);
    bool res = am->addAssetPath(String8(path8.c_str()), &cookie, appAsLib);


    return (res) ? static_cast<jint>(cookie) : 0;
    return (res) ? static_cast<jint>(cookie) : 0;
}
}
@@ -2138,7 +2138,7 @@ static JNINativeMethod gAssetManagerMethods[] = {
        (void*) android_content_AssetManager_getAssetLength },
        (void*) android_content_AssetManager_getAssetLength },
    { "getAssetRemainingLength", "(J)J",
    { "getAssetRemainingLength", "(J)J",
        (void*) android_content_AssetManager_getAssetRemainingLength },
        (void*) android_content_AssetManager_getAssetRemainingLength },
    { "addAssetPathNative", "(Ljava/lang/String;)I",
    { "addAssetPathNative", "(Ljava/lang/String;Z)I",
        (void*) android_content_AssetManager_addAssetPath },
        (void*) android_content_AssetManager_addAssetPath },
    { "addOverlayPathNative",   "(Ljava/lang/String;)I",
    { "addOverlayPathNative",   "(Ljava/lang/String;)I",
        (void*) android_content_AssetManager_addOverlayPath },
        (void*) android_content_AssetManager_addOverlayPath },
+4 −3
Original line number Original line Diff line number Diff line
@@ -93,13 +93,14 @@ public:
     * look in multiple places for assets.  It can be either a directory (for
     * look in multiple places for assets.  It can be either a directory (for
     * finding assets as raw files on the disk) or a ZIP file.  This newly
     * finding assets as raw files on the disk) or a ZIP file.  This newly
     * added asset path will be examined first when searching for assets,
     * added asset path will be examined first when searching for assets,
     * before any that were previously added.
     * before any that were previously added, the assets are added as shared
     * library if appAsLib is true.
     *
     *
     * Returns "true" on success, "false" on failure.  If 'cookie' is non-NULL,
     * Returns "true" on success, "false" on failure.  If 'cookie' is non-NULL,
     * then on success, *cookie is set to the value corresponding to the
     * then on success, *cookie is set to the value corresponding to the
     * newly-added asset source.
     * newly-added asset source.
     */
     */
    bool addAssetPath(const String8& path, int32_t* cookie);
    bool addAssetPath(const String8& path, int32_t* cookie, bool appAsLib=false);
    bool addOverlayPath(const String8& path, int32_t* cookie);
    bool addOverlayPath(const String8& path, int32_t* cookie);


    /*                                                                       
    /*                                                                       
@@ -280,7 +281,7 @@ private:
    const ResTable* getResTable(bool required = true) const;
    const ResTable* getResTable(bool required = true) const;
    void setLocaleLocked(const char* locale);
    void setLocaleLocked(const char* locale);
    void updateResourceParamsLocked() const;
    void updateResourceParamsLocked() const;
    bool appendPathToResTable(const asset_path& ap) const;
    bool appendPathToResTable(const asset_path& ap, bool appAsLib=false) const;


    Asset* openIdmapLocked(const struct asset_path& ap) const;
    Asset* openIdmapLocked(const struct asset_path& ap) const;


+7 −5
Original line number Original line Diff line number Diff line
@@ -1505,7 +1505,7 @@ struct ResTable_lib_entry
class DynamicRefTable
class DynamicRefTable
{
{
public:
public:
    DynamicRefTable(uint8_t packageId);
    DynamicRefTable(uint8_t packageId, bool appAsLib);


    // Loads an unmapped reference table from the package.
    // Loads an unmapped reference table from the package.
    status_t load(const ResTable_lib_header* const header);
    status_t load(const ResTable_lib_header* const header);
@@ -1530,6 +1530,7 @@ private:
    const uint8_t                   mAssignedPackageId;
    const uint8_t                   mAssignedPackageId;
    uint8_t                         mLookupTable[256];
    uint8_t                         mLookupTable[256];
    KeyedVector<String16, uint8_t>  mEntries;
    KeyedVector<String16, uint8_t>  mEntries;
    bool                            mAppAsLib;
};
};


bool U16StringToInt(const char16_t* s, size_t len, Res_value* outValue);
bool U16StringToInt(const char16_t* s, size_t len, Res_value* outValue);
@@ -1547,10 +1548,11 @@ public:


    status_t add(const void* data, size_t size, const int32_t cookie=-1, bool copyData=false);
    status_t add(const void* data, size_t size, const int32_t cookie=-1, bool copyData=false);
    status_t add(const void* data, size_t size, const void* idmapData, size_t idmapDataSize,
    status_t add(const void* data, size_t size, const void* idmapData, size_t idmapDataSize,
            const int32_t cookie=-1, bool copyData=false);
            const int32_t cookie=-1, bool copyData=false, bool appAsLib=false);


    status_t add(Asset* asset, const int32_t cookie=-1, bool copyData=false);
    status_t add(Asset* asset, const int32_t cookie=-1, bool copyData=false);
    status_t add(Asset* asset, Asset* idmapAsset, const int32_t cookie=-1, bool copyData=false);
    status_t add(Asset* asset, Asset* idmapAsset, const int32_t cookie=-1, bool copyData=false,
            bool appAsLib=false);


    status_t add(ResTable* src);
    status_t add(ResTable* src);
    status_t addEmpty(const int32_t cookie);
    status_t addEmpty(const int32_t cookie);
@@ -1858,7 +1860,7 @@ private:
    typedef Vector<Type*> TypeList;
    typedef Vector<Type*> TypeList;


    status_t addInternal(const void* data, size_t size, const void* idmapData, size_t idmapDataSize,
    status_t addInternal(const void* data, size_t size, const void* idmapData, size_t idmapDataSize,
            const int32_t cookie, bool copyData);
            bool appAsLib, const int32_t cookie, bool copyData);


    ssize_t getResourcePackageIndex(uint32_t resID) const;
    ssize_t getResourcePackageIndex(uint32_t resID) const;


@@ -1871,7 +1873,7 @@ private:
            size_t nameLen, uint32_t* outTypeSpecFlags) const;
            size_t nameLen, uint32_t* outTypeSpecFlags) const;


    status_t parsePackage(
    status_t parsePackage(
        const ResTable_package* const pkg, const Header* const header);
        const ResTable_package* const pkg, const Header* const header, bool appAsLib);


    void print_value(const Package* pkg, const Res_value& value) const;
    void print_value(const Package* pkg, const Res_value& value) const;
    
    
+4 −4
Original line number Original line Diff line number Diff line
@@ -176,7 +176,7 @@ AssetManager::~AssetManager(void)
    delete[] mVendor;
    delete[] mVendor;
}
}


bool AssetManager::addAssetPath(const String8& path, int32_t* cookie)
bool AssetManager::addAssetPath(const String8& path, int32_t* cookie, bool appAsLib)
{
{
    AutoMutex _l(mLock);
    AutoMutex _l(mLock);


@@ -238,7 +238,7 @@ bool AssetManager::addAssetPath(const String8& path, int32_t* cookie)
#endif
#endif


    if (mResources != NULL) {
    if (mResources != NULL) {
        appendPathToResTable(ap);
        appendPathToResTable(ap, appAsLib);
    }
    }


    return true;
    return true;
@@ -610,7 +610,7 @@ FileType AssetManager::getFileType(const char* fileName)
        return kFileTypeRegular;
        return kFileTypeRegular;
}
}


bool AssetManager::appendPathToResTable(const asset_path& ap) const {
bool AssetManager::appendPathToResTable(const asset_path& ap, bool appAsLib) const {
    // skip those ap's that correspond to system overlays
    // skip those ap's that correspond to system overlays
    if (ap.isSystemOverlay) {
    if (ap.isSystemOverlay) {
        return true;
        return true;
@@ -685,7 +685,7 @@ bool AssetManager::appendPathToResTable(const asset_path& ap) const {
            mResources->add(sharedRes);
            mResources->add(sharedRes);
        } else {
        } else {
            ALOGV("Parsing resources for %s", ap.path.string());
            ALOGV("Parsing resources for %s", ap.path.string());
            mResources->add(ass, idmap, nextEntryIdx + 1, !shared);
            mResources->add(ass, idmap, nextEntryIdx + 1, !shared, appAsLib);
        }
        }
        onlyEmptyResources = false;
        onlyEmptyResources = false;


Loading