Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.maartendekkers.cmapps;
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URL;
- import java.net.URLConnection;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class CustomAdapter extends BaseAdapter {
- protected void doDownload(final String urlLink, final String fileName) {
- Thread dx = new Thread() {
- public void run() {
- File root = android.os.Environment.getExternalStorageDirectory();
- File dir = new File (root.getAbsolutePath() + "/cmapps/");
- if(dir.exists()==false) {
- dir.mkdirs();
- }
- //Save the path as a string value
- try {
- URL url = new URL(urlLink);
- Log.i("FILE_NAME", "File name is "+fileName);
- Log.i("FILE_URLLINK", "File URL is "+url);
- URLConnection connection = url.openConnection();
- connection.connect();
- // this will be useful so that you can show a typical 0-100% progress bar
- int fileLength = connection.getContentLength();
- ProgressDialog myDialog;
- myDialog = new ProgressDialog(MainActivity.this);
- myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- myDialog.setTitle("Title");
- myDialog.setMessage("This is my progress bar:");
- myDialog.show();
- // download the file
- InputStream input = new BufferedInputStream(url.openStream());
- OutputStream output = new FileOutputStream(dir+"/"+fileName);
- byte data[] = new byte[1024];
- long total = 0;
- int count;
- while ((count = input.read(data)) != -1) {
- total += count;
- output.write(data, 0, count);
- }
- output.flush();
- output.close();
- input.close();
- myDialog.dismiss();
- } catch (Exception e) {
- e.printStackTrace();
- Log.i("ERROR ON DOWNLOADING FILES", "ERROR IS" +e);
- }
- }
- };
- dx.start();
- }
- String [] result;
- String [] desc;
- String [] app;
- Context context;
- int [] imageId;
- private static LayoutInflater inflater=null;
- public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, String [] prgmAppName, String[] prgmDescList, int[] prgmImages) {
- // TODO Auto-generated constructor stub
- result=prgmNameList;
- context=mainActivity;
- app=prgmAppName;
- desc=prgmDescList;
- imageId=prgmImages;
- inflater = ( LayoutInflater )context.
- getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return result.length;
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- public class Holder
- {
- TextView tv;
- TextView tv2;
- ImageView img;
- }
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- Holder holder=new Holder();
- View rowView;
- rowView = inflater.inflate(R.layout.single_row, null);
- holder.tv=(TextView) rowView.findViewById(R.id.textView1);
- holder.tv2=(TextView) rowView.findViewById(R.id.textView2);
- holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
- holder.tv.setText(result[position]);
- holder.tv2.setText(desc[position]);
- holder.img.setImageResource(imageId[position]);
- rowView.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());
- alert.setTitle("Install?");
- alert.setMessage("Do you want to install "+result[position]+"?");
- alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- //Download app
- doDownload("http://maartenn.eu/cmapps"+app+".apk", ""+app+".apk");
- }
- });
- alert.setNegativeButton("Cancel",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- }
- });
- alert.show();
- }
- });
- return rowView;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement