package com.ideatest.

kpi;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class KPI extends Activity implements OnClickListener {
TextFileHelper TextFileH;
// where our text file will be created
String filePath = Environment.getExternalStorageDirectory() + "/";

// name of our text file
String fileName ="q.txt";

// filePath and fileName combined
String actualFile = filePath + fileName;

TextView contentsTextView;

/********* work only for Dedicated IP ***********/
static final String FTP_HOST= "192.168.100.10";

/********* FTP USERNAME ***********/
static final String FTP_USER = "idea";

/********* FTP PASSWORD ***********/
static final String FTP_PASS ="idea";
static final String FN ="/home/idea/p.txt";

Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);

}

public void onClick(View v) {

/********** Pick file from sdcard *******/
File f = new File(actualFile);
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Bui
lder().permitAll().build();
StrictMode.setThreadPolicy(policy);
} // Upload sdcard file
downloadFile(FN ,f);
// initialize our TextFileHelper here
TextFileH = new TextFileHelper(KPI.this);
// first, make sure the path to your text file is created
TextFileH.createPath(filePath);

// so we can show file contents to the user
contentsTextView = (TextView) findViewById(R.id.contentsTextView);

// here is the actual button listener
View.OnClickListener handler = new View.OnClickListener() {
public void onClick(View v) {
// we will use switch statement and just
// get the buttons id to make things easier
switch (v.getId()) {
// when create button was clicked
//case R.id.createBtn:
// TextFileH.createTextFile(actualFile);
// contentsTextView.setText( );
//break;
// when read button was clicked
case R.id.readBtn:
String contents = TextFileH.readTextFile(actualFile);
contentsTextView.setText(contents);
break;
// when update button was clicked
//case R.id.updateBtn:
//TextFileH.updateTextFile(actualFile, Mike is so handsome!nN
ew line here!);
//contentsTextView.setText();
// break;
// when edit button was clicked
//case R.id.deleteBtn:
// TextFileH.deleteTextFile(actualFile);
// contentsTextView.setText();
// break;
}
}
};
// we will get the button views and set the listener (handler)
//findViewById(R.id.createBtn).setOnClickListener(handler);
findViewById(R.id.readBtn).setOnClickListener(handler);
// findViewById(R.id.updateBtn).setOnClickListener(handler);
// findViewById(R.id.deleteBtn).setOnClickListener(handler);

}

public void downloadFile(String FNN ,File fileName){



FTPClient client = new FTPClient();

try {

client.connect(FTP_HOST,21);
client.login(FTP_USER, FTP_PASS);
client.setType(FTPClient.TYPE_BINARY);

client.download(FNN ,fileName, new MyTransferListener());

} catch (Exception e) {
e.printStackTrace();
try {
client.disconnect(true);
} catch (Exception e2) {
e2.printStackTrace();
}
}

}

/******* Used to file upload and show progress **********/

public class MyTransferListener implements FTPDataTransferListener {

public void started() {

btn.setVisibility(View.GONE);
// Transfer started
Toast.makeText(getBaseContext(), " Upload Started ...", Toast.LENGTH
_SHORT).show();
//System.out.println(" Upload Started ...");
}

public void transferred(int length) {

// Yet other length bytes has been transferred since the last time t
his
// method was called
Toast.makeText(getBaseContext(), " transferred ..." + length, Toast.
LENGTH_SHORT).show();
//System.out.println(" transferred ..." + length);
}

public void completed() {

btn.setVisibility(View.VISIBLE);
// Transfer completed

Toast.makeText(getBaseContext(), " completed ...", Toast.LENGTH_SHOR
T).show();
//System.out.println(" completed ..." );
}

public void aborted() {

btn.setVisibility(View.VISIBLE);
// Transfer aborted
Toast.makeText(getBaseContext()," transfer aborted ,please try again
...", Toast.LENGTH_SHORT).show();
//System.out.println(" aborted ..." );
}

public void failed() {

btn.setVisibility(View.VISIBLE);
// Transfer failed
System.out.println(" failed ..." );
}

}
}