add flutter

This commit is contained in:
Ariska
2026-03-11 15:29:37 +07:00
parent c253e1a370
commit 619d758027
9490 changed files with 135801 additions and 1353 deletions

View File

@@ -0,0 +1 @@
/build

View File

@@ -0,0 +1,28 @@
apply plugin: 'com.android.library'
android {
namespace 'com.downloader'
compileSdk 33
defaultConfig {
minSdkVersion 21
targetSdkVersion 33
versionCode 1
versionName "1.0.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,26 @@
package com.downloader;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.downloader.test", appContext.getPackageName());
}
}

View File

@@ -0,0 +1,26 @@
package com.downloader;
/**
* Created by amitshekhar on 13/11/17.
*/
public final class Constants {
private Constants() {
// no instance
}
public static final int UPDATE = 0x01;
public static final String RANGE = "Range";
public static final String ETAG = "ETag";
public static final String USER_AGENT = "User-Agent";
public static final String DEFAULT_USER_AGENT = "PRDownloader";
public static final int DEFAULT_READ_TIMEOUT_IN_MILLS = 20_000;
public static final int DEFAULT_CONNECT_TIMEOUT_IN_MILLS = 20_000;
public static final int HTTP_RANGE_NOT_SATISFIABLE = 416;
public static final int HTTP_TEMPORARY_REDIRECT = 307;
public static final int HTTP_PERMANENT_REDIRECT = 308;
}

View File

@@ -0,0 +1,66 @@
package com.downloader;
import java.util.List;
import java.util.Map;
/**
* Created by amitshekhar on 13/11/17.
*/
public class Error {
private boolean isServerError;
private boolean isConnectionError;
private String serverErrorMessage;
private Map<String, List<String>> headerFields;
private Throwable connectionException;
private int responseCode;
public boolean isServerError() {
return isServerError;
}
public void setServerError(boolean serverError) {
isServerError = serverError;
}
public boolean isConnectionError() {
return isConnectionError;
}
public void setConnectionError(boolean connectionError) {
isConnectionError = connectionError;
}
public void setServerErrorMessage(String serverErrorMessage) {
this.serverErrorMessage = serverErrorMessage;
}
public String getServerErrorMessage() {
return serverErrorMessage;
}
public void setHeaderFields(Map<String, List<String>> headerFields) {
this.headerFields = headerFields;
}
public Map<String, List<String>> getHeaderFields() {
return headerFields;
}
public void setConnectionException(Throwable connectionException) {
this.connectionException = connectionException;
}
public Throwable getConnectionException() {
return connectionException;
}
public void setResponseCode(int responseCode) {
this.responseCode = responseCode;
}
public int getResponseCode() {
return responseCode;
}
}

View File

@@ -0,0 +1,11 @@
package com.downloader;
/**
* Created by amitshekhar on 15/11/17.
*/
public interface OnCancelListener {
void onCancel();
}

View File

@@ -0,0 +1,13 @@
package com.downloader;
/**
* Created by amitshekhar on 13/11/17.
*/
public interface OnDownloadListener {
void onDownloadComplete();
void onError(Error error);
}

View File

@@ -0,0 +1,11 @@
package com.downloader;
/**
* Created by amitshekhar on 13/11/17.
*/
public interface OnPauseListener {
void onPause();
}

View File

@@ -0,0 +1,11 @@
package com.downloader;
/**
* Created by amitshekhar on 13/11/17.
*/
public interface OnProgressListener {
void onProgress(Progress progress);
}

View File

@@ -0,0 +1,11 @@
package com.downloader;
/**
* Created by amitshekhar on 15/11/17.
*/
public interface OnStartOrResumeListener {
void onStartOrResume();
}

View File

@@ -0,0 +1,129 @@
package com.downloader;
/**
* Created by amitshekhar on 12/11/17.
*/
import android.content.Context;
import com.downloader.core.Core;
import com.downloader.internal.ComponentHolder;
import com.downloader.internal.DownloadRequestQueue;
import com.downloader.request.DownloadRequestBuilder;
import com.downloader.utils.Utils;
/**
* PRDownloader entry point.
* You must initialize this class before use. The simplest way is to just do
* {#code PRDownloader.initialize(context)}.
*/
public class PRDownloader {
/**
* private constructor to prevent instantiation of this class
*/
private PRDownloader() {
}
/**
* Initializes PRDownloader with the default config.
*
* @param context The context
*/
public static void initialize(Context context) {
initialize(context, PRDownloaderConfig.newBuilder().build());
}
/**
* Initializes PRDownloader with the custom config.
*
* @param context The context
* @param config The PRDownloaderConfig
*/
public static void initialize(Context context, PRDownloaderConfig config) {
ComponentHolder.getInstance().init(context, config);
DownloadRequestQueue.initialize();
}
/**
* Method to make download request
*
* @param url The url on which request is to be made
* @param dirPath The directory path on which file is to be saved
* @param fileName The file name with which file is to be saved
* @return the DownloadRequestBuilder
*/
public static DownloadRequestBuilder download(String url, String dirPath, String fileName) {
return new DownloadRequestBuilder(url, dirPath, fileName);
}
/**
* Method to pause request with the given downloadId
*
* @param downloadId The downloadId with which request is to be paused
*/
public static void pause(int downloadId) {
DownloadRequestQueue.getInstance().pause(downloadId);
}
/**
* Method to resume request with the given downloadId
*
* @param downloadId The downloadId with which request is to be resumed
*/
public static void resume(int downloadId) {
DownloadRequestQueue.getInstance().resume(downloadId);
}
/**
* Method to cancel request with the given downloadId
*
* @param downloadId The downloadId with which request is to be cancelled
*/
public static void cancel(int downloadId) {
DownloadRequestQueue.getInstance().cancel(downloadId);
}
/**
* Method to cancel requests with the given tag
*
* @param tag The tag with which requests are to be cancelled
*/
public static void cancel(Object tag) {
DownloadRequestQueue.getInstance().cancel(tag);
}
/**
* Method to cancel all requests
*/
public static void cancelAll() {
DownloadRequestQueue.getInstance().cancelAll();
}
/**
* Method to check the request with the given downloadId is running or not
*
* @param downloadId The downloadId with which request status is to be checked
* @return the running status
*/
public static Status getStatus(int downloadId) {
return DownloadRequestQueue.getInstance().getStatus(downloadId);
}
/**
* Method to clean up temporary resumed files which is older than the given day
*
* @param days the days
*/
public static void cleanUp(int days) {
Utils.deleteUnwantedModelsAndTempFiles(days);
}
/**
* Shuts PRDownloader down
*/
public static void shutDown() {
Core.shutDown();
}
}

View File

@@ -0,0 +1,107 @@
package com.downloader;
import com.downloader.httpclient.DefaultHttpClient;
import com.downloader.httpclient.HttpClient;
/**
* Created by amitshekhar on 13/11/17.
*/
public class PRDownloaderConfig {
private int readTimeout;
private int connectTimeout;
private String userAgent;
private HttpClient httpClient;
private boolean databaseEnabled;
private PRDownloaderConfig(Builder builder) {
this.readTimeout = builder.readTimeout;
this.connectTimeout = builder.connectTimeout;
this.userAgent = builder.userAgent;
this.httpClient = builder.httpClient;
this.databaseEnabled = builder.databaseEnabled;
}
public int getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public String getUserAgent() {
return userAgent;
}
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
public HttpClient getHttpClient() {
return httpClient;
}
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}
public boolean isDatabaseEnabled() {
return databaseEnabled;
}
public void setDatabaseEnabled(boolean databaseEnabled) {
this.databaseEnabled = databaseEnabled;
}
public static Builder newBuilder() {
return new Builder();
}
public static class Builder {
int readTimeout = Constants.DEFAULT_READ_TIMEOUT_IN_MILLS;
int connectTimeout = Constants.DEFAULT_CONNECT_TIMEOUT_IN_MILLS;
String userAgent = Constants.DEFAULT_USER_AGENT;
HttpClient httpClient = new DefaultHttpClient();
boolean databaseEnabled = false;
public Builder setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}
public Builder setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
public Builder setUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}
public Builder setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
return this;
}
public Builder setDatabaseEnabled(boolean databaseEnabled) {
this.databaseEnabled = databaseEnabled;
return this;
}
public PRDownloaderConfig build() {
return new PRDownloaderConfig(this);
}
}
}

View File

@@ -0,0 +1,32 @@
package com.downloader;
/**
* Created by amitshekhar on 13/11/17.
*/
/**
* Priority levels recognized by the request server.
*/
public enum Priority {
/**
* Lowest priority level. Used for prefetches of data.
*/
LOW,
/**
* Medium priority level. Used for warming of data that might soon get visible.
*/
MEDIUM,
/**
* Highest priority level. Used for data that are currently visible on screen.
*/
HIGH,
/**
* Highest priority level. Used for data that are required instantly(mainly for emergency).
*/
IMMEDIATE
}

View File

@@ -0,0 +1,26 @@
package com.downloader;
import java.io.Serializable;
/**
* Created by amitshekhar on 13/11/17.
*/
public class Progress implements Serializable {
public long currentBytes;
public long totalBytes;
public Progress(long currentBytes, long totalBytes) {
this.currentBytes = currentBytes;
this.totalBytes = totalBytes;
}
@Override
public String toString() {
return "Progress{" +
"currentBytes=" + currentBytes +
", totalBytes=" + totalBytes +
'}';
}
}

View File

@@ -0,0 +1,46 @@
package com.downloader;
/**
* Created by amitshekhar on 13/11/17.
*/
public class Response {
private Error error;
private boolean isSuccessful;
private boolean isPaused;
private boolean isCancelled;
public Error getError() {
return error;
}
public void setError(Error error) {
this.error = error;
}
public boolean isSuccessful() {
return isSuccessful;
}
public void setSuccessful(boolean successful) {
isSuccessful = successful;
}
public boolean isPaused() {
return isPaused;
}
public void setPaused(boolean paused) {
isPaused = paused;
}
public boolean isCancelled() {
return isCancelled;
}
public void setCancelled(boolean cancelled) {
isCancelled = cancelled;
}
}

View File

@@ -0,0 +1,23 @@
package com.downloader;
/**
* Created by amitshekhar on 15/11/17.
*/
public enum Status {
QUEUED,
RUNNING,
PAUSED,
COMPLETED,
CANCELLED,
FAILED,
UNKNOWN
}

View File

@@ -0,0 +1,36 @@
package com.downloader.core;
/**
* Created by amitshekhar on 13/11/17.
*/
public class Core {
private static Core instance = null;
private final ExecutorSupplier executorSupplier;
private Core() {
this.executorSupplier = new DefaultExecutorSupplier();
}
public static Core getInstance() {
if (instance == null) {
synchronized (Core.class) {
if (instance == null) {
instance = new Core();
}
}
}
return instance;
}
public ExecutorSupplier getExecutorSupplier() {
return executorSupplier;
}
public static void shutDown() {
if (instance != null) {
instance = null;
}
}
}

View File

@@ -0,0 +1,41 @@
package com.downloader.core;
import android.os.Process;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DefaultExecutorSupplier implements ExecutorSupplier {
private static final int DEFAULT_MAX_NUM_THREADS = 2 * Runtime.getRuntime().availableProcessors() + 1;
private final DownloadExecutor networkExecutor;
private final Executor backgroundExecutor;
private final Executor mainThreadExecutor;
DefaultExecutorSupplier() {
ThreadFactory backgroundPriorityThreadFactory = new PriorityThreadFactory(Process.THREAD_PRIORITY_BACKGROUND);
networkExecutor = new DownloadExecutor(DEFAULT_MAX_NUM_THREADS, backgroundPriorityThreadFactory);
backgroundExecutor = Executors.newSingleThreadExecutor();
mainThreadExecutor = new MainThreadExecutor();
}
@Override
public DownloadExecutor forDownloadTasks() {
return networkExecutor;
}
@Override
public Executor forBackgroundTasks() {
return backgroundExecutor;
}
@Override
public Executor forMainThreadTasks() {
return mainThreadExecutor;
}
}

View File

@@ -0,0 +1,27 @@
package com.downloader.core;
import com.downloader.internal.DownloadRunnable;
import java.util.concurrent.Future;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadExecutor extends ThreadPoolExecutor {
DownloadExecutor(int maxNumThreads, ThreadFactory threadFactory) {
super(maxNumThreads, maxNumThreads, 0, TimeUnit.MILLISECONDS, new PriorityBlockingQueue<Runnable>(), threadFactory);
}
@Override
public Future<?> submit(Runnable task) {
DownloadFutureTask futureTask = new DownloadFutureTask((DownloadRunnable) task);
execute(futureTask);
return futureTask;
}
}

View File

@@ -0,0 +1,27 @@
package com.downloader.core;
import com.downloader.Priority;
import com.downloader.internal.DownloadRunnable;
import java.util.concurrent.FutureTask;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadFutureTask extends FutureTask<DownloadRunnable> implements Comparable<DownloadFutureTask> {
private final DownloadRunnable runnable;
DownloadFutureTask(DownloadRunnable downloadRunnable) {
super(downloadRunnable, null);
this.runnable = downloadRunnable;
}
@Override
public int compareTo(DownloadFutureTask other) {
Priority p1 = runnable.priority;
Priority p2 = other.runnable.priority;
return (p1 == p2 ? runnable.sequence - other.runnable.sequence : p2.ordinal() - p1.ordinal());
}
}

View File

@@ -0,0 +1,17 @@
package com.downloader.core;
import java.util.concurrent.Executor;
/**
* Created by amitshekhar on 13/11/17.
*/
public interface ExecutorSupplier {
DownloadExecutor forDownloadTasks();
Executor forBackgroundTasks();
Executor forMainThreadTasks();
}

View File

@@ -0,0 +1,20 @@
package com.downloader.core;
import android.os.Handler;
import android.os.Looper;
import java.util.concurrent.Executor;
/**
* Created by amitshekhar on 13/11/17.
*/
public class MainThreadExecutor implements Executor {
private final Handler handler = new Handler(Looper.getMainLooper());
@Override
public void execute(Runnable runnable) {
handler.post(runnable);
}
}

View File

@@ -0,0 +1,34 @@
package com.downloader.core;
import android.os.Process;
import java.util.concurrent.ThreadFactory;
/**
* Created by amitshekhar on 13/11/17.
*/
public class PriorityThreadFactory implements ThreadFactory {
private final int mThreadPriority;
PriorityThreadFactory(int threadPriority) {
mThreadPriority = threadPriority;
}
@Override
public Thread newThread(final Runnable runnable) {
Runnable wrapperRunnable = new Runnable() {
@Override
public void run() {
try {
Process.setThreadPriority(mThreadPriority);
} catch (Throwable ignored) {
}
runnable.run();
}
};
return new Thread(wrapperRunnable);
}
}

View File

@@ -0,0 +1,154 @@
package com.downloader.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
/**
* Created by anandgaurav on 14-11-2017.
*/
public class AppDbHelper implements DbHelper {
public static final String TABLE_NAME = "prdownloader";
private final SQLiteDatabase db;
public AppDbHelper(Context context) {
DatabaseOpenHelper databaseOpenHelper = new DatabaseOpenHelper(context);
db = databaseOpenHelper.getWritableDatabase();
}
@Override
public DownloadModel find(int id) {
Cursor cursor = null;
DownloadModel model = null;
try {
cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " +
DownloadModel.ID + " = " + id, null);
if (cursor != null && cursor.moveToFirst()) {
model = new DownloadModel();
model.setId(id);
model.setUrl(cursor.getString(cursor.getColumnIndex(DownloadModel.URL)));
model.setETag(cursor.getString(cursor.getColumnIndex(DownloadModel.ETAG)));
model.setDirPath(cursor.getString(cursor.getColumnIndex(DownloadModel.DIR_PATH)));
model.setFileName(cursor.getString(cursor.getColumnIndex(DownloadModel.FILE_NAME)));
model.setTotalBytes(cursor.getLong(cursor.getColumnIndex(DownloadModel.TOTAL_BYTES)));
model.setDownloadedBytes(cursor.getLong(cursor.getColumnIndex(DownloadModel.DOWNLOADED_BYTES)));
model.setLastModifiedAt(cursor.getLong(cursor.getColumnIndex(DownloadModel.LAST_MODIFIED_AT)));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return model;
}
@Override
public void insert(DownloadModel model) {
try {
ContentValues values = new ContentValues();
values.put(DownloadModel.ID, model.getId());
values.put(DownloadModel.URL, model.getUrl());
values.put(DownloadModel.ETAG, model.getETag());
values.put(DownloadModel.DIR_PATH, model.getDirPath());
values.put(DownloadModel.FILE_NAME, model.getFileName());
values.put(DownloadModel.TOTAL_BYTES, model.getTotalBytes());
values.put(DownloadModel.DOWNLOADED_BYTES, model.getDownloadedBytes());
values.put(DownloadModel.LAST_MODIFIED_AT, model.getLastModifiedAt());
db.insert(TABLE_NAME, null, values);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void update(DownloadModel model) {
try {
ContentValues values = new ContentValues();
values.put(DownloadModel.URL, model.getUrl());
values.put(DownloadModel.ETAG, model.getETag());
values.put(DownloadModel.DIR_PATH, model.getDirPath());
values.put(DownloadModel.FILE_NAME, model.getFileName());
values.put(DownloadModel.TOTAL_BYTES, model.getTotalBytes());
values.put(DownloadModel.DOWNLOADED_BYTES, model.getDownloadedBytes());
values.put(DownloadModel.LAST_MODIFIED_AT, model.getLastModifiedAt());
db.update(TABLE_NAME, values, DownloadModel.ID + " = ? ",
new String[]{String.valueOf(model.getId())});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void updateProgress(int id, long downloadedBytes, long lastModifiedAt) {
try {
ContentValues values = new ContentValues();
values.put(DownloadModel.DOWNLOADED_BYTES, downloadedBytes);
values.put(DownloadModel.LAST_MODIFIED_AT, lastModifiedAt);
db.update(TABLE_NAME, values, DownloadModel.ID + " = ? ",
new String[]{String.valueOf(id)});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void remove(int id) {
try {
db.execSQL("DELETE FROM " + TABLE_NAME + " WHERE " +
DownloadModel.ID + " = " + id);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public List<DownloadModel> getUnwantedModels(int days) {
List<DownloadModel> models = new ArrayList<>();
Cursor cursor = null;
try {
final long daysInMillis = days * 24 * 60 * 60 * 1000L;
final long beforeTimeInMillis = System.currentTimeMillis() - daysInMillis;
cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " +
DownloadModel.LAST_MODIFIED_AT + " <= " + beforeTimeInMillis, null);
if (cursor != null && cursor.moveToFirst()) {
do {
DownloadModel model = new DownloadModel();
model.setId(cursor.getInt(cursor.getColumnIndex(DownloadModel.ID)));
model.setUrl(cursor.getString(cursor.getColumnIndex(DownloadModel.URL)));
model.setETag(cursor.getString(cursor.getColumnIndex(DownloadModel.ETAG)));
model.setDirPath(cursor.getString(cursor.getColumnIndex(DownloadModel.DIR_PATH)));
model.setFileName(cursor.getString(cursor.getColumnIndex(DownloadModel.FILE_NAME)));
model.setTotalBytes(cursor.getLong(cursor.getColumnIndex(DownloadModel.TOTAL_BYTES)));
model.setDownloadedBytes(cursor.getLong(cursor.getColumnIndex(DownloadModel.DOWNLOADED_BYTES)));
model.setLastModifiedAt(cursor.getLong(cursor.getColumnIndex(DownloadModel.LAST_MODIFIED_AT)));
models.add(model);
} while (cursor.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return models;
}
@Override
public void clear() {
try {
db.delete(TABLE_NAME, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,40 @@
package com.downloader.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by anandgaurav on 14-11-2017.
*/
public class DatabaseOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "prdownloader.db";
private static final int DATABASE_VERSION = 1;
DatabaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " +
AppDbHelper.TABLE_NAME + "( " +
DownloadModel.ID + " INTEGER PRIMARY KEY, " +
DownloadModel.URL + " VARCHAR, " +
DownloadModel.ETAG + " VARCHAR, " +
DownloadModel.DIR_PATH + " VARCHAR, " +
DownloadModel.FILE_NAME + " VARCHAR, " +
DownloadModel.TOTAL_BYTES + " INTEGER, " +
DownloadModel.DOWNLOADED_BYTES + " INTEGER, " +
DownloadModel.LAST_MODIFIED_AT + " INTEGER " +
")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
}

View File

@@ -0,0 +1,25 @@
package com.downloader.database;
import java.util.List;
/**
* Created by anandgaurav on 14-11-2017.
*/
public interface DbHelper {
DownloadModel find(int id);
void insert(DownloadModel model);
void update(DownloadModel model);
void updateProgress(int id, long downloadedBytes, long lastModifiedAt);
void remove(int id);
List<DownloadModel> getUnwantedModels(int days);
void clear();
}

View File

@@ -0,0 +1,91 @@
package com.downloader.database;
/**
* Created by anandgaurav on 14-11-2017.
*/
public class DownloadModel {
static final String ID = "id";
static final String URL = "url";
static final String ETAG = "etag";
static final String DIR_PATH = "dir_path";
static final String FILE_NAME = "file_name";
static final String TOTAL_BYTES = "total_bytes";
static final String DOWNLOADED_BYTES = "downloaded_bytes";
static final String LAST_MODIFIED_AT = "last_modified_at";
private int id;
private String url;
private String eTag;
private String dirPath;
private String fileName;
private long totalBytes;
private long downloadedBytes;
private long lastModifiedAt;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getETag() {
return eTag;
}
public void setETag(String eTag) {
this.eTag = eTag;
}
public String getDirPath() {
return dirPath;
}
public void setDirPath(String dirPath) {
this.dirPath = dirPath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public long getTotalBytes() {
return totalBytes;
}
public void setTotalBytes(long totalBytes) {
this.totalBytes = totalBytes;
}
public long getDownloadedBytes() {
return downloadedBytes;
}
public void setDownloadedBytes(long downloadedBytes) {
this.downloadedBytes = downloadedBytes;
}
public long getLastModifiedAt() {
return lastModifiedAt;
}
public void setLastModifiedAt(long lastModifiedAt) {
this.lastModifiedAt = lastModifiedAt;
}
}

View File

@@ -0,0 +1,49 @@
package com.downloader.database;
import java.util.List;
/**
* Created by anandgaurav on 14-11-2017.
*/
public class NoOpsDbHelper implements DbHelper {
public NoOpsDbHelper() {
}
@Override
public DownloadModel find(int id) {
return null;
}
@Override
public void insert(DownloadModel model) {
}
@Override
public void update(DownloadModel model) {
}
@Override
public void updateProgress(int id, long downloadedBytes, long lastModifiedAt) {
}
@Override
public void remove(int id) {
}
@Override
public List<DownloadModel> getUnwantedModels(int days) {
return null;
}
@Override
public void clear() {
}
}

View File

@@ -0,0 +1,38 @@
package com.downloader.handler;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.downloader.Constants;
import com.downloader.Progress;
import com.downloader.OnProgressListener;
/**
* Created by amitshekhar on 13/11/17.
*/
public class ProgressHandler extends Handler {
private final OnProgressListener listener;
public ProgressHandler(OnProgressListener listener) {
super(Looper.getMainLooper());
this.listener = listener;
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Constants.UPDATE:
if (listener != null) {
final Progress progress = (Progress) msg.obj;
listener.onProgress(progress);
}
break;
default:
super.handleMessage(msg);
break;
}
}
}

View File

@@ -0,0 +1,111 @@
package com.downloader.httpclient;
import com.downloader.Constants;
import com.downloader.request.DownloadRequest;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DefaultHttpClient implements HttpClient {
private URLConnection connection;
public DefaultHttpClient() {
}
@SuppressWarnings("CloneDoesntCallSuperClone")
@Override
public HttpClient clone() {
return new DefaultHttpClient();
}
@Override
public void connect(DownloadRequest request) throws IOException {
connection = new URL(request.getUrl()).openConnection();
connection.setReadTimeout(request.getReadTimeout());
connection.setConnectTimeout(request.getConnectTimeout());
final String range = String.format(Locale.ENGLISH,
"bytes=%d-", request.getDownloadedBytes());
connection.addRequestProperty(Constants.RANGE, range);
connection.addRequestProperty(Constants.USER_AGENT, request.getUserAgent());
addHeaders(request);
connection.connect();
}
@Override
public int getResponseCode() throws IOException {
int responseCode = 0;
if (connection instanceof HttpURLConnection) {
responseCode = ((HttpURLConnection) connection).getResponseCode();
}
return responseCode;
}
@Override
public InputStream getInputStream() throws IOException {
return connection.getInputStream();
}
@Override
public long getContentLength() {
String length = connection.getHeaderField("Content-Length");
try {
return Long.parseLong(length);
} catch (NumberFormatException e) {
return -1;
}
}
@Override
public String getResponseHeader(String name) {
return connection.getHeaderField(name);
}
@Override
public void close() {
// no operation
}
@Override
public Map<String, List<String>> getHeaderFields() {
return connection.getHeaderFields();
}
@Override
public InputStream getErrorStream() {
if (connection instanceof HttpURLConnection) {
return ((HttpURLConnection) connection).getErrorStream();
}
return null;
}
private void addHeaders(DownloadRequest request) {
final HashMap<String, List<String>> headers = request.getHeaders();
if (headers != null) {
Set<Map.Entry<String, List<String>>> entries = headers.entrySet();
for (Map.Entry<String, List<String>> entry : entries) {
String name = entry.getKey();
List<String> list = entry.getValue();
if (list != null) {
for (String value : list) {
connection.addRequestProperty(name, value);
}
}
}
}
}
}

View File

@@ -0,0 +1,34 @@
package com.downloader.httpclient;
import com.downloader.request.DownloadRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
/**
* Created by amitshekhar on 13/11/17.
*/
public interface HttpClient extends Cloneable {
HttpClient clone();
void connect(DownloadRequest request) throws IOException;
int getResponseCode() throws IOException;
InputStream getInputStream() throws IOException;
long getContentLength();
String getResponseHeader(String name);
void close();
Map<String, List<String>> getHeaderFields();
InputStream getErrorStream() throws IOException;
}

View File

@@ -0,0 +1,97 @@
package com.downloader.internal;
import android.content.Context;
import com.downloader.Constants;
import com.downloader.PRDownloader;
import com.downloader.PRDownloaderConfig;
import com.downloader.database.AppDbHelper;
import com.downloader.database.DbHelper;
import com.downloader.database.NoOpsDbHelper;
import com.downloader.httpclient.DefaultHttpClient;
import com.downloader.httpclient.HttpClient;
/**
* Created by amitshekhar on 14/11/17.
*/
public class ComponentHolder {
private final static ComponentHolder INSTANCE = new ComponentHolder();
private int readTimeout;
private int connectTimeout;
private String userAgent;
private HttpClient httpClient;
private DbHelper dbHelper;
public static ComponentHolder getInstance() {
return INSTANCE;
}
public void init(Context context, PRDownloaderConfig config) {
this.readTimeout = config.getReadTimeout();
this.connectTimeout = config.getConnectTimeout();
this.userAgent = config.getUserAgent();
this.httpClient = config.getHttpClient();
this.dbHelper = config.isDatabaseEnabled() ? new AppDbHelper(context) : new NoOpsDbHelper();
if (config.isDatabaseEnabled()) {
PRDownloader.cleanUp(30);
}
}
public int getReadTimeout() {
if (readTimeout == 0) {
synchronized (ComponentHolder.class) {
if (readTimeout == 0) {
readTimeout = Constants.DEFAULT_READ_TIMEOUT_IN_MILLS;
}
}
}
return readTimeout;
}
public int getConnectTimeout() {
if (connectTimeout == 0) {
synchronized (ComponentHolder.class) {
if (connectTimeout == 0) {
connectTimeout = Constants.DEFAULT_CONNECT_TIMEOUT_IN_MILLS;
}
}
}
return connectTimeout;
}
public String getUserAgent() {
if (userAgent == null) {
synchronized (ComponentHolder.class) {
if (userAgent == null) {
userAgent = Constants.DEFAULT_USER_AGENT;
}
}
}
return userAgent;
}
public DbHelper getDbHelper() {
if (dbHelper == null) {
synchronized (ComponentHolder.class) {
if (dbHelper == null) {
dbHelper = new NoOpsDbHelper();
}
}
}
return dbHelper;
}
public HttpClient getHttpClient() {
if (httpClient == null) {
synchronized (ComponentHolder.class) {
if (httpClient == null) {
httpClient = new DefaultHttpClient();
}
}
}
return httpClient.clone();
}
}

View File

@@ -0,0 +1,118 @@
package com.downloader.internal;
import com.downloader.Status;
import com.downloader.core.Core;
import com.downloader.request.DownloadRequest;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadRequestQueue {
private static DownloadRequestQueue instance;
private final Map<Integer, DownloadRequest> currentRequestMap;
private final AtomicInteger sequenceGenerator;
private DownloadRequestQueue() {
currentRequestMap = new ConcurrentHashMap<>();
sequenceGenerator = new AtomicInteger();
}
public static void initialize() {
getInstance();
}
public static DownloadRequestQueue getInstance() {
if (instance == null) {
synchronized (DownloadRequestQueue.class) {
if (instance == null) {
instance = new DownloadRequestQueue();
}
}
}
return instance;
}
private int getSequenceNumber() {
return sequenceGenerator.incrementAndGet();
}
public void pause(int downloadId) {
DownloadRequest request = currentRequestMap.get(downloadId);
if (request != null) {
request.setStatus(Status.PAUSED);
}
}
public void resume(int downloadId) {
DownloadRequest request = currentRequestMap.get(downloadId);
if (request != null) {
request.setStatus(Status.QUEUED);
request.setFuture(Core.getInstance()
.getExecutorSupplier()
.forDownloadTasks()
.submit(new DownloadRunnable(request)));
}
}
private void cancelAndRemoveFromMap(DownloadRequest request) {
if (request != null) {
request.cancel();
currentRequestMap.remove(request.getDownloadId());
}
}
public void cancel(int downloadId) {
DownloadRequest request = currentRequestMap.get(downloadId);
cancelAndRemoveFromMap(request);
}
public void cancel(Object tag) {
for (Map.Entry<Integer, DownloadRequest> currentRequestMapEntry : currentRequestMap.entrySet()) {
DownloadRequest request = currentRequestMapEntry.getValue();
if (request.getTag() instanceof String && tag instanceof String) {
final String tempRequestTag = (String) request.getTag();
final String tempTag = (String) tag;
if (tempRequestTag.equals(tempTag)) {
cancelAndRemoveFromMap(request);
}
} else if (request.getTag().equals(tag)) {
cancelAndRemoveFromMap(request);
}
}
}
public void cancelAll() {
for (Map.Entry<Integer, DownloadRequest> currentRequestMapEntry : currentRequestMap.entrySet()) {
DownloadRequest request = currentRequestMapEntry.getValue();
cancelAndRemoveFromMap(request);
}
}
public Status getStatus(int downloadId) {
DownloadRequest request = currentRequestMap.get(downloadId);
if (request != null) {
return request.getStatus();
}
return Status.UNKNOWN;
}
public void addRequest(DownloadRequest request) {
currentRequestMap.put(request.getDownloadId(), request);
request.setStatus(Status.QUEUED);
request.setSequenceNumber(getSequenceNumber());
request.setFuture(Core.getInstance()
.getExecutorSupplier()
.forDownloadTasks()
.submit(new DownloadRunnable(request)));
}
public void finish(DownloadRequest request) {
currentRequestMap.remove(request.getDownloadId());
}
}

View File

@@ -0,0 +1,41 @@
package com.downloader.internal;
import com.downloader.Error;
import com.downloader.Priority;
import com.downloader.Response;
import com.downloader.Status;
import com.downloader.request.DownloadRequest;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadRunnable implements Runnable {
public final Priority priority;
public final int sequence;
public final DownloadRequest request;
DownloadRunnable(DownloadRequest request) {
this.request = request;
this.priority = request.getPriority();
this.sequence = request.getSequenceNumber();
}
@Override
public void run() {
request.setStatus(Status.RUNNING);
DownloadTask downloadTask = DownloadTask.create(request);
Response response = downloadTask.run();
if (response.isSuccessful()) {
request.deliverSuccess();
} else if (response.isPaused()) {
request.deliverPauseEvent();
} else if (response.getError() != null) {
request.deliverError(response.getError());
} else if (!response.isCancelled()) {
request.deliverError(new Error());
}
}
}

View File

@@ -0,0 +1,390 @@
package com.downloader.internal;
import com.downloader.Constants;
import com.downloader.Error;
import com.downloader.Progress;
import com.downloader.Response;
import com.downloader.Status;
import com.downloader.database.DownloadModel;
import com.downloader.handler.ProgressHandler;
import com.downloader.httpclient.HttpClient;
import com.downloader.internal.stream.FileDownloadOutputStream;
import com.downloader.internal.stream.FileDownloadRandomAccessFile;
import com.downloader.request.DownloadRequest;
import com.downloader.utils.Utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadTask {
private static final int BUFFER_SIZE = 1024 * 4;
private static final long TIME_GAP_FOR_SYNC = 2000;
private static final long MIN_BYTES_FOR_SYNC = 65536;
private final DownloadRequest request;
private ProgressHandler progressHandler;
private long lastSyncTime;
private long lastSyncBytes;
private InputStream inputStream;
private FileDownloadOutputStream outputStream;
private HttpClient httpClient;
private long totalBytes;
private int responseCode;
private String eTag;
private boolean isResumeSupported;
private String tempPath;
private DownloadTask(DownloadRequest request) {
this.request = request;
}
static DownloadTask create(DownloadRequest request) {
return new DownloadTask(request);
}
Response run() {
Response response = new Response();
if (request.getStatus() == Status.CANCELLED) {
response.setCancelled(true);
return response;
} else if (request.getStatus() == Status.PAUSED) {
response.setPaused(true);
return response;
}
try {
if (request.getOnProgressListener() != null) {
progressHandler = new ProgressHandler(request.getOnProgressListener());
}
tempPath = Utils.getTempPath(request.getDirPath(), request.getFileName());
File file = new File(tempPath);
DownloadModel model = getDownloadModelIfAlreadyPresentInDatabase();
if (model != null) {
if (file.exists()) {
request.setTotalBytes(model.getTotalBytes());
request.setDownloadedBytes(model.getDownloadedBytes());
} else {
removeNoMoreNeededModelFromDatabase();
request.setDownloadedBytes(0);
request.setTotalBytes(0);
model = null;
}
}
httpClient = ComponentHolder.getInstance().getHttpClient();
httpClient.connect(request);
if (request.getStatus() == Status.CANCELLED) {
response.setCancelled(true);
return response;
} else if (request.getStatus() == Status.PAUSED) {
response.setPaused(true);
return response;
}
httpClient = Utils.getRedirectedConnectionIfAny(httpClient, request);
responseCode = httpClient.getResponseCode();
eTag = httpClient.getResponseHeader(Constants.ETAG);
if (checkIfFreshStartRequiredAndStart(model)) {
model = null;
}
if (!isSuccessful()) {
Error error = new Error();
error.setServerError(true);
error.setServerErrorMessage(convertStreamToString(httpClient.getErrorStream()));
error.setHeaderFields(httpClient.getHeaderFields());
error.setResponseCode(responseCode);
response.setError(error);
return response;
}
setResumeSupportedOrNot();
totalBytes = request.getTotalBytes();
if (!isResumeSupported) {
deleteTempFile();
}
if (totalBytes == 0) {
totalBytes = httpClient.getContentLength();
request.setTotalBytes(totalBytes);
}
if (isResumeSupported && model == null) {
createAndInsertNewModel();
}
if (request.getStatus() == Status.CANCELLED) {
response.setCancelled(true);
return response;
} else if (request.getStatus() == Status.PAUSED) {
response.setPaused(true);
return response;
}
request.deliverStartEvent();
inputStream = httpClient.getInputStream();
byte[] buff = new byte[BUFFER_SIZE];
if (!file.exists()) {
if (file.getParentFile() != null && !file.getParentFile().exists()) {
if (file.getParentFile().mkdirs()) {
//noinspection ResultOfMethodCallIgnored
file.createNewFile();
}
} else {
//noinspection ResultOfMethodCallIgnored
file.createNewFile();
}
}
this.outputStream = FileDownloadRandomAccessFile.create(file);
if (isResumeSupported && request.getDownloadedBytes() != 0) {
outputStream.seek(request.getDownloadedBytes());
}
if (request.getStatus() == Status.CANCELLED) {
response.setCancelled(true);
return response;
} else if (request.getStatus() == Status.PAUSED) {
response.setPaused(true);
return response;
}
do {
final int byteCount = inputStream.read(buff, 0, BUFFER_SIZE);
if (byteCount == -1) {
break;
}
outputStream.write(buff, 0, byteCount);
request.setDownloadedBytes(request.getDownloadedBytes() + byteCount);
sendProgress();
syncIfRequired(outputStream);
if (request.getStatus() == Status.CANCELLED) {
response.setCancelled(true);
return response;
} else if (request.getStatus() == Status.PAUSED) {
sync(outputStream);
response.setPaused(true);
return response;
}
} while (true);
final String path = Utils.getPath(request.getDirPath(), request.getFileName());
Utils.renameFileName(tempPath, path);
response.setSuccessful(true);
if (isResumeSupported) {
removeNoMoreNeededModelFromDatabase();
}
} catch (IOException | IllegalAccessException e) {
if (!isResumeSupported) {
deleteTempFile();
}
Error error = new Error();
error.setConnectionError(true);
error.setConnectionException(e);
response.setError(error);
} finally {
closeAllSafely(outputStream);
}
return response;
}
private void deleteTempFile() {
File file = new File(tempPath);
if (file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.delete();
}
}
private boolean isSuccessful() {
return responseCode >= HttpURLConnection.HTTP_OK
&& responseCode < HttpURLConnection.HTTP_MULT_CHOICE;
}
private void setResumeSupportedOrNot() {
isResumeSupported = (responseCode == HttpURLConnection.HTTP_PARTIAL);
}
private boolean checkIfFreshStartRequiredAndStart(DownloadModel model) throws IOException,
IllegalAccessException {
if (responseCode == Constants.HTTP_RANGE_NOT_SATISFIABLE || isETagChanged(model)) {
if (model != null) {
removeNoMoreNeededModelFromDatabase();
}
deleteTempFile();
request.setDownloadedBytes(0);
request.setTotalBytes(0);
httpClient = ComponentHolder.getInstance().getHttpClient();
httpClient.connect(request);
httpClient = Utils.getRedirectedConnectionIfAny(httpClient, request);
responseCode = httpClient.getResponseCode();
return true;
}
return false;
}
private boolean isETagChanged(DownloadModel model) {
return !(eTag == null || model == null || model.getETag() == null)
&& !model.getETag().equals(eTag);
}
private DownloadModel getDownloadModelIfAlreadyPresentInDatabase() {
return ComponentHolder.getInstance().getDbHelper().find(request.getDownloadId());
}
private void createAndInsertNewModel() {
DownloadModel model = new DownloadModel();
model.setId(request.getDownloadId());
model.setUrl(request.getUrl());
model.setETag(eTag);
model.setDirPath(request.getDirPath());
model.setFileName(request.getFileName());
model.setDownloadedBytes(request.getDownloadedBytes());
model.setTotalBytes(totalBytes);
model.setLastModifiedAt(System.currentTimeMillis());
ComponentHolder.getInstance().getDbHelper().insert(model);
}
private void removeNoMoreNeededModelFromDatabase() {
ComponentHolder.getInstance().getDbHelper().remove(request.getDownloadId());
}
private void sendProgress() {
if (request.getStatus() != Status.CANCELLED) {
if (progressHandler != null) {
progressHandler
.obtainMessage(Constants.UPDATE,
new Progress(request.getDownloadedBytes(),
totalBytes)).sendToTarget();
}
}
}
private void syncIfRequired(FileDownloadOutputStream outputStream) {
final long currentBytes = request.getDownloadedBytes();
final long currentTime = System.currentTimeMillis();
final long bytesDelta = currentBytes - lastSyncBytes;
final long timeDelta = currentTime - lastSyncTime;
if (bytesDelta > MIN_BYTES_FOR_SYNC && timeDelta > TIME_GAP_FOR_SYNC) {
sync(outputStream);
lastSyncBytes = currentBytes;
lastSyncTime = currentTime;
}
}
private void sync(FileDownloadOutputStream outputStream) {
boolean success;
try {
outputStream.flushAndSync();
success = true;
} catch (IOException e) {
success = false;
e.printStackTrace();
}
if (success && isResumeSupported) {
ComponentHolder.getInstance().getDbHelper()
.updateProgress(request.getDownloadId(),
request.getDownloadedBytes(),
System.currentTimeMillis());
}
}
private void closeAllSafely(FileDownloadOutputStream outputStream) {
if (httpClient != null) {
try {
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if (outputStream != null) {
try {
sync(outputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
} finally {
if (outputStream != null)
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String convertStreamToString(InputStream stream) {
StringBuilder stringBuilder = new StringBuilder();
if (stream != null) {
String line;
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(stream));
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException ignored) {
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (NullPointerException | IOException ignored) {
}
}
}
return stringBuilder.toString();
}
}

View File

@@ -0,0 +1,19 @@
package com.downloader.internal;
import com.downloader.Response;
import com.downloader.request.DownloadRequest;
public class SynchronousCall {
public final DownloadRequest request;
public SynchronousCall(DownloadRequest request) {
this.request = request;
}
public Response execute() {
DownloadTask downloadTask = DownloadTask.create(request);
return downloadTask.run();
}
}

View File

@@ -0,0 +1,38 @@
package com.downloader.internal.stream;
import java.io.IOException;
public interface FileDownloadOutputStream {
/**
* Writes <code>len</code> bytes from the specified byte array
* starting at offset <code>off</code> to this file.
*/
void write(byte b[], int off, int len) throws IOException;
/**
* Flush all buffer to system and force all system buffers to synchronize with the underlying
* device.
*/
void flushAndSync() throws IOException;
/**
* Closes this output stream and releases any system resources associated with this stream. The
* general contract of <code>close</code> is that it closes the output stream. A closed stream
* cannot perform output operations and cannot be reopened.
*/
void close() throws IOException;
/**
* Sets the file-pointer offset, measured from the beginning of this file, at which the next
* read or write occurs. The offset may be set beyond the end of the file.
*/
void seek(long offset) throws IOException, IllegalAccessException;
/**
* Sets the length of this file.
*/
void setLength(final long newLength) throws IOException, IllegalAccessException;
}

View File

@@ -0,0 +1,53 @@
package com.downloader.internal.stream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
public class FileDownloadRandomAccessFile implements FileDownloadOutputStream {
private final BufferedOutputStream out;
private final FileDescriptor fd;
private final RandomAccessFile randomAccess;
private FileDownloadRandomAccessFile(File file) throws IOException {
randomAccess = new RandomAccessFile(file, "rw");
fd = randomAccess.getFD();
out = new BufferedOutputStream(new FileOutputStream(randomAccess.getFD()));
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
}
@Override
public void flushAndSync() throws IOException {
out.flush();
fd.sync();
}
@Override
public void close() throws IOException {
out.close();
randomAccess.close();
}
@Override
public void seek(long offset) throws IOException {
randomAccess.seek(offset);
}
@Override
public void setLength(long totalBytes) throws IOException {
randomAccess.setLength(totalBytes);
}
public static FileDownloadOutputStream create(File file) throws IOException {
return new FileDownloadRandomAccessFile(file);
}
}

View File

@@ -0,0 +1,319 @@
package com.downloader.request;
import com.downloader.Error;
import com.downloader.OnCancelListener;
import com.downloader.OnDownloadListener;
import com.downloader.OnPauseListener;
import com.downloader.OnProgressListener;
import com.downloader.OnStartOrResumeListener;
import com.downloader.Priority;
import com.downloader.Response;
import com.downloader.Status;
import com.downloader.core.Core;
import com.downloader.internal.ComponentHolder;
import com.downloader.internal.DownloadRequestQueue;
import com.downloader.internal.SynchronousCall;
import com.downloader.utils.Utils;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Future;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadRequest {
private Priority priority;
private Object tag;
private String url;
private String dirPath;
private String fileName;
private int sequenceNumber;
private Future future;
private long downloadedBytes;
private long totalBytes;
private int readTimeout;
private int connectTimeout;
private String userAgent;
private OnProgressListener onProgressListener;
private OnDownloadListener onDownloadListener;
private OnStartOrResumeListener onStartOrResumeListener;
private OnPauseListener onPauseListener;
private OnCancelListener onCancelListener;
private int downloadId;
private HashMap<String, List<String>> headerMap;
private Status status;
DownloadRequest(DownloadRequestBuilder builder) {
this.url = builder.url;
this.dirPath = builder.dirPath;
this.fileName = builder.fileName;
this.headerMap = builder.headerMap;
this.priority = builder.priority;
this.tag = builder.tag;
this.readTimeout =
builder.readTimeout != 0 ?
builder.readTimeout :
getReadTimeoutFromConfig();
this.connectTimeout =
builder.connectTimeout != 0 ?
builder.connectTimeout :
getConnectTimeoutFromConfig();
this.userAgent = builder.userAgent;
}
public Priority getPriority() {
return priority;
}
public void setPriority(Priority priority) {
this.priority = priority;
}
public Object getTag() {
return tag;
}
public void setTag(Object tag) {
this.tag = tag;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDirPath() {
return dirPath;
}
public void setDirPath(String dirPath) {
this.dirPath = dirPath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public int getSequenceNumber() {
return sequenceNumber;
}
public void setSequenceNumber(int sequenceNumber) {
this.sequenceNumber = sequenceNumber;
}
public HashMap<String, List<String>> getHeaders() {
return headerMap;
}
public Future getFuture() {
return future;
}
public void setFuture(Future future) {
this.future = future;
}
public long getDownloadedBytes() {
return downloadedBytes;
}
public void setDownloadedBytes(long downloadedBytes) {
this.downloadedBytes = downloadedBytes;
}
public long getTotalBytes() {
return totalBytes;
}
public void setTotalBytes(long totalBytes) {
this.totalBytes = totalBytes;
}
public int getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public String getUserAgent() {
if (userAgent == null) {
userAgent = ComponentHolder.getInstance().getUserAgent();
}
return userAgent;
}
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
public int getDownloadId() {
return downloadId;
}
public void setDownloadId(int downloadId) {
this.downloadId = downloadId;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public OnProgressListener getOnProgressListener() {
return onProgressListener;
}
public DownloadRequest setOnStartOrResumeListener(OnStartOrResumeListener onStartOrResumeListener) {
this.onStartOrResumeListener = onStartOrResumeListener;
return this;
}
public DownloadRequest setOnProgressListener(OnProgressListener onProgressListener) {
this.onProgressListener = onProgressListener;
return this;
}
public DownloadRequest setOnPauseListener(OnPauseListener onPauseListener) {
this.onPauseListener = onPauseListener;
return this;
}
public DownloadRequest setOnCancelListener(OnCancelListener onCancelListener) {
this.onCancelListener = onCancelListener;
return this;
}
public int start(OnDownloadListener onDownloadListener) {
this.onDownloadListener = onDownloadListener;
downloadId = Utils.getUniqueId(url, dirPath, fileName);
DownloadRequestQueue.getInstance().addRequest(this);
return downloadId;
}
public Response executeSync() {
downloadId = Utils.getUniqueId(url, dirPath, fileName);
return new SynchronousCall(this).execute();
}
public void deliverError(final Error error) {
if (status != Status.CANCELLED) {
setStatus(Status.FAILED);
Core.getInstance().getExecutorSupplier().forMainThreadTasks()
.execute(new Runnable() {
public void run() {
if (onDownloadListener != null) {
onDownloadListener.onError(error);
}
finish();
}
});
}
}
public void deliverSuccess() {
if (status != Status.CANCELLED) {
setStatus(Status.COMPLETED);
Core.getInstance().getExecutorSupplier().forMainThreadTasks()
.execute(new Runnable() {
public void run() {
if (onDownloadListener != null) {
onDownloadListener.onDownloadComplete();
}
finish();
}
});
}
}
public void deliverStartEvent() {
if (status != Status.CANCELLED) {
Core.getInstance().getExecutorSupplier().forMainThreadTasks()
.execute(new Runnable() {
public void run() {
if (onStartOrResumeListener != null) {
onStartOrResumeListener.onStartOrResume();
}
}
});
}
}
public void deliverPauseEvent() {
if (status != Status.CANCELLED) {
Core.getInstance().getExecutorSupplier().forMainThreadTasks()
.execute(new Runnable() {
public void run() {
if (onPauseListener != null) {
onPauseListener.onPause();
}
}
});
}
}
private void deliverCancelEvent() {
Core.getInstance().getExecutorSupplier().forMainThreadTasks()
.execute(new Runnable() {
public void run() {
if (onCancelListener != null) {
onCancelListener.onCancel();
}
}
});
}
public void cancel() {
status = Status.CANCELLED;
if (future != null) {
future.cancel(true);
}
deliverCancelEvent();
Utils.deleteTempFileAndDatabaseEntryInBackground(Utils.getTempPath(dirPath, fileName), downloadId);
}
private void finish() {
destroy();
DownloadRequestQueue.getInstance().finish(this);
}
private void destroy() {
this.onProgressListener = null;
this.onDownloadListener = null;
this.onStartOrResumeListener = null;
this.onPauseListener = null;
this.onCancelListener = null;
}
private int getReadTimeoutFromConfig() {
return ComponentHolder.getInstance().getReadTimeout();
}
private int getConnectTimeoutFromConfig() {
return ComponentHolder.getInstance().getConnectTimeout();
}
}

View File

@@ -0,0 +1,81 @@
package com.downloader.request;
import com.downloader.Priority;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by amitshekhar on 13/11/17.
*/
public class DownloadRequestBuilder implements RequestBuilder {
String url;
String dirPath;
String fileName;
Priority priority = Priority.MEDIUM;
Object tag;
int readTimeout;
int connectTimeout;
String userAgent;
HashMap<String, List<String>> headerMap;
public DownloadRequestBuilder(String url, String dirPath, String fileName) {
this.url = url;
this.dirPath = dirPath;
this.fileName = fileName;
}
@Override
public DownloadRequestBuilder setHeader(String name, String value) {
if (headerMap == null) {
headerMap = new HashMap<>();
}
List<String> list = headerMap.get(name);
if (list == null) {
list = new ArrayList<>();
headerMap.put(name, list);
}
if (!list.contains(value)) {
list.add(value);
}
return this;
}
@Override
public DownloadRequestBuilder setPriority(Priority priority) {
this.priority = priority;
return this;
}
@Override
public DownloadRequestBuilder setTag(Object tag) {
this.tag = tag;
return this;
}
@Override
public DownloadRequestBuilder setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}
@Override
public DownloadRequestBuilder setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
@Override
public DownloadRequestBuilder setUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}
public DownloadRequest build() {
return new DownloadRequest(this);
}
}

View File

@@ -0,0 +1,23 @@
package com.downloader.request;
import com.downloader.Priority;
/**
* Created by amitshekhar on 13/11/17.
*/
public interface RequestBuilder {
RequestBuilder setHeader(String name, String value);
RequestBuilder setPriority(Priority priority);
RequestBuilder setTag(Object tag);
RequestBuilder setReadTimeout(int readTimeout);
RequestBuilder setConnectTimeout(int connectTimeout);
RequestBuilder setUserAgent(String userAgent);
}

View File

@@ -0,0 +1,157 @@
package com.downloader.utils;
import com.downloader.Constants;
import com.downloader.core.Core;
import com.downloader.database.DownloadModel;
import com.downloader.httpclient.HttpClient;
import com.downloader.internal.ComponentHolder;
import com.downloader.request.DownloadRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
/**
* Created by amitshekhar on 13/11/17.
*/
public final class Utils {
private final static int MAX_REDIRECTION = 10;
private Utils() {
// no instance
}
public static String getPath(String dirPath, String fileName) {
return dirPath + File.separator + fileName;
}
public static String getTempPath(String dirPath, String fileName) {
return getPath(dirPath, fileName) + ".temp";
}
public static void renameFileName(String oldPath, String newPath) throws IOException {
final File oldFile = new File(oldPath);
try {
final File newFile = new File(newPath);
if (newFile.exists()) {
if (!newFile.delete()) {
throw new IOException("Deletion Failed");
}
}
if (!oldFile.renameTo(newFile)) {
throw new IOException("Rename Failed");
}
} finally {
if (oldFile.exists()) {
//noinspection ResultOfMethodCallIgnored
oldFile.delete();
}
}
}
public static void deleteTempFileAndDatabaseEntryInBackground(final String path, final int downloadId) {
Core.getInstance().getExecutorSupplier().forBackgroundTasks()
.execute(new Runnable() {
@Override
public void run() {
ComponentHolder.getInstance().getDbHelper().remove(downloadId);
File file = new File(path);
if (file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.delete();
}
}
});
}
public static void deleteUnwantedModelsAndTempFiles(final int days) {
Core.getInstance().getExecutorSupplier().forBackgroundTasks()
.execute(new Runnable() {
@Override
public void run() {
List<DownloadModel> models = ComponentHolder.getInstance()
.getDbHelper()
.getUnwantedModels(days);
if (models != null) {
for (DownloadModel model : models) {
final String tempPath = getTempPath(model.getDirPath(), model.getFileName());
ComponentHolder.getInstance().getDbHelper().remove(model.getId());
File file = new File(tempPath);
if (file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.delete();
}
}
}
}
});
}
public static int getUniqueId(String url, String dirPath, String fileName) {
String string = url + File.separator + dirPath + File.separator + fileName;
byte[] hash;
try {
hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("NoSuchAlgorithmException", e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UnsupportedEncodingException", e);
}
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10) hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString().hashCode();
}
public static HttpClient getRedirectedConnectionIfAny(HttpClient httpClient,
DownloadRequest request)
throws IOException, IllegalAccessException {
int redirectTimes = 0;
int code = httpClient.getResponseCode();
String location = httpClient.getResponseHeader("Location");
while (isRedirection(code)) {
if (location == null) {
throw new IllegalAccessException("Location is null");
}
httpClient.close();
request.setUrl(location);
httpClient = ComponentHolder.getInstance().getHttpClient();
httpClient.connect(request);
code = httpClient.getResponseCode();
location = httpClient.getResponseHeader("Location");
redirectTimes++;
if (redirectTimes >= MAX_REDIRECTION) {
throw new IllegalAccessException("Max redirection done");
}
}
return httpClient;
}
private static boolean isRedirection(int code) {
return code == HttpURLConnection.HTTP_MOVED_PERM
|| code == HttpURLConnection.HTTP_MOVED_TEMP
|| code == HttpURLConnection.HTTP_SEE_OTHER
|| code == HttpURLConnection.HTTP_MULT_CHOICE
|| code == Constants.HTTP_TEMPORARY_REDIRECT
|| code == Constants.HTTP_PERMANENT_REDIRECT;
}
}

View File

@@ -0,0 +1,3 @@
<resources>
<string name="app_name">downloader</string>
</resources>

View File

@@ -0,0 +1,17 @@
package com.downloader;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}