8e1c1f4d9d
This CL topic speeds up DefaultMimeMapFactory.create() from 15.5msec to 5.7msec (measured by bundling a copy of the logic and data into a test app) and saves 5.2 KByte of space (16 KBytes uncompressed) in framework.jar. This CL topic does not change the amount of heap memory consumed by the loaded MimeMap. This is achieved by moving the following parsing steps from DefaultMimeMapFactory into a build-time optimization step: * strip off comments (starting with '#') * normalize whitespace (trim leading and trailing whitespace; replace remaining whitespace with a single ' '). * drop lines that do not contain a ' ' after this step (those only contained comments or only a MIME type without any extension). * use String.indexOf(char) in favor of String.contains(String) or Pattern matching. Noteworthy extra step specific to this CL: * specifically for vendor.mime.types (but not android.mime.types), add a prefix '?' to MIME types and extensions at build time, rather than at runtime. Note that after this CL, DefaultMimeMapFactory can now *only* parse minimized *mime.types files, not the original (non-minimized) file format. Test: Checked that the mapping didn't change by checking that a device flashed after this CL passes CtsMimeMapTestCases built before this CL. Test: Ran "make mimemap-res.jar" and manually verified that files in the resulting jar look as expected. Test: Locally added some extra mappings to vendor.mime.types, some of them with a leading '?', and verified that they all show up with exactly one leading '?' for the MIME type and each extension within mimemap-res.jar. Bug: 142267887 Change-Id: Idf1ef945a4ac225476f2036fbc8bb35eb9c090af
122 lines
4.2 KiB
Plaintext
122 lines
4.2 KiB
Plaintext
// Copyright (C) 2019 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.
|
|
|
|
|
|
java_defaults {
|
|
name: "mimemap-defaults",
|
|
srcs: [
|
|
"java/android/content/type/DefaultMimeMapFactory.java",
|
|
],
|
|
sdk_version: "core_platform",
|
|
}
|
|
|
|
java_library {
|
|
name: "mimemap",
|
|
defaults: ["mimemap-defaults"],
|
|
static_libs: ["mimemap-res.jar"],
|
|
visibility: [
|
|
"//frameworks/base:__subpackages__",
|
|
],
|
|
}
|
|
|
|
java_library {
|
|
name: "mimemap-testing",
|
|
defaults: ["mimemap-defaults"],
|
|
static_libs: ["mimemap-testing-res.jar"],
|
|
jarjar_rules: "jarjar-rules.txt",
|
|
visibility: [
|
|
"//cts/tests/tests/mimemap:__subpackages__",
|
|
"//frameworks/base:__subpackages__",
|
|
],
|
|
}
|
|
|
|
// The mimemap-res.jar and mimemap-testing-res.jar genrules produce a .jar that
|
|
// has the resource file in a subdirectory res/ and testres/, respectively.
|
|
// They need to be in different paths because one of them ends up in a
|
|
// bootclasspath jar whereas the other one ends up in a test jar. Bootclasspath
|
|
// resources hide test or application resources under the same path because
|
|
// ClassLoader.getResource(String) consults the parent ClassLoader first.
|
|
//
|
|
// Further notes:
|
|
// - the "cp" command will flatten any directory paths that occur in $(in),
|
|
// but here they happen to already be in the root directory. If we needed
|
|
// to preserve sub paths then we might want to zip the files first and then
|
|
// unzip them below the new parent directory.
|
|
// - the path names "res/" and "testres/" and duplicated in .java source files
|
|
// (DefaultMimeMapFactory.java and MimeMapTest.java, as of October 2019).
|
|
java_genrule {
|
|
name: "mimemap-res.jar",
|
|
tools: [
|
|
"soong_zip",
|
|
],
|
|
srcs: [":mime.types.minimized"],
|
|
out: ["mimemap-res.jar"],
|
|
cmd: "mkdir $(genDir)/res/ && cp $(in) $(genDir)/res/ && $(location soong_zip) -C $(genDir) -o $(out) -D $(genDir)/res/",
|
|
}
|
|
|
|
// The same as mimemap-res.jar except that the resources are placed in a different directory.
|
|
// They get bundled with CTS so that CTS can compare a device's MimeMap implementation vs.
|
|
// the stock Android one from when CTS was built.
|
|
java_genrule {
|
|
name: "mimemap-testing-res.jar",
|
|
tools: [
|
|
"soong_zip",
|
|
],
|
|
srcs: [":mime.types.minimized"],
|
|
out: ["mimemap-testing-res.jar"],
|
|
cmd: "mkdir $(genDir)/testres/ && cp $(in) $(genDir)/testres/ && $(location soong_zip) -C $(genDir) -o $(out) -D $(genDir)/testres/",
|
|
}
|
|
|
|
// Combination of all *mime.types.minimized resources.
|
|
filegroup {
|
|
name: "mime.types.minimized",
|
|
visibility: [
|
|
"//visibility:private",
|
|
],
|
|
srcs: [
|
|
":debian.mime.types.minimized",
|
|
":android.mime.types.minimized",
|
|
":vendor.mime.types.minimized",
|
|
],
|
|
}
|
|
|
|
java_genrule {
|
|
name: "android.mime.types.minimized",
|
|
visibility: [
|
|
"//visibility:private",
|
|
],
|
|
out: ["android.mime.types"],
|
|
srcs: [
|
|
"java-res/android.mime.types",
|
|
],
|
|
// strip comments normalize whitepace drop empty lines
|
|
cmd: "awk '{gsub(/#.*$$/,\"\"); $$1=$$1; print;}' $(in) | grep ' ' > $(out)",
|
|
}
|
|
|
|
// Unlike the other *mime.types files, vendor.mime.types gets '?' prepended to
|
|
// every field so that its mappings will never overwrite earlier mappings by
|
|
// the other resource files. http://b/141842825
|
|
java_genrule {
|
|
name: "vendor.mime.types.minimized",
|
|
visibility: [
|
|
"//visibility:private",
|
|
],
|
|
out: ["vendor.mime.types"],
|
|
srcs: [
|
|
"java-res/vendor.mime.types",
|
|
],
|
|
// strip comments normalize whitepace drop empty lines prepend ? to fields that are missing it
|
|
cmd: "awk '{gsub(/#.*$$/,\"\"); $$1=$$1; print;}' $(in) | grep ' ' | awk '{for(i=1;i<=NF;i++) { sub(/^\\??/, \"?\", $$i); }; print}' > $(out)",
|
|
}
|