Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

trying to use a better async manager.

  • Loading branch information...
commit c77f6ada4ac5ff1f31f8a86a59a5c2ce7fe97c36 1 parent a086d7a
@mariotaku authored
View
9 src/org/mariotaku/twidere/Constants.java
@@ -483,4 +483,13 @@
public static final String ICON_SPECIAL_TYPE_CUSTOMIZE = "_customize";
public static final String FRAGMENT_TAG_API_UPGRADE_NOTICE = "api_upgrade_notice";
+
+ public static final String TASK_TAG_GET_HOME_TIMELINE = "get_home_tomeline";
+ public static final String TASK_TAG_GET_MENTIONS = "get_mentions";
+ public static final String TASK_TAG_GET_SENT_DIRECT_MESSAGES = "get_sent_direct_messages";
+ public static final String TASK_TAG_GET_RECEIVED_DIRECT_MESSAGES = "get_received_direct_messages";
+ public static final String TASK_TAG_STORE_HOME_TIMELINE = "store_home_tomeline";
+ public static final String TASK_TAG_STORE_MENTIONS = "store_mentions";
+ public static final String TASK_TAG_STORE_SENT_DIRECT_MESSAGES = "store_sent_direct_messages";
+ public static final String TASK_TAG_STORE_RECEIVED_DIRECT_MESSAGES = "store_received_direct_messages";
}
View
47 src/org/mariotaku/twidere/service/TwidereService.java
@@ -280,8 +280,8 @@ public boolean hasActivatedTask() {
}
public boolean isHomeTimelineRefreshing() {
- return mAsyncTaskManager.isExcuting(mGetHomeTimelineTaskId)
- || mAsyncTaskManager.isExcuting(mStoreStatusesTaskId);
+ return mAsyncTaskManager.hasRunningTasksForTag(TASK_TAG_GET_HOME_TIMELINE)
+ || mAsyncTaskManager.hasRunningTasksForTag(TASK_TAG_STORE_HOME_TIMELINE);
}
public boolean isLocalTrendsRefreshing() {
@@ -290,17 +290,18 @@ public boolean isLocalTrendsRefreshing() {
}
public boolean isMentionsRefreshing() {
- return mAsyncTaskManager.isExcuting(mGetMentionsTaskId) || mAsyncTaskManager.isExcuting(mStoreMentionsTaskId);
+ return mAsyncTaskManager.hasRunningTasksForTag(TASK_TAG_GET_MENTIONS)
+ || mAsyncTaskManager.hasRunningTasksForTag(TASK_TAG_STORE_MENTIONS);
}
public boolean isReceivedDirectMessagesRefreshing() {
- return mAsyncTaskManager.isExcuting(mGetReceivedDirectMessagesTaskId)
- || mAsyncTaskManager.isExcuting(mStoreReceivedDirectMessagesTaskId);
+ return mAsyncTaskManager.hasRunningTasksForTag(TASK_TAG_GET_RECEIVED_DIRECT_MESSAGES)
+ || mAsyncTaskManager.hasRunningTasksForTag(TASK_TAG_STORE_RECEIVED_DIRECT_MESSAGES);
}
public boolean isSentDirectMessagesRefreshing() {
- return mAsyncTaskManager.isExcuting(mGetSentDirectMessagesTaskId)
- || mAsyncTaskManager.isExcuting(mStoreSentDirectMessagesTaskId);
+ return mAsyncTaskManager.hasRunningTasksForTag(TASK_TAG_GET_SENT_DIRECT_MESSAGES)
+ || mAsyncTaskManager.hasRunningTasksForTag(TASK_TAG_STORE_SENT_DIRECT_MESSAGES);
}
@Override
@@ -1637,8 +1638,8 @@ private TwidereService getOuterType() {
private final long[] account_ids, max_ids, since_ids;
- public GetDirectMessagesTask(final long[] account_ids, final long[] max_ids, final long[] since_ids) {
- super(TwidereService.this, mAsyncTaskManager);
+ public GetDirectMessagesTask(final long[] account_ids, final long[] max_ids, final long[] since_ids, final String tag) {
+ super(TwidereService.this, mAsyncTaskManager, tag);
this.account_ids = account_ids;
this.max_ids = max_ids;
this.since_ids = since_ids;
@@ -1737,7 +1738,7 @@ final boolean isSinceIdsValid() {
class GetHomeTimelineTask extends GetStatusesTask {
public GetHomeTimelineTask(final long[] account_ids, final long[] max_ids, final long[] since_ids) {
- super(account_ids, max_ids, since_ids);
+ super(account_ids, max_ids, since_ids, TASK_TAG_GET_HOME_TIMELINE);
}
@Override
@@ -1850,7 +1851,7 @@ private TwidereService getOuterType() {
class GetMentionsTask extends GetStatusesTask {
public GetMentionsTask(final long[] account_ids, final long[] max_ids, final long[] since_ids) {
- super(account_ids, max_ids, since_ids);
+ super(account_ids, max_ids, since_ids, TASK_TAG_GET_MENTIONS);
}
@Override
@@ -1912,7 +1913,7 @@ private TwidereService getOuterType() {
class GetReceivedDirectMessagesTask extends GetDirectMessagesTask {
public GetReceivedDirectMessagesTask(final long[] account_ids, final long[] max_ids, final long[] since_ids) {
- super(account_ids, max_ids, since_ids);
+ super(account_ids, max_ids, since_ids, TASK_TAG_GET_RECEIVED_DIRECT_MESSAGES);
}
@Override
@@ -1969,7 +1970,7 @@ private TwidereService getOuterType() {
class GetSentDirectMessagesTask extends GetDirectMessagesTask {
public GetSentDirectMessagesTask(final long[] account_ids, final long[] max_ids, final long[] since_ids) {
- super(account_ids, max_ids, since_ids);
+ super(account_ids, max_ids, since_ids, TASK_TAG_GET_SENT_DIRECT_MESSAGES);
}
@Override
@@ -1992,8 +1993,8 @@ protected void onPostExecute(final List<StatusesListResponse<DirectMessage>> res
private final long[] account_ids, max_ids, since_ids;
- public GetStatusesTask(final long[] account_ids, final long[] max_ids, final long[] since_ids) {
- super(TwidereService.this, mAsyncTaskManager);
+ public GetStatusesTask(final long[] account_ids, final long[] max_ids, final long[] since_ids, final String tag) {
+ super(TwidereService.this, mAsyncTaskManager, tag);
this.account_ids = account_ids;
this.max_ids = max_ids;
this.since_ids = since_ids;
@@ -2783,8 +2784,8 @@ public StatusesListResponse(final long account_id, final long max_id, final long
private final Uri uri;
public StoreDirectMessagesTask(final List<StatusesListResponse<DirectMessage>> result, final Uri uri,
- final boolean notify) {
- super(TwidereService.this, mAsyncTaskManager);
+ final boolean notify, final String tag) {
+ super(TwidereService.this, mAsyncTaskManager, tag);
responses = result;
this.uri = uri.buildUpon().appendQueryParameter(QUERY_PARAM_NOTIFY, String.valueOf(notify)).build();
}
@@ -2870,7 +2871,7 @@ private TwidereService getOuterType() {
public StoreHomeTimelineTask(final List<StatusesListResponse<twitter4j.Status>> result,
final boolean should_set_min_id, final boolean notify) {
- super(result, Statuses.CONTENT_URI, should_set_min_id, notify);
+ super(result, Statuses.CONTENT_URI, should_set_min_id, notify, TASK_TAG_STORE_HOME_TIMELINE);
}
@Override
@@ -2926,7 +2927,7 @@ public StoreLocalTrendsTask(final ListResponse<Trends> result) {
public StoreMentionsTask(final List<StatusesListResponse<twitter4j.Status>> result,
final boolean should_set_min_id, final boolean notify) {
- super(result, Mentions.CONTENT_URI, should_set_min_id, notify);
+ super(result, Mentions.CONTENT_URI, should_set_min_id, notify, TASK_TAG_STORE_MENTIONS);
}
@Override
@@ -2974,7 +2975,7 @@ private TwidereService getOuterType() {
public StoreReceivedDirectMessagesTask(final List<StatusesListResponse<DirectMessage>> result,
final boolean notify) {
- super(result, DirectMessages.Inbox.CONTENT_URI, notify);
+ super(result, DirectMessages.Inbox.CONTENT_URI, notify, TASK_TAG_STORE_RECEIVED_DIRECT_MESSAGES);
}
@Override
@@ -3019,7 +3020,7 @@ boolean isOutgoing() {
class StoreSentDirectMessagesTask extends StoreDirectMessagesTask {
public StoreSentDirectMessagesTask(final List<StatusesListResponse<DirectMessage>> result, final boolean notify) {
- super(result, DirectMessages.Outbox.CONTENT_URI, notify);
+ super(result, DirectMessages.Outbox.CONTENT_URI, notify, TASK_TAG_STORE_SENT_DIRECT_MESSAGES);
}
@Override
@@ -3046,8 +3047,8 @@ boolean isOutgoing() {
private final ArrayList<ContentValues> all_statuses = new ArrayList<ContentValues>();
public StoreStatusesTask(final List<StatusesListResponse<twitter4j.Status>> result, final Uri uri,
- final boolean should_set_min_id, final boolean notify) {
- super(TwidereService.this, mAsyncTaskManager);
+ final boolean should_set_min_id, final boolean notify, final String tag) {
+ super(TwidereService.this, mAsyncTaskManager, tag);
responses = result;
this.should_set_min_id = should_set_min_id;
this.uri = uri.buildUpon().appendQueryParameter(QUERY_PARAM_NOTIFY, String.valueOf(notify)).build();
View
44 src/org/mariotaku/twidere/util/AsyncTaskManager.java
@@ -22,14 +22,13 @@
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
-@SuppressWarnings({ "rawtypes", "unchecked" })
public class AsyncTaskManager {
- private final ArrayList<ManagedAsyncTask> mTasks = new ArrayList<ManagedAsyncTask>();
+ private final ArrayList<ManagedAsyncTask<?, ?, ?>> mTasks = new ArrayList<ManagedAsyncTask<?, ?, ?>>();
private static AsyncTaskManager sInstance;
- public <T> int add(final ManagedAsyncTask task, final boolean exec, final T... params) {
+ public <T> int add(final ManagedAsyncTask<T, ?, ?> task, final boolean exec, final T... params) {
final int hashCode = task.hashCode();
mTasks.add(task);
if (exec) {
@@ -56,14 +55,14 @@ public boolean cancel(final int hashCode, final boolean mayInterruptIfRunning) {
* Cancel all tasks added, then clear all tasks.
*/
public void cancelAll() {
- for (final ManagedAsyncTask task : mTasks) {
+ for (final ManagedAsyncTask<?, ?, ?> task : getTaskSpecList()) {
task.cancel(true);
}
mTasks.clear();
}
public <T> boolean execute(final int hashCode, final T... params) {
- final ManagedAsyncTask task = findTask(hashCode);
+ final ManagedAsyncTask<?, ?, ?> task = findTask(hashCode);
if (task != null) {
task.execute(params == null || params.length == 0 ? null : params);
return true;
@@ -71,21 +70,27 @@ public void cancelAll() {
return false;
}
- public ArrayList<ManagedAsyncTask<?, ?, ?>> getTaskList() {
- return (ArrayList<ManagedAsyncTask<?, ?, ?>>) mTasks.clone();
+ public ArrayList<ManagedAsyncTask<?, ?, ?>> getTaskSpecList() {
+ return new ArrayList<ManagedAsyncTask<?, ?, ?>>(mTasks);
}
public boolean hasRunningTask() {
- final ArrayList<ManagedAsyncTask> tasks_to_remove = new ArrayList<ManagedAsyncTask>();
- for (final ManagedAsyncTask task : getTaskList()) {
- if (task.getStatus() != ManagedAsyncTask.Status.RUNNING) {
- tasks_to_remove.add(task);
+ for (final ManagedAsyncTask<?, ?, ?> task : getTaskSpecList()) {
+ if (task.getStatus() == ManagedAsyncTask.Status.RUNNING) {
+ return true;
}
}
- for (final ManagedAsyncTask task : tasks_to_remove) {
- remove(task.hashCode());
+ return false;
+ }
+
+ public boolean hasRunningTasksForTag(final String tag) {
+ if (tag == null) return false;
+ for (final ManagedAsyncTask<?, ?, ?> task : getTaskSpecList()) {
+ if (task.getStatus() == ManagedAsyncTask.Status.RUNNING && tag.equals(task.getTag())) {
+ return true;
+ }
}
- return mTasks.size() > 0;
+ return false;
}
public boolean isExcuting(final int hashCode) {
@@ -102,13 +107,9 @@ public void remove(final int hashCode) {
}
}
- private ManagedAsyncTask findTask(final int hashCode) {
- try {
- for (final ManagedAsyncTask task : getTaskList()) {
- if (hashCode == task.hashCode()) return task;
- }
- } catch (final ConcurrentModificationException e) {
-
+ private ManagedAsyncTask<?, ?, ?> findTask(final int hashCode) {
+ for (final ManagedAsyncTask<?, ?, ?> task : getTaskSpecList()) {
+ if (hashCode == task.hashCode()) return task;
}
return null;
}
@@ -119,4 +120,5 @@ public static AsyncTaskManager getInstance() {
}
return sInstance;
}
+
}
View
10 src/org/mariotaku/twidere/util/ManagedAsyncTask.java
@@ -29,10 +29,16 @@
private final AsyncTaskManager manager;
private final Context context;
+ private final String tag;
public ManagedAsyncTask(final Context context, final AsyncTaskManager manager) {
+ this(context, manager, null);
+ }
+
+ public ManagedAsyncTask(final Context context, final AsyncTaskManager manager, final String tag) {
this.manager = manager;
this.context = context;
+ this.tag = tag;
}
@Override
@@ -61,5 +67,9 @@ protected void onPreExecute() {
context.sendBroadcast(new Intent(BROADCAST_REFRESHSTATE_CHANGED).putExtra(INTENT_KEY_HAS_RUNNING_TASK,
manager.hasRunningTask()));
}
+
+ public String getTag() {
+ return tag;
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.