From eca56522886b4141582cd86aa8abb6751058e8a7 Mon Sep 17 00:00:00 2001 From: David Finol Date: Wed, 18 Oct 2023 22:24:03 -0500 Subject: [PATCH] Modify Unity Android Manifest --- Assets/Scripts/Cgs/CardGameManager.cs | 6 +- .../Cgs/Editor/ModifyUnityAndroidManifest.cs | 126 ++++++++++++++++++ .../Editor/ModifyUnityAndroidManifest.cs.meta | 11 ++ ProjectSettings/ProjectSettings.asset | 2 +- 4 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 Assets/Scripts/Cgs/Editor/ModifyUnityAndroidManifest.cs create mode 100644 Assets/Scripts/Cgs/Editor/ModifyUnityAndroidManifest.cs.meta diff --git a/Assets/Scripts/Cgs/CardGameManager.cs b/Assets/Scripts/Cgs/CardGameManager.cs index ed3fb5a5..fa54cc7e 100644 --- a/Assets/Scripts/Cgs/CardGameManager.cs +++ b/Assets/Scripts/Cgs/CardGameManager.cs @@ -197,10 +197,14 @@ private void CreateDefaultCardGames() { #if UNITY_ANDROID && !UNITY_EDITOR UnityFileMethods.ExtractAndroidStreamingAssets(UnityCardGame.GamesDirectoryPath); -#else +#elif UNITY_WEBGL UnityFileMethods.CopyDirectory( Application.streamingAssetsPath + Tags.StandardPlayingCardsDirectoryName, UnityCardGame.GamesDirectoryPath + Tags.StandardPlayingCardsDirectoryName); +#else + UnityFileMethods.CopyDirectory( + Application.streamingAssetsPath, + UnityCardGame.GamesDirectoryPath); #endif } diff --git a/Assets/Scripts/Cgs/Editor/ModifyUnityAndroidManifest.cs b/Assets/Scripts/Cgs/Editor/ModifyUnityAndroidManifest.cs new file mode 100644 index 00000000..908c97f5 --- /dev/null +++ b/Assets/Scripts/Cgs/Editor/ModifyUnityAndroidManifest.cs @@ -0,0 +1,126 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +using System.IO; +using System.Text; +using System.Xml; +using UnityEditor.Android; +using UnityEngine; + +namespace Cgs.Editor +{ + public class ModifyUnityAndroidAppManifest : IPostGenerateGradleAndroidProject + { + public void OnPostGenerateGradleAndroidProject(string basePath) + { + Debug.Log("OnPostGenerateGradleAndroidProject"); + var androidManifest = new AndroidManifest(GetManifestPath(basePath)); + androidManifest.SetDeepLinkHook(); + var androidManifestPath = androidManifest.Save(); + Debug.Log($"Updated Android Manifest {androidManifestPath}"); + } + + public int callbackOrder => 1; + + private string _manifestFilePath; + + private string GetManifestPath(string basePath) + { + if (!string.IsNullOrEmpty(_manifestFilePath)) + return _manifestFilePath; + + var pathBuilder = new StringBuilder(basePath); + pathBuilder.Append(Path.DirectorySeparatorChar).Append("src"); + pathBuilder.Append(Path.DirectorySeparatorChar).Append("main"); + pathBuilder.Append(Path.DirectorySeparatorChar).Append("AndroidManifest.xml"); + _manifestFilePath = pathBuilder.ToString(); + + return _manifestFilePath; + } + } + + + internal class AndroidXmlDocument : XmlDocument + { + protected const string AndroidXmlNamespace = "http://schemas.android.com/apk/res/android"; + + // ReSharper disable once FieldCanBeMadeReadOnly.Global + protected XmlNamespaceManager NsMgr; + private readonly string _path; + + protected AndroidXmlDocument(string path) + { + _path = path; + using (var reader = new XmlTextReader(_path)) + { + reader.Read(); + // ReSharper disable once VirtualMemberCallInConstructor + Load(reader); + } + + NsMgr = new XmlNamespaceManager(NameTable); + NsMgr.AddNamespace("android", AndroidXmlNamespace); + } + + public string Save() + { + return SaveAs(_path); + } + + private string SaveAs(string path) + { + using var writer = new XmlTextWriter(path, new UTF8Encoding(false)); + writer.Formatting = Formatting.Indented; + Save(writer); + + return path; + } + } + + internal class AndroidManifest : AndroidXmlDocument + { + public AndroidManifest(string path) : base(path) + { + } + + private XmlNode GetActivityWithLaunchIntent() + { + return SelectSingleNode( + "/manifest/application/activity[intent-filter/action/@android:name='android.intent.action.MAIN' and " + + "intent-filter/category/@android:name='android.intent.category.LAUNCHER']", NsMgr); + } + + private XmlAttribute CreateAndroidAttribute(string key, string value) + { + var xmlAttribute = CreateAttribute("android", key, AndroidXmlNamespace); + xmlAttribute.Value = value; + return xmlAttribute; + } + + internal void SetDeepLinkHook() + { + var activityWithLaunchIntent = GetActivityWithLaunchIntent(); + XmlElement intentFilter = CreateElement("intent-filter"); + activityWithLaunchIntent?.AppendChild(intentFilter); + XmlElement action = CreateElement("action"); + intentFilter.AppendChild(action); + XmlAttribute actionAttribute = CreateAndroidAttribute("name", "android.intent.action.VIEW"); + action.Attributes.Append(actionAttribute); + XmlElement category = CreateElement("category"); + intentFilter.AppendChild(category); + XmlAttribute categoryAttribute = CreateAndroidAttribute("name", "android.intent.category.DEFAULT"); + category.Attributes.Append(categoryAttribute); + XmlElement category2 = CreateElement("category"); + intentFilter.AppendChild(category2); + XmlAttribute category2Attribute = CreateAndroidAttribute("name", "android.intent.category.BROWSABLE"); + category2.Attributes.Append(category2Attribute); + XmlElement data = CreateElement("data"); + intentFilter.AppendChild(data); + XmlAttribute dataAttribute = CreateAndroidAttribute("scheme", "cardgamesim"); + data.Attributes.Append(dataAttribute); + XmlAttribute dataAttribute2 = CreateAndroidAttribute("host", "link"); + data.Attributes.Append(dataAttribute2); + } + } +} diff --git a/Assets/Scripts/Cgs/Editor/ModifyUnityAndroidManifest.cs.meta b/Assets/Scripts/Cgs/Editor/ModifyUnityAndroidManifest.cs.meta new file mode 100644 index 00000000..148a4dba --- /dev/null +++ b/Assets/Scripts/Cgs/Editor/ModifyUnityAndroidManifest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1876c893d8b6d75449b49c49d7bd69b3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 3b17e07f..5da002f9 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -252,7 +252,7 @@ PlayerSettings: clonedFromGUID: 00000000000000000000000000000000 templatePackageId: templateDefaultScene: - useCustomMainManifest: 0 + useCustomMainManifest: 1 useCustomLauncherManifest: 0 useCustomMainGradleTemplate: 0 useCustomLauncherGradleManifest: 0