PageRenderTime 30ms CodeModel.GetById 20ms RepoModel.GetById 0ms 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. package android.tether.usb.system;
  13. //import java.io.BufferedInputStream;
  14. import java.io.File;
  15. //import java.io.FileInputStream;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.util.Properties;
  20. //import java.util.zip.GZIPInputStream;
  21. import org.apache.http.HttpEntity;
  22. import org.apache.http.HttpResponse;
  23. import org.apache.http.StatusLine;
  24. import org.apache.http.client.HttpClient;
  25. import org.apache.http.client.methods.HttpGet;
  26. import org.apache.http.impl.client.DefaultHttpClient;
  27. import android.os.Message;
  28. import android.tether.usb.MainActivity;
  29. import android.util.Log;
  30. public class WebserviceTask {
  31. public static final String MSG_TAG = "TETHER -> WebserviceTask";
  32. public static final String DOWNLOAD_FILEPATH = "/sdcard/download";
  33. //public static final String BLUETOOTH_FILEPATH = "/sdcard/android.tether";
  34. public MainActivity mainActivity;
  35. public Properties queryForProperty(String url) {
  36. Properties properties = null;
  37. HttpClient client = new DefaultHttpClient();
  38. HttpGet request = new HttpGet(String.format(url));
  39. try {
  40. HttpResponse response = client.execute(request);
  41. StatusLine status = response.getStatusLine();
  42. Log.d(MSG_TAG, "Request returned status " + status);
  43. if (status.getStatusCode() == 200) {
  44. HttpEntity entity = response.getEntity();
  45. properties = new Properties();
  46. properties.load(entity.getContent());
  47. }
  48. } catch (IOException e) {
  49. Log.d(MSG_TAG, "Can't get property '"+url+"'.");
  50. }
  51. return properties;
  52. }
  53. public boolean downloadUpdateFile(String downloadFileUrl, String destinationFilename) {
  54. if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED) == false) {
  55. return false;
  56. }
  57. File downloadDir = new File(DOWNLOAD_FILEPATH);
  58. if (downloadDir.exists() == false) {
  59. downloadDir.mkdirs();
  60. }
  61. else {
  62. File downloadFile = new File(DOWNLOAD_FILEPATH+"/"+destinationFilename);
  63. if (downloadFile.exists()) {
  64. downloadFile.delete();
  65. }
  66. }
  67. return this.downloadFile(downloadFileUrl, DOWNLOAD_FILEPATH, destinationFilename);
  68. }
  69. /*public boolean downloadBluetoothModule(String downloadFileUrl, String destinationFilename) {
  70. if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED) == false) {
  71. return false;
  72. }
  73. File bluetoothDir = new File(BLUETOOTH_FILEPATH);
  74. if (bluetoothDir.exists() == false) {
  75. bluetoothDir.mkdirs();
  76. }
  77. if (this.downloadFile(downloadFileUrl, "", destinationFilename) == true) {
  78. try {
  79. FileOutputStream out = new FileOutputStream(new File(destinationFilename.replace(".gz", "")));
  80. FileInputStream fis = new FileInputStream(destinationFilename);
  81. GZIPInputStream gzin = new GZIPInputStream(new BufferedInputStream(fis));
  82. int count;
  83. byte buf[] = new byte[8192];
  84. while ((count = gzin.read(buf, 0, 8192)) != -1) {
  85. //System.out.write(x);
  86. out.write(buf, 0, count);
  87. }
  88. out.flush();
  89. out.close();
  90. gzin.close();
  91. File inputFile = new File(destinationFilename);
  92. inputFile.delete();
  93. } catch (IOException e) {
  94. return false;
  95. }
  96. return true;
  97. } else
  98. return false;
  99. }*/
  100. public boolean downloadFile(String url, String destinationDirectory, String destinationFilename) {
  101. boolean filedownloaded = true;
  102. HttpClient client = new DefaultHttpClient();
  103. HttpGet request = new HttpGet(String.format(url));
  104. Message msg = Message.obtain();
  105. try {
  106. HttpResponse response = client.execute(request);
  107. StatusLine status = response.getStatusLine();
  108. Log.d(MSG_TAG, "Request returned status " + status);
  109. if (status.getStatusCode() == 200) {
  110. HttpEntity entity = response.getEntity();
  111. InputStream instream = entity.getContent();
  112. int fileSize = (int)entity.getContentLength();
  113. FileOutputStream out = new FileOutputStream(new File(destinationDirectory+"/"+destinationFilename));
  114. byte buf[] = new byte[8192];
  115. int len;
  116. int totalRead = 0;
  117. while((len = instream.read(buf)) > 0) {
  118. msg = Message.obtain();
  119. msg.what = MainActivity.MESSAGE_DOWNLOAD_PROGRESS;
  120. totalRead += len;
  121. msg.arg1 = totalRead / 1024;
  122. msg.arg2 = fileSize / 1024;
  123. MainActivity.currentInstance.viewUpdateHandler.sendMessage(msg);
  124. out.write(buf,0,len);
  125. }
  126. out.close();
  127. }
  128. else {
  129. throw new IOException();
  130. }
  131. } catch (IOException e) {
  132. Log.d(MSG_TAG, "Can't download file '"+url+"' to '" + destinationDirectory+"/"+destinationFilename + "'.");
  133. filedownloaded = false;
  134. }
  135. msg = Message.obtain();
  136. msg.what = MainActivity.MESSAGE_DOWNLOAD_COMPLETE;
  137. MainActivity.currentInstance.viewUpdateHandler.sendMessage(msg);
  138. return filedownloaded;
  139. }
  140. }