Skip to content

Commit

Permalink
Issue #296 free space check happens earlier now
Browse files Browse the repository at this point in the history
Free space check now happens before starting all the downloads, not
before individual files. All files are taken into account, not only the
current one.
  • Loading branch information
yeriomin committed Nov 27, 2017
1 parent 3704048 commit 0d06980
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.github.yeriomin.yalpstore;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

import com.github.yeriomin.playstoreapi.AndroidAppDeliveryData;
Expand Down Expand Up @@ -33,20 +31,17 @@ public long enqueue(App app, AndroidAppDeliveryData deliveryData, Type type) {
long downloadId = url.hashCode();
statuses.put(downloadId, DownloadManagerInterface.IN_PROGRESS);
DownloadState.get(app.getPackageName()).setStarted(downloadId);
if (!enoughSpace(deliveryData)) {
statuses.put(downloadId, DownloadManagerInterface.ERROR_INSUFFICIENT_SPACE);
} else {
HttpURLConnectionDownloadTask task = new HttpURLConnectionDownloadTask();
task.setContext(context);
task.setDownloadId(downloadId);
task.setTargetFile(getDestinationFile(app, type));
String cookieString = null;
if (deliveryData.getDownloadAuthCookieCount() > 0) {
HttpCookie cookie = deliveryData.getDownloadAuthCookie(0);
cookieString = cookie.getName() + "=" + cookie.getValue();
}
task.execute(url, cookieString);

HttpURLConnectionDownloadTask task = new HttpURLConnectionDownloadTask();
task.setContext(context);
task.setDownloadId(downloadId);
task.setTargetFile(getDestinationFile(app, type));
String cookieString = null;
if (deliveryData.getDownloadAuthCookieCount() > 0) {
HttpCookie cookie = deliveryData.getDownloadAuthCookie(0);
cookieString = cookie.getName() + "=" + cookie.getValue();
}
task.execute(url, cookieString);
return downloadId;
}

Expand All @@ -62,20 +57,7 @@ public boolean success(long downloadId) {

@Override
public String getError(long downloadId) {
return null;
}

private boolean enoughSpace(AndroidAppDeliveryData deliveryData) {
long bytesNeeded = 0;
bytesNeeded += deliveryData.getDownloadSize();
if (deliveryData.getAdditionalFileCount() == 2) {
bytesNeeded += deliveryData.getAdditionalFile(1).getSize();
} else if (deliveryData.getAdditionalFileCount() == 1) {
bytesNeeded += deliveryData.getAdditionalFile(0).getSize();
}
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long) stat.getBlockSize() * (long) stat.getBlockCount();
return bytesAvailable >= bytesNeeded;
return getErrorString(context, statuses.get(downloadId));
}

private String getUrl(AndroidAppDeliveryData deliveryData, Type type) {
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/github/yeriomin/yalpstore/Downloader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.yeriomin.yalpstore;

import android.content.Context;
import android.os.StatFs;
import android.util.Log;

import com.github.yeriomin.playstoreapi.AndroidAppDeliveryData;
Expand All @@ -12,9 +13,11 @@
public class Downloader {

private DownloadManagerInterface dm;
private File storagePath;

public Downloader(Context context) {
this.dm = DownloadManagerFactory.get(context);
storagePath = Paths.getYalpPath(context);
}

public void download(App app, AndroidAppDeliveryData deliveryData) {
Expand All @@ -33,6 +36,18 @@ public void download(App app, AndroidAppDeliveryData deliveryData) {
}
}

public boolean enoughSpace(AndroidAppDeliveryData deliveryData) {
long bytesNeeded = deliveryData.getDownloadSize();
if (deliveryData.getAdditionalFileCount() > 0) {
bytesNeeded += deliveryData.getAdditionalFile(0).getSize();
}
if (deliveryData.getAdditionalFileCount() > 1) {
bytesNeeded += deliveryData.getAdditionalFile(1).getSize();
}
StatFs stat = new StatFs(storagePath.getPath());
return (long) stat.getBlockSize() * (long) stat.getAvailableBlocks() >= bytesNeeded;
}

private void checkAndStartObbDownload(DownloadState state, AndroidAppDeliveryData deliveryData, boolean main) {
App app = state.getApp();
AppFileMetadata metadata = deliveryData.getAdditionalFile(main ? 0 : 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ protected AndroidAppDeliveryData getResult(GooglePlayAPI api, String... argument
DownloadState.get(app.getPackageName()).setTriggeredBy(triggeredBy);
super.getResult(api, arguments);
if (null != deliveryData) {
new Downloader(context).download(app, deliveryData);
if (null != progressBarUpdater) {
progressBarUpdater.execute(UPDATE_INTERVAL);
Downloader downloader = new Downloader(context);
if (downloader.enoughSpace(deliveryData)) {
downloader.download(app, deliveryData);
if (null != progressBarUpdater) {
progressBarUpdater.execute(UPDATE_INTERVAL);
}
} else {
context.sendBroadcast(new Intent(DownloadManagerInterface.ACTION_DOWNLOAD_CANCELLED));
Log.e(getClass().getSimpleName(), app.getPackageName() + " not enough storage space");
throw new IOException(context.getString(R.string.download_manager_ERROR_INSUFFICIENT_SPACE));
}
} else {
context.sendBroadcast(new Intent(DownloadManagerInterface.ACTION_DOWNLOAD_CANCELLED));
Expand Down

0 comments on commit 0d06980

Please sign in to comment.