Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Improve Android sample
Browse files Browse the repository at this point in the history
  • Loading branch information
ermau committed Aug 21, 2013
1 parent 856941a commit b43fea8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 deletions.
33 changes: 20 additions & 13 deletions samples/Xamarin.Auth.Sample.Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,45 @@ void LoginToFacebook (bool allowCancel)
// If authorization succeeds or is canceled, .Completed will be fired.
auth.Completed += (s, ee) => {
if (!ee.IsAuthenticated) {
this.facebookStatus.Text = "Not Authenticated";
var builder = new AlertDialog.Builder (this);
builder.SetMessage ("Not Authenticated");
builder.SetPositiveButton ("Ok", (o, e) => { });
builder.Create().Show();
return;
}

// Now that we're logged in, make a OAuth2 request to get the user's info.
var request = new OAuth2Request ("GET", new Uri ("https://graph.facebook.com/me"), null, ee.Account);
request.GetResponseAsync().ContinueWith (t => {
if (t.IsFaulted)
this.facebookStatus.Text = "Error: " + t.Exception.InnerException.Message;
else if (t.IsCanceled)
this.facebookStatus.Text = "Canceled";
else
{
var builder = new AlertDialog.Builder (this);
if (t.IsFaulted) {
builder.SetTitle ("Error");
builder.SetMessage (t.Exception.Flatten().InnerException.ToString());
} else if (t.IsCanceled)
builder.SetTitle ("Task Canceled");
else {
var obj = JsonValue.Parse (t.Result.GetResponseText());
this.facebookStatus.Text = "Logged in as " + obj["name"];

builder.SetTitle ("Logged in");
builder.SetMessage ("Name: " + obj["name"]);
}
}, uiScheduler);

builder.SetPositiveButton ("Ok", (o, e) => { });
builder.Create().Show();
}, UIScheduler);
};

var intent = auth.GetUI (this);
StartActivityForResult (intent, 42);
StartActivity (intent);
}

private TextView facebookStatus;
private readonly TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
private static readonly TaskScheduler UIScheduler = TaskScheduler.FromCurrentSynchronizationContext();

protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);

this.facebookStatus = FindViewById<TextView> (Resource.Id.FacebookTextView);
var facebook = FindViewById<Button> (Resource.Id.FacebookButton);
facebook.Click += delegate { LoginToFacebook(true);};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="Xamarin.Auth.Sample.Android">
<uses-sdk android:minSdkVersion="7" />
<application android:label="Xamarin.Auth Sample (Android)">
</application>
<uses-sdk android:minSdkVersion="8" />
<application android:label="Xamarin.Auth Sample (Android)"></application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
23 changes: 10 additions & 13 deletions samples/Xamarin.Auth.Sample.Android/Resources/Resource.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,4 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/FacebookButtonNoCancel" />
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/FacebookTextView" />
</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<AndroidApplication>True</AndroidApplication>
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<AssemblyName>Xamarin.Auth.Sample.Android</AssemblyName>
<TargetFrameworkVersion>v4.0.3</TargetFrameworkVersion>
<TargetFrameworkVersion>v2.2</TargetFrameworkVersion>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down

0 comments on commit b43fea8

Please sign in to comment.