-
Notifications
You must be signed in to change notification settings - Fork 254
/
ManifestMod.cs
371 lines (315 loc) · 16.6 KB
/
ManifestMod.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
* copy, modify, and distribute this software in source code or binary form for use
* in connection with the web services and APIs provided by Facebook.
*
* As with any software that integrates with the Facebook platform, your use of
* this software is subject to the Facebook Developer Principles and Policies
* [http://developers.facebook.com/policy/]. This copyright notice shall be
* included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Facebook.Unity.Editor
{
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Xml;
using Facebook.Unity;
using Facebook.Unity.Settings;
using UnityEditor;
using UnityEngine;
public class ManifestMod
{
public const string AppLinkActivityName = "com.facebook.unity.FBUnityAppLinkActivity";
public const string DeepLinkingActivityName = "com.facebook.unity.FBUnityDeepLinkingActivity";
public const string UnityLoginActivityName = "com.facebook.unity.FBUnityLoginActivity";
public const string UnityDialogsActivityName = "com.facebook.unity.FBUnityDialogsActivity";
public const string UnityGameRequestActivityName = "com.facebook.unity.FBUnityGameRequestActivity";
public const string UnityGamingServicesFriendFinderActivityName = "com.facebook.unity.FBUnityGamingServicesFriendFinderActivity";
public const string ApplicationIdMetaDataName = "com.facebook.sdk.ApplicationId";
public const string AutoLogAppEventsEnabled = "com.facebook.sdk.AutoLogAppEventsEnabled";
public const string AdvertiserIDCollectionEnabled = "com.facebook.sdk.AdvertiserIDCollectionEnabled";
public const string FacebookContentProviderName = "com.facebook.FacebookContentProvider";
public const string FacebookContentProviderAuthFormat = "com.facebook.app.FacebookContentProvider{0}";
public const string FacebookActivityName = "com.facebook.FacebookActivity";
public const string AndroidManifestPath = "Plugins/Android/AndroidManifest.xml";
public const string FacebookDefaultAndroidManifestPath = "FacebookSDK/SDK/Editor/android/DefaultAndroidManifest.xml";
public static void GenerateManifest()
{
var outputFile = Path.Combine(Application.dataPath, ManifestMod.AndroidManifestPath);
// Create containing directory if it does not exist
Directory.CreateDirectory(Path.GetDirectoryName(outputFile));
// only copy over a fresh copy of the AndroidManifest if one does not exist
if (!File.Exists(outputFile))
{
ManifestMod.CreateDefaultAndroidManifest(outputFile);
}
UpdateManifest(outputFile);
}
public static bool CheckManifest()
{
bool result = true;
var outputFile = Path.Combine(Application.dataPath, ManifestMod.AndroidManifestPath);
if (!File.Exists(outputFile))
{
Debug.LogError("An android manifest must be generated for the Facebook SDK to work. Go to Facebook->Edit Settings and press \"Regenerate Android Manifest\"");
return false;
}
XmlDocument doc = new XmlDocument();
doc.Load(outputFile);
if (doc == null)
{
Debug.LogError("Couldn't load " + outputFile);
return false;
}
XmlNode manNode = FindChildNode(doc, "manifest");
XmlNode dict = FindChildNode(manNode, "application");
if (dict == null)
{
Debug.LogError("Error parsing " + outputFile);
return false;
}
XmlElement loginElement;
if (!ManifestMod.TryFindElementWithAndroidName(dict, UnityLoginActivityName, out loginElement))
{
Debug.LogError(string.Format("{0} is missing from your android manifest. Go to Facebook->Edit Settings and press \"Regenerate Android Manifest\"", UnityLoginActivityName));
result = false;
}
var deprecatedMainActivityName = "com.facebook.unity.FBUnityPlayerActivity";
XmlElement deprecatedElement;
if (ManifestMod.TryFindElementWithAndroidName(dict, deprecatedMainActivityName, out deprecatedElement))
{
Debug.LogWarning(string.Format("{0} is deprecated and no longer needed for the Facebook SDK. Feel free to use your own main activity or use the default \"com.unity3d.player.UnityPlayerNativeActivity\"", deprecatedMainActivityName));
}
return result;
}
public static void UpdateManifest(string fullPath)
{
string appId = FacebookSettings.AppId;
if (!FacebookSettings.IsValidAppId)
{
Debug.LogError("You didn't specify a Facebook app ID. Please add one using the Facebook menu in the main Unity editor.");
return;
}
XmlDocument doc = new XmlDocument();
doc.Load(fullPath);
if (doc == null)
{
Debug.LogError("Couldn't load " + fullPath);
return;
}
XmlNode manNode = FindChildNode(doc, "manifest");
XmlNode dict = FindChildNode(manNode, "application");
if (dict == null)
{
Debug.LogError("Error parsing " + fullPath);
return;
}
string ns = dict.GetNamespaceOfPrefix("android");
// add the unity login activity
XmlElement unityLoginElement = CreateUnityOverlayElement(doc, ns, UnityLoginActivityName);
ManifestMod.SetOrReplaceXmlElement(dict, unityLoginElement);
// add the unity dialogs activity
XmlElement unityDialogsElement = CreateUnityOverlayElement(doc, ns, UnityDialogsActivityName);
ManifestMod.SetOrReplaceXmlElement(dict, unityDialogsElement);
XmlElement unityGamingFriendFinderElement = CreateUnityOverlayElement(doc, ns, UnityGamingServicesFriendFinderActivityName);
ManifestMod.SetOrReplaceXmlElement(dict, unityGamingFriendFinderElement);
ManifestMod.AddAppLinkingActivity(doc, dict, ns, FacebookSettings.AppLinkSchemes[FacebookSettings.SelectedAppIndex].Schemes);
ManifestMod.AddSimpleActivity(doc, dict, ns, DeepLinkingActivityName, true);
ManifestMod.AddSimpleActivity(doc, dict, ns, UnityGameRequestActivityName);
// add the app id
// <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="\ fb<APPID>" />
XmlElement appIdElement = doc.CreateElement("meta-data");
appIdElement.SetAttribute("name", ns, ApplicationIdMetaDataName);
appIdElement.SetAttribute("value", ns, "fb" + appId);
ManifestMod.SetOrReplaceXmlElement(dict, appIdElement);
// enable AutoLogAppEventsEnabled by default
// <meta-data android:name="com.facebook.sdk.AutoLogAppEventsEnabled" android:value="true"/>
string autoLogAppEventsEnabled = FacebookSettings.AutoLogAppEventsEnabled.ToString().ToLower();
XmlElement autoLogAppEventsEnabledElement = doc.CreateElement("meta-data");
autoLogAppEventsEnabledElement.SetAttribute("name", ns, AutoLogAppEventsEnabled);
autoLogAppEventsEnabledElement.SetAttribute("value", ns, autoLogAppEventsEnabled);
ManifestMod.SetOrReplaceXmlElement(dict, autoLogAppEventsEnabledElement);
// enable AdvertiserIDCollectionEnabled by default
// <meta-data android:name="com.facebook.sdk.AdvertiserIDCollectionEnabled" android:value="true"/>
string advertiserIDCollectionEnabled = FacebookSettings.AdvertiserIDCollectionEnabled.ToString().ToLower();
XmlElement advertiserIDCollectionEnabledElement = doc.CreateElement("meta-data");
advertiserIDCollectionEnabledElement.SetAttribute("name", ns, AdvertiserIDCollectionEnabled);
advertiserIDCollectionEnabledElement.SetAttribute("value", ns, advertiserIDCollectionEnabled);
ManifestMod.SetOrReplaceXmlElement(dict, advertiserIDCollectionEnabledElement);
// Add the facebook content provider
// <provider
// android:name="com.facebook.FacebookContentProvider"
// android:authorities="com.facebook.app.FacebookContentProvider<APPID>"
// android:exported="true" />
XmlElement contentProviderElement = CreateContentProviderElement(doc, ns, appId);
ManifestMod.SetOrReplaceXmlElement(dict, contentProviderElement);
// Remove the FacebookActivity since we can rely on it in the androidsdk aar as of v4.12
// (otherwise unity manifest merge likes fail if there's any difference at all)
XmlElement facebookElement;
if (TryFindElementWithAndroidName(dict, FacebookActivityName, out facebookElement))
{
dict.RemoveChild(facebookElement);
}
// Save the document formatted
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
NewLineChars = "\r\n",
NewLineHandling = NewLineHandling.Replace
};
using (XmlWriter xmlWriter = XmlWriter.Create(fullPath, settings))
{
doc.Save(xmlWriter);
}
}
private static XmlNode FindChildNode(XmlNode parent, string name)
{
XmlNode curr = parent.FirstChild;
while (curr != null)
{
if (curr.Name.Equals(name))
{
return curr;
}
curr = curr.NextSibling;
}
return null;
}
private static void SetOrReplaceXmlElement(
XmlNode parent,
XmlElement newElement)
{
string attrNameValue = newElement.GetAttribute("name");
string elementType = newElement.Name;
XmlElement existingElment;
if (TryFindElementWithAndroidName(parent, attrNameValue, out existingElment, elementType))
{
parent.ReplaceChild(newElement, existingElment);
}
else
{
parent.AppendChild(newElement);
}
}
private static bool TryFindElementWithAndroidName(
XmlNode parent,
string attrNameValue,
out XmlElement element,
string elementType = "activity")
{
string ns = parent.GetNamespaceOfPrefix("android");
var curr = parent.FirstChild;
while (curr != null)
{
var currXmlElement = curr as XmlElement;
if (currXmlElement != null &&
currXmlElement.Name == elementType &&
currXmlElement.GetAttribute("name", ns) == attrNameValue)
{
element = currXmlElement;
return true;
}
curr = curr.NextSibling;
}
element = null;
return false;
}
private static void AddSimpleActivity(XmlDocument doc, XmlNode xmlNode, string ns, string className, bool export = false)
{
XmlElement element = CreateActivityElement(doc, ns, className, export);
ManifestMod.SetOrReplaceXmlElement(xmlNode, element);
}
private static XmlElement CreateUnityOverlayElement(XmlDocument doc, string ns, string activityName)
{
// <activity android:name="activityName" android:configChanges="all|of|them" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
// </activity>
XmlElement activityElement = ManifestMod.CreateActivityElement(doc, ns, activityName);
activityElement.SetAttribute("configChanges", ns, "fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen");
activityElement.SetAttribute("theme", ns, "@android:style/Theme.Translucent.NoTitleBar.Fullscreen");
return activityElement;
}
private static XmlElement CreateContentProviderElement(XmlDocument doc, string ns, string appId)
{
XmlElement provierElement = doc.CreateElement("provider");
provierElement.SetAttribute("name", ns, FacebookContentProviderName);
string authorities = string.Format(CultureInfo.InvariantCulture, FacebookContentProviderAuthFormat, appId);
provierElement.SetAttribute("authorities", ns, authorities);
provierElement.SetAttribute("exported", ns, "true");
return provierElement;
}
private static XmlElement CreateActivityElement(XmlDocument doc, string ns, string activityName, bool exported = false)
{
// <activity android:name="activityName" android:exported="true">
// </activity>
XmlElement activityElement = doc.CreateElement("activity");
activityElement.SetAttribute("name", ns, activityName);
if (exported)
{
activityElement.SetAttribute("exported", ns, "true");
}
return activityElement;
}
private static void AddAppLinkingActivity(XmlDocument doc, XmlNode xmlNode, string ns, List<string> schemes)
{
XmlElement element = ManifestMod.CreateActivityElement(doc, ns, AppLinkActivityName, true);
foreach (var scheme in schemes)
{
// We have to create an intent filter for each scheme since an intent filter
// can have only one data element.
XmlElement intentFilter = doc.CreateElement("intent-filter");
var action = doc.CreateElement("action");
action.SetAttribute("name", ns, "android.intent.action.VIEW");
intentFilter.AppendChild(action);
var category = doc.CreateElement("category");
category.SetAttribute("name", ns, "android.intent.category.DEFAULT");
intentFilter.AppendChild(category);
XmlElement dataElement = doc.CreateElement("data");
dataElement.SetAttribute("scheme", ns, scheme);
intentFilter.AppendChild(dataElement);
element.AppendChild(intentFilter);
}
ManifestMod.SetOrReplaceXmlElement(xmlNode, element);
}
private static void CreateDefaultAndroidManifest(string outputFile)
{
var inputFile = Path.Combine(
EditorApplication.applicationContentsPath,
"PlaybackEngines/androidplayer/AndroidManifest.xml");
if (!File.Exists(inputFile))
{
// Unity moved this file. Try to get it at its new location
inputFile = Path.Combine(
EditorApplication.applicationContentsPath,
"PlaybackEngines/AndroidPlayer/Apk/AndroidManifest.xml");
}
if (File.Exists(inputFile))
{
File.Copy(inputFile, outputFile);
return;
}
// On Unity 5.3+ we don't have default manifest so use our own
// manifest and warn the user that they may need to modify it manually
Assembly assembly = Assembly.GetExecutingAssembly();
Stream xmlStream = assembly.GetManifestResourceStream("Facebook.Unity.Editor.android.DefaultAndroidManifest.xml");
var xmlDocument = new XmlDocument();
xmlDocument.Load(xmlStream);
Debug.LogWarning(
string.Format(
"No existing android manifest found at '{0}'. Creating a default manifest file",
outputFile));
xmlDocument.Save(outputFile);
}
}
}