summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android.AppLinks/AndroidAppLinks.cs
blob: df9b4e9df4eb4562d6eaf8c00285dba213e63017 (plain)
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
using System;
using Android.Util;
using Android.App;
using Android.Content;
using Android.Gms.Tasks;
using Android.Runtime;
using Firebase.AppIndexing;
using Actions = Firebase.AppIndexing.Builders.Actions;
using GMSTask = Android.Gms.Tasks.Task;
using IndexingAction = Firebase.AppIndexing.IAction;

namespace Xamarin.Forms.Platform.Android.AppLinks
{
	[Preserve(AllMembers = true)]
	public class AndroidAppLinks : IAppLinks, IDisposable
	{
		bool _disposed;

		public static bool IsInitialized { get; private set; }

		public static Context Context { get; private set; }

		public static void Init(Activity activity)
		{
			if (IsInitialized)
				return;
			IsInitialized = true;

			Context = activity;
		}

		public AndroidAppLinks(Context context)
		{
		}

		public void DeregisterLink(IAppLinkEntry appLink)
		{
			RemoveFromIndexItemAsync(appLink.AppLinkUri.ToString());
		}

		public void DeregisterLink(Uri appLinkUri)
		{
			RemoveFromIndexItemAsync(appLinkUri.ToString());
		}

		public void RegisterLink(IAppLinkEntry appLink)
		{
			IndexItemAsync(appLink);

		}

		public void Dispose()
		{
			Dispose(true);
		}

		protected virtual void Dispose(bool isDisposing)
		{
			if (isDisposing && !_disposed)
			{
				_disposed = true;
			}
		}

		void IndexItemAsync(IAppLinkEntry appLink)
		{
			//IndexingAction indexAction = BuildIndexAction(appLink);
			var url = global::Android.Net.Uri.Parse(appLink.AppLinkUri.AbsoluteUri).ToString();
			IIndexable indexable = GetIndexable(appLink, url);
			IndexingAction indexAction = GetAction(appLink, url);
			/* If you’re logging an action on an item that has already been added to the index,
			 * you don’t have to add the following update line. See
			 * https://firebase.google.com/docs/app-indexing/android/personal-content#update-the-index for
			 * adding content to the index 
            */
			FirebaseAppIndex.Instance.Update(indexable);
			GMSTask gmsTask = FirebaseUserActions.Instance
												 .Start(indexAction)
												 .AddOnSuccessListener(Context as Activity,
																	   new AndroidActionSuccessListener(appLink as AppLinkEntry, indexAction))
												 .AddOnFailureListener(Context as Activity,
																	   new AndroidActionFailureListener(appLink as AppLinkEntry, indexAction));
		}

		void RemoveFromIndexItemAsync(string identifier)
		{
			FirebaseAppIndex.Instance.Remove(identifier);
		}

		IIndexable GetIndexable(IAppLinkEntry appLink, string url)
		{
			var indexableBuilder = new IndexableBuilder();
			indexableBuilder.SetName(appLink.Title);
			indexableBuilder.SetUrl(url);
			indexableBuilder.SetDescription(appLink.Description);
			return indexableBuilder.Build();
		}

		IndexingAction GetAction(IAppLinkEntry applink, string url)
		{
			return Actions.NewView(applink.Title, url);
		}

		internal class AndroidActionSuccessListener : Java.Lang.Object, IOnSuccessListener
		{
			readonly AppLinkEntry appLink;
			readonly IndexingAction indexAction;

			public AndroidActionSuccessListener(AppLinkEntry appLink, IndexingAction indexAction)
			{
				this.appLink = appLink;
				this.indexAction = indexAction;
			}

			public void OnSuccess(Java.Lang.Object result)
			{
				if (appLink != null)
				{
					appLink.PropertyChanged += (sender, e) =>
					{
						if (e.PropertyName == AppLinkEntry.IsLinkActiveProperty.PropertyName)
						{
							if (appLink.IsLinkActive)
							{
								FirebaseUserActions.Instance.Start(indexAction);
							}
							else
							{
								FirebaseUserActions.Instance.End(indexAction);
							}
						}
					};
				}
			}
		}
		internal class AndroidActionFailureListener : Java.Lang.Object, IOnFailureListener
		{
			readonly AppLinkEntry appLink;
			readonly IndexingAction indexAction;

			public AndroidActionFailureListener(AppLinkEntry appLink, IndexingAction indexAction)
			{
				this.appLink = appLink;
				this.indexAction = indexAction;
			}

			public void OnFailure(Java.Lang.Exception e)
			{
				Log.Error(this.Class.Name, e, $" [{DateTime.Now}] - [AndroidAppLinks Failure] - {e.Message}");
				throw e;
			}
		}
	}
}