Parallelize AsyncTasks in OwncloudSyncService
Synchronize completion of tasks using a CountDownLatch
This commit is contained in:
parent
3368d1e0c1
commit
63371ae4f2
5 changed files with 65 additions and 82 deletions
|
@ -475,36 +475,20 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
|
|||
}
|
||||
|
||||
@Override
|
||||
public void startedSync(String sync_type) throws RemoteException {
|
||||
public void startedSync() throws RemoteException {
|
||||
UpdateButtonLayoutWithHandler();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finishedSync(String sync_type) throws RemoteException {
|
||||
UpdateButtonLayoutWithHandler();
|
||||
|
||||
Constants.SYNC_TYPES st = Constants.SYNC_TYPES.valueOf(sync_type);
|
||||
|
||||
switch(st) {
|
||||
case SYNC_TYPE__GET_API:
|
||||
break;
|
||||
case SYNC_TYPE__ITEM_STATES:
|
||||
break;
|
||||
case SYNC_TYPE__FOLDER:
|
||||
break;
|
||||
case SYNC_TYPE__FEEDS:
|
||||
break;
|
||||
case SYNC_TYPE__ITEMS:
|
||||
|
||||
Log.d(TAG, "finished sync");
|
||||
Handler refresh = new Handler(Looper.getMainLooper());
|
||||
refresh.post(new Runnable() {
|
||||
public void run() {
|
||||
syncFinishedHandler();
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
public void finishedSync() throws RemoteException {
|
||||
Log.d(TAG, "finished sync");
|
||||
Handler refresh = new Handler(Looper.getMainLooper());
|
||||
refresh.post(new Runnable() {
|
||||
public void run() {
|
||||
UpdateButtonLayout();
|
||||
syncFinishedHandler();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -31,8 +31,11 @@ import android.graphics.Bitmap;
|
|||
import android.graphics.BitmapFactory;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import de.luhmer.owncloudnewsreader.Constants;
|
||||
import de.luhmer.owncloudnewsreader.DownloadImagesActivity;
|
||||
import de.luhmer.owncloudnewsreader.R;
|
||||
|
@ -45,6 +48,8 @@ import de.luhmer.owncloudnewsreader.reader.OnAsyncTaskCompletedListener;
|
|||
import de.luhmer.owncloudnewsreader.services.DownloadImagesService;
|
||||
|
||||
public class AsyncTask_GetItems extends AsyncTask_Reader {
|
||||
private static final String TAG = "AsyncTask_GetItems";
|
||||
|
||||
private long highestItemIdBeforeSync;
|
||||
int totalCount;
|
||||
|
||||
|
@ -117,6 +122,7 @@ public class AsyncTask_GetItems extends AsyncTask_Reader {
|
|||
//If no exception occurs, set the number of updated items
|
||||
mPrefs.edit().putInt(Constants.LAST_UPDATE_NEW_ITEMS_COUNT_STRING, result[1]).commit();
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return ex;
|
||||
|
|
|
@ -68,7 +68,7 @@ public class OwnCloud_Reader {
|
|||
}
|
||||
|
||||
public void Start_AsyncTask_GetItems(Context context, OnAsyncTaskCompletedListener listener, FeedItemTags tag) {
|
||||
Start_AsyncTask(new AsyncTask_GetItems(context, AsyncTask_finished, listener),tag);
|
||||
Start_AsyncTask(new AsyncTask_GetItems(context, AsyncTask_finished, listener), tag);
|
||||
}
|
||||
|
||||
public void Start_AsyncTask_GetOldItems(Context context, OnAsyncTaskCompletedListener listener, Long feed_id, Long folder_id) {
|
||||
|
@ -93,7 +93,7 @@ public class OwnCloud_Reader {
|
|||
if(apiFuture == null)
|
||||
apiFuture = executor.submit(apiCallable);
|
||||
asyncTask.setAPIFuture(apiFuture);
|
||||
asyncTask.execute(params);
|
||||
asyncTask.executeOnExecutor(executor, params);
|
||||
}
|
||||
|
||||
public boolean isSyncRunning() {
|
||||
|
|
|
@ -5,8 +5,8 @@ import de.luhmer.owncloudnewsreader.helper.AidlException;
|
|||
oneway interface IOwnCloudSyncServiceCallback {
|
||||
//void doCallback(in String value);
|
||||
|
||||
void startedSync(String sync_type);
|
||||
void finishedSync(String sync_type);
|
||||
|
||||
void startedSync();
|
||||
void finishedSync();
|
||||
|
||||
void throwException(out AidlException ex);
|
||||
}
|
|
@ -28,26 +28,26 @@ import android.content.ComponentName;
|
|||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Resources;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.Looper;
|
||||
import android.os.RemoteCallbackList;
|
||||
import android.os.RemoteException;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import de.luhmer.owncloudnewsreader.Constants;
|
||||
import de.luhmer.owncloudnewsreader.Constants.SYNC_TYPES;
|
||||
import de.luhmer.owncloudnewsreader.R;
|
||||
import de.luhmer.owncloudnewsreader.SettingsActivity;
|
||||
import de.luhmer.owncloudnewsreader.helper.AidlException;
|
||||
import de.luhmer.owncloudnewsreader.helper.NotificationManagerNewsReader;
|
||||
import de.luhmer.owncloudnewsreader.reader.FeedItemTags;
|
||||
import de.luhmer.owncloudnewsreader.reader.OnAsyncTaskCompletedListener;
|
||||
import de.luhmer.owncloudnewsreader.reader.owncloud.API;
|
||||
import de.luhmer.owncloudnewsreader.reader.owncloud.OwnCloud_Reader;
|
||||
import de.luhmer.owncloudnewsreader.services.IOwnCloudSyncService.Stub;
|
||||
import de.luhmer.owncloudnewsreader.widget.WidgetProvider;
|
||||
|
@ -58,6 +58,8 @@ public class OwnCloudSyncService extends Service {
|
|||
|
||||
private RemoteCallbackList<IOwnCloudSyncServiceCallback> callbacks = new RemoteCallbackList<>();
|
||||
|
||||
private CountDownLatch syncCompletedLatch;
|
||||
|
||||
private Stub mBinder = new IOwnCloudSyncService.Stub() {
|
||||
|
||||
public void registerCallback(IOwnCloudSyncServiceCallback callback) {
|
||||
|
@ -71,8 +73,8 @@ public class OwnCloudSyncService extends Service {
|
|||
@Override
|
||||
public void startSync() throws RemoteException {
|
||||
if(!isSyncRunning()) {
|
||||
startedSync();
|
||||
OwnCloud_Reader.getInstance().Start_AsyncTask_PerformItemStateChange(OwnCloudSyncService.this, onAsyncTask_PerformTagExecute);
|
||||
startedSync(SYNC_TYPES.SYNC_TYPE__ITEM_STATES);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -103,41 +105,36 @@ public class OwnCloudSyncService extends Service {
|
|||
OnAsyncTaskCompletedListener onAsyncTask_PerformTagExecute = new OnAsyncTaskCompletedListener() {
|
||||
@Override
|
||||
public void onAsyncTaskCompleted(int task_id, Object task_result) {
|
||||
|
||||
finishedSync(SYNC_TYPES.SYNC_TYPE__ITEM_STATES);
|
||||
|
||||
if(task_result != null)//task result is null if there was an error
|
||||
{
|
||||
if((Boolean) task_result)
|
||||
{
|
||||
if(task_id == Constants.TaskID_PerformStateChange) {
|
||||
OwnCloud_Reader.getInstance().Start_AsyncTask_GetFolder(OwnCloudSyncService.this, onAsyncTask_GetFolder);
|
||||
|
||||
|
||||
startedSync(SYNC_TYPES.SYNC_TYPE__FOLDER);
|
||||
}
|
||||
else
|
||||
OwnCloud_Reader.getInstance().setSyncRunning(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
syncCompletedLatch = new CountDownLatch(3);
|
||||
|
||||
OwnCloud_Reader.getInstance().Start_AsyncTask_GetFolder(OwnCloudSyncService.this, onAsyncTask_GetFolder);
|
||||
OwnCloud_Reader.getInstance().Start_AsyncTask_GetFeeds(OwnCloudSyncService.this, onAsyncTask_GetFeed);
|
||||
OwnCloud_Reader.getInstance().Start_AsyncTask_GetItems(OwnCloudSyncService.this, onAsyncTask_GetItems, FeedItemTags.ALL); //Receive all unread Items
|
||||
AsyncTask.execute(syncCompletionRunnable);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
private final Runnable syncCompletionRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
syncCompletedLatch.await();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
||||
public void run() {
|
||||
finishedSync();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
OnAsyncTaskCompletedListener onAsyncTask_GetFolder = new OnAsyncTaskCompletedListener() {
|
||||
@Override
|
||||
public void onAsyncTaskCompleted(int task_id, Object task_result) {
|
||||
|
||||
finishedSync(SYNC_TYPES.SYNC_TYPE__FOLDER);
|
||||
|
||||
if(task_result != null)
|
||||
ThrowException((Exception) task_result);
|
||||
else {
|
||||
OwnCloud_Reader.getInstance().Start_AsyncTask_GetFeeds(OwnCloudSyncService.this, onAsyncTask_GetFeed);
|
||||
|
||||
startedSync(SYNC_TYPES.SYNC_TYPE__FEEDS);
|
||||
}
|
||||
|
||||
syncCompletedLatch.countDown();
|
||||
Log.d(TAG, "onAsyncTask_GetFolder Finished");
|
||||
|
||||
}
|
||||
|
@ -147,16 +144,10 @@ public class OwnCloudSyncService extends Service {
|
|||
|
||||
@Override
|
||||
public void onAsyncTaskCompleted(int task_id, Object task_result) {
|
||||
|
||||
finishedSync(SYNC_TYPES.SYNC_TYPE__FEEDS);
|
||||
|
||||
syncCompletedLatch.countDown();
|
||||
|
||||
if(task_result != null)
|
||||
ThrowException((Exception) task_result);
|
||||
else {
|
||||
OwnCloud_Reader.getInstance().Start_AsyncTask_GetItems(OwnCloudSyncService.this, onAsyncTask_GetItems, FeedItemTags.ALL);//Recieve all unread Items
|
||||
|
||||
startedSync(SYNC_TYPES.SYNC_TYPE__ITEMS);
|
||||
}
|
||||
|
||||
Log.d(TAG, "onAsyncTask_GetFeed Finished");
|
||||
}
|
||||
|
@ -166,8 +157,8 @@ public class OwnCloudSyncService extends Service {
|
|||
|
||||
@Override
|
||||
public void onAsyncTaskCompleted(int task_id, Object task_result) {
|
||||
finishedSync(SYNC_TYPES.SYNC_TYPE__ITEMS);
|
||||
|
||||
syncCompletedLatch.countDown();
|
||||
|
||||
if(task_result != null)
|
||||
ThrowException((Exception) task_result);
|
||||
else
|
||||
|
@ -224,11 +215,13 @@ public class OwnCloudSyncService extends Service {
|
|||
callbacks.finishBroadcast();
|
||||
}
|
||||
|
||||
private void startedSync(SYNC_TYPES sync_type) {
|
||||
private void startedSync() {
|
||||
Log.v(TAG, "Synchronization started");
|
||||
|
||||
List<IOwnCloudSyncServiceCallback> callbackList = getCallBackItemsAndBeginBroadcast();
|
||||
for(IOwnCloudSyncServiceCallback icb : callbackList) {
|
||||
try {
|
||||
icb.startedSync(sync_type.toString());
|
||||
icb.startedSync();
|
||||
//icb.finishedSyncOfItems();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -237,13 +230,13 @@ public class OwnCloudSyncService extends Service {
|
|||
callbacks.finishBroadcast();
|
||||
}
|
||||
|
||||
private void finishedSync(SYNC_TYPES sync_type) {
|
||||
Log.v(TAG, "Finished Sync: " + sync_type.toString());
|
||||
private void finishedSync() {
|
||||
Log.v(TAG, "Synchronization finished");
|
||||
|
||||
List<IOwnCloudSyncServiceCallback> callbackList = getCallBackItemsAndBeginBroadcast();
|
||||
for(IOwnCloudSyncServiceCallback icb : callbackList) {
|
||||
try {
|
||||
icb.finishedSync(sync_type.toString());
|
||||
icb.finishedSync();
|
||||
//icb.finishedSyncOfItems();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
|
|
Loading…
Reference in a new issue