Advertisement
skiddhard

TagFriends.java

May 31st, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.45 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import org.json.JSONArray;
  3. import org.json.JSONObject;
  4.  
  5. import com.actionbarsherlock.app.ActionBar;
  6. import com.actionbarsherlock.app.SherlockActivity;
  7. import com.dzinesunlimited.sociallyyou.BaseRequestListener;
  8. import com.dzinesunlimited.sociallyyou.R;
  9. import com.dzinesunlimited.sociallyyou.UsersProfile;
  10. import com.dzinesunlimited.sociallyyou.Utility;
  11. import com.facebook.sdk.FacebookError;
  12. import android.app.ProgressDialog;
  13. import android.content.Intent;
  14. import android.graphics.PixelFormat;
  15. import android.os.Bundle;
  16. import android.os.StrictMode;
  17. import android.text.Editable;
  18. import android.text.TextWatcher;
  19. import android.view.Window;
  20. import android.view.WindowManager;
  21. import android.widget.EditText;
  22. import android.widget.GridView;
  23. import android.widget.Toast;
  24.  
  25. public class TagFriends extends SherlockActivity {
  26.  
  27.     ProgressDialog dialog;
  28.     protected String graph_or_fql;
  29.     private JSONArray JAFriends;
  30.     JSONObject json_data;
  31.    
  32.     protected JSONArray jsonArray;
  33.    
  34.     TagFriendsAdapter adapter;
  35.    
  36.     @Override
  37.     protected void onCreate(Bundle savedInstanceState) {
  38.         super.onCreate(savedInstanceState);
  39.        
  40.         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  41.         setProgressBarIndeterminateVisibility(true);
  42.        
  43.         setContentView(R.layout.tag_friends_list);
  44.        
  45.         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  46.         StrictMode.setThreadPolicy(policy);
  47.        
  48.         ActionBar actionBar = getSupportActionBar();
  49.         actionBar.setDisplayHomeAsUpEnabled(true);
  50.         actionBar.setDisplayShowHomeEnabled(true);
  51.         actionBar.setDisplayShowTitleEnabled(true);
  52.         actionBar.setTitle("Friends List");
  53.        
  54.         // Hide the On-screen Keyboard when loading the activity
  55.         hideKeyboard();
  56.        
  57.         ArrayList<Friends> listOfFriends = getFriends();
  58.        
  59.         final GridView grid = (GridView) findViewById(R.id.gridFriends);
  60.        
  61.         adapter = new TagFriendsAdapter(this, listOfFriends);
  62.        
  63.         grid.setAdapter(adapter);
  64.        
  65.         setProgressBarIndeterminateVisibility(false);
  66.        
  67.         EditText filterText = (EditText) findViewById(R.id.editFilterList);
  68.         filterText.addTextChangedListener(filterTextWatcher);
  69.        
  70.     }
  71.    
  72.    
  73.     private TextWatcher filterTextWatcher = new TextWatcher() {
  74.        
  75.         @Override
  76.         public void onTextChanged(CharSequence s, int start, int before, int count) {
  77.            
  78.             adapter.getFilter().filter(s.toString().toLowerCase());
  79.             adapter.notifyDataSetChanged();
  80.         }
  81.        
  82.         @Override
  83.         public void beforeTextChanged(CharSequence s, int start, int count,
  84.                 int after) {
  85.            
  86.         }
  87.        
  88.         @Override
  89.         public void afterTextChanged(Editable s) {
  90.            
  91.         }
  92.     };
  93.    
  94.     private ArrayList<Friends> getFriends() {
  95.        
  96.         ArrayList<Friends> results = new ArrayList<TagFriends.Friends>();
  97.         Friends friends = new Friends();
  98.        
  99.         try {
  100.             // MAKE THE CALL TO THE FQL TABLE
  101.             graph_or_fql = "fql";
  102.             String query = "select name, uid, pic_big from user where uid in (select uid2 from friend where uid1=me()) order by name";
  103.             Bundle params = new Bundle();
  104.             params.putString("method", "fql.query");
  105.             params.putString("query", query);
  106.             String response = Utility.mFacebook.request(params);
  107. //          Log.e("response", response);
  108.            
  109.             JAFriends = new JSONArray(response);
  110. //          Log.e("JAFriends", JAFriends.toString());
  111.            
  112.             for (int i = 0; i < JAFriends.length(); i++) {
  113.                 json_data = JAFriends.getJSONObject(i);
  114. //              Log.d("json_data", json_data.toString());
  115.                
  116.                 friends = new Friends();
  117.                
  118.                 if (json_data.has("uid"))   {
  119.                     String getFriendID = json_data.getString("uid");
  120.                     friends.setID(getFriendID);
  121. //                  Log.d("ID", friends.getID());
  122.                 } else {
  123.                     String getFriendID = null;
  124. //                  Log.d("ID", getFriendID);
  125.                 }
  126.                
  127.                
  128.                 if (json_data.has("name"))  {
  129.                     String getFriendName = json_data.getString("name");
  130.                     friends.setName(getFriendName);
  131. //                  Log.e("NAME", friends.getName());
  132.                 } else {
  133.                     String getFriendName = null;
  134. //                  Log.e("NAME", getFriendName);
  135.                 }
  136.                
  137.                
  138.                 if (json_data.has("pic_big"))   {
  139.                     String getFriendPhoto = json_data.getString("pic_big");
  140.                     friends.setPicture(getFriendPhoto);
  141. //                  Log.e("COVER", friends.getPicture());
  142.                 } else {
  143.                     String getFriendPhoto = null;
  144. //                  Log.e("COVER", getFriendPhoto);
  145.                 }
  146.                 results.add(friends);
  147.             }
  148.            
  149. //          Utility.mAsyncRunner.request(null, params, new GetFriendsForTagging());
  150.         } catch (Exception e) {
  151.             e.printStackTrace();
  152.         }
  153.        
  154.         return results;
  155.        
  156.     }
  157.    
  158.     class Friends   {
  159.         private String userID = "";
  160.         private String userName = "";
  161.         private String userPicture = "";
  162.        
  163.         // USER ID
  164.         public void setID(String userID)    {
  165.             this.userID = userID;
  166.         }
  167.        
  168.         public String getID()   {
  169.             return userID;
  170.         }
  171.        
  172.         // USER NAME
  173.         public void setName(String userName)    {
  174.             this.userName = userName;
  175.         }
  176.        
  177.         public String getName() {
  178.             return userName;
  179.         }
  180.        
  181.         // USER PICTURE
  182.         public void setPicture(String userPicture)  {
  183.             this.userPicture = userPicture;
  184.         }
  185.        
  186.         public String getPicture()  {
  187.             return userPicture;
  188.         }
  189.        
  190.        
  191.     }
  192.    
  193.    
  194.     public class ProfileListener extends BaseRequestListener {
  195.  
  196.         private String userID;
  197.         private String userName;
  198.         private String userProfilePicture;
  199.         private String userRelationshipStatus;
  200.         private String userSignificantOtherName;
  201.         private String userSignificantOtherID;
  202.         private String userSignificantOtherPic;
  203.         private String userBirthDay;
  204.         private String userGender;
  205.         private String userLanguages;
  206.         private String userLink;
  207.         private String userEmail;
  208.         private String userWebsite;
  209.         private String userBio;
  210.         private StringBuilder langBuilder;
  211.  
  212.         @Override
  213.         public void onComplete(final String response, final Object state) {
  214. //          Log.e("PROFILE DATA", response);
  215.            
  216.             try {
  217.                 JSONObject jsonObject = new JSONObject(response);
  218.                
  219.                 if (jsonObject.has("id"))   {
  220.                     userID = jsonObject.getString("id");
  221. //                  Log.v("ID", userID);
  222.                 } else {
  223.                     userID = null;
  224. //                  Log.v("ID", userID);
  225.                 }
  226.                
  227.                 if (jsonObject.has("name")) {
  228.                     userName = jsonObject.getString("name");
  229. //                  Log.v("NAME", userName);
  230.                 } else {
  231.                     userName = null;
  232. //                  Log.v("NAME", userName);
  233.                 }
  234.                
  235.                 userProfilePicture = "https://graph.facebook.com/"+ userID
  236.                         + "/picture?type=large" + "&access_token=" + Utility.mFacebook.getAccessToken();
  237. //              Log.v("PROFILE PICTURE", userProfilePicture);
  238.                
  239.                 if (jsonObject.has("relationship_status"))  {
  240.                     userRelationshipStatus = jsonObject.getString("relationship_status");
  241. //                  Log.v("RELATIONSHIP STATUS", userRelationshipStatus);
  242.                    
  243.                     if (jsonObject.has("significant_other"))    {
  244.                         JSONObject joSignificantOther = jsonObject.optJSONObject("significant_other");
  245.                         userSignificantOtherName = joSignificantOther.getString("name");
  246.                         userSignificantOtherID = joSignificantOther.getString("id");
  247.                         userSignificantOtherPic = "https://graph.facebook.com/"+ userSignificantOtherID
  248.                                 + "/picture?type=square" + "&access_token=" + Utility.mFacebook.getAccessToken();
  249. //                      Log.v("SIGNIFICANT OTHER NAME", userSignificantOtherName);
  250. //                      Log.v("SIGNIFICANT OTHER ID", userSignificantOtherID);
  251. //                      Log.v("SIGNIFICANT OTHER PICTURE", userSignificantOtherPic);
  252.                     } else {
  253.                         userSignificantOtherName = null;
  254.                         userSignificantOtherID = null;
  255.                         userSignificantOtherPic = null;
  256. //                      Log.v("SIGNIFICANT OTHER NAME", userSignificantOtherName);
  257. //                      Log.v("SIGNIFICANT OTHER ID", userSignificantOtherID);
  258. //                      Log.v("SIGNIFICANT OTHER PICTURE", userSignificantOtherPic);
  259.                     }
  260.                    
  261.                 } else {
  262.                     userRelationshipStatus = null;
  263. //                  Log.v("RELATIONSHIP STATUS", userRelationshipStatus);
  264.                 }
  265.                
  266.                 if (jsonObject.has("birthday")) {
  267.                     userBirthDay = jsonObject.getString("birthday");
  268. //                  Log.v("BIRTHDAY", userBirthDay);
  269.                 } else {
  270.                     userBirthDay = null;
  271. //                  Log.v("BIRTHDAY", userBirthDay);
  272.                 }
  273.                
  274.                 if (jsonObject.has("gender"))   {
  275.                     userGender = jsonObject.getString("gender");
  276. //                  Log.v("GENDER", userGender);
  277.                 } else {
  278.                     userGender = null;
  279. //                  Log.v("GENDER", userGender);
  280.                 }
  281.                
  282.                 if (jsonObject.has("languages"))    {
  283.                     JSONArray jaLang = jsonObject.getJSONArray("languages");
  284.                    
  285.                     langBuilder = new StringBuilder();
  286.                     for(int i = 0; i < jaLang.length(); i ++)   {
  287.                         JSONObject json_data = jaLang.getJSONObject(i);
  288.                        
  289.                         langBuilder.append((json_data.getString("name")) + ("    ") );
  290.                     }
  291.                    
  292.                      userLanguages = langBuilder.toString();
  293. //                   Log.v("LANGUAGES", userLanguages);
  294.                 } else {
  295.                     userLanguages = null;
  296. //                  Log.v("LANGUAGES", userLanguages);
  297.                 }
  298.                
  299.                 if (jsonObject.has("link")) {
  300.                     userLink = jsonObject.getString("link");
  301. //                  Log.v("LINK", userLink);
  302.                 } else {
  303.                     userLink = null;
  304. //                  Log.v("LINK", userLink);
  305.                 }
  306.                
  307.                 if (jsonObject.has("email"))    {
  308.                     userEmail = jsonObject.getString("email");
  309. //                  Log.v("EMAIL", userEmail);
  310.                 } else {
  311.                     userEmail = null;
  312. //                  Log.v("EMAIL", userEmail);
  313.                 }
  314.                
  315.                 if (jsonObject.has("website"))  {
  316.                     userWebsite = jsonObject.getString("website");
  317. //                  Log.v("WEBSITE", userWebsite);
  318. //              } else {
  319.                     userWebsite = null;
  320. //                  Log.v("WEBSITE", userWebsite);
  321.                 }
  322.                
  323.                 if (jsonObject.has("bio"))  {
  324.                     userBio = jsonObject.getString("bio");
  325. //                  Log.v("BIO", userBio);
  326.                 } else {
  327.                     userBio = null;
  328. //                  Log.v("BIO", userBio);
  329.                 }
  330.                
  331.             } catch (Exception e) {
  332.                 e.printStackTrace();
  333.             }
  334.            
  335.             Intent myIntent = new Intent(getApplicationContext(), UsersProfile.class);
  336.            
  337.             myIntent.putExtra("USER_ID", userID);
  338.             myIntent.putExtra("USER_NAME", userName);
  339.             myIntent.putExtra("USER_PROFILE_PICTURE", userProfilePicture);
  340.             myIntent.putExtra("USER_RELATIONSHIP_STATUS", userRelationshipStatus);
  341.             myIntent.putExtra("USER_SIGNIFICANT_OTHER_NAME", userSignificantOtherName);
  342.             myIntent.putExtra("USER_SIGNIFICANT_OTHER_ID", userSignificantOtherID);
  343.             myIntent.putExtra("USER_SIGNIFICANT_OTHER_PROFILE_PICTURE", userSignificantOtherPic);
  344.             myIntent.putExtra("USER_BIRTHDAY", userBirthDay);
  345.             myIntent.putExtra("USER_GENDER", userGender);
  346.             myIntent.putExtra("USER_LANGUAGES", userLanguages);
  347.             myIntent.putExtra("USER_LINK", userLink);
  348.             myIntent.putExtra("USER_EMAIL", userEmail);
  349.             myIntent.putExtra("USER_WEBSITE", userWebsite);
  350.             myIntent.putExtra("USER_BIO", userBio);
  351.            
  352.             dialog.dismiss();
  353.             startActivity(myIntent);
  354.         }
  355.  
  356.         public void onFacebookError(FacebookError error) {
  357.             dialog.dismiss();
  358.             Toast.makeText(getApplicationContext(), "Facebook Error: " + error.getMessage(),
  359.                     Toast.LENGTH_SHORT).show();
  360.         }
  361.     }
  362.    
  363.  
  364.     private void hideKeyboard() {
  365.         getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  366.        
  367.     }
  368.    
  369.  
  370.     @Override
  371.     public void onAttachedToWindow() {
  372.         super.onAttachedToWindow();
  373.        
  374.         Window window = getWindow();
  375.         window.setFormat(PixelFormat.RGBA_8888);
  376.     }
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement