From c6cbdc20f05fda96f52a53ad902b1b24f8f7d6fd Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 19 Nov 2017 14:02:08 +0100 Subject: [PATCH] Friends list is automatically refreshed --- .../android/comunic/client/MainActivity.java | 34 ++++++++++- .../FriendRefreshLoopRunnable.java | 61 +++++++++++++++++++ .../FriendsListRefreshRunnable.java | 54 ++++++++++++++++ .../data/friendsList/GetFriendsList.java | 2 + .../data/friendsList/GetFriendsListTask.java | 12 ++-- .../client/fragments/FriendsListFragment.java | 2 +- 6 files changed, 156 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendRefreshLoopRunnable.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListRefreshRunnable.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java index ba27656..77e830e 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java @@ -1,7 +1,6 @@ package org.communiquons.android.comunic.client; import android.app.AlertDialog; -import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.DialogInterface; import android.content.Intent; @@ -14,9 +13,10 @@ import android.view.MenuItem; import android.widget.Toast; import org.communiquons.android.comunic.client.api.APIRequest; -import org.communiquons.android.comunic.client.api.APIRequestTask; import org.communiquons.android.comunic.client.data.Account.Account; import org.communiquons.android.comunic.client.data.Account.AccountUtils; +import org.communiquons.android.comunic.client.data.DatabaseHelper; +import org.communiquons.android.comunic.client.data.friendsList.FriendRefreshLoopRunnable; import org.communiquons.android.comunic.client.fragments.FriendsListFragment; import org.communiquons.android.comunic.client.fragments.UserInfosFragment; @@ -38,6 +38,16 @@ public class MainActivity extends AppCompatActivity { */ private AccountUtils aUtils; + /** + * Friends list refresh thread + */ + private Thread friendsListRefreshThread; + + /** + * Database helper + */ + private DatabaseHelper dbHelper; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -63,7 +73,10 @@ public class MainActivity extends AppCompatActivity { Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show(); } - //If it is the first time the application is launched, started the user friends tab + //Initialize DatabaseHelper + dbHelper = new DatabaseHelper(getApplicationContext()); + + //If it is the first time the application is launched, start the user friends tab if(savedInstanceState == null){ openFriendsFragment(); } @@ -77,7 +90,22 @@ public class MainActivity extends AppCompatActivity { if(!account.signed_in()){ //Open the login activity startActivity(new Intent(this, LoginActivity.class)); + return; } + + //Refresh friends list through a thread + friendsListRefreshThread = new Thread( + new FriendRefreshLoopRunnable(getApplicationContext(), dbHelper)); + friendsListRefreshThread.start(); + } + + @Override + protected void onStop() { + super.onStop(); + + //Stop the friends list refresh thread + if(friendsListRefreshThread != null) + friendsListRefreshThread.interrupt(); } /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendRefreshLoopRunnable.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendRefreshLoopRunnable.java new file mode 100644 index 0000000..4902f42 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendRefreshLoopRunnable.java @@ -0,0 +1,61 @@ +package org.communiquons.android.comunic.client.data.friendsList; + + +import android.content.Context; + +import org.communiquons.android.comunic.client.data.DatabaseHelper; + +/** + * Automatically refresh friends list thread + * + * Goal this thread : this thread is killed when it is not required anymore to refresh the friends + * list. + * + * However, it launches periodically another thread, FriendsListRefreshRunnable, which is never + * killed, in order not to get errors. + * + * @author Pierre HUBERT + * Created by pierre on 11/19/17. + */ + +public class FriendRefreshLoopRunnable implements Runnable { + + private final Object object = new Object(); + + private Context context; + private DatabaseHelper dbHelper; + + public FriendRefreshLoopRunnable(Context context, DatabaseHelper dbHelper){ + this.context = context; + this.dbHelper = dbHelper; + } + + /** + * Perpetual loop that only stops when it get killed + */ + @Override + public void run() { + + synchronized (object) { + + Thread thread; + + while (true) { + + //Create a refresh thread + thread = new Thread(new FriendsListRefreshRunnable(context, dbHelper)); + thread.start(); + + try { + thread.join(); + object.wait(15000); + } catch (Exception e) { + e.printStackTrace(); + return; //Stop the refresh loop + } + } + } + + } + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListRefreshRunnable.java b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListRefreshRunnable.java new file mode 100644 index 0000000..f7333be --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/friendsList/FriendsListRefreshRunnable.java @@ -0,0 +1,54 @@ +package org.communiquons.android.comunic.client.data.friendsList; + +import android.content.Context; + +import org.communiquons.android.comunic.client.data.DatabaseHelper; + +import java.util.ArrayList; + +/** + * Auto friends list refresh runnable + * + * @author Pierre HUBERT + * Created by pierre on 11/19/17. + */ + +class FriendsListRefreshRunnable implements Runnable { + + /** + * This runnable requires a context object in order to connect to the API + * + * The context of the application (or of the activity) + */ + private Context mContext; + + /** + * Database object + */ + private DatabaseHelper dbHelper; + + /** + * Runnable constructor + * + * @param context The context of the application + * @param dbHelper Database helper + */ + FriendsListRefreshRunnable(Context context, DatabaseHelper dbHelper){ + this.mContext = context; + this.dbHelper = dbHelper; + } + + @Override + public void run() { + + GetFriendsList getFriendsList = new GetFriendsList(mContext); + FriendsListDbHelper friendsDBHelper = new FriendsListDbHelper(dbHelper); + + //Get the latest version of the list + ArrayList friendsList = getFriendsList.get(); + + //Save it (only in case of success) + if(friendsList != null) + friendsDBHelper.update_list(friendsList); + } +} 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 index cefc3f2..2136ecd 100644 --- 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 @@ -1,6 +1,7 @@ package org.communiquons.android.comunic.client.data.friendsList; import android.content.Context; +import android.support.annotation.Nullable; import org.communiquons.android.comunic.client.api.APIRequest; import org.communiquons.android.comunic.client.api.APIRequestParameters; @@ -38,6 +39,7 @@ class GetFriendsList { * * @return The list of friend of the user */ + @Nullable ArrayList get(){ //Prepare the API request 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 index 185eb38..22b1278 100644 --- 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 @@ -3,6 +3,8 @@ package org.communiquons.android.comunic.client.data.friendsList; import android.content.Context; import android.os.AsyncTask; +import org.communiquons.android.comunic.client.data.DatabaseHelper; + import java.util.ArrayList; /** @@ -17,15 +19,15 @@ public abstract class GetFriendsListTask extends AsyncTask doInBackground(Void... params) { //Create the GetFriendList object and use it to fetch the list - return new GetFriendsList(mContext).get(); + return new FriendsListDbHelper(dbHelper).get_list(); } } 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 beaf310..751cad5 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 @@ -83,7 +83,7 @@ public class FriendsListFragment extends Fragment { //Display loading bar display_progress_bar(true); - new GetFriendsListTask(getActivity().getApplicationContext()){ + new GetFriendsListTask(mDbHelper){ @Override protected void onPostExecute(ArrayList friendsList) {