Advertisement
Guest User

Untitled

a guest
May 24th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. LoginFragment loginFragment = new LoginFragment();
  8. FacebookSdk.sdkInitialize(this.getApplicationContext());
  9.  
  10.  
  11. FragmentManager fragmentManager = getFragmentManager();
  12. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  13. fragmentTransaction.add(R.id.fragment_right_container, loginFragment);
  14. fragmentTransaction.commit();
  15.  
  16. SessionManager sessionManager=new SessionManager();
  17. Log.d("Is Logged", "" + "" + AccessToken.getCurrentAccessToken());
  18.  
  19.  
  20.  
  21. /* if(sessionManager.isLoggedIn()){
  22. Toast.makeText(this,"Logueado!",Toast.LENGTH_LONG).show();
  23. }*/
  24.  
  25. }
  26. }
  27.  
  28. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  29. xmlns:tools="http://schemas.android.com/tools"
  30. android:layout_width="match_parent"
  31. android:layout_height="match_parent"
  32. tools:context=".LoginFragment">
  33.  
  34. <!-- TODO: Update blank fragment layout -->
  35. <TextView
  36. android:id="@+id/text_details"
  37. android:layout_width="match_parent"
  38. android:layout_height="match_parent"
  39. android:text=""
  40. />
  41.  
  42. <com.facebook.login.widget.LoginButton
  43. android:id="@+id/login_button"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:layout_gravity="center_horizontal"
  47. android:layout_marginTop="30dp"
  48. android:layout_marginBottom="30dp" />
  49.  
  50. </FrameLayout>
  51.  
  52. public class LoginFragment extends Fragment {
  53.  
  54. private TextView textDetails;
  55.  
  56. private CallbackManager callbackManager;
  57.  
  58. private AccessTokenTracker tokenTracker;
  59. private ProfileTracker profileTracker;
  60.  
  61. FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
  62. @Override
  63. public void onSuccess(LoginResult loginResult) {
  64. Log.d("Facebook-Callback", "onSuccess");
  65. AccessToken accessToken = loginResult.getAccessToken();
  66. Profile profile = Profile.getCurrentProfile();
  67. displayWelcomeMessage(profile);
  68.  
  69.  
  70.  
  71. }
  72.  
  73. @Override
  74. public void onCancel() {
  75. Log.d("Facebook-Cancel", "onCancel");
  76. }
  77.  
  78. @Override
  79. public void onError(FacebookException error) {
  80. Log.d("Facebook-Error", "onCancel");
  81. }
  82. };
  83.  
  84. public LoginFragment() {
  85. // Required empty public constructor
  86. }
  87.  
  88.  
  89. @Override
  90. public void onCreate(Bundle savedInstanceState) {
  91. super.onCreate(savedInstanceState);
  92. callbackManager=CallbackManager.Factory.create();
  93.  
  94. setupTokenTracker();
  95. setupProfileTracker();
  96.  
  97. tokenTracker.startTracking();
  98. profileTracker.startTracking();
  99.  
  100. }
  101.  
  102.  
  103.  
  104. @Override
  105. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  106. Bundle savedInstanceState) {
  107. // Inflate the layout for this fragment
  108. return inflater.inflate(R.layout.fragment_main, container, false);
  109. }
  110.  
  111. @Override
  112. public void onViewCreated(View view, Bundle savedInstanceState) {
  113. super.onViewCreated(view, savedInstanceState);
  114. setupTextDetails(view);
  115. setupLoginButton(view);
  116. }
  117.  
  118. @Override
  119. public void onResume() {
  120. super.onResume();
  121. Profile profile=Profile.getCurrentProfile();
  122. displayWelcomeMessage(profile);
  123. }
  124.  
  125. @Override
  126. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  127. super.onActivityResult(requestCode, resultCode, data);
  128. callbackManager.onActivityResult(requestCode, resultCode, data);
  129. }
  130.  
  131.  
  132. @Override
  133. public void onStop() {
  134. super.onStop();
  135. tokenTracker.stopTracking();
  136. profileTracker.stopTracking();
  137. }
  138.  
  139.  
  140. //funciones de usuario
  141.  
  142. private void displayWelcomeMessage(Profile profile){
  143.  
  144. if(SessionManager.isLoggedIn()){
  145. textDetails.setText("Logged "+profile.getName());
  146.  
  147. }else{
  148. textDetails.setText("Logged out");
  149. }
  150.  
  151. }
  152.  
  153.  
  154.  
  155. private void setupTextDetails(View view) {
  156. textDetails = (TextView) view.findViewById(R.id.text_details);
  157. }
  158.  
  159.  
  160. private void setupLoginButton(View view) {
  161. LoginButton buttonLogin = (LoginButton) view.findViewById(R.id.login_button);
  162. buttonLogin.setFragment(this);
  163. //I set up the permissions I want to ask for
  164. buttonLogin.setReadPermissions("user_friends");
  165. buttonLogin.registerCallback(callbackManager, callback);
  166. }
  167.  
  168. //it's the callback function that listens everytime the profile of the user changes(change profile pic or whatever)
  169. private void setupProfileTracker() {
  170.  
  171. profileTracker= new ProfileTracker() {
  172. @Override
  173. protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
  174. Log.d("New Profile ", "" + newProfile);
  175. displayWelcomeMessage(newProfile);
  176.  
  177. }
  178.  
  179. };
  180. }
  181.  
  182. //it's the callback function that listens everytime the token changes(When I lost connection I receive another token for example and the nthis function executes)
  183. private void setupTokenTracker() {
  184.  
  185. tokenTracker= new AccessTokenTracker() {
  186. @Override
  187. protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
  188. //log in the console the new token
  189. Log.d("New token for User ", "" + newToken);
  190. }
  191. };
  192.  
  193. }
  194.  
  195.  
  196. }
  197.  
  198. public class SessionManager {
  199. //check if it'slogged in to facebook
  200.  
  201. String currentAccessToken=null;
  202.  
  203. public void setAccessToken(String newToken){
  204.  
  205. currentAccessToken=newToken;
  206.  
  207. }
  208.  
  209. public static boolean isLoggedIn() {
  210. AccessToken accessToken = AccessToken.getCurrentAccessToken();
  211. //Log.d("access token facebook",""+accessToken);
  212. return accessToken != null;
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement