Skip to content

Commit

Permalink
Modify Unity Android Manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmfinol committed Oct 19, 2023
1 parent 2201155 commit eca5652
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Assets/Scripts/Cgs/CardGameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
126 changes: 126 additions & 0 deletions Assets/Scripts/Cgs/Editor/ModifyUnityAndroidManifest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Cgs/Editor/ModifyUnityAndroidManifest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ProjectSettings/ProjectSettings.asset

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eca5652

Please sign in to comment.