Created
July 8, 2014 17:20
-
-
Save gmsetiawan/0ab20ff6188e3eceebf9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gmsetiawan.droidclass; | |
import java.util.Arrays; | |
import android.support.v7.app.ActionBarActivity; | |
import android.support.v4.app.Fragment; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import com.facebook.*; | |
import com.facebook.model.*; | |
import com.facebook.widget.LoginButton; | |
public class MainActivity extends ActionBarActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
if (savedInstanceState == null) { | |
getSupportFragmentManager().beginTransaction() | |
.add(R.id.container, new PlaceholderFragment()) | |
.commit(); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); | |
} | |
/** | |
* A placeholder fragment containing a simple view. | |
*/ | |
public static class PlaceholderFragment extends Fragment { | |
private static final String TAG = "PlaceholderFragment"; | |
private UiLifecycleHelper uiHelper; | |
public PlaceholderFragment() { | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View rootView = inflater.inflate(R.layout.fragment_main, container, false); | |
/*Session.openActiveSession(getActivity(), true, new Session.StatusCallback() { | |
@Override | |
public void call(Session session, SessionState state, Exception exception) { | |
// TODO Auto-generated method stub | |
if (session.isOpened()) { | |
Request.newMeRequest(session, new Request.GraphUserCallback() { | |
@Override | |
public void onCompleted( GraphUser user, Response response) { | |
// TODO Auto-generated method stub | |
if (user != null) { | |
TextView welcome = (TextView) getView().findViewById(R.id.welcome); | |
welcome.setText("Hello " + user.getName() + " " + user.getId() + "!"); | |
} | |
} | |
}).executeAsync(); | |
} | |
} | |
});*/ | |
LoginButton authButton = (LoginButton) rootView.findViewById(R.id.authButton); | |
authButton.setFragment(this); | |
authButton.setReadPermissions(Arrays.asList("user_likes", "user_status")); | |
Session session = Session.getActiveSession(); | |
if (session != null && | |
(session.isOpened() || session.isClosed()) ) { | |
onSessionStateChange(session, session.getState(), null); | |
} | |
uiHelper.onResume(); | |
return rootView; | |
} | |
private void onSessionStateChange(Session session, SessionState state, Exception exception) { | |
if (state.isOpened()) { | |
Log.i(TAG, "Logged in..."); | |
Request.newMeRequest(session, new Request.GraphUserCallback() { | |
@Override | |
public void onCompleted( GraphUser user, Response response) { | |
// TODO Auto-generated method stub | |
if (user != null) { | |
TextView welcome = (TextView) getView().findViewById(R.id.welcome); | |
welcome.setText("Hello " + user.getName() + " " + user.getId() + "!"); | |
} | |
} | |
}).executeAsync(); | |
} else if (state.isClosed()) { | |
Log.i(TAG, "Logged out..."); | |
TextView welcome = (TextView) getView().findViewById(R.id.welcome); | |
welcome.setText("Please Login"); | |
} | |
} | |
private Session.StatusCallback callback = new Session.StatusCallback() { | |
@Override | |
public void call(Session session, SessionState state, Exception exception) { | |
onSessionStateChange(session, state, exception); | |
} | |
}; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
// TODO Auto-generated method stub | |
super.onCreate(savedInstanceState); | |
uiHelper = new UiLifecycleHelper(getActivity(), callback); | |
uiHelper.onCreate(savedInstanceState); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
uiHelper.onResume(); | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
uiHelper.onActivityResult(requestCode, resultCode, data); | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
uiHelper.onPause(); | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
uiHelper.onDestroy(); | |
} | |
@Override | |
public void onSaveInstanceState(Bundle outState) { | |
super.onSaveInstanceState(outState); | |
uiHelper.onSaveInstanceState(outState); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment