diff options
Diffstat (limited to 'libs')
108 files changed, 4814 insertions, 264 deletions
diff --git a/libs/androidfw/Android.bp b/libs/androidfw/Android.bp new file mode 100644 index 000000000000..5d12d9507c22 --- /dev/null +++ b/libs/androidfw/Android.bp @@ -0,0 +1,73 @@ +// Copyright (C) 2010 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// libandroidfw is partially built for the host (used by obbtool, aapt, and others) + +cc_library { + name: "libandroidfw", + host_supported: true, + cflags: [ + "-Wall", + "-Werror", + "-Wunused", + "-Wunreachable-code", + ], + srcs: [ + "Asset.cpp", + "AssetDir.cpp", + "AssetManager.cpp", + "LocaleData.cpp", + "misc.cpp", + "ObbFile.cpp", + "ResourceTypes.cpp", + "StreamingZipInflater.cpp", + "TypeWrappers.cpp", + "ZipFileRO.cpp", + "ZipUtils.cpp", + ], + export_include_dirs: ["include"], + target: { + android: { + srcs: [ + "BackupData.cpp", + "BackupHelpers.cpp", + "CursorWindow.cpp", + "DisplayEventDispatcher.cpp", + ], + shared_libs: [ + "libziparchive", + "libbase", + "libbinder", + "liblog", + "libcutils", + "libgui", + "libutils", + "libz", + ], + static: { + enabled: false, + }, + }, + host: { + cflags: ["-DSTATIC_ANDROIDFW_FOR_TOOLS"], + shared: { + enabled: false, + }, + shared_libs: ["libz-host"], + }, + windows: { + enabled: true, + }, + }, +} diff --git a/libs/androidfw/Android.mk b/libs/androidfw/Android.mk deleted file mode 100644 index 6bbfcd2f9d8a..000000000000 --- a/libs/androidfw/Android.mk +++ /dev/null @@ -1,86 +0,0 @@ -# Copyright (C) 2010 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -LOCAL_PATH:= $(call my-dir) - -# libandroidfw is partially built for the host (used by obbtool, aapt, and others) -# These files are common to host and target builds. - -commonSources := \ - Asset.cpp \ - AssetDir.cpp \ - AssetManager.cpp \ - LocaleData.cpp \ - misc.cpp \ - ObbFile.cpp \ - ResourceTypes.cpp \ - StreamingZipInflater.cpp \ - TypeWrappers.cpp \ - ZipFileRO.cpp \ - ZipUtils.cpp - -deviceSources := \ - $(commonSources) \ - BackupData.cpp \ - BackupHelpers.cpp \ - CursorWindow.cpp \ - DisplayEventDispatcher.cpp - -hostSources := $(commonSources) - -# For the host -# ===================================================== -include $(CLEAR_VARS) - -LOCAL_MODULE:= libandroidfw -LOCAL_MODULE_HOST_OS := darwin linux windows -LOCAL_CFLAGS += -DSTATIC_ANDROIDFW_FOR_TOOLS -LOCAL_CFLAGS += -Wall -Werror -Wunused -Wunreachable-code -LOCAL_SRC_FILES:= $(hostSources) -LOCAL_C_INCLUDES := external/zlib - -include $(BUILD_HOST_STATIC_LIBRARY) - - -# For the device -# ===================================================== - -include $(CLEAR_VARS) - -LOCAL_MODULE:= libandroidfw -LOCAL_SRC_FILES:= $(deviceSources) -LOCAL_C_INCLUDES := \ - system/core/include -LOCAL_STATIC_LIBRARIES := libziparchive libbase -LOCAL_SHARED_LIBRARIES := \ - libbinder \ - liblog \ - libcutils \ - libgui \ - libutils \ - libz - -LOCAL_CFLAGS += -Wall -Werror -Wunused -Wunreachable-code - -include $(BUILD_SHARED_LIBRARY) - - -# Include subdirectory makefiles -# ============================================================ - -# If we're building with ONE_SHOT_MAKEFILE (mm, mmm), then what the framework -# team really wants is to build the stuff defined by this makefile. -ifeq (,$(ONE_SHOT_MAKEFILE)) -include $(call first-makefiles-under,$(LOCAL_PATH)) -endif diff --git a/libs/androidfw/Asset.cpp b/libs/androidfw/Asset.cpp index 6ae39d5d5d01..8e8c6a2e25a2 100644 --- a/libs/androidfw/Asset.cpp +++ b/libs/androidfw/Asset.cpp @@ -110,7 +110,7 @@ String8 Asset::getAssetAllocations() res.append(cur->getAssetSource()); off64_t size = (cur->getLength()+512)/1024; char buf[64]; - sprintf(buf, ": %dK\n", (int)size); + snprintf(buf, sizeof(buf), ": %dK\n", (int)size); res.append(buf); } cur = cur->mNext; diff --git a/libs/androidfw/BackupHelpers.cpp b/libs/androidfw/BackupHelpers.cpp index 9300794cf310..78e9d91c4d67 100644 --- a/libs/androidfw/BackupHelpers.cpp +++ b/libs/androidfw/BackupHelpers.cpp @@ -442,7 +442,7 @@ back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD return 0; } -static void calc_tar_checksum(char* buf) { +static void calc_tar_checksum(char* buf, size_t buf_size) { // [ 148 : 8 ] checksum -- to be calculated with this field as space chars memset(buf + 148, ' ', 8); @@ -453,11 +453,13 @@ static void calc_tar_checksum(char* buf) { // Now write the real checksum value: // [ 148 : 8 ] checksum: 6 octal digits [leading zeroes], NUL, SPC - sprintf(buf + 148, "%06o", sum); // the trailing space is already in place + snprintf(buf + 148, buf_size - 148, "%06o", sum); // the trailing space is + // already in place } // Returns number of bytes written -static int write_pax_header_entry(char* buf, const char* key, const char* value) { +static int write_pax_header_entry(char* buf, size_t buf_size, + const char* key, const char* value) { // start with the size of "1 key=value\n" int len = strlen(key) + strlen(value) + 4; if (len > 9) len++; @@ -466,7 +468,7 @@ static int write_pax_header_entry(char* buf, const char* key, const char* value) // since PATH_MAX is 4096 we don't expect to have to generate any single // header entry longer than 9999 characters - return sprintf(buf, "%d %s=%s\n", len, key, value); + return snprintf(buf, buf_size, "%d %s=%s\n", len, key, value); } // Wire format to the backup manager service is chunked: each chunk is prefixed by @@ -550,8 +552,12 @@ int write_tarfile(const String8& packageName, const String8& domain, // read/write up to this much at a time. const size_t BUFSIZE = 32 * 1024; char* buf = (char *)calloc(1,BUFSIZE); - char* paxHeader = buf + 512; // use a different chunk of it as separate scratch - char* paxData = buf + 1024; + const size_t PAXHEADER_OFFSET = 512; + const size_t PAXHEADER_SIZE = 512; + const size_t PAXDATA_SIZE = BUFSIZE - (PAXHEADER_SIZE + PAXHEADER_OFFSET); + char* const paxHeader = buf + PAXHEADER_OFFSET; // use a different chunk of + // it as separate scratch + char* const paxData = paxHeader + PAXHEADER_SIZE; if (buf == NULL) { ALOGE("Out of mem allocating transfer buffer"); @@ -630,21 +636,21 @@ int write_tarfile(const String8& packageName, const String8& domain, // already preflighted if (needExtended) { char sizeStr[32]; // big enough for a 64-bit unsigned value in decimal - char* p = paxData; // construct the pax extended header data block - memset(paxData, 0, BUFSIZE - (paxData - buf)); + memset(paxData, 0, PAXDATA_SIZE); // size header -- calc len in digits by actually rendering the number // to a string - brute force but simple + int paxLen = 0; snprintf(sizeStr, sizeof(sizeStr), "%lld", (long long)s.st_size); - p += write_pax_header_entry(p, "size", sizeStr); + paxLen += write_pax_header_entry(paxData, PAXDATA_SIZE, "size", sizeStr); // fullname was generated above with the ustar paths - p += write_pax_header_entry(p, "path", fullname.string()); + paxLen += write_pax_header_entry(paxData + paxLen, PAXDATA_SIZE - paxLen, + "path", fullname.string()); // Now we know how big the pax data is - int paxLen = p - paxData; // Now build the pax *header* templated on the ustar header memcpy(paxHeader, buf, 512); @@ -659,10 +665,10 @@ int write_tarfile(const String8& packageName, const String8& domain, // [ 124 : 12 ] size of pax extended header data memset(paxHeader + 124, 0, 12); - snprintf(paxHeader + 124, 12, "%011o", (unsigned int)(p - paxData)); + snprintf(paxHeader + 124, 12, "%011o", (unsigned int)paxLen); // Checksum and write the pax block header - calc_tar_checksum(paxHeader); + calc_tar_checksum(paxHeader, PAXHEADER_SIZE); send_tarfile_chunk(writer, paxHeader, 512); // Now write the pax data itself @@ -671,7 +677,7 @@ int write_tarfile(const String8& packageName, const String8& domain, } // Checksum and write the 512-byte ustar file header block to the output - calc_tar_checksum(buf); + calc_tar_checksum(buf, BUFSIZE); send_tarfile_chunk(writer, buf, 512); // Now write the file data itself, for real files. We honor tar's convention that diff --git a/libs/androidfw/ResourceTypes.cpp b/libs/androidfw/ResourceTypes.cpp index ceeb12bab205..e10db05e8557 100644 --- a/libs/androidfw/ResourceTypes.cpp +++ b/libs/androidfw/ResourceTypes.cpp @@ -739,7 +739,7 @@ const char16_t* ResStringPool::stringAt(size_t idx, size_t* u16len) const ALOGW("CREATING STRING CACHE OF %zu bytes", static_cast<size_t>(mHeader->stringCount*sizeof(char16_t**))); #endif - mCache = (char16_t**)calloc(mHeader->stringCount, sizeof(char16_t**)); + mCache = (char16_t**)calloc(mHeader->stringCount, sizeof(char16_t*)); if (mCache == NULL) { ALOGW("No memory trying to allocate decode cache table of %d bytes\n", (int)(mHeader->stringCount*sizeof(char16_t**))); @@ -776,7 +776,7 @@ const char16_t* ResStringPool::stringAt(size_t idx, size_t* u16len) const if (kDebugStringPoolNoisy) { ALOGI("Caching UTF8 string: %s", u8str); } - utf8_to_utf16(u8str, u8len, u16str); + utf8_to_utf16(u8str, u8len, u16str, *u16len + 1); mCache[idx] = u16str; return u16str; } else { @@ -877,7 +877,8 @@ ssize_t ResStringPool::indexOfString(const char16_t* str, size_t strLen) const // the ordering, we need to convert strings in the pool to UTF-16. // But we don't want to hit the cache, so instead we will have a // local temporary allocation for the conversions. - char16_t* convBuffer = (char16_t*)malloc(strLen+4); + size_t convBufferLen = strLen + 4; + char16_t* convBuffer = (char16_t*)calloc(convBufferLen, sizeof(char16_t)); ssize_t l = 0; ssize_t h = mHeader->stringCount-1; @@ -887,8 +888,7 @@ ssize_t ResStringPool::indexOfString(const char16_t* str, size_t strLen) const const uint8_t* s = (const uint8_t*)string8At(mid, &len); int c; if (s != NULL) { - char16_t* end = utf8_to_utf16_n(s, len, convBuffer, strLen+3); - *end = 0; + char16_t* end = utf8_to_utf16(s, len, convBuffer, convBufferLen); c = strzcmp16(convBuffer, end-convBuffer, str, strLen); } else { c = -1; @@ -3116,7 +3116,7 @@ String8 ResTable_config::toString() const { struct ResTable::Header { - Header(ResTable* _owner) : owner(_owner), ownedData(NULL), header(NULL), + explicit Header(ResTable* _owner) : owner(_owner), ownedData(NULL), header(NULL), resourceIDMap(NULL), resourceIDMapSize(0) { } ~Header() @@ -3166,7 +3166,7 @@ struct ResTable::Package { Package(ResTable* _owner, const Header* _header, const ResTable_package* _package) : owner(_owner), header(_header), package(_package), typeIdOffset(0) { - if (dtohs(package->header.headerSize) == sizeof(package)) { + if (dtohs(package->header.headerSize) == sizeof(*package)) { // The package structure is the same size as the definition. // This means it contains the typeIdOffset field. typeIdOffset = package->typeIdOffset; @@ -3539,7 +3539,7 @@ ssize_t ResTable::Theme::getAttribute(uint32_t resID, Res_value* outValue, } if (pi != NULL) { if (kDebugTableTheme) { - ALOGI("Desired type index is %zd in avail %zu", t, Res_MAXTYPE + 1); + ALOGI("Desired type index is %u in avail %zu", t, Res_MAXTYPE + 1); } if (t <= Res_MAXTYPE) { const type_info& ti = pi->types[t]; @@ -4352,10 +4352,12 @@ ssize_t ResTable::getBagLocked(uint32_t resID, const bag_entry** outBag, if (set->numAttrs >= set->availAttrs) { // Need to alloc more memory... const size_t newAvail = set->availAttrs+N; + void *oldSet = set; set = (bag_set*)realloc(set, sizeof(bag_set) + sizeof(bag_entry)*newAvail); if (set == NULL) { + free(oldSet); return NO_MEMORY; } set->availAttrs = newAvail; @@ -4402,7 +4404,7 @@ ssize_t ResTable::getBagLocked(uint32_t resID, const bag_entry** outBag, pos++; const size_t size = dtohs(map->value.size); curOff += size + sizeof(*map)-sizeof(map->value); - }; + } if (curEntry > set->numAttrs) { set->numAttrs = curEntry; @@ -6759,7 +6761,7 @@ bool ResTable::getIdmapInfo(const void* idmap, size_t sizeBytes, #define CHAR16_TO_CSTR(c16, len) (String8(String16(c16,len)).string()) #define CHAR16_ARRAY_EQ(constant, var, len) \ - ((len == (sizeof(constant)/sizeof(constant[0]))) && (0 == memcmp((var), (constant), (len)))) + (((len) == (sizeof(constant)/sizeof((constant)[0]))) && (0 == memcmp((var), (constant), (len)))) static void print_complex(uint32_t complex, bool isFraction) { diff --git a/libs/androidfw/ZipUtils.cpp b/libs/androidfw/ZipUtils.cpp index 6fa0f14ecb8e..5abfc8ee917e 100644 --- a/libs/androidfw/ZipUtils.cpp +++ b/libs/androidfw/ZipUtils.cpp @@ -150,7 +150,7 @@ bail: class FileReader { public: - FileReader(FILE* fp) : + explicit FileReader(FILE* fp) : mFp(fp), mReadBuf(new unsigned char[kReadBufSize]) { } @@ -170,7 +170,7 @@ public: class FdReader { public: - FdReader(int fd) : + explicit FdReader(int fd) : mFd(fd), mReadBuf(new unsigned char[kReadBufSize]) { } diff --git a/libs/androidfw/include/androidfw/Asset.h b/libs/androidfw/include/androidfw/Asset.h new file mode 100644 index 000000000000..2cd8c0bf3c78 --- /dev/null +++ b/libs/androidfw/include/androidfw/Asset.h @@ -0,0 +1,334 @@ +/* + * Copyright (C) 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// +// Class providing access to a read-only asset. Asset objects are NOT +// thread-safe, and should not be shared across threads. +// +#ifndef __LIBS_ASSET_H +#define __LIBS_ASSET_H + +#include <stdio.h> +#include <sys/types.h> + +#include <utils/Compat.h> +#include <utils/Errors.h> +#include <utils/FileMap.h> +#include <utils/String8.h> + +namespace android { + +/* + * Instances of this class provide read-only operations on a byte stream. + * + * Access may be optimized for streaming, random, or whole buffer modes. All + * operations are supported regardless of how the file was opened, but some + * things will be less efficient. [pass that in??] + * + * "Asset" is the base class for all types of assets. The classes below + * provide most of the implementation. The AssetManager uses one of the + * static "create" functions defined here to create a new instance. + */ +class Asset { +public: + virtual ~Asset(void) = default; + + static int32_t getGlobalCount(); + static String8 getAssetAllocations(); + + /* used when opening an asset */ + typedef enum AccessMode { + ACCESS_UNKNOWN = 0, + + /* read chunks, and seek forward and backward */ + ACCESS_RANDOM, + + /* read sequentially, with an occasional forward seek */ + ACCESS_STREAMING, + + /* caller plans to ask for a read-only buffer with all data */ + ACCESS_BUFFER, + } AccessMode; + + /* + * Read data from the current offset. Returns the actual number of + * bytes read, 0 on EOF, or -1 on error. + */ + virtual ssize_t read(void* buf, size_t count) = 0; + + /* + * Seek to the specified offset. "whence" uses the same values as + * lseek/fseek. Returns the new position on success, or (off64_t) -1 + * on failure. + */ + virtual off64_t seek(off64_t offset, int whence) = 0; + + /* + * Close the asset, freeing all associated resources. + */ + virtual void close(void) = 0; + + /* + * Get a pointer to a buffer with the entire contents of the file. + */ + virtual const void* getBuffer(bool wordAligned) = 0; + + /* + * Get the total amount of data that can be read. + */ + virtual off64_t getLength(void) const = 0; + + /* + * Get the total amount of data that can be read from the current position. + */ + virtual off64_t getRemainingLength(void) const = 0; + + /* + * Open a new file descriptor that can be used to read this asset. + * Returns -1 if you can not use the file descriptor (for example if the + * asset is compressed). + */ + virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const = 0; + + /* + * Return whether this asset's buffer is allocated in RAM (not mmapped). + * Note: not virtual so it is safe to call even when being destroyed. + */ + virtual bool isAllocated(void) const { return false; } + + /* + * Get a string identifying the asset's source. This might be a full + * path, it might be a colon-separated list of identifiers. + * + * This is NOT intended to be used for anything except debug output. + * DO NOT try to parse this or use it to open a file. + */ + const char* getAssetSource(void) const { return mAssetSource.string(); } + +protected: + /* + * Adds this Asset to the global Asset list for debugging and + * accounting. + * Concrete subclasses must call this in their constructor. + */ + static void registerAsset(Asset* asset); + + /* + * Removes this Asset from the global Asset list. + * Concrete subclasses must call this in their destructor. + */ + static void unregisterAsset(Asset* asset); + + Asset(void); // constructor; only invoked indirectly + + /* handle common seek() housekeeping */ + off64_t handleSeek(off64_t offset, int whence, off64_t curPosn, off64_t maxPosn); + + /* set the asset source string */ + void setAssetSource(const String8& path) { mAssetSource = path; } + + AccessMode getAccessMode(void) const { return mAccessMode; } + +private: + /* these operations are not implemented */ + Asset(const Asset& src); + Asset& operator=(const Asset& src); + + /* AssetManager needs access to our "create" functions */ + friend class AssetManager; + + /* + * Create the asset from a named file on disk. + */ + static Asset* createFromFile(const char* fileName, AccessMode mode); + + /* + * Create the asset from a named, compressed file on disk (e.g. ".gz"). + */ + static Asset* createFromCompressedFile(const char* fileName, + AccessMode mode); + +#if 0 + /* + * Create the asset from a segment of an open file. This will fail + * if "offset" and "length" don't fit within the bounds of the file. + * + * The asset takes ownership of the file descriptor. + */ + static Asset* createFromFileSegment(int fd, off64_t offset, size_t length, + AccessMode mode); + + /* + * Create from compressed data. "fd" should be seeked to the start of + * the compressed data. This could be inside a gzip file or part of a + * Zip archive. + * + * The asset takes ownership of the file descriptor. + * + * This may not verify the validity of the compressed data until first + * use. + */ + static Asset* createFromCompressedData(int fd, off64_t offset, + int compressionMethod, size_t compressedLength, + size_t uncompressedLength, AccessMode mode); +#endif + + /* + * Create the asset from a memory-mapped file segment. + * + * The asset takes ownership of the FileMap. + */ + static Asset* createFromUncompressedMap(FileMap* dataMap, AccessMode mode); + + /* + * Create the asset from a memory-mapped file segment with compressed + * data. + * + * The asset takes ownership of the FileMap. + */ + static Asset* createFromCompressedMap(FileMap* dataMap, + size_t uncompressedLen, AccessMode mode); + + + /* + * Create from a reference-counted chunk of shared memory. + */ + // TODO + + AccessMode mAccessMode; // how the asset was opened + String8 mAssetSource; // debug string + + Asset* mNext; // linked list. + Asset* mPrev; +}; + + +/* + * =========================================================================== + * + * Innards follow. Do not use these classes directly. + */ + +/* + * An asset based on an uncompressed file on disk. It may encompass the + * entire file or just a piece of it. Access is through fread/fseek. + */ +class _FileAsset : public Asset { +public: + _FileAsset(void); + virtual ~_FileAsset(void); + + /* + * Use a piece of an already-open file. + * + * On success, the object takes ownership of "fd". + */ + status_t openChunk(const char* fileName, int fd, off64_t offset, size_t length); + + /* + * Use a memory-mapped region. + * + * On success, the object takes ownership of "dataMap". + */ + status_t openChunk(FileMap* dataMap); + + /* + * Standard Asset interfaces. + */ + virtual ssize_t read(void* buf, size_t count); + virtual off64_t seek(off64_t offset, int whence); + virtual void close(void); + virtual const void* getBuffer(bool wordAligned); + virtual off64_t getLength(void) const { return mLength; } + virtual off64_t getRemainingLength(void) const { return mLength-mOffset; } + virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const; + virtual bool isAllocated(void) const { return mBuf != NULL; } + +private: + off64_t mStart; // absolute file offset of start of chunk + off64_t mLength; // length of the chunk + off64_t mOffset; // current local offset, 0 == mStart + FILE* mFp; // for read/seek + char* mFileName; // for opening + + /* + * To support getBuffer() we either need to read the entire thing into + * a buffer or memory-map it. For small files it's probably best to + * just read them in. + */ + enum { kReadVsMapThreshold = 4096 }; + + FileMap* mMap; // for memory map + unsigned char* mBuf; // for read + + const void* ensureAlignment(FileMap* map); +}; + + +/* + * An asset based on compressed data in a file. + */ +class _CompressedAsset : public Asset { +public: + _CompressedAsset(void); + virtual ~_CompressedAsset(void); + + /* + * Use a piece of an already-open file. + * + * On success, the object takes ownership of "fd". + */ + status_t openChunk(int fd, off64_t offset, int compressionMethod, + size_t uncompressedLen, size_t compressedLen); + + /* + * Use a memory-mapped region. + * + * On success, the object takes ownership of "fd". + */ + status_t openChunk(FileMap* dataMap, size_t uncompressedLen); + + /* + * Standard Asset interfaces. + */ + virtual ssize_t read(void* buf, size_t count); + virtual off64_t seek(off64_t offset, int whence); + virtual void close(void); + virtual const void* getBuffer(bool wordAligned); + virtual off64_t getLength(void) const { return mUncompressedLen; } + virtual off64_t getRemainingLength(void) const { return mUncompressedLen-mOffset; } + virtual int openFileDescriptor(off64_t* /* outStart */, off64_t* /* outLength */) const { return -1; } + virtual bool isAllocated(void) const { return mBuf != NULL; } + +private: + off64_t mStart; // offset to start of compressed data + off64_t mCompressedLen; // length of the compressed data + off64_t mUncompressedLen; // length of the uncompressed data + off64_t mOffset; // current offset, 0 == start of uncomp data + + FileMap* mMap; // for memory-mapped input + int mFd; // for file input + + class StreamingZipInflater* mZipInflater; // for streaming large compressed assets + + unsigned char* mBuf; // for getBuffer() +}; + +// need: shared mmap version? + +}; // namespace android + +#endif // __LIBS_ASSET_H diff --git a/libs/androidfw/include/androidfw/AssetDir.h b/libs/androidfw/include/androidfw/AssetDir.h new file mode 100644 index 000000000000..bd89d7d34b97 --- /dev/null +++ b/libs/androidfw/include/androidfw/AssetDir.h @@ -0,0 +1,145 @@ +/* + * Copyright (C) 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// +// Access a chunk of the asset hierarchy as if it were a single directory. +// +#ifndef __LIBS_ASSETDIR_H +#define __LIBS_ASSETDIR_H + +#include <androidfw/misc.h> +#include <utils/String8.h> +#include <utils/Vector.h> +#include <utils/SortedVector.h> +#include <sys/types.h> + +namespace android { + +/* + * This provides vector-style access to a directory. We do this rather + * than modeling opendir/readdir access because it's simpler and the + * nature of the operation requires us to have all data on hand anyway. + * + * The list of files will be sorted in ascending order by ASCII value. + * + * The contents are populated by our friend, the AssetManager. + */ +class AssetDir { +public: + AssetDir(void) + : mFileInfo(NULL) + {} + virtual ~AssetDir(void) { + delete mFileInfo; + } + + /* + * Vector-style access. + */ + size_t getFileCount(void) { return mFileInfo->size(); } + const String8& getFileName(int idx) { + return mFileInfo->itemAt(idx).getFileName(); + } + const String8& getSourceName(int idx) { + return mFileInfo->itemAt(idx).getSourceName(); + } + + /* + * Get the type of a file (usually regular or directory). + */ + FileType getFileType(int idx) { + return mFileInfo->itemAt(idx).getFileType(); + } + +private: + /* these operations are not implemented */ + AssetDir(const AssetDir& src); + const AssetDir& operator=(const AssetDir& src); + + friend class AssetManager; + + /* + * This holds information about files in the asset hierarchy. + */ + class FileInfo { + public: + FileInfo(void) {} + FileInfo(const String8& path) // useful for e.g. svect.indexOf + : mFileName(path), mFileType(kFileTypeUnknown) + {} + ~FileInfo(void) {} + FileInfo(const FileInfo& src) { + copyMembers(src); + } + const FileInfo& operator= (const FileInfo& src) { + if (this != &src) + copyMembers(src); + return *this; + } + + void copyMembers(const FileInfo& src) { + mFileName = src.mFileName; + mFileType = src.mFileType; + mSourceName = src.mSourceName; + } + + /* need this for SortedVector; must compare only on file name */ + bool operator< (const FileInfo& rhs) const { + return mFileName < rhs.mFileName; + } + + /* used by AssetManager */ + bool operator== (const FileInfo& rhs) const { + return mFileName == rhs.mFileName; + } + + void set(const String8& path, FileType type) { + mFileName = path; + mFileType = type; + } + + const String8& getFileName(void) const { return mFileName; } + void setFileName(const String8& path) { mFileName = path; } + + FileType getFileType(void) const { return mFileType; } + void setFileType(FileType type) { mFileType = type; } + + const String8& getSourceName(void) const { return mSourceName; } + void setSourceName(const String8& path) { mSourceName = path; } + + /* + * Handy utility for finding an entry in a sorted vector of FileInfo. + * Returns the index of the matching entry, or -1 if none found. + */ + static int findEntry(const SortedVector<FileInfo>* pVector, + const String8& fileName); + + private: + String8 mFileName; // filename only + FileType mFileType; // regular, directory, etc + + String8 mSourceName; // currently debug-only + }; + + /* AssetManager uses this to initialize us */ + void setFileList(SortedVector<FileInfo>* list) { mFileInfo = list; } + + SortedVector<FileInfo>* mFileInfo; +}; + +}; // namespace android + +#endif // __LIBS_ASSETDIR_H diff --git a/libs/androidfw/include/androidfw/AssetManager.h b/libs/androidfw/include/androidfw/AssetManager.h new file mode 100644 index 000000000000..594dba5049dd --- /dev/null +++ b/libs/androidfw/include/androidfw/AssetManager.h @@ -0,0 +1,396 @@ +/* + * Copyright (C) 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// +// Asset management class. AssetManager objects are thread-safe. +// +#ifndef __LIBS_ASSETMANAGER_H +#define __LIBS_ASSETMANAGER_H + +#include <androidfw/Asset.h> +#include <androidfw/AssetDir.h> +#include <androidfw/ZipFileRO.h> +#include <utils/KeyedVector.h> +#include <utils/SortedVector.h> +#include <utils/String16.h> +#include <utils/String8.h> +#include <utils/threads.h> +#include <utils/Vector.h> + +/* + * Native-app access is via the opaque typedef struct AAssetManager in the C namespace. + */ +struct AAssetManager { }; + +/* + * Now the proper C++ android-namespace definitions + */ + +namespace android { + +class Asset; // fwd decl for things that include Asset.h first +class ResTable; +struct ResTable_config; + +/* + * Every application that uses assets needs one instance of this. A + * single instance may be shared across multiple threads, and a single + * thread may have more than one instance (the latter is discouraged). + * + * The purpose of the AssetManager is to create Asset objects. To do + * this efficiently it may cache information about the locations of + * files it has seen. This can be controlled with the "cacheMode" + * argument. + * + * The asset hierarchy may be examined like a filesystem, using + * AssetDir objects to peruse a single directory. + */ +class AssetManager : public AAssetManager { +public: + static const char* RESOURCES_FILENAME; + static const char* IDMAP_BIN; + static const char* OVERLAY_DIR; + /* + * If OVERLAY_THEME_DIR_PROPERTY is set, search for runtime resource overlay + * APKs in OVERLAY_DIR/<value of OVERLAY_THEME_DIR_PROPERTY> in addition to + * OVERLAY_DIR. + */ + static const char* OVERLAY_THEME_DIR_PROPERTY; + static const char* TARGET_PACKAGE_NAME; + static const char* TARGET_APK_PATH; + static const char* IDMAP_DIR; + + typedef enum CacheMode { + CACHE_UNKNOWN = 0, + CACHE_OFF, // don't try to cache file locations + CACHE_DEFER, // construct cache as pieces are needed + //CACHE_SCAN, // scan full(!) asset hierarchy at init() time + } CacheMode; + + AssetManager(CacheMode cacheMode = CACHE_OFF); + virtual ~AssetManager(void); + + static int32_t getGlobalCount(); + + /* + * Add a new source for assets. This can be called multiple times to + * 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 + * added asset path will be examined first when searching for assets, + * 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, + * then on success, *cookie is set to the value corresponding to the + * newly-added asset source. + */ + bool addAssetPath(const String8& path, int32_t* cookie, + bool appAsLib=false, bool isSystemAsset=false); + bool addOverlayPath(const String8& path, int32_t* cookie); + + /* + * Convenience for adding the standard system assets. Uses the + * ANDROID_ROOT environment variable to find them. + */ + bool addDefaultAssets(); + + /* + * Iterate over the asset paths in this manager. (Previously + * added via addAssetPath() and addDefaultAssets().) On first call, + * 'cookie' must be 0, resulting in the first cookie being returned. + * Each next cookie will be returned there-after, until -1 indicating + * the end has been reached. + */ + int32_t nextAssetPath(const int32_t cookie) const; + + /* + * Return an asset path in the manager. 'which' must be between 0 and + * countAssetPaths(). + */ + String8 getAssetPath(const int32_t cookie) const; + + /* + * Set the current locale and vendor. The locale can change during + * the lifetime of an AssetManager if the user updates the device's + * language setting. The vendor is less likely to change. + * + * Pass in NULL to indicate no preference. + */ + void setLocale(const char* locale); + void setVendor(const char* vendor); + + /* + * Choose screen orientation for resources values returned. + */ + void setConfiguration(const ResTable_config& config, const char* locale = NULL); + + void getConfiguration(ResTable_config* outConfig) const; + + typedef Asset::AccessMode AccessMode; // typing shortcut + + /* + * Open an asset. + * + * This will search through locale-specific and vendor-specific + * directories and packages to find the file. + * + * The object returned does not depend on the AssetManager. It should + * be freed by calling Asset::close(). + */ + Asset* open(const char* fileName, AccessMode mode); + + /* + * Open a non-asset file as an asset. + * + * This is for opening files that are included in an asset package + * but aren't assets. These sit outside the usual "locale/vendor" + * path hierarchy, and will not be seen by "AssetDir" or included + * in our filename cache. + */ + Asset* openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie = NULL); + + /* + * Explicit non-asset file. The file explicitly named by the cookie (the + * resource set to look in) and fileName will be opened and returned. + */ + Asset* openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode); + + /* + * Open a directory within the asset hierarchy. + * + * The contents of the directory are an amalgam of vendor-specific, + * locale-specific, and generic assets stored loosely or in asset + * packages. Depending on the cache setting and previous accesses, + * this call may incur significant disk overhead. + * + * To open the top-level directory, pass in "". + */ + AssetDir* openDir(const char* dirName); + + /* + * Open a directory within a particular path of the asset manager. + * + * The contents of the directory are an amalgam of vendor-specific, + * locale-specific, and generic assets stored loosely or in asset + * packages. Depending on the cache setting and previous accesses, + * this call may incur significant disk overhead. + * + * To open the top-level directory, pass in "". + */ + AssetDir* openNonAssetDir(const int32_t cookie, const char* dirName); + + /* + * Get the type of a file in the asset hierarchy. They will either + * be "regular" or "directory". [Currently only works for "regular".] + * + * Can also be used as a quick test for existence of a file. + */ + FileType getFileType(const char* fileName); + + /* + * Return the complete resource table to find things in the package. + */ + const ResTable& getResources(bool required = true) const; + + /* + * Discard cached filename information. This only needs to be called + * if somebody has updated the set of "loose" files, and we want to + * discard our cached notion of what's where. + */ + void purge(void) { purgeFileNameCacheLocked(); } + + /* + * Return true if the files this AssetManager references are all + * up-to-date (have not been changed since it was created). If false + * is returned, you will need to create a new AssetManager to get + * the current data. + */ + bool isUpToDate(); + + /** + * Get the known locales for this asset manager object. + */ + void getLocales(Vector<String8>* locales, bool includeSystemLocales=true) const; + + /** + * Generate idmap data to translate resources IDs between a package and a + * corresponding overlay package. + */ + bool createIdmap(const char* targetApkPath, const char* overlayApkPath, + uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize); + +private: + struct asset_path + { + asset_path() : path(""), type(kFileTypeRegular), idmap(""), + isSystemOverlay(false), isSystemAsset(false) {} + String8 path; + FileType type; + String8 idmap; + bool isSystemOverlay; + bool isSystemAsset; + }; + + Asset* openInPathLocked(const char* fileName, AccessMode mode, + const asset_path& path); + Asset* openNonAssetInPathLocked(const char* fileName, AccessMode mode, + const asset_path& path); + Asset* openInLocaleVendorLocked(const char* fileName, AccessMode mode, + const asset_path& path, const char* locale, const char* vendor); + String8 createPathNameLocked(const asset_path& path, const char* locale, + const char* vendor); + String8 createPathNameLocked(const asset_path& path, const char* rootDir); + String8 createZipSourceNameLocked(const String8& zipFileName, + const String8& dirName, const String8& fileName); + + ZipFileRO* getZipFileLocked(const asset_path& path); + Asset* openAssetFromFileLocked(const String8& fileName, AccessMode mode); + Asset* openAssetFromZipLocked(const ZipFileRO* pZipFile, + const ZipEntryRO entry, AccessMode mode, const String8& entryName); + + bool scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, + const asset_path& path, const char* rootDir, const char* dirName); + SortedVector<AssetDir::FileInfo>* scanDirLocked(const String8& path); + bool scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, + const asset_path& path, const char* rootDir, const char* dirName); + void mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, + const SortedVector<AssetDir::FileInfo>* pContents); + + void loadFileNameCacheLocked(void); + void fncScanLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, + const char* dirName); + bool fncScanAndMergeDirLocked( + SortedVector<AssetDir::FileInfo>* pMergedInfo, + const asset_path& path, const char* locale, const char* vendor, + const char* dirName); + void purgeFileNameCacheLocked(void); + + const ResTable* getResTable(bool required = true) const; + void setLocaleLocked(const char* locale); + void updateResourceParamsLocked() const; + bool appendPathToResTable(const asset_path& ap, bool appAsLib=false) const; + + Asset* openIdmapLocked(const struct asset_path& ap) const; + + void addSystemOverlays(const char* pathOverlaysList, const String8& targetPackagePath, + ResTable* sharedRes, size_t offset) const; + + class SharedZip : public RefBase { + public: + static sp<SharedZip> get(const String8& path, bool createIfNotPresent = true); + + ZipFileRO* getZip(); + + Asset* getResourceTableAsset(); + Asset* setResourceTableAsset(Asset* asset); + + ResTable* getResourceTable(); + ResTable* setResourceTable(ResTable* res); + + bool isUpToDate(); + + void addOverlay(const asset_path& ap); + bool getOverlay(size_t idx, asset_path* out) const; + + protected: + ~SharedZip(); + + private: + SharedZip(const String8& path, time_t modWhen); + SharedZip(); // <-- not implemented + + String8 mPath; + ZipFileRO* mZipFile; + time_t mModWhen; + + Asset* mResourceTableAsset; + ResTable* mResourceTable; + + Vector<asset_path> mOverlays; + + static Mutex gLock; + static DefaultKeyedVector<String8, wp<SharedZip> > gOpen; + }; + + /* + * Manage a set of Zip files. For each file we need a pointer to the + * ZipFile and a time_t with the file's modification date. + * + * We currently only have two zip files (current app, "common" app). + * (This was originally written for 8, based on app/locale/vendor.) + */ + class ZipSet { + public: + ZipSet(void); + ~ZipSet(void); + + /* + * Return a ZipFileRO structure for a ZipFileRO with the specified + * parameters. + */ + ZipFileRO* getZip(const String8& path); + + Asset* getZipResourceTableAsset(const String8& path); + Asset* setZipResourceTableAsset(const String8& path, Asset* asset); + + ResTable* getZipResourceTable(const String8& path); + ResTable* setZipResourceTable(const String8& path, ResTable* res); + + // generate path, e.g. "common/en-US-noogle.zip" + static String8 getPathName(const char* path); + + bool isUpToDate(); + + void addOverlay(const String8& path, const asset_path& overlay); + bool getOverlay(const String8& path, size_t idx, asset_path* out) const; + + private: + void closeZip(int idx); + + int getIndex(const String8& zip) const; + mutable Vector<String8> mZipPath; + mutable Vector<sp<SharedZip> > mZipFile; + }; + + // Protect all internal state. + mutable Mutex mLock; + + ZipSet mZipSet; + + Vector<asset_path> mAssetPaths; + char* mLocale; + char* mVendor; + + mutable ResTable* mResources; + ResTable_config* mConfig; + + /* + * Cached data for "loose" files. This lets us avoid poking at the + * filesystem when searching for loose assets. Each entry is the + * "extended partial" path, e.g. "default/default/foo/bar.txt". The + * full set of files is present, including ".EXCLUDE" entries. + * + * We do not cache directory names. We don't retain the ".gz", + * because to our clients "foo" and "foo.gz" both look like "foo". + */ + CacheMode mCacheMode; // is the cache enabled? + bool mCacheValid; // clear when locale or vendor changes + SortedVector<AssetDir::FileInfo> mCache; +}; + +}; // namespace android + +#endif // __LIBS_ASSETMANAGER_H diff --git a/libs/androidfw/include/androidfw/AttributeFinder.h b/libs/androidfw/include/androidfw/AttributeFinder.h new file mode 100644 index 000000000000..acf70565c4f9 --- /dev/null +++ b/libs/androidfw/include/androidfw/AttributeFinder.h @@ -0,0 +1,206 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef H_ATTRIBUTE_FINDER +#define H_ATTRIBUTE_FINDER + +#include <stdint.h> +#include <utils/KeyedVector.h> + +namespace android { + +static inline uint32_t getPackage(uint32_t attr) { + return attr >> 24; +} + +/** + * A helper class to search linearly for the requested + * attribute, maintaining it's position and optimizing for + * the case that subsequent searches will involve an attribute with + * a higher attribute ID. + * + * In the case that a subsequent attribute has a different package ID, + * its resource ID may not be larger than the preceding search, so + * back tracking is supported for this case. This + * back tracking requirement is mainly for shared library + * resources, whose package IDs get assigned at runtime + * and thus attributes from a shared library may + * be out of order. + * + * We make two assumptions about the order of attributes: + * 1) The input has the same sorting rules applied to it as + * the attribute data contained by this class. + * 2) Attributes are grouped by package ID. + * 3) Among attributes with the same package ID, the attributes are + * sorted by increasing resource ID. + * + * Ex: 02010000, 02010001, 010100f4, 010100f5, 0x7f010001, 07f010003 + * + * The total order of attributes (including package ID) can not be linear + * as shared libraries get assigned dynamic package IDs at runtime, which + * may break the sort order established at build time. + */ +template <typename Derived, typename Iterator> +class BackTrackingAttributeFinder { +public: + BackTrackingAttributeFinder(const Iterator& begin, const Iterator& end); + + Iterator find(uint32_t attr); + +private: + void jumpToClosestAttribute(uint32_t packageId); + void markCurrentPackageId(uint32_t packageId); + + bool mFirstTime; + Iterator mBegin; + Iterator mEnd; + Iterator mCurrent; + Iterator mLargest; + uint32_t mLastPackageId; + uint32_t mCurrentAttr; + + // Package Offsets (best-case, fast look-up). + Iterator mFrameworkStart; + Iterator mAppStart; + + // Worst case, we have shared-library resources. + KeyedVector<uint32_t, Iterator> mPackageOffsets; +}; + +template <typename Derived, typename Iterator> inline +BackTrackingAttributeFinder<Derived, Iterator>::BackTrackingAttributeFinder(const Iterator& begin, const Iterator& end) + : mFirstTime(true) + , mBegin(begin) + , mEnd(end) + , mCurrent(begin) + , mLargest(begin) + , mLastPackageId(0) + , mCurrentAttr(0) + , mFrameworkStart(end) + , mAppStart(end) { +} + +template <typename Derived, typename Iterator> +void BackTrackingAttributeFinder<Derived, Iterator>::jumpToClosestAttribute(const uint32_t packageId) { + switch (packageId) { + case 0x01: + mCurrent = mFrameworkStart; + break; + case 0x7f: + mCurrent = mAppStart; + break; + default: { + ssize_t idx = mPackageOffsets.indexOfKey(packageId); + if (idx >= 0) { + // We have seen this package ID before, so jump to the first + // attribute with this package ID. + mCurrent = mPackageOffsets[idx]; + } else { + mCurrent = mEnd; + } + break; + } + } + + // We have never seen this package ID yet, so jump to the + // latest/largest index we have processed so far. + if (mCurrent == mEnd) { + mCurrent = mLargest; + } + + if (mCurrent != mEnd) { + mCurrentAttr = static_cast<const Derived*>(this)->getAttribute(mCurrent); + } +} + +template <typename Derived, typename Iterator> +void BackTrackingAttributeFinder<Derived, Iterator>::markCurrentPackageId(const uint32_t packageId) { + switch (packageId) { + case 0x01: + mFrameworkStart = mCurrent; + break; + case 0x7f: + mAppStart = mCurrent; + break; + default: + mPackageOffsets.add(packageId, mCurrent); + break; + } +} + +template <typename Derived, typename Iterator> +Iterator BackTrackingAttributeFinder<Derived, Iterator>::find(uint32_t attr) { + if (!(mBegin < mEnd)) { + return mEnd; + } + + if (mFirstTime) { + // One-time initialization. We do this here instead of the constructor + // because the derived class we access in getAttribute() may not be + // fully constructed. + mFirstTime = false; + mCurrentAttr = static_cast<const Derived*>(this)->getAttribute(mBegin); + mLastPackageId = getPackage(mCurrentAttr); + markCurrentPackageId(mLastPackageId); + } + + // Looking for the needle (attribute we're looking for) + // in the haystack (the attributes we're searching through) + const uint32_t needlePackageId = getPackage(attr); + if (mLastPackageId != needlePackageId) { + jumpToClosestAttribute(needlePackageId); + mLastPackageId = needlePackageId; + } + + // Walk through the xml attributes looking for the requested attribute. + while (mCurrent != mEnd) { + const uint32_t haystackPackageId = getPackage(mCurrentAttr); + if (needlePackageId == haystackPackageId && attr < mCurrentAttr) { + // The attribute we are looking was not found. + break; + } + const uint32_t prevAttr = mCurrentAttr; + + // Move to the next attribute in the XML. + ++mCurrent; + if (mCurrent != mEnd) { + mCurrentAttr = static_cast<const Derived*>(this)->getAttribute(mCurrent); + const uint32_t newHaystackPackageId = getPackage(mCurrentAttr); + if (haystackPackageId != newHaystackPackageId) { + // We've moved to the next group of attributes + // with a new package ID, so we should record + // the offset of this new package ID. + markCurrentPackageId(newHaystackPackageId); + } + } + + if (mCurrent > mLargest) { + // We've moved past the latest attribute we've + // seen. + mLargest = mCurrent; + } + + if (attr == prevAttr) { + // We found the attribute we were looking for. + return mCurrent - 1; + } + } + return mEnd; +} + +} // namespace android + +#endif // H_ATTRIBUTE_FINDER diff --git a/libs/androidfw/include/androidfw/BackupHelpers.h b/libs/androidfw/include/androidfw/BackupHelpers.h new file mode 100644 index 000000000000..fc1ad4717c16 --- /dev/null +++ b/libs/androidfw/include/androidfw/BackupHelpers.h @@ -0,0 +1,172 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _UTILS_BACKUP_HELPERS_H +#define _UTILS_BACKUP_HELPERS_H + +#include <sys/stat.h> + +#include <utils/Errors.h> +#include <utils/String8.h> +#include <utils/KeyedVector.h> + +namespace android { + +enum { + BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian) +}; + +typedef struct { + int type; // BACKUP_HEADER_ENTITY_V1 + int keyLen; // length of the key name, not including the null terminator + int dataSize; // size of the data, not including the padding, -1 means delete +} entity_header_v1; + +struct SnapshotHeader { + int magic0; + int fileCount; + int magic1; + int totalSize; +}; + +struct FileState { + int modTime_sec; + int modTime_nsec; + int mode; + int size; + int crc32; + int nameLen; +}; + +struct FileRec { + String8 file; + bool deleted; + FileState s; +}; + + +/** + * Writes the data. + * + * If an error occurs, it poisons this object and all write calls will fail + * with the error that occurred. + */ +class BackupDataWriter +{ +public: + BackupDataWriter(int fd); + // does not close fd + ~BackupDataWriter(); + + status_t WriteEntityHeader(const String8& key, size_t dataSize); + + /* Note: WriteEntityData will write arbitrary data into the file without + * validation or a previously-supplied header. The full backup implementation + * uses it this way to generate a controlled binary stream that is not + * entity-structured. If the implementation here is changed, either this + * use case must remain valid, or the full backup implementation should be + * adjusted to use some other appropriate mechanism. + */ + status_t WriteEntityData(const void* data, size_t size); + + void SetKeyPrefix(const String8& keyPrefix); + +private: + explicit BackupDataWriter(); + status_t write_padding_for(int n); + + int m_fd; + status_t m_status; + ssize_t m_pos; + int m_entityCount; + String8 m_keyPrefix; +}; + +/** + * Reads the data. + * + * If an error occurs, it poisons this object and all write calls will fail + * with the error that occurred. + */ +class BackupDataReader +{ +public: + BackupDataReader(int fd); + // does not close fd + ~BackupDataReader(); + + status_t Status(); + status_t ReadNextHeader(bool* done, int* type); + + bool HasEntities(); + status_t ReadEntityHeader(String8* key, size_t* dataSize); + status_t SkipEntityData(); // must be called with the pointer at the beginning of the data. + ssize_t ReadEntityData(void* data, size_t size); + +private: + explicit BackupDataReader(); + status_t skip_padding(); + + int m_fd; + bool m_done; + status_t m_status; + ssize_t m_pos; + ssize_t m_dataEndPos; + int m_entityCount; + union { + int type; + entity_header_v1 entity; + } m_header; + String8 m_key; +}; + +int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD, + char const* const* files, char const* const *keys, int fileCount); + +int write_tarfile(const String8& packageName, const String8& domain, + const String8& rootPath, const String8& filePath, off_t* outSize, + BackupDataWriter* outputStream); + +class RestoreHelperBase +{ +public: + RestoreHelperBase(); + ~RestoreHelperBase(); + + status_t WriteFile(const String8& filename, BackupDataReader* in); + status_t WriteSnapshot(int fd); + +private: + void* m_buf; + bool m_loggedUnknownMetadata; + KeyedVector<String8,FileRec> m_files; +}; + +//#define TEST_BACKUP_HELPERS 1 + +#if TEST_BACKUP_HELPERS +int backup_helper_test_empty(); +int backup_helper_test_four(); +int backup_helper_test_files(); +int backup_helper_test_null_base(); +int backup_helper_test_missing_file(); +int backup_helper_test_data_writer(); +int backup_helper_test_data_reader(); +#endif + +} // namespace android + +#endif // _UTILS_BACKUP_HELPERS_H diff --git a/libs/androidfw/include/androidfw/ByteBucketArray.h b/libs/androidfw/include/androidfw/ByteBucketArray.h new file mode 100644 index 000000000000..87c6b128eca1 --- /dev/null +++ b/libs/androidfw/include/androidfw/ByteBucketArray.h @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __BYTE_BUCKET_ARRAY_H +#define __BYTE_BUCKET_ARRAY_H + +#include <utils/Log.h> +#include <stdint.h> +#include <string.h> + +namespace android { + +/** + * Stores a sparsely populated array. Has a fixed size of 256 + * (number of entries that a byte can represent). + */ +template<typename T> +class ByteBucketArray { +public: + ByteBucketArray() : mDefault() { + memset(mBuckets, 0, sizeof(mBuckets)); + } + + ~ByteBucketArray() { + for (size_t i = 0; i < NUM_BUCKETS; i++) { + if (mBuckets[i] != NULL) { + delete [] mBuckets[i]; + } + } + memset(mBuckets, 0, sizeof(mBuckets)); + } + + inline size_t size() const { + return NUM_BUCKETS * BUCKET_SIZE; + } + + inline const T& get(size_t index) const { + return (*this)[index]; + } + + const T& operator[](size_t index) const { + if (index >= size()) { + return mDefault; + } + + uint8_t bucketIndex = static_cast<uint8_t>(index) >> 4; + T* bucket = mBuckets[bucketIndex]; + if (bucket == NULL) { + return mDefault; + } + return bucket[0x0f & static_cast<uint8_t>(index)]; + } + + T& editItemAt(size_t index) { + ALOG_ASSERT(index < size(), "ByteBucketArray.getOrCreate(index=%u) with size=%u", + (uint32_t) index, (uint32_t) size()); + + uint8_t bucketIndex = static_cast<uint8_t>(index) >> 4; + T* bucket = mBuckets[bucketIndex]; + if (bucket == NULL) { + bucket = mBuckets[bucketIndex] = new T[BUCKET_SIZE](); + } + return bucket[0x0f & static_cast<uint8_t>(index)]; + } + + bool set(size_t index, const T& value) { + if (index >= size()) { + return false; + } + + editItemAt(index) = value; + return true; + } + +private: + enum { NUM_BUCKETS = 16, BUCKET_SIZE = 16 }; + + T* mBuckets[NUM_BUCKETS]; + T mDefault; +}; + +} // namespace android + +#endif // __BYTE_BUCKET_ARRAY_H diff --git a/libs/androidfw/include/androidfw/CursorWindow.h b/libs/androidfw/include/androidfw/CursorWindow.h new file mode 100644 index 000000000000..8a2979a3756d --- /dev/null +++ b/libs/androidfw/include/androidfw/CursorWindow.h @@ -0,0 +1,193 @@ +/* + * Copyright (C) 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _ANDROID__DATABASE_WINDOW_H +#define _ANDROID__DATABASE_WINDOW_H + +#include <cutils/log.h> +#include <stddef.h> +#include <stdint.h> + +#include <binder/Parcel.h> +#include <utils/String8.h> + +#if LOG_NDEBUG + +#define IF_LOG_WINDOW() if (false) +#define LOG_WINDOW(...) + +#else + +#define IF_LOG_WINDOW() IF_ALOG(LOG_DEBUG, "CursorWindow") +#define LOG_WINDOW(...) ALOG(LOG_DEBUG, "CursorWindow", __VA_ARGS__) + +#endif + +namespace android { + +/** + * This class stores a set of rows from a database in a buffer. The begining of the + * window has first chunk of RowSlots, which are offsets to the row directory, followed by + * an offset to the next chunk in a linked-list of additional chunk of RowSlots in case + * the pre-allocated chunk isn't big enough to refer to all rows. Each row directory has a + * FieldSlot per column, which has the size, offset, and type of the data for that field. + * Note that the data types come from sqlite3.h. + * + * Strings are stored in UTF-8. + */ +class CursorWindow { + CursorWindow(const String8& name, int ashmemFd, + void* data, size_t size, bool readOnly); + +public: + /* Field types. */ + enum { + FIELD_TYPE_NULL = 0, + FIELD_TYPE_INTEGER = 1, + FIELD_TYPE_FLOAT = 2, + FIELD_TYPE_STRING = 3, + FIELD_TYPE_BLOB = 4, + }; + + /* Opaque type that describes a field slot. */ + struct FieldSlot { + private: + int32_t type; + union { + double d; + int64_t l; + struct { + uint32_t offset; + uint32_t size; + } buffer; + } data; + + friend class CursorWindow; + } __attribute((packed)); + + ~CursorWindow(); + + static status_t create(const String8& name, size_t size, CursorWindow** outCursorWindow); + static status_t createFromParcel(Parcel* parcel, CursorWindow** outCursorWindow); + + status_t writeToParcel(Parcel* parcel); + + inline String8 name() { return mName; } + inline size_t size() { return mSize; } + inline size_t freeSpace() { return mSize - mHeader->freeOffset; } + inline uint32_t getNumRows() { return mHeader->numRows; } + inline uint32_t getNumColumns() { return mHeader->numColumns; } + + status_t clear(); + status_t setNumColumns(uint32_t numColumns); + + /** + * Allocate a row slot and its directory. + * The row is initialized will null entries for each field. + */ + status_t allocRow(); + status_t freeLastRow(); + + status_t putBlob(uint32_t row, uint32_t column, const void* value, size_t size); + status_t putString(uint32_t row, uint32_t column, const char* value, size_t sizeIncludingNull); + status_t putLong(uint32_t row, uint32_t column, int64_t value); + status_t putDouble(uint32_t row, uint32_t column, double value); + status_t putNull(uint32_t row, uint32_t column); + + /** + * Gets the field slot at the specified row and column. + * Returns null if the requested row or column is not in the window. + */ + FieldSlot* getFieldSlot(uint32_t row, uint32_t column); + + inline int32_t getFieldSlotType(FieldSlot* fieldSlot) { + return fieldSlot->type; + } + + inline int64_t getFieldSlotValueLong(FieldSlot* fieldSlot) { + return fieldSlot->data.l; + } + + inline double getFieldSlotValueDouble(FieldSlot* fieldSlot) { + return fieldSlot->data.d; + } + + inline const char* getFieldSlotValueString(FieldSlot* fieldSlot, + size_t* outSizeIncludingNull) { + *outSizeIncludingNull = fieldSlot->data.buffer.size; + return static_cast<char*>(offsetToPtr(fieldSlot->data.buffer.offset)); + } + + inline const void* getFieldSlotValueBlob(FieldSlot* fieldSlot, size_t* outSize) { + *outSize = fieldSlot->data.buffer.size; + return offsetToPtr(fieldSlot->data.buffer.offset); + } + +private: + static const size_t ROW_SLOT_CHUNK_NUM_ROWS = 100; + + struct Header { + // Offset of the lowest unused byte in the window. + uint32_t freeOffset; + + // Offset of the first row slot chunk. + uint32_t firstChunkOffset; + + uint32_t numRows; + uint32_t numColumns; + }; + + struct RowSlot { + uint32_t offset; + }; + + struct RowSlotChunk { + RowSlot slots[ROW_SLOT_CHUNK_NUM_ROWS]; + uint32_t nextChunkOffset; + }; + + String8 mName; + int mAshmemFd; + void* mData; + size_t mSize; + bool mReadOnly; + Header* mHeader; + + inline void* offsetToPtr(uint32_t offset) { + return static_cast<uint8_t*>(mData) + offset; + } + + inline uint32_t offsetFromPtr(void* ptr) { + return static_cast<uint8_t*>(ptr) - static_cast<uint8_t*>(mData); + } + + /** + * Allocate a portion of the window. Returns the offset + * of the allocation, or 0 if there isn't enough space. + * If aligned is true, the allocation gets 4 byte alignment. + */ + uint32_t alloc(size_t size, bool aligned = false); + + RowSlot* getRowSlot(uint32_t row); + RowSlot* allocRowSlot(); + + status_t putBlobOrString(uint32_t row, uint32_t column, + const void* value, size_t size, int32_t type); +}; + +}; // namespace android + +#endif diff --git a/libs/androidfw/include/androidfw/DisplayEventDispatcher.h b/libs/androidfw/include/androidfw/DisplayEventDispatcher.h new file mode 100644 index 000000000000..3ade2156589a --- /dev/null +++ b/libs/androidfw/include/androidfw/DisplayEventDispatcher.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gui/DisplayEventReceiver.h> +#include <utils/Log.h> +#include <utils/Looper.h> + +namespace android { + +class DisplayEventDispatcher : public LooperCallback { +public: + DisplayEventDispatcher(const sp<Looper>& looper); + + status_t initialize(); + void dispose(); + status_t scheduleVsync(); + +protected: + virtual ~DisplayEventDispatcher() = default; + +private: + sp<Looper> mLooper; + DisplayEventReceiver mReceiver; + bool mWaitingForVsync; + + virtual void dispatchVsync(nsecs_t timestamp, int32_t id, uint32_t count) = 0; + virtual void dispatchHotplug(nsecs_t timestamp, int32_t id, bool connected) = 0; + + virtual int handleEvent(int receiveFd, int events, void* data); + bool processPendingEvents(nsecs_t* outTimestamp, int32_t* id, uint32_t* outCount); +}; +} diff --git a/libs/androidfw/include/androidfw/LocaleData.h b/libs/androidfw/include/androidfw/LocaleData.h new file mode 100644 index 000000000000..b14829d07f04 --- /dev/null +++ b/libs/androidfw/include/androidfw/LocaleData.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _LIBS_UTILS_LOCALE_DATA_H +#define _LIBS_UTILS_LOCALE_DATA_H + +#include <stddef.h> +#include <stdint.h> + +namespace android { + +int localeDataCompareRegions( + const char* left_region, const char* right_region, + const char* requested_language, const char* requested_script, + const char* requested_region); + +void localeDataComputeScript(char out[4], const char* language, const char* region); + +bool localeDataIsCloseToUsEnglish(const char* region); + +} // namespace android + +#endif // _LIBS_UTILS_LOCALE_DATA_H diff --git a/libs/androidfw/include/androidfw/ObbFile.h b/libs/androidfw/include/androidfw/ObbFile.h new file mode 100644 index 000000000000..3dbf997dc367 --- /dev/null +++ b/libs/androidfw/include/androidfw/ObbFile.h @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OBBFILE_H_ +#define OBBFILE_H_ + +#include <stdint.h> +#include <strings.h> + +#include <utils/RefBase.h> +#include <utils/String8.h> + +namespace android { + +// OBB flags (bit 0) +#define OBB_OVERLAY (1 << 0) +#define OBB_SALTED (1 << 1) + +class ObbFile : public RefBase { +protected: + virtual ~ObbFile(); + +public: + ObbFile(); + + bool readFrom(const char* filename); + bool readFrom(int fd); + bool writeTo(const char* filename); + bool writeTo(int fd); + bool removeFrom(const char* filename); + bool removeFrom(int fd); + + const char* getFileName() const { + return mFileName; + } + + const String8 getPackageName() const { + return mPackageName; + } + + void setPackageName(String8 packageName) { + mPackageName = packageName; + } + + int32_t getVersion() const { + return mVersion; + } + + void setVersion(int32_t version) { + mVersion = version; + } + + int32_t getFlags() const { + return mFlags; + } + + void setFlags(int32_t flags) { + mFlags = flags; + } + + const unsigned char* getSalt(size_t* length) const { + if ((mFlags & OBB_SALTED) == 0) { + *length = 0; + return NULL; + } + + *length = sizeof(mSalt); + return mSalt; + } + + bool setSalt(const unsigned char* salt, size_t length) { + if (length != sizeof(mSalt)) { + return false; + } + + memcpy(mSalt, salt, sizeof(mSalt)); + mFlags |= OBB_SALTED; + return true; + } + + bool isOverlay() { + return (mFlags & OBB_OVERLAY) == OBB_OVERLAY; + } + + void setOverlay(bool overlay) { + if (overlay) { + mFlags |= OBB_OVERLAY; + } else { + mFlags &= ~OBB_OVERLAY; + } + } + + static inline uint32_t get4LE(const unsigned char* buf) { + return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24); + } + + static inline void put4LE(unsigned char* buf, uint32_t val) { + buf[0] = val & 0xFF; + buf[1] = (val >> 8) & 0xFF; + buf[2] = (val >> 16) & 0xFF; + buf[3] = (val >> 24) & 0xFF; + } + +private: + /* Package name this ObbFile is associated with */ + String8 mPackageName; + + /* Package version this ObbFile is associated with */ + int32_t mVersion; + + /* Flags for this OBB type. */ + int32_t mFlags; + + /* The encryption salt. */ + unsigned char mSalt[8]; + + const char* mFileName; + + size_t mFooterStart; + + bool parseObbFile(int fd); +}; + +} +#endif /* OBBFILE_H_ */ diff --git a/libs/androidfw/include/androidfw/ResourceTypes.h b/libs/androidfw/include/androidfw/ResourceTypes.h new file mode 100644 index 000000000000..12a6b0f9a4ec --- /dev/null +++ b/libs/androidfw/include/androidfw/ResourceTypes.h @@ -0,0 +1,1959 @@ +/* + * Copyright (C) 2005 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// +// Definitions of resource data structures. +// +#ifndef _LIBS_UTILS_RESOURCE_TYPES_H +#define _LIBS_UTILS_RESOURCE_TYPES_H + +#include <androidfw/Asset.h> +#include <androidfw/LocaleData.h> +#include <utils/ByteOrder.h> +#include <utils/Errors.h> +#include <utils/String16.h> +#include <utils/Vector.h> +#include <utils/KeyedVector.h> + +#include <utils/threads.h> + +#include <stdint.h> +#include <sys/types.h> + +#include <android/configuration.h> + +#include <memory> + +namespace android { + +/** + * In C++11, char16_t is defined as *at least* 16 bits. We do a lot of + * casting on raw data and expect char16_t to be exactly 16 bits. + */ +#if __cplusplus >= 201103L +struct __assertChar16Size { + static_assert(sizeof(char16_t) == sizeof(uint16_t), "char16_t is not 16 bits"); + static_assert(alignof(char16_t) == alignof(uint16_t), "char16_t is not 16-bit aligned"); +}; +#endif + +/** ******************************************************************** + * PNG Extensions + * + * New private chunks that may be placed in PNG images. + * + *********************************************************************** */ + +/** + * This chunk specifies how to split an image into segments for + * scaling. + * + * There are J horizontal and K vertical segments. These segments divide + * the image into J*K regions as follows (where J=4 and K=3): + * + * F0 S0 F1 S1 + * +-----+----+------+-------+ + * S2| 0 | 1 | 2 | 3 | + * +-----+----+------+-------+ + * | | | | | + * | | | | | + * F2| 4 | 5 | 6 | 7 | + * | | | | | + * | | | | | + * +-----+----+------+-------+ + * S3| 8 | 9 | 10 | 11 | + * +-----+----+------+-------+ + * + * Each horizontal and vertical segment is considered to by either + * stretchable (marked by the Sx labels) or fixed (marked by the Fy + * labels), in the horizontal or vertical axis, respectively. In the + * above example, the first is horizontal segment (F0) is fixed, the + * next is stretchable and then they continue to alternate. Note that + * the segment list for each axis can begin or end with a stretchable + * or fixed segment. + * + * The relative sizes of the stretchy segments indicates the relative + * amount of stretchiness of the regions bordered by the segments. For + * example, regions 3, 7 and 11 above will take up more horizontal space + * than regions 1, 5 and 9 since the horizontal segment associated with + * the first set of regions is larger than the other set of regions. The + * ratios of the amount of horizontal (or vertical) space taken by any + * two stretchable slices is exactly the ratio of their corresponding + * segment lengths. + * + * xDivs and yDivs are arrays of horizontal and vertical pixel + * indices. The first pair of Divs (in either array) indicate the + * starting and ending points of the first stretchable segment in that + * axis. The next pair specifies the next stretchable segment, etc. So + * in the above example xDiv[0] and xDiv[1] specify the horizontal + * coordinates for the regions labeled 1, 5 and 9. xDiv[2] and + * xDiv[3] specify the coordinates for regions 3, 7 and 11. Note that + * the leftmost slices always start at x=0 and the rightmost slices + * always end at the end of the image. So, for example, the regions 0, + * 4 and 8 (which are fixed along the X axis) start at x value 0 and + * go to xDiv[0] and slices 2, 6 and 10 start at xDiv[1] and end at + * xDiv[2]. + * + * The colors array contains hints for each of the regions. They are + * ordered according left-to-right and top-to-bottom as indicated above. + * For each segment that is a solid color the array entry will contain + * that color value; otherwise it will contain NO_COLOR. Segments that + * are completely transparent will always have the value TRANSPARENT_COLOR. + * + * The PNG chunk type is "npTc". + */ +struct alignas(uintptr_t) Res_png_9patch +{ + Res_png_9patch() : wasDeserialized(false), xDivsOffset(0), + yDivsOffset(0), colorsOffset(0) { } + + int8_t wasDeserialized; + uint8_t numXDivs; + uint8_t numYDivs; + uint8_t numColors; + + // The offset (from the start of this structure) to the xDivs & yDivs + // array for this 9patch. To get a pointer to this array, call + // getXDivs or getYDivs. Note that the serialized form for 9patches places + // the xDivs, yDivs and colors arrays immediately after the location + // of the Res_png_9patch struct. + uint32_t xDivsOffset; + uint32_t yDivsOffset; + + int32_t paddingLeft, paddingRight; + int32_t paddingTop, paddingBottom; + + enum { + // The 9 patch segment is not a solid color. + NO_COLOR = 0x00000001, + + // The 9 patch segment is completely transparent. + TRANSPARENT_COLOR = 0x00000000 + }; + + // The offset (from the start of this structure) to the colors array + // for this 9patch. + uint32_t colorsOffset; + + // Convert data from device representation to PNG file representation. + void deviceToFile(); + // Convert data from PNG file representation to device representation. + void fileToDevice(); + + // Serialize/Marshall the patch data into a newly malloc-ed block. + static void* serialize(const Res_png_9patch& patchHeader, const int32_t* xDivs, + const int32_t* yDivs, const uint32_t* colors); + // Serialize/Marshall the patch data into |outData|. + static void serialize(const Res_png_9patch& patchHeader, const int32_t* xDivs, + const int32_t* yDivs, const uint32_t* colors, void* outData); + // Deserialize/Unmarshall the patch data + static Res_png_9patch* deserialize(void* data); + // Compute the size of the serialized data structure + size_t serializedSize() const; + + // These tell where the next section of a patch starts. + // For example, the first patch includes the pixels from + // 0 to xDivs[0]-1 and the second patch includes the pixels + // from xDivs[0] to xDivs[1]-1. + inline int32_t* getXDivs() const { + return reinterpret_cast<int32_t*>(reinterpret_cast<uintptr_t>(this) + xDivsOffset); + } + inline int32_t* getYDivs() const { + return reinterpret_cast<int32_t*>(reinterpret_cast<uintptr_t>(this) + yDivsOffset); + } + inline uint32_t* getColors() const { + return reinterpret_cast<uint32_t*>(reinterpret_cast<uintptr_t>(this) + colorsOffset); + } + +} __attribute__((packed)); + +/** ******************************************************************** + * Base Types + * + * These are standard types that are shared between multiple specific + * resource types. + * + *********************************************************************** */ + +/** + * Header that appears at the front of every data chunk in a resource. + */ +struct ResChunk_header +{ + // Type identifier for this chunk. The meaning of this value depends + // on the containing chunk. + uint16_t type; + + // Size of the chunk header (in bytes). Adding this value to + // the address of the chunk allows you to find its associated data + // (if any). + uint16_t headerSize; + + // Total size of this chunk (in bytes). This is the chunkSize plus + // the size of any data associated with the chunk. Adding this value + // to the chunk allows you to completely skip its contents (including + // any child chunks). If this value is the same as chunkSize, there is + // no data associated with the chunk. + uint32_t size; +}; + +enum { + RES_NULL_TYPE = 0x0000, + RES_STRING_POOL_TYPE = 0x0001, + RES_TABLE_TYPE = 0x0002, + RES_XML_TYPE = 0x0003, + + // Chunk types in RES_XML_TYPE + RES_XML_FIRST_CHUNK_TYPE = 0x0100, + RES_XML_START_NAMESPACE_TYPE= 0x0100, + RES_XML_END_NAMESPACE_TYPE = 0x0101, + RES_XML_START_ELEMENT_TYPE = 0x0102, + RES_XML_END_ELEMENT_TYPE = 0x0103, + RES_XML_CDATA_TYPE = 0x0104, + RES_XML_LAST_CHUNK_TYPE = 0x017f, + // This contains a uint32_t array mapping strings in the string + // pool back to resource identifiers. It is optional. + RES_XML_RESOURCE_MAP_TYPE = 0x0180, + + // Chunk types in RES_TABLE_TYPE + RES_TABLE_PACKAGE_TYPE = 0x0200, + RES_TABLE_TYPE_TYPE = 0x0201, + RES_TABLE_TYPE_SPEC_TYPE = 0x0202, + RES_TABLE_LIBRARY_TYPE = 0x0203 +}; + +/** + * Macros for building/splitting resource identifiers. + */ +#define Res_VALIDID(resid) (resid != 0) +#define Res_CHECKID(resid) ((resid&0xFFFF0000) != 0) +#define Res_MAKEID(package, type, entry) \ + (((package+1)<<24) | (((type+1)&0xFF)<<16) | (entry&0xFFFF)) +#define Res_GETPACKAGE(id) ((id>>24)-1) +#define Res_GETTYPE(id) (((id>>16)&0xFF)-1) +#define Res_GETENTRY(id) (id&0xFFFF) + +#define Res_INTERNALID(resid) ((resid&0xFFFF0000) != 0 && (resid&0xFF0000) == 0) +#define Res_MAKEINTERNAL(entry) (0x01000000 | (entry&0xFFFF)) +#define Res_MAKEARRAY(entry) (0x02000000 | (entry&0xFFFF)) + +static const size_t Res_MAXPACKAGE = 255; +static const size_t Res_MAXTYPE = 255; + +/** + * Representation of a value in a resource, supplying type + * information. + */ +struct Res_value +{ + // Number of bytes in this structure. + uint16_t size; + + // Always set to 0. + uint8_t res0; + + // Type of the data value. + enum { + // The 'data' is either 0 or 1, specifying this resource is either + // undefined or empty, respectively. + TYPE_NULL = 0x00, + // The 'data' holds a ResTable_ref, a reference to another resource + // table entry. + TYPE_REFERENCE = 0x01, + // The 'data' holds an attribute resource identifier. + TYPE_ATTRIBUTE = 0x02, + // The 'data' holds an index into the containing resource table's + // global value string pool. + TYPE_STRING = 0x03, + // The 'data' holds a single-precision floating point number. + TYPE_FLOAT = 0x04, + // The 'data' holds a complex number encoding a dimension value, + // such as "100in". + TYPE_DIMENSION = 0x05, + // The 'data' holds a complex number encoding a fraction of a + // container. + TYPE_FRACTION = 0x06, + // The 'data' holds a dynamic ResTable_ref, which needs to be + // resolved before it can be used like a TYPE_REFERENCE. + TYPE_DYNAMIC_REFERENCE = 0x07, + // The 'data' holds an attribute resource identifier, which needs to be resolved + // before it can be used like a TYPE_ATTRIBUTE. + TYPE_DYNAMIC_ATTRIBUTE = 0x08, + + // Beginning of integer flavors... + TYPE_FIRST_INT = 0x10, + + // The 'data' is a raw integer value of the form n..n. + TYPE_INT_DEC = 0x10, + // The 'data' is a raw integer value of the form 0xn..n. + TYPE_INT_HEX = 0x11, + // The 'data' is either 0 or 1, for input "false" or "true" respectively. + TYPE_INT_BOOLEAN = 0x12, + + // Beginning of color integer flavors... + TYPE_FIRST_COLOR_INT = 0x1c, + + // The 'data' is a raw integer value of the form #aarrggbb. + TYPE_INT_COLOR_ARGB8 = 0x1c, + // The 'data' is a raw integer value of the form #rrggbb. + TYPE_INT_COLOR_RGB8 = 0x1d, + // The 'data' is a raw integer value of the form #argb. + TYPE_INT_COLOR_ARGB4 = 0x1e, + // The 'data' is a raw integer value of the form #rgb. + TYPE_INT_COLOR_RGB4 = 0x1f, + + // ...end of integer flavors. + TYPE_LAST_COLOR_INT = 0x1f, + + // ...end of integer flavors. + TYPE_LAST_INT = 0x1f + }; + uint8_t dataType; + + // Structure of complex data values (TYPE_UNIT and TYPE_FRACTION) + enum { + // Where the unit type information is. This gives us 16 possible + // types, as defined below. + COMPLEX_UNIT_SHIFT = 0, + COMPLEX_UNIT_MASK = 0xf, + + // TYPE_DIMENSION: Value is raw pixels. + COMPLEX_UNIT_PX = 0, + // TYPE_DIMENSION: Value is Device Independent Pixels. + COMPLEX_UNIT_DIP = 1, + // TYPE_DIMENSION: Value is a Scaled device independent Pixels. + COMPLEX_UNIT_SP = 2, + // TYPE_DIMENSION: Value is in points. + COMPLEX_UNIT_PT = 3, + // TYPE_DIMENSION: Value is in inches. + COMPLEX_UNIT_IN = 4, + // TYPE_DIMENSION: Value is in millimeters. + COMPLEX_UNIT_MM = 5, + + // TYPE_FRACTION: A basic fraction of the overall size. + COMPLEX_UNIT_FRACTION = 0, + // TYPE_FRACTION: A fraction of the parent size. + COMPLEX_UNIT_FRACTION_PARENT = 1, + + // Where the radix information is, telling where the decimal place + // appears in the mantissa. This give us 4 possible fixed point + // representations as defined below. + COMPLEX_RADIX_SHIFT = 4, + COMPLEX_RADIX_MASK = 0x3, + + // The mantissa is an integral number -- i.e., 0xnnnnnn.0 + COMPLEX_RADIX_23p0 = 0, + // The mantissa magnitude is 16 bits -- i.e, 0xnnnn.nn + COMPLEX_RADIX_16p7 = 1, + // The mantissa magnitude is 8 bits -- i.e, 0xnn.nnnn + COMPLEX_RADIX_8p15 = 2, + // The mantissa magnitude is 0 bits -- i.e, 0x0.nnnnnn + COMPLEX_RADIX_0p23 = 3, + + // Where the actual value is. This gives us 23 bits of + // precision. The top bit is the sign. + COMPLEX_MANTISSA_SHIFT = 8, + COMPLEX_MANTISSA_MASK = 0xffffff + }; + + // Possible data values for TYPE_NULL. + enum { + // The value is not defined. + DATA_NULL_UNDEFINED = 0, + // The value is explicitly defined as empty. + DATA_NULL_EMPTY = 1 + }; + + // The data for this item, as interpreted according to dataType. + typedef uint32_t data_type; + data_type data; + + void copyFrom_dtoh(const Res_value& src); +}; + +/** + * This is a reference to a unique entry (a ResTable_entry structure) + * in a resource table. The value is structured as: 0xpptteeee, + * where pp is the package index, tt is the type index in that + * package, and eeee is the entry index in that type. The package + * and type values start at 1 for the first item, to help catch cases + * where they have not been supplied. + */ +struct ResTable_ref +{ + uint32_t ident; +}; + +/** + * Reference to a string in a string pool. + */ +struct ResStringPool_ref +{ + // Index into the string pool table (uint32_t-offset from the indices + // immediately after ResStringPool_header) at which to find the location + // of the string data in the pool. + uint32_t index; +}; + +/** ******************************************************************** + * String Pool + * + * A set of strings that can be references by others through a + * ResStringPool_ref. + * + *********************************************************************** */ + +/** + * Definition for a pool of strings. The data of this chunk is an + * array of uint32_t providing indices into the pool, relative to + * stringsStart. At stringsStart are all of the UTF-16 strings + * concatenated together; each starts with a uint16_t of the string's + * length and each ends with a 0x0000 terminator. If a string is > + * 32767 characters, the high bit of the length is set meaning to take + * those 15 bits as a high word and it will be followed by another + * uint16_t containing the low word. + * + * If styleCount is not zero, then immediately following the array of + * uint32_t indices into the string table is another array of indices + * into a style table starting at stylesStart. Each entry in the + * style table is an array of ResStringPool_span structures. + */ +struct ResStringPool_header +{ + struct ResChunk_header header; + + // Number of strings in this pool (number of uint32_t indices that follow + // in the data). + uint32_t stringCount; + + // Number of style span arrays in the pool (number of uint32_t indices + // follow the string indices). + uint32_t styleCount; + + // Flags. + enum { + // If set, the string index is sorted by the string values (based + // on strcmp16()). + SORTED_FLAG = 1<<0, + + // String pool is encoded in UTF-8 + UTF8_FLAG = 1<<8 + }; + uint32_t flags; + + // Index from header of the string data. + uint32_t stringsStart; + + // Index from header of the style data. + uint32_t stylesStart; +}; + +/** + * This structure defines a span of style information associated with + * a string in the pool. + */ +struct ResStringPool_span +{ + enum { + END = 0xFFFFFFFF + }; + + // This is the name of the span -- that is, the name of the XML + // tag that defined it. The special value END (0xFFFFFFFF) indicates + // the end of an array of spans. + ResStringPool_ref name; + + // The range of characters in the string that this span applies to. + uint32_t firstChar, lastChar; +}; + +/** + * Convenience class for accessing data in a ResStringPool resource. + */ +class ResStringPool +{ +public: + ResStringPool(); + ResStringPool(const void* data, size_t size, bool copyData=false); + ~ResStringPool(); + + void setToEmpty(); + status_t setTo(const void* data, size_t size, bool copyData=false); + + status_t getError() const; + + void uninit(); + + // Return string entry as UTF16; if the pool is UTF8, the string will + // be converted before returning. + inline const char16_t* stringAt(const ResStringPool_ref& ref, size_t* outLen) const { + return stringAt(ref.index, outLen); + } + const char16_t* stringAt(size_t idx, size_t* outLen) const; + + // Note: returns null if the string pool is not UTF8. + const char* string8At(size_t idx, size_t* outLen) const; + + // Return string whether the pool is UTF8 or UTF16. Does not allow you + // to distinguish null. + const String8 string8ObjectAt(size_t idx) const; + + const ResStringPool_span* styleAt(const ResStringPool_ref& ref) const; + const ResStringPool_span* styleAt(size_t idx) const; + + ssize_t indexOfString(const char16_t* str, size_t strLen) const; + + size_t size() const; + size_t styleCount() const; + size_t bytes() const; + + bool isSorted() const; + bool isUTF8() const; + +private: + status_t mError; + void* mOwnedData; + const ResStringPool_header* mHeader; + size_t mSize; + mutable Mutex mDecodeLock; + const uint32_t* mEntries; + const uint32_t* mEntryStyles; + const void* mStrings; + char16_t mutable** mCache; + uint32_t mStringPoolSize; // number of uint16_t + const uint32_t* mStyles; + uint32_t mStylePoolSize; // number of uint32_t +}; + +/** + * Wrapper class that allows the caller to retrieve a string from + * a string pool without knowing which string pool to look. + */ +class StringPoolRef { +public: + StringPoolRef(); + StringPoolRef(const ResStringPool* pool, uint32_t index); + + const char* string8(size_t* outLen) const; + const char16_t* string16(size_t* outLen) const; + +private: + const ResStringPool* mPool; + uint32_t mIndex; +}; + +/** ******************************************************************** + * XML Tree + * + * Binary representation of an XML document. This is designed to + * express everything in an XML document, in a form that is much + * easier to parse on the device. + * + *********************************************************************** */ + +/** + * XML tree header. This appears at the front of an XML tree, + * describing its content. It is followed by a flat array of + * ResXMLTree_node structures; the hierarchy of the XML document + * is described by the occurrance of RES_XML_START_ELEMENT_TYPE + * and corresponding RES_XML_END_ELEMENT_TYPE nodes in the array. + */ +struct ResXMLTree_header +{ + struct ResChunk_header header; +}; + +/** + * Basic XML tree node. A single item in the XML document. Extended info + * about the node can be found after header.headerSize. + */ +struct ResXMLTree_node +{ + struct ResChunk_header header; + + // Line number in original source file at which this element appeared. + uint32_t lineNumber; + + // Optional XML comment that was associated with this element; -1 if none. + struct ResStringPool_ref comment; +}; + +/** + * Extended XML tree node for CDATA tags -- includes the CDATA string. + * Appears header.headerSize bytes after a ResXMLTree_node. + */ +struct ResXMLTree_cdataExt +{ + // The raw CDATA character data. + struct ResStringPool_ref data; + + // The typed value of the character data if this is a CDATA node. + struct Res_value typedData; +}; + +/** + * Extended XML tree node for namespace start/end nodes. + * Appears header.headerSize bytes after a ResXMLTree_node. + */ +struct ResXMLTree_namespaceExt +{ + // The prefix of the namespace. + struct ResStringPool_ref prefix; + + // The URI of the namespace. + struct ResStringPool_ref uri; +}; + +/** + * Extended XML tree node for element start/end nodes. + * Appears header.headerSize bytes after a ResXMLTree_node. + */ +struct ResXMLTree_endElementExt +{ + // String of the full namespace of this element. + struct ResStringPool_ref ns; + + // String name of this node if it is an ELEMENT; the raw + // character data if this is a CDATA node. + struct ResStringPool_ref name; +}; + +/** + * Extended XML tree node for start tags -- includes attribute + * information. + * Appears header.headerSize bytes after a ResXMLTree_node. + */ +struct ResXMLTree_attrExt +{ + // String of the full namespace of this element. + struct ResStringPool_ref ns; + + // String name of this node if it is an ELEMENT; the raw + // character data if this is a CDATA node. + struct ResStringPool_ref name; + + // Byte offset from the start of this structure where the attributes start. + uint16_t attributeStart; + + // Size of the ResXMLTree_attribute structures that follow. + uint16_t attributeSize; + + // Number of attributes associated with an ELEMENT. These are + // available as an array of ResXMLTree_attribute structures + // immediately following this node. + uint16_t attributeCount; + + // Index (1-based) of the "id" attribute. 0 if none. + uint16_t idIndex; + + // Index (1-based) of the "class" attribute. 0 if none. + uint16_t classIndex; + + // Index (1-based) of the "style" attribute. 0 if none. + uint16_t styleIndex; +}; + +struct ResXMLTree_attribute +{ + // Namespace of this attribute. + struct ResStringPool_ref ns; + + // Name of this attribute. + struct ResStringPool_ref name; + + // The original raw string value of this attribute. + struct ResStringPool_ref rawValue; + + // Processesd typed value of this attribute. + struct Res_value typedValue; +}; + +class ResXMLTree; + +class ResXMLParser +{ +public: + ResXMLParser(const ResXMLTree& tree); + + enum event_code_t { + BAD_DOCUMENT = -1, + START_DOCUMENT = 0, + END_DOCUMENT = 1, + + FIRST_CHUNK_CODE = RES_XML_FIRST_CHUNK_TYPE, + + START_NAMESPACE = RES_XML_START_NAMESPACE_TYPE, + END_NAMESPACE = RES_XML_END_NAMESPACE_TYPE, + START_TAG = RES_XML_START_ELEMENT_TYPE, + END_TAG = RES_XML_END_ELEMENT_TYPE, + TEXT = RES_XML_CDATA_TYPE + }; + + struct ResXMLPosition + { + event_code_t eventCode; + const ResXMLTree_node* curNode; + const void* curExt; + }; + + void restart(); + + const ResStringPool& getStrings() const; + + event_code_t getEventType() const; + // Note, unlike XmlPullParser, the first call to next() will return + // START_TAG of the first element. + event_code_t next(); + + // These are available for all nodes: + int32_t getCommentID() const; + const char16_t* getComment(size_t* outLen) const; + uint32_t getLineNumber() const; + + // This is available for TEXT: + int32_t getTextID() const; + const char16_t* getText(size_t* outLen) const; + ssize_t getTextValue(Res_value* outValue) const; + + // These are available for START_NAMESPACE and END_NAMESPACE: + int32_t getNamespacePrefixID() const; + const char16_t* getNamespacePrefix(size_t* outLen) const; + int32_t getNamespaceUriID() const; + const char16_t* getNamespaceUri(size_t* outLen) const; + + // These are available for START_TAG and END_TAG: + int32_t getElementNamespaceID() const; + const char16_t* getElementNamespace(size_t* outLen) const; + int32_t getElementNameID() const; + const char16_t* getElementName(size_t* outLen) const; + + // Remaining methods are for retrieving information about attributes + // associated with a START_TAG: + + size_t getAttributeCount() const; + + // Returns -1 if no namespace, -2 if idx out of range. + int32_t getAttributeNamespaceID(size_t idx) const; + const char16_t* getAttributeNamespace(size_t idx, size_t* outLen) const; + + int32_t getAttributeNameID(size_t idx) const; + const char16_t* getAttributeName(size_t idx, size_t* outLen) const; + uint32_t getAttributeNameResID(size_t idx) const; + + // These will work only if the underlying string pool is UTF-8. + const char* getAttributeNamespace8(size_t idx, size_t* outLen) const; + const char* getAttributeName8(size_t idx, size_t* outLen) const; + + int32_t getAttributeValueStringID(size_t idx) const; + const char16_t* getAttributeStringValue(size_t idx, size_t* outLen) const; + + int32_t getAttributeDataType(size_t idx) const; + int32_t getAttributeData(size_t idx) const; + ssize_t getAttributeValue(size_t idx, Res_value* outValue) const; + + ssize_t indexOfAttribute(const char* ns, const char* attr) const; + ssize_t indexOfAttribute(const char16_t* ns, size_t nsLen, + const char16_t* attr, size_t attrLen) const; + + ssize_t indexOfID() const; + ssize_t indexOfClass() const; + ssize_t indexOfStyle() const; + + void getPosition(ResXMLPosition* pos) const; + void setPosition(const ResXMLPosition& pos); + +private: + friend class ResXMLTree; + + event_code_t nextNode(); + + const ResXMLTree& mTree; + event_code_t mEventCode; + const ResXMLTree_node* mCurNode; + const void* mCurExt; +}; + +class DynamicRefTable; + +/** + * Convenience class for accessing data in a ResXMLTree resource. + */ +class ResXMLTree : public ResXMLParser +{ +public: + ResXMLTree(const DynamicRefTable* dynamicRefTable); + ResXMLTree(); + ~ResXMLTree(); + + status_t setTo(const void* data, size_t size, bool copyData=false); + + status_t getError() const; + + void uninit(); + +private: + friend class ResXMLParser; + + status_t validateNode(const ResXMLTree_node* node) const; + + const DynamicRefTable* const mDynamicRefTable; + + status_t mError; + void* mOwnedData; + const ResXMLTree_header* mHeader; + size_t mSize; + const uint8_t* mDataEnd; + ResStringPool mStrings; + const uint32_t* mResIds; + size_t mNumResIds; + const ResXMLTree_node* mRootNode; + const void* mRootExt; + event_code_t mRootCode; +}; + +/** ******************************************************************** + * RESOURCE TABLE + * + *********************************************************************** */ + +/** + * Header for a resource table. Its data contains a series of + * additional chunks: + * * A ResStringPool_header containing all table values. This string pool + * contains all of the string values in the entire resource table (not + * the names of entries or type identifiers however). + * * One or more ResTable_package chunks. + * + * Specific entries within a resource table can be uniquely identified + * with a single integer as defined by the ResTable_ref structure. + */ +struct ResTable_header +{ + struct ResChunk_header header; + + // The number of ResTable_package structures. + uint32_t packageCount; +}; + +/** + * A collection of resource data types within a package. Followed by + * one or more ResTable_type and ResTable_typeSpec structures containing the + * entry values for each resource type. + */ +struct ResTable_package +{ + struct ResChunk_header header; + + // If this is a base package, its ID. Package IDs start + // at 1 (corresponding to the value of the package bits in a + // resource identifier). 0 means this is not a base package. + uint32_t id; + + // Actual name of this package, \0-terminated. + uint16_t name[128]; + + // Offset to a ResStringPool_header defining the resource + // type symbol table. If zero, this package is inheriting from + // another base package (overriding specific values in it). + uint32_t typeStrings; + + // Last index into typeStrings that is for public use by others. + uint32_t lastPublicType; + + // Offset to a ResStringPool_header defining the resource + // key symbol table. If zero, this package is inheriting from + // another base package (overriding specific values in it). + uint32_t keyStrings; + + // Last index into keyStrings that is for public use by others. + uint32_t lastPublicKey; + + uint32_t typeIdOffset; +}; + +// The most specific locale can consist of: +// +// - a 3 char language code +// - a 3 char region code prefixed by a 'r' +// - a 4 char script code prefixed by a 's' +// - a 8 char variant code prefixed by a 'v' +// +// each separated by a single char separator, which sums up to a total of 24 +// chars, (25 include the string terminator) rounded up to 28 to be 4 byte +// aligned. +#define RESTABLE_MAX_LOCALE_LEN 28 + + +/** + * Describes a particular resource configuration. + */ +struct ResTable_config +{ + // Number of bytes in this structure. + uint32_t size; + + union { + struct { + // Mobile country code (from SIM). 0 means "any". + uint16_t mcc; + // Mobile network code (from SIM). 0 means "any". + uint16_t mnc; + }; + uint32_t imsi; + }; + + union { + struct { + // This field can take three different forms: + // - \0\0 means "any". + // + // - Two 7 bit ascii values interpreted as ISO-639-1 language + // codes ('fr', 'en' etc. etc.). The high bit for both bytes is + // zero. + // + // - A single 16 bit little endian packed value representing an + // ISO-639-2 3 letter language code. This will be of the form: + // + // {1, t, t, t, t, t, s, s, s, s, s, f, f, f, f, f} + // + // bit[0, 4] = first letter of the language code + // bit[5, 9] = second letter of the language code + // bit[10, 14] = third letter of the language code. + // bit[15] = 1 always + // + // For backwards compatibility, languages that have unambiguous + // two letter codes are represented in that format. + // + // The layout is always bigendian irrespective of the runtime + // architecture. + char language[2]; + + // This field can take three different forms: + // - \0\0 means "any". + // + // - Two 7 bit ascii values interpreted as 2 letter region + // codes ('US', 'GB' etc.). The high bit for both bytes is zero. + // + // - An UN M.49 3 digit region code. For simplicity, these are packed + // in the same manner as the language codes, though we should need + // only 10 bits to represent them, instead of the 15. + // + // The layout is always bigendian irrespective of the runtime + // architecture. + char country[2]; + }; + uint32_t locale; + }; + + enum { + ORIENTATION_ANY = ACONFIGURATION_ORIENTATION_ANY, + ORIENTATION_PORT = ACONFIGURATION_ORIENTATION_PORT, + ORIENTATION_LAND = ACONFIGURATION_ORIENTATION_LAND, + ORIENTATION_SQUARE = ACONFIGURATION_ORIENTATION_SQUARE, + }; + + enum { + TOUCHSCREEN_ANY = ACONFIGURATION_TOUCHSCREEN_ANY, + TOUCHSCREEN_NOTOUCH = ACONFIGURATION_TOUCHSCREEN_NOTOUCH, + TOUCHSCREEN_STYLUS = ACONFIGURATION_TOUCHSCREEN_STYLUS, + TOUCHSCREEN_FINGER = ACONFIGURATION_TOUCHSCREEN_FINGER, + }; + + enum { + DENSITY_DEFAULT = ACONFIGURATION_DENSITY_DEFAULT, + DENSITY_LOW = ACONFIGURATION_DENSITY_LOW, + DENSITY_MEDIUM = ACONFIGURATION_DENSITY_MEDIUM, + DENSITY_TV = ACONFIGURATION_DENSITY_TV, + DENSITY_HIGH = ACONFIGURATION_DENSITY_HIGH, + DENSITY_XHIGH = ACONFIGURATION_DENSITY_XHIGH, + DENSITY_XXHIGH = ACONFIGURATION_DENSITY_XXHIGH, + DENSITY_XXXHIGH = ACONFIGURATION_DENSITY_XXXHIGH, + DENSITY_ANY = ACONFIGURATION_DENSITY_ANY, + DENSITY_NONE = ACONFIGURATION_DENSITY_NONE + }; + + union { + struct { + uint8_t orientation; + uint8_t touchscreen; + uint16_t density; + }; + uint32_t screenType; + }; + + enum { + KEYBOARD_ANY = ACONFIGURATION_KEYBOARD_ANY, + KEYBOARD_NOKEYS = ACONFIGURATION_KEYBOARD_NOKEYS, + KEYBOARD_QWERTY = ACONFIGURATION_KEYBOARD_QWERTY, + KEYBOARD_12KEY = ACONFIGURATION_KEYBOARD_12KEY, + }; + + enum { + NAVIGATION_ANY = ACONFIGURATION_NAVIGATION_ANY, + NAVIGATION_NONAV = ACONFIGURATION_NAVIGATION_NONAV, + NAVIGATION_DPAD = ACONFIGURATION_NAVIGATION_DPAD, + NAVIGATION_TRACKBALL = ACONFIGURATION_NAVIGATION_TRACKBALL, + NAVIGATION_WHEEL = ACONFIGURATION_NAVIGATION_WHEEL, + }; + + enum { + MASK_KEYSHIDDEN = 0x0003, + KEYSHIDDEN_ANY = ACONFIGURATION_KEYSHIDDEN_ANY, + KEYSHIDDEN_NO = ACONFIGURATION_KEYSHIDDEN_NO, + KEYSHIDDEN_YES = ACONFIGURATION_KEYSHIDDEN_YES, + KEYSHIDDEN_SOFT = ACONFIGURATION_KEYSHIDDEN_SOFT, + }; + + enum { + MASK_NAVHIDDEN = 0x000c, + SHIFT_NAVHIDDEN = 2, + NAVHIDDEN_ANY = ACONFIGURATION_NAVHIDDEN_ANY << SHIFT_NAVHIDDEN, + NAVHIDDEN_NO = ACONFIGURATION_NAVHIDDEN_NO << SHIFT_NAVHIDDEN, + NAVHIDDEN_YES = ACONFIGURATION_NAVHIDDEN_YES << SHIFT_NAVHIDDEN, + }; + + union { + struct { + uint8_t keyboard; + uint8_t navigation; + uint8_t inputFlags; + uint8_t inputPad0; + }; + uint32_t input; + }; + + enum { + SCREENWIDTH_ANY = 0 + }; + + enum { + SCREENHEIGHT_ANY = 0 + }; + + union { + struct { + uint16_t screenWidth; + uint16_t screenHeight; + }; + uint32_t screenSize; + }; + + enum { + SDKVERSION_ANY = 0 + }; + + enum { + MINORVERSION_ANY = 0 + }; + + union { + struct { + uint16_t sdkVersion; + // For now minorVersion must always be 0!!! Its meaning + // is currently undefined. + uint16_t minorVersion; + }; + uint32_t version; + }; + + enum { + // screenLayout bits for screen size class. + MASK_SCREENSIZE = 0x0f, + SCREENSIZE_ANY = ACONFIGURATION_SCREENSIZE_ANY, + SCREENSIZE_SMALL = ACONFIGURATION_SCREENSIZE_SMALL, + SCREENSIZE_NORMAL = ACONFIGURATION_SCREENSIZE_NORMAL, + SCREENSIZE_LARGE = ACONFIGURATION_SCREENSIZE_LARGE, + SCREENSIZE_XLARGE = ACONFIGURATION_SCREENSIZE_XLARGE, + + // screenLayout bits for wide/long screen variation. + MASK_SCREENLONG = 0x30, + SHIFT_SCREENLONG = 4, + SCREENLONG_ANY = ACONFIGURATION_SCREENLONG_ANY << SHIFT_SCREENLONG, + SCREENLONG_NO = ACONFIGURATION_SCREENLONG_NO << SHIFT_SCREENLONG, + SCREENLONG_YES = ACONFIGURATION_SCREENLONG_YES << SHIFT_SCREENLONG, + + // screenLayout bits for layout direction. + MASK_LAYOUTDIR = 0xC0, + SHIFT_LAYOUTDIR = 6, + LAYOUTDIR_ANY = ACONFIGURATION_LAYOUTDIR_ANY << SHIFT_LAYOUTDIR, + LAYOUTDIR_LTR = ACONFIGURATION_LAYOUTDIR_LTR << SHIFT_LAYOUTDIR, + LAYOUTDIR_RTL = ACONFIGURATION_LAYOUTDIR_RTL << SHIFT_LAYOUTDIR, + }; + + enum { + // uiMode bits for the mode type. + MASK_UI_MODE_TYPE = 0x0f, + UI_MODE_TYPE_ANY = ACONFIGURATION_UI_MODE_TYPE_ANY, + UI_MODE_TYPE_NORMAL = ACONFIGURATION_UI_MODE_TYPE_NORMAL, + UI_MODE_TYPE_DESK = ACONFIGURATION_UI_MODE_TYPE_DESK, + UI_MODE_TYPE_CAR = ACONFIGURATION_UI_MODE_TYPE_CAR, + UI_MODE_TYPE_TELEVISION = ACONFIGURATION_UI_MODE_TYPE_TELEVISION, + UI_MODE_TYPE_APPLIANCE = ACONFIGURATION_UI_MODE_TYPE_APPLIANCE, + UI_MODE_TYPE_WATCH = ACONFIGURATION_UI_MODE_TYPE_WATCH, + + // uiMode bits for the night switch. + MASK_UI_MODE_NIGHT = 0x30, + SHIFT_UI_MODE_NIGHT = 4, + UI_MODE_NIGHT_ANY = ACONFIGURATION_UI_MODE_NIGHT_ANY << SHIFT_UI_MODE_NIGHT, + UI_MODE_NIGHT_NO = ACONFIGURATION_UI_MODE_NIGHT_NO << SHIFT_UI_MODE_NIGHT, + UI_MODE_NIGHT_YES = ACONFIGURATION_UI_MODE_NIGHT_YES << SHIFT_UI_MODE_NIGHT, + }; + + union { + struct { + uint8_t screenLayout; + uint8_t uiMode; + uint16_t smallestScreenWidthDp; + }; + uint32_t screenConfig; + }; + + union { + struct { + uint16_t screenWidthDp; + uint16_t screenHeightDp; + }; + uint32_t screenSizeDp; + }; + + // The ISO-15924 short name for the script corresponding to this + // configuration. (eg. Hant, Latn, etc.). Interpreted in conjunction with + // the locale field. + char localeScript[4]; + + // A single BCP-47 variant subtag. Will vary in length between 4 and 8 + // chars. Interpreted in conjunction with the locale field. + char localeVariant[8]; + + enum { + // screenLayout2 bits for round/notround. + MASK_SCREENROUND = 0x03, + SCREENROUND_ANY = ACONFIGURATION_SCREENROUND_ANY, + SCREENROUND_NO = ACONFIGURATION_SCREENROUND_NO, + SCREENROUND_YES = ACONFIGURATION_SCREENROUND_YES, + }; + + // An extension of screenConfig. + union { + struct { + uint8_t screenLayout2; // Contains round/notround qualifier. + uint8_t screenConfigPad1; // Reserved padding. + uint16_t screenConfigPad2; // Reserved padding. + }; + uint32_t screenConfig2; + }; + + // If false and localeScript is set, it means that the script of the locale + // was explicitly provided. + // + // If true, it means that localeScript was automatically computed. + // localeScript may still not be set in this case, which means that we + // tried but could not compute a script. + bool localeScriptWasComputed; + + void copyFromDeviceNoSwap(const ResTable_config& o); + + void copyFromDtoH(const ResTable_config& o); + + void swapHtoD(); + + int compare(const ResTable_config& o) const; + int compareLogical(const ResTable_config& o) const; + + // Flags indicating a set of config values. These flag constants must + // match the corresponding ones in android.content.pm.ActivityInfo and + // attrs_manifest.xml. + enum { + CONFIG_MCC = ACONFIGURATION_MCC, + CONFIG_MNC = ACONFIGURATION_MNC, + CONFIG_LOCALE = ACONFIGURATION_LOCALE, + CONFIG_TOUCHSCREEN = ACONFIGURATION_TOUCHSCREEN, + CONFIG_KEYBOARD = ACONFIGURATION_KEYBOARD, + CONFIG_KEYBOARD_HIDDEN = ACONFIGURATION_KEYBOARD_HIDDEN, + CONFIG_NAVIGATION = ACONFIGURATION_NAVIGATION, + CONFIG_ORIENTATION = ACONFIGURATION_ORIENTATION, + CONFIG_DENSITY = ACONFIGURATION_DENSITY, + CONFIG_SCREEN_SIZE = ACONFIGURATION_SCREEN_SIZE, + CONFIG_SMALLEST_SCREEN_SIZE = ACONFIGURATION_SMALLEST_SCREEN_SIZE, + CONFIG_VERSION = ACONFIGURATION_VERSION, + CONFIG_SCREEN_LAYOUT = ACONFIGURATION_SCREEN_LAYOUT, + CONFIG_UI_MODE = ACONFIGURATION_UI_MODE, + CONFIG_LAYOUTDIR = ACONFIGURATION_LAYOUTDIR, + CONFIG_SCREEN_ROUND = ACONFIGURATION_SCREEN_ROUND, + }; + + // Compare two configuration, returning CONFIG_* flags set for each value + // that is different. + int diff(const ResTable_config& o) const; + + // Return true if 'this' is more specific than 'o'. + bool isMoreSpecificThan(const ResTable_config& o) const; + + // Return true if 'this' is a better match than 'o' for the 'requested' + // configuration. This assumes that match() has already been used to + // remove any configurations that don't match the requested configuration + // at all; if they are not first filtered, non-matching results can be + // considered better than matching ones. + // The general rule per attribute: if the request cares about an attribute + // (it normally does), if the two (this and o) are equal it's a tie. If + // they are not equal then one must be generic because only generic and + // '==requested' will pass the match() call. So if this is not generic, + // it wins. If this IS generic, o wins (return false). + bool isBetterThan(const ResTable_config& o, const ResTable_config* requested) const; + + // Return true if 'this' can be considered a match for the parameters in + // 'settings'. + // Note this is asymetric. A default piece of data will match every request + // but a request for the default should not match odd specifics + // (ie, request with no mcc should not match a particular mcc's data) + // settings is the requested settings + bool match(const ResTable_config& settings) const; + + // Get the string representation of the locale component of this + // Config. The maximum size of this representation will be + // |RESTABLE_MAX_LOCALE_LEN| (including a terminating '\0'). + // + // Example: en-US, en-Latn-US, en-POSIX. + void getBcp47Locale(char* out) const; + + // Append to str the resource-qualifer string representation of the + // locale component of this Config. If the locale is only country + // and language, it will look like en-rUS. If it has scripts and + // variants, it will be a modified bcp47 tag: b+en+Latn+US. + void appendDirLocale(String8& str) const; + + // Sets the values of language, region, script and variant to the + // well formed BCP-47 locale contained in |in|. The input locale is + // assumed to be valid and no validation is performed. + void setBcp47Locale(const char* in); + + inline void clearLocale() { + locale = 0; + localeScriptWasComputed = false; + memset(localeScript, 0, sizeof(localeScript)); + memset(localeVariant, 0, sizeof(localeVariant)); + } + + inline void computeScript() { + localeDataComputeScript(localeScript, language, country); + } + + // Get the 2 or 3 letter language code of this configuration. Trailing + // bytes are set to '\0'. + size_t unpackLanguage(char language[4]) const; + // Get the 2 or 3 letter language code of this configuration. Trailing + // bytes are set to '\0'. + size_t unpackRegion(char region[4]) const; + + // Sets the language code of this configuration to the first three + // chars at |language|. + // + // If |language| is a 2 letter code, the trailing byte must be '\0' or + // the BCP-47 separator '-'. + void packLanguage(const char* language); + // Sets the region code of this configuration to the first three bytes + // at |region|. If |region| is a 2 letter code, the trailing byte must be '\0' + // or the BCP-47 separator '-'. + void packRegion(const char* region); + + // Returns a positive integer if this config is more specific than |o| + // with respect to their locales, a negative integer if |o| is more specific + // and 0 if they're equally specific. + int isLocaleMoreSpecificThan(const ResTable_config &o) const; + + // Return true if 'this' is a better locale match than 'o' for the + // 'requested' configuration. Similar to isBetterThan(), this assumes that + // match() has already been used to remove any configurations that don't + // match the requested configuration at all. + bool isLocaleBetterThan(const ResTable_config& o, const ResTable_config* requested) const; + + String8 toString() const; +}; + +/** + * A specification of the resources defined by a particular type. + * + * There should be one of these chunks for each resource type. + * + * This structure is followed by an array of integers providing the set of + * configuration change flags (ResTable_config::CONFIG_*) that have multiple + * resources for that configuration. In addition, the high bit is set if that + * resource has been made public. + */ +struct ResTable_typeSpec +{ + struct ResChunk_header header; + + // The type identifier this chunk is holding. Type IDs start + // at 1 (corresponding to the value of the type bits in a + // resource identifier). 0 is invalid. + uint8_t id; + + // Must be 0. + uint8_t res0; + // Must be 0. + uint16_t res1; + + // Number of uint32_t entry configuration masks that follow. + uint32_t entryCount; + + enum { + // Additional flag indicating an entry is public. + SPEC_PUBLIC = 0x40000000 + }; +}; + +/** + * A collection of resource entries for a particular resource data + * type. Followed by an array of uint32_t defining the resource + * values, corresponding to the array of type strings in the + * ResTable_package::typeStrings string block. Each of these hold an + * index from entriesStart; a value of NO_ENTRY means that entry is + * not defined. + * + * There may be multiple of these chunks for a particular resource type, + * supply different configuration variations for the resource values of + * that type. + * + * It would be nice to have an additional ordered index of entries, so + * we can do a binary search if trying to find a resource by string name. + */ +struct ResTable_type +{ + struct ResChunk_header header; + + enum { + NO_ENTRY = 0xFFFFFFFF + }; + + // The type identifier this chunk is holding. Type IDs start + // at 1 (corresponding to the value of the type bits in a + // resource identifier). 0 is invalid. + uint8_t id; + + // Must be 0. + uint8_t res0; + // Must be 0. + uint16_t res1; + + // Number of uint32_t entry indices that follow. + uint32_t entryCount; + + // Offset from header where ResTable_entry data starts. + uint32_t entriesStart; + + // Configuration this collection of entries is designed for. + ResTable_config config; +}; + +/** + * This is the beginning of information about an entry in the resource + * table. It holds the reference to the name of this entry, and is + * immediately followed by one of: + * * A Res_value structure, if FLAG_COMPLEX is -not- set. + * * An array of ResTable_map structures, if FLAG_COMPLEX is set. + * These supply a set of name/value mappings of data. + */ +struct ResTable_entry +{ + // Number of bytes in this structure. + uint16_t size; + + enum { + // If set, this is a complex entry, holding a set of name/value + // mappings. It is followed by an array of ResTable_map structures. + FLAG_COMPLEX = 0x0001, + // If set, this resource has been declared public, so libraries + // are allowed to reference it. + FLAG_PUBLIC = 0x0002, + // If set, this is a weak resource and may be overriden by strong + // resources of the same name/type. This is only useful during + // linking with other resource tables. + FLAG_WEAK = 0x0004 + }; + uint16_t flags; + + // Reference into ResTable_package::keyStrings identifying this entry. + struct ResStringPool_ref key; +}; + +/** + * Extended form of a ResTable_entry for map entries, defining a parent map + * resource from which to inherit values. + */ +struct ResTable_map_entry : public ResTable_entry +{ + // Resource identifier of the parent mapping, or 0 if there is none. + // This is always treated as a TYPE_DYNAMIC_REFERENCE. + ResTable_ref parent; + // Number of name/value pairs that follow for FLAG_COMPLEX. + uint32_t count; +}; + +/** + * A single name/value mapping that is part of a complex resource + * entry. + */ +struct ResTable_map +{ + // The resource identifier defining this mapping's name. For attribute + // resources, 'name' can be one of the following special resource types + // to supply meta-data about the attribute; for all other resource types + // it must be an attribute resource. + ResTable_ref name; + + // Special values for 'name' when defining attribute resources. + enum { + // This entry holds the attribute's type code. + ATTR_TYPE = Res_MAKEINTERNAL(0), + + // For integral attributes, this is the minimum value it can hold. + ATTR_MIN = Res_MAKEINTERNAL(1), + + // For integral attributes, this is the maximum value it can hold. + ATTR_MAX = Res_MAKEINTERNAL(2), + + // Localization of this resource is can be encouraged or required with + // an aapt flag if this is set + ATTR_L10N = Res_MAKEINTERNAL(3), + + // for plural support, see android.content.res.PluralRules#attrForQuantity(int) + ATTR_OTHER = Res_MAKEINTERNAL(4), + ATTR_ZERO = Res_MAKEINTERNAL(5), + ATTR_ONE = Res_MAKEINTERNAL(6), + ATTR_TWO = Res_MAKEINTERNAL(7), + ATTR_FEW = Res_MAKEINTERNAL(8), + ATTR_MANY = Res_MAKEINTERNAL(9) + + }; + + // Bit mask of allowed types, for use with ATTR_TYPE. + enum { + // No type has been defined for this attribute, use generic + // type handling. The low 16 bits are for types that can be + // handled generically; the upper 16 require additional information + // in the bag so can not be handled generically for TYPE_ANY. + TYPE_ANY = 0x0000FFFF, + + // Attribute holds a references to another resource. + TYPE_REFERENCE = 1<<0, + + // Attribute holds a generic string. + TYPE_STRING = 1<<1, + + // Attribute holds an integer value. ATTR_MIN and ATTR_MIN can + // optionally specify a constrained range of possible integer values. + TYPE_INTEGER = 1<<2, + + // Attribute holds a boolean integer. + TYPE_BOOLEAN = 1<<3, + + // Attribute holds a color value. + TYPE_COLOR = 1<<4, + + // Attribute holds a floating point value. + TYPE_FLOAT = 1<<5, + + // Attribute holds a dimension value, such as "20px". + TYPE_DIMENSION = 1<<6, + + // Attribute holds a fraction value, such as "20%". + TYPE_FRACTION = 1<<7, + + // Attribute holds an enumeration. The enumeration values are + // supplied as additional entries in the map. + TYPE_ENUM = 1<<16, + + // Attribute holds a bitmaks of flags. The flag bit values are + // supplied as additional entries in the map. + TYPE_FLAGS = 1<<17 + }; + + // Enum of localization modes, for use with ATTR_L10N. + enum { + L10N_NOT_REQUIRED = 0, + L10N_SUGGESTED = 1 + }; + + // This mapping's value. + Res_value value; +}; + +/** + * A package-id to package name mapping for any shared libraries used + * in this resource table. The package-id's encoded in this resource + * table may be different than the id's assigned at runtime. We must + * be able to translate the package-id's based on the package name. + */ +struct ResTable_lib_header +{ + struct ResChunk_header header; + + // The number of shared libraries linked in this resource table. + uint32_t count; +}; + +/** + * A shared library package-id to package name entry. + */ +struct ResTable_lib_entry +{ + // The package-id this shared library was assigned at build time. + // We use a uint32 to keep the structure aligned on a uint32 boundary. + uint32_t packageId; + + // The package name of the shared library. \0 terminated. + uint16_t packageName[128]; +}; + +/** + * Holds the shared library ID table. Shared libraries are assigned package IDs at + * build time, but they may be loaded in a different order, so we need to maintain + * a mapping of build-time package ID to run-time assigned package ID. + * + * Dynamic references are not currently supported in overlays. Only the base package + * may have dynamic references. + */ +class DynamicRefTable +{ +public: + DynamicRefTable(uint8_t packageId, bool appAsLib); + + // Loads an unmapped reference table from the package. + status_t load(const ResTable_lib_header* const header); + + // Adds mappings from the other DynamicRefTable + status_t addMappings(const DynamicRefTable& other); + + // Creates a mapping from build-time package ID to run-time package ID for + // the given package. + status_t addMapping(const String16& packageName, uint8_t packageId); + + // Performs the actual conversion of build-time resource ID to run-time + // resource ID. + inline status_t lookupResourceId(uint32_t* resId) const; + inline status_t lookupResourceValue(Res_value* value) const; + + inline const KeyedVector<String16, uint8_t>& entries() const { + return mEntries; + } + +private: + const uint8_t mAssignedPackageId; + uint8_t mLookupTable[256]; + KeyedVector<String16, uint8_t> mEntries; + bool mAppAsLib; +}; + +bool U16StringToInt(const char16_t* s, size_t len, Res_value* outValue); + +/** + * Convenience class for accessing data in a ResTable resource. + */ +class ResTable +{ +public: + ResTable(); + ResTable(const void* data, size_t size, const int32_t cookie, + bool copyData=false); + ~ResTable(); + + 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, + 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, Asset* idmapAsset, const int32_t cookie=-1, bool copyData=false, + bool appAsLib=false, bool isSystemAsset=false); + + status_t add(ResTable* src, bool isSystemAsset=false); + status_t addEmpty(const int32_t cookie); + + status_t getError() const; + + void uninit(); + + struct resource_name + { + const char16_t* package; + size_t packageLen; + const char16_t* type; + const char* type8; + size_t typeLen; + const char16_t* name; + const char* name8; + size_t nameLen; + }; + + bool getResourceName(uint32_t resID, bool allowUtf8, resource_name* outName) const; + + bool getResourceFlags(uint32_t resID, uint32_t* outFlags) const; + + /** + * Retrieve the value of a resource. If the resource is found, returns a + * value >= 0 indicating the table it is in (for use with + * getTableStringBlock() and getTableCookie()) and fills in 'outValue'. If + * not found, returns a negative error code. + * + * Note that this function does not do reference traversal. If you want + * to follow references to other resources to get the "real" value to + * use, you need to call resolveReference() after this function. + * + * @param resID The desired resoruce identifier. + * @param outValue Filled in with the resource data that was found. + * + * @return ssize_t Either a >= 0 table index or a negative error code. + */ + ssize_t getResource(uint32_t resID, Res_value* outValue, bool mayBeBag = false, + uint16_t density = 0, + uint32_t* outSpecFlags = NULL, + ResTable_config* outConfig = NULL) const; + + inline ssize_t getResource(const ResTable_ref& res, Res_value* outValue, + uint32_t* outSpecFlags=NULL) const { + return getResource(res.ident, outValue, false, 0, outSpecFlags, NULL); + } + + ssize_t resolveReference(Res_value* inOutValue, + ssize_t blockIndex, + uint32_t* outLastRef = NULL, + uint32_t* inoutTypeSpecFlags = NULL, + ResTable_config* outConfig = NULL) const; + + enum { + TMP_BUFFER_SIZE = 16 + }; + const char16_t* valueToString(const Res_value* value, size_t stringBlock, + char16_t tmpBuffer[TMP_BUFFER_SIZE], + size_t* outLen) const; + + struct bag_entry { + ssize_t stringBlock; + ResTable_map map; + }; + + /** + * Retrieve the bag of a resource. If the resoruce is found, returns the + * number of bags it contains and 'outBag' points to an array of their + * values. If not found, a negative error code is returned. + * + * Note that this function -does- do reference traversal of the bag data. + * + * @param resID The desired resource identifier. + * @param outBag Filled inm with a pointer to the bag mappings. + * + * @return ssize_t Either a >= 0 bag count of negative error code. + */ + ssize_t lockBag(uint32_t resID, const bag_entry** outBag) const; + + void unlockBag(const bag_entry* bag) const; + + void lock() const; + + ssize_t getBagLocked(uint32_t resID, const bag_entry** outBag, + uint32_t* outTypeSpecFlags=NULL) const; + + void unlock() const; + + class Theme { + public: + Theme(const ResTable& table); + ~Theme(); + + inline const ResTable& getResTable() const { return mTable; } + + status_t applyStyle(uint32_t resID, bool force=false); + status_t setTo(const Theme& other); + status_t clear(); + + /** + * Retrieve a value in the theme. If the theme defines this + * value, returns a value >= 0 indicating the table it is in + * (for use with getTableStringBlock() and getTableCookie) and + * fills in 'outValue'. If not found, returns a negative error + * code. + * + * Note that this function does not do reference traversal. If you want + * to follow references to other resources to get the "real" value to + * use, you need to call resolveReference() after this function. + * + * @param resID A resource identifier naming the desired theme + * attribute. + * @param outValue Filled in with the theme value that was + * found. + * + * @return ssize_t Either a >= 0 table index or a negative error code. + */ + ssize_t getAttribute(uint32_t resID, Res_value* outValue, + uint32_t* outTypeSpecFlags = NULL) const; + + /** + * This is like ResTable::resolveReference(), but also takes + * care of resolving attribute references to the theme. + */ + ssize_t resolveAttributeReference(Res_value* inOutValue, + ssize_t blockIndex, uint32_t* outLastRef = NULL, + uint32_t* inoutTypeSpecFlags = NULL, + ResTable_config* inoutConfig = NULL) const; + + /** + * Returns a bit mask of configuration changes that will impact this + * theme (and thus require completely reloading it). + */ + uint32_t getChangingConfigurations() const; + + void dumpToLog() const; + + private: + Theme(const Theme&); + Theme& operator=(const Theme&); + + struct theme_entry { + ssize_t stringBlock; + uint32_t typeSpecFlags; + Res_value value; + }; + + struct type_info { + size_t numEntries; + theme_entry* entries; + }; + + struct package_info { + type_info types[Res_MAXTYPE + 1]; + }; + + void free_package(package_info* pi); + package_info* copy_package(package_info* pi); + + const ResTable& mTable; + package_info* mPackages[Res_MAXPACKAGE]; + uint32_t mTypeSpecFlags; + }; + + void setParameters(const ResTable_config* params); + void getParameters(ResTable_config* params) const; + + // Retrieve an identifier (which can be passed to getResource) + // for a given resource name. The 'name' can be fully qualified + // (<package>:<type>.<basename>) or the package or type components + // can be dropped if default values are supplied here. + // + // Returns 0 if no such resource was found, else a valid resource ID. + uint32_t identifierForName(const char16_t* name, size_t nameLen, + const char16_t* type = 0, size_t typeLen = 0, + const char16_t* defPackage = 0, + size_t defPackageLen = 0, + uint32_t* outTypeSpecFlags = NULL) const; + + static bool expandResourceRef(const char16_t* refStr, size_t refLen, + String16* outPackage, + String16* outType, + String16* outName, + const String16* defType = NULL, + const String16* defPackage = NULL, + const char** outErrorMsg = NULL, + bool* outPublicOnly = NULL); + + static bool stringToInt(const char16_t* s, size_t len, Res_value* outValue); + static bool stringToFloat(const char16_t* s, size_t len, Res_value* outValue); + + // Used with stringToValue. + class Accessor + { + public: + inline virtual ~Accessor() { } + + virtual const String16& getAssetsPackage() const = 0; + + virtual uint32_t getCustomResource(const String16& package, + const String16& type, + const String16& name) const = 0; + virtual uint32_t getCustomResourceWithCreation(const String16& package, + const String16& type, + const String16& name, + const bool createIfNeeded = false) = 0; + virtual uint32_t getRemappedPackage(uint32_t origPackage) const = 0; + virtual bool getAttributeType(uint32_t attrID, uint32_t* outType) = 0; + virtual bool getAttributeMin(uint32_t attrID, uint32_t* outMin) = 0; + virtual bool getAttributeMax(uint32_t attrID, uint32_t* outMax) = 0; + virtual bool getAttributeEnum(uint32_t attrID, + const char16_t* name, size_t nameLen, + Res_value* outValue) = 0; + virtual bool getAttributeFlags(uint32_t attrID, + const char16_t* name, size_t nameLen, + Res_value* outValue) = 0; + virtual uint32_t getAttributeL10N(uint32_t attrID) = 0; + virtual bool getLocalizationSetting() = 0; + virtual void reportError(void* accessorCookie, const char* fmt, ...) = 0; + }; + + // Convert a string to a resource value. Handles standard "@res", + // "#color", "123", and "0x1bd" types; performs escaping of strings. + // The resulting value is placed in 'outValue'; if it is a string type, + // 'outString' receives the string. If 'attrID' is supplied, the value is + // type checked against this attribute and it is used to perform enum + // evaluation. If 'acccessor' is supplied, it will be used to attempt to + // resolve resources that do not exist in this ResTable. If 'attrType' is + // supplied, the value will be type checked for this format if 'attrID' + // is not supplied or found. + bool stringToValue(Res_value* outValue, String16* outString, + const char16_t* s, size_t len, + bool preserveSpaces, bool coerceType, + uint32_t attrID = 0, + const String16* defType = NULL, + const String16* defPackage = NULL, + Accessor* accessor = NULL, + void* accessorCookie = NULL, + uint32_t attrType = ResTable_map::TYPE_ANY, + bool enforcePrivate = true) const; + + // Perform processing of escapes and quotes in a string. + static bool collectString(String16* outString, + const char16_t* s, size_t len, + bool preserveSpaces, + const char** outErrorMsg = NULL, + bool append = false); + + size_t getBasePackageCount() const; + const String16 getBasePackageName(size_t idx) const; + uint32_t getBasePackageId(size_t idx) const; + uint32_t getLastTypeIdForPackage(size_t idx) const; + + // Return the number of resource tables that the object contains. + size_t getTableCount() const; + // Return the values string pool for the resource table at the given + // index. This string pool contains all of the strings for values + // contained in the resource table -- that is the item values themselves, + // but not the names their entries or types. + const ResStringPool* getTableStringBlock(size_t index) const; + // Return unique cookie identifier for the given resource table. + int32_t getTableCookie(size_t index) const; + + const DynamicRefTable* getDynamicRefTableForCookie(int32_t cookie) const; + + // Return the configurations (ResTable_config) that we know about + void getConfigurations(Vector<ResTable_config>* configs, bool ignoreMipmap=false, + bool ignoreAndroidPackage=false, bool includeSystemConfigs=true) const; + + void getLocales(Vector<String8>* locales, bool includeSystemLocales=true) const; + + // Generate an idmap. + // + // Return value: on success: NO_ERROR; caller is responsible for free-ing + // outData (using free(3)). On failure, any status_t value other than + // NO_ERROR; the caller should not free outData. + status_t createIdmap(const ResTable& overlay, + uint32_t targetCrc, uint32_t overlayCrc, + const char* targetPath, const char* overlayPath, + void** outData, size_t* outSize) const; + + static const size_t IDMAP_HEADER_SIZE_BYTES = 4 * sizeof(uint32_t) + 2 * 256; + + // Retrieve idmap meta-data. + // + // This function only requires the idmap header (the first + // IDMAP_HEADER_SIZE_BYTES) bytes of an idmap file. + static bool getIdmapInfo(const void* idmap, size_t size, + uint32_t* pVersion, + uint32_t* pTargetCrc, uint32_t* pOverlayCrc, + String8* pTargetPath, String8* pOverlayPath); + + void print(bool inclValues) const; + static String8 normalizeForOutput(const char* input); + +private: + struct Header; + struct Type; + struct Entry; + struct Package; + struct PackageGroup; + typedef Vector<Type*> TypeList; + + struct bag_set { + size_t numAttrs; // number in array + size_t availAttrs; // total space in array + uint32_t typeSpecFlags; + // Followed by 'numAttr' bag_entry structures. + }; + + /** + * Configuration dependent cached data. This must be cleared when the configuration is + * changed (setParameters). + */ + struct TypeCacheEntry { + TypeCacheEntry() : cachedBags(NULL) {} + + // Computed attribute bags for this type. + bag_set** cachedBags; + + // Pre-filtered list of configurations (per asset path) that match the parameters set on this + // ResTable. + Vector<std::shared_ptr<Vector<const ResTable_type*>>> filteredConfigs; + }; + + status_t addInternal(const void* data, size_t size, const void* idmapData, size_t idmapDataSize, + bool appAsLib, const int32_t cookie, bool copyData, bool isSystemAsset=false); + + ssize_t getResourcePackageIndex(uint32_t resID) const; + + status_t getEntry( + const PackageGroup* packageGroup, int typeIndex, int entryIndex, + const ResTable_config* config, + Entry* outEntry) const; + + uint32_t findEntry(const PackageGroup* group, ssize_t typeIndex, const char16_t* name, + size_t nameLen, uint32_t* outTypeSpecFlags) const; + + status_t parsePackage( + const ResTable_package* const pkg, const Header* const header, + bool appAsLib, bool isSystemAsset); + + void print_value(const Package* pkg, const Res_value& value) const; + + template <typename Func> + void forEachConfiguration(bool ignoreMipmap, bool ignoreAndroidPackage, + bool includeSystemConfigs, const Func& f) const; + + mutable Mutex mLock; + + // Mutex that controls access to the list of pre-filtered configurations + // to check when looking up entries. + // When iterating over a bag, the mLock mutex is locked. While mLock is locked, + // we do resource lookups. + // Mutex is not reentrant, so we must use a different lock than mLock. + mutable Mutex mFilteredConfigLock; + + status_t mError; + + ResTable_config mParams; + + // Array of all resource tables. + Vector<Header*> mHeaders; + + // Array of packages in all resource tables. + Vector<PackageGroup*> mPackageGroups; + + // Mapping from resource package IDs to indices into the internal + // package array. + uint8_t mPackageMap[256]; + + uint8_t mNextPackageId; +}; + +} // namespace android + +#endif // _LIBS_UTILS_RESOURCE_TYPES_H diff --git a/libs/androidfw/include/androidfw/StreamingZipInflater.h b/libs/androidfw/include/androidfw/StreamingZipInflater.h new file mode 100644 index 000000000000..3ace5d5a83cf --- /dev/null +++ b/libs/androidfw/include/androidfw/StreamingZipInflater.h @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __LIBS_STREAMINGZIPINFLATER_H +#define __LIBS_STREAMINGZIPINFLATER_H + +#include <unistd.h> +#include <inttypes.h> +#include <zlib.h> + +#include <utils/Compat.h> + +namespace android { + +class StreamingZipInflater { +public: + static const size_t INPUT_CHUNK_SIZE = 64 * 1024; + static const size_t OUTPUT_CHUNK_SIZE = 64 * 1024; + + // Flavor that pages in the compressed data from a fd + StreamingZipInflater(int fd, off64_t compDataStart, size_t uncompSize, size_t compSize); + + // Flavor that gets the compressed data from an in-memory buffer + StreamingZipInflater(class FileMap* dataMap, size_t uncompSize); + + ~StreamingZipInflater(); + + // read 'count' bytes of uncompressed data from the current position. outBuf may + // be NULL, in which case the data is consumed and discarded. + ssize_t read(void* outBuf, size_t count); + + // seeking backwards requires uncompressing fom the beginning, so is very + // expensive. seeking forwards only requires uncompressing from the current + // position to the destination. + off64_t seekAbsolute(off64_t absoluteInputPosition); + +private: + void initInflateState(); + int readNextChunk(); + + // where to find the uncompressed data + int mFd; + off64_t mInFileStart; // where the compressed data lives in the file + class FileMap* mDataMap; + + z_stream mInflateState; + bool mStreamNeedsInit; + + // output invariants for this asset + uint8_t* mOutBuf; // output buf for decompressed bytes + size_t mOutBufSize; // allocated size of mOutBuf + size_t mOutTotalSize; // total uncompressed size of the blob + + // current output state bookkeeping + off64_t mOutCurPosition; // current position in total offset + size_t mOutLastDecoded; // last decoded byte + 1 in mOutbuf + size_t mOutDeliverable; // next undelivered byte of decoded output in mOutBuf + + // input invariants + uint8_t* mInBuf; + size_t mInBufSize; // allocated size of mInBuf; + size_t mInTotalSize; // total size of compressed data for this blob + + // input state bookkeeping + size_t mInNextChunkOffset; // offset from start of blob at which the next input chunk lies + // the z_stream contains state about input block consumption +}; + +} + +#endif diff --git a/libs/androidfw/include/androidfw/TypeWrappers.h b/libs/androidfw/include/androidfw/TypeWrappers.h new file mode 100644 index 000000000000..fd848736d2d6 --- /dev/null +++ b/libs/androidfw/include/androidfw/TypeWrappers.h @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __TYPE_WRAPPERS_H +#define __TYPE_WRAPPERS_H + +#include <androidfw/ResourceTypes.h> + +namespace android { + +struct TypeVariant { + TypeVariant(const ResTable_type* data) + : data(data) {} + + class iterator { + public: + iterator& operator=(const iterator& rhs) { + mTypeVariant = rhs.mTypeVariant; + mIndex = rhs.mIndex; + return *this; + } + + bool operator==(const iterator& rhs) const { + return mTypeVariant == rhs.mTypeVariant && mIndex == rhs.mIndex; + } + + bool operator!=(const iterator& rhs) const { + return mTypeVariant != rhs.mTypeVariant || mIndex != rhs.mIndex; + } + + iterator operator++(int) { + uint32_t prevIndex = mIndex; + operator++(); + return iterator(mTypeVariant, prevIndex); + } + + const ResTable_entry* operator->() const { + return operator*(); + } + + uint32_t index() const { + return mIndex; + } + + iterator& operator++(); + const ResTable_entry* operator*() const; + + private: + friend struct TypeVariant; + iterator(const TypeVariant* tv, uint32_t index) + : mTypeVariant(tv), mIndex(index) {} + const TypeVariant* mTypeVariant; + uint32_t mIndex; + }; + + iterator beginEntries() const { + return iterator(this, 0); + } + + iterator endEntries() const { + return iterator(this, dtohl(data->entryCount)); + } + + const ResTable_type* data; +}; + +} // namespace android + +#endif // __TYPE_WRAPPERS_H diff --git a/libs/androidfw/include/androidfw/ZipFileRO.h b/libs/androidfw/include/androidfw/ZipFileRO.h new file mode 100644 index 000000000000..768034287afa --- /dev/null +++ b/libs/androidfw/include/androidfw/ZipFileRO.h @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Read-only access to Zip archives, with minimal heap allocation. + * + * This is similar to the more-complete ZipFile class, but no attempt + * has been made to make them interchangeable. This class operates under + * a very different set of assumptions and constraints. + * + * One such assumption is that if you're getting file descriptors for + * use with this class as a child of a fork() operation, you must be on + * a pread() to guarantee correct operation. This is because pread() can + * atomically read at a file offset without worrying about a lock around an + * lseek() + read() pair. + */ +#ifndef __LIBS_ZIPFILERO_H +#define __LIBS_ZIPFILERO_H + +#include <utils/Compat.h> +#include <utils/Errors.h> +#include <utils/FileMap.h> +#include <utils/threads.h> + +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <time.h> + +typedef void* ZipArchiveHandle; + +namespace android { + +/* + * Trivial typedef to ensure that ZipEntryRO is not treated as a simple + * integer. We use NULL to indicate an invalid value. + */ +typedef void* ZipEntryRO; + +/* + * Open a Zip archive for reading. + * + * Implemented as a thin wrapper over system/core/libziparchive. + * + * "open" and "find entry by name" are fast operations and use as little + * memory as possible. + * + * We also support fast iteration over all entries in the file (with a + * stable, but unspecified iteration order). + * + * NOTE: If this is used on file descriptors inherited from a fork() operation, + * you must be on a platform that implements pread() to guarantee correctness + * on the shared file descriptors. + */ +class ZipFileRO { +public: + /* Zip compression methods we support */ + enum : uint16_t { + kCompressStored = 0, + kCompressDeflated = 8 + }; + + /* + * Open an archive. + */ + static ZipFileRO* open(const char* zipFileName); + + /* + * Find an entry, by name. Returns the entry identifier, or NULL if + * not found. + */ + ZipEntryRO findEntryByName(const char* entryName) const; + + + /* + * Start iterating over the list of entries in the zip file. Requires + * a matching call to endIteration with the same cookie. + */ + bool startIteration(void** cookie); + bool startIteration(void** cookie, const char* prefix, const char* suffix); + + /** + * Return the next entry in iteration order, or NULL if there are no more + * entries in this archive. + */ + ZipEntryRO nextEntry(void* cookie); + + void endIteration(void* cookie); + + void releaseEntry(ZipEntryRO entry) const; + + /* + * Return the #of entries in the Zip archive. + */ + int getNumEntries(); + + /* + * Copy the filename into the supplied buffer. Returns 0 on success, + * -1 if "entry" is invalid, or the filename length if it didn't fit. The + * length, and the returned string, include the null-termination. + */ + int getEntryFileName(ZipEntryRO entry, char* buffer, size_t bufLen) const; + + /* + * Get the vital stats for an entry. Pass in NULL pointers for anything + * you don't need. + * + * "*pOffset" holds the Zip file offset of the entry's data. + * + * Returns "false" if "entry" is bogus or if the data in the Zip file + * appears to be bad. + */ + bool getEntryInfo(ZipEntryRO entry, uint16_t* pMethod, uint32_t* pUncompLen, + uint32_t* pCompLen, off64_t* pOffset, uint32_t* pModWhen, + uint32_t* pCrc32) const; + + /* + * Create a new FileMap object that maps a subset of the archive. For + * an uncompressed entry this effectively provides a pointer to the + * actual data, for a compressed entry this provides the input buffer + * for inflate(). + */ + FileMap* createEntryFileMap(ZipEntryRO entry) const; + + /* + * Uncompress the data into a buffer. Depending on the compression + * format, this is either an "inflate" operation or a memcpy. + * + * Use "uncompLen" from getEntryInfo() to determine the required + * buffer size. + * + * Returns "true" on success. + */ + bool uncompressEntry(ZipEntryRO entry, void* buffer, size_t size) const; + + /* + * Uncompress the data to an open file descriptor. + */ + bool uncompressEntry(ZipEntryRO entry, int fd) const; + + ~ZipFileRO(); + +private: + /* these are private and not defined */ + ZipFileRO(const ZipFileRO& src); + ZipFileRO& operator=(const ZipFileRO& src); + + ZipFileRO(ZipArchiveHandle handle, char* fileName) : mHandle(handle), + mFileName(fileName) + { + } + + const ZipArchiveHandle mHandle; + char* mFileName; +}; + +}; // namespace android + +#endif /*__LIBS_ZIPFILERO_H*/ diff --git a/libs/androidfw/include/androidfw/ZipUtils.h b/libs/androidfw/include/androidfw/ZipUtils.h new file mode 100644 index 000000000000..55575d774522 --- /dev/null +++ b/libs/androidfw/include/androidfw/ZipUtils.h @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// +// Miscellaneous zip/gzip utility functions. +// +#ifndef __LIBS_ZIPUTILS_H +#define __LIBS_ZIPUTILS_H + +#include <stdint.h> +#include <string.h> +#include <stdio.h> +#include <time.h> + +namespace android { + +/* + * Container class for utility functions, primarily for namespace reasons. + */ +class ZipUtils { +public: + /* + * General utility function for uncompressing "deflate" data from a file + * to a buffer. + */ + static bool inflateToBuffer(FILE* fp, void* buf, long uncompressedLen, + long compressedLen); + static bool inflateToBuffer(int fd, void* buf, long uncompressedLen, + long compressedLen); + static bool inflateToBuffer(void *in, void* buf, long uncompressedLen, + long compressedLen); + + /* + * Someday we might want to make this generic and handle bzip2 ".bz2" + * files too. + * + * We could declare gzip to be a sub-class of zip that has exactly + * one always-compressed entry, but we currently want to treat Zip + * and gzip as distinct, so there's no value. + * + * The zlib library has some gzip utilities, but it has no interface + * for extracting the uncompressed length of the file (you do *not* + * want to gzseek to the end). + * + * Pass in a seeked file pointer for the gzip file. If this is a gzip + * file, we set our return values appropriately and return "true" with + * the file seeked to the start of the compressed data. + */ + static bool examineGzip(FILE* fp, int* pCompressionMethod, + long* pUncompressedLen, long* pCompressedLen, unsigned long* pCRC32); + + /* + * Utility function to convert ZIP's time format to a timespec struct. + * + * NOTE: this method will clear all existing state from |timespec|. + */ + static inline void zipTimeToTimespec(uint32_t when, struct tm* timespec) { + const uint32_t date = when >> 16; + + memset(timespec, 0, sizeof(struct tm)); + timespec->tm_year = ((date >> 9) & 0x7F) + 80; // Zip is years since 1980 + timespec->tm_mon = ((date >> 5) & 0x0F) - 1; + timespec->tm_mday = date & 0x1F; + + timespec->tm_hour = (when >> 11) & 0x1F; + timespec->tm_min = (when >> 5) & 0x3F; + timespec->tm_sec = (when & 0x1F) << 1; + timespec->tm_isdst = -1; + } +private: + ZipUtils() {} + ~ZipUtils() {} +}; + +}; // namespace android + +#endif /*__LIBS_ZIPUTILS_H*/ diff --git a/libs/androidfw/include/androidfw/misc.h b/libs/androidfw/include/androidfw/misc.h new file mode 100644 index 000000000000..5a5a0e29125d --- /dev/null +++ b/libs/androidfw/include/androidfw/misc.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2005 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <sys/types.h> + +// +// Handy utility functions and portability code. +// +#ifndef _LIBS_ANDROID_FW_MISC_H +#define _LIBS_ANDROID_FW_MISC_H + +namespace android { + +/* + * Some utility functions for working with files. These could be made + * part of a "File" class. + */ +typedef enum FileType { + kFileTypeUnknown = 0, + kFileTypeNonexistent, // i.e. ENOENT + kFileTypeRegular, + kFileTypeDirectory, + kFileTypeCharDev, + kFileTypeBlockDev, + kFileTypeFifo, + kFileTypeSymlink, + kFileTypeSocket, +} FileType; +/* get the file's type; follows symlinks */ +FileType getFileType(const char* fileName); +/* get the file's modification date; returns -1 w/errno set on failure */ +time_t getFileModDate(const char* fileName); + +}; // namespace android + +#endif // _LIBS_ANDROID_FW_MISC_H diff --git a/libs/common_time/common_clock_service.h b/libs/common_time/common_clock_service.h index bd663f06d43a..aea507ec0f30 100644 --- a/libs/common_time/common_clock_service.h +++ b/libs/common_time/common_clock_service.h @@ -53,7 +53,7 @@ class CommonClockService : public BnCommonClock, void notifyOnTimelineChanged(uint64_t timelineID); private: - CommonClockService(CommonTimeServer& timeServer) + explicit CommonClockService(CommonTimeServer& timeServer) : mTimeServer(timeServer) { }; virtual void binderDied(const wp<IBinder>& who); diff --git a/libs/common_time/common_time_config_service.h b/libs/common_time/common_time_config_service.h index 89806dd87e29..23abb1a9e47e 100644 --- a/libs/common_time/common_time_config_service.h +++ b/libs/common_time/common_time_config_service.h @@ -49,7 +49,7 @@ class CommonTimeConfigService : public BnCommonTimeConfig { virtual status_t forceNetworklessMasterMode(); private: - CommonTimeConfigService(CommonTimeServer& timeServer) + explicit CommonTimeConfigService(CommonTimeServer& timeServer) : mTimeServer(timeServer) { } CommonTimeServer& mTimeServer; diff --git a/libs/common_time/common_time_server.cpp b/libs/common_time/common_time_server.cpp index f72ffaa63712..b1495effbc81 100644 --- a/libs/common_time/common_time_server.cpp +++ b/libs/common_time/common_time_server.cpp @@ -615,12 +615,11 @@ bool CommonTimeServer::handlePacket() { ssize_t recvBytes = recvfrom( mSocket, buf, sizeof(buf), 0, - reinterpret_cast<const sockaddr *>(&srcAddr), &srcAddrLen); + reinterpret_cast<sockaddr *>(&srcAddr), &srcAddrLen); if (recvBytes < 0) { - mBadPktLog.log(ANDROID_LOG_ERROR, LOG_TAG, - "recvfrom failed (res %d, errno %d)", - recvBytes, errno); + mBadPktLog.log(ANDROID_LOG_ERROR, LOG_TAG, "recvfrom failed (%s)", + strerror(errno)); return false; } diff --git a/libs/common_time/common_time_server_api.cpp b/libs/common_time/common_time_server_api.cpp index e0f35a9def8b..60e65677b2f9 100644 --- a/libs/common_time/common_time_server_api.cpp +++ b/libs/common_time/common_time_server_api.cpp @@ -285,7 +285,7 @@ void CommonTimeServer::reevaluateAutoDisableState(bool commonClockHasClients) { if (res > 0) \ write(fd, buffer, res); \ } while (0) -#define checked_percentage(a, b) ((0 == b) ? 0.0f : ((100.0f * a) / b)) +#define checked_percentage(a, b) ((0 == (b)) ? 0.0f : ((100.0f * (a)) / (b))) status_t CommonTimeServer::dumpClockInterface(int fd, const Vector<String16>& /* args */, diff --git a/libs/common_time/common_time_server_packets.cpp b/libs/common_time/common_time_server_packets.cpp index 9833c37f2519..c7c893d34499 100644 --- a/libs/common_time/common_time_server_packets.cpp +++ b/libs/common_time/common_time_server_packets.cpp @@ -48,12 +48,12 @@ const uint16_t TimeServicePacketHeader::kCurVersion = 1; #define SERIALIZE_INT32(field_name) SERIALIZE_FIELD(field_name, int32_t, htonl) #define SERIALIZE_INT64(field_name) SERIALIZE_FIELD(field_name, int64_t, htonq) -#define DESERIALIZE_FIELD(field_name, type, converter) \ - do { \ - if ((offset + sizeof(field_name)) > length) \ - return -1; \ - field_name = converter(*((type*)(data + offset))); \ - offset += sizeof(field_name); \ +#define DESERIALIZE_FIELD(field_name, type, converter) \ + do { \ + if ((offset + sizeof(field_name)) > length) \ + return -1; \ + (field_name) = converter(*((type*)(data + offset))); \ + offset += sizeof(field_name); \ } while (0) #define DESERIALIZE_INT16(field_name) DESERIALIZE_FIELD(field_name, int16_t, ntohs) #define DESERIALIZE_INT32(field_name) DESERIALIZE_FIELD(field_name, int32_t, ntohl) diff --git a/libs/hwui/Android.mk b/libs/hwui/Android.mk index cacfce16ab9a..a7cbf5e562d1 100644 --- a/libs/hwui/Android.mk +++ b/libs/hwui/Android.mk @@ -193,7 +193,6 @@ LOCAL_SRC_FILES := $(hwui_src_files) LOCAL_C_INCLUDES := $(hwui_c_includes) $(call hwui_proto_include) LOCAL_EXPORT_C_INCLUDE_DIRS := \ $(LOCAL_PATH) \ - $(hwui_c_includes) \ $(call hwui_proto_include) include $(LOCAL_PATH)/hwui_static_deps.mk @@ -217,7 +216,6 @@ LOCAL_SRC_FILES := \ LOCAL_C_INCLUDES := $(hwui_c_includes) $(call hwui_proto_include) LOCAL_EXPORT_C_INCLUDE_DIRS := \ $(LOCAL_PATH) \ - $(hwui_c_includes) \ $(call hwui_proto_include) include $(LOCAL_PATH)/hwui_static_deps.mk @@ -250,6 +248,7 @@ LOCAL_SHARED_LIBRARIES := libmemunreachable LOCAL_CFLAGS := \ $(hwui_cflags) \ -DHWUI_NULL_GPU +LOCAL_C_INCLUDES := $(hwui_c_includes) LOCAL_SRC_FILES += \ $(hwui_test_common_src_files) \ @@ -305,6 +304,7 @@ LOCAL_MULTILIB := both LOCAL_MODULE_STEM_32 := hwuitest LOCAL_MODULE_STEM_64 := hwuitest64 LOCAL_CFLAGS := $(hwui_cflags) +LOCAL_C_INCLUDES := $(hwui_c_includes) # set to libhwui_static_null_gpu to skip actual GL commands LOCAL_WHOLE_STATIC_LIBRARIES := libhwui_static @@ -333,6 +333,8 @@ LOCAL_CFLAGS := \ $(hwui_cflags) \ -DHWUI_NULL_GPU +LOCAL_C_INCLUDES := $(hwui_c_includes) + LOCAL_WHOLE_STATIC_LIBRARIES := libhwui_static_null_gpu LOCAL_STATIC_LIBRARIES := libgoogle-benchmark diff --git a/libs/hwui/AnimationContext.h b/libs/hwui/AnimationContext.h index 11d305c3c8d0..71ee8602a29f 100644 --- a/libs/hwui/AnimationContext.h +++ b/libs/hwui/AnimationContext.h @@ -57,7 +57,7 @@ public: private: friend class AnimationContext; - AnimationHandle(AnimationContext& context); + explicit AnimationHandle(AnimationContext& context); AnimationHandle(RenderNode& animatingNode, AnimationContext& context); ~AnimationHandle(); @@ -75,7 +75,7 @@ private: class AnimationContext { PREVENT_COPY_AND_ASSIGN(AnimationContext); public: - ANDROID_API AnimationContext(renderthread::TimeLord& clock); + ANDROID_API explicit AnimationContext(renderthread::TimeLord& clock); ANDROID_API virtual ~AnimationContext(); nsecs_t frameTimeMs() { return mFrameTimeMs; } diff --git a/libs/hwui/Animator.h b/libs/hwui/Animator.h index 72bac6c2a515..aac355c11ac9 100644 --- a/libs/hwui/Animator.h +++ b/libs/hwui/Animator.h @@ -124,7 +124,7 @@ protected: Finished, }; - BaseRenderNodeAnimator(float finalValue); + explicit BaseRenderNodeAnimator(float finalValue); virtual ~BaseRenderNodeAnimator(); virtual float getValue(RenderNode* target) const = 0; diff --git a/libs/hwui/AnimatorManager.cpp b/libs/hwui/AnimatorManager.cpp index f170e9cda8af..9d5860ca0d1a 100644 --- a/libs/hwui/AnimatorManager.cpp +++ b/libs/hwui/AnimatorManager.cpp @@ -168,7 +168,7 @@ void AnimatorManager::endAllStagingAnimators() { class EndActiveAnimatorsFunctor { public: - EndActiveAnimatorsFunctor(AnimationContext& context) : mContext(context) {} + explicit EndActiveAnimatorsFunctor(AnimationContext& context) : mContext(context) {} void operator() (sp<BaseRenderNodeAnimator>& animator) { animator->forceEndNow(mContext); diff --git a/libs/hwui/AnimatorManager.h b/libs/hwui/AnimatorManager.h index 61f6179d217c..8e6f820608b5 100644 --- a/libs/hwui/AnimatorManager.h +++ b/libs/hwui/AnimatorManager.h @@ -35,7 +35,7 @@ class TreeInfo; class AnimatorManager { PREVENT_COPY_AND_ASSIGN(AnimatorManager); public: - AnimatorManager(RenderNode& parent); + explicit AnimatorManager(RenderNode& parent); ~AnimatorManager(); void addAnimator(const sp<BaseRenderNodeAnimator>& animator); diff --git a/libs/hwui/AssetAtlas.cpp b/libs/hwui/AssetAtlas.cpp index 6afff1b7158e..e2e7037202b8 100644 --- a/libs/hwui/AssetAtlas.cpp +++ b/libs/hwui/AssetAtlas.cpp @@ -27,7 +27,7 @@ namespace uirenderer { // Lifecycle /////////////////////////////////////////////////////////////////////////////// -void AssetAtlas::init(sp<GraphicBuffer> buffer, int64_t* map, int count) { +void AssetAtlas::init(const sp<GraphicBuffer>& buffer, int64_t* map, int count) { if (mImage) { return; } diff --git a/libs/hwui/AssetAtlas.h b/libs/hwui/AssetAtlas.h index 75400ff494c3..b32e51851b94 100644 --- a/libs/hwui/AssetAtlas.h +++ b/libs/hwui/AssetAtlas.h @@ -109,7 +109,7 @@ public: * initialized. To re-initialize the atlas, you must * first call terminate(). */ - ANDROID_API void init(sp<GraphicBuffer> buffer, int64_t* map, int count); + ANDROID_API void init(const sp<GraphicBuffer>& buffer, int64_t* map, int count); /** * Destroys the atlas texture. This object can be diff --git a/libs/hwui/Caches.h b/libs/hwui/Caches.h index eac9359beab3..317c122c62b8 100644 --- a/libs/hwui/Caches.h +++ b/libs/hwui/Caches.h @@ -77,7 +77,7 @@ public: return sInstance != nullptr; } private: - Caches(RenderState& renderState); + explicit Caches(RenderState& renderState); static Caches* sInstance; public: diff --git a/libs/hwui/CanvasProperty.h b/libs/hwui/CanvasProperty.h index 6074394d0720..56671bba654b 100644 --- a/libs/hwui/CanvasProperty.h +++ b/libs/hwui/CanvasProperty.h @@ -28,7 +28,7 @@ namespace uirenderer { class CanvasPropertyPrimitive : public VirtualLightRefBase { PREVENT_COPY_AND_ASSIGN(CanvasPropertyPrimitive); public: - CanvasPropertyPrimitive(float initialValue) : value(initialValue) {} + explicit CanvasPropertyPrimitive(float initialValue) : value(initialValue) {} float value; }; @@ -36,7 +36,7 @@ public: class CanvasPropertyPaint : public VirtualLightRefBase { PREVENT_COPY_AND_ASSIGN(CanvasPropertyPaint); public: - CanvasPropertyPaint(const SkPaint& initialValue) : value(initialValue) {} + explicit CanvasPropertyPaint(const SkPaint& initialValue) : value(initialValue) {} SkPaint value; }; diff --git a/libs/hwui/CanvasState.h b/libs/hwui/CanvasState.h index b9e87ae5595d..22feef523f49 100644 --- a/libs/hwui/CanvasState.h +++ b/libs/hwui/CanvasState.h @@ -73,7 +73,7 @@ public: class CanvasState { public: - CanvasState(CanvasStateClient& renderer); + explicit CanvasState(CanvasStateClient& renderer); ~CanvasState(); /** diff --git a/libs/hwui/ClipArea.h b/libs/hwui/ClipArea.h index 53d9d03a3629..2e561601d452 100644 --- a/libs/hwui/ClipArea.h +++ b/libs/hwui/ClipArea.h @@ -97,9 +97,9 @@ enum class ClipMode { }; struct ClipBase { - ClipBase(ClipMode mode) + explicit ClipBase(ClipMode mode) : mode(mode) {} - ClipBase(const Rect& rect) + explicit ClipBase(const Rect& rect) : mode(ClipMode::Rectangle) , rect(rect) {} const ClipMode mode; @@ -112,19 +112,19 @@ struct ClipBase { }; struct ClipRect : ClipBase { - ClipRect(const Rect& rect) + explicit ClipRect(const Rect& rect) : ClipBase(rect) {} }; struct ClipRectList : ClipBase { - ClipRectList(const RectangleList& rectList) + explicit ClipRectList(const RectangleList& rectList) : ClipBase(ClipMode::RectangleList) , rectList(rectList) {} RectangleList rectList; }; struct ClipRegion : ClipBase { - ClipRegion(const SkRegion& region) + explicit ClipRegion(const SkRegion& region) : ClipBase(ClipMode::Region) , region(region) {} ClipRegion() diff --git a/libs/hwui/DeferredDisplayList.cpp b/libs/hwui/DeferredDisplayList.cpp index 1b0f42466bff..689179dd8fb4 100644 --- a/libs/hwui/DeferredDisplayList.cpp +++ b/libs/hwui/DeferredDisplayList.cpp @@ -62,7 +62,7 @@ public: class DrawBatch : public Batch { public: - DrawBatch(const DeferInfo& deferInfo) : mAllOpsOpaque(true), + explicit DrawBatch(const DeferInfo& deferInfo) : mAllOpsOpaque(true), mBatchId(deferInfo.batchId), mMergeId(deferInfo.mergeId) { mOps.clear(); } diff --git a/libs/hwui/DeferredDisplayList.h b/libs/hwui/DeferredDisplayList.h index 98ccf11b1c2a..15703ea3feef 100644 --- a/libs/hwui/DeferredDisplayList.h +++ b/libs/hwui/DeferredDisplayList.h @@ -77,7 +77,7 @@ public: class DeferredDisplayList { friend struct DeferStateStruct; // used to give access to allocator public: - DeferredDisplayList(const Rect& bounds) + explicit DeferredDisplayList(const Rect& bounds) : mBounds(bounds) { clear(); } diff --git a/libs/hwui/DeferredLayerUpdater.h b/libs/hwui/DeferredLayerUpdater.h index 44a24c840892..40058223fb48 100644 --- a/libs/hwui/DeferredLayerUpdater.h +++ b/libs/hwui/DeferredLayerUpdater.h @@ -35,7 +35,7 @@ class DeferredLayerUpdater : public VirtualLightRefBase { public: // Note that DeferredLayerUpdater assumes it is taking ownership of the layer // and will not call incrementRef on it as a result. - ANDROID_API DeferredLayerUpdater(Layer* layer); + ANDROID_API explicit DeferredLayerUpdater(Layer* layer); ANDROID_API ~DeferredLayerUpdater(); ANDROID_API bool setSize(int width, int height) { diff --git a/libs/hwui/DisplayListOp.h b/libs/hwui/DisplayListOp.h index 59f073ff593c..51b987c7ed85 100644 --- a/libs/hwui/DisplayListOp.h +++ b/libs/hwui/DisplayListOp.h @@ -111,7 +111,7 @@ public: class DrawOp : public DisplayListOp { friend class MergingDrawBatch; public: - DrawOp(const SkPaint* paint) + explicit DrawOp(const SkPaint* paint) : mPaint(paint), mQuickRejected(false) {} virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level, @@ -244,7 +244,7 @@ public: } // default empty constructor for bounds, to be overridden in child constructor body - DrawBoundedOp(const SkPaint* paint): DrawOp(paint) { } + explicit DrawBoundedOp(const SkPaint* paint): DrawOp(paint) { } virtual bool getLocalBounds(Rect& localBounds) override { localBounds.set(mLocalBounds); @@ -269,7 +269,7 @@ protected: class SaveOp : public StateOp { public: - SaveOp(int flags) + explicit SaveOp(int flags) : mFlags(flags) {} virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level, @@ -295,7 +295,7 @@ private: class RestoreToCountOp : public StateOp { public: - RestoreToCountOp(int count) + explicit RestoreToCountOp(int count) : mCount(count) {} virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level, @@ -408,7 +408,7 @@ private: class RotateOp : public StateOp { public: - RotateOp(float degrees) + explicit RotateOp(float degrees) : mDegrees(degrees) {} virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override { @@ -467,7 +467,7 @@ private: class SetMatrixOp : public StateOp { public: - SetMatrixOp(const SkMatrix& matrix) + explicit SetMatrixOp(const SkMatrix& matrix) : mMatrix(matrix) {} virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override { @@ -492,7 +492,7 @@ private: class ConcatMatrixOp : public StateOp { public: - ConcatMatrixOp(const SkMatrix& matrix) + explicit ConcatMatrixOp(const SkMatrix& matrix) : mMatrix(matrix) {} virtual void applyState(OpenGLRenderer& renderer, int saveCount) const override { @@ -511,7 +511,7 @@ private: class ClipOp : public StateOp { public: - ClipOp(SkRegion::Op op) : mOp(op) {} + explicit ClipOp(SkRegion::Op op) : mOp(op) {} virtual void defer(DeferStateStruct& deferStruct, int saveCount, int level, bool useQuickReject) override { @@ -621,8 +621,8 @@ public: } #define SET_TEXTURE(ptr, posRect, offsetRect, texCoordsRect, xDim, yDim) \ - TextureVertex::set(ptr++, posRect.xDim - offsetRect.left, posRect.yDim - offsetRect.top, \ - texCoordsRect.xDim, texCoordsRect.yDim) + TextureVertex::set((ptr)++, (posRect).xDim - (offsetRect).left, (posRect).yDim - (offsetRect).top, \ + (texCoordsRect).xDim, (texCoordsRect).yDim) /** * This multi-draw operation builds a mesh on the stack by generating a quad @@ -1384,7 +1384,7 @@ private: class DrawFunctorOp : public DrawOp { public: - DrawFunctorOp(Functor* functor) + explicit DrawFunctorOp(Functor* functor) : DrawOp(nullptr), mFunctor(functor) {} virtual void applyDraw(OpenGLRenderer& renderer, Rect& dirty) override { diff --git a/libs/hwui/Dither.h b/libs/hwui/Dither.h index b589b80496f0..6af3e8384472 100644 --- a/libs/hwui/Dither.h +++ b/libs/hwui/Dither.h @@ -37,7 +37,7 @@ class Program; */ class Dither { public: - Dither(Caches& caches); + explicit Dither(Caches& caches); void clear(); void setupProgram(Program& program, GLuint* textureUnit); diff --git a/libs/hwui/FontRenderer.h b/libs/hwui/FontRenderer.h index 504dce862f71..1e59a966750e 100644 --- a/libs/hwui/FontRenderer.h +++ b/libs/hwui/FontRenderer.h @@ -100,7 +100,7 @@ public: class FontRenderer { public: - FontRenderer(const uint8_t* gammaTable); + explicit FontRenderer(const uint8_t* gammaTable); ~FontRenderer(); void flushLargeCaches(std::vector<CacheTexture*>& cacheTextures); diff --git a/libs/hwui/FrameInfo.h b/libs/hwui/FrameInfo.h index 45b57de0b504..bac9d12d273c 100644 --- a/libs/hwui/FrameInfo.h +++ b/libs/hwui/FrameInfo.h @@ -69,7 +69,7 @@ namespace FrameInfoFlags { class ANDROID_API UiFrameInfoBuilder { public: - UiFrameInfoBuilder(int64_t* buffer) : mBuffer(buffer) { + explicit UiFrameInfoBuilder(int64_t* buffer) : mBuffer(buffer) { memset(mBuffer, 0, UI_THREAD_FRAME_INFO_SIZE * sizeof(int64_t)); } diff --git a/libs/hwui/FrameInfoVisualizer.h b/libs/hwui/FrameInfoVisualizer.h index 83adf1985c72..719d0f8c5032 100644 --- a/libs/hwui/FrameInfoVisualizer.h +++ b/libs/hwui/FrameInfoVisualizer.h @@ -45,7 +45,7 @@ typedef RingBuffer<FrameInfo, 120> FrameInfoSource; class FrameInfoVisualizer { public: - FrameInfoVisualizer(FrameInfoSource& source); + explicit FrameInfoVisualizer(FrameInfoSource& source); ~FrameInfoVisualizer(); bool consumeProperties(); diff --git a/libs/hwui/GpuMemoryTracker.h b/libs/hwui/GpuMemoryTracker.h index 851aeae8352c..bfb1bf1ba098 100644 --- a/libs/hwui/GpuMemoryTracker.h +++ b/libs/hwui/GpuMemoryTracker.h @@ -52,7 +52,7 @@ public: static void onFrameCompleted(); protected: - GpuMemoryTracker(GpuObjectType type) : mType(type) { + explicit GpuMemoryTracker(GpuObjectType type) : mType(type) { ASSERT_GPU_THREAD(); startTrackingObject(); } diff --git a/libs/hwui/GradientCache.h b/libs/hwui/GradientCache.h index dccd45072522..49be19ab1c81 100644 --- a/libs/hwui/GradientCache.h +++ b/libs/hwui/GradientCache.h @@ -103,7 +103,7 @@ inline hash_t hash_type(const GradientCacheEntry& entry) { */ class GradientCache: public OnEntryRemoved<GradientCacheEntry, Texture*> { public: - GradientCache(Extensions& extensions); + explicit GradientCache(Extensions& extensions); ~GradientCache(); /** diff --git a/libs/hwui/Image.h b/libs/hwui/Image.h index 25145354a4f0..b8f5a5bec087 100644 --- a/libs/hwui/Image.h +++ b/libs/hwui/Image.h @@ -38,7 +38,7 @@ public: * cannot be created, getTexture() will return 0 and getImage() will * return EGL_NO_IMAGE_KHR. */ - Image(sp<GraphicBuffer> buffer); + explicit Image(sp<GraphicBuffer> buffer); ~Image(); /** diff --git a/libs/hwui/Interpolator.h b/libs/hwui/Interpolator.h index 66ce1197f060..65120087ff60 100644 --- a/libs/hwui/Interpolator.h +++ b/libs/hwui/Interpolator.h @@ -43,7 +43,7 @@ public: class ANDROID_API AccelerateInterpolator : public Interpolator { public: - AccelerateInterpolator(float factor) : mFactor(factor), mDoubleFactor(factor*2) {} + explicit AccelerateInterpolator(float factor) : mFactor(factor), mDoubleFactor(factor*2) {} virtual float interpolate(float input) override; private: const float mFactor; @@ -52,7 +52,7 @@ private: class ANDROID_API AnticipateInterpolator : public Interpolator { public: - AnticipateInterpolator(float tension) : mTension(tension) {} + explicit AnticipateInterpolator(float tension) : mTension(tension) {} virtual float interpolate(float input) override; private: const float mTension; @@ -60,7 +60,7 @@ private: class ANDROID_API AnticipateOvershootInterpolator : public Interpolator { public: - AnticipateOvershootInterpolator(float tension) : mTension(tension) {} + explicit AnticipateOvershootInterpolator(float tension) : mTension(tension) {} virtual float interpolate(float input) override; private: const float mTension; @@ -73,7 +73,7 @@ public: class ANDROID_API CycleInterpolator : public Interpolator { public: - CycleInterpolator(float cycles) : mCycles(cycles) {} + explicit CycleInterpolator(float cycles) : mCycles(cycles) {} virtual float interpolate(float input) override; private: const float mCycles; @@ -81,7 +81,7 @@ private: class ANDROID_API DecelerateInterpolator : public Interpolator { public: - DecelerateInterpolator(float factor) : mFactor(factor) {} + explicit DecelerateInterpolator(float factor) : mFactor(factor) {} virtual float interpolate(float input) override; private: const float mFactor; @@ -94,7 +94,7 @@ public: class ANDROID_API OvershootInterpolator : public Interpolator { public: - OvershootInterpolator(float tension) : mTension(tension) {} + explicit OvershootInterpolator(float tension) : mTension(tension) {} virtual float interpolate(float input) override; private: const float mTension; diff --git a/libs/hwui/JankTracker.h b/libs/hwui/JankTracker.h index 126025c00933..8b482d5a804d 100644 --- a/libs/hwui/JankTracker.h +++ b/libs/hwui/JankTracker.h @@ -57,7 +57,7 @@ struct ProfileData { // TODO: Replace DrawProfiler with this class JankTracker { public: - JankTracker(const DisplayInfo& displayInfo); + explicit JankTracker(const DisplayInfo& displayInfo); ~JankTracker(); void addFrame(const FrameInfo& frame); diff --git a/libs/hwui/LayerCache.h b/libs/hwui/LayerCache.h index 6fe7b3aae859..7a1a9ae3dd5d 100644 --- a/libs/hwui/LayerCache.h +++ b/libs/hwui/LayerCache.h @@ -105,7 +105,7 @@ private: mHeight = Layer::computeIdealHeight(layerHeight); } - LayerEntry(Layer* layer): + explicit LayerEntry(Layer* layer): mLayer(layer), mWidth(layer->getWidth()), mHeight(layer->getHeight()) { } diff --git a/libs/hwui/Matrix.cpp b/libs/hwui/Matrix.cpp index 709156c4098b..a936661f86f9 100644 --- a/libs/hwui/Matrix.cpp +++ b/libs/hwui/Matrix.cpp @@ -419,7 +419,7 @@ void Matrix4::mapPoint3d(Vector3& vec) const { vec.z = orig.x * data[2] + orig.y * data[6] + orig.z * data[kScaleZ] + data[kTranslateZ]; } -#define MUL_ADD_STORE(a, b, c) a = (a) * (b) + (c) +#define MUL_ADD_STORE(a, b, c) ((a) = (a) * (b) + (c)) void Matrix4::mapPoint(float& x, float& y) const { if (isSimple()) { diff --git a/libs/hwui/Matrix.h b/libs/hwui/Matrix.h index 9cde5d6aa04e..ba9cbbe02ec1 100644 --- a/libs/hwui/Matrix.h +++ b/libs/hwui/Matrix.h @@ -81,11 +81,11 @@ public: loadIdentity(); } - Matrix4(const float* v) { + explicit Matrix4(const float* v) { load(v); } - Matrix4(const SkMatrix& v) { + Matrix4(const SkMatrix& v) { // NOLINT, implicit load(v); } diff --git a/libs/hwui/OpenGLRenderer.cpp b/libs/hwui/OpenGLRenderer.cpp index b68240ac7519..9d821f38fab6 100644 --- a/libs/hwui/OpenGLRenderer.cpp +++ b/libs/hwui/OpenGLRenderer.cpp @@ -838,7 +838,7 @@ void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect) { */ #define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \ DRAW_COMMAND; \ - if (CC_UNLIKELY(Properties::debugOverdraw && getTargetFbo() == 0 && COND)) { \ + if (CC_UNLIKELY(Properties::debugOverdraw && getTargetFbo() == 0 && (COND))) { \ glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \ DRAW_COMMAND; \ glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \ diff --git a/libs/hwui/OpenGLRenderer.h b/libs/hwui/OpenGLRenderer.h index dacd8ccaa6ea..ec450bd63296 100755..100644 --- a/libs/hwui/OpenGLRenderer.h +++ b/libs/hwui/OpenGLRenderer.h @@ -116,7 +116,7 @@ enum ModelViewMode { */ class OpenGLRenderer : public CanvasStateClient { public: - OpenGLRenderer(RenderState& renderState); + explicit OpenGLRenderer(RenderState& renderState); virtual ~OpenGLRenderer(); void initProperties(); diff --git a/libs/hwui/PatchCache.h b/libs/hwui/PatchCache.h index 66ef6a0279ba..bc5981d53457 100644 --- a/libs/hwui/PatchCache.h +++ b/libs/hwui/PatchCache.h @@ -51,7 +51,7 @@ class Caches; class PatchCache { public: - PatchCache(RenderState& renderState); + explicit PatchCache(RenderState& renderState); ~PatchCache(); void init(); diff --git a/libs/hwui/PathCache.cpp b/libs/hwui/PathCache.cpp index a8ace8c10edd..972ff81ad31f 100644 --- a/libs/hwui/PathCache.cpp +++ b/libs/hwui/PathCache.cpp @@ -182,7 +182,8 @@ static void drawPath(const SkPath *path, const SkPaint* paint, SkBitmap& bitmap, PathCache::PathCache() : mCache(LruCache<PathDescription, PathTexture*>::kUnlimitedCapacity) , mSize(0) - , mMaxSize(Properties::pathCacheSize) { + , mMaxSize(Properties::pathCacheSize) + , mTexNum(0) { mCache.setOnEntryRemovedListener(this); GLint maxTextureSize; @@ -238,6 +239,7 @@ void PathCache::removeTexture(PathTexture* texture) { "the cache in an inconsistent state", size); } mSize -= size; + mTexNum--; } PATH_LOGD("PathCache::delete name, size, mSize = %d, %d, %d", @@ -262,7 +264,7 @@ void PathCache::purgeCache(uint32_t width, uint32_t height) { } void PathCache::trim() { - while (mSize > mMaxSize) { + while (mSize > mMaxSize || mTexNum > DEFAULT_PATH_TEXTURE_CAP) { mCache.removeOldest(); } } @@ -316,6 +318,7 @@ void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) { ATRACE_NAME("Upload Path Texture"); texture->upload(bitmap); texture->setFilter(GL_LINEAR); + mTexNum++; } /////////////////////////////////////////////////////////////////////////////// diff --git a/libs/hwui/PathCache.h b/libs/hwui/PathCache.h index 6368ddd49966..e925848a25b8 100644 --- a/libs/hwui/PathCache.h +++ b/libs/hwui/PathCache.h @@ -288,7 +288,7 @@ private: class PathProcessor: public TaskProcessor<SkBitmap*> { public: - PathProcessor(Caches& caches); + explicit PathProcessor(Caches& caches); ~PathProcessor() { } virtual void onProcess(const sp<Task<SkBitmap*> >& task) override; @@ -304,6 +304,12 @@ private: bool mDebugEnabled; + /** + * Driver allocated 4k/8k/16k memory for small path cache, + * limit the number of PathTexture in case occupy too much memory in hardware. + */ + uint32_t mTexNum; + sp<PathProcessor> mProcessor; std::vector<uint32_t> mGarbage; diff --git a/libs/hwui/ProgramCache.h b/libs/hwui/ProgramCache.h index 1dadda1a4c83..9ac885b665e7 100644 --- a/libs/hwui/ProgramCache.h +++ b/libs/hwui/ProgramCache.h @@ -40,7 +40,7 @@ namespace uirenderer { */ class ProgramCache { public: - ProgramCache(Extensions& extensions); + explicit ProgramCache(Extensions& extensions); ~ProgramCache(); Program* get(const ProgramDescription& description); diff --git a/libs/hwui/Properties.h b/libs/hwui/Properties.h index 171873de1f9a..881bd5f57714 100644 --- a/libs/hwui/Properties.h +++ b/libs/hwui/Properties.h @@ -149,7 +149,7 @@ enum DebugLevel { * This will disable the use of EGL_EXT_buffer_age and BUFFER_PRESERVED. * Default is "true" */ -#define PROPERTY_ENABLE_PARTIAL_UPDATES "debug.hwui.enable_partial_updates" +#define PROPERTY_ENABLE_PARTIAL_UPDATES "debug.hwui.use_partial_updates" #define PROPERTY_FILTER_TEST_OVERHEAD "debug.hwui.filter_test_overhead" @@ -219,14 +219,17 @@ enum DebugLevel { #define DEFAULT_TEXT_GAMMA 1.4f +// cap to 256 to limite paths in the path cache +#define DEFAULT_PATH_TEXTURE_CAP 256 + /////////////////////////////////////////////////////////////////////////////// // Misc /////////////////////////////////////////////////////////////////////////////// // Converts a number of mega-bytes into bytes -#define MB(s) s * 1024 * 1024 +#define MB(s) ((s) * 1024 * 1024) // Converts a number of kilo-bytes into bytes -#define KB(s) s * 1024 +#define KB(s) ((s) * 1024) enum class ProfileType { None, diff --git a/libs/hwui/PropertyValuesAnimatorSet.h b/libs/hwui/PropertyValuesAnimatorSet.h index e208b08cc27a..a5d9e869196f 100644 --- a/libs/hwui/PropertyValuesAnimatorSet.h +++ b/libs/hwui/PropertyValuesAnimatorSet.h @@ -89,7 +89,7 @@ private: class PropertyAnimatorSetListener : public AnimationListener { public: - PropertyAnimatorSetListener(PropertyValuesAnimatorSet* set) : mSet(set) {} + explicit PropertyAnimatorSetListener(PropertyValuesAnimatorSet* set) : mSet(set) {} virtual void onAnimationFinished(BaseRenderNodeAnimator* animator) override; private: diff --git a/libs/hwui/RecordedOp.h b/libs/hwui/RecordedOp.h index aee9d6370083..5497f863b357 100644 --- a/libs/hwui/RecordedOp.h +++ b/libs/hwui/RecordedOp.h @@ -509,7 +509,7 @@ struct LayerOp : RecordedOp { , mode(PaintUtils::getXfermodeDirect(paint)) , colorFilter(paint ? paint->getColorFilter() : nullptr) {} - LayerOp(RenderNode& node) + explicit LayerOp(RenderNode& node) : RecordedOp(RecordedOpId::LayerOp, Rect(node.getWidth(), node.getHeight()), Matrix4::identity(), nullptr, nullptr) , layerHandle(node.getLayerHandle()) , alpha(node.properties().layerProperties().alpha() / 255.0f) diff --git a/libs/hwui/Rect.h b/libs/hwui/Rect.h index 5786668addc5..eb05e9171335 100644 --- a/libs/hwui/Rect.h +++ b/libs/hwui/Rect.h @@ -73,14 +73,14 @@ public: bottom(height) { } - inline Rect(const SkIRect& rect): + inline Rect(const SkIRect& rect): // NOLINT, implicit left(rect.fLeft), top(rect.fTop), right(rect.fRight), bottom(rect.fBottom) { } - inline Rect(const SkRect& rect): + inline Rect(const SkRect& rect): // NOLINT, implicit left(rect.fLeft), top(rect.fTop), right(rect.fRight), diff --git a/libs/hwui/RenderBufferCache.h b/libs/hwui/RenderBufferCache.h index f77f4c95b5ba..644433145cd0 100644 --- a/libs/hwui/RenderBufferCache.h +++ b/libs/hwui/RenderBufferCache.h @@ -82,7 +82,7 @@ private: mBuffer(nullptr), mFormat(format), mWidth(width), mHeight(height) { } - RenderBufferEntry(RenderBuffer* buffer): + explicit RenderBufferEntry(RenderBuffer* buffer): mBuffer(buffer), mFormat(buffer->getFormat()), mWidth(buffer->getWidth()), mHeight(buffer->getHeight()) { } diff --git a/libs/hwui/RenderNode.h b/libs/hwui/RenderNode.h index 47fef6da355c..f9735a231d7a 100644 --- a/libs/hwui/RenderNode.h +++ b/libs/hwui/RenderNode.h @@ -146,7 +146,7 @@ public: void setName(const char* name) { if (name) { - char* lastPeriod = strrchr(name, '.'); + const char* lastPeriod = strrchr(name, '.'); if (lastPeriod) { mName.setTo(lastPeriod + 1); } else { diff --git a/libs/hwui/RenderProperties.h b/libs/hwui/RenderProperties.h index 395279806adb..6a6e8dbb3fcd 100644 --- a/libs/hwui/RenderProperties.h +++ b/libs/hwui/RenderProperties.h @@ -47,7 +47,7 @@ class RenderNode; class RenderProperties; // The __VA_ARGS__ will be executed if a & b are not equal -#define RP_SET(a, b, ...) (a != b ? (a = b, ##__VA_ARGS__, true) : false) +#define RP_SET(a, b, ...) ((a) != (b) ? ((a) = (b), ##__VA_ARGS__, true) : false) #define RP_SET_AND_DIRTY(a, b) RP_SET(a, b, mPrimitiveFields.mMatrixOrPivotDirty = true) // Keep in sync with View.java:LAYER_TYPE_* diff --git a/libs/hwui/ResourceCache.h b/libs/hwui/ResourceCache.h index 4583c8de87e8..3ac78642c7ee 100644 --- a/libs/hwui/ResourceCache.h +++ b/libs/hwui/ResourceCache.h @@ -42,7 +42,7 @@ enum ResourceType { class ResourceReference { public: - ResourceReference(ResourceType type) { + explicit ResourceReference(ResourceType type) { refCount = 0; destroyed = false; resourceType = type; } diff --git a/libs/hwui/SkiaCanvas.cpp b/libs/hwui/SkiaCanvas.cpp index c7289fce1fbf..3b6fae08773d 100644 --- a/libs/hwui/SkiaCanvas.cpp +++ b/libs/hwui/SkiaCanvas.cpp @@ -208,7 +208,7 @@ SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) { class ClipCopier : public SkCanvas::ClipVisitor { public: - ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {} + explicit ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {} virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) { m_dstCanvas->clipRect(rect, op, antialias); diff --git a/libs/hwui/SkiaCanvasProxy.h b/libs/hwui/SkiaCanvasProxy.h index 973c55fe2236..c03c523cd106 100644 --- a/libs/hwui/SkiaCanvasProxy.h +++ b/libs/hwui/SkiaCanvasProxy.h @@ -39,7 +39,7 @@ namespace uirenderer { */ class ANDROID_API SkiaCanvasProxy : public SkCanvas { public: - SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls = false); + explicit SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls = false); virtual ~SkiaCanvasProxy() {} protected: diff --git a/libs/hwui/Snapshot.cpp b/libs/hwui/Snapshot.cpp index 2c9c9d90f686..7c187fbda457 100644 --- a/libs/hwui/Snapshot.cpp +++ b/libs/hwui/Snapshot.cpp @@ -38,6 +38,7 @@ Snapshot::Snapshot() , mClipArea(&mClipAreaRoot) { transform = &mTransformRoot; region = nullptr; + mRelativeLightCenter.x = mRelativeLightCenter.y = mRelativeLightCenter.z = 0; } /** diff --git a/libs/hwui/TessellationCache.cpp b/libs/hwui/TessellationCache.cpp index cfdc0848c8b0..d9e811684610 100644 --- a/libs/hwui/TessellationCache.cpp +++ b/libs/hwui/TessellationCache.cpp @@ -136,7 +136,7 @@ public: class TessellationCache::TessellationProcessor : public TaskProcessor<VertexBuffer*> { public: - TessellationProcessor(Caches& caches) + explicit TessellationProcessor(Caches& caches) : TaskProcessor<VertexBuffer*>(&caches.tasks) {} ~TessellationProcessor() {} @@ -150,7 +150,7 @@ public: class TessellationCache::Buffer { public: - Buffer(const sp<Task<VertexBuffer*> >& task) + explicit Buffer(const sp<Task<VertexBuffer*> >& task) : mTask(task) , mBuffer(nullptr) { } @@ -270,7 +270,7 @@ void tessellateShadows( class ShadowProcessor : public TaskProcessor<TessellationCache::vertexBuffer_pair_t> { public: - ShadowProcessor(Caches& caches) + explicit ShadowProcessor(Caches& caches) : TaskProcessor<TessellationCache::vertexBuffer_pair_t>(&caches.tasks) {} ~ShadowProcessor() {} diff --git a/libs/hwui/TextDropShadowCache.h b/libs/hwui/TextDropShadowCache.h index d536c40756ff..13e87749029a 100644 --- a/libs/hwui/TextDropShadowCache.h +++ b/libs/hwui/TextDropShadowCache.h @@ -111,7 +111,7 @@ inline hash_t hash_type(const ShadowText& entry) { * Alpha texture used to represent a shadow. */ struct ShadowTexture: public Texture { - ShadowTexture(Caches& caches): Texture(caches) { + explicit ShadowTexture(Caches& caches): Texture(caches) { } float left; @@ -121,7 +121,7 @@ struct ShadowTexture: public Texture { class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> { public: TextDropShadowCache(); - TextDropShadowCache(uint32_t maxByteSize); + explicit TextDropShadowCache(uint32_t maxByteSize); ~TextDropShadowCache(); /** diff --git a/libs/hwui/Texture.h b/libs/hwui/Texture.h index 9749f734fd1f..b72742f45654 100644 --- a/libs/hwui/Texture.h +++ b/libs/hwui/Texture.h @@ -34,7 +34,7 @@ class Layer; */ class Texture : public GpuMemoryTracker { public: - Texture(Caches& caches) + explicit Texture(Caches& caches) : GpuMemoryTracker(GpuObjectType::Texture) , mCaches(caches) { } @@ -171,7 +171,7 @@ private: class AutoTexture { public: - AutoTexture(Texture* texture) + explicit AutoTexture(Texture* texture) : texture(texture) {} ~AutoTexture() { if (texture && texture->cleanup) { diff --git a/libs/hwui/VectorDrawable.h b/libs/hwui/VectorDrawable.h index 54cd06504920..b867dbc59714 100644 --- a/libs/hwui/VectorDrawable.h +++ b/libs/hwui/VectorDrawable.h @@ -90,7 +90,7 @@ class ANDROID_API Node { public: class Properties { public: - Properties(Node* node) : mNode(node) {} + explicit Properties(Node* node) : mNode(node) {} inline void onPropertyChanged() { mNode->onPropertyChanged(this); } @@ -132,7 +132,7 @@ public: class PathProperties : public Properties { public: - PathProperties(Node* node) : Properties(node) {} + explicit PathProperties(Node* node) : Properties(node) {} void syncProperties(const PathProperties& prop) { mData = prop.mData; onPropertyChanged(); @@ -218,7 +218,7 @@ public: float strokeMiterLimit = 4; int fillType = 0; /* non-zero or kWinding_FillType in Skia */ }; - FullPathProperties(Node* mNode) : Properties(mNode), mTrimDirty(false) {} + explicit FullPathProperties(Node* mNode) : Properties(mNode), mTrimDirty(false) {} ~FullPathProperties() { SkSafeUnref(fillGradient); SkSafeUnref(strokeGradient); @@ -409,7 +409,7 @@ class ANDROID_API Group: public Node { public: class GroupProperties : public Properties { public: - GroupProperties(Node* mNode) : Properties(mNode) {} + explicit GroupProperties(Node* mNode) : Properties(mNode) {} struct PrimitiveFields { float rotate = 0; float pivotX = 0; @@ -539,7 +539,7 @@ private: class ANDROID_API Tree : public VirtualLightRefBase { public: - Tree(Group* rootNode) : mRootNode(rootNode) { + explicit Tree(Group* rootNode) : mRootNode(rootNode) { mRootNode->setPropertyChangedListener(&mPropertyChangedListener); } @@ -576,7 +576,7 @@ public: class TreeProperties { public: - TreeProperties(Tree* tree) : mTree(tree) {} + explicit TreeProperties(Tree* tree) : mTree(tree) {} // Properties that can only be modified by UI thread, therefore sync should // only go from UI to RT struct NonAnimatableProperties { diff --git a/libs/hwui/font/CacheTexture.cpp b/libs/hwui/font/CacheTexture.cpp index 4b13814bfdc6..49e9f65582ae 100644 --- a/libs/hwui/font/CacheTexture.cpp +++ b/libs/hwui/font/CacheTexture.cpp @@ -188,15 +188,21 @@ void CacheTexture::allocatePixelBuffer() { bool CacheTexture::upload() { const Rect& dirtyRect = mDirtyRect; - uint32_t x = mHasUnpackRowLength ? dirtyRect.left : 0; - uint32_t y = dirtyRect.top; - uint32_t width = mHasUnpackRowLength ? dirtyRect.getWidth() : getWidth(); - uint32_t height = dirtyRect.getHeight(); + // align the x direction to 32 and y direction to 4 for better performance + uint32_t x = (((uint32_t)dirtyRect.left) & (~0x1F)); + uint32_t y = (((uint32_t)dirtyRect.top) & (~0x3)); + uint32_t r = ((((uint32_t)dirtyRect.right) + 0x1F) & (~0x1F)) - x; + uint32_t b = ((((uint32_t)dirtyRect.bottom) + 0x3) & (~0x3)) - y; + uint32_t width = (r > getWidth() ? getWidth() : r); + uint32_t height = (b > getHeight() ? getHeight() : b); // The unpack row length only needs to be specified when a new // texture is bound if (mHasUnpackRowLength) { glPixelStorei(GL_UNPACK_ROW_LENGTH, getWidth()); + } else { + x = 0; + width = getWidth(); } mPixelBuffer->upload(x, y, width, height); diff --git a/libs/hwui/hwui/Paint.h b/libs/hwui/hwui/Paint.h index 9599c30c639b..f72a6321643a 100644 --- a/libs/hwui/hwui/Paint.h +++ b/libs/hwui/hwui/Paint.h @@ -30,7 +30,7 @@ class ANDROID_API Paint : public SkPaint { public: Paint(); Paint(const Paint& paint); - Paint(const SkPaint& paint); + Paint(const SkPaint& paint); // NOLINT(implicit) ~Paint(); Paint& operator=(const Paint& other); diff --git a/libs/hwui/hwui/Typeface.cpp b/libs/hwui/hwui/Typeface.cpp index c583988554c7..0b46c09936c3 100644 --- a/libs/hwui/hwui/Typeface.cpp +++ b/libs/hwui/hwui/Typeface.cpp @@ -22,6 +22,8 @@ #include "Typeface.h" +#include <pthread.h> + #include "MinikinSkia.h" #include "SkTypeface.h" #include "SkPaint.h" diff --git a/libs/hwui/hwui_static_deps.mk b/libs/hwui/hwui_static_deps.mk index 299095217a8d..dca78b38e942 100644 --- a/libs/hwui/hwui_static_deps.mk +++ b/libs/hwui/hwui_static_deps.mk @@ -24,7 +24,8 @@ LOCAL_SHARED_LIBRARIES += \ libprotobuf-cpp-lite \ libharfbuzz_ng \ libft2 \ - libminikin + libminikin \ + libandroidfw ifneq (false,$(ANDROID_ENABLE_RENDERSCRIPT)) LOCAL_SHARED_LIBRARIES += libRS libRScpp diff --git a/libs/hwui/private/README b/libs/hwui/private/README new file mode 100644 index 000000000000..ee4149270ae2 --- /dev/null +++ b/libs/hwui/private/README @@ -0,0 +1,4 @@ +This folder contains private include files. + +These include files are part of the private implementation details of +various framework components. Use at your peril. diff --git a/libs/hwui/private/hwui/DrawGlInfo.h b/libs/hwui/private/hwui/DrawGlInfo.h new file mode 100644 index 000000000000..f57851322716 --- /dev/null +++ b/libs/hwui/private/hwui/DrawGlInfo.h @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_HWUI_DRAW_GL_INFO_H +#define ANDROID_HWUI_DRAW_GL_INFO_H + +namespace android { +namespace uirenderer { + +/** + * Structure used by OpenGLRenderer::callDrawGLFunction() to pass and + * receive data from OpenGL functors. + */ +struct DrawGlInfo { + // Input: current clip rect + int clipLeft; + int clipTop; + int clipRight; + int clipBottom; + + // Input: current width/height of destination surface + int width; + int height; + + // Input: is the render target an FBO + bool isLayer; + + // Input: current transform matrix, in OpenGL format + float transform[16]; + + // Output: dirty region to redraw + float dirtyLeft; + float dirtyTop; + float dirtyRight; + float dirtyBottom; + + /** + * Values used as the "what" parameter of the functor. + */ + enum Mode { + // Indicates that the functor is called to perform a draw + kModeDraw, + // Indicates the the functor is called only to perform + // processing and that no draw should be attempted + kModeProcess, + // Same as kModeProcess, however there is no GL context because it was + // lost or destroyed + kModeProcessNoContext, + // Invoked every time the UI thread pushes over a frame to the render thread + // *and the owning view has a dirty display list*. This is a signal to sync + // any data that needs to be shared between the UI thread and the render thread. + // During this time the UI thread is blocked. + kModeSync + }; + + /** + * Values used by OpenGL functors to tell the framework + * what to do next. + */ + enum Status { + // The functor is done + kStatusDone = 0x0, + // DisplayList actually issued GL drawing commands. + // This is used to signal the HardwareRenderer that the + // buffers should be flipped - otherwise, there were no + // changes to the buffer, so no need to flip. Some hardware + // has issues with stale buffer contents when no GL + // commands are issued. + kStatusDrew = 0x4 + }; +}; // struct DrawGlInfo + +}; // namespace uirenderer +}; // namespace android + +#endif // ANDROID_HWUI_DRAW_GL_INFO_H diff --git a/libs/hwui/renderstate/OffscreenBufferPool.h b/libs/hwui/renderstate/OffscreenBufferPool.h index 73a3392a7811..26d4e3654a48 100644 --- a/libs/hwui/renderstate/OffscreenBufferPool.h +++ b/libs/hwui/renderstate/OffscreenBufferPool.h @@ -126,7 +126,7 @@ private: : width(OffscreenBuffer::computeIdealDimension(layerWidth)) , height(OffscreenBuffer::computeIdealDimension(layerHeight)) {} - Entry(OffscreenBuffer* layer) + explicit Entry(OffscreenBuffer* layer) : layer(layer) , width(layer->texture.width()) , height(layer->texture.height()) { diff --git a/libs/hwui/renderstate/RenderState.cpp b/libs/hwui/renderstate/RenderState.cpp index e78cd7296f42..5e600644ca19 100644 --- a/libs/hwui/renderstate/RenderState.cpp +++ b/libs/hwui/renderstate/RenderState.cpp @@ -211,7 +211,7 @@ void RenderState::debugOverdraw(bool enable, bool clear) { class DecStrongTask : public renderthread::RenderTask { public: - DecStrongTask(VirtualLightRefBase* object) : mObject(object) {} + explicit DecStrongTask(VirtualLightRefBase* object) : mObject(object) {} virtual void run() override { mObject->decStrong(nullptr); diff --git a/libs/hwui/renderstate/RenderState.h b/libs/hwui/renderstate/RenderState.h index 731d9bbb3355..9e0fb121be65 100644 --- a/libs/hwui/renderstate/RenderState.h +++ b/libs/hwui/renderstate/RenderState.h @@ -106,7 +106,7 @@ private: void interruptForFunctorInvoke(); void resumeFromFunctorInvoke(); - RenderState(renderthread::RenderThread& thread); + explicit RenderState(renderthread::RenderThread& thread); ~RenderState(); diff --git a/libs/hwui/renderthread/EglManager.h b/libs/hwui/renderthread/EglManager.h index 459baed70e40..41047fecf960 100644 --- a/libs/hwui/renderthread/EglManager.h +++ b/libs/hwui/renderthread/EglManager.h @@ -86,7 +86,7 @@ public: private: friend class RenderThread; - EglManager(RenderThread& thread); + explicit EglManager(RenderThread& thread); // EglContext is never destroyed, method is purposely not implemented ~EglManager(); diff --git a/libs/hwui/renderthread/RenderTask.h b/libs/hwui/renderthread/RenderTask.h index 89c3a7d08a62..9ea671be5b86 100644 --- a/libs/hwui/renderthread/RenderTask.h +++ b/libs/hwui/renderthread/RenderTask.h @@ -73,7 +73,7 @@ typedef void* (*RunnableMethod)(void* data); class MethodInvokeRenderTask : public RenderTask { public: - MethodInvokeRenderTask(RunnableMethod method) + explicit MethodInvokeRenderTask(RunnableMethod method) : mMethod(method), mReturnPtr(nullptr) {} void* payload() { return mData; } diff --git a/libs/hwui/renderthread/RenderThread.cpp b/libs/hwui/renderthread/RenderThread.cpp index f4b44164b84e..9c10c4fdbad1 100644 --- a/libs/hwui/renderthread/RenderThread.cpp +++ b/libs/hwui/renderthread/RenderThread.cpp @@ -20,6 +20,7 @@ #include "CanvasContext.h" #include "EglManager.h" #include "RenderProxy.h" +#include "utils/FatVector.h" #include <gui/DisplayEventReceiver.h> #include <gui/ISurfaceComposer.h> @@ -128,7 +129,7 @@ class DispatchFrameCallbacks : public RenderTask { private: RenderThread* mRenderThread; public: - DispatchFrameCallbacks(RenderThread* rt) : mRenderThread(rt) {} + explicit DispatchFrameCallbacks(RenderThread* rt) : mRenderThread(rt) {} virtual void run() override { mRenderThread->dispatchFrameCallbacks(); @@ -282,10 +283,18 @@ bool RenderThread::threadLoop() { "RenderThread Looper POLL_ERROR!"); nsecs_t nextWakeup; - // Process our queue, if we have anything - while (RenderTask* task = nextTask(&nextWakeup)) { - task->run(); - // task may have deleted itself, do not reference it again + { + FatVector<RenderTask*, 10> workQueue; + // Process our queue, if we have anything. By first acquiring + // all the pending events then processing them we avoid vsync + // starvation if more tasks are queued while we are processing tasks. + while (RenderTask* task = nextTask(&nextWakeup)) { + workQueue.push_back(task); + } + for (auto task : workQueue) { + task->run(); + // task may have deleted itself, do not reference it again + } } if (nextWakeup == LLONG_MAX) { timeoutMillis = -1; diff --git a/libs/hwui/tests/common/TestScene.h b/libs/hwui/tests/common/TestScene.h index 706f2ff75222..060984ea9f32 100644 --- a/libs/hwui/tests/common/TestScene.h +++ b/libs/hwui/tests/common/TestScene.h @@ -55,7 +55,7 @@ public: class Registrar { public: - Registrar(const TestScene::Info& info) { + explicit Registrar(const TestScene::Info& info) { TestScene::registerScene(info); } private: diff --git a/libs/hwui/tests/common/TestUtils.h b/libs/hwui/tests/common/TestUtils.h index 4536bef6d391..5a4ab99012b9 100644 --- a/libs/hwui/tests/common/TestUtils.h +++ b/libs/hwui/tests/common/TestUtils.h @@ -48,10 +48,10 @@ typedef DisplayListCanvas TestCanvas; EXPECT_TRUE(TestUtils::matricesAreApproxEqual(a, b)) #define EXPECT_RECT_APPROX_EQ(a, b) \ - EXPECT_TRUE(MathUtils::areEqual(a.left, b.left) \ - && MathUtils::areEqual(a.top, b.top) \ - && MathUtils::areEqual(a.right, b.right) \ - && MathUtils::areEqual(a.bottom, b.bottom)); + EXPECT_TRUE(MathUtils::areEqual((a).left, (b).left) \ + && MathUtils::areEqual((a).top, (b).top) \ + && MathUtils::areEqual((a).right, (b).right) \ + && MathUtils::areEqual((a).bottom, (b).bottom)); #define EXPECT_CLIP_RECT(expRect, clipStatePtr) \ EXPECT_NE(nullptr, (clipStatePtr)) << "Op is unclipped"; \ @@ -102,7 +102,7 @@ public: public: SignalingDtor() : mSignal(nullptr) {} - SignalingDtor(int* signal) + explicit SignalingDtor(int* signal) : mSignal(signal) {} void setSignal(int* signal) { mSignal = signal; @@ -202,7 +202,7 @@ public: class TestTask : public renderthread::RenderTask { public: - TestTask(RtCallback rtCallback) + explicit TestTask(RtCallback rtCallback) : rtCallback(rtCallback) {} virtual ~TestTask() {} virtual void run() override; diff --git a/libs/hwui/tests/microbench/FrameBuilderBench.cpp b/libs/hwui/tests/microbench/FrameBuilderBench.cpp index 84ef9c2575f5..362890b52b61 100644 --- a/libs/hwui/tests/microbench/FrameBuilderBench.cpp +++ b/libs/hwui/tests/microbench/FrameBuilderBench.cpp @@ -113,7 +113,7 @@ static auto SCENES = { void BM_FrameBuilder_defer_scene(benchmark::State& state) { TestUtils::runOnRenderThread([&state](RenderThread& thread) { - const char* sceneName = *(SCENES.begin() + state.range_x()); + const char* sceneName = *(SCENES.begin() + state.range(0)); state.SetLabel(sceneName); auto node = getSyncedSceneNode(sceneName); while (state.KeepRunning()) { @@ -129,7 +129,7 @@ BENCHMARK(BM_FrameBuilder_defer_scene)->DenseRange(0, SCENES.size() - 1); void BM_FrameBuilder_deferAndRender_scene(benchmark::State& state) { TestUtils::runOnRenderThread([&state](RenderThread& thread) { - const char* sceneName = *(SCENES.begin() + state.range_x()); + const char* sceneName = *(SCENES.begin() + state.range(0)); state.SetLabel(sceneName); auto node = getSyncedSceneNode(sceneName); diff --git a/libs/hwui/thread/Barrier.h b/libs/hwui/thread/Barrier.h index 0a7acb0fbbfd..17f82ba9d67d 100644 --- a/libs/hwui/thread/Barrier.h +++ b/libs/hwui/thread/Barrier.h @@ -24,7 +24,7 @@ namespace uirenderer { class Barrier { public: - Barrier(Condition::WakeUpType type = Condition::WAKE_UP_ALL) : mType(type), mOpened(false) { } + explicit Barrier(Condition::WakeUpType type = Condition::WAKE_UP_ALL) : mType(type), mOpened(false) { } ~Barrier() { } void open() { diff --git a/libs/hwui/thread/Future.h b/libs/hwui/thread/Future.h index a3ff3bcc3698..177eebd9b85f 100644 --- a/libs/hwui/thread/Future.h +++ b/libs/hwui/thread/Future.h @@ -27,7 +27,7 @@ namespace uirenderer { template<typename T> class Future: public LightRefBase<Future<T> > { public: - Future(Condition::WakeUpType type = Condition::WAKE_UP_ONE): mBarrier(type), mResult() { } + explicit Future(Condition::WakeUpType type = Condition::WAKE_UP_ONE): mBarrier(type), mResult() { } ~Future() { } /** diff --git a/libs/hwui/thread/Signal.h b/libs/hwui/thread/Signal.h index d4cfeeb29c17..93e6f4c27535 100644 --- a/libs/hwui/thread/Signal.h +++ b/libs/hwui/thread/Signal.h @@ -26,7 +26,7 @@ namespace uirenderer { class Signal { public: - Signal(Condition::WakeUpType type = Condition::WAKE_UP_ALL) : mType(type), mSignaled(false) { } + explicit Signal(Condition::WakeUpType type = Condition::WAKE_UP_ALL) : mType(type), mSignaled(false) { } ~Signal() { } void signal() { diff --git a/libs/hwui/thread/TaskManager.h b/libs/hwui/thread/TaskManager.h index e4808f7b7181..c9d12bc6044b 100644 --- a/libs/hwui/thread/TaskManager.h +++ b/libs/hwui/thread/TaskManager.h @@ -78,7 +78,7 @@ private: class WorkerThread: public Thread { public: - WorkerThread(const String8 name): mSignal(Condition::WAKE_UP_ONE), mName(name) { } + explicit WorkerThread(const String8& name): mSignal(Condition::WAKE_UP_ONE), mName(name) { } bool addTask(const TaskWrapper& task); size_t getTaskCount() const; diff --git a/libs/hwui/thread/TaskProcessor.h b/libs/hwui/thread/TaskProcessor.h index 82538e9c7342..58674505c457 100644 --- a/libs/hwui/thread/TaskProcessor.h +++ b/libs/hwui/thread/TaskProcessor.h @@ -36,7 +36,7 @@ public: template<typename T> class TaskProcessor: public TaskProcessorBase { public: - TaskProcessor(TaskManager* manager): mManager(manager) { } + explicit TaskProcessor(TaskManager* manager): mManager(manager) { } virtual ~TaskProcessor() { } void add(const sp<Task<T> >& task) { diff --git a/libs/hwui/utils/FatVector.h b/libs/hwui/utils/FatVector.h index 93d37c28f8a4..df8cb076e763 100644 --- a/libs/hwui/utils/FatVector.h +++ b/libs/hwui/utils/FatVector.h @@ -53,7 +53,7 @@ public: typedef T value_type; // needed to implement std::allocator typedef T* pointer; // needed to implement std::allocator - InlineStdAllocator(Allocation& allocation) + explicit InlineStdAllocator(Allocation& allocation) : mAllocation(allocation) {} InlineStdAllocator(const InlineStdAllocator& other) : mAllocation(other.mAllocation) {} @@ -93,7 +93,7 @@ public: this->reserve(SIZE); } - FatVector(size_t capacity) : FatVector() { + explicit FatVector(size_t capacity) : FatVector() { this->resize(capacity); } diff --git a/libs/hwui/utils/LinearAllocator.cpp b/libs/hwui/utils/LinearAllocator.cpp index 5bba420a258f..d92bc0cd1fca 100644 --- a/libs/hwui/utils/LinearAllocator.cpp +++ b/libs/hwui/utils/LinearAllocator.cpp @@ -48,8 +48,8 @@ #define ALIGN_SZ (sizeof(int)) #endif -#define ALIGN(x) ((x + ALIGN_SZ - 1 ) & ~(ALIGN_SZ - 1)) -#define ALIGN_PTR(p) ((void*)(ALIGN((size_t)p))) +#define ALIGN(x) (((x) + ALIGN_SZ - 1 ) & ~(ALIGN_SZ - 1)) +#define ALIGN_PTR(p) ((void*)(ALIGN((size_t)(p)))) #if LOG_NDEBUG #define ADD_ALLOCATION() diff --git a/libs/hwui/utils/LinearAllocator.h b/libs/hwui/utils/LinearAllocator.h index 34c8c6beea81..f95a6fe5060e 100644 --- a/libs/hwui/utils/LinearAllocator.h +++ b/libs/hwui/utils/LinearAllocator.h @@ -157,7 +157,7 @@ public: typedef T value_type; // needed to implement std::allocator typedef T* pointer; // needed to implement std::allocator - LinearStdAllocator(LinearAllocator& allocator) + explicit LinearStdAllocator(LinearAllocator& allocator) : linearAllocator(allocator) {} LinearStdAllocator(const LinearStdAllocator& other) : linearAllocator(other.linearAllocator) {} @@ -170,7 +170,7 @@ public: }; // enable allocators to be constructed from other templated types template <class U> - LinearStdAllocator(const LinearStdAllocator<U>& other) + LinearStdAllocator(const LinearStdAllocator<U>& other) // NOLINT(implicit) : linearAllocator(other.linearAllocator) {} T* allocate(size_t num, const void* = 0) { @@ -195,7 +195,7 @@ bool operator!= (const LinearStdAllocator<T1>&, const LinearStdAllocator<T2>&) { template <class T> class LsaVector : public std::vector<T, LinearStdAllocator<T>> { public: - LsaVector(const LinearStdAllocator<T>& allocator) + explicit LsaVector(const LinearStdAllocator<T>& allocator) : std::vector<T, LinearStdAllocator<T>>(allocator) {} }; diff --git a/libs/hwui/utils/Timing.h b/libs/hwui/utils/Timing.h index dd8847add2e8..4b1fabe42c35 100644 --- a/libs/hwui/utils/Timing.h +++ b/libs/hwui/utils/Timing.h @@ -22,7 +22,7 @@ #define TIME_METHOD() MethodTimer __method_timer(__func__) class MethodTimer { public: - MethodTimer(const char* name) + explicit MethodTimer(const char* name) : mMethodName(name) { gettimeofday(&mStart, nullptr); } diff --git a/libs/input/SpriteController.h b/libs/input/SpriteController.h index 905833c7877d..7fc8d6fd5197 100644 --- a/libs/input/SpriteController.h +++ b/libs/input/SpriteController.h @@ -214,7 +214,7 @@ private: virtual ~SpriteImpl(); public: - SpriteImpl(const sp<SpriteController> controller); + explicit SpriteImpl(const sp<SpriteController> controller); virtual void setIcon(const SpriteIcon& icon); virtual void setVisible(bool visible); diff --git a/libs/storage/Android.bp b/libs/storage/Android.bp new file mode 100644 index 000000000000..911bd1d25393 --- /dev/null +++ b/libs/storage/Android.bp @@ -0,0 +1,19 @@ +cc_library_static { + name: "libstorage", + + srcs: [ + "IMountServiceListener.cpp", + "IMountShutdownObserver.cpp", + "IObbActionListener.cpp", + "IMountService.cpp", + ], + + export_include_dirs: ["include"], + + cflags: [ + "-Wall", + "-Werror", + ], + + shared_libs: ["libbinder"], +} diff --git a/libs/storage/Android.mk b/libs/storage/Android.mk deleted file mode 100644 index fae2bf7c3457..000000000000 --- a/libs/storage/Android.mk +++ /dev/null @@ -1,14 +0,0 @@ -LOCAL_PATH:= $(call my-dir) -include $(CLEAR_VARS) - -LOCAL_SRC_FILES:= \ - IMountServiceListener.cpp \ - IMountShutdownObserver.cpp \ - IObbActionListener.cpp \ - IMountService.cpp - -LOCAL_MODULE:= libstorage - -LOCAL_CFLAGS += -Wall -Werror - -include $(BUILD_STATIC_LIBRARY) diff --git a/libs/storage/IMountService.cpp b/libs/storage/IMountService.cpp index c643ed008a3b..74638e7eccc3 100644 --- a/libs/storage/IMountService.cpp +++ b/libs/storage/IMountService.cpp @@ -55,7 +55,7 @@ enum { class BpMountService: public BpInterface<IMountService> { public: - BpMountService(const sp<IBinder>& impl) + explicit BpMountService(const sp<IBinder>& impl) : BpInterface<IMountService>(impl) { } diff --git a/libs/storage/IObbActionListener.cpp b/libs/storage/IObbActionListener.cpp index 9656e655e22c..a71341bc1364 100644 --- a/libs/storage/IObbActionListener.cpp +++ b/libs/storage/IObbActionListener.cpp @@ -26,7 +26,7 @@ enum { // This is a stub that real consumers should override. class BpObbActionListener: public BpInterface<IObbActionListener> { public: - BpObbActionListener(const sp<IBinder>& impl) + explicit BpObbActionListener(const sp<IBinder>& impl) : BpInterface<IObbActionListener>(impl) { } diff --git a/libs/storage/include/storage/IMountService.h b/libs/storage/include/storage/IMountService.h new file mode 100644 index 000000000000..c3d34d84958b --- /dev/null +++ b/libs/storage/include/storage/IMountService.h @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_IMOUNTSERVICE_H +#define ANDROID_IMOUNTSERVICE_H + +#include <storage/IMountServiceListener.h> +#include <storage/IMountShutdownObserver.h> +#include <storage/IObbActionListener.h> + +#include <utils/String8.h> + +#include <binder/IInterface.h> +#include <binder/Parcel.h> + +namespace android { + +class IMountService: public IInterface { +public: + DECLARE_META_INTERFACE(MountService); + + virtual void registerListener(const sp<IMountServiceListener>& listener) = 0; + virtual void + unregisterListener(const sp<IMountServiceListener>& listener) = 0; + virtual bool isUsbMassStorageConnected() = 0; + virtual void setUsbMassStorageEnabled(const bool enable) = 0; + virtual bool isUsbMassStorageEnabled() = 0; + virtual int32_t mountVolume(const String16& mountPoint) = 0; + virtual int32_t unmountVolume( + const String16& mountPoint, const bool force, const bool removeEncryption) = 0; + virtual int32_t formatVolume(const String16& mountPoint) = 0; + virtual int32_t + getStorageUsers(const String16& mountPoint, int32_t** users) = 0; + virtual int32_t getVolumeState(const String16& mountPoint) = 0; + virtual int32_t createSecureContainer(const String16& id, + const int32_t sizeMb, const String16& fstype, const String16& key, + const int32_t ownerUid) = 0; + virtual int32_t finalizeSecureContainer(const String16& id) = 0; + virtual int32_t destroySecureContainer(const String16& id) = 0; + virtual int32_t mountSecureContainer(const String16& id, + const String16& key, const int32_t ownerUid) = 0; + virtual int32_t + unmountSecureContainer(const String16& id, const bool force) = 0; + virtual bool isSecureContainerMounted(const String16& id) = 0; + virtual int32_t renameSecureContainer(const String16& oldId, + const String16& newId) = 0; + virtual bool getSecureContainerPath(const String16& id, String16& path) = 0; + virtual int32_t getSecureContainerList(const String16& id, + String16*& containers) = 0; + virtual void shutdown(const sp<IMountShutdownObserver>& observer) = 0; + virtual void finishMediaUpdate() = 0; + virtual void mountObb(const String16& rawPath, const String16& canonicalPath, + const String16& key, const sp<IObbActionListener>& token, + const int32_t nonce) = 0; + virtual void unmountObb(const String16& filename, const bool force, + const sp<IObbActionListener>& token, const int32_t nonce) = 0; + virtual bool isObbMounted(const String16& filename) = 0; + virtual bool getMountedObbPath(const String16& filename, String16& path) = 0; + virtual int32_t decryptStorage(const String16& password) = 0; + virtual int32_t encryptStorage(const String16& password) = 0; +}; + +// ---------------------------------------------------------------------------- + +class BnMountService: public BnInterface<IMountService> { +public: + virtual status_t onTransact(uint32_t code, const Parcel& data, + Parcel* reply, uint32_t flags = 0); +}; + +} +; // namespace android + +#endif // ANDROID_IMOUNTSERVICE_H diff --git a/libs/storage/include/storage/IMountServiceListener.h b/libs/storage/include/storage/IMountServiceListener.h new file mode 100644 index 000000000000..5b1f21cf98b5 --- /dev/null +++ b/libs/storage/include/storage/IMountServiceListener.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_IMOUNTSERVICELISTENER_H +#define ANDROID_IMOUNTSERVICELISTENER_H + +#include <binder/IInterface.h> +#include <binder/Parcel.h> + +namespace android { + +class IMountServiceListener: public IInterface { +public: + DECLARE_META_INTERFACE(MountServiceListener); + + virtual void onUsbMassStorageConnectionChanged(const bool connected) = 0; + virtual void onStorageStateChanged(const String16& path, + const String16& oldState, const String16& newState) = 0; +}; + +// ---------------------------------------------------------------------------- + +class BnMountServiceListener: public BnInterface<IMountServiceListener> { +public: + virtual status_t onTransact(uint32_t code, const Parcel& data, + Parcel* reply, uint32_t flags = 0); +}; + +} +; // namespace android + +#endif // ANDROID_IMOUNTSERVICELISTENER_H diff --git a/libs/storage/include/storage/IMountShutdownObserver.h b/libs/storage/include/storage/IMountShutdownObserver.h new file mode 100644 index 000000000000..d019e019bc0d --- /dev/null +++ b/libs/storage/include/storage/IMountShutdownObserver.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_IMOUNTSHUTDOWNOBSERVER_H +#define ANDROID_IMOUNTSHUTDOWNOBSERVER_H + +#include <binder/IInterface.h> +#include <binder/Parcel.h> + +namespace android { + +class IMountShutdownObserver: public IInterface +{ +public: + DECLARE_META_INTERFACE(MountShutdownObserver); + + virtual void onShutDownComplete(const int32_t statusCode) = 0; +}; + +// ---------------------------------------------------------------------------- + +class BnMountShutdownObserver: public BnInterface<IMountShutdownObserver> +{ +public: + virtual status_t onTransact( uint32_t code, + const Parcel& data, + Parcel* reply, + uint32_t flags = 0); +}; + +}; // namespace android + +#endif // ANDROID_IMOUNTSHUTDOWNOBSERVER_H diff --git a/libs/storage/include/storage/IObbActionListener.h b/libs/storage/include/storage/IObbActionListener.h new file mode 100644 index 000000000000..d78273cf2dd4 --- /dev/null +++ b/libs/storage/include/storage/IObbActionListener.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_IOBBACTIONLISTENER_H +#define ANDROID_IOBBACTIONLISTENER_H + +#include <binder/IInterface.h> +#include <binder/Parcel.h> + +#include <utils/String16.h> + +namespace android { + +class IObbActionListener: public IInterface +{ +public: + DECLARE_META_INTERFACE(ObbActionListener); + + virtual void onObbResult(const String16& filename, const int32_t nonce, const int32_t state) = 0; +}; + +// ---------------------------------------------------------------------------- + +class BnObbActionListener: public BnInterface<IObbActionListener> +{ +public: + virtual status_t onTransact( uint32_t code, + const Parcel& data, + Parcel* reply, + uint32_t flags = 0); +}; + +}; // namespace android + +#endif // ANDROID_IOBBACTIONLISTENER_H |