|
|
@ -15,6 +15,7 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package com.arialyy.aria.core.manager; |
|
|
|
package com.arialyy.aria.core.manager; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.support.v4.util.LruCache; |
|
|
|
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; |
|
|
|
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; |
|
|
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
|
|
|
import com.arialyy.aria.core.download.DownloadTaskEntity; |
|
|
|
import com.arialyy.aria.core.inf.AbsEntity; |
|
|
|
import com.arialyy.aria.core.inf.AbsEntity; |
|
|
@ -23,8 +24,6 @@ import com.arialyy.aria.core.upload.UploadTaskEntity; |
|
|
|
import com.arialyy.aria.util.ALog; |
|
|
|
import com.arialyy.aria.util.ALog; |
|
|
|
import com.arialyy.aria.util.CommonUtil; |
|
|
|
import com.arialyy.aria.util.CommonUtil; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
|
|
|
|
import java.util.concurrent.locks.Lock; |
|
|
|
import java.util.concurrent.locks.Lock; |
|
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
|
|
|
|
|
|
|
|
@ -35,7 +34,7 @@ import java.util.concurrent.locks.ReentrantLock; |
|
|
|
public class TEManager { |
|
|
|
public class TEManager { |
|
|
|
private static final String TAG = "TaskManager"; |
|
|
|
private static final String TAG = "TaskManager"; |
|
|
|
private static volatile TEManager INSTANCE = null; |
|
|
|
private static volatile TEManager INSTANCE = null; |
|
|
|
private Map<String, AbsTaskEntity> map = new ConcurrentHashMap<>(); |
|
|
|
private LruCache<String, AbsTaskEntity> cache = new LruCache<>(1024); |
|
|
|
private Lock lock; |
|
|
|
private Lock lock; |
|
|
|
|
|
|
|
|
|
|
|
public static TEManager getInstance() { |
|
|
|
public static TEManager getInstance() { |
|
|
@ -61,7 +60,7 @@ public class TEManager { |
|
|
|
final Lock lock = this.lock; |
|
|
|
final Lock lock = this.lock; |
|
|
|
lock.lock(); |
|
|
|
lock.lock(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
AbsTaskEntity tEntity = map.get(convertKey(key)); |
|
|
|
AbsTaskEntity tEntity = cache.get(convertKey(key)); |
|
|
|
if (tEntity == null || tEntity.getClass() != clazz) { |
|
|
|
if (tEntity == null || tEntity.getClass() != clazz) { |
|
|
|
ITEntityFactory factory = chooseFactory(clazz); |
|
|
|
ITEntityFactory factory = chooseFactory(clazz); |
|
|
|
if (factory == null) { |
|
|
|
if (factory == null) { |
|
|
@ -69,7 +68,7 @@ public class TEManager { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
tEntity = factory.create(key); |
|
|
|
tEntity = factory.create(key); |
|
|
|
map.put(convertKey(key), tEntity); |
|
|
|
cache.put(convertKey(key), tEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
return (TE) tEntity; |
|
|
|
return (TE) tEntity; |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
@ -87,7 +86,7 @@ public class TEManager { |
|
|
|
lock.lock(); |
|
|
|
lock.lock(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
String groupName = CommonUtil.getMd5Code(urls); |
|
|
|
String groupName = CommonUtil.getMd5Code(urls); |
|
|
|
AbsTaskEntity tEntity = map.get(convertKey(groupName)); |
|
|
|
AbsTaskEntity tEntity = cache.get(convertKey(groupName)); |
|
|
|
if (tEntity == null || tEntity.getClass() != clazz) { |
|
|
|
if (tEntity == null || tEntity.getClass() != clazz) { |
|
|
|
IGTEntityFactory factory = chooseGroupFactory(clazz); |
|
|
|
IGTEntityFactory factory = chooseGroupFactory(clazz); |
|
|
|
if (factory == null) { |
|
|
|
if (factory == null) { |
|
|
@ -95,7 +94,7 @@ public class TEManager { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
tEntity = factory.create(groupName, urls); |
|
|
|
tEntity = factory.create(groupName, urls); |
|
|
|
map.put(convertKey(groupName), tEntity); |
|
|
|
cache.put(convertKey(groupName), tEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
return (TE) tEntity; |
|
|
|
return (TE) tEntity; |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
@ -112,7 +111,7 @@ public class TEManager { |
|
|
|
final Lock lock = this.lock; |
|
|
|
final Lock lock = this.lock; |
|
|
|
lock.lock(); |
|
|
|
lock.lock(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
AbsTaskEntity tEntity = map.get(convertKey(absEntity.getKey())); |
|
|
|
AbsTaskEntity tEntity = cache.get(convertKey(absEntity.getKey())); |
|
|
|
if (tEntity == null || tEntity.getClass() != clazz) { |
|
|
|
if (tEntity == null || tEntity.getClass() != clazz) { |
|
|
|
ITEntityFactory factory = chooseFactory(clazz); |
|
|
|
ITEntityFactory factory = chooseFactory(clazz); |
|
|
|
if (factory == null) { |
|
|
|
if (factory == null) { |
|
|
@ -120,7 +119,7 @@ public class TEManager { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
tEntity = factory.create(absEntity); |
|
|
|
tEntity = factory.create(absEntity); |
|
|
|
map.put(convertKey(absEntity.getKey()), tEntity); |
|
|
|
cache.put(convertKey(absEntity.getKey()), tEntity); |
|
|
|
} |
|
|
|
} |
|
|
|
return (TE) tEntity; |
|
|
|
return (TE) tEntity; |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
@ -153,7 +152,7 @@ public class TEManager { |
|
|
|
final Lock lock = this.lock; |
|
|
|
final Lock lock = this.lock; |
|
|
|
lock.lock(); |
|
|
|
lock.lock(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
AbsTaskEntity tEntity = map.get(convertKey(key)); |
|
|
|
AbsTaskEntity tEntity = cache.get(convertKey(key)); |
|
|
|
if (tEntity == null) { |
|
|
|
if (tEntity == null) { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -177,7 +176,7 @@ public class TEManager { |
|
|
|
final Lock lock = this.lock; |
|
|
|
final Lock lock = this.lock; |
|
|
|
lock.lock(); |
|
|
|
lock.lock(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
return map.put(convertKey(te.key), te) != null; |
|
|
|
return cache.put(convertKey(te.key), te) != null; |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
lock.unlock(); |
|
|
|
lock.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
@ -190,7 +189,7 @@ public class TEManager { |
|
|
|
final Lock lock = this.lock; |
|
|
|
final Lock lock = this.lock; |
|
|
|
lock.lock(); |
|
|
|
lock.lock(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
return map.remove(convertKey(key)); |
|
|
|
return cache.remove(convertKey(key)); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
lock.unlock(); |
|
|
|
lock.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|