Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- android:id="@+id/main_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/Background2"
- android:orientation="vertical"
- android:weightSum="4">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="3"
- android:gravity="center_horizontal"
- android:orientation="vertical">
- <ImageView
- android:id="@+id/icon"
- style="@style/ThemeOverlay.FirebaseIcon"
- android:contentDescription="@string/desc_firebase_lockup"
- android:src="@drawable/firebase_lockup_400"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="@string/sign_out"
- android:theme="@style/ThemeOverlay.MyDarkButton"
- android:visibility="gone"
- android:id="@+id/button1" />
- <TextView
- android:id="@+id/title_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="@dimen/title_bottom_margin"
- android:text="@string/facebook_title_text"
- android:theme="@style/ThemeOverlay.MyTitleText" />
- <TextView
- android:id="@+id/status"
- style="@style/ThemeOverlay.MyTextDetail"
- android:text="@string/signed_out" />
- <TextView
- android:id="@+id/detail"
- style="@style/ThemeOverlay.MyTextDetail"
- tools:text="Firebase User ID: 123456789abc" />
- <TextView
- android:text="First Name"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/TxtFirstname"
- android:layout_centerHorizontal="true"
- android:layout_gravity="center_horizontal"
- android:layout_marginTop="20dp" />
- <TextView
- android:text="Last Name"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/TxtLastName"
- android:layout_centerHorizontal="true"
- android:layout_gravity="center_horizontal"
- android:layout_marginTop="10dp" />
- <TextView
- android:text="User Name"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/TxtName"
- android:layout_centerHorizontal="true"
- android:layout_gravity="center_horizontal"
- android:layout_marginTop="10dp" />
- </LinearLayout>
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:background="@color/grey_300">
- <com.facebook.login.widget.LoginButton
- android:id="@+id/button_facebook_login"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true" />
- <com.facebook.login.widget.ProfilePictureView
- android:id="@+id/ImgPro"
- android:layout_width="150dp"
- android:layout_height="150dp"
- android:layout_marginTop="20dp"
- android:layout_gravity="center_horizontal" />
- <Button
- android:id="@+id/button_facebook_signout"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="@string/sign_out"
- android:theme="@style/ThemeOverlay.MyDarkButton"
- android:visibility="gone" />
- </RelativeLayout>
- mprofileTracker = new MyProfileTracker();
- mprofileTracker.mOnProfileChanged += FirebaseUser;
- mprofileTracker.StartTracking();
- SetContentView(Resource.Layout.activity_facebook);
- var button1 = FindViewById<Button>(Resource.Id.button1);
- // Views
- TxtFirstName = FindViewById<TextView>(Resource.Id.TxtFirstname);
- TxtLastName = FindViewById<TextView>(Resource.Id.TxtLastName);
- TxtName = FindViewById<TextView>(Resource.Id.TxtName);
- mprofilepic = FindViewById<ProfilePictureView>(Resource.Id.ImgPro);
- mStatusTextView = FindViewById<TextView>(Resource.Id.status);
- mDetailTextView = FindViewById<TextView>(Resource.Id.detail);
- // Button listeners
- FirebaseApp.InitializeApp(this);
- mAuth = FirebaseAuth.Instance;
- // mprofilepic = FindViewById<ProfilePictureView>(Resource.Id.ImgPro);
- LoginButton fblogin = FindViewById<LoginButton>(Resource.Id.button_facebook_login);
- fblogin.Click += delegate
- {
- mCallbackManager = CallbackManagerFactory.Create();
- fblogin.RegisterCallback(mCallbackManager, this);
- };
- FindViewById(Resource.Id.button_facebook_signout).Click += delegate
- {
- SignOut();
- };
- }
- void FirebaseUser(object sender, OnFirebaseChangedEventArgs e)
- {
- if (e.FirebaseUser != null)
- {
- try
- {
- TxtFirstName.Text = e.FirebaseUser.FirstName;
- TxtLastName.Text = e.FirebaseUser.LastName;
- TxtName.Text = e.FirebaseUser.Name;
- mprofilepic.ProfileId = e.FirebaseUser.Id;
- }
- catch (Java.Lang.Exception ex)
- {
- }
- }
- else
- {
- TxtFirstName.Text = "First Name";
- TxtLastName.Text = "Last Name";
- TxtName.Text = "Name";
- mprofilepic.ProfileId = null;
- }
- }
- void AuthStateChanged(object sender, FirebaseAuth.AuthStateEventArgs e)
- {
- var user = e.Auth.CurrentUser;
- if (user != null)
- {
- // User is signed in
- Android.Util.Log.Debug(TAG, "onAuthStateChanged:signed_in:" + user.Uid);
- }
- else
- {
- // User is signed out
- Android.Util.Log.Debug(TAG, "onAuthStateChanged:signed_out");
- }
- // [START_EXCLUDE]
- // [END_EXCLUDE]
- }
- // [START on_start_add_listener]
- protected override void OnStart()
- {
- base.OnStart();
- mAuth.AuthState += AuthStateChanged;
- }
- // [END on_start_add_listener]
- //[START on_stop_remove_listener]
- protected override void OnStop()
- {
- base.OnStop();
- mAuth.AuthState -= AuthStateChanged;
- }
- // [END on_stop_remove_listener]
- protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
- {
- base.OnActivityResult(requestCode, resultCode, data);
- var resultCodeNum = 0;
- switch (resultCode)
- {
- case Result.Ok:
- resultCodeNum = -1;
- break;
- case Result.Canceled:
- resultCodeNum = 0;
- break;
- case Result.FirstUser:
- resultCodeNum = 1;
- break;
- }
- mCallbackManager.OnActivityResult(requestCode, resultCodeNum, data);
- }
- private void handleFacebookAccessToken(AccessToken accessToken)
- {
- AuthCredential credential = FacebookAuthProvider.GetCredential(accessToken.Token);
- mAuth.SignInWithCredential(credential).AddOnCompleteListener(this, this);
- }
- private void SignOut()
- {
- // Firebase sign out
- mAuth.SignOut();
- }
- public void OnCancel()
- {
- }
- public void OnError(FacebookException error)
- {
- throw new NotImplementedException();
- }
- public void OnSuccess(Java.Lang.Object result)
- {
- LoginResult loginResult = result as LoginResult;
- handleFacebookAccessToken(loginResult.AccessToken);
- }
- public void OnComplete(Android.Gms.Tasks.Task task)
- {
- if (task.IsSuccessful)
- {
- FirebaseUser user = mAuth.CurrentUser;
- }
- else
- {
- Toast.MakeText(this, "Authentication failed.", ToastLength.Short).Show();
- }
- }
- public void OnCompleted(Org.Json.JSONObject json, GraphResponse response)
- {
- string data = json.ToString();
- FacebookResult result = JsonConvert.DeserializeObject<FacebookResult>(data);
- }
- public class MyProfileTracker : ProfileTracker
- {
- public event EventHandler<OnFirebaseChangedEventArgs> mOnProfileChanged;
- protected override void OnCurrentProfileChanged(Profile oldProfile, Profile newProfile)
- {
- if (mOnProfileChanged != null)
- {
- mOnProfileChanged.Invoke(this, new OnFirebaseChangedEventArgs(newProfile));
- }
- }
- }
- public class OnFirebaseChangedEventArgs : EventArgs
- {
- public Profile FirebaseUser;
- public OnFirebaseChangedEventArgs(Profile profile) { FirebaseUser = profile; }
- }
- }
Add Comment
Please, Sign In to add comment