DownloadManager : Improve Download Manager

This commit is contained in:
Mr. Dragon
2019-05-09 20:46:53 +05:30
parent 2f8bd7cad9
commit 7b37c87aa3
12 changed files with 34 additions and 40 deletions

View File

@@ -108,7 +108,7 @@ public class DownloadsActivity extends AppCompatActivity {
}
private void init() {
fetch = new DownloadManager(this).getFetchInstance();
fetch = DownloadManager.getFetchInstance(this);
fetch.getDownloads(downloadList -> {
if (downloadList.isEmpty()) {
ViewUtil.hideWithAnimation(mRecyclerView);

View File

@@ -65,7 +65,7 @@ public class DownloadsAdapter extends RecyclerView.Adapter<DownloadsAdapter.View
public DownloadsAdapter(Context context, List<Download> downloadList) {
this.context = context;
this.downloadList = downloadList;
fetch = new DownloadManager(context).getFetchInstance();
fetch = DownloadManager.getFetchInstance(context);
}
public void refreshList() {

View File

@@ -23,10 +23,7 @@ package com.aurora.store.download;
import android.content.Context;
import com.aurora.store.Constants;
import com.aurora.store.NotificationProvider;
import com.aurora.store.utility.NotificationUtil;
import com.aurora.store.utility.Util;
import com.tonyodev.fetch2.DefaultFetchNotificationManager;
import com.tonyodev.fetch2.Fetch;
import com.tonyodev.fetch2.FetchConfiguration;
import com.tonyodev.fetch2okhttp.OkHttpDownloader;
@@ -37,38 +34,41 @@ import okhttp3.OkHttpClient;
public class DownloadManager {
private Context context;
private Fetch fetch;
private static volatile DownloadManager instance;
private static Fetch fetch;
public DownloadManager(Context context) {
this.context = context;
init();
public DownloadManager() {
if (instance != null) {
throw new RuntimeException("Use get() method to get the single instance of RxBus");
}
}
private void init() {
public static Fetch getFetchInstance(Context context) {
if (instance == null) {
synchronized (DownloadManager.class) {
if (instance == null) {
instance = new DownloadManager();
fetch = getFetch(context);
}
}
}
return fetch;
}
private static Fetch getFetch(Context context) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(20, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
if (Util.isNetworkProxyEnabled(context))
builder.proxy(Util.getNetworkProxy(context));
OkHttpClient okHttpClient = builder.build();
FetchConfiguration.Builder fetchConfiguration = new FetchConfiguration.Builder(context);
fetchConfiguration.setDownloadConcurrentLimit(Util.getActiveDownloadCount(context));
fetchConfiguration.setHttpDownloader(new OkHttpDownloader(okHttpClient, Util.getDownloadStrategy(context)));
fetchConfiguration.setNamespace(Constants.TAG);
if (NotificationUtil.isNotificationEnabled(context) &&
NotificationUtil.getNotificationProvider(context) == NotificationProvider.NATIVE)
fetchConfiguration.setNotificationManager(new DefaultFetchNotificationManager(context));
fetchConfiguration.setDownloadConcurrentLimit(Util.getActiveDownloadCount(context));
fetch = Fetch.Impl.getInstance(fetchConfiguration.build());
if (Util.isFetchDebugEnabled(context))
fetch.enableLogging(true);
}
public Fetch getFetchInstance() {
return fetch;
fetchConfiguration.enableLogging(Util.isFetchDebugEnabled(context));
return Fetch.Impl.getInstance(fetchConfiguration.build());
}
}

View File

@@ -128,7 +128,7 @@ public class UpdatesFragment extends BaseFragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
fetch = new DownloadManager(context).getFetchInstance();
fetch = DownloadManager.getFetchInstance(context);
setErrorView(ErrorType.UNKNOWN);
customSwipeToRefresh.setOnRefreshListener(() -> fetchData());
if (getActivity() instanceof AuroraActivity) {
@@ -156,11 +156,8 @@ public class UpdatesFragment extends BaseFragment {
public void onDestroy() {
super.onDestroy();
disposable.clear();
pseudoPackageAppMap = null;
updatableAppList = null;
updatableAppTask = null;
adapter = null;
requestList = null;
}
@Override

View File

@@ -28,7 +28,6 @@ import android.content.res.Configuration;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
@@ -136,7 +135,7 @@ public class ActionButton extends AbstractHelper {
if (app.isInstalled())
runOrUpdate();
fetch = new DownloadManager(context).getFetchInstance();
fetch = DownloadManager.getFetchInstance(context);
notification = new GeneralNotification(context, app);
fetch.getDownloads(downloadList -> {

View File

@@ -78,7 +78,8 @@ public class DownloaderFragment extends PreferenceFragmentCompat implements Shar
seekBarPreference.setMin(1);
seekBarPreference.setOnPreferenceChangeListener((preference, newValue) -> {
int value = (Integer) newValue;
PrefUtil.putInteger(context, Constants.PREFERENCE_DOWNLOAD_ACTIVE, value - 1);
PrefUtil.putInteger(context, Constants.PREFERENCE_DOWNLOAD_ACTIVE, value);
SettingsActivity.shouldRestart = true;
return true;
});
}

View File

@@ -124,7 +124,7 @@ public class FavouriteFragment extends BaseFragment implements SelectableViewHol
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
manager = new FavouriteListManager(context);
fetch = new DownloadManager(context).getFetchInstance();
fetch = DownloadManager.getFetchInstance(context);
}
@Override

View File

@@ -36,8 +36,7 @@ public class DownloadCancelReceiver extends BroadcastReceiver {
Bundle extras = intent.getExtras();
if ((extras != null)) {
final int requestId = extras.getInt(REQUEST_ID, -1);
final DownloadManager mDownloadManager = new DownloadManager(context);
final Fetch fetch = mDownloadManager.getFetchInstance();
final Fetch fetch = DownloadManager.getFetchInstance(context);
fetch.cancel(requestId);
}
}

View File

@@ -35,8 +35,7 @@ public class DownloadPauseReceiver extends BroadcastReceiver {
Bundle extras = intent.getExtras();
if ((extras != null)) {
final int requestId = extras.getInt(REQUEST_ID, -1);
final DownloadManager mDownloadManager = new DownloadManager(context);
mDownloadManager.getFetchInstance().pause(requestId);
DownloadManager.getFetchInstance(context).pause(requestId);
}
}
}

View File

@@ -35,8 +35,7 @@ public class DownloadResumeReceiver extends BroadcastReceiver {
Bundle extras = intent.getExtras();
if ((extras != null)) {
final int requestId = extras.getInt(REQUEST_ID, -1);
final DownloadManager mDownloadManager = new DownloadManager(context);
mDownloadManager.getFetchInstance().resume(requestId);
DownloadManager.getFetchInstance(context).resume(requestId);
}
}
}

View File

@@ -102,7 +102,7 @@ public class DownloadMenuSheet extends CustomBottomSheetDialogFragment implement
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
fetch = new DownloadManager(context).getFetchInstance();
fetch = DownloadManager.getFetchInstance(context);
downloadTitle.setText(getTitle());
menuRecyclerView.setNestedScrollingEnabled(false);
menuRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));

View File

@@ -367,7 +367,7 @@ public class Util {
public static Proxy getNetworkProxy(Context context) {
String proxyHost = getPrefs(context).getString(Constants.PREFERENCE_PROXY_HOST, "127.0.0.1");
String proxyPort = getPrefs(context).getString(Constants.PREFERENCE_PROXY_PORT, "8118");
int port = proxyPort != null ? Integer.valueOf(proxyPort) : 8118;
int port = Util.parseInt(proxyPort, 8118);
return new Proxy(getProxyType(context), new InetSocketAddress(proxyHost, port));
}