mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-27 07:49:28 +00:00
Merged FriendRefreshLoopRunnable and FriendListRefreshRunnable
This commit is contained in:
parent
4a5902973b
commit
e582053655
@ -4,16 +4,15 @@ package org.communiquons.android.comunic.client.data.runnables;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper;
|
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
|
* 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
|
* @author Pierre HUBERT
|
||||||
* Created by pierre on 11/19/17.
|
* Created by pierre on 11/19/17.
|
||||||
*/
|
*/
|
||||||
@ -22,40 +21,56 @@ public class FriendRefreshLoopRunnable implements Runnable {
|
|||||||
|
|
||||||
private final Object object = new Object();
|
private final Object object = new Object();
|
||||||
|
|
||||||
private Context context;
|
private Context mContext;
|
||||||
private DatabaseHelper dbHelper;
|
private DatabaseHelper dbHelper;
|
||||||
|
|
||||||
|
private boolean mStop = false;
|
||||||
|
|
||||||
public FriendRefreshLoopRunnable(Context context, DatabaseHelper dbHelper){
|
public FriendRefreshLoopRunnable(Context context, DatabaseHelper dbHelper){
|
||||||
this.context = context;
|
this.mContext = context.getApplicationContext();
|
||||||
this.dbHelper = dbHelper;
|
this.dbHelper = dbHelper;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perpetual loop that only stops when it download killed
|
* Perpetual loop
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
synchronized (object) {
|
synchronized (object) {
|
||||||
|
|
||||||
Thread thread;
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|
||||||
//Create a refresh thread
|
FriendsListHelper friendsListHelper = new FriendsListHelper(mContext);
|
||||||
thread = new Thread(new FriendsListRefreshRunnable(context, dbHelper));
|
FriendsListDbHelper friendsDBHelper = new FriendsListDbHelper(dbHelper);
|
||||||
thread.start();
|
|
||||||
|
//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 {
|
try {
|
||||||
thread.join();
|
object.wait(500); //TODO: increase the value
|
||||||
object.wait(15000);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return; //Stop the refresh loop
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -72,7 +72,7 @@ public class MainActivity extends AppCompatActivity implements openConversationL
|
|||||||
/**
|
/**
|
||||||
* Friends list refresh thread
|
* Friends list refresh thread
|
||||||
*/
|
*/
|
||||||
private Thread friendsListRefreshThread;
|
private FriendRefreshLoopRunnable friendsListRefreshRunnable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Database helper
|
* Database helper
|
||||||
@ -144,9 +144,9 @@ public class MainActivity extends AppCompatActivity implements openConversationL
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Refresh friends list through a thread
|
//Refresh friends list through a thread
|
||||||
friendsListRefreshThread = new Thread(
|
friendsListRefreshRunnable = new FriendRefreshLoopRunnable(getApplicationContext(),
|
||||||
new FriendRefreshLoopRunnable(getApplicationContext(), dbHelper));
|
dbHelper);
|
||||||
friendsListRefreshThread.start();
|
new Thread(friendsListRefreshRunnable).start();
|
||||||
|
|
||||||
//Start notification thread
|
//Start notification thread
|
||||||
Intent intent = new Intent(this, NotificationsService.class);
|
Intent intent = new Intent(this, NotificationsService.class);
|
||||||
@ -158,8 +158,8 @@ public class MainActivity extends AppCompatActivity implements openConversationL
|
|||||||
super.onStop();
|
super.onStop();
|
||||||
|
|
||||||
//Stop the friends list refresh thread
|
//Stop the friends list refresh thread
|
||||||
if(friendsListRefreshThread != null)
|
if(friendsListRefreshRunnable != null)
|
||||||
friendsListRefreshThread.interrupt();
|
friendsListRefreshRunnable.interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user