Merged FriendRefreshLoopRunnable and FriendListRefreshRunnable

This commit is contained in:
Pierre 2018-05-20 18:01:05 +02:00
parent 4a5902973b
commit e582053655
3 changed files with 37 additions and 79 deletions

View File

@ -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<Friend> 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;
}
}

View File

@ -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<Friend> friendsList = friendsListHelper.download();
//Save it (only in case of success)
if(friendsList != null)
friendsDBHelper.update_list(friendsList);
}
}

View File

@ -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();
}
/**