summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android.AppLinks/AndroidAppLinks.cs
diff options
context:
space:
mode:
authorRui Marinho <me@ruimarinho.net>2017-10-09 21:25:45 +0100
committerKangho Hur <kangho.hur@samsung.com>2017-10-23 13:34:10 +0900
commitbc7d18c1da659f8e838d8fe65adcbd5429054e6b (patch)
tree1b4e9d499116fd3b534bb42e978e68674eb870d8 /Xamarin.Forms.Platform.Android.AppLinks/AndroidAppLinks.cs
parentcca40c55b80fc8e3b5dfee7f832936dc078cb72d (diff)
downloadxamarin-forms-bc7d18c1da659f8e838d8fe65adcbd5429054e6b.tar.gz
xamarin-forms-bc7d18c1da659f8e838d8fe65adcbd5429054e6b.tar.bz2
xamarin-forms-bc7d18c1da659f8e838d8fe65adcbd5429054e6b.zip
[Android] Applinks firebase 42 (#1107)
* Android AppLinks updated packages and refactor to comply with Firebase packages * made nested classes internal * removed notimplementedexception and added a Console log when on Failure * removed Firebase init method. Changed Console for Android's native Exception logging * formatted code styling with Visual Studio Community 2017 for Mac * [Android] Update nuspec and gallery * [Packages] Update android support packages for 25.4.0.2
Diffstat (limited to 'Xamarin.Forms.Platform.Android.AppLinks/AndroidAppLinks.cs')
-rw-r--r--Xamarin.Forms.Platform.Android.AppLinks/AndroidAppLinks.cs120
1 files changed, 80 insertions, 40 deletions
diff --git a/Xamarin.Forms.Platform.Android.AppLinks/AndroidAppLinks.cs b/Xamarin.Forms.Platform.Android.AppLinks/AndroidAppLinks.cs
index 975e65fb..df9b4e9d 100644
--- a/Xamarin.Forms.Platform.Android.AppLinks/AndroidAppLinks.cs
+++ b/Xamarin.Forms.Platform.Android.AppLinks/AndroidAppLinks.cs
@@ -1,19 +1,19 @@
using System;
-using System.Threading.Tasks;
+using Android.Util;
+using Android.App;
using Android.Content;
-using Android.Gms.AppIndexing;
-using Android.Gms.Common.Apis;
+using Android.Gms.Tasks;
using Android.Runtime;
-using IndexingAction = Android.Gms.AppIndexing.Action;
-using Android.App;
+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
{
- readonly GoogleApiClient _client;
-
bool _disposed;
public static bool IsInitialized { get; private set; }
@@ -31,8 +31,6 @@ namespace Xamarin.Forms.Platform.Android.AppLinks
public AndroidAppLinks(Context context)
{
- _client = new GoogleApiClient.Builder(context).AddApi(AppIndex.API).Build();
- _client.Connect();
}
public void DeregisterLink(IAppLinkEntry appLink)
@@ -45,9 +43,10 @@ namespace Xamarin.Forms.Platform.Android.AppLinks
RemoveFromIndexItemAsync(appLinkUri.ToString());
}
- public async void RegisterLink(IAppLinkEntry appLink)
+ public void RegisterLink(IAppLinkEntry appLink)
{
- await IndexItemAsync(appLink);
+ IndexItemAsync(appLink);
+
}
public void Dispose()
@@ -60,54 +59,95 @@ namespace Xamarin.Forms.Platform.Android.AppLinks
if (isDisposing && !_disposed)
{
_disposed = true;
- _client.Disconnect();
- _client.Dispose();
}
}
- static IndexingAction BuildIndexAction(IAppLinkEntry appLink)
+ void IndexItemAsync(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;
+ //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));
}
- async Task IndexItemAsync(IAppLinkEntry appLink)
+ void RemoveFromIndexItemAsync(string identifier)
{
- IndexingAction indexAction = BuildIndexAction(appLink);
+ FirebaseAppIndex.Instance.Remove(identifier);
+ }
- if (_client.IsConnected && appLink.IsLinkActive)
+ 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)
{
- Statuses resultStart = await AppIndex.AppIndexApi.StartAsync(_client, indexAction);
- if (resultStart.IsSuccess)
+ if (appLink != null)
{
- var aL = appLink as AppLinkEntry;
- if (aL != null)
+ appLink.PropertyChanged += (sender, e) =>
{
- aL.PropertyChanged += async (sender, e) =>
+ if (e.PropertyName == AppLinkEntry.IsLinkActiveProperty.PropertyName)
{
- if (e.PropertyName == AppLinkEntry.IsLinkActiveProperty.PropertyName)
+ if (appLink.IsLinkActive)
+ {
+ FirebaseUserActions.Instance.Start(indexAction);
+ }
+ else
{
- if (appLink.IsLinkActive)
- {
- await AppIndex.AppIndexApi.StartAsync(_client, indexAction);
- }
- else
- {
- await AppIndex.AppIndexApi.EndAsync(_client, indexAction);
- }
+ FirebaseUserActions.Instance.End(indexAction);
}
- };
- }
+ }
+ };
}
}
}
-
- void RemoveFromIndexItemAsync(string identifier)
+ internal class AndroidActionFailureListener : Java.Lang.Object, IOnFailureListener
{
- if (_client.IsConnected)
+ 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;
}
}
}