|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.wear.watchface.dfx.memory |
| 18 | + |
| 19 | +import java.nio.file.Path |
| 20 | +import java.nio.file.Paths |
| 21 | +import java.util.regex.Pattern |
| 22 | + |
| 23 | +/** Represents a resource, from an AAB or APK. */ |
| 24 | +class AndroidResource( |
| 25 | + // Resource type, for example "raw", "asset", "drawable" etc. |
| 26 | + private val resourceType: String, |
| 27 | + // Resource name, for example "watchface" for res/raw/watchface.xml. |
| 28 | + val resourceName: String, |
| 29 | + // File extension of the resource, for example "xml" for res/raw/watchface.xml |
| 30 | + private val extension: String, |
| 31 | + // Path in the package. This is the obfuscated path to the actual data, where obfuscation has |
| 32 | + // been used, for example "res/raw/watchface.xml" may point to something like "res/li.xml". |
| 33 | + val filePath: Path, |
| 34 | + // The resource data itself. |
| 35 | + val data: ByteArray |
| 36 | +) { |
| 37 | + // TODO: This should be improved to parse res/xml/watch_face_info.xml where present, so as not |
| 38 | + // to assume all XML files in the res/raw directory are watch face XML files. |
| 39 | + fun isWatchFaceXml(): Boolean = extension == "xml" && resourceType == "raw" |
| 40 | + |
| 41 | + fun isDrawable(): Boolean = resourceType == "drawable" |
| 42 | + |
| 43 | + fun isFont(): Boolean = resourceType == "font" |
| 44 | + |
| 45 | + fun isAsset(): Boolean = resourceType == "asset" |
| 46 | + |
| 47 | + fun isRaw(): Boolean = resourceType == "raw" |
| 48 | + |
| 49 | + companion object { |
| 50 | + private val VALID_RESOURCE_PATH: Pattern = |
| 51 | + Pattern.compile(".*res/([^-/]+).*/([^.]+)(\\.|)(.*|)$") |
| 52 | + private const val VALID_RESOURCE_GROUPS: Int = 4 |
| 53 | + |
| 54 | + @JvmStatic |
| 55 | + fun fromPath(filePath: Path, data: ByteArray): AndroidResource { |
| 56 | + val pathWithFwdSlashes = filePath.toString().replace('\\', '/') |
| 57 | + val matcher = VALID_RESOURCE_PATH.matcher(pathWithFwdSlashes) |
| 58 | + if (matcher.matches() && matcher.groupCount() == VALID_RESOURCE_GROUPS) { |
| 59 | + val resType = matcher.group(1) |
| 60 | + val resName = matcher.group(2) |
| 61 | + val ext = matcher.group(4) |
| 62 | + return AndroidResource(resType, resName, ext, filePath, data) |
| 63 | + } |
| 64 | + throw RuntimeException("Not a valid resource file: $pathWithFwdSlashes") |
| 65 | + } |
| 66 | + |
| 67 | + @JvmStatic |
| 68 | + fun fromPath(filePath: String, data: ByteArray): AndroidResource = |
| 69 | + fromPath(Paths.get(filePath), data) |
| 70 | + |
| 71 | + @JvmStatic |
| 72 | + fun isValidResourcePath(filePath: Path): Boolean { |
| 73 | + val pathWithFwdSlashes = filePath.toString().replace('\\', '/') |
| 74 | + val matcher = VALID_RESOURCE_PATH.matcher(pathWithFwdSlashes) |
| 75 | + return matcher.matches() && matcher.groupCount() == VALID_RESOURCE_GROUPS |
| 76 | + } |
| 77 | + |
| 78 | + @JvmStatic |
| 79 | + fun isValidResourcePath(filePath: String): Boolean = |
| 80 | + isValidResourcePath(Paths.get(filePath)) |
| 81 | + } |
| 82 | +} |
0 commit comments