Android Open Source - TuxRemote File Selector Dialog






From Project

Back to project page TuxRemote.

License

The source code is released under:

GNU General Public License

If you think the Android project TuxRemote listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.tuxremote.app;
//  ww w .  j  ava2  s.c om
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.tuxremote.app.TuxeRemoteSsh.BashReturn;

import java.util.ArrayList;

public abstract class FileSelectorDialog extends Dialog {

    private final ProgressBar progress;
    private ArrayList<File> fileList;
    private FileListAdapter adapter;
    private ListView fileListView;
    private static String currentDir;
    private String currentParent;

    public FileSelectorDialog(Context context) {
        super(context);
        setContentView(R.layout.file_selector);
        setTitle("Selection fichier");
        progress = (ProgressBar) findViewById(R.id.progress);
        progress.setIndeterminate(true);
        progress.setVisibility(View.GONE);
        fileListView = (ListView) findViewById(R.id.file_list);
        fileList = new ArrayList<File>();
        adapter = new FileListAdapter(context, fileList);
        fileListView.setAdapter(adapter);
        fileListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                File file = fileList.get(i);
                if(file.isDir()){
                    String path = currentDir + file.getFileName();
                    loadFileList(path);
                }else if (file.getFileName().equals("..")){
                    loadFileList(currentParent);
                }else{
                    customItemClick(file, currentDir, currentParent);
                }
            }
        });
        SharedPreferences pref = TuxRemoteUtils.getPref(Global.getStaticContext());
        currentDir = (currentDir == null) ? pref.getString("file_selector_home", ""): currentDir;
        loadFileList(currentDir);
    }

    public abstract void customItemClick(File file, String currentDir, String currentParent);

    private void loadFileList(String dir){
        fileList.clear();
        SSHAsyncTask fileTask = new SSHAsyncTask(new Command("ls", "cd '"+dir+"' ; ls -1p . | awk -v p=\"$(pwd)\" -v q=\"$(cd .. ; pwd)\" 'BEGIN{print p \"\\n\" q}{print $0}'", null)){
            @Override
            protected void onPreExecute () {
                super.onPreExecute();
                try {
                    progress.setVisibility(View.VISIBLE);
                }catch (Exception e){e.printStackTrace();}
            }
            @Override
            protected void onProgressUpdate (BashReturn... prog) {
                try {
                    if (prog[0] != null) {
                        ArrayList<String> liste = prog[0].getBashReturn();
                        currentDir = liste.remove(0) + "/";
                        currentParent = liste.remove(0) + "/";
                        liste.add(0, "..");
                        for (String line : liste) {
                            fileList.add(getFileByLine(line));
                            adapter.notifyDataSetChanged();
                        }
                    }
                    fileListView.smoothScrollToPosition(0);
                    progress.setVisibility(View.GONE);
                }catch (Exception e){e.printStackTrace();}
            }
        };
        fileTask.execTask();
    }

    private File getFileByLine(String line){
        File file = new File(line, line, false);
        if(line.contains("/"))
            file.setDir(true);
        return file;
    }

    public static class File {
        private String filePath = null;
        private String fileName = null;
        private boolean isDir = false;
        public File(String name, String filePath,boolean is_dir){
            this.fileName = name;
            this.filePath = filePath;
            this.isDir = is_dir;
        }

        public boolean isDir() {
            return isDir;
        }

        public void setDir(boolean isDir) {
            this.isDir = isDir;
        }

        public String getFileName() {
            return fileName;
        }

        public String getFilePath() {
            return filePath;
        }
    }

    public static class FileListAdapter extends BaseAdapter {
        private final Context context;
        private ArrayList<File> files;

        public FileListAdapter(Context c, ArrayList<File> items){
            this.files = items;
            this.context = c;
        }
        @Override
        public int getCount() {
            return files.size();
        }

        @Override
        public Object getItem(int i) {
            return files.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // reuse views
            ViewHolder holder;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.row_file, parent, false);
                holder.text = (TextView) convertView.findViewById(R.id.label);
                convertView.setTag(holder);
            } else
                holder = (ViewHolder) convertView.getTag();
            // fill data
            File file = this.files.get(position);
            holder.text.setText(file.getFileName());
            if(file.isDir()){
                holder.text.setTextColor(context.getResources().getColor(R.color.dir_color));
            }else{
                holder.text.setTextColor(context.getResources().getColor(R.color.file_color));
            }

            return convertView;
        }

        private class ViewHolder {
            public TextView text;
        }
    }
}




Java Source Code List

com.tuxremote.app.AppFragment.java
com.tuxremote.app.AppListViewAdapter.java
com.tuxremote.app.App.java
com.tuxremote.app.CmdListViewAdapter.java
com.tuxremote.app.Command.java
com.tuxremote.app.ConfigXML.java
com.tuxremote.app.ConnectFragment.java
com.tuxremote.app.EmptyFragment.java
com.tuxremote.app.FileSelectorDialog.java
com.tuxremote.app.Global.java
com.tuxremote.app.MainActivity.java
com.tuxremote.app.NavigationDrawerFragment.java
com.tuxremote.app.Preference.java
com.tuxremote.app.SSHAsyncTask.java
com.tuxremote.app.Server.java
com.tuxremote.app.TuxRemoteUtils.java
com.tuxremote.app.TuxeRemoteSsh.BashReturn.java
com.tuxremote.app.TuxeRemoteSsh.MyUserInfo.java
com.tuxremote.app.TuxeRemoteSsh.SshSession.java