summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android.AppLinks/AndroidAppLinks.cs
blob: 975e65fbab394bd176f0f9bfb3b1a861e43c1912 (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
using System;
using System.Threading.Tasks;
using Android.Content;
using Android.Gms.AppIndexing;
using Android.Gms.Common.Apis;
using Android.Runtime;
using IndexingAction = Android.Gms.AppIndexing.Action;
using Android.App;

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

		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)
		{
			_client = new GoogleApiClient.Builder(context).AddApi(AppIndex.API).Build();
			_client.Connect();
		}

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

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

		public async void RegisterLink(IAppLinkEntry appLink)
		{
			await IndexItemAsync(appLink);
		}

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

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

		static IndexingAction BuildIndexAction(IAppLinkEntry appLink)
		{
			Thing item = new Thing.Builder().SetName(appLink.Title).SetDescription(appLink.Description).SetUrl(global::Android.Net.Uri.Parse(appLink.AppLinkUri.AbsoluteUri)).Build();
			Thing thing = new IndexingAction.Builder(IndexingAction.TypeView).SetObject(item).SetActionStatus(IndexingAction.StatusTypeCompleted).Build();
			var indexAction = thing.JavaCast<IndexingAction>();
			return indexAction;
		}

		async Task IndexItemAsync(IAppLinkEntry appLink)
		{
			IndexingAction indexAction = BuildIndexAction(appLink);

			if (_client.IsConnected && appLink.IsLinkActive)
			{
				Statuses resultStart = await AppIndex.AppIndexApi.StartAsync(_client, indexAction);
				if (resultStart.IsSuccess)
				{
					var aL = appLink as AppLinkEntry;
					if (aL != null)
					{
						aL.PropertyChanged += async (sender, e) =>
						{
							if (e.PropertyName == AppLinkEntry.IsLinkActiveProperty.PropertyName)
							{
								if (appLink.IsLinkActive)
								{
									await AppIndex.AppIndexApi.StartAsync(_client, indexAction);
								}
								else
								{
									await AppIndex.AppIndexApi.EndAsync(_client, indexAction);
								}
							}
						};
					}
				}
			}
		}

		void RemoveFromIndexItemAsync(string identifier)
		{
			if (_client.IsConnected)
			{
			}
		}
	}
}