add directory size calculation to properties dialog

This commit is contained in:
tibbi 2016-09-06 22:07:05 +02:00
parent 3aaa5308ed
commit e5fe28cbc5
3 changed files with 55 additions and 11 deletions

View File

@ -14,6 +14,9 @@ import android.graphics.PorterDuffColorFilter;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;
import com.simplemobiletools.filemanager.models.FileDirItem;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -52,4 +55,18 @@ public class Utils {
final Matcher matcher = pattern.matcher(name);
return matcher.matches();
}
public static String getFormattedSize(FileDirItem item) {
final long size = item.getSize();
return formatSize(size);
}
public static String formatSize(long size) {
if (size <= 0)
return "0 B";
final String[] units = {"B", "kB", "MB", "GB", "TB"};
final int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
}

View File

@ -14,7 +14,6 @@ import com.simplemobiletools.filemanager.R;
import com.simplemobiletools.filemanager.Utils;
import com.simplemobiletools.filemanager.models.FileDirItem;
import java.text.DecimalFormat;
import java.util.List;
import butterknife.BindView;
@ -55,7 +54,7 @@ public class ItemsAdapter extends BaseAdapter {
viewHolder.details.setText(getChildrenCnt(item));
} else {
viewHolder.icon.setImageBitmap(mFileBmp);
viewHolder.details.setText(getFormattedSize(item));
viewHolder.details.setText(Utils.getFormattedSize(item));
}
return convertView;
@ -66,15 +65,6 @@ public class ItemsAdapter extends BaseAdapter {
return mRes.getQuantityString(R.plurals.items, children, children);
}
private String getFormattedSize(FileDirItem item) {
final long size = item.getSize();
if (size <= 0)
return "0 B";
final String[] units = {"B", "kB", "MB", "GB", "TB"};
final int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
@Override
public int getCount() {
return mItems.size();

View File

@ -5,10 +5,14 @@ import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;
import com.simplemobiletools.filemanager.R;
import com.simplemobiletools.filemanager.Utils;
import com.simplemobiletools.filemanager.models.FileDirItem;
import java.io.File;
public class PropertiesDialog extends DialogFragment {
private static FileDirItem mItem;
@ -25,7 +29,16 @@ public class PropertiesDialog extends DialogFragment {
if (mItem.getIsDirectory()) {
infoView.findViewById(R.id.properties_files_count_label).setVisibility(View.VISIBLE);
infoView.findViewById(R.id.properties_files_count).setVisibility(View.VISIBLE);
((TextView) infoView.findViewById(R.id.properties_files_count)).setText(String.valueOf(mItem.getChildren()));
}
((TextView) infoView.findViewById(R.id.properties_name)).setText(mItem.getName());
((TextView) infoView.findViewById(R.id.properties_path)).setText(mItem.getPath());
((TextView) infoView.findViewById(R.id.properties_size)).setText(getItemSize());
final File file = new File(mItem.getPath());
((TextView) infoView.findViewById(R.id.properties_last_modified)).setText(String.valueOf(file.lastModified()));
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(getResources().getString(title));
builder.setView(infoView);
@ -33,4 +46,28 @@ public class PropertiesDialog extends DialogFragment {
return builder.create();
}
private String getItemSize() {
if (mItem.getIsDirectory()) {
return Utils.formatSize(directorySize(new File(mItem.getPath())));
}
return Utils.getFormattedSize(mItem);
}
private long directorySize(File dir) {
if (dir.exists()) {
long size = 0;
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
size += directorySize(files[i]);
} else {
size += files[i].length();
}
}
return size;
}
return 0;
}
}