Add PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES

It is a list of <module_name>:<manifest_name> pairs. When the module
name of an APK or an APEX matches with <module_name>, then its app
manifest name is overridden to <manifest_name>.

<module_name> and <manifest_name> can be patterns as in
com.android.%:com.mycompany.android.%.release

Note that, in case of APEXes, the manifest name refers to the name of
the zip container. The apex manifest name (which is specified in
apex_manifest.json) is not overridden.

Test: m with PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES for
1) an APK in Android.mk
2) an APK in ANdroid.bp
3) an APEX
and check that manifest names are modified as specified

Change-Id: Ie58882d90884695e893944c43d9c8803b283e93d
diff --git a/android/config.go b/android/config.go
index f046318..b3469a9 100644
--- a/android/config.go
+++ b/android/config.go
@@ -862,6 +862,24 @@
 	return c.config.productVariables.BoardPlatPrivateSepolicyDirs
 }
 
+func (c *deviceConfig) OverrideManifestPackageNameFor(name string) (manifestName string, overridden bool) {
+	overrides := c.config.productVariables.ManifestPackageNameOverrides
+	if overrides == nil || len(overrides) == 0 {
+		return "", false
+	}
+	for _, o := range overrides {
+		split := strings.Split(o, ":")
+		if len(split) != 2 {
+			// This shouldn't happen as this is first checked in make, but just in case.
+			panic(fmt.Errorf("invalid override rule %q in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES should be <module_name>:<manifest_name>", o))
+		}
+		if matchPattern(split[0], name) {
+			return substPattern(split[0], split[1], name), true
+		}
+	}
+	return "", false
+}
+
 func (c *config) SecondArchIsTranslated() bool {
 	deviceTargets := c.Targets[Android]
 	if len(deviceTargets) < 2 {
diff --git a/android/util.go b/android/util.go
index e9b9764..92ab845 100644
--- a/android/util.go
+++ b/android/util.go
@@ -202,3 +202,44 @@
 	}
 	return v
 }
+
+// copied from build/kati/strutil.go
+func substPattern(pat, repl, str string) string {
+	ps := strings.SplitN(pat, "%", 2)
+	if len(ps) != 2 {
+		if str == pat {
+			return repl
+		}
+		return str
+	}
+	in := str
+	trimed := str
+	if ps[0] != "" {
+		trimed = strings.TrimPrefix(in, ps[0])
+		if trimed == in {
+			return str
+		}
+	}
+	in = trimed
+	if ps[1] != "" {
+		trimed = strings.TrimSuffix(in, ps[1])
+		if trimed == in {
+			return str
+		}
+	}
+
+	rs := strings.SplitN(repl, "%", 2)
+	if len(rs) != 2 {
+		return repl
+	}
+	return rs[0] + trimed + rs[1]
+}
+
+// copied from build/kati/strutil.go
+func matchPattern(pat, str string) bool {
+	i := strings.IndexByte(pat, '%')
+	if i < 0 {
+		return pat == str
+	}
+	return strings.HasPrefix(str, pat[:i]) && strings.HasSuffix(str, pat[i+1:])
+}
diff --git a/android/variable.go b/android/variable.go
index a1cdea1..264869a 100644
--- a/android/variable.go
+++ b/android/variable.go
@@ -261,6 +261,8 @@
 	FlattenApex *bool `json:",omitempty"`
 
 	DexpreoptGlobalConfig *string `json:",omitempty"`
+
+	ManifestPackageNameOverrides []string `json:",omitempty"`
 }
 
 func boolPtr(v bool) *bool {