Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.39 KB | None | 0 0
  1. package com.bmc08gt.testkodifetch;
  2.  
  3. import android.app.Activity;
  4. import android.app.DownloadManager;
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.IntentFilter;
  9. import android.database.Cursor;
  10. import android.net.Uri;
  11. import android.os.AsyncTask;
  12. import android.os.Bundle;
  13. import android.os.Environment;
  14. import android.util.Log;
  15. import android.view.Menu;
  16. import android.view.MenuItem;
  17. import android.view.View;
  18. import android.widget.Button;
  19. import android.widget.Toast;
  20.  
  21. import org.apache.http.HttpResponse;
  22. import org.apache.http.client.ClientProtocolException;
  23. import org.apache.http.client.HttpClient;
  24. import org.apache.http.client.methods.HttpPost;
  25. import org.apache.http.impl.client.DefaultHttpClient;
  26. import org.apache.http.util.EntityUtils;
  27. import org.json.JSONArray;
  28. import org.json.JSONException;
  29. import org.json.JSONObject;
  30.  
  31. import java.io.DataInputStream;
  32. import java.io.DataOutputStream;
  33. import java.io.File;
  34. import java.io.FileNotFoundException;
  35. import java.io.FileOutputStream;
  36. import java.io.IOException;
  37. import java.io.InputStream;
  38. import java.net.URL;
  39. import java.net.URLConnection;
  40.  
  41.  
  42. public class MyActivity extends Activity {
  43.  
  44.     public String currentSha;
  45.  
  46.     private long enqueue;
  47.     private DownloadManager dm;
  48.  
  49.     private static final String VERSION_CHECK_JSON = "http://matriserver.com/firmware/xbmc/source/helix/version";
  50.     private static final String KODI_URL = "http://matriserver.com/firmware/xbmc/source/helix/";
  51.     private static final String KODI_PREFIX = "kodi-";
  52.  
  53.     @Override
  54.     protected void onCreate(Bundle savedInstanceState) {
  55.         super.onCreate(savedInstanceState);
  56.         setContentView(R.layout.activity_my);
  57.  
  58.         Button mButton = (Button) findViewById(R.id.button);
  59.         mButton.setOnClickListener(new View.OnClickListener() {
  60.             @Override
  61.             public void onClick(View view) {
  62.                 new RetrieveCurrentShaTask().execute("");
  63.                 Toast.makeText(MyActivity.this, "currentSha=" + currentSha, Toast.LENGTH_SHORT).show();
  64.                 Log.d("FETCH", "Current sha is " + currentSha);
  65.                 if (currentSha != null && !currentSha.isEmpty()) {
  66.                     String apkUrl = KODI_URL +  KODI_PREFIX + currentSha + ".apk";
  67.                     new DownloadApkTask(apkUrl).execute();
  68.                 } else {
  69.                     Log.d("DOWNLOAD", "sha is null or empty");
  70.                 }
  71.             }
  72.         });
  73.         registerReceiver(handleDownloadReceiver, new IntentFilter(
  74.                 DownloadManager.ACTION_DOWNLOAD_COMPLETE));
  75.     }
  76.  
  77.     private String fetchDownloadPartialPath(long id) {
  78.         DownloadManager.Query query = new DownloadManager.Query();
  79.         query.setFilterById(id);
  80.         Cursor c = dm.query(query);
  81.         try {
  82.             if (c.moveToFirst()) {
  83.                 return c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
  84.             }
  85.         } finally {
  86.             c.close();
  87.         }
  88.         return null;
  89.     }
  90.  
  91.     public class DownloadApkTask extends AsyncTask<String, Void, Void> {
  92.  
  93.         public String url;
  94.  
  95.         public DownloadApkTask(String apkUrl) {
  96.             this.url = apkUrl;
  97.         }
  98.  
  99.         @Override
  100.         protected Void doInBackground(String... strings) {
  101.             dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
  102.             DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
  103.             String fullFilePath = "file://" +
  104.                     Environment.getExternalStorageDirectory().getAbsolutePath()
  105.                     + "/" + KODI_PREFIX + currentSha + ".apk";
  106.             request.setTitle(getString(R.string.app_name));
  107.             request.setDestinationUri(Uri.parse(fullFilePath));
  108.             request.setAllowedOverRoaming(false);
  109.             request.setVisibleInDownloadsUi(false);
  110.             request.setAllowedOverMetered(true);
  111.             enqueue = dm.enqueue(request);
  112.             return null;
  113.         }
  114.     }
  115.  
  116.     BroadcastReceiver handleDownloadReceiver = new BroadcastReceiver() {
  117.         @Override
  118.         public void onReceive(Context context, Intent intent) {
  119.             String action = intent.getAction();
  120.             if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
  121.                 long downloadId = intent.getLongExtra(
  122.                         DownloadManager.EXTRA_DOWNLOAD_ID, 0);
  123.                 DownloadManager.Query query = new DownloadManager.Query();
  124.                 query.setFilterById(enqueue);
  125.                 Cursor c = dm.query(query);
  126.                 if (c.moveToFirst()) {
  127.                     int columnIndex = c
  128.                             .getColumnIndex(DownloadManager.COLUMN_STATUS);
  129.                     if (DownloadManager.STATUS_SUCCESSFUL == c
  130.                             .getInt(columnIndex)) {
  131.                         String partialFileFullPath = fetchDownloadPartialPath(downloadId);
  132.                         if (partialFileFullPath != null) {
  133.                             String completedFileFullPath = partialFileFullPath.replace(".partial", "");
  134.                             File partialFile = new File(partialFileFullPath);
  135.                             File updateFile = new File(completedFileFullPath);
  136.                             partialFile.renameTo(updateFile);
  137.                             Log.d("RECEIVER", "downloaded file is at" + updateFile.getAbsolutePath());
  138.                         }
  139.                     }
  140.                 }
  141.             }
  142.         }
  143.     };
  144.  
  145.  
  146.     @Override
  147.     public boolean onCreateOptionsMenu(Menu menu) {
  148.         // Inflate the menu; this adds items to the action bar if it is present.
  149.         getMenuInflater().inflate(R.menu.my, menu);
  150.         return true;
  151.     }
  152.  
  153.     @Override
  154.     public boolean onOptionsItemSelected(MenuItem item) {
  155.         // Handle action bar item clicks here. The action bar will
  156.         // automatically handle clicks on the Home/Up button, so long
  157.         // as you specify a parent activity in AndroidManifest.xml.
  158.         int id = item.getItemId();
  159.         if (id == R.id.action_settings) {
  160.             return true;
  161.         }
  162.         return super.onOptionsItemSelected(item);
  163.     }
  164.  
  165.     public class RetrieveCurrentShaTask extends AsyncTask<String, Void, String> {
  166.  
  167.         @Override
  168.         protected String doInBackground(String... strings) {
  169.             JSONObject json = null;
  170.             String str = "";
  171.             HttpResponse response;
  172.             HttpClient myClient = new DefaultHttpClient();
  173.             HttpPost myConnection = new HttpPost(VERSION_CHECK_JSON);
  174.  
  175.             try {
  176.                 response = myClient.execute(myConnection);
  177.                 str = EntityUtils.toString(response.getEntity(), "UTF-8");
  178.             } catch (ClientProtocolException e) {
  179.                 e.printStackTrace();
  180.             } catch (IOException e) {
  181.                 e.printStackTrace();
  182.             }
  183.  
  184.             try{
  185.                 JSONArray jArray = new JSONArray(str);
  186.                 json = jArray.getJSONObject(0);
  187.  
  188.                 return json.getString("sha");
  189.  
  190.  
  191.             } catch ( JSONException e) {
  192.                 e.printStackTrace();
  193.             }
  194.             return null;
  195.         }
  196.  
  197.         @Override
  198.         protected void onPostExecute(String sha) {
  199.             currentSha = sha;
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement