og.android.tether.system.WebserviceTask.java Source code

Java tutorial

Introduction

Here is the source code for og.android.tether.system.WebserviceTask.java

Source

/**
 *  This program is free software; you can redistribute it and/or modify it under 
 *  the terms of the GNU General Public License as published by the Free Software 
 *  Foundation; either version 3 of the License, or (at your option) any later 
 *  version.
 *  You should have received a copy of the GNU General Public License along with 
 *  this program; if not, see <http://www.gnu.org/licenses/>. 
 *  Use this application at your own risk.
 *
 *  Copyright (c) 2009 by Harald Mueller, Sofia Lemons and Ben Buxton.
 */

package og.android.tether.system;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.zip.GZIPInputStream;

import og.android.tether.MainActivity;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.Message;
import android.util.Log;

public class WebserviceTask {

    public static final String MSG_TAG = "TETHER -> WebserviceTask";
    public static final String DOWNLOAD_FILEPATH = "/sdcard/download";
    public static final String BLUETOOTH_FILEPATH = "/sdcard/og.android.tether";

    public static HttpResponse makeRequest(String url, List<BasicNameValuePair> params)
            throws ClientProtocolException, IOException {
        HttpClient client = new DefaultHttpClient();
        String paramString = URLEncodedUtils.format(params, "utf-8");
        Log.d(MSG_TAG, url + "?" + paramString);
        HttpGet request = new HttpGet(url + "?" + paramString);
        return client.execute(request);
    }

    public static void report(String url, HashMap<String, Object> paramMap) {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        Set<Entry<String, Object>> a = paramMap.entrySet();
        for (Entry<String, Object> e : a) {
            Object o = e.getValue();
            if (o != null) {
                params.add(new BasicNameValuePair(e.getKey(), o.toString()));
            }
        }
        try {
            HttpResponse response = makeRequest(url, params);

            StatusLine status = response.getStatusLine();
            Log.d(MSG_TAG, "Request returned status " + status);
            if (status.getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                Log.d(MSG_TAG, "Request returned: " + entity.getContent());
            }
        } catch (Exception e) {
            Log.d(MSG_TAG, "Can't report stats '" + url + "' (" + e.toString() + ").");
        }
    }

    public static Properties queryForProperty(String url) {
        Properties properties = null;
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(String.format(url));
        try {
            HttpResponse response = client.execute(request);

            StatusLine status = response.getStatusLine();
            Log.d(MSG_TAG, "Request returned status " + status);
            if (status.getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                properties = new Properties();
                properties.load(entity.getContent());
            }
        } catch (IOException e) {
            Log.d(MSG_TAG, "Can't get property '" + url + "'.");
        }
        return properties;
    }

    public static boolean downloadUpdateFile(String downloadFileUrl, String destinationFilename) {
        if (android.os.Environment.getExternalStorageState()
                .equals(android.os.Environment.MEDIA_MOUNTED) == false) {
            return false;
        }
        File downloadDir = new File(DOWNLOAD_FILEPATH);
        if (downloadDir.exists() == false) {
            downloadDir.mkdirs();
        } else {
            File downloadFile = new File(DOWNLOAD_FILEPATH + "/" + destinationFilename);
            if (downloadFile.exists()) {
                downloadFile.delete();
            }
        }
        return downloadFile(downloadFileUrl, DOWNLOAD_FILEPATH, destinationFilename);
    }

    public static boolean downloadBluetoothModule(String downloadFileUrl, String destinationFilename) {
        if (android.os.Environment.getExternalStorageState()
                .equals(android.os.Environment.MEDIA_MOUNTED) == false) {
            return false;
        }
        File bluetoothDir = new File(BLUETOOTH_FILEPATH);
        if (bluetoothDir.exists() == false) {
            bluetoothDir.mkdirs();
        }
        if (downloadFile(downloadFileUrl, "", destinationFilename) == true) {
            try {
                FileOutputStream out = new FileOutputStream(new File(destinationFilename.replace(".gz", "")));
                FileInputStream fis = new FileInputStream(destinationFilename);
                GZIPInputStream gzin = new GZIPInputStream(new BufferedInputStream(fis));
                int count;
                byte buf[] = new byte[8192];
                while ((count = gzin.read(buf, 0, 8192)) != -1) {
                    //System.out.write(x);
                    out.write(buf, 0, count);
                }
                out.flush();
                out.close();
                gzin.close();
                File inputFile = new File(destinationFilename);
                inputFile.delete();
            } catch (IOException e) {
                return false;
            }
            return true;
        } else
            return false;
    }

    public static boolean downloadFile(String url, String destinationDirectory, String destinationFilename) {
        boolean filedownloaded = true;
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(String.format(url));
        Message msg = Message.obtain();
        try {
            HttpResponse response = client.execute(request);
            StatusLine status = response.getStatusLine();
            Log.d(MSG_TAG, "Request returned status " + status);
            if (status.getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                InputStream instream = entity.getContent();
                int fileSize = (int) entity.getContentLength();
                FileOutputStream out = new FileOutputStream(
                        new File(destinationDirectory + "/" + destinationFilename));
                byte buf[] = new byte[8192];
                int len;
                int totalRead = 0;
                while ((len = instream.read(buf)) > 0) {
                    msg = Message.obtain();
                    msg.what = MainActivity.MESSAGE_DOWNLOAD_PROGRESS;
                    totalRead += len;
                    msg.arg1 = totalRead / 1024;
                    msg.arg2 = fileSize / 1024;
                    MainActivity.currentInstance.viewUpdateHandler.sendMessage(msg);
                    out.write(buf, 0, len);
                }
                out.close();
            } else {
                throw new IOException();
            }
        } catch (IOException e) {
            Log.d(MSG_TAG, "Can't download file '" + url + "' to '" + destinationDirectory + "/"
                    + destinationFilename + "'.");
            filedownloaded = false;
        }
        msg = Message.obtain();
        msg.what = MainActivity.MESSAGE_DOWNLOAD_COMPLETE;
        MainActivity.currentInstance.viewUpdateHandler.sendMessage(msg);
        return filedownloaded;
    }
}