Created
November 15, 2013 17:01
-
-
Save DiegoSeC/7487787 to your computer and use it in GitHub Desktop.
Callback on Java
This file contains 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.nevacom.vigi.intface; | |
public interface Callback<T> { | |
public void invoke(T arg); | |
} |
This file contains 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
/* | |
* Mi capa que hará las consultas a facebook | |
*/ | |
package com.nevacom.vigi.dao; | |
import java.util.ArrayList; | |
import java.util.List; | |
import com.facebook.Request; | |
import com.facebook.Response; | |
import com.facebook.Session; | |
import com.facebook.model.GraphUser; | |
import com.nevacom.vigi.intface.Callback; | |
import com.nevacom.vigi.model.Friends; | |
public class FacebookDAO { | |
public void getFacebookFriends(final Callback<Friends> callback) { | |
Session session = Session.getActiveSession(); | |
Request.newMyFriendsRequest(session, new Request.GraphUserListCallback() { | |
@Override | |
public void onCompleted(List<GraphUser> users, Response response) { | |
// TODO Auto-generated method stub | |
ArrayList<Friends> friends = new ArrayList<Friends>(); | |
Friends f = new Friends(); | |
f.setName("funciona el callback"); | |
callback.invoke(f); | |
} | |
}).executeAsync(); | |
} | |
} |
This file contains 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.nevacom.vigi; | |
import java.util.ArrayList; | |
import com.nevacom.vigi.dao.FacebookDAO; | |
import com.nevacom.vigi.helpers.Util; | |
import com.nevacom.vigi.intface.Callback; | |
import com.nevacom.vigi.model.Friends; | |
import android.app.Activity; | |
import android.content.ContentResolver; | |
import android.database.Cursor; | |
import android.os.Bundle; | |
import android.provider.ContactsContract; | |
import android.view.Menu; | |
public class FriendActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_friend); | |
ArrayList<Friends> friendsPhone = this.readContacts(); | |
Util.Log("Facebook Friends!"); | |
// Llamamos a nuestra clase FacebookDAO | |
FacebookDAO facebook = new FacebookDAO(); | |
// Nuestro callback wiiii :) | |
facebook.getFacebookFriends(new Callback<Friends>() { | |
@Override | |
public void invoke(Friends arg) { | |
// TODO Auto-generated method stub | |
// Aquí recuperamos nuestra data y hacemos lo que se nos da la gana con eso :) | |
Util.Log(arg.getName()); | |
System.out.println("Progress" + arg.getName()); | |
} | |
}); | |
} | |
private ArrayList<Friends> readContacts() { | |
ContentResolver cr = getContentResolver(); | |
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); | |
ArrayList<Friends> friends = new ArrayList<Friends>(); | |
if(cur.getCount() > 0) { | |
while(cur.moveToNext()) { | |
int hasPhone = Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); | |
if(hasPhone > 0) { | |
Friends friend = new Friends(); | |
friend.setUid(cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID))); | |
friend.setName(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); | |
friend.setFacebook(0); | |
friends.add(friend); | |
} | |
} | |
} | |
return friends; | |
} | |
private ArrayList<Friends> readFacebookFriends() { | |
ArrayList<Friends> friends = new ArrayList<Friends>(); | |
return friends; | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.friend, menu); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment