use of ch.boye.httpclientandroidlib.conn.ConnectTimeoutException in project storymaker by StoryMaker.
the class StorymakerDownloadManager method downloadWithTor.
private void downloadWithTor(boolean useTor, Uri uri, String title, String desc, File targetFile) {
initNotificationManager();
// generate id/tag for notification
String nTag = indexItem.getExpansionId();
int nId = 0;
if (fileName.contains(scal.io.liger.Constants.MAIN)) {
nId = Integer.parseInt(indexItem.getExpansionFileVersion());
} else if (fileName.contains(scal.io.liger.Constants.PATCH)) {
nId = Integer.parseInt(indexItem.getPatchFileVersion());
}
// incompatible with lungcast certificate
// StrongHttpsClient httpClient = getHttpClientInstance();
OkHttpClient httpClient = new OkHttpClient();
// we're now using this method to support non-tor downloads as well, so settings must be checked
if (useTor) {
if (checkTor(context)) {
Timber.d("DOWNLOAD WITH TOR PROXY: " + scal.io.liger.Constants.TOR_PROXY_HOST + "/" + scal.io.liger.Constants.TOR_PROXY_PORT);
SocketAddress torSocket = new InetSocketAddress(scal.io.liger.Constants.TOR_PROXY_HOST, scal.io.liger.Constants.TOR_PROXY_PORT);
Proxy torProxy = new Proxy(Proxy.Type.HTTP, torSocket);
httpClient.setProxy(torProxy);
} else {
Timber.e("CANNOT DOWNLOAD WITH TOR, TOR IS NOT ACTIVE");
if (context instanceof Activity) {
// FIXME move to strings
Utils.toastOnUiThread((Activity) context, "Check settings, can't use tor if orbot isn't running", true);
}
StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
return;
}
}
// disable attempts to retry (more retries ties up connection and prevents failure handling)
httpClient.setRetryOnConnectionFailure(false);
// set modest timeout (longer timeout ties up connection and prevents failure handling)
httpClient.setConnectTimeout(3000, TimeUnit.MILLISECONDS);
String actualFileName = targetFile.getName().substring(0, targetFile.getName().lastIndexOf("."));
Timber.d("CHECKING URI: " + uri.toString());
try {
// FIXME - adding the "Connection: close" header to resolve an issue that seemed to be caused
// FIXME - by a lingering connection. when possible, the better solution would be to track
// FIXME - down the possible end states and add appropriate cleanup steps.
Request request = new Request.Builder().url(uri.toString()).addHeader("Connection", "close").build();
// check for partially downloaded file
File partFile = new File(targetFile.getPath().replace(".tmp", ".part"));
if (partFile.exists()) {
long partBytes = partFile.length();
Timber.d("PARTIAL FILE " + partFile.getPath() + " FOUND, SETTING RANGE HEADER: " + "Range" + " / " + "bytes=" + Long.toString(partBytes) + "-");
// request.setHeader("Range", "bytes=" + Long.toString(partBytes) + "-");
request = new Request.Builder().url(uri.toString()).addHeader("Connection", "close").addHeader("Range", "bytes=" + Long.toString(partBytes) + "-").build();
} else {
Timber.d("PARTIAL FILE " + partFile.getPath() + " NOT FOUND, STARTING AT BYTE 0");
}
Response response = httpClient.newCall(request).execute();
int statusCode = response.code();
if ((statusCode == 200) || (statusCode == 206)) {
Timber.d("DOWNLOAD SUCCEEDED, STATUS CODE: " + statusCode);
// queue item here, "download" doesn't start until after we get a status code
// queue item, use date to get a unique long, subtract to get a negative number (to distinguish from download manager items)
Date startTime = new Date();
long queueId = 0 - startTime.getTime();
StorymakerQueueManager.addToQueue(context, queueId, targetFile.getName(), queueDao);
targetFile.getParentFile().mkdirs();
Timber.d("DOWNLOAD SUCCEEDED, GETTING ENTITY...");
BufferedInputStream responseInput = new BufferedInputStream(response.body().byteStream());
try {
FileOutputStream targetOutput = new FileOutputStream(targetFile);
byte[] buf = new byte[1024];
int i;
int oldPercent = 0;
long thisTime;
long lastTime = -1;
int lastPercent = 0;
while ((i = responseInput.read(buf)) > 0) {
// create status bar notification
int nPercent = StorymakerDownloadHelper.getDownloadPercent(context, fileName, installedDao);
thisTime = System.currentTimeMillis();
if (oldPercent == nPercent) {
// need to cut back on notification traffic
} else {
if (nPercent % 10 == 0 && nPercent != lastPercent) {
if (lastTime == -1 || (thisTime - lastTime) > 1000) {
lastPercent = nPercent;
oldPercent = nPercent;
lastTime = thisTime;
Notification nProgress = new Notification.Builder(context).setContentTitle(mAppTitle + " content download").setContentText(// assignment file names are meaningless uuids
indexItem.getTitle() + " - " + (nPercent / 10.0) + "%").setSmallIcon(android.R.drawable.arrow_down_float).setProgress(100, (nPercent / 10), false).setWhen(startTime.getTime()).build();
nManager.notify(nTag, nId, nProgress);
//Log.d("Storymaker Download Manager", "** NOTIFICATION ** " + nPercent );
}
}
}
targetOutput.write(buf, 0, i);
}
targetOutput.close();
responseInput.close();
Timber.d("SAVED DOWNLOAD TO " + targetFile);
} catch (ConnectTimeoutException cte) {
Timber.e(cte, "FAILED TO SAVE DOWNLOAD TO " + actualFileName + " (CONNECTION EXCEPTION)");
} catch (SocketTimeoutException ste) {
Timber.e(ste, "FAILED TO SAVE DOWNLOAD TO " + actualFileName + " (SOCKET EXCEPTION)");
} catch (IOException ioe) {
Timber.e(ioe, "FAILED TO SAVE DOWNLOAD TO " + actualFileName + " (IO EXCEPTION)");
}
// remove from queue here, regardless of success
StorymakerQueueManager.removeFromQueue(context, queueId, queueDao);
// remove notification, regardless of success
nManager.cancel(nTag, nId);
// (assumes .tmp file will exist if download is interrupted)
if (!handleFile(targetFile)) {
Timber.e("ERROR DURING FILE PROCESSING FOR " + actualFileName);
}
} else {
Timber.e("DOWNLOAD FAILED FOR " + actualFileName + ", STATUS CODE: " + statusCode);
StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
}
// clean up connection
// EntityUtils.consume(entity);
// request.abort();
// request.releaseConnection();
} catch (IOException ioe) {
Timber.e(ioe, "DOWNLOAD FAILED FOR " + actualFileName + ", EXCEPTION THROWN");
StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
}
}
Aggregations