Advertisement
Guest User

Untitled

a guest
May 15th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. package com.maartendekkers.cmapps;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10.  
  11. import android.app.AlertDialog;
  12. import android.app.ProgressDialog;
  13. import android.content.Context;
  14. import android.content.DialogInterface;
  15. import android.util.Log;
  16. import android.view.LayoutInflater;
  17. import android.view.View;
  18. import android.view.View.OnClickListener;
  19. import android.view.ViewGroup;
  20. import android.widget.BaseAdapter;
  21. import android.widget.ImageView;
  22. import android.widget.TextView;
  23. public class CustomAdapter extends BaseAdapter {
  24.    
  25.      protected void doDownload(final String urlLink, final String fileName) {
  26.             Thread dx = new Thread() {
  27.  
  28.                 public void run() {
  29.                       File root = android.os.Environment.getExternalStorageDirectory();              
  30.                       File dir = new File (root.getAbsolutePath() + "/cmapps/");
  31.                       if(dir.exists()==false) {
  32.                               dir.mkdirs();
  33.                          }
  34.                       //Save the path as a string value
  35.  
  36.                     try {
  37.                        
  38.                             URL url = new URL(urlLink);
  39.                             Log.i("FILE_NAME", "File name is "+fileName);
  40.                             Log.i("FILE_URLLINK", "File URL is "+url);
  41.                             URLConnection connection = url.openConnection();
  42.                             connection.connect();
  43.                             // this will be useful so that you can show a typical 0-100% progress bar
  44.                             int fileLength = connection.getContentLength();
  45.                             ProgressDialog myDialog;
  46.                             myDialog = new ProgressDialog(MainActivity.this);
  47.                             myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  48.                             myDialog.setTitle("Title");
  49.                             myDialog.setMessage("This is my progress bar:");
  50.                             myDialog.show();
  51.                             // download the file
  52.                             InputStream input = new BufferedInputStream(url.openStream());
  53.                             OutputStream output = new FileOutputStream(dir+"/"+fileName);
  54.  
  55.                             byte data[] = new byte[1024];
  56.                             long total = 0;
  57.                             int count;
  58.                             while ((count = input.read(data)) != -1) {
  59.                                 total += count;
  60.  
  61.                                 output.write(data, 0, count);
  62.                             }
  63.  
  64.                             output.flush();
  65.                             output.close();
  66.                             input.close();
  67.                             myDialog.dismiss();
  68.                         } catch (Exception e) {
  69.                              e.printStackTrace();
  70.                              Log.i("ERROR ON DOWNLOADING FILES", "ERROR IS" +e);
  71.                         }
  72.                 }
  73.             };
  74.             dx.start();      
  75.         }
  76.    
  77.     String [] result;
  78.     String [] desc;
  79.     String [] app;
  80.     Context context;
  81.  int [] imageId;
  82.       private static LayoutInflater inflater=null;
  83.     public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, String [] prgmAppName, String[] prgmDescList, int[] prgmImages) {
  84.         // TODO Auto-generated constructor stub
  85.         result=prgmNameList;
  86.         context=mainActivity;
  87.         app=prgmAppName;
  88.         desc=prgmDescList;
  89.         imageId=prgmImages;
  90.          inflater = ( LayoutInflater )context.
  91.                  getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  92.     }
  93.     @Override
  94.     public int getCount() {
  95.         // TODO Auto-generated method stub
  96.         return result.length;
  97.     }
  98.  
  99.     @Override
  100.     public Object getItem(int position) {
  101.         // TODO Auto-generated method stub
  102.         return position;
  103.     }
  104.  
  105.     @Override
  106.     public long getItemId(int position) {
  107.         // TODO Auto-generated method stub
  108.         return position;
  109.     }
  110.  
  111.     public class Holder
  112.     {
  113.         TextView tv;
  114.         TextView tv2;
  115.         ImageView img;
  116.     }
  117.     @Override
  118.     public View getView(final int position, View convertView, ViewGroup parent) {        
  119.         // TODO Auto-generated method stub
  120.         Holder holder=new Holder();
  121.         View rowView;      
  122.              rowView = inflater.inflate(R.layout.single_row, null);
  123.              holder.tv=(TextView) rowView.findViewById(R.id.textView1);
  124.              holder.tv2=(TextView) rowView.findViewById(R.id.textView2);
  125.              holder.img=(ImageView) rowView.findViewById(R.id.imageView1);      
  126.              holder.tv.setText(result[position]);    
  127.              holder.tv2.setText(desc[position]);
  128.          holder.img.setImageResource(imageId[position]);        
  129.          rowView.setOnClickListener(new OnClickListener() {        
  130.             @Override
  131.             public void onClick(View v) {
  132.                 AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());
  133.                 alert.setTitle("Install?");
  134.                 alert.setMessage("Do you want to install "+result[position]+"?");
  135.  
  136.                 alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  137.                     public void onClick(DialogInterface dialog, int whichButton) {
  138.                         //Download app
  139.                         doDownload("http://maartenn.eu/cmapps"+app+".apk", ""+app+".apk");
  140.                     }
  141.                 });
  142.  
  143.                 alert.setNegativeButton("Cancel",
  144.                     new DialogInterface.OnClickListener() {
  145.                         public void onClick(DialogInterface dialog, int whichButton) {
  146.                         }
  147.                     });
  148.  
  149.                 alert.show();
  150.             }
  151.         });
  152.         return rowView;
  153.     }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement