Downloader.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package net.indivia.hacklabbo.statusreader;
  2. import java.io.BufferedInputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.net.HttpURLConnection;
  9. import java.net.URL;
  10. import java.net.URLConnection;
  11. import android.app.Activity;
  12. import android.app.IntentService;
  13. import android.app.ProgressDialog;
  14. import android.content.Intent;
  15. import android.net.Uri;
  16. import android.os.Bundle;
  17. import android.os.Message;
  18. import android.os.Messenger;
  19. import android.util.Log;
  20. public class Downloader extends IntentService {
  21. private static final String LOG_TAG = "Downloader";
  22. public static final int UPDATE_PROGRESS = 8344;
  23. private int resultDownload = Activity.RESULT_CANCELED;
  24. private File file;
  25. public ProgressDialog mProgressDialog;
  26. public Downloader() {
  27. super(LOG_TAG);
  28. }
  29. public void sendNotification(Intent i) {
  30. Bundle extras = i.getExtras();
  31. if (extras != null) {
  32. Messenger messenger = (Messenger) extras.get("messenger");
  33. Message msg = Message.obtain();
  34. msg.arg1 = resultDownload;
  35. msg.obj = file.getAbsolutePath();
  36. try {
  37. messenger.send(msg);
  38. } catch (android.os.RemoteException e1) {
  39. Log.w(getClass().getName(), "Exception sending message", e1);
  40. }
  41. }
  42. }
  43. @Override
  44. protected void onHandleIntent(Intent intent) {
  45. try {
  46. Log.d(LOG_TAG, "onHandIntent");
  47. int cu = intent.getIntExtra("checkUpdate", 0);
  48. String urlfile = intent.getStringExtra("urlpath");
  49. URL url = new URL(urlfile);
  50. file = SUtility.getFile(urlfile,"");
  51. if (cu != CheckUpdate.NOTIFY_APP_UPDATE){
  52. Log.d(LOG_TAG,"file exist & cu != 2 - STOP");
  53. resultDownload = Activity.RESULT_FIRST_USER;
  54. sendNotification(intent);
  55. } else {
  56. if (file.exists()) {
  57. file.delete();
  58. }
  59. URLConnection c = (HttpURLConnection) url.openConnection();
  60. c.connect();
  61. InputStream input = new BufferedInputStream(c.getInputStream());
  62. OutputStream output = new FileOutputStream(file);
  63. byte data[] = new byte[1024];
  64. int count;
  65. while ((count = input.read(data)) != -1) {
  66. output.write(data, 0, count);
  67. }
  68. output.flush();
  69. output.close();
  70. input.close();
  71. Intent i = new Intent(Intent.ACTION_VIEW);
  72. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  73. i.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
  74. startActivity(i);
  75. }
  76. } catch (IOException e) {
  77. e.getStackTrace();
  78. }
  79. }
  80. @Override
  81. public void onDestroy() {
  82. super.onDestroy();
  83. Log.i(LOG_TAG, "Service Destroyed");
  84. }
  85. }