Showing posts with label List Of Remote Folders. Show all posts
Showing posts with label List Of Remote Folders. Show all posts

Monday, November 11, 2013

List Of Remote Folders

Create a List Of Remote Folders using JSCH for android, Java in a ListView In Dialog Box.




import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import com.example.newsync.R;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Listremote extends Activity{
ChannelSftp sftpChannel;
ListAdapter adapter;
ArrayAdapter<String> adaptor;
com.jcraft.jsch.ChannelSftp.LsEntry lsEntry ;
private Item[] fileList;
ArrayList<String> list = new ArrayList<String>();
String path;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


JSch jsch = new JSch();
Session session = null;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
session = jsch.getSession("USER Name", "Host Address", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("Password");
session.connect();
Channel channel = session.openChannel("sftp");
Handler h = new Handler(Listremote.this.getMainLooper());
h.post(new Runnable() {
@Override
public void run() {
Toast.makeText(Listremote.this, "Connecting",
Toast.LENGTH_LONG).show();
}
});
channel.connect();
h.post(new Runnable() {
@Override
public void run() {
Toast.makeText(Listremote.this, "Connected",
Toast.LENGTH_LONG).show();
}
});
sftpChannel = (ChannelSftp) channel;
sftpChannel.cd(".");


try {

if(path==null){
Log.e("path null", "path null");
path=".";

}
@SuppressWarnings("rawtypes")
Vector files = sftpChannel.ls(path);
if (files.size() == 0) {
Toast.makeText(getApplicationContext(),
"No files are available for download.", 4)
.show();
} else {
for (int i = 0; i < files.size(); i++) {
lsEntry = (com.jcraft.jsch.ChannelSftp.LsEntry) files
.get(i);
if (lsEntry.getAttrs().isDir()&& !lsEntry.getFilename().equals(".")
&& !lsEntry.getFilename().equals("..")&&!lsEntry.getFilename().startsWith(".")&&!lsEntry.getFilename().startsWith("..")){
// Log.e(lsEntry.toString(), "files");
//List items = Arrays.asList(lsEntry.getFilename());
list.add(lsEntry.getFilename());

//Log.e(""+items.toString(), "items");
// Log.e(lsEntry.getAttrs().getSize()+"","lsentry Size");
// Log.e(lsEntry+"","list Size");
fileList = new Item[list.size()];

for (int j = 0; j < list.size(); j++) {
fileList[j] = new Item(list.toString(), R.drawable.directory_icon);

}
Log.e("file list "+fileList, "list "+list.toString());
//List<String> colorChoices = list;
//Log.e(colorChoices+"","list ");
adaptor = new ArrayAdapter<String>(
this,android.R.layout.select_dialog_item, android.R.id.text1, list){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// creates view
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view
.findViewById(android.R.id.text1);

// put the image on the text view
textView.setCompoundDrawablesWithIntrinsicBounds(
fileList[position].icon, 0, 0, 0);

// add margin between image and text (support various screen
// densities)
int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
textView.setCompoundDrawablePadding(dp5);

return view;
}
};


}

}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Remote Files");

builder.setAdapter(adaptor, new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int item) {
    String itemname = adaptor.getItem(item);
    Log.e(""+itemname,"file Clicked");
   // String chosenFile = fileList[item].file;
    path=itemname;

   }
});
builder.show();}
}catch(Exception e){e.printStackTrace();}

//sftpChannel.put("" + sel, "Desktop/");

//String r = userId + "." + sel.getName();
//sftpChannel.rename("Desktop/" + sel.getName(),
// "Desktop/" + r);
h.post(new Runnable() {
@Override
public void run() {
try {
/*
AlertDialog.Builder builder = new AlertDialog.Builder(
Listremote.this);
builder.setMessage(
"File Sent Successfully")
.setCancelable(false)
.setPositiveButton(
"Listremote More",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
//loadFileList();
//showDialog(1);

}
});
builder.setNegativeButton(
"Not now",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
//dialog.cancel();
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
*/
} catch (Exception e) {
e.printStackTrace();
}
}
});
sftpChannel.exit();
session.disconnect();
//Log.e("" + sel, "FileClicked");
} catch (final JSchException e) {
runOnUiThread(new Runnable() {
@SuppressLint("ShowToast")
@Override
public void run() {
Toast.makeText(
Listremote.this,
"Check Host, Username and Password "
+ e.getMessage().toString(),
10).show();
}
});

e.printStackTrace();
} catch (final SftpException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(
Listremote.this,
"error - server not responding"
+ e.getMessage().toString(),
Toast.LENGTH_LONG).show();
}
});
e.printStackTrace();

}


}private class Item {
public String file;
public int icon;

public Item(String file, Integer icon) {
this.file = file;
this.icon = icon;
}

@Override
public String toString() {
return file;
}
}

}