Fix issue with turning off WiFi

This commit is contained in:
Victor Shcherb 2011-11-26 20:38:54 +01:00
parent cbe671ffc6
commit ff9b25cbc3
2 changed files with 18 additions and 9 deletions

View file

@ -15,6 +15,9 @@ import java.util.zip.ZipInputStream;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import net.osmand.IProgress; import net.osmand.IProgress;
import net.osmand.LogUtil; import net.osmand.LogUtil;
@ -42,7 +45,7 @@ public class DownloadFileHelper {
} }
protected void downloadFile(String fileName, FileOutputStream out, URL url, String part, String indexOfAllFiles, protected void downloadFile(String fileName, FileOutputStream out, URL url, String part, String indexOfAllFiles,
IProgress progress) throws IOException, InterruptedException { IProgress progress, boolean forceWifi) throws IOException, InterruptedException {
InputStream is = null; InputStream is = null;
byte[] buffer = new byte[BUFFER_SIZE]; byte[] buffer = new byte[BUFFER_SIZE];
@ -69,8 +72,9 @@ public class DownloadFileHelper {
} }
conn.setConnectTimeout(30000); conn.setConnectTimeout(30000);
log.info(conn.getResponseMessage() + " " + conn.getResponseCode()); //$NON-NLS-1$ log.info(conn.getResponseMessage() + " " + conn.getResponseCode()); //$NON-NLS-1$
if (conn.getResponseCode() != HttpURLConnection.HTTP_PARTIAL && boolean wifiConnectionBroken = forceWifi && !isWifiConnected();
conn.getResponseCode() != HttpURLConnection.HTTP_OK) { if ((conn.getResponseCode() != HttpURLConnection.HTTP_PARTIAL &&
conn.getResponseCode() != HttpURLConnection.HTTP_OK ) || wifiConnectionBroken) {
conn.disconnect(); conn.disconnect();
triesDownload--; triesDownload--;
continue; continue;
@ -118,22 +122,27 @@ public class DownloadFileHelper {
} }
public boolean isWifiConnected(){
ConnectivityManager mgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = mgr.getActiveNetworkInfo();
return ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI;
}
protected boolean downloadFile(final String fileName, final File fileToDownload, final File fileToUnZip, final boolean unzipToDir, protected boolean downloadFile(final String fileName, final File fileToDownload, final File fileToUnZip, final boolean unzipToDir,
IProgress progress, Long dateModified, int parts, List<File> toReIndex, String indexOfAllFiles, IProgress progress, Long dateModified, int parts, List<File> toReIndex, String indexOfAllFiles,
DownloadFileShowWarning showWarningCallback) throws InterruptedException { DownloadFileShowWarning showWarningCallback, boolean forceWifi ) throws InterruptedException {
FileOutputStream out = null; FileOutputStream out = null;
try { try {
out = new FileOutputStream(fileToDownload); out = new FileOutputStream(fileToDownload);
try { try {
if(parts == 1){ if(parts == 1){
URL url = new URL("http://download.osmand.net/download?file="+fileName + "&" + Version.getVersionAsURLParam()); //$NON-NLS-1$ URL url = new URL("http://download.osmand.net/download?file="+fileName + "&" + Version.getVersionAsURLParam()); //$NON-NLS-1$
downloadFile(fileName, out, url, null, indexOfAllFiles, progress); downloadFile(fileName, out, url, null, indexOfAllFiles, progress, forceWifi);
} else { } else {
for(int i=1; i<=parts; i++){ for(int i=1; i<=parts; i++){
URL url = new URL("http://download.osmand.net/download?file="+fileName+"-"+i + "&" + Version.getVersionAsURLParam()); //$NON-NLS-1$ URL url = new URL("http://download.osmand.net/download?file="+fileName+"-"+i + "&" + Version.getVersionAsURLParam()); //$NON-NLS-1$
downloadFile(fileName, out, url, " ["+i+"/"+parts+"]", indexOfAllFiles, progress); downloadFile(fileName, out, url, " ["+i+"/"+parts+"]", indexOfAllFiles, progress, forceWifi);
} }
} }
} finally { } finally {

View file

@ -573,7 +573,7 @@ private class DownloadIndexesAsyncTask extends AsyncTask<String, Object, String
protected String doInBackground(String... filesToDownload) { protected String doInBackground(String... filesToDownload) {
try { try {
List<File> filesToReindex = new ArrayList<File>(); List<File> filesToReindex = new ArrayList<File>();
boolean forceWifi = downloadFileHelper.isWifiConnected();
for (int i = 0; i < filesToDownload.length; i++) { for (int i = 0; i < filesToDownload.length; i++) {
String filename = filesToDownload[i]; String filename = filesToDownload[i];
DownloadEntry entry = entriesToDownload.get(filename); DownloadEntry entry = entriesToDownload.get(filename);
@ -582,7 +582,7 @@ private class DownloadIndexesAsyncTask extends AsyncTask<String, Object, String
+ filesToDownload.length + "]"); + filesToDownload.length + "]");
boolean result = downloadFileHelper.downloadFile(filename, boolean result = downloadFileHelper.downloadFile(filename,
entry.fileToSave, entry.fileToUnzip, entry.unzip, progress, entry.dateModified, entry.fileToSave, entry.fileToUnzip, entry.unzip, progress, entry.dateModified,
entry.parts, filesToReindex, indexOfAllFiles, this); entry.parts, filesToReindex, indexOfAllFiles, this, forceWifi);
if (result) { if (result) {
entriesToDownload.remove(filename); entriesToDownload.remove(filename);
publishProgress(entry); publishProgress(entry);