123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- /****************************************************************************
- * Copyright 2019 Nreal Techonology Limited.All rights reserved.
- *
- * This file is part of NRSDK.
- *
- * https://www.nreal.ai/
- *
- *****************************************************************************/
- namespace NRKernal
- {
- using System.Text;
- using System.Xml;
- /// <summary> An android XML document. </summary>
- internal class AndroidXmlDocument : XmlDocument
- {
- /// <summary> Full pathname of the file. </summary>
- protected string m_Path;
- /// <summary> Manager for name space. </summary>
- protected XmlNamespaceManager nameSpaceManager;
- /// <summary> The android XML namespace. </summary>
- public readonly string AndroidXmlNamespace = "http://schemas.android.com/apk/res/android";
- /// <summary> The android tools XML namespace. </summary>
- public readonly string AndroidToolsXmlNamespace = "http://schemas.android.com/tools";
- /// <summary> Constructor. </summary>
- /// <param name="path"> Full pathname of the file.</param>
- public AndroidXmlDocument(string path)
- {
- m_Path = path;
- using (var reader = new XmlTextReader(m_Path))
- {
- reader.Read();
- Load(reader);
- }
- nameSpaceManager = new XmlNamespaceManager(NameTable);
- nameSpaceManager.AddNamespace("android", AndroidXmlNamespace);
- }
- /// <summary> Gets the save. </summary>
- /// <returns> A string. </returns>
- public string Save()
- {
- return SaveAs(m_Path);
- }
- /// <summary> Saves as. </summary>
- /// <param name="path"> Full pathname of the file.</param>
- /// <returns> A string. </returns>
- public string SaveAs(string path)
- {
- using (var writer = new XmlTextWriter(path, new UTF8Encoding(false)))
- {
- writer.Formatting = Formatting.Indented;
- Save(writer);
- }
- return path;
- }
- }
- /// <summary> A list of the android. </summary>
- internal class AndroidManifest : AndroidXmlDocument
- {
- /// <summary> Element describing the application. </summary>
- private readonly XmlElement ApplicationElement;
- /// <summary> Constructor. </summary>
- /// <param name="path"> Full pathname of the file.</param>
- public AndroidManifest(string path) : base(path)
- {
- ApplicationElement = SelectSingleNode("/manifest/application") as XmlElement;
- }
- /// <summary> Creates android attribute. </summary>
- /// <param name="key"> The key.</param>
- /// <param name="value"> The value.</param>
- /// <param name="name"> (Optional) The name.</param>
- /// <returns> The new android attribute. </returns>
- private XmlAttribute CreateAndroidAttribute(string key, string value, string name = "android")
- {
- XmlAttribute attr;
- if (name.Equals("tools"))
- {
- attr = CreateAttribute(name, key, AndroidToolsXmlNamespace);
- attr.Value = value;
- }
- else
- {
- attr = CreateAttribute(name, key, AndroidXmlNamespace);
- attr.Value = value;
- }
- return attr;
- }
- /// <summary> Gets activity with launch intent. </summary>
- /// <returns> The activity with launch intent. </returns>
- internal 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']", nameSpaceManager);
- }
- /// <summary> Gets activity with information intent. </summary>
- /// <returns> The activity with information intent. </returns>
- internal XmlNode GetActivityWithInfoIntent()
- {
- return SelectSingleNode("/manifest/application/activity[intent-filter/action/@android:name='android.intent.action.MAIN' and " +
- "intent-filter/category/@android:name='android.intent.category.INFO']", nameSpaceManager);
- }
- /// <summary> Sets external storage. </summary>
- /// <param name="flag"> True to flag.</param>
- internal void SetExternalStorage(bool flag)
- {
- var activity = SelectSingleNode("/manifest/application");
- var rightapplicationData = SelectSingleNode("/manifest/application[@android:requestLegacyExternalStorage='true']", nameSpaceManager);
- if (flag)
- {
- if (rightapplicationData == null)
- {
- XmlAttribute newAttribute = CreateAndroidAttribute("requestLegacyExternalStorage", "true");
- activity.Attributes.Append(newAttribute);
- }
- }
- else
- {
- if (rightapplicationData != null)
- {
- activity.Attributes.RemoveNamedItem("android:requestLegacyExternalStorage");
- }
- }
- }
- /// <summary> Sets camera permission. </summary>
- internal void SetCameraPermission()
- {
- var manifest = SelectSingleNode("/manifest");
- if (!manifest.InnerXml.Contains("android.permission.CAMERA"))
- {
- XmlElement child = CreateElement("uses-permission");
- manifest.AppendChild(child);
- XmlAttribute newAttribute = CreateAndroidAttribute("name", "android.permission.CAMERA");
- child.Attributes.Append(newAttribute);
- }
- //else
- //{
- // NRDebugger.Info("Already has the camera permission.");
- //}
- }
- internal void SetPackageReadPermission()
- {
- var manifest = SelectSingleNode("/manifest");
- if (!manifest.InnerXml.Contains("android.permission.QUERY_ALL_PACKAGES"))
- {
- XmlElement child = CreateElement("uses-permission");
- manifest.AppendChild(child);
- XmlAttribute newAttribute = CreateAndroidAttribute("name", "android.permission.QUERY_ALL_PACKAGES");
- child.Attributes.Append(newAttribute);
- }
- //else
- //{
- // NRDebugger.Info("Already has the permission of 'android.permission.QUERY_ALL_PACKAGES'.");
- //}
- }
- /// <summary> Sets blue tooth permission. </summary>
- internal void SetBlueToothPermission()
- {
- var manifest = SelectSingleNode("/manifest");
- if (!manifest.InnerXml.Contains("android.permission.BLUETOOTH"))
- {
- XmlElement child = CreateElement("uses-permission");
- manifest.AppendChild(child);
- XmlAttribute newAttribute = CreateAndroidAttribute("name", "android.permission.BLUETOOTH");
- child.Attributes.Append(newAttribute);
- // newAttribute = CreateAndroidAttribute("name", "android.permission.BLUETOOTH_ADMIN");
- // child.Attributes.Append(newAttribute);
- }
- //else
- //{
- // NRDebugger.Info("Already has the bluetooth permission.");
- //}
- }
- internal void SetAudioRecordPermission()
- {
- var manifest = SelectSingleNode("/manifest");
- if (!manifest.InnerXml.Contains("android.permission.RECORD_AUDIO"))
- {
- XmlElement child = CreateElement("uses-permission");
- manifest.AppendChild(child);
- XmlAttribute newAttribute = CreateAndroidAttribute("name", "android.permission.RECORD_AUDIO");
- child.Attributes.Append(newAttribute);
- }
- }
- /// <summary> Sets sdk meta data. </summary>
- internal void SetSDKMetaData()
- {
- var activity = SelectSingleNode("/manifest/application");
- // metadata for "nreal_sdk
- var newMetaNRSDK = SelectSingleNode("/manifest/application/meta-data[@android:name='nreal_sdk' and " +
- "@android:value='true']", nameSpaceManager);
- var oldMetaNRSDK = SelectSingleNode("/manifest/application/meta-data[@android:name='nreal_sdk']", nameSpaceManager);
- if (newMetaNRSDK == null)
- {
- if (oldMetaNRSDK != null)
- {
- activity.RemoveChild(oldMetaNRSDK);
- }
- XmlElement child = CreateElement("meta-data");
- activity.AppendChild(child);
- XmlAttribute newAttribute = CreateAndroidAttribute("name", "nreal_sdk");
- child.Attributes.Append(newAttribute);
- newAttribute = CreateAndroidAttribute("value", "true");
- child.Attributes.Append(newAttribute);
- }
- // metadata for "com.nreal.supportDevices"
- string supportDevices = NRProjectConfigHelper.GetProjectConfig().GetTargetDeviceTypesDesc();
- var newMetaDevices = SelectSingleNode("/manifest/application/meta-data[@android:name='com.nreal.supportDevices' and " +
- "@android:value='']", nameSpaceManager);
- var oldMetaDevices = SelectSingleNode("/manifest/application/meta-data[@android:name='com.nreal.supportDevices']", nameSpaceManager);
- if (oldMetaDevices != null)
- activity.RemoveChild(oldMetaDevices);
- if (newMetaDevices == null)
- {
- XmlElement child = CreateElement("meta-data");
- activity.AppendChild(child);
- XmlAttribute newAttribute = CreateAndroidAttribute("name", "com.nreal.supportDevices");
- child.Attributes.Append(newAttribute);
- newAttribute = CreateAndroidAttribute("value", supportDevices);
- child.Attributes.Append(newAttribute);
- }
- }
- /// <summary> Sets a pk displayed on launcher. </summary>
- /// <param name="show"> True to show, false to hide.</param>
- internal void SetAPKDisplayedOnLauncher(bool show)
- {
- var activity = GetActivityWithLaunchIntent();
- if (activity == null)
- {
- activity = GetActivityWithInfoIntent();
- }
- var intentfilter = SelectSingleNode("/manifest/application/activity/intent-filter[action/@android:name='android.intent.action.MAIN']", nameSpaceManager);
- var categoryInfo = SelectSingleNode("/manifest/application/activity/intent-filter/category[@android:name='android.intent.category.INFO']", nameSpaceManager);
- var categoryLauncher = SelectSingleNode("/manifest/application/activity/intent-filter/category[@android:name='android.intent.category.LAUNCHER']", nameSpaceManager);
- if (show)
- {
- // Add launcher category
- XmlElement newcategory = CreateElement("category");
- XmlAttribute newAttribute = CreateAndroidAttribute("name", "android.intent.category.LAUNCHER");
- newcategory.Attributes.Append(newAttribute);
- if (categoryInfo != null)
- {
- intentfilter.ReplaceChild(newcategory, categoryInfo);
- }
- else if (categoryLauncher == null)
- {
- intentfilter.AppendChild(newcategory);
- }
- }
- else
- {
- // Add info category
- XmlElement newcategory = CreateElement("category");
- XmlAttribute newAttribute = CreateAndroidAttribute("name", "android.intent.category.INFO");
- newcategory.Attributes.Append(newAttribute);
- newAttribute = CreateAndroidAttribute("node", "replace", "tools");
- newcategory.Attributes.Append(newAttribute);
- if (categoryLauncher != null)
- {
- intentfilter.ReplaceChild(newcategory, categoryLauncher);
- }
- else if (categoryInfo == null)
- {
- intentfilter.AppendChild(newcategory);
- }
- }
- }
- }
- }
|