package net.nitroshare.android.explorer;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.nitroshare.android.R;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;

/**
 * Adapter for displaying directory contents
 */
class DirectoryAdapter extends ArrayAdapter<File> {

    private Context mContext;

    DirectoryAdapter(String directory, Context context) {
        super(context, R.layout.view_simple_list_item, android.R.id.text1);
        mContext = context;

        File[] files = new File(directory).listFiles();
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1.isDirectory() == o2.isDirectory()) {
                    return o1.getName().compareToIgnoreCase(o2.getName());
                } else {
                    return o1.isDirectory() ? -1 : 1;
                }
            }
        });
        for (File file : files) {
            add(file);
        }
    }

    private String getDirectorySummary(File directory) {
        int numItems = directory.listFiles().length;
        return mContext.getResources().getQuantityString(R.plurals.activity_explorer_folder, numItems, numItems);
    }

    private String getFileSummary(File file) {
        long size = file.length();
        if (size < 1000) {
            return mContext.getResources().getQuantityString(
                    R.plurals.activity_explorer_bytes, (int) size, size);
        } else if (size < 1000000) {
            return mContext.getString(R.string.activity_explorer_kb, size / 1000);
        } else if (size < 1000000000) {
            return mContext.getString(R.string.activity_explorer_mb, size / 1000000);
        } else {
            return mContext.getString(R.string.activity_explorer_gb, size / 1000000000);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = super.getView(position, convertView, parent);
        File file = getItem(position);
        ((TextView) convertView.findViewById(android.R.id.text1)).setText(file.getName());
        ((TextView) convertView.findViewById(android.R.id.text2)).setText(
                file.isDirectory() ? getDirectorySummary(file) : getFileSummary(file)
        );
        ((ImageView) convertView.findViewById(android.R.id.icon)).setImageResource(
                file.isDirectory() ? R.drawable.ic_folder : R.drawable.ic_file
        );
        return convertView;
    }
}

@@ -160,16 +160,86 @@
 dapter;%0A
+import android.widget.CheckBox;%0Aimport android.widget.CompoundButton;%0A
 import a
@@ -537,24 +537,56 @@
 xt mContext;
+%0A    private boolean%5B%5D mChecked;
 %0A%0A    Direct
@@ -686,16 +686,30 @@
 ist_item
+_with_checkbox
 , androi
@@ -1249,32 +1249,77 @@
 file);%0A        %7D
+%0A%0A        mChecked = new boolean%5BgetCount()%5D;
 %0A    %7D%0A%0A    priv
@@ -2193,16 +2193,22 @@
 getView(
+final 
 int posi
@@ -2259,24 +2259,22 @@
         
-convertV
+View v
 iew = su
@@ -2372,32 +2372,25 @@
 ((TextView) 
-convertV
+v
 iew.findView
@@ -2456,32 +2456,25 @@
 ((TextView) 
-convertV
+v
 iew.findView
@@ -2626,24 +2626,17 @@
 geView) 
-convertV
+v
 iew.find
@@ -2784,23 +2784,473 @@
     
-return convertV
+CheckBox checkBox = (CheckBox) view.findViewById(android.R.id.checkbox);%0A        checkBox.setOnCheckedChangeListener(null);%0A        checkBox.setChecked(mChecked%5Bposition%5D);%0A        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() %7B%0A            @Override%0A            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) %7B%0A                mChecked%5Bposition%5D = isChecked;%0A            %7D%0A        %7D);%0A        return v
 iew;

Add checkboxes to file explorer.