diff --git a/.idea/misc.xml b/.idea/misc.xml
index 5d19981..33952c6 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -3,6 +3,24 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java
index cb7970b..86f53b6 100644
--- a/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java
+++ b/app/src/main/java/org/communiquons/android/comunic/client/api/APIRequest.java
@@ -3,11 +3,14 @@ package org.communiquons.android.comunic.client.api;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
+import android.util.Log;
import org.communiquons.android.comunic.client.BuildConfig;
import org.communiquons.android.comunic.client.data.Account.Account;
+import org.communiquons.android.comunic.client.data.Utilities;
import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
@@ -84,7 +87,7 @@ public class APIRequest {
result.setResponse_code(conn.getResponseCode());
is = conn.getInputStream();
- String response = readIt(is, 5000);
+ String response = readIt(is);
result.setResponse(response);
conn.disconnect();
@@ -99,11 +102,18 @@ public class APIRequest {
}
// Reads an InputStream and converts it to a String.
- private String readIt(InputStream stream, int len) throws IOException {
- Reader reader = new InputStreamReader(stream, "UTF-8");
- char[] buffer = new char[len];
- reader.read(buffer);
- return new String(buffer);
+ private String readIt(InputStream stream) throws IOException {
+
+ BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
+ StringBuilder out = new StringBuilder();
+ String line;
+ while((line = reader.readLine()) != null){
+ out.append(line);
+ }
+ reader.close();
+
+ return out.toString();
+
}
/**
diff --git a/app/src/main/java/org/communiquons/android/comunic/client/api/APIResponse.java b/app/src/main/java/org/communiquons/android/comunic/client/api/APIResponse.java
index 5a0dd27..1f54275 100644
--- a/app/src/main/java/org/communiquons/android/comunic/client/api/APIResponse.java
+++ b/app/src/main/java/org/communiquons/android/comunic/client/api/APIResponse.java
@@ -1,5 +1,6 @@
package org.communiquons.android.comunic.client.api;
+import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@@ -81,4 +82,28 @@ public class APIResponse {
return response;
}
+
+ /**
+ * Get the response as a JSON array
+ *
+ * Might be required in some case
+ *
+ * Warning ! Take care : use getJSONArray or getJSONObject in an adapted way to the response
+ *
+ * @return The response as JSON object. False in case of failure
+ */
+ public JSONArray getJSONArray(){
+
+ JSONArray response = null;
+
+ //Try to decode JSON object
+ try {
+ response = new JSONArray(this.response);
+ } catch (JSONException e) {
+ //In case of failure
+ e.printStackTrace();
+ }
+
+ return response;
+ }
}
diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java b/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java
index fc17474..0c035bf 100644
--- a/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java
+++ b/app/src/main/java/org/communiquons/android/comunic/client/data/Utilities.java
@@ -11,9 +11,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
+import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
-import java.security.MessageDigest;
/**
* Application utilities
diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/GetFriendsList.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/GetFriendsList.java
new file mode 100644
index 0000000..cefc3f2
--- /dev/null
+++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/GetFriendsList.java
@@ -0,0 +1,87 @@
+package org.communiquons.android.comunic.client.data.friendsList;
+
+import android.content.Context;
+
+import org.communiquons.android.comunic.client.api.APIRequest;
+import org.communiquons.android.comunic.client.api.APIRequestParameters;
+import org.communiquons.android.comunic.client.api.APIResponse;
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import java.util.ArrayList;
+
+/**
+ * Fetch and return friends list
+ *
+ * @author Pierre HUBERT
+ * Created by pierre on 11/12/17.
+ */
+
+class GetFriendsList {
+
+ /**
+ * The context of execution of the application
+ */
+ private Context mContext;
+
+ /**
+ * Public constructor of the class
+ *
+ * @param context the context of execution of the application
+ */
+ GetFriendsList(Context context){
+ mContext = context;
+ }
+
+ /**
+ * Get and return the friends list
+ *
+ * @return The list of friend of the user
+ */
+ ArrayList get(){
+
+ //Prepare the API request
+ APIRequestParameters params = new APIRequestParameters(mContext, "friends/getList");
+
+ //Prepare the result
+ ArrayList friends = new ArrayList<>();
+
+ try {
+
+ //Perform the request and retrieve the response
+ APIResponse response = new APIRequest().exec(params);
+ JSONArray friendsList = response.getJSONArray();
+
+ if(friendsList == null)
+ return null;
+
+ //Process JSON array
+ for(int i = 0; i < friendsList.length(); i++){
+
+ //Try to extract JSON object containing informations
+ JSONObject friendship_infos = friendsList.getJSONObject(i);
+
+ //Save informations about the friend in the friend object
+ Friend friend = new Friend();
+
+ //Set friend informations
+ friend.setId(friendship_infos.getInt("ID_friend"));
+ friend.setAccepted(friendship_infos.getInt("accepted") == 1);
+ friend.setFollowing(friendship_infos.getInt("ID_friend") == 1);
+ friend.setLast_activity(friendship_infos.getInt("time_last_activity"));
+
+ //Add the friend to the list
+ friends.add(friend);
+
+ }
+
+
+ } catch (Exception e){
+ e.printStackTrace();
+ return null;
+ }
+
+ return friends;
+ }
+
+}
diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/GetFriendsListTask.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/GetFriendsListTask.java
new file mode 100644
index 0000000..185eb38
--- /dev/null
+++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/GetFriendsListTask.java
@@ -0,0 +1,52 @@
+package org.communiquons.android.comunic.client.data.friendsList;
+
+import android.content.Context;
+import android.os.AsyncTask;
+
+import java.util.ArrayList;
+
+/**
+ * This class handles the asynchronous refresh of the friends list
+ *
+ * @author Pierre HUBERT
+ * Created by pierre on 11/12/17.
+ */
+
+public abstract class GetFriendsListTask extends AsyncTask> {
+
+ /**
+ * The context of the task
+ */
+ private Context mContext;
+
+ /**
+ * Public constructor to the class
+ *
+ * @param context the context of execution of the refresh task
+ */
+ public GetFriendsListTask(Context context){
+ mContext = context;
+ }
+
+ /**
+ * What to to once the users list has been refreshed
+ *
+ * It it the role of each class that use this task class to implement this method
+ *
+ * @param friendsList The list of friends of the user / null in case of failure
+ */
+ @Override
+ abstract protected void onPostExecute(ArrayList friendsList);
+
+ /**
+ * Background operation
+ *
+ * @param params
+ * @return The list of friends of the user
+ */
+ @Override
+ protected ArrayList doInBackground(Void... params) {
+ //Create the GetFriendList object and use it to fetch the list
+ return new GetFriendsList(mContext).get();
+ }
+}
diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java
index bfdf9e3..1e199e0 100644
--- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java
+++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java
@@ -14,6 +14,8 @@ import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.data.DatabaseHelper;
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersInfos;
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo;
+import org.communiquons.android.comunic.client.data.friendsList.Friend;
+import org.communiquons.android.comunic.client.data.friendsList.GetFriendsListTask;
import java.util.ArrayList;
@@ -43,27 +45,29 @@ public class FriendsListFragment extends Fragment {
public void onStart() {
super.onStart();
- final ArrayList list = new ArrayList<>();
- list.add(2);
- list.add(3);
- new GetUsersInfos(getActivity(), new DatabaseHelper(getActivity())).getMultiple(list, new GetUsersInfos.getMultipleUserInfosCallback() {
+ //Refresh the friends list
+ refresh_friend_list();
+ }
+
+ /**
+ * Refresh the friend list
+ */
+ void refresh_friend_list(){
+ new GetFriendsListTask(getActivity().getApplicationContext()){
+
@Override
- public void callback(ArrayMap info) {
- Log.v("FriendsListFragment", "User infos callback");
- if(info == null)
- Toast.makeText(getActivity(), "Failure", Toast.LENGTH_LONG).show();
- else
- Toast.makeText(getActivity(), "Success", Toast.LENGTH_LONG).show();
+ protected void onPostExecute(ArrayList friendsList) {
- for(int ID : list){
- UserInfo infos = info.get(ID);
-
- if(infos == null)
- Log.e("FriendsListFragment", "Error with user infos for ID " + ID);
- else
- Log.v("FriendsListFragment", ID + " is " + infos.getFullName() + " !");
+ //Display informations in the console
+ for(Friend friend : friendsList){
+ Log.v("FriendsListFragment", "Friend: " + friend.getId() + " " +
+ (friend.isAccepted() ? 1 : 0 ) + " " + (friend.isFollowing() ? 1 : 0) +
+ " " + friend.getLast_activity() + " "
+ );
}
+
}
- });
+
+ }.execute();
}
}