267 lines
8.2 KiB
Java
267 lines
8.2 KiB
Java
package space.rocketnine.gophast;
|
|
|
|
import android.app.Activity;
|
|
import android.app.Application;
|
|
import android.app.DownloadManager;
|
|
import android.content.SharedPreferences;
|
|
import android.content.pm.PackageInfo;
|
|
import android.content.pm.PackageManager;
|
|
import android.net.Uri;
|
|
import android.os.Build;
|
|
import android.os.Environment;
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import android.os.StrictMode;
|
|
import android.preference.PreferenceManager;
|
|
import android.util.DisplayMetrics;
|
|
import android.util.Log;
|
|
import android.widget.Toast;
|
|
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
|
|
import manager.Manager;
|
|
|
|
public class GoPhast extends Application {
|
|
ArrayList<Download> downloads;
|
|
ExecutorService exec;
|
|
WakeLocker wakeLocker;
|
|
SharedPreferences prefs;
|
|
DownloadAdapter adapter;
|
|
DownloadManager DownloadManager;
|
|
|
|
@Override
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
|
|
if (Build.VERSION.SDK_INT >= 24) {
|
|
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
|
|
StrictMode.setVmPolicy(builder.build());
|
|
}
|
|
|
|
exec = Executors.newCachedThreadPool();
|
|
|
|
DownloadManager = (DownloadManager) getSystemService(Activity.DOWNLOAD_SERVICE);
|
|
|
|
wakeLocker = new WakeLocker();
|
|
|
|
try {
|
|
manager.Manager.initialize(2);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return;
|
|
}
|
|
|
|
String appVersion;
|
|
try {
|
|
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
|
|
appVersion = packageInfo.versionName;
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
appVersion = "?";
|
|
}
|
|
manager.Manager.setUserAgentExtra("gophast-android/" + appVersion + " (https://git.sr.ht/~tslocum/gophast-android)");
|
|
|
|
setDownloadDir("");
|
|
|
|
Long minSplitSize = Long.valueOf(getPrefs().getString("pref_minsplitsize", "10485760"));
|
|
if (minSplitSize >= 0) {
|
|
manager.Manager.setMinSplitSize(minSplitSize);
|
|
}
|
|
|
|
Long maxConnections = Long.valueOf(getPrefs().getString("pref_maxconnections", "3"));
|
|
if (maxConnections >= 0) {
|
|
manager.Manager.setMaxConnections(maxConnections);
|
|
}
|
|
|
|
downloads = new ArrayList<Download>();
|
|
}
|
|
|
|
void addDownload(String url) {
|
|
url = url.trim();
|
|
if (url.equals("") || !url.toLowerCase().startsWith("http")) {
|
|
longToast(getString(R.string.invalidURL));
|
|
return;
|
|
}
|
|
|
|
Download download = new Download();
|
|
download.URL = url;
|
|
download.Name = (new File(url)).getName();
|
|
|
|
downloads.add(download);
|
|
updateDownloadIndexes();
|
|
|
|
download.Path = Uri.fromFile(new File(getDownloadDir(), download.Name));
|
|
Log.d("gophast", "Add index " + download.Index);
|
|
|
|
adapter.notifyItemInserted(download.Index);
|
|
|
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
|
|
DownloadStatusRunnable statusrunnable = new DownloadStatusRunnable();
|
|
statusrunnable.d = download;
|
|
statusrunnable.gophast = GoPhast.this;
|
|
statusrunnable.mainHandler = mainHandler;
|
|
Thread statusthread = new Thread(statusrunnable);
|
|
|
|
DownloadRunnable runnable = new DownloadRunnable();
|
|
runnable.d = download;
|
|
runnable.gophast = GoPhast.this;
|
|
runnable.mainHandler = mainHandler;
|
|
Thread thread = new Thread(runnable);
|
|
|
|
exec.execute(statusthread);
|
|
exec.execute(thread);
|
|
}
|
|
|
|
void updateDownloadIndexes() {
|
|
int index = 0;
|
|
for (Iterator<Download> i = downloads.iterator(); i.hasNext(); ) {
|
|
Download d = i.next();
|
|
d.Index = index;
|
|
|
|
index++;
|
|
}
|
|
}
|
|
|
|
void removeDownload(Download delete) {
|
|
if (delete == null) {
|
|
return;
|
|
}
|
|
|
|
long id = delete.ID;
|
|
if (delete.ID > 0) {
|
|
try {
|
|
Manager.removeDownload(delete.ID);
|
|
} catch (Exception e) {
|
|
// Already removed
|
|
}
|
|
}
|
|
|
|
for (Iterator<Download> i = downloads.iterator(); i.hasNext(); ) {
|
|
Download d = i.next();
|
|
if (d.equals(delete)) {
|
|
if (d.DownloadManagerID > 0) {
|
|
try {
|
|
DownloadManager.remove(d.DownloadManagerID);
|
|
} catch (Exception e) {
|
|
// Already removed
|
|
}
|
|
}
|
|
|
|
int index = d.Index;
|
|
String path = d.Path != null ? d.Path.getPath() : "";
|
|
String name = d.Name;
|
|
i.remove();
|
|
updateDownloadIndexes();
|
|
adapter.notifyItemRemoved(index);
|
|
|
|
if (path != null && !path.isEmpty()) {
|
|
boolean deleted = false;
|
|
try {
|
|
File file = new File(path);
|
|
if (file.exists()) {
|
|
if (file.isFile()) {
|
|
deleted = file.delete();
|
|
}
|
|
} else {
|
|
deleted = true;
|
|
}
|
|
} catch (Exception e) {
|
|
// Failed to delete
|
|
}
|
|
if (!deleted) {
|
|
longToast(String.format(getString(R.string.failedToDelete), name));
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (id > 0) {
|
|
releaseWakeLock(id);
|
|
}
|
|
}
|
|
|
|
void shortToast(final String message) {
|
|
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Toast.makeText(GoPhast.this, message, Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
}
|
|
|
|
void longToast(final String message) {
|
|
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
Toast.makeText(GoPhast.this, message, Toast.LENGTH_LONG).show();
|
|
}
|
|
});
|
|
}
|
|
|
|
String getDefaultDownloadDir() {
|
|
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
|
|
}
|
|
|
|
String getDownloadDir() {
|
|
String downloadDir = getPrefs().getString("pref_downloaddir", "");
|
|
if (downloadDir == null || downloadDir.isEmpty()) {
|
|
downloadDir = getDefaultDownloadDir();
|
|
}
|
|
|
|
return downloadDir;
|
|
}
|
|
|
|
void setDownloadDir(String newDownloadDir) {
|
|
String downloadDir;
|
|
if (newDownloadDir != null && !newDownloadDir.isEmpty()) {
|
|
downloadDir = newDownloadDir;
|
|
} else {
|
|
downloadDir = getDownloadDir();
|
|
}
|
|
|
|
Log.d("gophast", "Setting download directory to " + downloadDir);
|
|
manager.Manager.setDownloadDir(downloadDir);
|
|
}
|
|
|
|
/**
|
|
* This method converts dp unit to equivalent pixels, depending on device density.
|
|
*
|
|
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
|
|
* @return A float value to represent px equivalent to dp depending on device density
|
|
*/
|
|
public int convertDpToPixel(int dp) {
|
|
return (int) (dp * ((float) getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT));
|
|
}
|
|
|
|
/**
|
|
* This method converts device specific pixels to density independent pixels.
|
|
*
|
|
* @param px A value in px (pixels) unit. Which we need to convert into db
|
|
* @return A float value to represent dp equivalent to px value
|
|
*/
|
|
public int convertPixelsToDp(int px) {
|
|
return (int) (px / ((float) getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT));
|
|
}
|
|
|
|
public void acquireWakeLock(long id) {
|
|
wakeLocker.acquire(GoPhast.this, id, false);
|
|
}
|
|
|
|
public void releaseWakeLock(long id) {
|
|
wakeLocker.release(id);
|
|
}
|
|
|
|
public SharedPreferences getPrefs() {
|
|
if (prefs == null) {
|
|
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
|
}
|
|
return prefs;
|
|
}
|
|
}
|