Guest User

Untitled

a guest
Jan 14th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 KB | None | 0 0
  1. android:id="@+id/main_layout"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:background="@drawable/Background2"
  5. android:orientation="vertical"
  6. android:weightSum="4">
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="0dp"
  10. android:layout_weight="3"
  11. android:gravity="center_horizontal"
  12. android:orientation="vertical">
  13. <ImageView
  14. android:id="@+id/icon"
  15. style="@style/ThemeOverlay.FirebaseIcon"
  16. android:contentDescription="@string/desc_firebase_lockup"
  17. android:src="@drawable/firebase_lockup_400"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content" />
  20. <Button
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:layout_centerInParent="true"
  24. android:text="@string/sign_out"
  25. android:theme="@style/ThemeOverlay.MyDarkButton"
  26. android:visibility="gone"
  27. android:id="@+id/button1" />
  28. <TextView
  29. android:id="@+id/title_text"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_marginBottom="@dimen/title_bottom_margin"
  33. android:text="@string/facebook_title_text"
  34. android:theme="@style/ThemeOverlay.MyTitleText" />
  35. <TextView
  36. android:id="@+id/status"
  37. style="@style/ThemeOverlay.MyTextDetail"
  38. android:text="@string/signed_out" />
  39. <TextView
  40. android:id="@+id/detail"
  41. style="@style/ThemeOverlay.MyTextDetail"
  42. tools:text="Firebase User ID: 123456789abc" />
  43. <TextView
  44. android:text="First Name"
  45. android:textAppearance="?android:attr/textAppearanceMedium"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:id="@+id/TxtFirstname"
  49. android:layout_centerHorizontal="true"
  50. android:layout_gravity="center_horizontal"
  51. android:layout_marginTop="20dp" />
  52. <TextView
  53. android:text="Last Name"
  54. android:textAppearance="?android:attr/textAppearanceMedium"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:id="@+id/TxtLastName"
  58. android:layout_centerHorizontal="true"
  59. android:layout_gravity="center_horizontal"
  60. android:layout_marginTop="10dp" />
  61. <TextView
  62. android:text="User Name"
  63. android:textAppearance="?android:attr/textAppearanceMedium"
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:id="@+id/TxtName"
  67. android:layout_centerHorizontal="true"
  68. android:layout_gravity="center_horizontal"
  69. android:layout_marginTop="10dp" />
  70. </LinearLayout>
  71. <RelativeLayout
  72. android:layout_width="fill_parent"
  73. android:layout_height="0dp"
  74. android:layout_weight="1"
  75. android:background="@color/grey_300">
  76. <com.facebook.login.widget.LoginButton
  77. android:id="@+id/button_facebook_login"
  78. android:layout_width="wrap_content"
  79. android:layout_height="wrap_content"
  80. android:layout_centerInParent="true" />
  81. <com.facebook.login.widget.ProfilePictureView
  82. android:id="@+id/ImgPro"
  83. android:layout_width="150dp"
  84. android:layout_height="150dp"
  85. android:layout_marginTop="20dp"
  86. android:layout_gravity="center_horizontal" />
  87. <Button
  88. android:id="@+id/button_facebook_signout"
  89. android:layout_width="wrap_content"
  90. android:layout_height="wrap_content"
  91. android:layout_centerInParent="true"
  92. android:text="@string/sign_out"
  93. android:theme="@style/ThemeOverlay.MyDarkButton"
  94. android:visibility="gone" />
  95. </RelativeLayout>
  96.  
  97. mprofileTracker = new MyProfileTracker();
  98. mprofileTracker.mOnProfileChanged += FirebaseUser;
  99. mprofileTracker.StartTracking();
  100. SetContentView(Resource.Layout.activity_facebook);
  101. var button1 = FindViewById<Button>(Resource.Id.button1);
  102.  
  103. // Views
  104. TxtFirstName = FindViewById<TextView>(Resource.Id.TxtFirstname);
  105. TxtLastName = FindViewById<TextView>(Resource.Id.TxtLastName);
  106. TxtName = FindViewById<TextView>(Resource.Id.TxtName);
  107. mprofilepic = FindViewById<ProfilePictureView>(Resource.Id.ImgPro);
  108. mStatusTextView = FindViewById<TextView>(Resource.Id.status);
  109. mDetailTextView = FindViewById<TextView>(Resource.Id.detail);
  110. // Button listeners
  111. FirebaseApp.InitializeApp(this);
  112. mAuth = FirebaseAuth.Instance;
  113. // mprofilepic = FindViewById<ProfilePictureView>(Resource.Id.ImgPro);
  114. LoginButton fblogin = FindViewById<LoginButton>(Resource.Id.button_facebook_login);
  115. fblogin.Click += delegate
  116. {
  117. mCallbackManager = CallbackManagerFactory.Create();
  118. fblogin.RegisterCallback(mCallbackManager, this);
  119.  
  120. };
  121.  
  122. FindViewById(Resource.Id.button_facebook_signout).Click += delegate
  123. {
  124. SignOut();
  125. };
  126. }
  127. void FirebaseUser(object sender, OnFirebaseChangedEventArgs e)
  128. {
  129. if (e.FirebaseUser != null)
  130. {
  131. try
  132. {
  133. TxtFirstName.Text = e.FirebaseUser.FirstName;
  134. TxtLastName.Text = e.FirebaseUser.LastName;
  135. TxtName.Text = e.FirebaseUser.Name;
  136. mprofilepic.ProfileId = e.FirebaseUser.Id;
  137. }
  138. catch (Java.Lang.Exception ex)
  139. {
  140.  
  141. }
  142. }
  143.  
  144. else
  145. {
  146. TxtFirstName.Text = "First Name";
  147. TxtLastName.Text = "Last Name";
  148. TxtName.Text = "Name";
  149. mprofilepic.ProfileId = null;
  150. }
  151. }
  152.  
  153.  
  154. void AuthStateChanged(object sender, FirebaseAuth.AuthStateEventArgs e)
  155. {
  156. var user = e.Auth.CurrentUser;
  157. if (user != null)
  158. {
  159. // User is signed in
  160. Android.Util.Log.Debug(TAG, "onAuthStateChanged:signed_in:" + user.Uid);
  161. }
  162. else
  163. {
  164. // User is signed out
  165. Android.Util.Log.Debug(TAG, "onAuthStateChanged:signed_out");
  166. }
  167. // [START_EXCLUDE]
  168.  
  169. // [END_EXCLUDE]
  170. }
  171.  
  172. // [START on_start_add_listener]
  173. protected override void OnStart()
  174. {
  175. base.OnStart();
  176. mAuth.AuthState += AuthStateChanged;
  177. }
  178. // [END on_start_add_listener]
  179.  
  180. //[START on_stop_remove_listener]
  181. protected override void OnStop()
  182. {
  183. base.OnStop();
  184. mAuth.AuthState -= AuthStateChanged;
  185. }
  186. // [END on_stop_remove_listener]
  187. protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
  188. {
  189. base.OnActivityResult(requestCode, resultCode, data);
  190. var resultCodeNum = 0;
  191. switch (resultCode)
  192. {
  193. case Result.Ok:
  194. resultCodeNum = -1;
  195. break;
  196.  
  197. case Result.Canceled:
  198. resultCodeNum = 0;
  199. break;
  200.  
  201. case Result.FirstUser:
  202. resultCodeNum = 1;
  203. break;
  204. }
  205. mCallbackManager.OnActivityResult(requestCode, resultCodeNum, data);
  206. }
  207.  
  208.  
  209. private void handleFacebookAccessToken(AccessToken accessToken)
  210. {
  211.  
  212. AuthCredential credential = FacebookAuthProvider.GetCredential(accessToken.Token);
  213. mAuth.SignInWithCredential(credential).AddOnCompleteListener(this, this);
  214. }
  215. private void SignOut()
  216. {
  217. // Firebase sign out
  218. mAuth.SignOut();
  219.  
  220.  
  221. }
  222.  
  223.  
  224. public void OnCancel()
  225. {
  226.  
  227. }
  228.  
  229.  
  230.  
  231. public void OnError(FacebookException error)
  232. {
  233. throw new NotImplementedException();
  234. }
  235.  
  236. public void OnSuccess(Java.Lang.Object result)
  237. {
  238. LoginResult loginResult = result as LoginResult;
  239. handleFacebookAccessToken(loginResult.AccessToken);
  240. }
  241.  
  242. public void OnComplete(Android.Gms.Tasks.Task task)
  243. {
  244. if (task.IsSuccessful)
  245. {
  246. FirebaseUser user = mAuth.CurrentUser;
  247. }
  248. else
  249. {
  250. Toast.MakeText(this, "Authentication failed.", ToastLength.Short).Show();
  251. }
  252.  
  253. }
  254.  
  255. public void OnCompleted(Org.Json.JSONObject json, GraphResponse response)
  256. {
  257. string data = json.ToString();
  258. FacebookResult result = JsonConvert.DeserializeObject<FacebookResult>(data);
  259. }
  260.  
  261. public class MyProfileTracker : ProfileTracker
  262. {
  263. public event EventHandler<OnFirebaseChangedEventArgs> mOnProfileChanged;
  264.  
  265. protected override void OnCurrentProfileChanged(Profile oldProfile, Profile newProfile)
  266. {
  267. if (mOnProfileChanged != null)
  268. {
  269. mOnProfileChanged.Invoke(this, new OnFirebaseChangedEventArgs(newProfile));
  270. }
  271. }
  272. }
  273. public class OnFirebaseChangedEventArgs : EventArgs
  274. {
  275. public Profile FirebaseUser;
  276.  
  277. public OnFirebaseChangedEventArgs(Profile profile) { FirebaseUser = profile; }
  278. }
  279. }
Add Comment
Please, Sign In to add comment