package com.dtf.client;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

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.io.OutputStream;


public class HelperService extends IntentService {

    private static final String ACTION_COPY_TO_SDCARD = "com.dtf.action.name.COPY_TO_SDCARD";
    private static final String ACTION_RESTART_SOCKET = "com.dtf.action.name.RESTART_SOCKET";

    private static final String COPY_FILENAME = "filename";
    private static final String TAG = "DtfHelperService";

    public HelperService() {
        super("HelperService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();

            if (ACTION_COPY_TO_SDCARD.equals(action)) {
                final String file_name = intent.getStringExtra(COPY_FILENAME);
                handleActionCopyToSdCard(file_name);
            }
            if (ACTION_RESTART_SOCKET.equals(action)) {
                handleActionRestartSocket();
            }
        }
    }

    private void handleActionCopyToSdCard(String file_name) {

        if (file_name == null) {
            Log.e(TAG, "File name cannot be null!");
            return;
        }

        Log.d(TAG, "Copying file '"+file_name+"' to SDCard...");
        copyToSdCard(file_name);
    }

    private void handleActionRestartSocket() {

        if (SocketService.isRunning) {

            // First stop the socket service.
            Log.d(TAG, "Stopping the socket service...");

            this.stopService(new Intent(this, SocketService.class));

        } else {
            Log.d(TAG, "Socket service is not running, skipping...");
        }

        // Give it 2 seconds to cool down
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Now start it!
        Log.d(TAG, "Starting socket service!");
        this.startService(new Intent(this, SocketService.class));
    }

    private void copyToSdCard(String file_name) {

        InputStream in = null;
        OutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(
                    new File("/data/data/com.dtf.client/"+file_name)));

            File outFile = new File("/mnt/sdcard/"+file_name);
            out = new FileOutputStream(outFile);
            copyFile(in, out);
        } catch(IOException e) {
            Log.e(TAG, "Failed to copy file: " + file_name, e);
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
    }

    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
    }
}

@@ -113,362 +113,60 @@
 g;%0A%0A
-import java.io.BufferedInputStream;%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;%0A%0A%0Apublic class HelperService extends IntentService %7B%0A%0A    private static final String ACTION_COPY_TO_SDCARD = %22com.dtf.action.name.COPY_TO_SDCARD%22;
+%0Apublic class HelperService extends IntentService %7B%0A
 %0A   
@@ -261,68 +261,8 @@
 %22;%0A%0A
-    private static final String COPY_FILENAME = %22filename%22;%0A
     
@@ -561,596 +561,109 @@
 ION_
-COPY_TO_SDCARD.equals(action)) %7B%0A                final String file_name = intent.getStringExtra(COPY_FILENAME);%0A                handleActionCopyToSdCard(file_name);%0A            %7D%0A            if (ACTION_RESTART_SOCKET.equals(action)) %7B%0A                handleActionRestartSocket();%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void handleActionCopyToSdCard(String file_name) %7B%0A%0A        if (file_name == null) %7B%0A            Log.e(TAG, %22File name cannot be null!%22);%0A            return;%0A        %7D%0A%0A        Log.d(TAG, %22Copying file '%22+file_name+%22' to SDCard...%22);%0A        copyToSdCard(file_name);
+RESTART_SOCKET.equals(action)) %7B%0A                handleActionRestartSocket();%0A            %7D%0A        %7D
 %0A   
@@ -1350,1117 +1350,7 @@
    %7D
-%0A%0A    private void copyToSdCard(String file_name) %7B%0A%0A        InputStream in = null;%0A        OutputStream out = null;%0A        try %7B%0A            in = new BufferedInputStream(new FileInputStream(%0A                    new File(%22/data/data/com.dtf.client/%22+file_name)));%0A%0A            File outFile = new File(%22/mnt/sdcard/%22+file_name);%0A            out = new FileOutputStream(outFile);%0A            copyFile(in, out);%0A        %7D catch(IOException e) %7B%0A            Log.e(TAG, %22Failed to copy file: %22 + file_name, e);%0A        %7D%0A        finally %7B%0A            if (in != null) %7B%0A                try %7B%0A                    in.close();%0A                %7D catch (IOException e) %7B%0A                %7D%0A            %7D%0A            if (out != null) %7B%0A                try %7B%0A                    out.close();%0A                %7D catch (IOException e) %7B%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void copyFile(InputStream in, OutputStream out) throws IOException %7B%0A        byte%5B%5D buffer = new byte%5B1024%5D;%0A        int read;%0A        while((read = in.read(buffer)) != -1)%7B%0A            out.write(buffer, 0, read);%0A        %7D%0A    %7D
 %0A%7D%0A

Remove unused/deprecated functionality