From e582053655d6894edd262b11108e91075a171e49 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 20 May 2018 18:01:05 +0200 Subject: [PATCH] Merged FriendRefreshLoopRunnable and FriendListRefreshRunnable --- .../runnables/FriendRefreshLoopRunnable.java | 47 +++++++++------ .../runnables/FriendsListRefreshRunnable.java | 57 ------------------- .../client/ui/activities/MainActivity.java | 12 ++-- 3 files changed, 37 insertions(+), 79 deletions(-) delete mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/runnables/FriendsListRefreshRunnable.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/FriendRefreshLoopRunnable.java b/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/FriendRefreshLoopRunnable.java index ccfe122..f2fc740 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/FriendRefreshLoopRunnable.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/FriendRefreshLoopRunnable.java @@ -4,16 +4,15 @@ package org.communiquons.android.comunic.client.data.runnables; import android.content.Context; import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper; +import org.communiquons.android.comunic.client.data.helpers.FriendsListDbHelper; +import org.communiquons.android.comunic.client.data.helpers.FriendsListHelper; +import org.communiquons.android.comunic.client.data.models.Friend; + +import java.util.ArrayList; /** * 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 download errors. - * * @author Pierre HUBERT * Created by pierre on 11/19/17. */ @@ -22,40 +21,56 @@ public class FriendRefreshLoopRunnable implements Runnable { private final Object object = new Object(); - private Context context; + private Context mContext; private DatabaseHelper dbHelper; + private boolean mStop = false; + public FriendRefreshLoopRunnable(Context context, DatabaseHelper dbHelper){ - this.context = context; + this.mContext = context.getApplicationContext(); this.dbHelper = dbHelper; } /** - * Perpetual loop that only stops when it download killed + * Perpetual loop */ @Override public void run() { synchronized (object) { - Thread thread; - while (true) { - //Create a refresh thread - thread = new Thread(new FriendsListRefreshRunnable(context, dbHelper)); - thread.start(); + FriendsListHelper friendsListHelper = new FriendsListHelper(mContext); + FriendsListDbHelper friendsDBHelper = new FriendsListDbHelper(dbHelper); + + //Get the latest version of the list + ArrayList friendsList = friendsListHelper.download(); + + //Save it (only in case of success) + if(friendsList != null) + friendsDBHelper.update_list(friendsList); try { - thread.join(); - object.wait(15000); + object.wait(500); //TODO: increase the value } catch (Exception e) { e.printStackTrace(); return; //Stop the refresh loop } + + //Check if this thread has to be interrupted + if(mStop) + return; } } } + /** + * Query this thread to stop + */ + public void interrupt(){ + mStop = true; + } + } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/FriendsListRefreshRunnable.java b/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/FriendsListRefreshRunnable.java deleted file mode 100644 index a5ad9aa..0000000 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/runnables/FriendsListRefreshRunnable.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.communiquons.android.comunic.client.data.runnables; - -import android.content.Context; - -import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper; -import org.communiquons.android.comunic.client.data.helpers.FriendsListDbHelper; -import org.communiquons.android.comunic.client.data.helpers.FriendsListHelper; -import org.communiquons.android.comunic.client.data.models.Friend; - -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() { - - FriendsListHelper friendsListHelper = new FriendsListHelper(mContext); - FriendsListDbHelper friendsDBHelper = new FriendsListDbHelper(dbHelper); - - //Get the latest version of the list - ArrayList friendsList = friendsListHelper.download(); - - //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/ui/activities/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java index b06f767..d94a655 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java @@ -72,7 +72,7 @@ public class MainActivity extends AppCompatActivity implements openConversationL /** * Friends list refresh thread */ - private Thread friendsListRefreshThread; + private FriendRefreshLoopRunnable friendsListRefreshRunnable; /** * Database helper @@ -144,9 +144,9 @@ public class MainActivity extends AppCompatActivity implements openConversationL } //Refresh friends list through a thread - friendsListRefreshThread = new Thread( - new FriendRefreshLoopRunnable(getApplicationContext(), dbHelper)); - friendsListRefreshThread.start(); + friendsListRefreshRunnable = new FriendRefreshLoopRunnable(getApplicationContext(), + dbHelper); + new Thread(friendsListRefreshRunnable).start(); //Start notification thread Intent intent = new Intent(this, NotificationsService.class); @@ -158,8 +158,8 @@ public class MainActivity extends AppCompatActivity implements openConversationL super.onStop(); //Stop the friends list refresh thread - if(friendsListRefreshThread != null) - friendsListRefreshThread.interrupt(); + if(friendsListRefreshRunnable != null) + friendsListRefreshRunnable.interrupt(); } /**