package com.vaguehope.mikuru;

import java.io.IOException;
import java.util.Arrays;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;

import com.vaguehope.mikuru.ExecHelper.LineProcessor;

public class RsyncTask extends AsyncTask<String, String, Void> {
//	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	private final Context context;
	private final TextView txtConsole;
	
//	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	public RsyncTask (Context context, TextView txtConsole) {
		this.context = context;
		this.txtConsole = txtConsole;
	}
	
//	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	@Override
	protected Void doInBackground (String... params) {
		try {
			RsyncHelper.readyRsync(this.context);
			ProcessBuilder pb = RsyncHelper.makeProcessBulder(this.context, Arrays.asList(params));
			ExecHelper.expectExec(pb, this.lineProc);
		}
		catch (IOException e) {
			throw new RuntimeException(e);
		}
		return null;
	}
	
	protected final LineProcessor lineProc = new LineProcessor() {
		@Override
		@SuppressWarnings("synthetic-access")
		public boolean processLine (String line) {
			publishProgress(new String[] { line });
			return true;
		}
	};
	
	@Override
	protected void onProgressUpdate (String... values) {
		if (values == null || values.length < 1) return;
		this.txtConsole.append(values[0]);
		this.txtConsole.append("\n");
	}
	
//	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
}

@@ -1408,16 +1408,48 @@
 return;%0A
+%09%09for (String line : values) %7B%0A%09
 %09%09this.t
@@ -1469,20 +1469,16 @@
 end(
-values%5B0%5D
+line
 );%0A
+%09
 %09%09th
@@ -1505,16 +1505,20 @@
 (%22%5Cn%22);%0A
+%09%09%7D%0A
 %09%7D%0A%09%0A//%09

Add all lines passed to publishProgress to txtView.