PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 12ms RepoModel.GetById 28ms app.codeStats 0ms

/src/android/tether/usb/system/WebserviceTask.java

http://android-wired-tether.googlecode.com/
Java | 152 lines | 94 code | 12 blank | 46 comment | 11 complexity | 74ba216653613ba76d1ecafb5b2f18e4 MD5 | raw file
  1/**
  2 *  This program is free software; you can redistribute it and/or modify it under 
  3 *  the terms of the GNU General Public License as published by the Free Software 
  4 *  Foundation; either version 3 of the License, or (at your option) any later 
  5 *  version.
  6 *  You should have received a copy of the GNU General Public License along with 
  7 *  this program; if not, see <http://www.gnu.org/licenses/>. 
  8 *  Use this application at your own risk.
  9 *
 10 *  Copyright (c) 2009 by Harald Mueller, Seth Lemons and Ben Buxton.
 11 */
 12
 13package android.tether.usb.system;
 14
 15//import java.io.BufferedInputStream;
 16import java.io.File;
 17//import java.io.FileInputStream;
 18import java.io.FileOutputStream;
 19import java.io.IOException;
 20import java.io.InputStream;
 21import java.util.Properties;
 22//import java.util.zip.GZIPInputStream;
 23
 24import org.apache.http.HttpEntity;
 25import org.apache.http.HttpResponse;
 26import org.apache.http.StatusLine;
 27import org.apache.http.client.HttpClient;
 28import org.apache.http.client.methods.HttpGet;
 29import org.apache.http.impl.client.DefaultHttpClient;
 30
 31import android.os.Message;
 32import android.tether.usb.MainActivity;
 33import android.util.Log;
 34
 35public class WebserviceTask {
 36	
 37	public static final String MSG_TAG = "TETHER -> WebserviceTask";
 38	public static final String DOWNLOAD_FILEPATH = "/sdcard/download";
 39	//public static final String BLUETOOTH_FILEPATH = "/sdcard/android.tether";
 40	
 41	public MainActivity mainActivity;
 42	
 43	public Properties queryForProperty(String url) {
 44		Properties properties = null; 
 45		HttpClient client = new DefaultHttpClient();
 46        HttpGet request = new HttpGet(String.format(url));
 47        try {
 48            HttpResponse response = client.execute(request);
 49
 50            StatusLine status = response.getStatusLine();
 51            Log.d(MSG_TAG, "Request returned status " + status);
 52            if (status.getStatusCode() == 200) {
 53	            HttpEntity entity = response.getEntity();
 54	            properties = new Properties();
 55	            properties.load(entity.getContent());
 56            }
 57        } catch (IOException e) {
 58        	Log.d(MSG_TAG, "Can't get property '"+url+"'.");
 59        }
 60		return properties;
 61	}
 62	
 63	public boolean downloadUpdateFile(String downloadFileUrl, String destinationFilename) {
 64		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED) == false) {
 65			return false;
 66		}
 67		File downloadDir = new File(DOWNLOAD_FILEPATH);
 68		if (downloadDir.exists() == false) {
 69			downloadDir.mkdirs();
 70		}
 71		else {
 72			File downloadFile = new File(DOWNLOAD_FILEPATH+"/"+destinationFilename);
 73			if (downloadFile.exists()) {
 74				downloadFile.delete();
 75			}
 76		}
 77		return this.downloadFile(downloadFileUrl, DOWNLOAD_FILEPATH, destinationFilename);
 78	}
 79	
 80	/*public boolean downloadBluetoothModule(String downloadFileUrl, String destinationFilename) {
 81		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED) == false) {
 82			return false;
 83		}
 84		File bluetoothDir = new File(BLUETOOTH_FILEPATH);
 85		if (bluetoothDir.exists() == false) {
 86			bluetoothDir.mkdirs();
 87		}
 88		if (this.downloadFile(downloadFileUrl, "", destinationFilename) == true) {
 89			try {
 90				FileOutputStream out = new FileOutputStream(new File(destinationFilename.replace(".gz", "")));
 91				FileInputStream fis = new FileInputStream(destinationFilename);
 92				GZIPInputStream gzin = new GZIPInputStream(new BufferedInputStream(fis));
 93				int count;
 94				byte buf[] = new byte[8192];
 95				while ((count = gzin.read(buf, 0, 8192)) != -1) {
 96					   //System.out.write(x);
 97					   out.write(buf, 0, count);
 98				}
 99				out.flush();
100				out.close();
101				gzin.close();
102				File inputFile = new File(destinationFilename);
103				inputFile.delete();
104			} catch (IOException e) {
105				return false;
106			}
107			return true;
108		} else
109			return false;
110	}*/
111	
112	public boolean downloadFile(String url, String destinationDirectory, String destinationFilename) {
113		boolean filedownloaded = true;
114		HttpClient client = new DefaultHttpClient();
115        HttpGet request = new HttpGet(String.format(url));
116        Message msg = Message.obtain();
117        try {
118            HttpResponse response = client.execute(request);
119            StatusLine status = response.getStatusLine();
120            Log.d(MSG_TAG, "Request returned status " + status);
121            if (status.getStatusCode() == 200) {
122	            HttpEntity entity = response.getEntity();
123	            InputStream instream = entity.getContent();
124	            int fileSize = (int)entity.getContentLength();
125	            FileOutputStream out = new FileOutputStream(new File(destinationDirectory+"/"+destinationFilename));
126	            byte buf[] = new byte[8192];
127	            int len;
128	            int totalRead = 0;
129	            while((len = instream.read(buf)) > 0) {
130	            	msg = Message.obtain();
131	            	msg.what = MainActivity.MESSAGE_DOWNLOAD_PROGRESS;
132	            	totalRead += len;
133	            	msg.arg1 = totalRead / 1024;
134	            	msg.arg2 = fileSize / 1024;
135	            	MainActivity.currentInstance.viewUpdateHandler.sendMessage(msg);
136	            	out.write(buf,0,len);
137            	}
138	            out.close();
139            }
140            else {
141            	throw new IOException();
142            }
143        } catch (IOException e) {
144        	Log.d(MSG_TAG, "Can't download file '"+url+"' to '" + destinationDirectory+"/"+destinationFilename + "'.");
145        	filedownloaded = false;
146        }
147        msg = Message.obtain();
148        msg.what = MainActivity.MESSAGE_DOWNLOAD_COMPLETE;
149        MainActivity.currentInstance.viewUpdateHandler.sendMessage(msg);
150        return filedownloaded;
151	}
152}