Closed devjoseq closed 7 months ago
I will update it shortly(day/s).
I'm looking forward to it, thank you for your help
public class Test {
public void main(Context context, LifecycleOwner lifecycleOwner) {
////////////////////// Single Download /////////////////////////////////
// lifecycle will make ur life cycle harder if you don't know it well.
// so avoid it
final boolean downloadCallbacksOnMainThread = true; // false if u want background thread for callbacks
ScheduledBackgroundExecutorImpl executor = new ScheduledBackgroundExecutorImpl(_Kt.DEF_MAX_THREADS_PER_EXECUTOR, null);
// if u want download callbacks to invoke on non ui thread. then pass non null BackgroundExecutor but don't touch {executor object} create new one. it's ok...as you may suffer download callbacks due to busy threads.
DownloadCallbacksHandler downloadCallbacksHandler = new DownloadCallbacksHandler(downloadCallbacksOnMainThread, null, new BackgroundExecutorImpl());
File directoryWhereYouWantToSaveFiles = null;
FileStorageHelper storageHelper = new DefaultFileStorageHelper(context);
if (directoryWhereYouWantToSaveFiles == null) {
storageHelper.setFilesRootToDownloadsOrFallbackToInternalDirectory();
} else {
storageHelper.setFilesRoot(directoryWhereYouWantToSaveFiles);
}
Factory<URLConnectionHandler> connectionHandlerFactory = null; // in case u want ur own things.
ConnectionManager connectionManager = new ConnectionManagerImpl(connectionHandlerFactory == null ? new URLConnectionFactory() : connectionHandlerFactory, executor);
SQLiteManager databaseManager = SQLiteManager.Companion.getInstance(context);
NetworkInfoProvider networkInfoProvider = new NetworkInfoProvider(context);
/**
* remember you don't need to overload all the methods. just overload what you want.
*/
DownloadProgressListener downloadProgressListener = new DownloadProgressListener() {
@Override
public void onConnectionEstablished(DownloadInfo downloadInfo) {
DownloadProgressListener.super.onConnectionEstablished(downloadInfo);
System.out.println("log: Download connection established.");
}
@Override
public void onDownloadProgress(DownloadInfo downloadInfo) {
DownloadProgressListener.super.onDownloadProgress(downloadInfo);
System.out.println("log: on Progress "+downloadInfo.getProgress());
}
@Override
public void onDownloadFailed(@NonNull DownloadInfo downloadInfo, @NonNull String ex) {
DownloadProgressListener.super.onDownloadFailed(downloadInfo, ex);
System.out.println("log: failed download ->"+ex);
}
@Override
public void onDownloadSuccess(@NonNull DownloadInfo downloadInfo) {
DownloadProgressListener.super.onDownloadSuccess(downloadInfo);
System.out.println("marko: file downloaded successfully at ->"+downloadInfo.getFilePath());
}
};
/**
* Bonus
* remember progress -1.0 means indeterminate -> downloader unable to determine the total bytes or whatsoever.
*/
DownloadManager downloadManager = new DownloadManager.Builder(context).
setCallbacksHandler(downloadCallbacksHandler).
setDownloadDatabaseManager(databaseManager).
setConnectionManager(connectionManager).
setNetworkInfoProvider(networkInfoProvider).
setStorageHelper(storageHelper).build();
downloadManager.download("https://github.com/tanoDxyz/GDownload", "heelo.txt", NetworkType.ALL, downloadProgressListener);
// it is best to clean up.if downloader is not required.
downloadManager.shutDown(null);
////////////////////////////// Single download code ends here //////////////////////////////////////////////////////////////////////
Group group = new GroupImpl.Builder(context).
setNetworkInfoProvider(networkInfoProvider).
setConcurrentDownloadsCapacity(10).
setConnectionRetryCount(10).
setDatabaseManager(databaseManager).
setGroupLoopTimeMilliSecs(1000).
setMaxConnectionsPerDownload(32)
.setProgressUpdateTimeMilliSecs(1000)
.setFileSaveRootPath(directoryWhereYouWantToSaveFiles)
.setUrlConnectionFactory(connectionHandlerFactory == null ? new URLConnectionFactory() : connectionHandlerFactory)
.build();
//////////////////////////////////////////// group downlods /////////////////////////////////////////////////////////////////
//to add group progress listener to manage multiple downloads or treat them as a whole
GroupListener groupListener = new GroupListener() {
@Override
public void onEnqueued(long l, @NonNull DownloadInfo downloadInfo, @NonNull GroupState groupState) {
}
@Override
public void onAdded(long l, @NonNull DownloadInfo downloadInfo, @NonNull GroupState groupState) {
}
@Override
public void onStarting(long l, @NonNull DownloadInfo downloadInfo, @NonNull GroupState groupState) {
}
@Override
public void onDownloading(long l, @NonNull DownloadInfo downloadInfo, @NonNull GroupState groupState) {
}
@Override
public void onSuccess(long l, @NonNull DownloadInfo downloadInfo, @NonNull GroupState groupState) {
}
@Override
public void onFailure(long l, @Nullable String s, @NonNull DownloadInfo downloadInfo, @NonNull GroupState groupState) {
}
@Override
public void onPaused(long l, @NonNull DownloadInfo downloadInfo, @NonNull GroupState groupState) {
}
@Override
public void onStopped(long l, @NonNull DownloadInfo downloadInfo, @NonNull GroupState groupState) {
}
@Override
public void onWaitingForTurn(long l, @NonNull DownloadInfo downloadInfo, @NonNull GroupState groupState) {
}
};
group.addGroupProgressListener(groupListener);
group.start(); // this call is necessary. this starts internal loop.
Long id = group.add("downloadUrl", "fileName", downloadProgressListener);
group.startDownload(id);
//////////////////////////////////////////////// group downloads ends here ///////////////////////////
}
}
You can use the Kotlin way in java too but it's little weird to say.
Hello, is there a document explaining how we can use this library in Java?